2018-02-19 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / gcc / builtins.c
blob683c6ec6e5b814ee2650f8a5bf538d4f394c7ae6
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 "tree-chkp.h"
68 #include "rtl-chkp.h"
69 #include "internal-fn.h"
70 #include "case-cfn-macros.h"
71 #include "gimple-fold.h"
72 #include "intl.h"
73 #include "file-prefix-map.h" /* remap_macro_filename() */
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_memcpy_with_bounds (tree, rtx);
127 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
128 rtx target, tree exp, int endp);
129 static rtx expand_builtin_memmove (tree, rtx);
130 static rtx expand_builtin_mempcpy (tree, rtx);
131 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx);
132 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, int);
133 static rtx expand_builtin_strcat (tree, rtx);
134 static rtx expand_builtin_strcpy (tree, rtx);
135 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
136 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
137 static rtx expand_builtin_stpncpy (tree, rtx);
138 static rtx expand_builtin_strncat (tree, rtx);
139 static rtx expand_builtin_strncpy (tree, rtx);
140 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, scalar_int_mode);
141 static rtx expand_builtin_memset (tree, rtx, machine_mode);
142 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
143 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
144 static rtx expand_builtin_bzero (tree);
145 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
146 static rtx expand_builtin_alloca (tree);
147 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
148 static rtx expand_builtin_frame_address (tree, tree);
149 static tree stabilize_va_list_loc (location_t, tree, int);
150 static rtx expand_builtin_expect (tree, rtx);
151 static tree fold_builtin_constant_p (tree);
152 static tree fold_builtin_classify_type (tree);
153 static tree fold_builtin_strlen (location_t, tree, tree);
154 static tree fold_builtin_inf (location_t, tree, int);
155 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
156 static bool validate_arg (const_tree, enum tree_code code);
157 static rtx expand_builtin_fabs (tree, rtx, rtx);
158 static rtx expand_builtin_signbit (tree, rtx);
159 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
160 static tree fold_builtin_isascii (location_t, tree);
161 static tree fold_builtin_toascii (location_t, tree);
162 static tree fold_builtin_isdigit (location_t, tree);
163 static tree fold_builtin_fabs (location_t, tree, tree);
164 static tree fold_builtin_abs (location_t, tree, tree);
165 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
166 enum tree_code);
167 static tree fold_builtin_0 (location_t, tree);
168 static tree fold_builtin_1 (location_t, tree, tree);
169 static tree fold_builtin_2 (location_t, tree, tree, tree);
170 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_varargs (location_t, tree, tree*, int);
173 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
174 static tree fold_builtin_strspn (location_t, tree, tree);
175 static tree fold_builtin_strcspn (location_t, tree, tree);
177 static rtx expand_builtin_object_size (tree);
178 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
179 enum built_in_function);
180 static void maybe_emit_chk_warning (tree, enum built_in_function);
181 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
182 static void maybe_emit_free_warning (tree);
183 static tree fold_builtin_object_size (tree, tree);
185 unsigned HOST_WIDE_INT target_newline;
186 unsigned HOST_WIDE_INT target_percent;
187 static unsigned HOST_WIDE_INT target_c;
188 static unsigned HOST_WIDE_INT target_s;
189 char target_percent_c[3];
190 char target_percent_s[3];
191 char target_percent_s_newline[4];
192 static tree do_mpfr_remquo (tree, tree, tree);
193 static tree do_mpfr_lgamma_r (tree, tree, tree);
194 static void expand_builtin_sync_synchronize (void);
196 /* Return true if NAME starts with __builtin_ or __sync_. */
198 static bool
199 is_builtin_name (const char *name)
201 if (strncmp (name, "__builtin_", 10) == 0)
202 return true;
203 if (strncmp (name, "__sync_", 7) == 0)
204 return true;
205 if (strncmp (name, "__atomic_", 9) == 0)
206 return true;
207 return false;
211 /* Return true if DECL is a function symbol representing a built-in. */
213 bool
214 is_builtin_fn (tree decl)
216 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
219 /* Return true if NODE should be considered for inline expansion regardless
220 of the optimization level. This means whenever a function is invoked with
221 its "internal" name, which normally contains the prefix "__builtin". */
223 bool
224 called_as_built_in (tree node)
226 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
227 we want the name used to call the function, not the name it
228 will have. */
229 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
230 return is_builtin_name (name);
233 /* Compute values M and N such that M divides (address of EXP - N) and such
234 that N < M. If these numbers can be determined, store M in alignp and N in
235 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
236 *alignp and any bit-offset to *bitposp.
238 Note that the address (and thus the alignment) computed here is based
239 on the address to which a symbol resolves, whereas DECL_ALIGN is based
240 on the address at which an object is actually located. These two
241 addresses are not always the same. For example, on ARM targets,
242 the address &foo of a Thumb function foo() has the lowest bit set,
243 whereas foo() itself starts on an even address.
245 If ADDR_P is true we are taking the address of the memory reference EXP
246 and thus cannot rely on the access taking place. */
248 static bool
249 get_object_alignment_2 (tree exp, unsigned int *alignp,
250 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
252 poly_int64 bitsize, bitpos;
253 tree offset;
254 machine_mode mode;
255 int unsignedp, reversep, volatilep;
256 unsigned int align = BITS_PER_UNIT;
257 bool known_alignment = false;
259 /* Get the innermost object and the constant (bitpos) and possibly
260 variable (offset) offset of the access. */
261 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
262 &unsignedp, &reversep, &volatilep);
264 /* Extract alignment information from the innermost object and
265 possibly adjust bitpos and offset. */
266 if (TREE_CODE (exp) == FUNCTION_DECL)
268 /* Function addresses can encode extra information besides their
269 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
270 allows the low bit to be used as a virtual bit, we know
271 that the address itself must be at least 2-byte aligned. */
272 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
273 align = 2 * BITS_PER_UNIT;
275 else if (TREE_CODE (exp) == LABEL_DECL)
277 else if (TREE_CODE (exp) == CONST_DECL)
279 /* The alignment of a CONST_DECL is determined by its initializer. */
280 exp = DECL_INITIAL (exp);
281 align = TYPE_ALIGN (TREE_TYPE (exp));
282 if (CONSTANT_CLASS_P (exp))
283 align = targetm.constant_alignment (exp, align);
285 known_alignment = true;
287 else if (DECL_P (exp))
289 align = DECL_ALIGN (exp);
290 known_alignment = true;
292 else if (TREE_CODE (exp) == INDIRECT_REF
293 || TREE_CODE (exp) == MEM_REF
294 || TREE_CODE (exp) == TARGET_MEM_REF)
296 tree addr = TREE_OPERAND (exp, 0);
297 unsigned ptr_align;
298 unsigned HOST_WIDE_INT ptr_bitpos;
299 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
301 /* If the address is explicitely aligned, handle that. */
302 if (TREE_CODE (addr) == BIT_AND_EXPR
303 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
305 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
306 ptr_bitmask *= BITS_PER_UNIT;
307 align = least_bit_hwi (ptr_bitmask);
308 addr = TREE_OPERAND (addr, 0);
311 known_alignment
312 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
313 align = MAX (ptr_align, align);
315 /* Re-apply explicit alignment to the bitpos. */
316 ptr_bitpos &= ptr_bitmask;
318 /* The alignment of the pointer operand in a TARGET_MEM_REF
319 has to take the variable offset parts into account. */
320 if (TREE_CODE (exp) == TARGET_MEM_REF)
322 if (TMR_INDEX (exp))
324 unsigned HOST_WIDE_INT step = 1;
325 if (TMR_STEP (exp))
326 step = TREE_INT_CST_LOW (TMR_STEP (exp));
327 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
329 if (TMR_INDEX2 (exp))
330 align = BITS_PER_UNIT;
331 known_alignment = false;
334 /* When EXP is an actual memory reference then we can use
335 TYPE_ALIGN of a pointer indirection to derive alignment.
336 Do so only if get_pointer_alignment_1 did not reveal absolute
337 alignment knowledge and if using that alignment would
338 improve the situation. */
339 unsigned int talign;
340 if (!addr_p && !known_alignment
341 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
342 && talign > align)
343 align = talign;
344 else
346 /* Else adjust bitpos accordingly. */
347 bitpos += ptr_bitpos;
348 if (TREE_CODE (exp) == MEM_REF
349 || TREE_CODE (exp) == TARGET_MEM_REF)
350 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
353 else if (TREE_CODE (exp) == STRING_CST)
355 /* STRING_CST are the only constant objects we allow to be not
356 wrapped inside a CONST_DECL. */
357 align = TYPE_ALIGN (TREE_TYPE (exp));
358 if (CONSTANT_CLASS_P (exp))
359 align = targetm.constant_alignment (exp, align);
361 known_alignment = true;
364 /* If there is a non-constant offset part extract the maximum
365 alignment that can prevail. */
366 if (offset)
368 unsigned int trailing_zeros = tree_ctz (offset);
369 if (trailing_zeros < HOST_BITS_PER_INT)
371 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
372 if (inner)
373 align = MIN (align, inner);
377 /* Account for the alignment of runtime coefficients, so that the constant
378 bitpos is guaranteed to be accurate. */
379 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
380 if (alt_align != 0 && alt_align < align)
382 align = alt_align;
383 known_alignment = false;
386 *alignp = align;
387 *bitposp = bitpos.coeffs[0] & (align - 1);
388 return known_alignment;
391 /* For a memory reference expression EXP compute values M and N such that M
392 divides (&EXP - N) and such that N < M. If these numbers can be determined,
393 store M in alignp and N in *BITPOSP and return true. Otherwise return false
394 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
396 bool
397 get_object_alignment_1 (tree exp, unsigned int *alignp,
398 unsigned HOST_WIDE_INT *bitposp)
400 return get_object_alignment_2 (exp, alignp, bitposp, false);
403 /* Return the alignment in bits of EXP, an object. */
405 unsigned int
406 get_object_alignment (tree exp)
408 unsigned HOST_WIDE_INT bitpos = 0;
409 unsigned int align;
411 get_object_alignment_1 (exp, &align, &bitpos);
413 /* align and bitpos now specify known low bits of the pointer.
414 ptr & (align - 1) == bitpos. */
416 if (bitpos != 0)
417 align = least_bit_hwi (bitpos);
418 return align;
421 /* For a pointer valued expression EXP compute values M and N such that M
422 divides (EXP - N) and such that N < M. If these numbers can be determined,
423 store M in alignp and N in *BITPOSP and return true. Return false if
424 the results are just a conservative approximation.
426 If EXP is not a pointer, false is returned too. */
428 bool
429 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
430 unsigned HOST_WIDE_INT *bitposp)
432 STRIP_NOPS (exp);
434 if (TREE_CODE (exp) == ADDR_EXPR)
435 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
436 alignp, bitposp, true);
437 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
439 unsigned int align;
440 unsigned HOST_WIDE_INT bitpos;
441 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
442 &align, &bitpos);
443 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
444 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
445 else
447 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
448 if (trailing_zeros < HOST_BITS_PER_INT)
450 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
451 if (inner)
452 align = MIN (align, inner);
455 *alignp = align;
456 *bitposp = bitpos & (align - 1);
457 return res;
459 else if (TREE_CODE (exp) == SSA_NAME
460 && POINTER_TYPE_P (TREE_TYPE (exp)))
462 unsigned int ptr_align, ptr_misalign;
463 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
465 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
467 *bitposp = ptr_misalign * BITS_PER_UNIT;
468 *alignp = ptr_align * BITS_PER_UNIT;
469 /* Make sure to return a sensible alignment when the multiplication
470 by BITS_PER_UNIT overflowed. */
471 if (*alignp == 0)
472 *alignp = 1u << (HOST_BITS_PER_INT - 1);
473 /* We cannot really tell whether this result is an approximation. */
474 return false;
476 else
478 *bitposp = 0;
479 *alignp = BITS_PER_UNIT;
480 return false;
483 else if (TREE_CODE (exp) == INTEGER_CST)
485 *alignp = BIGGEST_ALIGNMENT;
486 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
487 & (BIGGEST_ALIGNMENT - 1));
488 return true;
491 *bitposp = 0;
492 *alignp = BITS_PER_UNIT;
493 return false;
496 /* Return the alignment in bits of EXP, a pointer valued expression.
497 The alignment returned is, by default, the alignment of the thing that
498 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
500 Otherwise, look at the expression to see if we can do better, i.e., if the
501 expression is actually pointing at an object whose alignment is tighter. */
503 unsigned int
504 get_pointer_alignment (tree exp)
506 unsigned HOST_WIDE_INT bitpos = 0;
507 unsigned int align;
509 get_pointer_alignment_1 (exp, &align, &bitpos);
511 /* align and bitpos now specify known low bits of the pointer.
512 ptr & (align - 1) == bitpos. */
514 if (bitpos != 0)
515 align = least_bit_hwi (bitpos);
517 return align;
520 /* Return the number of non-zero elements in the sequence
521 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
522 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
524 static unsigned
525 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
527 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
529 unsigned n;
531 if (eltsize == 1)
533 /* Optimize the common case of plain char. */
534 for (n = 0; n < maxelts; n++)
536 const char *elt = (const char*) ptr + n;
537 if (!*elt)
538 break;
541 else
543 for (n = 0; n < maxelts; n++)
545 const char *elt = (const char*) ptr + n * eltsize;
546 if (!memcmp (elt, "\0\0\0\0", eltsize))
547 break;
550 return n;
553 /* Compute the length of a null-terminated character string or wide
554 character string handling character sizes of 1, 2, and 4 bytes.
555 TREE_STRING_LENGTH is not the right way because it evaluates to
556 the size of the character array in bytes (as opposed to characters)
557 and because it can contain a zero byte in the middle.
559 ONLY_VALUE should be nonzero if the result is not going to be emitted
560 into the instruction stream and zero if it is going to be expanded.
561 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
562 is returned, otherwise NULL, since
563 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
564 evaluate the side-effects.
566 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
567 accesses. Note that this implies the result is not going to be emitted
568 into the instruction stream.
570 The value returned is of type `ssizetype'.
572 Unfortunately, string_constant can't access the values of const char
573 arrays with initializers, so neither can we do so here. */
575 tree
576 c_strlen (tree src, int only_value)
578 STRIP_NOPS (src);
579 if (TREE_CODE (src) == COND_EXPR
580 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
582 tree len1, len2;
584 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
585 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
586 if (tree_int_cst_equal (len1, len2))
587 return len1;
590 if (TREE_CODE (src) == COMPOUND_EXPR
591 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 return c_strlen (TREE_OPERAND (src, 1), only_value);
594 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
596 /* Offset from the beginning of the string in bytes. */
597 tree byteoff;
598 src = string_constant (src, &byteoff);
599 if (src == 0)
600 return NULL_TREE;
602 /* Determine the size of the string element. */
603 unsigned eltsize
604 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))));
606 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
607 length of SRC. */
608 unsigned maxelts = TREE_STRING_LENGTH (src) / eltsize - 1;
610 /* PTR can point to the byte representation of any string type, including
611 char* and wchar_t*. */
612 const char *ptr = TREE_STRING_POINTER (src);
614 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
616 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
617 compute the offset to the following null if we don't know where to
618 start searching for it. */
619 if (string_length (ptr, eltsize, maxelts) < maxelts)
621 /* Return when an embedded null character is found. */
622 return NULL_TREE;
625 if (!maxelts)
626 return ssize_int (0);
628 /* We don't know the starting offset, but we do know that the string
629 has no internal zero bytes. We can assume that the offset falls
630 within the bounds of the string; otherwise, the programmer deserves
631 what he gets. Subtract the offset from the length of the string,
632 and return that. This would perhaps not be valid if we were dealing
633 with named arrays in addition to literal string constants. */
635 return size_diffop_loc (loc, size_int (maxelts * eltsize), byteoff);
638 /* Offset from the beginning of the string in elements. */
639 HOST_WIDE_INT eltoff;
641 /* We have a known offset into the string. Start searching there for
642 a null character if we can represent it as a single HOST_WIDE_INT. */
643 if (byteoff == 0)
644 eltoff = 0;
645 else if (! tree_fits_shwi_p (byteoff))
646 eltoff = -1;
647 else
648 eltoff = tree_to_shwi (byteoff) / eltsize;
650 /* If the offset is known to be out of bounds, warn, and call strlen at
651 runtime. */
652 if (eltoff < 0 || eltoff > maxelts)
654 /* Suppress multiple warnings for propagated constant strings. */
655 if (only_value != 2
656 && !TREE_NO_WARNING (src))
658 warning_at (loc, OPT_Warray_bounds,
659 "offset %qwi outside bounds of constant string",
660 eltoff);
661 TREE_NO_WARNING (src) = 1;
663 return NULL_TREE;
666 /* Use strlen to search for the first zero byte. Since any strings
667 constructed with build_string will have nulls appended, we win even
668 if we get handed something like (char[4])"abcd".
670 Since ELTOFF is our starting index into the string, no further
671 calculation is needed. */
672 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
673 maxelts - eltoff);
675 return ssize_int (len);
678 /* Return a constant integer corresponding to target reading
679 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
681 static rtx
682 c_readstr (const char *str, scalar_int_mode mode)
684 HOST_WIDE_INT ch;
685 unsigned int i, j;
686 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
688 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
689 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
690 / HOST_BITS_PER_WIDE_INT;
692 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
693 for (i = 0; i < len; i++)
694 tmp[i] = 0;
696 ch = 1;
697 for (i = 0; i < GET_MODE_SIZE (mode); i++)
699 j = i;
700 if (WORDS_BIG_ENDIAN)
701 j = GET_MODE_SIZE (mode) - i - 1;
702 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
703 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
704 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
705 j *= BITS_PER_UNIT;
707 if (ch)
708 ch = (unsigned char) str[i];
709 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
712 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
713 return immed_wide_int_const (c, mode);
716 /* Cast a target constant CST to target CHAR and if that value fits into
717 host char type, return zero and put that value into variable pointed to by
718 P. */
720 static int
721 target_char_cast (tree cst, char *p)
723 unsigned HOST_WIDE_INT val, hostval;
725 if (TREE_CODE (cst) != INTEGER_CST
726 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
727 return 1;
729 /* Do not care if it fits or not right here. */
730 val = TREE_INT_CST_LOW (cst);
732 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
733 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
735 hostval = val;
736 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
737 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
739 if (val != hostval)
740 return 1;
742 *p = hostval;
743 return 0;
746 /* Similar to save_expr, but assumes that arbitrary code is not executed
747 in between the multiple evaluations. In particular, we assume that a
748 non-addressable local variable will not be modified. */
750 static tree
751 builtin_save_expr (tree exp)
753 if (TREE_CODE (exp) == SSA_NAME
754 || (TREE_ADDRESSABLE (exp) == 0
755 && (TREE_CODE (exp) == PARM_DECL
756 || (VAR_P (exp) && !TREE_STATIC (exp)))))
757 return exp;
759 return save_expr (exp);
762 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
763 times to get the address of either a higher stack frame, or a return
764 address located within it (depending on FNDECL_CODE). */
766 static rtx
767 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
769 int i;
770 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
771 if (tem == NULL_RTX)
773 /* For a zero count with __builtin_return_address, we don't care what
774 frame address we return, because target-specific definitions will
775 override us. Therefore frame pointer elimination is OK, and using
776 the soft frame pointer is OK.
778 For a nonzero count, or a zero count with __builtin_frame_address,
779 we require a stable offset from the current frame pointer to the
780 previous one, so we must use the hard frame pointer, and
781 we must disable frame pointer elimination. */
782 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
783 tem = frame_pointer_rtx;
784 else
786 tem = hard_frame_pointer_rtx;
788 /* Tell reload not to eliminate the frame pointer. */
789 crtl->accesses_prior_frames = 1;
793 if (count > 0)
794 SETUP_FRAME_ADDRESSES ();
796 /* On the SPARC, the return address is not in the frame, it is in a
797 register. There is no way to access it off of the current frame
798 pointer, but it can be accessed off the previous frame pointer by
799 reading the value from the register window save area. */
800 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
801 count--;
803 /* Scan back COUNT frames to the specified frame. */
804 for (i = 0; i < count; i++)
806 /* Assume the dynamic chain pointer is in the word that the
807 frame address points to, unless otherwise specified. */
808 tem = DYNAMIC_CHAIN_ADDRESS (tem);
809 tem = memory_address (Pmode, tem);
810 tem = gen_frame_mem (Pmode, tem);
811 tem = copy_to_reg (tem);
814 /* For __builtin_frame_address, return what we've got. But, on
815 the SPARC for example, we may have to add a bias. */
816 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
817 return FRAME_ADDR_RTX (tem);
819 /* For __builtin_return_address, get the return address from that frame. */
820 #ifdef RETURN_ADDR_RTX
821 tem = RETURN_ADDR_RTX (count, tem);
822 #else
823 tem = memory_address (Pmode,
824 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
825 tem = gen_frame_mem (Pmode, tem);
826 #endif
827 return tem;
830 /* Alias set used for setjmp buffer. */
831 static alias_set_type setjmp_alias_set = -1;
833 /* Construct the leading half of a __builtin_setjmp call. Control will
834 return to RECEIVER_LABEL. This is also called directly by the SJLJ
835 exception handling code. */
837 void
838 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
840 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
841 rtx stack_save;
842 rtx mem;
844 if (setjmp_alias_set == -1)
845 setjmp_alias_set = new_alias_set ();
847 buf_addr = convert_memory_address (Pmode, buf_addr);
849 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
851 /* We store the frame pointer and the address of receiver_label in
852 the buffer and use the rest of it for the stack save area, which
853 is machine-dependent. */
855 mem = gen_rtx_MEM (Pmode, buf_addr);
856 set_mem_alias_set (mem, setjmp_alias_set);
857 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
859 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
860 GET_MODE_SIZE (Pmode))),
861 set_mem_alias_set (mem, setjmp_alias_set);
863 emit_move_insn (validize_mem (mem),
864 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
866 stack_save = gen_rtx_MEM (sa_mode,
867 plus_constant (Pmode, buf_addr,
868 2 * GET_MODE_SIZE (Pmode)));
869 set_mem_alias_set (stack_save, setjmp_alias_set);
870 emit_stack_save (SAVE_NONLOCAL, &stack_save);
872 /* If there is further processing to do, do it. */
873 if (targetm.have_builtin_setjmp_setup ())
874 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
876 /* We have a nonlocal label. */
877 cfun->has_nonlocal_label = 1;
880 /* Construct the trailing part of a __builtin_setjmp call. This is
881 also called directly by the SJLJ exception handling code.
882 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
884 void
885 expand_builtin_setjmp_receiver (rtx receiver_label)
887 rtx chain;
889 /* Mark the FP as used when we get here, so we have to make sure it's
890 marked as used by this function. */
891 emit_use (hard_frame_pointer_rtx);
893 /* Mark the static chain as clobbered here so life information
894 doesn't get messed up for it. */
895 chain = rtx_for_static_chain (current_function_decl, true);
896 if (chain && REG_P (chain))
897 emit_clobber (chain);
899 /* Now put in the code to restore the frame pointer, and argument
900 pointer, if needed. */
901 if (! targetm.have_nonlocal_goto ())
903 /* First adjust our frame pointer to its actual value. It was
904 previously set to the start of the virtual area corresponding to
905 the stacked variables when we branched here and now needs to be
906 adjusted to the actual hardware fp value.
908 Assignments to virtual registers are converted by
909 instantiate_virtual_regs into the corresponding assignment
910 to the underlying register (fp in this case) that makes
911 the original assignment true.
912 So the following insn will actually be decrementing fp by
913 TARGET_STARTING_FRAME_OFFSET. */
914 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
916 /* Restoring the frame pointer also modifies the hard frame pointer.
917 Mark it used (so that the previous assignment remains live once
918 the frame pointer is eliminated) and clobbered (to represent the
919 implicit update from the assignment). */
920 emit_use (hard_frame_pointer_rtx);
921 emit_clobber (hard_frame_pointer_rtx);
924 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
926 /* If the argument pointer can be eliminated in favor of the
927 frame pointer, we don't need to restore it. We assume here
928 that if such an elimination is present, it can always be used.
929 This is the case on all known machines; if we don't make this
930 assumption, we do unnecessary saving on many machines. */
931 size_t i;
932 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
934 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
935 if (elim_regs[i].from == ARG_POINTER_REGNUM
936 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
937 break;
939 if (i == ARRAY_SIZE (elim_regs))
941 /* Now restore our arg pointer from the address at which it
942 was saved in our stack frame. */
943 emit_move_insn (crtl->args.internal_arg_pointer,
944 copy_to_reg (get_arg_pointer_save_area ()));
948 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
949 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
950 else if (targetm.have_nonlocal_goto_receiver ())
951 emit_insn (targetm.gen_nonlocal_goto_receiver ());
952 else
953 { /* Nothing */ }
955 /* We must not allow the code we just generated to be reordered by
956 scheduling. Specifically, the update of the frame pointer must
957 happen immediately, not later. */
958 emit_insn (gen_blockage ());
961 /* __builtin_longjmp is passed a pointer to an array of five words (not
962 all will be used on all machines). It operates similarly to the C
963 library function of the same name, but is more efficient. Much of
964 the code below is copied from the handling of non-local gotos. */
966 static void
967 expand_builtin_longjmp (rtx buf_addr, rtx value)
969 rtx fp, lab, stack;
970 rtx_insn *insn, *last;
971 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
973 /* DRAP is needed for stack realign if longjmp is expanded to current
974 function */
975 if (SUPPORTS_STACK_ALIGNMENT)
976 crtl->need_drap = true;
978 if (setjmp_alias_set == -1)
979 setjmp_alias_set = new_alias_set ();
981 buf_addr = convert_memory_address (Pmode, buf_addr);
983 buf_addr = force_reg (Pmode, buf_addr);
985 /* We require that the user must pass a second argument of 1, because
986 that is what builtin_setjmp will return. */
987 gcc_assert (value == const1_rtx);
989 last = get_last_insn ();
990 if (targetm.have_builtin_longjmp ())
991 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
992 else
994 fp = gen_rtx_MEM (Pmode, buf_addr);
995 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
996 GET_MODE_SIZE (Pmode)));
998 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
999 2 * GET_MODE_SIZE (Pmode)));
1000 set_mem_alias_set (fp, setjmp_alias_set);
1001 set_mem_alias_set (lab, setjmp_alias_set);
1002 set_mem_alias_set (stack, setjmp_alias_set);
1004 /* Pick up FP, label, and SP from the block and jump. This code is
1005 from expand_goto in stmt.c; see there for detailed comments. */
1006 if (targetm.have_nonlocal_goto ())
1007 /* We have to pass a value to the nonlocal_goto pattern that will
1008 get copied into the static_chain pointer, but it does not matter
1009 what that value is, because builtin_setjmp does not use it. */
1010 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1011 else
1013 lab = copy_to_reg (lab);
1015 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1016 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1018 emit_move_insn (hard_frame_pointer_rtx, fp);
1019 emit_stack_restore (SAVE_NONLOCAL, stack);
1021 emit_use (hard_frame_pointer_rtx);
1022 emit_use (stack_pointer_rtx);
1023 emit_indirect_jump (lab);
1027 /* Search backwards and mark the jump insn as a non-local goto.
1028 Note that this precludes the use of __builtin_longjmp to a
1029 __builtin_setjmp target in the same function. However, we've
1030 already cautioned the user that these functions are for
1031 internal exception handling use only. */
1032 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1034 gcc_assert (insn != last);
1036 if (JUMP_P (insn))
1038 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1039 break;
1041 else if (CALL_P (insn))
1042 break;
1046 static inline bool
1047 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1049 return (iter->i < iter->n);
1052 /* This function validates the types of a function call argument list
1053 against a specified list of tree_codes. If the last specifier is a 0,
1054 that represents an ellipsis, otherwise the last specifier must be a
1055 VOID_TYPE. */
1057 static bool
1058 validate_arglist (const_tree callexpr, ...)
1060 enum tree_code code;
1061 bool res = 0;
1062 va_list ap;
1063 const_call_expr_arg_iterator iter;
1064 const_tree arg;
1066 va_start (ap, callexpr);
1067 init_const_call_expr_arg_iterator (callexpr, &iter);
1069 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1070 tree fn = CALL_EXPR_FN (callexpr);
1071 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1073 for (unsigned argno = 1; ; ++argno)
1075 code = (enum tree_code) va_arg (ap, int);
1077 switch (code)
1079 case 0:
1080 /* This signifies an ellipses, any further arguments are all ok. */
1081 res = true;
1082 goto end;
1083 case VOID_TYPE:
1084 /* This signifies an endlink, if no arguments remain, return
1085 true, otherwise return false. */
1086 res = !more_const_call_expr_args_p (&iter);
1087 goto end;
1088 case POINTER_TYPE:
1089 /* The actual argument must be nonnull when either the whole
1090 called function has been declared nonnull, or when the formal
1091 argument corresponding to the actual argument has been. */
1092 if (argmap
1093 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1095 arg = next_const_call_expr_arg (&iter);
1096 if (!validate_arg (arg, code) || integer_zerop (arg))
1097 goto end;
1098 break;
1100 /* FALLTHRU */
1101 default:
1102 /* If no parameters remain or the parameter's code does not
1103 match the specified code, return false. Otherwise continue
1104 checking any remaining arguments. */
1105 arg = next_const_call_expr_arg (&iter);
1106 if (!validate_arg (arg, code))
1107 goto end;
1108 break;
1112 /* We need gotos here since we can only have one VA_CLOSE in a
1113 function. */
1114 end: ;
1115 va_end (ap);
1117 BITMAP_FREE (argmap);
1119 return res;
1122 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1123 and the address of the save area. */
1125 static rtx
1126 expand_builtin_nonlocal_goto (tree exp)
1128 tree t_label, t_save_area;
1129 rtx r_label, r_save_area, r_fp, r_sp;
1130 rtx_insn *insn;
1132 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1133 return NULL_RTX;
1135 t_label = CALL_EXPR_ARG (exp, 0);
1136 t_save_area = CALL_EXPR_ARG (exp, 1);
1138 r_label = expand_normal (t_label);
1139 r_label = convert_memory_address (Pmode, r_label);
1140 r_save_area = expand_normal (t_save_area);
1141 r_save_area = convert_memory_address (Pmode, r_save_area);
1142 /* Copy the address of the save location to a register just in case it was
1143 based on the frame pointer. */
1144 r_save_area = copy_to_reg (r_save_area);
1145 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1146 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1147 plus_constant (Pmode, r_save_area,
1148 GET_MODE_SIZE (Pmode)));
1150 crtl->has_nonlocal_goto = 1;
1152 /* ??? We no longer need to pass the static chain value, afaik. */
1153 if (targetm.have_nonlocal_goto ())
1154 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1155 else
1157 r_label = copy_to_reg (r_label);
1159 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1160 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1162 /* Restore frame pointer for containing function. */
1163 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1164 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1166 /* USE of hard_frame_pointer_rtx added for consistency;
1167 not clear if really needed. */
1168 emit_use (hard_frame_pointer_rtx);
1169 emit_use (stack_pointer_rtx);
1171 /* If the architecture is using a GP register, we must
1172 conservatively assume that the target function makes use of it.
1173 The prologue of functions with nonlocal gotos must therefore
1174 initialize the GP register to the appropriate value, and we
1175 must then make sure that this value is live at the point
1176 of the jump. (Note that this doesn't necessarily apply
1177 to targets with a nonlocal_goto pattern; they are free
1178 to implement it in their own way. Note also that this is
1179 a no-op if the GP register is a global invariant.) */
1180 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1181 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1182 emit_use (pic_offset_table_rtx);
1184 emit_indirect_jump (r_label);
1187 /* Search backwards to the jump insn and mark it as a
1188 non-local goto. */
1189 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1191 if (JUMP_P (insn))
1193 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1194 break;
1196 else if (CALL_P (insn))
1197 break;
1200 return const0_rtx;
1203 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1204 (not all will be used on all machines) that was passed to __builtin_setjmp.
1205 It updates the stack pointer in that block to the current value. This is
1206 also called directly by the SJLJ exception handling code. */
1208 void
1209 expand_builtin_update_setjmp_buf (rtx buf_addr)
1211 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1212 buf_addr = convert_memory_address (Pmode, buf_addr);
1213 rtx stack_save
1214 = gen_rtx_MEM (sa_mode,
1215 memory_address
1216 (sa_mode,
1217 plus_constant (Pmode, buf_addr,
1218 2 * GET_MODE_SIZE (Pmode))));
1220 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1223 /* Expand a call to __builtin_prefetch. For a target that does not support
1224 data prefetch, evaluate the memory address argument in case it has side
1225 effects. */
1227 static void
1228 expand_builtin_prefetch (tree exp)
1230 tree arg0, arg1, arg2;
1231 int nargs;
1232 rtx op0, op1, op2;
1234 if (!validate_arglist (exp, POINTER_TYPE, 0))
1235 return;
1237 arg0 = CALL_EXPR_ARG (exp, 0);
1239 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1240 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1241 locality). */
1242 nargs = call_expr_nargs (exp);
1243 if (nargs > 1)
1244 arg1 = CALL_EXPR_ARG (exp, 1);
1245 else
1246 arg1 = integer_zero_node;
1247 if (nargs > 2)
1248 arg2 = CALL_EXPR_ARG (exp, 2);
1249 else
1250 arg2 = integer_three_node;
1252 /* Argument 0 is an address. */
1253 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1255 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1256 if (TREE_CODE (arg1) != INTEGER_CST)
1258 error ("second argument to %<__builtin_prefetch%> must be a constant");
1259 arg1 = integer_zero_node;
1261 op1 = expand_normal (arg1);
1262 /* Argument 1 must be either zero or one. */
1263 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1265 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1266 " using zero");
1267 op1 = const0_rtx;
1270 /* Argument 2 (locality) must be a compile-time constant int. */
1271 if (TREE_CODE (arg2) != INTEGER_CST)
1273 error ("third argument to %<__builtin_prefetch%> must be a constant");
1274 arg2 = integer_zero_node;
1276 op2 = expand_normal (arg2);
1277 /* Argument 2 must be 0, 1, 2, or 3. */
1278 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1280 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1281 op2 = const0_rtx;
1284 if (targetm.have_prefetch ())
1286 struct expand_operand ops[3];
1288 create_address_operand (&ops[0], op0);
1289 create_integer_operand (&ops[1], INTVAL (op1));
1290 create_integer_operand (&ops[2], INTVAL (op2));
1291 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1292 return;
1295 /* Don't do anything with direct references to volatile memory, but
1296 generate code to handle other side effects. */
1297 if (!MEM_P (op0) && side_effects_p (op0))
1298 emit_insn (op0);
1301 /* Get a MEM rtx for expression EXP which is the address of an operand
1302 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1303 the maximum length of the block of memory that might be accessed or
1304 NULL if unknown. */
1306 static rtx
1307 get_memory_rtx (tree exp, tree len)
1309 tree orig_exp = exp;
1310 rtx addr, mem;
1312 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1313 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1314 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1315 exp = TREE_OPERAND (exp, 0);
1317 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1318 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1320 /* Get an expression we can use to find the attributes to assign to MEM.
1321 First remove any nops. */
1322 while (CONVERT_EXPR_P (exp)
1323 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1324 exp = TREE_OPERAND (exp, 0);
1326 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1327 (as builtin stringops may alias with anything). */
1328 exp = fold_build2 (MEM_REF,
1329 build_array_type (char_type_node,
1330 build_range_type (sizetype,
1331 size_one_node, len)),
1332 exp, build_int_cst (ptr_type_node, 0));
1334 /* If the MEM_REF has no acceptable address, try to get the base object
1335 from the original address we got, and build an all-aliasing
1336 unknown-sized access to that one. */
1337 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1338 set_mem_attributes (mem, exp, 0);
1339 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1340 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1341 0))))
1343 exp = build_fold_addr_expr (exp);
1344 exp = fold_build2 (MEM_REF,
1345 build_array_type (char_type_node,
1346 build_range_type (sizetype,
1347 size_zero_node,
1348 NULL)),
1349 exp, build_int_cst (ptr_type_node, 0));
1350 set_mem_attributes (mem, exp, 0);
1352 set_mem_alias_set (mem, 0);
1353 return mem;
1356 /* Built-in functions to perform an untyped call and return. */
1358 #define apply_args_mode \
1359 (this_target_builtins->x_apply_args_mode)
1360 #define apply_result_mode \
1361 (this_target_builtins->x_apply_result_mode)
1363 /* Return the size required for the block returned by __builtin_apply_args,
1364 and initialize apply_args_mode. */
1366 static int
1367 apply_args_size (void)
1369 static int size = -1;
1370 int align;
1371 unsigned int regno;
1373 /* The values computed by this function never change. */
1374 if (size < 0)
1376 /* The first value is the incoming arg-pointer. */
1377 size = GET_MODE_SIZE (Pmode);
1379 /* The second value is the structure value address unless this is
1380 passed as an "invisible" first argument. */
1381 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1382 size += GET_MODE_SIZE (Pmode);
1384 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1385 if (FUNCTION_ARG_REGNO_P (regno))
1387 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1389 gcc_assert (mode != VOIDmode);
1391 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1392 if (size % align != 0)
1393 size = CEIL (size, align) * align;
1394 size += GET_MODE_SIZE (mode);
1395 apply_args_mode[regno] = mode;
1397 else
1399 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1402 return size;
1405 /* Return the size required for the block returned by __builtin_apply,
1406 and initialize apply_result_mode. */
1408 static int
1409 apply_result_size (void)
1411 static int size = -1;
1412 int align, regno;
1414 /* The values computed by this function never change. */
1415 if (size < 0)
1417 size = 0;
1419 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1420 if (targetm.calls.function_value_regno_p (regno))
1422 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1424 gcc_assert (mode != VOIDmode);
1426 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1427 if (size % align != 0)
1428 size = CEIL (size, align) * align;
1429 size += GET_MODE_SIZE (mode);
1430 apply_result_mode[regno] = mode;
1432 else
1433 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1435 /* Allow targets that use untyped_call and untyped_return to override
1436 the size so that machine-specific information can be stored here. */
1437 #ifdef APPLY_RESULT_SIZE
1438 size = APPLY_RESULT_SIZE;
1439 #endif
1441 return size;
1444 /* Create a vector describing the result block RESULT. If SAVEP is true,
1445 the result block is used to save the values; otherwise it is used to
1446 restore the values. */
1448 static rtx
1449 result_vector (int savep, rtx result)
1451 int regno, size, align, nelts;
1452 fixed_size_mode mode;
1453 rtx reg, mem;
1454 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1456 size = nelts = 0;
1457 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1458 if ((mode = apply_result_mode[regno]) != VOIDmode)
1460 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1461 if (size % align != 0)
1462 size = CEIL (size, align) * align;
1463 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1464 mem = adjust_address (result, mode, size);
1465 savevec[nelts++] = (savep
1466 ? gen_rtx_SET (mem, reg)
1467 : gen_rtx_SET (reg, mem));
1468 size += GET_MODE_SIZE (mode);
1470 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1473 /* Save the state required to perform an untyped call with the same
1474 arguments as were passed to the current function. */
1476 static rtx
1477 expand_builtin_apply_args_1 (void)
1479 rtx registers, tem;
1480 int size, align, regno;
1481 fixed_size_mode mode;
1482 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1484 /* Create a block where the arg-pointer, structure value address,
1485 and argument registers can be saved. */
1486 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1488 /* Walk past the arg-pointer and structure value address. */
1489 size = GET_MODE_SIZE (Pmode);
1490 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1491 size += GET_MODE_SIZE (Pmode);
1493 /* Save each register used in calling a function to the block. */
1494 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1495 if ((mode = apply_args_mode[regno]) != VOIDmode)
1497 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1498 if (size % align != 0)
1499 size = CEIL (size, align) * align;
1501 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1503 emit_move_insn (adjust_address (registers, mode, size), tem);
1504 size += GET_MODE_SIZE (mode);
1507 /* Save the arg pointer to the block. */
1508 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1509 /* We need the pointer as the caller actually passed them to us, not
1510 as we might have pretended they were passed. Make sure it's a valid
1511 operand, as emit_move_insn isn't expected to handle a PLUS. */
1512 if (STACK_GROWS_DOWNWARD)
1514 = force_operand (plus_constant (Pmode, tem,
1515 crtl->args.pretend_args_size),
1516 NULL_RTX);
1517 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1519 size = GET_MODE_SIZE (Pmode);
1521 /* Save the structure value address unless this is passed as an
1522 "invisible" first argument. */
1523 if (struct_incoming_value)
1525 emit_move_insn (adjust_address (registers, Pmode, size),
1526 copy_to_reg (struct_incoming_value));
1527 size += GET_MODE_SIZE (Pmode);
1530 /* Return the address of the block. */
1531 return copy_addr_to_reg (XEXP (registers, 0));
1534 /* __builtin_apply_args returns block of memory allocated on
1535 the stack into which is stored the arg pointer, structure
1536 value address, static chain, and all the registers that might
1537 possibly be used in performing a function call. The code is
1538 moved to the start of the function so the incoming values are
1539 saved. */
1541 static rtx
1542 expand_builtin_apply_args (void)
1544 /* Don't do __builtin_apply_args more than once in a function.
1545 Save the result of the first call and reuse it. */
1546 if (apply_args_value != 0)
1547 return apply_args_value;
1549 /* When this function is called, it means that registers must be
1550 saved on entry to this function. So we migrate the
1551 call to the first insn of this function. */
1552 rtx temp;
1554 start_sequence ();
1555 temp = expand_builtin_apply_args_1 ();
1556 rtx_insn *seq = get_insns ();
1557 end_sequence ();
1559 apply_args_value = temp;
1561 /* Put the insns after the NOTE that starts the function.
1562 If this is inside a start_sequence, make the outer-level insn
1563 chain current, so the code is placed at the start of the
1564 function. If internal_arg_pointer is a non-virtual pseudo,
1565 it needs to be placed after the function that initializes
1566 that pseudo. */
1567 push_topmost_sequence ();
1568 if (REG_P (crtl->args.internal_arg_pointer)
1569 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1570 emit_insn_before (seq, parm_birth_insn);
1571 else
1572 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1573 pop_topmost_sequence ();
1574 return temp;
1578 /* Perform an untyped call and save the state required to perform an
1579 untyped return of whatever value was returned by the given function. */
1581 static rtx
1582 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1584 int size, align, regno;
1585 fixed_size_mode mode;
1586 rtx incoming_args, result, reg, dest, src;
1587 rtx_call_insn *call_insn;
1588 rtx old_stack_level = 0;
1589 rtx call_fusage = 0;
1590 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1592 arguments = convert_memory_address (Pmode, arguments);
1594 /* Create a block where the return registers can be saved. */
1595 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1597 /* Fetch the arg pointer from the ARGUMENTS block. */
1598 incoming_args = gen_reg_rtx (Pmode);
1599 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1600 if (!STACK_GROWS_DOWNWARD)
1601 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1602 incoming_args, 0, OPTAB_LIB_WIDEN);
1604 /* Push a new argument block and copy the arguments. Do not allow
1605 the (potential) memcpy call below to interfere with our stack
1606 manipulations. */
1607 do_pending_stack_adjust ();
1608 NO_DEFER_POP;
1610 /* Save the stack with nonlocal if available. */
1611 if (targetm.have_save_stack_nonlocal ())
1612 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1613 else
1614 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1616 /* Allocate a block of memory onto the stack and copy the memory
1617 arguments to the outgoing arguments address. We can pass TRUE
1618 as the 4th argument because we just saved the stack pointer
1619 and will restore it right after the call. */
1620 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1622 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1623 may have already set current_function_calls_alloca to true.
1624 current_function_calls_alloca won't be set if argsize is zero,
1625 so we have to guarantee need_drap is true here. */
1626 if (SUPPORTS_STACK_ALIGNMENT)
1627 crtl->need_drap = true;
1629 dest = virtual_outgoing_args_rtx;
1630 if (!STACK_GROWS_DOWNWARD)
1632 if (CONST_INT_P (argsize))
1633 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1634 else
1635 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1637 dest = gen_rtx_MEM (BLKmode, dest);
1638 set_mem_align (dest, PARM_BOUNDARY);
1639 src = gen_rtx_MEM (BLKmode, incoming_args);
1640 set_mem_align (src, PARM_BOUNDARY);
1641 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1643 /* Refer to the argument block. */
1644 apply_args_size ();
1645 arguments = gen_rtx_MEM (BLKmode, arguments);
1646 set_mem_align (arguments, PARM_BOUNDARY);
1648 /* Walk past the arg-pointer and structure value address. */
1649 size = GET_MODE_SIZE (Pmode);
1650 if (struct_value)
1651 size += GET_MODE_SIZE (Pmode);
1653 /* Restore each of the registers previously saved. Make USE insns
1654 for each of these registers for use in making the call. */
1655 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1656 if ((mode = apply_args_mode[regno]) != VOIDmode)
1658 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1659 if (size % align != 0)
1660 size = CEIL (size, align) * align;
1661 reg = gen_rtx_REG (mode, regno);
1662 emit_move_insn (reg, adjust_address (arguments, mode, size));
1663 use_reg (&call_fusage, reg);
1664 size += GET_MODE_SIZE (mode);
1667 /* Restore the structure value address unless this is passed as an
1668 "invisible" first argument. */
1669 size = GET_MODE_SIZE (Pmode);
1670 if (struct_value)
1672 rtx value = gen_reg_rtx (Pmode);
1673 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1674 emit_move_insn (struct_value, value);
1675 if (REG_P (struct_value))
1676 use_reg (&call_fusage, struct_value);
1677 size += GET_MODE_SIZE (Pmode);
1680 /* All arguments and registers used for the call are set up by now! */
1681 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1683 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1684 and we don't want to load it into a register as an optimization,
1685 because prepare_call_address already did it if it should be done. */
1686 if (GET_CODE (function) != SYMBOL_REF)
1687 function = memory_address (FUNCTION_MODE, function);
1689 /* Generate the actual call instruction and save the return value. */
1690 if (targetm.have_untyped_call ())
1692 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1693 emit_call_insn (targetm.gen_untyped_call (mem, result,
1694 result_vector (1, result)));
1696 else if (targetm.have_call_value ())
1698 rtx valreg = 0;
1700 /* Locate the unique return register. It is not possible to
1701 express a call that sets more than one return register using
1702 call_value; use untyped_call for that. In fact, untyped_call
1703 only needs to save the return registers in the given block. */
1704 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1705 if ((mode = apply_result_mode[regno]) != VOIDmode)
1707 gcc_assert (!valreg); /* have_untyped_call required. */
1709 valreg = gen_rtx_REG (mode, regno);
1712 emit_insn (targetm.gen_call_value (valreg,
1713 gen_rtx_MEM (FUNCTION_MODE, function),
1714 const0_rtx, NULL_RTX, const0_rtx));
1716 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1718 else
1719 gcc_unreachable ();
1721 /* Find the CALL insn we just emitted, and attach the register usage
1722 information. */
1723 call_insn = last_call_insn ();
1724 add_function_usage_to (call_insn, call_fusage);
1726 /* Restore the stack. */
1727 if (targetm.have_save_stack_nonlocal ())
1728 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1729 else
1730 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1731 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1733 OK_DEFER_POP;
1735 /* Return the address of the result block. */
1736 result = copy_addr_to_reg (XEXP (result, 0));
1737 return convert_memory_address (ptr_mode, result);
1740 /* Perform an untyped return. */
1742 static void
1743 expand_builtin_return (rtx result)
1745 int size, align, regno;
1746 fixed_size_mode mode;
1747 rtx reg;
1748 rtx_insn *call_fusage = 0;
1750 result = convert_memory_address (Pmode, result);
1752 apply_result_size ();
1753 result = gen_rtx_MEM (BLKmode, result);
1755 if (targetm.have_untyped_return ())
1757 rtx vector = result_vector (0, result);
1758 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1759 emit_barrier ();
1760 return;
1763 /* Restore the return value and note that each value is used. */
1764 size = 0;
1765 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1766 if ((mode = apply_result_mode[regno]) != VOIDmode)
1768 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1769 if (size % align != 0)
1770 size = CEIL (size, align) * align;
1771 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1772 emit_move_insn (reg, adjust_address (result, mode, size));
1774 push_to_sequence (call_fusage);
1775 emit_use (reg);
1776 call_fusage = get_insns ();
1777 end_sequence ();
1778 size += GET_MODE_SIZE (mode);
1781 /* Put the USE insns before the return. */
1782 emit_insn (call_fusage);
1784 /* Return whatever values was restored by jumping directly to the end
1785 of the function. */
1786 expand_naked_return ();
1789 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1791 static enum type_class
1792 type_to_class (tree type)
1794 switch (TREE_CODE (type))
1796 case VOID_TYPE: return void_type_class;
1797 case INTEGER_TYPE: return integer_type_class;
1798 case ENUMERAL_TYPE: return enumeral_type_class;
1799 case BOOLEAN_TYPE: return boolean_type_class;
1800 case POINTER_TYPE: return pointer_type_class;
1801 case REFERENCE_TYPE: return reference_type_class;
1802 case OFFSET_TYPE: return offset_type_class;
1803 case REAL_TYPE: return real_type_class;
1804 case COMPLEX_TYPE: return complex_type_class;
1805 case FUNCTION_TYPE: return function_type_class;
1806 case METHOD_TYPE: return method_type_class;
1807 case RECORD_TYPE: return record_type_class;
1808 case UNION_TYPE:
1809 case QUAL_UNION_TYPE: return union_type_class;
1810 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1811 ? string_type_class : array_type_class);
1812 case LANG_TYPE: return lang_type_class;
1813 default: return no_type_class;
1817 /* Expand a call EXP to __builtin_classify_type. */
1819 static rtx
1820 expand_builtin_classify_type (tree exp)
1822 if (call_expr_nargs (exp))
1823 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1824 return GEN_INT (no_type_class);
1827 /* This helper macro, meant to be used in mathfn_built_in below, determines
1828 which among a set of builtin math functions is appropriate for a given type
1829 mode. The `F' (float) and `L' (long double) are automatically generated
1830 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1831 types, there are additional types that are considered with 'F32', 'F64',
1832 'F128', etc. suffixes. */
1833 #define CASE_MATHFN(MATHFN) \
1834 CASE_CFN_##MATHFN: \
1835 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1836 fcodel = BUILT_IN_##MATHFN##L ; break;
1837 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1838 types. */
1839 #define CASE_MATHFN_FLOATN(MATHFN) \
1840 CASE_CFN_##MATHFN: \
1841 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1842 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1843 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1844 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1845 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1846 break;
1847 /* Similar to above, but appends _R after any F/L suffix. */
1848 #define CASE_MATHFN_REENT(MATHFN) \
1849 case CFN_BUILT_IN_##MATHFN##_R: \
1850 case CFN_BUILT_IN_##MATHFN##F_R: \
1851 case CFN_BUILT_IN_##MATHFN##L_R: \
1852 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1853 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1855 /* Return a function equivalent to FN but operating on floating-point
1856 values of type TYPE, or END_BUILTINS if no such function exists.
1857 This is purely an operation on function codes; it does not guarantee
1858 that the target actually has an implementation of the function. */
1860 static built_in_function
1861 mathfn_built_in_2 (tree type, combined_fn fn)
1863 tree mtype;
1864 built_in_function fcode, fcodef, fcodel;
1865 built_in_function fcodef16 = END_BUILTINS;
1866 built_in_function fcodef32 = END_BUILTINS;
1867 built_in_function fcodef64 = END_BUILTINS;
1868 built_in_function fcodef128 = END_BUILTINS;
1869 built_in_function fcodef32x = END_BUILTINS;
1870 built_in_function fcodef64x = END_BUILTINS;
1871 built_in_function fcodef128x = END_BUILTINS;
1873 switch (fn)
1875 CASE_MATHFN (ACOS)
1876 CASE_MATHFN (ACOSH)
1877 CASE_MATHFN (ASIN)
1878 CASE_MATHFN (ASINH)
1879 CASE_MATHFN (ATAN)
1880 CASE_MATHFN (ATAN2)
1881 CASE_MATHFN (ATANH)
1882 CASE_MATHFN (CBRT)
1883 CASE_MATHFN_FLOATN (CEIL)
1884 CASE_MATHFN (CEXPI)
1885 CASE_MATHFN_FLOATN (COPYSIGN)
1886 CASE_MATHFN (COS)
1887 CASE_MATHFN (COSH)
1888 CASE_MATHFN (DREM)
1889 CASE_MATHFN (ERF)
1890 CASE_MATHFN (ERFC)
1891 CASE_MATHFN (EXP)
1892 CASE_MATHFN (EXP10)
1893 CASE_MATHFN (EXP2)
1894 CASE_MATHFN (EXPM1)
1895 CASE_MATHFN (FABS)
1896 CASE_MATHFN (FDIM)
1897 CASE_MATHFN_FLOATN (FLOOR)
1898 CASE_MATHFN_FLOATN (FMA)
1899 CASE_MATHFN_FLOATN (FMAX)
1900 CASE_MATHFN_FLOATN (FMIN)
1901 CASE_MATHFN (FMOD)
1902 CASE_MATHFN (FREXP)
1903 CASE_MATHFN (GAMMA)
1904 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1905 CASE_MATHFN (HUGE_VAL)
1906 CASE_MATHFN (HYPOT)
1907 CASE_MATHFN (ILOGB)
1908 CASE_MATHFN (ICEIL)
1909 CASE_MATHFN (IFLOOR)
1910 CASE_MATHFN (INF)
1911 CASE_MATHFN (IRINT)
1912 CASE_MATHFN (IROUND)
1913 CASE_MATHFN (ISINF)
1914 CASE_MATHFN (J0)
1915 CASE_MATHFN (J1)
1916 CASE_MATHFN (JN)
1917 CASE_MATHFN (LCEIL)
1918 CASE_MATHFN (LDEXP)
1919 CASE_MATHFN (LFLOOR)
1920 CASE_MATHFN (LGAMMA)
1921 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1922 CASE_MATHFN (LLCEIL)
1923 CASE_MATHFN (LLFLOOR)
1924 CASE_MATHFN (LLRINT)
1925 CASE_MATHFN (LLROUND)
1926 CASE_MATHFN (LOG)
1927 CASE_MATHFN (LOG10)
1928 CASE_MATHFN (LOG1P)
1929 CASE_MATHFN (LOG2)
1930 CASE_MATHFN (LOGB)
1931 CASE_MATHFN (LRINT)
1932 CASE_MATHFN (LROUND)
1933 CASE_MATHFN (MODF)
1934 CASE_MATHFN (NAN)
1935 CASE_MATHFN (NANS)
1936 CASE_MATHFN_FLOATN (NEARBYINT)
1937 CASE_MATHFN (NEXTAFTER)
1938 CASE_MATHFN (NEXTTOWARD)
1939 CASE_MATHFN (POW)
1940 CASE_MATHFN (POWI)
1941 CASE_MATHFN (POW10)
1942 CASE_MATHFN (REMAINDER)
1943 CASE_MATHFN (REMQUO)
1944 CASE_MATHFN_FLOATN (RINT)
1945 CASE_MATHFN_FLOATN (ROUND)
1946 CASE_MATHFN (SCALB)
1947 CASE_MATHFN (SCALBLN)
1948 CASE_MATHFN (SCALBN)
1949 CASE_MATHFN (SIGNBIT)
1950 CASE_MATHFN (SIGNIFICAND)
1951 CASE_MATHFN (SIN)
1952 CASE_MATHFN (SINCOS)
1953 CASE_MATHFN (SINH)
1954 CASE_MATHFN_FLOATN (SQRT)
1955 CASE_MATHFN (TAN)
1956 CASE_MATHFN (TANH)
1957 CASE_MATHFN (TGAMMA)
1958 CASE_MATHFN_FLOATN (TRUNC)
1959 CASE_MATHFN (Y0)
1960 CASE_MATHFN (Y1)
1961 CASE_MATHFN (YN)
1963 default:
1964 return END_BUILTINS;
1967 mtype = TYPE_MAIN_VARIANT (type);
1968 if (mtype == double_type_node)
1969 return fcode;
1970 else if (mtype == float_type_node)
1971 return fcodef;
1972 else if (mtype == long_double_type_node)
1973 return fcodel;
1974 else if (mtype == float16_type_node)
1975 return fcodef16;
1976 else if (mtype == float32_type_node)
1977 return fcodef32;
1978 else if (mtype == float64_type_node)
1979 return fcodef64;
1980 else if (mtype == float128_type_node)
1981 return fcodef128;
1982 else if (mtype == float32x_type_node)
1983 return fcodef32x;
1984 else if (mtype == float64x_type_node)
1985 return fcodef64x;
1986 else if (mtype == float128x_type_node)
1987 return fcodef128x;
1988 else
1989 return END_BUILTINS;
1992 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1993 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1994 otherwise use the explicit declaration. If we can't do the conversion,
1995 return null. */
1997 static tree
1998 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2000 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2001 if (fcode2 == END_BUILTINS)
2002 return NULL_TREE;
2004 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2005 return NULL_TREE;
2007 return builtin_decl_explicit (fcode2);
2010 /* Like mathfn_built_in_1, but always use the implicit array. */
2012 tree
2013 mathfn_built_in (tree type, combined_fn fn)
2015 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2018 /* Like mathfn_built_in_1, but take a built_in_function and
2019 always use the implicit array. */
2021 tree
2022 mathfn_built_in (tree type, enum built_in_function fn)
2024 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2027 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2028 return its code, otherwise return IFN_LAST. Note that this function
2029 only tests whether the function is defined in internals.def, not whether
2030 it is actually available on the target. */
2032 internal_fn
2033 associated_internal_fn (tree fndecl)
2035 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2036 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
2037 switch (DECL_FUNCTION_CODE (fndecl))
2039 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2040 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2041 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2042 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2043 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2044 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2045 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2046 #include "internal-fn.def"
2048 CASE_FLT_FN (BUILT_IN_POW10):
2049 return IFN_EXP10;
2051 CASE_FLT_FN (BUILT_IN_DREM):
2052 return IFN_REMAINDER;
2054 CASE_FLT_FN (BUILT_IN_SCALBN):
2055 CASE_FLT_FN (BUILT_IN_SCALBLN):
2056 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2057 return IFN_LDEXP;
2058 return IFN_LAST;
2060 default:
2061 return IFN_LAST;
2065 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2066 on the current target by a call to an internal function, return the
2067 code of that internal function, otherwise return IFN_LAST. The caller
2068 is responsible for ensuring that any side-effects of the built-in
2069 call are dealt with correctly. E.g. if CALL sets errno, the caller
2070 must decide that the errno result isn't needed or make it available
2071 in some other way. */
2073 internal_fn
2074 replacement_internal_fn (gcall *call)
2076 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2078 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2079 if (ifn != IFN_LAST)
2081 tree_pair types = direct_internal_fn_types (ifn, call);
2082 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2083 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2084 return ifn;
2087 return IFN_LAST;
2090 /* Expand a call to the builtin trinary math functions (fma).
2091 Return NULL_RTX if a normal call should be emitted rather than expanding the
2092 function in-line. EXP is the expression that is a call to the builtin
2093 function; if convenient, the result should be placed in TARGET.
2094 SUBTARGET may be used as the target for computing one of EXP's
2095 operands. */
2097 static rtx
2098 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2100 optab builtin_optab;
2101 rtx op0, op1, op2, result;
2102 rtx_insn *insns;
2103 tree fndecl = get_callee_fndecl (exp);
2104 tree arg0, arg1, arg2;
2105 machine_mode mode;
2107 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2108 return NULL_RTX;
2110 arg0 = CALL_EXPR_ARG (exp, 0);
2111 arg1 = CALL_EXPR_ARG (exp, 1);
2112 arg2 = CALL_EXPR_ARG (exp, 2);
2114 switch (DECL_FUNCTION_CODE (fndecl))
2116 CASE_FLT_FN (BUILT_IN_FMA):
2117 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2118 builtin_optab = fma_optab; break;
2119 default:
2120 gcc_unreachable ();
2123 /* Make a suitable register to place result in. */
2124 mode = TYPE_MODE (TREE_TYPE (exp));
2126 /* Before working hard, check whether the instruction is available. */
2127 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2128 return NULL_RTX;
2130 result = gen_reg_rtx (mode);
2132 /* Always stabilize the argument list. */
2133 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2134 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2135 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2137 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2138 op1 = expand_normal (arg1);
2139 op2 = expand_normal (arg2);
2141 start_sequence ();
2143 /* Compute into RESULT.
2144 Set RESULT to wherever the result comes back. */
2145 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2146 result, 0);
2148 /* If we were unable to expand via the builtin, stop the sequence
2149 (without outputting the insns) and call to the library function
2150 with the stabilized argument list. */
2151 if (result == 0)
2153 end_sequence ();
2154 return expand_call (exp, target, target == const0_rtx);
2157 /* Output the entire sequence. */
2158 insns = get_insns ();
2159 end_sequence ();
2160 emit_insn (insns);
2162 return result;
2165 /* Expand a call to the builtin sin and cos math functions.
2166 Return NULL_RTX if a normal call should be emitted rather than expanding the
2167 function in-line. EXP is the expression that is a call to the builtin
2168 function; if convenient, the result should be placed in TARGET.
2169 SUBTARGET may be used as the target for computing one of EXP's
2170 operands. */
2172 static rtx
2173 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2175 optab builtin_optab;
2176 rtx op0;
2177 rtx_insn *insns;
2178 tree fndecl = get_callee_fndecl (exp);
2179 machine_mode mode;
2180 tree arg;
2182 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2183 return NULL_RTX;
2185 arg = CALL_EXPR_ARG (exp, 0);
2187 switch (DECL_FUNCTION_CODE (fndecl))
2189 CASE_FLT_FN (BUILT_IN_SIN):
2190 CASE_FLT_FN (BUILT_IN_COS):
2191 builtin_optab = sincos_optab; break;
2192 default:
2193 gcc_unreachable ();
2196 /* Make a suitable register to place result in. */
2197 mode = TYPE_MODE (TREE_TYPE (exp));
2199 /* Check if sincos insn is available, otherwise fallback
2200 to sin or cos insn. */
2201 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2202 switch (DECL_FUNCTION_CODE (fndecl))
2204 CASE_FLT_FN (BUILT_IN_SIN):
2205 builtin_optab = sin_optab; break;
2206 CASE_FLT_FN (BUILT_IN_COS):
2207 builtin_optab = cos_optab; break;
2208 default:
2209 gcc_unreachable ();
2212 /* Before working hard, check whether the instruction is available. */
2213 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2215 rtx result = gen_reg_rtx (mode);
2217 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2218 need to expand the argument again. This way, we will not perform
2219 side-effects more the once. */
2220 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2222 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2224 start_sequence ();
2226 /* Compute into RESULT.
2227 Set RESULT to wherever the result comes back. */
2228 if (builtin_optab == sincos_optab)
2230 int ok;
2232 switch (DECL_FUNCTION_CODE (fndecl))
2234 CASE_FLT_FN (BUILT_IN_SIN):
2235 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2236 break;
2237 CASE_FLT_FN (BUILT_IN_COS):
2238 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2239 break;
2240 default:
2241 gcc_unreachable ();
2243 gcc_assert (ok);
2245 else
2246 result = expand_unop (mode, builtin_optab, op0, result, 0);
2248 if (result != 0)
2250 /* Output the entire sequence. */
2251 insns = get_insns ();
2252 end_sequence ();
2253 emit_insn (insns);
2254 return result;
2257 /* If we were unable to expand via the builtin, stop the sequence
2258 (without outputting the insns) and call to the library function
2259 with the stabilized argument list. */
2260 end_sequence ();
2263 return expand_call (exp, target, target == const0_rtx);
2266 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2267 return an RTL instruction code that implements the functionality.
2268 If that isn't possible or available return CODE_FOR_nothing. */
2270 static enum insn_code
2271 interclass_mathfn_icode (tree arg, tree fndecl)
2273 bool errno_set = false;
2274 optab builtin_optab = unknown_optab;
2275 machine_mode mode;
2277 switch (DECL_FUNCTION_CODE (fndecl))
2279 CASE_FLT_FN (BUILT_IN_ILOGB):
2280 errno_set = true; builtin_optab = ilogb_optab; break;
2281 CASE_FLT_FN (BUILT_IN_ISINF):
2282 builtin_optab = isinf_optab; break;
2283 case BUILT_IN_ISNORMAL:
2284 case BUILT_IN_ISFINITE:
2285 CASE_FLT_FN (BUILT_IN_FINITE):
2286 case BUILT_IN_FINITED32:
2287 case BUILT_IN_FINITED64:
2288 case BUILT_IN_FINITED128:
2289 case BUILT_IN_ISINFD32:
2290 case BUILT_IN_ISINFD64:
2291 case BUILT_IN_ISINFD128:
2292 /* These builtins have no optabs (yet). */
2293 break;
2294 default:
2295 gcc_unreachable ();
2298 /* There's no easy way to detect the case we need to set EDOM. */
2299 if (flag_errno_math && errno_set)
2300 return CODE_FOR_nothing;
2302 /* Optab mode depends on the mode of the input argument. */
2303 mode = TYPE_MODE (TREE_TYPE (arg));
2305 if (builtin_optab)
2306 return optab_handler (builtin_optab, mode);
2307 return CODE_FOR_nothing;
2310 /* Expand a call to one of the builtin math functions that operate on
2311 floating point argument and output an integer result (ilogb, isinf,
2312 isnan, etc).
2313 Return 0 if a normal call should be emitted rather than expanding the
2314 function in-line. EXP is the expression that is a call to the builtin
2315 function; if convenient, the result should be placed in TARGET. */
2317 static rtx
2318 expand_builtin_interclass_mathfn (tree exp, rtx target)
2320 enum insn_code icode = CODE_FOR_nothing;
2321 rtx op0;
2322 tree fndecl = get_callee_fndecl (exp);
2323 machine_mode mode;
2324 tree arg;
2326 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2327 return NULL_RTX;
2329 arg = CALL_EXPR_ARG (exp, 0);
2330 icode = interclass_mathfn_icode (arg, fndecl);
2331 mode = TYPE_MODE (TREE_TYPE (arg));
2333 if (icode != CODE_FOR_nothing)
2335 struct expand_operand ops[1];
2336 rtx_insn *last = get_last_insn ();
2337 tree orig_arg = arg;
2339 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2340 need to expand the argument again. This way, we will not perform
2341 side-effects more the once. */
2342 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2344 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2346 if (mode != GET_MODE (op0))
2347 op0 = convert_to_mode (mode, op0, 0);
2349 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2350 if (maybe_legitimize_operands (icode, 0, 1, ops)
2351 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2352 return ops[0].value;
2354 delete_insns_since (last);
2355 CALL_EXPR_ARG (exp, 0) = orig_arg;
2358 return NULL_RTX;
2361 /* Expand a call to the builtin sincos math function.
2362 Return NULL_RTX if a normal call should be emitted rather than expanding the
2363 function in-line. EXP is the expression that is a call to the builtin
2364 function. */
2366 static rtx
2367 expand_builtin_sincos (tree exp)
2369 rtx op0, op1, op2, target1, target2;
2370 machine_mode mode;
2371 tree arg, sinp, cosp;
2372 int result;
2373 location_t loc = EXPR_LOCATION (exp);
2374 tree alias_type, alias_off;
2376 if (!validate_arglist (exp, REAL_TYPE,
2377 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2378 return NULL_RTX;
2380 arg = CALL_EXPR_ARG (exp, 0);
2381 sinp = CALL_EXPR_ARG (exp, 1);
2382 cosp = CALL_EXPR_ARG (exp, 2);
2384 /* Make a suitable register to place result in. */
2385 mode = TYPE_MODE (TREE_TYPE (arg));
2387 /* Check if sincos insn is available, otherwise emit the call. */
2388 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2389 return NULL_RTX;
2391 target1 = gen_reg_rtx (mode);
2392 target2 = gen_reg_rtx (mode);
2394 op0 = expand_normal (arg);
2395 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2396 alias_off = build_int_cst (alias_type, 0);
2397 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2398 sinp, alias_off));
2399 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2400 cosp, alias_off));
2402 /* Compute into target1 and target2.
2403 Set TARGET to wherever the result comes back. */
2404 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2405 gcc_assert (result);
2407 /* Move target1 and target2 to the memory locations indicated
2408 by op1 and op2. */
2409 emit_move_insn (op1, target1);
2410 emit_move_insn (op2, target2);
2412 return const0_rtx;
2415 /* Expand a call to the internal cexpi builtin to the sincos math function.
2416 EXP is the expression that is a call to the builtin function; if convenient,
2417 the result should be placed in TARGET. */
2419 static rtx
2420 expand_builtin_cexpi (tree exp, rtx target)
2422 tree fndecl = get_callee_fndecl (exp);
2423 tree arg, type;
2424 machine_mode mode;
2425 rtx op0, op1, op2;
2426 location_t loc = EXPR_LOCATION (exp);
2428 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2429 return NULL_RTX;
2431 arg = CALL_EXPR_ARG (exp, 0);
2432 type = TREE_TYPE (arg);
2433 mode = TYPE_MODE (TREE_TYPE (arg));
2435 /* Try expanding via a sincos optab, fall back to emitting a libcall
2436 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2437 is only generated from sincos, cexp or if we have either of them. */
2438 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2440 op1 = gen_reg_rtx (mode);
2441 op2 = gen_reg_rtx (mode);
2443 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2445 /* Compute into op1 and op2. */
2446 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2448 else if (targetm.libc_has_function (function_sincos))
2450 tree call, fn = NULL_TREE;
2451 tree top1, top2;
2452 rtx op1a, op2a;
2454 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2455 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2456 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2457 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2458 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2459 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2460 else
2461 gcc_unreachable ();
2463 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2464 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2465 op1a = copy_addr_to_reg (XEXP (op1, 0));
2466 op2a = copy_addr_to_reg (XEXP (op2, 0));
2467 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2468 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2470 /* Make sure not to fold the sincos call again. */
2471 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2472 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2473 call, 3, arg, top1, top2));
2475 else
2477 tree call, fn = NULL_TREE, narg;
2478 tree ctype = build_complex_type (type);
2480 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2481 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2482 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2483 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2484 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2485 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2486 else
2487 gcc_unreachable ();
2489 /* If we don't have a decl for cexp create one. This is the
2490 friendliest fallback if the user calls __builtin_cexpi
2491 without full target C99 function support. */
2492 if (fn == NULL_TREE)
2494 tree fntype;
2495 const char *name = NULL;
2497 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2498 name = "cexpf";
2499 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2500 name = "cexp";
2501 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2502 name = "cexpl";
2504 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2505 fn = build_fn_decl (name, fntype);
2508 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2509 build_real (type, dconst0), arg);
2511 /* Make sure not to fold the cexp call again. */
2512 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2513 return expand_expr (build_call_nary (ctype, call, 1, narg),
2514 target, VOIDmode, EXPAND_NORMAL);
2517 /* Now build the proper return type. */
2518 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2519 make_tree (TREE_TYPE (arg), op2),
2520 make_tree (TREE_TYPE (arg), op1)),
2521 target, VOIDmode, EXPAND_NORMAL);
2524 /* Conveniently construct a function call expression. FNDECL names the
2525 function to be called, N is the number of arguments, and the "..."
2526 parameters are the argument expressions. Unlike build_call_exr
2527 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2529 static tree
2530 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2532 va_list ap;
2533 tree fntype = TREE_TYPE (fndecl);
2534 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2536 va_start (ap, n);
2537 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2538 va_end (ap);
2539 SET_EXPR_LOCATION (fn, loc);
2540 return fn;
2543 /* Expand a call to one of the builtin rounding functions gcc defines
2544 as an extension (lfloor and lceil). As these are gcc extensions we
2545 do not need to worry about setting errno to EDOM.
2546 If expanding via optab fails, lower expression to (int)(floor(x)).
2547 EXP is the expression that is a call to the builtin function;
2548 if convenient, the result should be placed in TARGET. */
2550 static rtx
2551 expand_builtin_int_roundingfn (tree exp, rtx target)
2553 convert_optab builtin_optab;
2554 rtx op0, tmp;
2555 rtx_insn *insns;
2556 tree fndecl = get_callee_fndecl (exp);
2557 enum built_in_function fallback_fn;
2558 tree fallback_fndecl;
2559 machine_mode mode;
2560 tree arg;
2562 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2563 gcc_unreachable ();
2565 arg = CALL_EXPR_ARG (exp, 0);
2567 switch (DECL_FUNCTION_CODE (fndecl))
2569 CASE_FLT_FN (BUILT_IN_ICEIL):
2570 CASE_FLT_FN (BUILT_IN_LCEIL):
2571 CASE_FLT_FN (BUILT_IN_LLCEIL):
2572 builtin_optab = lceil_optab;
2573 fallback_fn = BUILT_IN_CEIL;
2574 break;
2576 CASE_FLT_FN (BUILT_IN_IFLOOR):
2577 CASE_FLT_FN (BUILT_IN_LFLOOR):
2578 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2579 builtin_optab = lfloor_optab;
2580 fallback_fn = BUILT_IN_FLOOR;
2581 break;
2583 default:
2584 gcc_unreachable ();
2587 /* Make a suitable register to place result in. */
2588 mode = TYPE_MODE (TREE_TYPE (exp));
2590 target = gen_reg_rtx (mode);
2592 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2593 need to expand the argument again. This way, we will not perform
2594 side-effects more the once. */
2595 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2597 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2599 start_sequence ();
2601 /* Compute into TARGET. */
2602 if (expand_sfix_optab (target, op0, builtin_optab))
2604 /* Output the entire sequence. */
2605 insns = get_insns ();
2606 end_sequence ();
2607 emit_insn (insns);
2608 return target;
2611 /* If we were unable to expand via the builtin, stop the sequence
2612 (without outputting the insns). */
2613 end_sequence ();
2615 /* Fall back to floating point rounding optab. */
2616 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2618 /* For non-C99 targets we may end up without a fallback fndecl here
2619 if the user called __builtin_lfloor directly. In this case emit
2620 a call to the floor/ceil variants nevertheless. This should result
2621 in the best user experience for not full C99 targets. */
2622 if (fallback_fndecl == NULL_TREE)
2624 tree fntype;
2625 const char *name = NULL;
2627 switch (DECL_FUNCTION_CODE (fndecl))
2629 case BUILT_IN_ICEIL:
2630 case BUILT_IN_LCEIL:
2631 case BUILT_IN_LLCEIL:
2632 name = "ceil";
2633 break;
2634 case BUILT_IN_ICEILF:
2635 case BUILT_IN_LCEILF:
2636 case BUILT_IN_LLCEILF:
2637 name = "ceilf";
2638 break;
2639 case BUILT_IN_ICEILL:
2640 case BUILT_IN_LCEILL:
2641 case BUILT_IN_LLCEILL:
2642 name = "ceill";
2643 break;
2644 case BUILT_IN_IFLOOR:
2645 case BUILT_IN_LFLOOR:
2646 case BUILT_IN_LLFLOOR:
2647 name = "floor";
2648 break;
2649 case BUILT_IN_IFLOORF:
2650 case BUILT_IN_LFLOORF:
2651 case BUILT_IN_LLFLOORF:
2652 name = "floorf";
2653 break;
2654 case BUILT_IN_IFLOORL:
2655 case BUILT_IN_LFLOORL:
2656 case BUILT_IN_LLFLOORL:
2657 name = "floorl";
2658 break;
2659 default:
2660 gcc_unreachable ();
2663 fntype = build_function_type_list (TREE_TYPE (arg),
2664 TREE_TYPE (arg), NULL_TREE);
2665 fallback_fndecl = build_fn_decl (name, fntype);
2668 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2670 tmp = expand_normal (exp);
2671 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2673 /* Truncate the result of floating point optab to integer
2674 via expand_fix (). */
2675 target = gen_reg_rtx (mode);
2676 expand_fix (target, tmp, 0);
2678 return target;
2681 /* Expand a call to one of the builtin math functions doing integer
2682 conversion (lrint).
2683 Return 0 if a normal call should be emitted rather than expanding the
2684 function in-line. EXP is the expression that is a call to the builtin
2685 function; if convenient, the result should be placed in TARGET. */
2687 static rtx
2688 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2690 convert_optab builtin_optab;
2691 rtx op0;
2692 rtx_insn *insns;
2693 tree fndecl = get_callee_fndecl (exp);
2694 tree arg;
2695 machine_mode mode;
2696 enum built_in_function fallback_fn = BUILT_IN_NONE;
2698 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2699 gcc_unreachable ();
2701 arg = CALL_EXPR_ARG (exp, 0);
2703 switch (DECL_FUNCTION_CODE (fndecl))
2705 CASE_FLT_FN (BUILT_IN_IRINT):
2706 fallback_fn = BUILT_IN_LRINT;
2707 gcc_fallthrough ();
2708 CASE_FLT_FN (BUILT_IN_LRINT):
2709 CASE_FLT_FN (BUILT_IN_LLRINT):
2710 builtin_optab = lrint_optab;
2711 break;
2713 CASE_FLT_FN (BUILT_IN_IROUND):
2714 fallback_fn = BUILT_IN_LROUND;
2715 gcc_fallthrough ();
2716 CASE_FLT_FN (BUILT_IN_LROUND):
2717 CASE_FLT_FN (BUILT_IN_LLROUND):
2718 builtin_optab = lround_optab;
2719 break;
2721 default:
2722 gcc_unreachable ();
2725 /* There's no easy way to detect the case we need to set EDOM. */
2726 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2727 return NULL_RTX;
2729 /* Make a suitable register to place result in. */
2730 mode = TYPE_MODE (TREE_TYPE (exp));
2732 /* There's no easy way to detect the case we need to set EDOM. */
2733 if (!flag_errno_math)
2735 rtx result = gen_reg_rtx (mode);
2737 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2738 need to expand the argument again. This way, we will not perform
2739 side-effects more the once. */
2740 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2742 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2744 start_sequence ();
2746 if (expand_sfix_optab (result, op0, builtin_optab))
2748 /* Output the entire sequence. */
2749 insns = get_insns ();
2750 end_sequence ();
2751 emit_insn (insns);
2752 return result;
2755 /* If we were unable to expand via the builtin, stop the sequence
2756 (without outputting the insns) and call to the library function
2757 with the stabilized argument list. */
2758 end_sequence ();
2761 if (fallback_fn != BUILT_IN_NONE)
2763 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2764 targets, (int) round (x) should never be transformed into
2765 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2766 a call to lround in the hope that the target provides at least some
2767 C99 functions. This should result in the best user experience for
2768 not full C99 targets. */
2769 tree fallback_fndecl = mathfn_built_in_1
2770 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2772 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2773 fallback_fndecl, 1, arg);
2775 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2776 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2777 return convert_to_mode (mode, target, 0);
2780 return expand_call (exp, target, target == const0_rtx);
2783 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2784 a normal call should be emitted rather than expanding the function
2785 in-line. EXP is the expression that is a call to the builtin
2786 function; if convenient, the result should be placed in TARGET. */
2788 static rtx
2789 expand_builtin_powi (tree exp, rtx target)
2791 tree arg0, arg1;
2792 rtx op0, op1;
2793 machine_mode mode;
2794 machine_mode mode2;
2796 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2797 return NULL_RTX;
2799 arg0 = CALL_EXPR_ARG (exp, 0);
2800 arg1 = CALL_EXPR_ARG (exp, 1);
2801 mode = TYPE_MODE (TREE_TYPE (exp));
2803 /* Emit a libcall to libgcc. */
2805 /* Mode of the 2nd argument must match that of an int. */
2806 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
2808 if (target == NULL_RTX)
2809 target = gen_reg_rtx (mode);
2811 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2812 if (GET_MODE (op0) != mode)
2813 op0 = convert_to_mode (mode, op0, 0);
2814 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2815 if (GET_MODE (op1) != mode2)
2816 op1 = convert_to_mode (mode2, op1, 0);
2818 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2819 target, LCT_CONST, mode,
2820 op0, mode, op1, mode2);
2822 return target;
2825 /* Expand expression EXP which is a call to the strlen builtin. Return
2826 NULL_RTX if we failed the caller should emit a normal call, otherwise
2827 try to get the result in TARGET, if convenient. */
2829 static rtx
2830 expand_builtin_strlen (tree exp, rtx target,
2831 machine_mode target_mode)
2833 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2834 return NULL_RTX;
2836 struct expand_operand ops[4];
2837 rtx pat;
2838 tree len;
2839 tree src = CALL_EXPR_ARG (exp, 0);
2840 rtx src_reg;
2841 rtx_insn *before_strlen;
2842 machine_mode insn_mode;
2843 enum insn_code icode = CODE_FOR_nothing;
2844 unsigned int align;
2846 /* If the length can be computed at compile-time, return it. */
2847 len = c_strlen (src, 0);
2848 if (len)
2849 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2851 /* If the length can be computed at compile-time and is constant
2852 integer, but there are side-effects in src, evaluate
2853 src for side-effects, then return len.
2854 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2855 can be optimized into: i++; x = 3; */
2856 len = c_strlen (src, 1);
2857 if (len && TREE_CODE (len) == INTEGER_CST)
2859 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2860 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2863 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2865 /* If SRC is not a pointer type, don't do this operation inline. */
2866 if (align == 0)
2867 return NULL_RTX;
2869 /* Bail out if we can't compute strlen in the right mode. */
2870 FOR_EACH_MODE_FROM (insn_mode, target_mode)
2872 icode = optab_handler (strlen_optab, insn_mode);
2873 if (icode != CODE_FOR_nothing)
2874 break;
2876 if (insn_mode == VOIDmode)
2877 return NULL_RTX;
2879 /* Make a place to hold the source address. We will not expand
2880 the actual source until we are sure that the expansion will
2881 not fail -- there are trees that cannot be expanded twice. */
2882 src_reg = gen_reg_rtx (Pmode);
2884 /* Mark the beginning of the strlen sequence so we can emit the
2885 source operand later. */
2886 before_strlen = get_last_insn ();
2888 create_output_operand (&ops[0], target, insn_mode);
2889 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2890 create_integer_operand (&ops[2], 0);
2891 create_integer_operand (&ops[3], align);
2892 if (!maybe_expand_insn (icode, 4, ops))
2893 return NULL_RTX;
2895 /* Check to see if the argument was declared attribute nonstring
2896 and if so, issue a warning since at this point it's not known
2897 to be nul-terminated. */
2898 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
2900 /* Now that we are assured of success, expand the source. */
2901 start_sequence ();
2902 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2903 if (pat != src_reg)
2905 #ifdef POINTERS_EXTEND_UNSIGNED
2906 if (GET_MODE (pat) != Pmode)
2907 pat = convert_to_mode (Pmode, pat,
2908 POINTERS_EXTEND_UNSIGNED);
2909 #endif
2910 emit_move_insn (src_reg, pat);
2912 pat = get_insns ();
2913 end_sequence ();
2915 if (before_strlen)
2916 emit_insn_after (pat, before_strlen);
2917 else
2918 emit_insn_before (pat, get_insns ());
2920 /* Return the value in the proper mode for this function. */
2921 if (GET_MODE (ops[0].value) == target_mode)
2922 target = ops[0].value;
2923 else if (target != 0)
2924 convert_move (target, ops[0].value, 0);
2925 else
2926 target = convert_to_mode (target_mode, ops[0].value, 0);
2928 return target;
2931 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2932 bytes from constant string DATA + OFFSET and return it as target
2933 constant. */
2935 static rtx
2936 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2937 scalar_int_mode mode)
2939 const char *str = (const char *) data;
2941 gcc_assert (offset >= 0
2942 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2943 <= strlen (str) + 1));
2945 return c_readstr (str + offset, mode);
2948 /* LEN specify length of the block of memcpy/memset operation.
2949 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
2950 In some cases we can make very likely guess on max size, then we
2951 set it into PROBABLE_MAX_SIZE. */
2953 static void
2954 determine_block_size (tree len, rtx len_rtx,
2955 unsigned HOST_WIDE_INT *min_size,
2956 unsigned HOST_WIDE_INT *max_size,
2957 unsigned HOST_WIDE_INT *probable_max_size)
2959 if (CONST_INT_P (len_rtx))
2961 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
2962 return;
2964 else
2966 wide_int min, max;
2967 enum value_range_type range_type = VR_UNDEFINED;
2969 /* Determine bounds from the type. */
2970 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
2971 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
2972 else
2973 *min_size = 0;
2974 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
2975 *probable_max_size = *max_size
2976 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
2977 else
2978 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
2980 if (TREE_CODE (len) == SSA_NAME)
2981 range_type = get_range_info (len, &min, &max);
2982 if (range_type == VR_RANGE)
2984 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
2985 *min_size = min.to_uhwi ();
2986 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
2987 *probable_max_size = *max_size = max.to_uhwi ();
2989 else if (range_type == VR_ANTI_RANGE)
2991 /* Anti range 0...N lets us to determine minimal size to N+1. */
2992 if (min == 0)
2994 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
2995 *min_size = max.to_uhwi () + 1;
2997 /* Code like
2999 int n;
3000 if (n < 100)
3001 memcpy (a, b, n)
3003 Produce anti range allowing negative values of N. We still
3004 can use the information and make a guess that N is not negative.
3006 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3007 *probable_max_size = min.to_uhwi () - 1;
3010 gcc_checking_assert (*max_size <=
3011 (unsigned HOST_WIDE_INT)
3012 GET_MODE_MASK (GET_MODE (len_rtx)));
3015 /* Try to verify that the sizes and lengths of the arguments to a string
3016 manipulation function given by EXP are within valid bounds and that
3017 the operation does not lead to buffer overflow or read past the end.
3018 Arguments other than EXP may be null. When non-null, the arguments
3019 have the following meaning:
3020 DST is the destination of a copy call or NULL otherwise.
3021 SRC is the source of a copy call or NULL otherwise.
3022 DSTWRITE is the number of bytes written into the destination obtained
3023 from the user-supplied size argument to the function (such as in
3024 memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
3025 MAXREAD is the user-supplied bound on the length of the source sequence
3026 (such as in strncat(d, s, N). It specifies the upper limit on the number
3027 of bytes to write. If NULL, it's taken to be the same as DSTWRITE.
3028 SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
3029 expression EXP is a string function call (as opposed to a memory call
3030 like memcpy). As an exception, SRCSTR can also be an integer denoting
3031 the precomputed size of the source string or object (for functions like
3032 memcpy).
3033 DSTSIZE is the size of the destination object specified by the last
3034 argument to the _chk builtins, typically resulting from the expansion
3035 of __builtin_object_size (such as in __builtin___strcpy_chk(DST, SRC,
3036 DSTSIZE).
3038 When DSTWRITE is null LEN is checked to verify that it doesn't exceed
3039 SIZE_MAX.
3041 If the call is successfully verified as safe return true, otherwise
3042 return false. */
3044 static bool
3045 check_access (tree exp, tree, tree, tree dstwrite,
3046 tree maxread, tree srcstr, tree dstsize)
3048 int opt = OPT_Wstringop_overflow_;
3050 /* The size of the largest object is half the address space, or
3051 PTRDIFF_MAX. (This is way too permissive.) */
3052 tree maxobjsize = max_object_size ();
3054 /* Either the length of the source string for string functions or
3055 the size of the source object for raw memory functions. */
3056 tree slen = NULL_TREE;
3058 tree range[2] = { NULL_TREE, NULL_TREE };
3060 /* Set to true when the exact number of bytes written by a string
3061 function like strcpy is not known and the only thing that is
3062 known is that it must be at least one (for the terminating nul). */
3063 bool at_least_one = false;
3064 if (srcstr)
3066 /* SRCSTR is normally a pointer to string but as a special case
3067 it can be an integer denoting the length of a string. */
3068 if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
3070 /* Try to determine the range of lengths the source string
3071 refers to. If it can be determined and is less than
3072 the upper bound given by MAXREAD add one to it for
3073 the terminating nul. Otherwise, set it to one for
3074 the same reason, or to MAXREAD as appropriate. */
3075 get_range_strlen (srcstr, range);
3076 if (range[0] && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
3078 if (maxread && tree_int_cst_le (maxread, range[0]))
3079 range[0] = range[1] = maxread;
3080 else
3081 range[0] = fold_build2 (PLUS_EXPR, size_type_node,
3082 range[0], size_one_node);
3084 if (maxread && tree_int_cst_le (maxread, range[1]))
3085 range[1] = maxread;
3086 else if (!integer_all_onesp (range[1]))
3087 range[1] = fold_build2 (PLUS_EXPR, size_type_node,
3088 range[1], size_one_node);
3090 slen = range[0];
3092 else
3094 at_least_one = true;
3095 slen = size_one_node;
3098 else
3099 slen = srcstr;
3102 if (!dstwrite && !maxread)
3104 /* When the only available piece of data is the object size
3105 there is nothing to do. */
3106 if (!slen)
3107 return true;
3109 /* Otherwise, when the length of the source sequence is known
3110 (as with strlen), set DSTWRITE to it. */
3111 if (!range[0])
3112 dstwrite = slen;
3115 if (!dstsize)
3116 dstsize = maxobjsize;
3118 if (dstwrite)
3119 get_size_range (dstwrite, range);
3121 tree func = get_callee_fndecl (exp);
3123 /* First check the number of bytes to be written against the maximum
3124 object size. */
3125 if (range[0] && tree_int_cst_lt (maxobjsize, range[0]))
3127 location_t loc = tree_nonartificial_location (exp);
3128 loc = expansion_point_location_if_in_system_header (loc);
3130 if (range[0] == range[1])
3131 warning_at (loc, opt,
3132 "%K%qD specified size %E "
3133 "exceeds maximum object size %E",
3134 exp, func, range[0], maxobjsize);
3135 else
3136 warning_at (loc, opt,
3137 "%K%qD specified size between %E and %E "
3138 "exceeds maximum object size %E",
3139 exp, func,
3140 range[0], range[1], maxobjsize);
3141 return false;
3144 /* The number of bytes to write is "exact" if DSTWRITE is non-null,
3145 constant, and in range of unsigned HOST_WIDE_INT. */
3146 bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite);
3148 /* Next check the number of bytes to be written against the destination
3149 object size. */
3150 if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
3152 if (range[0]
3153 && ((tree_fits_uhwi_p (dstsize)
3154 && tree_int_cst_lt (dstsize, range[0]))
3155 || (tree_fits_uhwi_p (dstwrite)
3156 && tree_int_cst_lt (dstwrite, range[0]))))
3158 if (TREE_NO_WARNING (exp))
3159 return false;
3161 location_t loc = tree_nonartificial_location (exp);
3162 loc = expansion_point_location_if_in_system_header (loc);
3164 if (dstwrite == slen && at_least_one)
3166 /* This is a call to strcpy with a destination of 0 size
3167 and a source of unknown length. The call will write
3168 at least one byte past the end of the destination. */
3169 warning_at (loc, opt,
3170 "%K%qD writing %E or more bytes into a region "
3171 "of size %E overflows the destination",
3172 exp, func, range[0], dstsize);
3174 else if (tree_int_cst_equal (range[0], range[1]))
3175 warning_at (loc, opt,
3176 (integer_onep (range[0])
3177 ? G_("%K%qD writing %E byte into a region "
3178 "of size %E overflows the destination")
3179 : G_("%K%qD writing %E bytes into a region "
3180 "of size %E overflows the destination")),
3181 exp, func, range[0], dstsize);
3182 else if (tree_int_cst_sign_bit (range[1]))
3184 /* Avoid printing the upper bound if it's invalid. */
3185 warning_at (loc, opt,
3186 "%K%qD writing %E or more bytes into a region "
3187 "of size %E overflows the destination",
3188 exp, func, range[0], dstsize);
3190 else
3191 warning_at (loc, opt,
3192 "%K%qD writing between %E and %E bytes into "
3193 "a region of size %E overflows the destination",
3194 exp, func, range[0], range[1],
3195 dstsize);
3197 /* Return error when an overflow has been detected. */
3198 return false;
3202 /* Check the maximum length of the source sequence against the size
3203 of the destination object if known, or against the maximum size
3204 of an object. */
3205 if (maxread)
3207 get_size_range (maxread, range);
3209 /* Use the lower end for MAXREAD from now on. */
3210 if (range[0])
3211 maxread = range[0];
3213 if (range[0] && dstsize && tree_fits_uhwi_p (dstsize))
3215 location_t loc = tree_nonartificial_location (exp);
3216 loc = expansion_point_location_if_in_system_header (loc);
3218 if (tree_int_cst_lt (maxobjsize, range[0]))
3220 if (TREE_NO_WARNING (exp))
3221 return false;
3223 /* Warn about crazy big sizes first since that's more
3224 likely to be meaningful than saying that the bound
3225 is greater than the object size if both are big. */
3226 if (range[0] == range[1])
3227 warning_at (loc, opt,
3228 "%K%qD specified bound %E "
3229 "exceeds maximum object size %E",
3230 exp, func,
3231 range[0], maxobjsize);
3232 else
3233 warning_at (loc, opt,
3234 "%K%qD specified bound between %E and %E "
3235 "exceeds maximum object size %E",
3236 exp, func,
3237 range[0], range[1], maxobjsize);
3239 return false;
3242 if (dstsize != maxobjsize && tree_int_cst_lt (dstsize, range[0]))
3244 if (TREE_NO_WARNING (exp))
3245 return false;
3247 if (tree_int_cst_equal (range[0], range[1]))
3248 warning_at (loc, opt,
3249 "%K%qD specified bound %E "
3250 "exceeds destination size %E",
3251 exp, func,
3252 range[0], dstsize);
3253 else
3254 warning_at (loc, opt,
3255 "%K%qD specified bound between %E and %E "
3256 "exceeds destination size %E",
3257 exp, func,
3258 range[0], range[1], dstsize);
3259 return false;
3264 /* Check for reading past the end of SRC. */
3265 if (slen
3266 && slen == srcstr
3267 && dstwrite && range[0]
3268 && tree_int_cst_lt (slen, range[0]))
3270 if (TREE_NO_WARNING (exp))
3271 return false;
3273 location_t loc = tree_nonartificial_location (exp);
3275 if (tree_int_cst_equal (range[0], range[1]))
3276 warning_at (loc, opt,
3277 (tree_int_cst_equal (range[0], integer_one_node)
3278 ? G_("%K%qD reading %E byte from a region of size %E")
3279 : G_("%K%qD reading %E bytes from a region of size %E")),
3280 exp, func, range[0], slen);
3281 else if (tree_int_cst_sign_bit (range[1]))
3283 /* Avoid printing the upper bound if it's invalid. */
3284 warning_at (loc, opt,
3285 "%K%qD reading %E or more bytes from a region "
3286 "of size %E",
3287 exp, func, range[0], slen);
3289 else
3290 warning_at (loc, opt,
3291 "%K%qD reading between %E and %E bytes from a region "
3292 "of size %E",
3293 exp, func, range[0], range[1], slen);
3294 return false;
3297 return true;
3300 /* Helper to compute the size of the object referenced by the DEST
3301 expression which must have pointer type, using Object Size type
3302 OSTYPE (only the least significant 2 bits are used). Return
3303 an estimate of the size of the object if successful or NULL when
3304 the size cannot be determined. When the referenced object involves
3305 a non-constant offset in some range the returned value represents
3306 the largest size given the smallest non-negative offset in the
3307 range. The function is intended for diagnostics and should not
3308 be used to influence code generation or optimization. */
3310 tree
3311 compute_objsize (tree dest, int ostype)
3313 unsigned HOST_WIDE_INT size;
3315 /* Only the two least significant bits are meaningful. */
3316 ostype &= 3;
3318 if (compute_builtin_object_size (dest, ostype, &size))
3319 return build_int_cst (sizetype, size);
3321 if (TREE_CODE (dest) == SSA_NAME)
3323 gimple *stmt = SSA_NAME_DEF_STMT (dest);
3324 if (!is_gimple_assign (stmt))
3325 return NULL_TREE;
3327 dest = gimple_assign_rhs1 (stmt);
3329 tree_code code = gimple_assign_rhs_code (stmt);
3330 if (code == POINTER_PLUS_EXPR)
3332 /* compute_builtin_object_size fails for addresses with
3333 non-constant offsets. Try to determine the range of
3334 such an offset here and use it to adjus the constant
3335 size. */
3336 tree off = gimple_assign_rhs2 (stmt);
3337 if (TREE_CODE (off) == SSA_NAME
3338 && INTEGRAL_TYPE_P (TREE_TYPE (off)))
3340 wide_int min, max;
3341 enum value_range_type rng = get_range_info (off, &min, &max);
3343 if (rng == VR_RANGE)
3345 if (tree size = compute_objsize (dest, ostype))
3347 wide_int wisiz = wi::to_wide (size);
3349 /* Ignore negative offsets for now. For others,
3350 use the lower bound as the most optimistic
3351 estimate of the (remaining)size. */
3352 if (wi::sign_mask (min))
3354 else if (wi::ltu_p (min, wisiz))
3355 return wide_int_to_tree (TREE_TYPE (size),
3356 wi::sub (wisiz, min));
3357 else
3358 return size_zero_node;
3363 else if (code != ADDR_EXPR)
3364 return NULL_TREE;
3367 /* Unless computing the largest size (for memcpy and other raw memory
3368 functions), try to determine the size of the object from its type. */
3369 if (!ostype)
3370 return NULL_TREE;
3372 if (TREE_CODE (dest) != ADDR_EXPR)
3373 return NULL_TREE;
3375 tree type = TREE_TYPE (dest);
3376 if (TREE_CODE (type) == POINTER_TYPE)
3377 type = TREE_TYPE (type);
3379 type = TYPE_MAIN_VARIANT (type);
3381 if (TREE_CODE (type) == ARRAY_TYPE
3382 && !array_at_struct_end_p (dest))
3384 /* Return the constant size unless it's zero (that's a zero-length
3385 array likely at the end of a struct). */
3386 tree size = TYPE_SIZE_UNIT (type);
3387 if (size && TREE_CODE (size) == INTEGER_CST
3388 && !integer_zerop (size))
3389 return size;
3392 return NULL_TREE;
3395 /* Helper to determine and check the sizes of the source and the destination
3396 of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. EXP is the
3397 call expression, DEST is the destination argument, SRC is the source
3398 argument or null, and LEN is the number of bytes. Use Object Size type-0
3399 regardless of the OPT_Wstringop_overflow_ setting. Return true on success
3400 (no overflow or invalid sizes), false otherwise. */
3402 static bool
3403 check_memop_access (tree exp, tree dest, tree src, tree size)
3405 /* For functions like memset and memcpy that operate on raw memory
3406 try to determine the size of the largest source and destination
3407 object using type-0 Object Size regardless of the object size
3408 type specified by the option. */
3409 tree srcsize = src ? compute_objsize (src, 0) : NULL_TREE;
3410 tree dstsize = compute_objsize (dest, 0);
3412 return check_access (exp, dest, src, size, /*maxread=*/NULL_TREE,
3413 srcsize, dstsize);
3416 /* Validate memchr arguments without performing any expansion.
3417 Return NULL_RTX. */
3419 static rtx
3420 expand_builtin_memchr (tree exp, rtx)
3422 if (!validate_arglist (exp,
3423 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3424 return NULL_RTX;
3426 tree arg1 = CALL_EXPR_ARG (exp, 0);
3427 tree len = CALL_EXPR_ARG (exp, 2);
3429 /* Diagnose calls where the specified length exceeds the size
3430 of the object. */
3431 if (warn_stringop_overflow)
3433 tree size = compute_objsize (arg1, 0);
3434 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
3435 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE);
3438 return NULL_RTX;
3441 /* Expand a call EXP to the memcpy builtin.
3442 Return NULL_RTX if we failed, the caller should emit a normal call,
3443 otherwise try to get the result in TARGET, if convenient (and in
3444 mode MODE if that's convenient). */
3446 static rtx
3447 expand_builtin_memcpy (tree exp, rtx target)
3449 if (!validate_arglist (exp,
3450 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3451 return NULL_RTX;
3453 tree dest = CALL_EXPR_ARG (exp, 0);
3454 tree src = CALL_EXPR_ARG (exp, 1);
3455 tree len = CALL_EXPR_ARG (exp, 2);
3457 check_memop_access (exp, dest, src, len);
3459 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3460 /*endp=*/ 0);
3463 /* Check a call EXP to the memmove built-in for validity.
3464 Return NULL_RTX on both success and failure. */
3466 static rtx
3467 expand_builtin_memmove (tree exp, rtx)
3469 if (!validate_arglist (exp,
3470 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3471 return NULL_RTX;
3473 tree dest = CALL_EXPR_ARG (exp, 0);
3474 tree src = CALL_EXPR_ARG (exp, 1);
3475 tree len = CALL_EXPR_ARG (exp, 2);
3477 check_memop_access (exp, dest, src, len);
3479 return NULL_RTX;
3482 /* Expand an instrumented call EXP to the memcpy builtin.
3483 Return NULL_RTX if we failed, the caller should emit a normal call,
3484 otherwise try to get the result in TARGET, if convenient (and in
3485 mode MODE if that's convenient). */
3487 static rtx
3488 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3490 if (!validate_arglist (exp,
3491 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3492 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3493 INTEGER_TYPE, VOID_TYPE))
3494 return NULL_RTX;
3495 else
3497 tree dest = CALL_EXPR_ARG (exp, 0);
3498 tree src = CALL_EXPR_ARG (exp, 2);
3499 tree len = CALL_EXPR_ARG (exp, 4);
3500 rtx res = expand_builtin_memory_copy_args (dest, src, len, target, exp,
3501 /*end_p=*/ 0);
3503 /* Return src bounds with the result. */
3504 if (res)
3506 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3507 expand_normal (CALL_EXPR_ARG (exp, 1)));
3508 res = chkp_join_splitted_slot (res, bnd);
3510 return res;
3514 /* Expand a call EXP to the mempcpy builtin.
3515 Return NULL_RTX if we failed; the caller should emit a normal call,
3516 otherwise try to get the result in TARGET, if convenient (and in
3517 mode MODE if that's convenient). If ENDP is 0 return the
3518 destination pointer, if ENDP is 1 return the end pointer ala
3519 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3520 stpcpy. */
3522 static rtx
3523 expand_builtin_mempcpy (tree exp, rtx target)
3525 if (!validate_arglist (exp,
3526 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3527 return NULL_RTX;
3529 tree dest = CALL_EXPR_ARG (exp, 0);
3530 tree src = CALL_EXPR_ARG (exp, 1);
3531 tree len = CALL_EXPR_ARG (exp, 2);
3533 /* Policy does not generally allow using compute_objsize (which
3534 is used internally by check_memop_size) to change code generation
3535 or drive optimization decisions.
3537 In this instance it is safe because the code we generate has
3538 the same semantics regardless of the return value of
3539 check_memop_sizes. Exactly the same amount of data is copied
3540 and the return value is exactly the same in both cases.
3542 Furthermore, check_memop_size always uses mode 0 for the call to
3543 compute_objsize, so the imprecise nature of compute_objsize is
3544 avoided. */
3546 /* Avoid expanding mempcpy into memcpy when the call is determined
3547 to overflow the buffer. This also prevents the same overflow
3548 from being diagnosed again when expanding memcpy. */
3549 if (!check_memop_access (exp, dest, src, len))
3550 return NULL_RTX;
3552 return expand_builtin_mempcpy_args (dest, src, len,
3553 target, exp, /*endp=*/ 1);
3556 /* Expand an instrumented call EXP to the mempcpy builtin.
3557 Return NULL_RTX if we failed, the caller should emit a normal call,
3558 otherwise try to get the result in TARGET, if convenient (and in
3559 mode MODE if that's convenient). */
3561 static rtx
3562 expand_builtin_mempcpy_with_bounds (tree exp, rtx target)
3564 if (!validate_arglist (exp,
3565 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3566 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3567 INTEGER_TYPE, VOID_TYPE))
3568 return NULL_RTX;
3569 else
3571 tree dest = CALL_EXPR_ARG (exp, 0);
3572 tree src = CALL_EXPR_ARG (exp, 2);
3573 tree len = CALL_EXPR_ARG (exp, 4);
3574 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3575 exp, 1);
3577 /* Return src bounds with the result. */
3578 if (res)
3580 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3581 expand_normal (CALL_EXPR_ARG (exp, 1)));
3582 res = chkp_join_splitted_slot (res, bnd);
3584 return res;
3588 /* Helper function to do the actual work for expand of memory copy family
3589 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3590 of memory from SRC to DEST and assign to TARGET if convenient.
3591 If ENDP is 0 return the
3592 destination pointer, if ENDP is 1 return the end pointer ala
3593 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3594 stpcpy. */
3596 static rtx
3597 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3598 rtx target, tree exp, int endp)
3600 const char *src_str;
3601 unsigned int src_align = get_pointer_alignment (src);
3602 unsigned int dest_align = get_pointer_alignment (dest);
3603 rtx dest_mem, src_mem, dest_addr, len_rtx;
3604 HOST_WIDE_INT expected_size = -1;
3605 unsigned int expected_align = 0;
3606 unsigned HOST_WIDE_INT min_size;
3607 unsigned HOST_WIDE_INT max_size;
3608 unsigned HOST_WIDE_INT probable_max_size;
3610 /* If DEST is not a pointer type, call the normal function. */
3611 if (dest_align == 0)
3612 return NULL_RTX;
3614 /* If either SRC is not a pointer type, don't do this
3615 operation in-line. */
3616 if (src_align == 0)
3617 return NULL_RTX;
3619 if (currently_expanding_gimple_stmt)
3620 stringop_block_profile (currently_expanding_gimple_stmt,
3621 &expected_align, &expected_size);
3623 if (expected_align < dest_align)
3624 expected_align = dest_align;
3625 dest_mem = get_memory_rtx (dest, len);
3626 set_mem_align (dest_mem, dest_align);
3627 len_rtx = expand_normal (len);
3628 determine_block_size (len, len_rtx, &min_size, &max_size,
3629 &probable_max_size);
3630 src_str = c_getstr (src);
3632 /* If SRC is a string constant and block move would be done
3633 by pieces, we can avoid loading the string from memory
3634 and only stored the computed constants. */
3635 if (src_str
3636 && CONST_INT_P (len_rtx)
3637 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3638 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3639 CONST_CAST (char *, src_str),
3640 dest_align, false))
3642 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3643 builtin_memcpy_read_str,
3644 CONST_CAST (char *, src_str),
3645 dest_align, false, endp);
3646 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3647 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3648 return dest_mem;
3651 src_mem = get_memory_rtx (src, len);
3652 set_mem_align (src_mem, src_align);
3654 /* Copy word part most expediently. */
3655 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3656 CALL_EXPR_TAILCALL (exp)
3657 && (endp == 0 || target == const0_rtx)
3658 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3659 expected_align, expected_size,
3660 min_size, max_size, probable_max_size);
3662 if (dest_addr == 0)
3664 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3665 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3668 if (endp && target != const0_rtx)
3670 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3671 /* stpcpy pointer to last byte. */
3672 if (endp == 2)
3673 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3676 return dest_addr;
3679 static rtx
3680 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3681 rtx target, tree orig_exp, int endp)
3683 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3684 endp);
3687 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3688 we failed, the caller should emit a normal call, otherwise try to
3689 get the result in TARGET, if convenient. If ENDP is 0 return the
3690 destination pointer, if ENDP is 1 return the end pointer ala
3691 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3692 stpcpy. */
3694 static rtx
3695 expand_movstr (tree dest, tree src, rtx target, int endp)
3697 struct expand_operand ops[3];
3698 rtx dest_mem;
3699 rtx src_mem;
3701 if (!targetm.have_movstr ())
3702 return NULL_RTX;
3704 dest_mem = get_memory_rtx (dest, NULL);
3705 src_mem = get_memory_rtx (src, NULL);
3706 if (!endp)
3708 target = force_reg (Pmode, XEXP (dest_mem, 0));
3709 dest_mem = replace_equiv_address (dest_mem, target);
3712 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3713 create_fixed_operand (&ops[1], dest_mem);
3714 create_fixed_operand (&ops[2], src_mem);
3715 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3716 return NULL_RTX;
3718 if (endp && target != const0_rtx)
3720 target = ops[0].value;
3721 /* movstr is supposed to set end to the address of the NUL
3722 terminator. If the caller requested a mempcpy-like return value,
3723 adjust it. */
3724 if (endp == 1)
3726 rtx tem = plus_constant (GET_MODE (target),
3727 gen_lowpart (GET_MODE (target), target), 1);
3728 emit_move_insn (target, force_operand (tem, NULL_RTX));
3731 return target;
3734 /* Do some very basic size validation of a call to the strcpy builtin
3735 given by EXP. Return NULL_RTX to have the built-in expand to a call
3736 to the library function. */
3738 static rtx
3739 expand_builtin_strcat (tree exp, rtx)
3741 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
3742 || !warn_stringop_overflow)
3743 return NULL_RTX;
3745 tree dest = CALL_EXPR_ARG (exp, 0);
3746 tree src = CALL_EXPR_ARG (exp, 1);
3748 /* There is no way here to determine the length of the string in
3749 the destination to which the SRC string is being appended so
3750 just diagnose cases when the souce string is longer than
3751 the destination object. */
3753 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3755 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, src,
3756 destsize);
3758 return NULL_RTX;
3761 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3762 NULL_RTX if we failed the caller should emit a normal call, otherwise
3763 try to get the result in TARGET, if convenient (and in mode MODE if that's
3764 convenient). */
3766 static rtx
3767 expand_builtin_strcpy (tree exp, rtx target)
3769 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3770 return NULL_RTX;
3772 tree dest = CALL_EXPR_ARG (exp, 0);
3773 tree src = CALL_EXPR_ARG (exp, 1);
3775 if (warn_stringop_overflow)
3777 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3778 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3779 src, destsize);
3782 return expand_builtin_strcpy_args (dest, src, target);
3785 /* Helper function to do the actual work for expand_builtin_strcpy. The
3786 arguments to the builtin_strcpy call DEST and SRC are broken out
3787 so that this can also be called without constructing an actual CALL_EXPR.
3788 The other arguments and return value are the same as for
3789 expand_builtin_strcpy. */
3791 static rtx
3792 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3794 return expand_movstr (dest, src, target, /*endp=*/0);
3797 /* Expand a call EXP to the stpcpy builtin.
3798 Return NULL_RTX if we failed the caller should emit a normal call,
3799 otherwise try to get the result in TARGET, if convenient (and in
3800 mode MODE if that's convenient). */
3802 static rtx
3803 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3805 tree dst, src;
3806 location_t loc = EXPR_LOCATION (exp);
3808 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3809 return NULL_RTX;
3811 dst = CALL_EXPR_ARG (exp, 0);
3812 src = CALL_EXPR_ARG (exp, 1);
3814 if (warn_stringop_overflow)
3816 tree destsize = compute_objsize (dst, warn_stringop_overflow - 1);
3817 check_access (exp, dst, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3818 src, destsize);
3821 /* If return value is ignored, transform stpcpy into strcpy. */
3822 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3824 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3825 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3826 return expand_expr (result, target, mode, EXPAND_NORMAL);
3828 else
3830 tree len, lenp1;
3831 rtx ret;
3833 /* Ensure we get an actual string whose length can be evaluated at
3834 compile-time, not an expression containing a string. This is
3835 because the latter will potentially produce pessimized code
3836 when used to produce the return value. */
3837 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3838 return expand_movstr (dst, src, target, /*endp=*/2);
3840 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3841 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3842 target, exp, /*endp=*/2);
3844 if (ret)
3845 return ret;
3847 if (TREE_CODE (len) == INTEGER_CST)
3849 rtx len_rtx = expand_normal (len);
3851 if (CONST_INT_P (len_rtx))
3853 ret = expand_builtin_strcpy_args (dst, src, target);
3855 if (ret)
3857 if (! target)
3859 if (mode != VOIDmode)
3860 target = gen_reg_rtx (mode);
3861 else
3862 target = gen_reg_rtx (GET_MODE (ret));
3864 if (GET_MODE (target) != GET_MODE (ret))
3865 ret = gen_lowpart (GET_MODE (target), ret);
3867 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3868 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3869 gcc_assert (ret);
3871 return target;
3876 return expand_movstr (dst, src, target, /*endp=*/2);
3880 /* Check a call EXP to the stpncpy built-in for validity.
3881 Return NULL_RTX on both success and failure. */
3883 static rtx
3884 expand_builtin_stpncpy (tree exp, rtx)
3886 if (!validate_arglist (exp,
3887 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3888 || !warn_stringop_overflow)
3889 return NULL_RTX;
3891 /* The source and destination of the call. */
3892 tree dest = CALL_EXPR_ARG (exp, 0);
3893 tree src = CALL_EXPR_ARG (exp, 1);
3895 /* The exact number of bytes to write (not the maximum). */
3896 tree len = CALL_EXPR_ARG (exp, 2);
3898 /* The size of the destination object. */
3899 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3901 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src, destsize);
3903 return NULL_RTX;
3906 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3907 bytes from constant string DATA + OFFSET and return it as target
3908 constant. */
3911 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3912 scalar_int_mode mode)
3914 const char *str = (const char *) data;
3916 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3917 return const0_rtx;
3919 return c_readstr (str + offset, mode);
3922 /* Helper to check the sizes of sequences and the destination of calls
3923 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3924 success (no overflow or invalid sizes), false otherwise. */
3926 static bool
3927 check_strncat_sizes (tree exp, tree objsize)
3929 tree dest = CALL_EXPR_ARG (exp, 0);
3930 tree src = CALL_EXPR_ARG (exp, 1);
3931 tree maxread = CALL_EXPR_ARG (exp, 2);
3933 /* Try to determine the range of lengths that the source expression
3934 refers to. */
3935 tree lenrange[2];
3936 get_range_strlen (src, lenrange);
3938 /* Try to verify that the destination is big enough for the shortest
3939 string. */
3941 if (!objsize && warn_stringop_overflow)
3943 /* If it hasn't been provided by __strncat_chk, try to determine
3944 the size of the destination object into which the source is
3945 being copied. */
3946 objsize = compute_objsize (dest, warn_stringop_overflow - 1);
3949 /* Add one for the terminating nul. */
3950 tree srclen = (lenrange[0]
3951 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3952 size_one_node)
3953 : NULL_TREE);
3955 /* The strncat function copies at most MAXREAD bytes and always appends
3956 the terminating nul so the specified upper bound should never be equal
3957 to (or greater than) the size of the destination. */
3958 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
3959 && tree_int_cst_equal (objsize, maxread))
3961 location_t loc = tree_nonartificial_location (exp);
3962 loc = expansion_point_location_if_in_system_header (loc);
3964 warning_at (loc, OPT_Wstringop_overflow_,
3965 "%K%qD specified bound %E equals destination size",
3966 exp, get_callee_fndecl (exp), maxread);
3968 return false;
3971 if (!srclen
3972 || (maxread && tree_fits_uhwi_p (maxread)
3973 && tree_fits_uhwi_p (srclen)
3974 && tree_int_cst_lt (maxread, srclen)))
3975 srclen = maxread;
3977 /* The number of bytes to write is LEN but check_access will also
3978 check SRCLEN if LEN's value isn't known. */
3979 return check_access (exp, dest, src, /*size=*/NULL_TREE, maxread, srclen,
3980 objsize);
3983 /* Similar to expand_builtin_strcat, do some very basic size validation
3984 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
3985 the built-in expand to a call to the library function. */
3987 static rtx
3988 expand_builtin_strncat (tree exp, rtx)
3990 if (!validate_arglist (exp,
3991 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3992 || !warn_stringop_overflow)
3993 return NULL_RTX;
3995 tree dest = CALL_EXPR_ARG (exp, 0);
3996 tree src = CALL_EXPR_ARG (exp, 1);
3997 /* The upper bound on the number of bytes to write. */
3998 tree maxread = CALL_EXPR_ARG (exp, 2);
3999 /* The length of the source sequence. */
4000 tree slen = c_strlen (src, 1);
4002 /* Try to determine the range of lengths that the source expression
4003 refers to. */
4004 tree lenrange[2];
4005 if (slen)
4006 lenrange[0] = lenrange[1] = slen;
4007 else
4008 get_range_strlen (src, lenrange);
4010 /* Try to verify that the destination is big enough for the shortest
4011 string. First try to determine the size of the destination object
4012 into which the source is being copied. */
4013 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
4015 /* Add one for the terminating nul. */
4016 tree srclen = (lenrange[0]
4017 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
4018 size_one_node)
4019 : NULL_TREE);
4021 /* The strncat function copies at most MAXREAD bytes and always appends
4022 the terminating nul so the specified upper bound should never be equal
4023 to (or greater than) the size of the destination. */
4024 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize)
4025 && tree_int_cst_equal (destsize, maxread))
4027 location_t loc = tree_nonartificial_location (exp);
4028 loc = expansion_point_location_if_in_system_header (loc);
4030 warning_at (loc, OPT_Wstringop_overflow_,
4031 "%K%qD specified bound %E equals destination size",
4032 exp, get_callee_fndecl (exp), maxread);
4034 return NULL_RTX;
4037 if (!srclen
4038 || (maxread && tree_fits_uhwi_p (maxread)
4039 && tree_fits_uhwi_p (srclen)
4040 && tree_int_cst_lt (maxread, srclen)))
4041 srclen = maxread;
4043 /* The number of bytes to write is SRCLEN. */
4044 check_access (exp, dest, src, NULL_TREE, maxread, srclen, destsize);
4046 return NULL_RTX;
4049 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4050 NULL_RTX if we failed the caller should emit a normal call. */
4052 static rtx
4053 expand_builtin_strncpy (tree exp, rtx target)
4055 location_t loc = EXPR_LOCATION (exp);
4057 if (validate_arglist (exp,
4058 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4060 tree dest = CALL_EXPR_ARG (exp, 0);
4061 tree src = CALL_EXPR_ARG (exp, 1);
4062 /* The number of bytes to write (not the maximum). */
4063 tree len = CALL_EXPR_ARG (exp, 2);
4064 /* The length of the source sequence. */
4065 tree slen = c_strlen (src, 1);
4067 if (warn_stringop_overflow)
4069 tree destsize = compute_objsize (dest,
4070 warn_stringop_overflow - 1);
4072 /* The number of bytes to write is LEN but check_access will also
4073 check SLEN if LEN's value isn't known. */
4074 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src,
4075 destsize);
4078 /* We must be passed a constant len and src parameter. */
4079 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4080 return NULL_RTX;
4082 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4084 /* We're required to pad with trailing zeros if the requested
4085 len is greater than strlen(s2)+1. In that case try to
4086 use store_by_pieces, if it fails, punt. */
4087 if (tree_int_cst_lt (slen, len))
4089 unsigned int dest_align = get_pointer_alignment (dest);
4090 const char *p = c_getstr (src);
4091 rtx dest_mem;
4093 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4094 || !can_store_by_pieces (tree_to_uhwi (len),
4095 builtin_strncpy_read_str,
4096 CONST_CAST (char *, p),
4097 dest_align, false))
4098 return NULL_RTX;
4100 dest_mem = get_memory_rtx (dest, len);
4101 store_by_pieces (dest_mem, tree_to_uhwi (len),
4102 builtin_strncpy_read_str,
4103 CONST_CAST (char *, p), dest_align, false, 0);
4104 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4105 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4106 return dest_mem;
4109 return NULL_RTX;
4112 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4113 bytes from constant string DATA + OFFSET and return it as target
4114 constant. */
4117 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4118 scalar_int_mode mode)
4120 const char *c = (const char *) data;
4121 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
4123 memset (p, *c, GET_MODE_SIZE (mode));
4125 return c_readstr (p, mode);
4128 /* Callback routine for store_by_pieces. Return the RTL of a register
4129 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4130 char value given in the RTL register data. For example, if mode is
4131 4 bytes wide, return the RTL for 0x01010101*data. */
4133 static rtx
4134 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4135 scalar_int_mode mode)
4137 rtx target, coeff;
4138 size_t size;
4139 char *p;
4141 size = GET_MODE_SIZE (mode);
4142 if (size == 1)
4143 return (rtx) data;
4145 p = XALLOCAVEC (char, size);
4146 memset (p, 1, size);
4147 coeff = c_readstr (p, mode);
4149 target = convert_to_mode (mode, (rtx) data, 1);
4150 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4151 return force_reg (mode, target);
4154 /* Expand expression EXP, which is a call to the memset builtin. Return
4155 NULL_RTX if we failed the caller should emit a normal call, otherwise
4156 try to get the result in TARGET, if convenient (and in mode MODE if that's
4157 convenient). */
4159 static rtx
4160 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4162 if (!validate_arglist (exp,
4163 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4164 return NULL_RTX;
4166 tree dest = CALL_EXPR_ARG (exp, 0);
4167 tree val = CALL_EXPR_ARG (exp, 1);
4168 tree len = CALL_EXPR_ARG (exp, 2);
4170 check_memop_access (exp, dest, NULL_TREE, len);
4172 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4175 /* Expand expression EXP, which is an instrumented call to the memset builtin.
4176 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
4177 try to get the result in TARGET, if convenient (and in mode MODE if that's
4178 convenient). */
4180 static rtx
4181 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
4183 if (!validate_arglist (exp,
4184 POINTER_TYPE, POINTER_BOUNDS_TYPE,
4185 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4186 return NULL_RTX;
4187 else
4189 tree dest = CALL_EXPR_ARG (exp, 0);
4190 tree val = CALL_EXPR_ARG (exp, 2);
4191 tree len = CALL_EXPR_ARG (exp, 3);
4192 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
4194 /* Return src bounds with the result. */
4195 if (res)
4197 rtx bnd = force_reg (targetm.chkp_bound_mode (),
4198 expand_normal (CALL_EXPR_ARG (exp, 1)));
4199 res = chkp_join_splitted_slot (res, bnd);
4201 return res;
4205 /* Helper function to do the actual work for expand_builtin_memset. The
4206 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4207 so that this can also be called without constructing an actual CALL_EXPR.
4208 The other arguments and return value are the same as for
4209 expand_builtin_memset. */
4211 static rtx
4212 expand_builtin_memset_args (tree dest, tree val, tree len,
4213 rtx target, machine_mode mode, tree orig_exp)
4215 tree fndecl, fn;
4216 enum built_in_function fcode;
4217 machine_mode val_mode;
4218 char c;
4219 unsigned int dest_align;
4220 rtx dest_mem, dest_addr, len_rtx;
4221 HOST_WIDE_INT expected_size = -1;
4222 unsigned int expected_align = 0;
4223 unsigned HOST_WIDE_INT min_size;
4224 unsigned HOST_WIDE_INT max_size;
4225 unsigned HOST_WIDE_INT probable_max_size;
4227 dest_align = get_pointer_alignment (dest);
4229 /* If DEST is not a pointer type, don't do this operation in-line. */
4230 if (dest_align == 0)
4231 return NULL_RTX;
4233 if (currently_expanding_gimple_stmt)
4234 stringop_block_profile (currently_expanding_gimple_stmt,
4235 &expected_align, &expected_size);
4237 if (expected_align < dest_align)
4238 expected_align = dest_align;
4240 /* If the LEN parameter is zero, return DEST. */
4241 if (integer_zerop (len))
4243 /* Evaluate and ignore VAL in case it has side-effects. */
4244 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4245 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4248 /* Stabilize the arguments in case we fail. */
4249 dest = builtin_save_expr (dest);
4250 val = builtin_save_expr (val);
4251 len = builtin_save_expr (len);
4253 len_rtx = expand_normal (len);
4254 determine_block_size (len, len_rtx, &min_size, &max_size,
4255 &probable_max_size);
4256 dest_mem = get_memory_rtx (dest, len);
4257 val_mode = TYPE_MODE (unsigned_char_type_node);
4259 if (TREE_CODE (val) != INTEGER_CST)
4261 rtx val_rtx;
4263 val_rtx = expand_normal (val);
4264 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4266 /* Assume that we can memset by pieces if we can store
4267 * the coefficients by pieces (in the required modes).
4268 * We can't pass builtin_memset_gen_str as that emits RTL. */
4269 c = 1;
4270 if (tree_fits_uhwi_p (len)
4271 && can_store_by_pieces (tree_to_uhwi (len),
4272 builtin_memset_read_str, &c, dest_align,
4273 true))
4275 val_rtx = force_reg (val_mode, val_rtx);
4276 store_by_pieces (dest_mem, tree_to_uhwi (len),
4277 builtin_memset_gen_str, val_rtx, dest_align,
4278 true, 0);
4280 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4281 dest_align, expected_align,
4282 expected_size, min_size, max_size,
4283 probable_max_size))
4284 goto do_libcall;
4286 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4287 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4288 return dest_mem;
4291 if (target_char_cast (val, &c))
4292 goto do_libcall;
4294 if (c)
4296 if (tree_fits_uhwi_p (len)
4297 && can_store_by_pieces (tree_to_uhwi (len),
4298 builtin_memset_read_str, &c, dest_align,
4299 true))
4300 store_by_pieces (dest_mem, tree_to_uhwi (len),
4301 builtin_memset_read_str, &c, dest_align, true, 0);
4302 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4303 gen_int_mode (c, val_mode),
4304 dest_align, expected_align,
4305 expected_size, min_size, max_size,
4306 probable_max_size))
4307 goto do_libcall;
4309 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4310 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4311 return dest_mem;
4314 set_mem_align (dest_mem, dest_align);
4315 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4316 CALL_EXPR_TAILCALL (orig_exp)
4317 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4318 expected_align, expected_size,
4319 min_size, max_size,
4320 probable_max_size);
4322 if (dest_addr == 0)
4324 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4325 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4328 return dest_addr;
4330 do_libcall:
4331 fndecl = get_callee_fndecl (orig_exp);
4332 fcode = DECL_FUNCTION_CODE (fndecl);
4333 if (fcode == BUILT_IN_MEMSET
4334 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
4335 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4336 dest, val, len);
4337 else if (fcode == BUILT_IN_BZERO)
4338 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4339 dest, len);
4340 else
4341 gcc_unreachable ();
4342 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4343 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4344 return expand_call (fn, target, target == const0_rtx);
4347 /* Expand expression EXP, which is a call to the bzero builtin. Return
4348 NULL_RTX if we failed the caller should emit a normal call. */
4350 static rtx
4351 expand_builtin_bzero (tree exp)
4353 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4354 return NULL_RTX;
4356 tree dest = CALL_EXPR_ARG (exp, 0);
4357 tree size = CALL_EXPR_ARG (exp, 1);
4359 check_memop_access (exp, dest, NULL_TREE, size);
4361 /* New argument list transforming bzero(ptr x, int y) to
4362 memset(ptr x, int 0, size_t y). This is done this way
4363 so that if it isn't expanded inline, we fallback to
4364 calling bzero instead of memset. */
4366 location_t loc = EXPR_LOCATION (exp);
4368 return expand_builtin_memset_args (dest, integer_zero_node,
4369 fold_convert_loc (loc,
4370 size_type_node, size),
4371 const0_rtx, VOIDmode, exp);
4374 /* Try to expand cmpstr operation ICODE with the given operands.
4375 Return the result rtx on success, otherwise return null. */
4377 static rtx
4378 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4379 HOST_WIDE_INT align)
4381 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4383 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4384 target = NULL_RTX;
4386 struct expand_operand ops[4];
4387 create_output_operand (&ops[0], target, insn_mode);
4388 create_fixed_operand (&ops[1], arg1_rtx);
4389 create_fixed_operand (&ops[2], arg2_rtx);
4390 create_integer_operand (&ops[3], align);
4391 if (maybe_expand_insn (icode, 4, ops))
4392 return ops[0].value;
4393 return NULL_RTX;
4396 /* Expand expression EXP, which is a call to the memcmp built-in function.
4397 Return NULL_RTX if we failed and the caller should emit a normal call,
4398 otherwise try to get the result in TARGET, if convenient.
4399 RESULT_EQ is true if we can relax the returned value to be either zero
4400 or nonzero, without caring about the sign. */
4402 static rtx
4403 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4405 if (!validate_arglist (exp,
4406 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4407 return NULL_RTX;
4409 tree arg1 = CALL_EXPR_ARG (exp, 0);
4410 tree arg2 = CALL_EXPR_ARG (exp, 1);
4411 tree len = CALL_EXPR_ARG (exp, 2);
4413 /* Diagnose calls where the specified length exceeds the size of either
4414 object. */
4415 if (warn_stringop_overflow)
4417 tree size = compute_objsize (arg1, 0);
4418 if (check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4419 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE))
4421 size = compute_objsize (arg2, 0);
4422 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4423 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE);
4427 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4428 location_t loc = EXPR_LOCATION (exp);
4430 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4431 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4433 /* If we don't have POINTER_TYPE, call the function. */
4434 if (arg1_align == 0 || arg2_align == 0)
4435 return NULL_RTX;
4437 rtx arg1_rtx = get_memory_rtx (arg1, len);
4438 rtx arg2_rtx = get_memory_rtx (arg2, len);
4439 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4441 /* Set MEM_SIZE as appropriate. */
4442 if (CONST_INT_P (len_rtx))
4444 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4445 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4448 by_pieces_constfn constfn = NULL;
4450 const char *src_str = c_getstr (arg2);
4451 if (result_eq && src_str == NULL)
4453 src_str = c_getstr (arg1);
4454 if (src_str != NULL)
4455 std::swap (arg1_rtx, arg2_rtx);
4458 /* If SRC is a string constant and block move would be done
4459 by pieces, we can avoid loading the string from memory
4460 and only stored the computed constants. */
4461 if (src_str
4462 && CONST_INT_P (len_rtx)
4463 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1)
4464 constfn = builtin_memcpy_read_str;
4466 rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4467 TREE_TYPE (len), target,
4468 result_eq, constfn,
4469 CONST_CAST (char *, src_str));
4471 if (result)
4473 /* Return the value in the proper mode for this function. */
4474 if (GET_MODE (result) == mode)
4475 return result;
4477 if (target != 0)
4479 convert_move (target, result, 0);
4480 return target;
4483 return convert_to_mode (mode, result, 0);
4486 return NULL_RTX;
4489 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4490 if we failed the caller should emit a normal call, otherwise try to get
4491 the result in TARGET, if convenient. */
4493 static rtx
4494 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4496 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4497 return NULL_RTX;
4499 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4500 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4501 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4502 return NULL_RTX;
4504 tree arg1 = CALL_EXPR_ARG (exp, 0);
4505 tree arg2 = CALL_EXPR_ARG (exp, 1);
4507 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4508 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4510 /* If we don't have POINTER_TYPE, call the function. */
4511 if (arg1_align == 0 || arg2_align == 0)
4512 return NULL_RTX;
4514 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4515 arg1 = builtin_save_expr (arg1);
4516 arg2 = builtin_save_expr (arg2);
4518 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4519 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4521 rtx result = NULL_RTX;
4522 /* Try to call cmpstrsi. */
4523 if (cmpstr_icode != CODE_FOR_nothing)
4524 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4525 MIN (arg1_align, arg2_align));
4527 /* Try to determine at least one length and call cmpstrnsi. */
4528 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4530 tree len;
4531 rtx arg3_rtx;
4533 tree len1 = c_strlen (arg1, 1);
4534 tree len2 = c_strlen (arg2, 1);
4536 if (len1)
4537 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4538 if (len2)
4539 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4541 /* If we don't have a constant length for the first, use the length
4542 of the second, if we know it. We don't require a constant for
4543 this case; some cost analysis could be done if both are available
4544 but neither is constant. For now, assume they're equally cheap,
4545 unless one has side effects. If both strings have constant lengths,
4546 use the smaller. */
4548 if (!len1)
4549 len = len2;
4550 else if (!len2)
4551 len = len1;
4552 else if (TREE_SIDE_EFFECTS (len1))
4553 len = len2;
4554 else if (TREE_SIDE_EFFECTS (len2))
4555 len = len1;
4556 else if (TREE_CODE (len1) != INTEGER_CST)
4557 len = len2;
4558 else if (TREE_CODE (len2) != INTEGER_CST)
4559 len = len1;
4560 else if (tree_int_cst_lt (len1, len2))
4561 len = len1;
4562 else
4563 len = len2;
4565 /* If both arguments have side effects, we cannot optimize. */
4566 if (len && !TREE_SIDE_EFFECTS (len))
4568 arg3_rtx = expand_normal (len);
4569 result = expand_cmpstrn_or_cmpmem
4570 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4571 arg3_rtx, MIN (arg1_align, arg2_align));
4575 /* Check to see if the argument was declared attribute nonstring
4576 and if so, issue a warning since at this point it's not known
4577 to be nul-terminated. */
4578 tree fndecl = get_callee_fndecl (exp);
4579 maybe_warn_nonstring_arg (fndecl, exp);
4581 if (result)
4583 /* Return the value in the proper mode for this function. */
4584 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4585 if (GET_MODE (result) == mode)
4586 return result;
4587 if (target == 0)
4588 return convert_to_mode (mode, result, 0);
4589 convert_move (target, result, 0);
4590 return target;
4593 /* Expand the library call ourselves using a stabilized argument
4594 list to avoid re-evaluating the function's arguments twice. */
4595 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4596 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4597 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4598 return expand_call (fn, target, target == const0_rtx);
4601 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4602 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4603 the result in TARGET, if convenient. */
4605 static rtx
4606 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4607 ATTRIBUTE_UNUSED machine_mode mode)
4609 if (!validate_arglist (exp,
4610 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4611 return NULL_RTX;
4613 /* If c_strlen can determine an expression for one of the string
4614 lengths, and it doesn't have side effects, then emit cmpstrnsi
4615 using length MIN(strlen(string)+1, arg3). */
4616 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4617 if (cmpstrn_icode == CODE_FOR_nothing)
4618 return NULL_RTX;
4620 tree len;
4622 tree arg1 = CALL_EXPR_ARG (exp, 0);
4623 tree arg2 = CALL_EXPR_ARG (exp, 1);
4624 tree arg3 = CALL_EXPR_ARG (exp, 2);
4626 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4627 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4629 tree len1 = c_strlen (arg1, 1);
4630 tree len2 = c_strlen (arg2, 1);
4632 location_t loc = EXPR_LOCATION (exp);
4634 if (len1)
4635 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4636 if (len2)
4637 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4639 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4641 /* If we don't have a constant length for the first, use the length
4642 of the second, if we know it. If neither string is constant length,
4643 use the given length argument. We don't require a constant for
4644 this case; some cost analysis could be done if both are available
4645 but neither is constant. For now, assume they're equally cheap,
4646 unless one has side effects. If both strings have constant lengths,
4647 use the smaller. */
4649 if (!len1 && !len2)
4650 len = len3;
4651 else if (!len1)
4652 len = len2;
4653 else if (!len2)
4654 len = len1;
4655 else if (TREE_SIDE_EFFECTS (len1))
4656 len = len2;
4657 else if (TREE_SIDE_EFFECTS (len2))
4658 len = len1;
4659 else if (TREE_CODE (len1) != INTEGER_CST)
4660 len = len2;
4661 else if (TREE_CODE (len2) != INTEGER_CST)
4662 len = len1;
4663 else if (tree_int_cst_lt (len1, len2))
4664 len = len1;
4665 else
4666 len = len2;
4668 /* If we are not using the given length, we must incorporate it here.
4669 The actual new length parameter will be MIN(len,arg3) in this case. */
4670 if (len != len3)
4671 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4672 rtx arg1_rtx = get_memory_rtx (arg1, len);
4673 rtx arg2_rtx = get_memory_rtx (arg2, len);
4674 rtx arg3_rtx = expand_normal (len);
4675 rtx result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4676 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4677 MIN (arg1_align, arg2_align));
4679 /* Check to see if the argument was declared attribute nonstring
4680 and if so, issue a warning since at this point it's not known
4681 to be nul-terminated. */
4682 tree fndecl = get_callee_fndecl (exp);
4683 maybe_warn_nonstring_arg (fndecl, exp);
4685 if (result)
4687 /* Return the value in the proper mode for this function. */
4688 mode = TYPE_MODE (TREE_TYPE (exp));
4689 if (GET_MODE (result) == mode)
4690 return result;
4691 if (target == 0)
4692 return convert_to_mode (mode, result, 0);
4693 convert_move (target, result, 0);
4694 return target;
4697 /* Expand the library call ourselves using a stabilized argument
4698 list to avoid re-evaluating the function's arguments twice. */
4699 tree fn = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4700 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4701 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4702 return expand_call (fn, target, target == const0_rtx);
4705 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4706 if that's convenient. */
4709 expand_builtin_saveregs (void)
4711 rtx val;
4712 rtx_insn *seq;
4714 /* Don't do __builtin_saveregs more than once in a function.
4715 Save the result of the first call and reuse it. */
4716 if (saveregs_value != 0)
4717 return saveregs_value;
4719 /* When this function is called, it means that registers must be
4720 saved on entry to this function. So we migrate the call to the
4721 first insn of this function. */
4723 start_sequence ();
4725 /* Do whatever the machine needs done in this case. */
4726 val = targetm.calls.expand_builtin_saveregs ();
4728 seq = get_insns ();
4729 end_sequence ();
4731 saveregs_value = val;
4733 /* Put the insns after the NOTE that starts the function. If this
4734 is inside a start_sequence, make the outer-level insn chain current, so
4735 the code is placed at the start of the function. */
4736 push_topmost_sequence ();
4737 emit_insn_after (seq, entry_of_function ());
4738 pop_topmost_sequence ();
4740 return val;
4743 /* Expand a call to __builtin_next_arg. */
4745 static rtx
4746 expand_builtin_next_arg (void)
4748 /* Checking arguments is already done in fold_builtin_next_arg
4749 that must be called before this function. */
4750 return expand_binop (ptr_mode, add_optab,
4751 crtl->args.internal_arg_pointer,
4752 crtl->args.arg_offset_rtx,
4753 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4756 /* Make it easier for the backends by protecting the valist argument
4757 from multiple evaluations. */
4759 static tree
4760 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4762 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4764 /* The current way of determining the type of valist is completely
4765 bogus. We should have the information on the va builtin instead. */
4766 if (!vatype)
4767 vatype = targetm.fn_abi_va_list (cfun->decl);
4769 if (TREE_CODE (vatype) == ARRAY_TYPE)
4771 if (TREE_SIDE_EFFECTS (valist))
4772 valist = save_expr (valist);
4774 /* For this case, the backends will be expecting a pointer to
4775 vatype, but it's possible we've actually been given an array
4776 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4777 So fix it. */
4778 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4780 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4781 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4784 else
4786 tree pt = build_pointer_type (vatype);
4788 if (! needs_lvalue)
4790 if (! TREE_SIDE_EFFECTS (valist))
4791 return valist;
4793 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4794 TREE_SIDE_EFFECTS (valist) = 1;
4797 if (TREE_SIDE_EFFECTS (valist))
4798 valist = save_expr (valist);
4799 valist = fold_build2_loc (loc, MEM_REF,
4800 vatype, valist, build_int_cst (pt, 0));
4803 return valist;
4806 /* The "standard" definition of va_list is void*. */
4808 tree
4809 std_build_builtin_va_list (void)
4811 return ptr_type_node;
4814 /* The "standard" abi va_list is va_list_type_node. */
4816 tree
4817 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4819 return va_list_type_node;
4822 /* The "standard" type of va_list is va_list_type_node. */
4824 tree
4825 std_canonical_va_list_type (tree type)
4827 tree wtype, htype;
4829 wtype = va_list_type_node;
4830 htype = type;
4832 if (TREE_CODE (wtype) == ARRAY_TYPE)
4834 /* If va_list is an array type, the argument may have decayed
4835 to a pointer type, e.g. by being passed to another function.
4836 In that case, unwrap both types so that we can compare the
4837 underlying records. */
4838 if (TREE_CODE (htype) == ARRAY_TYPE
4839 || POINTER_TYPE_P (htype))
4841 wtype = TREE_TYPE (wtype);
4842 htype = TREE_TYPE (htype);
4845 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4846 return va_list_type_node;
4848 return NULL_TREE;
4851 /* The "standard" implementation of va_start: just assign `nextarg' to
4852 the variable. */
4854 void
4855 std_expand_builtin_va_start (tree valist, rtx nextarg)
4857 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4858 convert_move (va_r, nextarg, 0);
4860 /* We do not have any valid bounds for the pointer, so
4861 just store zero bounds for it. */
4862 if (chkp_function_instrumented_p (current_function_decl))
4863 chkp_expand_bounds_reset_for_mem (valist,
4864 make_tree (TREE_TYPE (valist),
4865 nextarg));
4868 /* Expand EXP, a call to __builtin_va_start. */
4870 static rtx
4871 expand_builtin_va_start (tree exp)
4873 rtx nextarg;
4874 tree valist;
4875 location_t loc = EXPR_LOCATION (exp);
4877 if (call_expr_nargs (exp) < 2)
4879 error_at (loc, "too few arguments to function %<va_start%>");
4880 return const0_rtx;
4883 if (fold_builtin_next_arg (exp, true))
4884 return const0_rtx;
4886 nextarg = expand_builtin_next_arg ();
4887 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4889 if (targetm.expand_builtin_va_start)
4890 targetm.expand_builtin_va_start (valist, nextarg);
4891 else
4892 std_expand_builtin_va_start (valist, nextarg);
4894 return const0_rtx;
4897 /* Expand EXP, a call to __builtin_va_end. */
4899 static rtx
4900 expand_builtin_va_end (tree exp)
4902 tree valist = CALL_EXPR_ARG (exp, 0);
4904 /* Evaluate for side effects, if needed. I hate macros that don't
4905 do that. */
4906 if (TREE_SIDE_EFFECTS (valist))
4907 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4909 return const0_rtx;
4912 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4913 builtin rather than just as an assignment in stdarg.h because of the
4914 nastiness of array-type va_list types. */
4916 static rtx
4917 expand_builtin_va_copy (tree exp)
4919 tree dst, src, t;
4920 location_t loc = EXPR_LOCATION (exp);
4922 dst = CALL_EXPR_ARG (exp, 0);
4923 src = CALL_EXPR_ARG (exp, 1);
4925 dst = stabilize_va_list_loc (loc, dst, 1);
4926 src = stabilize_va_list_loc (loc, src, 0);
4928 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4930 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4932 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4933 TREE_SIDE_EFFECTS (t) = 1;
4934 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4936 else
4938 rtx dstb, srcb, size;
4940 /* Evaluate to pointers. */
4941 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4942 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4943 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4944 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4946 dstb = convert_memory_address (Pmode, dstb);
4947 srcb = convert_memory_address (Pmode, srcb);
4949 /* "Dereference" to BLKmode memories. */
4950 dstb = gen_rtx_MEM (BLKmode, dstb);
4951 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4952 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4953 srcb = gen_rtx_MEM (BLKmode, srcb);
4954 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4955 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4957 /* Copy. */
4958 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4961 return const0_rtx;
4964 /* Expand a call to one of the builtin functions __builtin_frame_address or
4965 __builtin_return_address. */
4967 static rtx
4968 expand_builtin_frame_address (tree fndecl, tree exp)
4970 /* The argument must be a nonnegative integer constant.
4971 It counts the number of frames to scan up the stack.
4972 The value is either the frame pointer value or the return
4973 address saved in that frame. */
4974 if (call_expr_nargs (exp) == 0)
4975 /* Warning about missing arg was already issued. */
4976 return const0_rtx;
4977 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4979 error ("invalid argument to %qD", fndecl);
4980 return const0_rtx;
4982 else
4984 /* Number of frames to scan up the stack. */
4985 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4987 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4989 /* Some ports cannot access arbitrary stack frames. */
4990 if (tem == NULL)
4992 warning (0, "unsupported argument to %qD", fndecl);
4993 return const0_rtx;
4996 if (count)
4998 /* Warn since no effort is made to ensure that any frame
4999 beyond the current one exists or can be safely reached. */
5000 warning (OPT_Wframe_address, "calling %qD with "
5001 "a nonzero argument is unsafe", fndecl);
5004 /* For __builtin_frame_address, return what we've got. */
5005 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5006 return tem;
5008 if (!REG_P (tem)
5009 && ! CONSTANT_P (tem))
5010 tem = copy_addr_to_reg (tem);
5011 return tem;
5015 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5016 failed and the caller should emit a normal call. */
5018 static rtx
5019 expand_builtin_alloca (tree exp)
5021 rtx op0;
5022 rtx result;
5023 unsigned int align;
5024 tree fndecl = get_callee_fndecl (exp);
5025 HOST_WIDE_INT max_size;
5026 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5027 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5028 bool valid_arglist
5029 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5030 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5031 VOID_TYPE)
5032 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5033 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5034 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5036 if (!valid_arglist)
5037 return NULL_RTX;
5039 if ((alloca_for_var && !warn_vla_limit)
5040 || (!alloca_for_var && !warn_alloca_limit))
5042 /* -Walloca-larger-than and -Wvla-larger-than settings override
5043 the more general -Walloc-size-larger-than so unless either of
5044 the former options is specified check the alloca arguments for
5045 overflow. */
5046 tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE };
5047 int idx[] = { 0, -1 };
5048 maybe_warn_alloc_args_overflow (fndecl, exp, args, idx);
5051 /* Compute the argument. */
5052 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5054 /* Compute the alignment. */
5055 align = (fcode == BUILT_IN_ALLOCA
5056 ? BIGGEST_ALIGNMENT
5057 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5059 /* Compute the maximum size. */
5060 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5061 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5062 : -1);
5064 /* Allocate the desired space. If the allocation stems from the declaration
5065 of a variable-sized object, it cannot accumulate. */
5066 result
5067 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5068 result = convert_memory_address (ptr_mode, result);
5070 return result;
5073 /* Emit a call to __asan_allocas_unpoison call in EXP. Replace second argument
5074 of the call with virtual_stack_dynamic_rtx because in asan pass we emit a
5075 dummy value into second parameter relying on this function to perform the
5076 change. See motivation for this in comment to handle_builtin_stack_restore
5077 function. */
5079 static rtx
5080 expand_asan_emit_allocas_unpoison (tree exp)
5082 tree arg0 = CALL_EXPR_ARG (exp, 0);
5083 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5084 rtx bot = convert_memory_address (ptr_mode, virtual_stack_dynamic_rtx);
5085 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5086 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5087 top, ptr_mode, bot, ptr_mode);
5088 return ret;
5091 /* Expand a call to bswap builtin in EXP.
5092 Return NULL_RTX if a normal call should be emitted rather than expanding the
5093 function in-line. If convenient, the result should be placed in TARGET.
5094 SUBTARGET may be used as the target for computing one of EXP's operands. */
5096 static rtx
5097 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5098 rtx subtarget)
5100 tree arg;
5101 rtx op0;
5103 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5104 return NULL_RTX;
5106 arg = CALL_EXPR_ARG (exp, 0);
5107 op0 = expand_expr (arg,
5108 subtarget && GET_MODE (subtarget) == target_mode
5109 ? subtarget : NULL_RTX,
5110 target_mode, EXPAND_NORMAL);
5111 if (GET_MODE (op0) != target_mode)
5112 op0 = convert_to_mode (target_mode, op0, 1);
5114 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5116 gcc_assert (target);
5118 return convert_to_mode (target_mode, target, 1);
5121 /* Expand a call to a unary builtin in EXP.
5122 Return NULL_RTX if a normal call should be emitted rather than expanding the
5123 function in-line. If convenient, the result should be placed in TARGET.
5124 SUBTARGET may be used as the target for computing one of EXP's operands. */
5126 static rtx
5127 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5128 rtx subtarget, optab op_optab)
5130 rtx op0;
5132 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5133 return NULL_RTX;
5135 /* Compute the argument. */
5136 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5137 (subtarget
5138 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5139 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5140 VOIDmode, EXPAND_NORMAL);
5141 /* Compute op, into TARGET if possible.
5142 Set TARGET to wherever the result comes back. */
5143 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5144 op_optab, op0, target, op_optab != clrsb_optab);
5145 gcc_assert (target);
5147 return convert_to_mode (target_mode, target, 0);
5150 /* Expand a call to __builtin_expect. We just return our argument
5151 as the builtin_expect semantic should've been already executed by
5152 tree branch prediction pass. */
5154 static rtx
5155 expand_builtin_expect (tree exp, rtx target)
5157 tree arg;
5159 if (call_expr_nargs (exp) < 2)
5160 return const0_rtx;
5161 arg = CALL_EXPR_ARG (exp, 0);
5163 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5164 /* When guessing was done, the hints should be already stripped away. */
5165 gcc_assert (!flag_guess_branch_prob
5166 || optimize == 0 || seen_error ());
5167 return target;
5170 /* Expand a call to __builtin_assume_aligned. We just return our first
5171 argument as the builtin_assume_aligned semantic should've been already
5172 executed by CCP. */
5174 static rtx
5175 expand_builtin_assume_aligned (tree exp, rtx target)
5177 if (call_expr_nargs (exp) < 2)
5178 return const0_rtx;
5179 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5180 EXPAND_NORMAL);
5181 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5182 && (call_expr_nargs (exp) < 3
5183 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5184 return target;
5187 void
5188 expand_builtin_trap (void)
5190 if (targetm.have_trap ())
5192 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5193 /* For trap insns when not accumulating outgoing args force
5194 REG_ARGS_SIZE note to prevent crossjumping of calls with
5195 different args sizes. */
5196 if (!ACCUMULATE_OUTGOING_ARGS)
5197 add_args_size_note (insn, stack_pointer_delta);
5199 else
5201 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5202 tree call_expr = build_call_expr (fn, 0);
5203 expand_call (call_expr, NULL_RTX, false);
5206 emit_barrier ();
5209 /* Expand a call to __builtin_unreachable. We do nothing except emit
5210 a barrier saying that control flow will not pass here.
5212 It is the responsibility of the program being compiled to ensure
5213 that control flow does never reach __builtin_unreachable. */
5214 static void
5215 expand_builtin_unreachable (void)
5217 emit_barrier ();
5220 /* Expand EXP, a call to fabs, fabsf or fabsl.
5221 Return NULL_RTX if a normal call should be emitted rather than expanding
5222 the function inline. If convenient, the result should be placed
5223 in TARGET. SUBTARGET may be used as the target for computing
5224 the operand. */
5226 static rtx
5227 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5229 machine_mode mode;
5230 tree arg;
5231 rtx op0;
5233 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5234 return NULL_RTX;
5236 arg = CALL_EXPR_ARG (exp, 0);
5237 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5238 mode = TYPE_MODE (TREE_TYPE (arg));
5239 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5240 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5243 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5244 Return NULL is a normal call should be emitted rather than expanding the
5245 function inline. If convenient, the result should be placed in TARGET.
5246 SUBTARGET may be used as the target for computing the operand. */
5248 static rtx
5249 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5251 rtx op0, op1;
5252 tree arg;
5254 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5255 return NULL_RTX;
5257 arg = CALL_EXPR_ARG (exp, 0);
5258 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5260 arg = CALL_EXPR_ARG (exp, 1);
5261 op1 = expand_normal (arg);
5263 return expand_copysign (op0, op1, target);
5266 /* Expand a call to __builtin___clear_cache. */
5268 static rtx
5269 expand_builtin___clear_cache (tree exp)
5271 if (!targetm.code_for_clear_cache)
5273 #ifdef CLEAR_INSN_CACHE
5274 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5275 does something. Just do the default expansion to a call to
5276 __clear_cache(). */
5277 return NULL_RTX;
5278 #else
5279 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5280 does nothing. There is no need to call it. Do nothing. */
5281 return const0_rtx;
5282 #endif /* CLEAR_INSN_CACHE */
5285 /* We have a "clear_cache" insn, and it will handle everything. */
5286 tree begin, end;
5287 rtx begin_rtx, end_rtx;
5289 /* We must not expand to a library call. If we did, any
5290 fallback library function in libgcc that might contain a call to
5291 __builtin___clear_cache() would recurse infinitely. */
5292 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5294 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5295 return const0_rtx;
5298 if (targetm.have_clear_cache ())
5300 struct expand_operand ops[2];
5302 begin = CALL_EXPR_ARG (exp, 0);
5303 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5305 end = CALL_EXPR_ARG (exp, 1);
5306 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5308 create_address_operand (&ops[0], begin_rtx);
5309 create_address_operand (&ops[1], end_rtx);
5310 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5311 return const0_rtx;
5313 return const0_rtx;
5316 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5318 static rtx
5319 round_trampoline_addr (rtx tramp)
5321 rtx temp, addend, mask;
5323 /* If we don't need too much alignment, we'll have been guaranteed
5324 proper alignment by get_trampoline_type. */
5325 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5326 return tramp;
5328 /* Round address up to desired boundary. */
5329 temp = gen_reg_rtx (Pmode);
5330 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5331 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5333 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5334 temp, 0, OPTAB_LIB_WIDEN);
5335 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5336 temp, 0, OPTAB_LIB_WIDEN);
5338 return tramp;
5341 static rtx
5342 expand_builtin_init_trampoline (tree exp, bool onstack)
5344 tree t_tramp, t_func, t_chain;
5345 rtx m_tramp, r_tramp, r_chain, tmp;
5347 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5348 POINTER_TYPE, VOID_TYPE))
5349 return NULL_RTX;
5351 t_tramp = CALL_EXPR_ARG (exp, 0);
5352 t_func = CALL_EXPR_ARG (exp, 1);
5353 t_chain = CALL_EXPR_ARG (exp, 2);
5355 r_tramp = expand_normal (t_tramp);
5356 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5357 MEM_NOTRAP_P (m_tramp) = 1;
5359 /* If ONSTACK, the TRAMP argument should be the address of a field
5360 within the local function's FRAME decl. Either way, let's see if
5361 we can fill in the MEM_ATTRs for this memory. */
5362 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5363 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5365 /* Creator of a heap trampoline is responsible for making sure the
5366 address is aligned to at least STACK_BOUNDARY. Normally malloc
5367 will ensure this anyhow. */
5368 tmp = round_trampoline_addr (r_tramp);
5369 if (tmp != r_tramp)
5371 m_tramp = change_address (m_tramp, BLKmode, tmp);
5372 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5373 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5376 /* The FUNC argument should be the address of the nested function.
5377 Extract the actual function decl to pass to the hook. */
5378 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5379 t_func = TREE_OPERAND (t_func, 0);
5380 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5382 r_chain = expand_normal (t_chain);
5384 /* Generate insns to initialize the trampoline. */
5385 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5387 if (onstack)
5389 trampolines_created = 1;
5391 if (targetm.calls.custom_function_descriptors != 0)
5392 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5393 "trampoline generated for nested function %qD", t_func);
5396 return const0_rtx;
5399 static rtx
5400 expand_builtin_adjust_trampoline (tree exp)
5402 rtx tramp;
5404 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5405 return NULL_RTX;
5407 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5408 tramp = round_trampoline_addr (tramp);
5409 if (targetm.calls.trampoline_adjust_address)
5410 tramp = targetm.calls.trampoline_adjust_address (tramp);
5412 return tramp;
5415 /* Expand a call to the builtin descriptor initialization routine.
5416 A descriptor is made up of a couple of pointers to the static
5417 chain and the code entry in this order. */
5419 static rtx
5420 expand_builtin_init_descriptor (tree exp)
5422 tree t_descr, t_func, t_chain;
5423 rtx m_descr, r_descr, r_func, r_chain;
5425 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5426 VOID_TYPE))
5427 return NULL_RTX;
5429 t_descr = CALL_EXPR_ARG (exp, 0);
5430 t_func = CALL_EXPR_ARG (exp, 1);
5431 t_chain = CALL_EXPR_ARG (exp, 2);
5433 r_descr = expand_normal (t_descr);
5434 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5435 MEM_NOTRAP_P (m_descr) = 1;
5437 r_func = expand_normal (t_func);
5438 r_chain = expand_normal (t_chain);
5440 /* Generate insns to initialize the descriptor. */
5441 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5442 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5443 POINTER_SIZE / BITS_PER_UNIT), r_func);
5445 return const0_rtx;
5448 /* Expand a call to the builtin descriptor adjustment routine. */
5450 static rtx
5451 expand_builtin_adjust_descriptor (tree exp)
5453 rtx tramp;
5455 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5456 return NULL_RTX;
5458 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5460 /* Unalign the descriptor to allow runtime identification. */
5461 tramp = plus_constant (ptr_mode, tramp,
5462 targetm.calls.custom_function_descriptors);
5464 return force_operand (tramp, NULL_RTX);
5467 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5468 function. The function first checks whether the back end provides
5469 an insn to implement signbit for the respective mode. If not, it
5470 checks whether the floating point format of the value is such that
5471 the sign bit can be extracted. If that is not the case, error out.
5472 EXP is the expression that is a call to the builtin function; if
5473 convenient, the result should be placed in TARGET. */
5474 static rtx
5475 expand_builtin_signbit (tree exp, rtx target)
5477 const struct real_format *fmt;
5478 scalar_float_mode fmode;
5479 scalar_int_mode rmode, imode;
5480 tree arg;
5481 int word, bitpos;
5482 enum insn_code icode;
5483 rtx temp;
5484 location_t loc = EXPR_LOCATION (exp);
5486 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5487 return NULL_RTX;
5489 arg = CALL_EXPR_ARG (exp, 0);
5490 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5491 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5492 fmt = REAL_MODE_FORMAT (fmode);
5494 arg = builtin_save_expr (arg);
5496 /* Expand the argument yielding a RTX expression. */
5497 temp = expand_normal (arg);
5499 /* Check if the back end provides an insn that handles signbit for the
5500 argument's mode. */
5501 icode = optab_handler (signbit_optab, fmode);
5502 if (icode != CODE_FOR_nothing)
5504 rtx_insn *last = get_last_insn ();
5505 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5506 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5507 return target;
5508 delete_insns_since (last);
5511 /* For floating point formats without a sign bit, implement signbit
5512 as "ARG < 0.0". */
5513 bitpos = fmt->signbit_ro;
5514 if (bitpos < 0)
5516 /* But we can't do this if the format supports signed zero. */
5517 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5519 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5520 build_real (TREE_TYPE (arg), dconst0));
5521 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5524 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5526 imode = int_mode_for_mode (fmode).require ();
5527 temp = gen_lowpart (imode, temp);
5529 else
5531 imode = word_mode;
5532 /* Handle targets with different FP word orders. */
5533 if (FLOAT_WORDS_BIG_ENDIAN)
5534 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5535 else
5536 word = bitpos / BITS_PER_WORD;
5537 temp = operand_subword_force (temp, word, fmode);
5538 bitpos = bitpos % BITS_PER_WORD;
5541 /* Force the intermediate word_mode (or narrower) result into a
5542 register. This avoids attempting to create paradoxical SUBREGs
5543 of floating point modes below. */
5544 temp = force_reg (imode, temp);
5546 /* If the bitpos is within the "result mode" lowpart, the operation
5547 can be implement with a single bitwise AND. Otherwise, we need
5548 a right shift and an AND. */
5550 if (bitpos < GET_MODE_BITSIZE (rmode))
5552 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5554 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5555 temp = gen_lowpart (rmode, temp);
5556 temp = expand_binop (rmode, and_optab, temp,
5557 immed_wide_int_const (mask, rmode),
5558 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5560 else
5562 /* Perform a logical right shift to place the signbit in the least
5563 significant bit, then truncate the result to the desired mode
5564 and mask just this bit. */
5565 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5566 temp = gen_lowpart (rmode, temp);
5567 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5568 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5571 return temp;
5574 /* Expand fork or exec calls. TARGET is the desired target of the
5575 call. EXP is the call. FN is the
5576 identificator of the actual function. IGNORE is nonzero if the
5577 value is to be ignored. */
5579 static rtx
5580 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5582 tree id, decl;
5583 tree call;
5585 /* If we are not profiling, just call the function. */
5586 if (!profile_arc_flag)
5587 return NULL_RTX;
5589 /* Otherwise call the wrapper. This should be equivalent for the rest of
5590 compiler, so the code does not diverge, and the wrapper may run the
5591 code necessary for keeping the profiling sane. */
5593 switch (DECL_FUNCTION_CODE (fn))
5595 case BUILT_IN_FORK:
5596 id = get_identifier ("__gcov_fork");
5597 break;
5599 case BUILT_IN_EXECL:
5600 id = get_identifier ("__gcov_execl");
5601 break;
5603 case BUILT_IN_EXECV:
5604 id = get_identifier ("__gcov_execv");
5605 break;
5607 case BUILT_IN_EXECLP:
5608 id = get_identifier ("__gcov_execlp");
5609 break;
5611 case BUILT_IN_EXECLE:
5612 id = get_identifier ("__gcov_execle");
5613 break;
5615 case BUILT_IN_EXECVP:
5616 id = get_identifier ("__gcov_execvp");
5617 break;
5619 case BUILT_IN_EXECVE:
5620 id = get_identifier ("__gcov_execve");
5621 break;
5623 default:
5624 gcc_unreachable ();
5627 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5628 FUNCTION_DECL, id, TREE_TYPE (fn));
5629 DECL_EXTERNAL (decl) = 1;
5630 TREE_PUBLIC (decl) = 1;
5631 DECL_ARTIFICIAL (decl) = 1;
5632 TREE_NOTHROW (decl) = 1;
5633 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5634 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5635 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5636 return expand_call (call, target, ignore);
5641 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5642 the pointer in these functions is void*, the tree optimizers may remove
5643 casts. The mode computed in expand_builtin isn't reliable either, due
5644 to __sync_bool_compare_and_swap.
5646 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5647 group of builtins. This gives us log2 of the mode size. */
5649 static inline machine_mode
5650 get_builtin_sync_mode (int fcode_diff)
5652 /* The size is not negotiable, so ask not to get BLKmode in return
5653 if the target indicates that a smaller size would be better. */
5654 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5657 /* Expand the memory expression LOC and return the appropriate memory operand
5658 for the builtin_sync operations. */
5660 static rtx
5661 get_builtin_sync_mem (tree loc, machine_mode mode)
5663 rtx addr, mem;
5665 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5666 addr = convert_memory_address (Pmode, addr);
5668 /* Note that we explicitly do not want any alias information for this
5669 memory, so that we kill all other live memories. Otherwise we don't
5670 satisfy the full barrier semantics of the intrinsic. */
5671 mem = validize_mem (gen_rtx_MEM (mode, addr));
5673 /* The alignment needs to be at least according to that of the mode. */
5674 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5675 get_pointer_alignment (loc)));
5676 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5677 MEM_VOLATILE_P (mem) = 1;
5679 return mem;
5682 /* Make sure an argument is in the right mode.
5683 EXP is the tree argument.
5684 MODE is the mode it should be in. */
5686 static rtx
5687 expand_expr_force_mode (tree exp, machine_mode mode)
5689 rtx val;
5690 machine_mode old_mode;
5692 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5693 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5694 of CONST_INTs, where we know the old_mode only from the call argument. */
5696 old_mode = GET_MODE (val);
5697 if (old_mode == VOIDmode)
5698 old_mode = TYPE_MODE (TREE_TYPE (exp));
5699 val = convert_modes (mode, old_mode, val, 1);
5700 return val;
5704 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5705 EXP is the CALL_EXPR. CODE is the rtx code
5706 that corresponds to the arithmetic or logical operation from the name;
5707 an exception here is that NOT actually means NAND. TARGET is an optional
5708 place for us to store the results; AFTER is true if this is the
5709 fetch_and_xxx form. */
5711 static rtx
5712 expand_builtin_sync_operation (machine_mode mode, tree exp,
5713 enum rtx_code code, bool after,
5714 rtx target)
5716 rtx val, mem;
5717 location_t loc = EXPR_LOCATION (exp);
5719 if (code == NOT && warn_sync_nand)
5721 tree fndecl = get_callee_fndecl (exp);
5722 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5724 static bool warned_f_a_n, warned_n_a_f;
5726 switch (fcode)
5728 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5729 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5730 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5731 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5732 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5733 if (warned_f_a_n)
5734 break;
5736 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5737 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5738 warned_f_a_n = true;
5739 break;
5741 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5742 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5743 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5744 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5745 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5746 if (warned_n_a_f)
5747 break;
5749 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5750 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5751 warned_n_a_f = true;
5752 break;
5754 default:
5755 gcc_unreachable ();
5759 /* Expand the operands. */
5760 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5761 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5763 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5764 after);
5767 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5768 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5769 true if this is the boolean form. TARGET is a place for us to store the
5770 results; this is NOT optional if IS_BOOL is true. */
5772 static rtx
5773 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5774 bool is_bool, rtx target)
5776 rtx old_val, new_val, mem;
5777 rtx *pbool, *poval;
5779 /* Expand the operands. */
5780 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5781 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5782 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5784 pbool = poval = NULL;
5785 if (target != const0_rtx)
5787 if (is_bool)
5788 pbool = &target;
5789 else
5790 poval = &target;
5792 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5793 false, MEMMODEL_SYNC_SEQ_CST,
5794 MEMMODEL_SYNC_SEQ_CST))
5795 return NULL_RTX;
5797 return target;
5800 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5801 general form is actually an atomic exchange, and some targets only
5802 support a reduced form with the second argument being a constant 1.
5803 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5804 the results. */
5806 static rtx
5807 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5808 rtx target)
5810 rtx val, mem;
5812 /* Expand the operands. */
5813 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5814 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5816 return expand_sync_lock_test_and_set (target, mem, val);
5819 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5821 static void
5822 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5824 rtx mem;
5826 /* Expand the operands. */
5827 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5829 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5832 /* Given an integer representing an ``enum memmodel'', verify its
5833 correctness and return the memory model enum. */
5835 static enum memmodel
5836 get_memmodel (tree exp)
5838 rtx op;
5839 unsigned HOST_WIDE_INT val;
5840 source_location loc
5841 = expansion_point_location_if_in_system_header (input_location);
5843 /* If the parameter is not a constant, it's a run time value so we'll just
5844 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5845 if (TREE_CODE (exp) != INTEGER_CST)
5846 return MEMMODEL_SEQ_CST;
5848 op = expand_normal (exp);
5850 val = INTVAL (op);
5851 if (targetm.memmodel_check)
5852 val = targetm.memmodel_check (val);
5853 else if (val & ~MEMMODEL_MASK)
5855 warning_at (loc, OPT_Winvalid_memory_model,
5856 "unknown architecture specifier in memory model to builtin");
5857 return MEMMODEL_SEQ_CST;
5860 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5861 if (memmodel_base (val) >= MEMMODEL_LAST)
5863 warning_at (loc, OPT_Winvalid_memory_model,
5864 "invalid memory model argument to builtin");
5865 return MEMMODEL_SEQ_CST;
5868 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5869 be conservative and promote consume to acquire. */
5870 if (val == MEMMODEL_CONSUME)
5871 val = MEMMODEL_ACQUIRE;
5873 return (enum memmodel) val;
5876 /* Expand the __atomic_exchange intrinsic:
5877 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5878 EXP is the CALL_EXPR.
5879 TARGET is an optional place for us to store the results. */
5881 static rtx
5882 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5884 rtx val, mem;
5885 enum memmodel model;
5887 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5889 if (!flag_inline_atomics)
5890 return NULL_RTX;
5892 /* Expand the operands. */
5893 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5894 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5896 return expand_atomic_exchange (target, mem, val, model);
5899 /* Expand the __atomic_compare_exchange intrinsic:
5900 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5901 TYPE desired, BOOL weak,
5902 enum memmodel success,
5903 enum memmodel failure)
5904 EXP is the CALL_EXPR.
5905 TARGET is an optional place for us to store the results. */
5907 static rtx
5908 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5909 rtx target)
5911 rtx expect, desired, mem, oldval;
5912 rtx_code_label *label;
5913 enum memmodel success, failure;
5914 tree weak;
5915 bool is_weak;
5916 source_location loc
5917 = expansion_point_location_if_in_system_header (input_location);
5919 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5920 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5922 if (failure > success)
5924 warning_at (loc, OPT_Winvalid_memory_model,
5925 "failure memory model cannot be stronger than success "
5926 "memory model for %<__atomic_compare_exchange%>");
5927 success = MEMMODEL_SEQ_CST;
5930 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5932 warning_at (loc, OPT_Winvalid_memory_model,
5933 "invalid failure memory model for "
5934 "%<__atomic_compare_exchange%>");
5935 failure = MEMMODEL_SEQ_CST;
5936 success = MEMMODEL_SEQ_CST;
5940 if (!flag_inline_atomics)
5941 return NULL_RTX;
5943 /* Expand the operands. */
5944 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5946 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5947 expect = convert_memory_address (Pmode, expect);
5948 expect = gen_rtx_MEM (mode, expect);
5949 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5951 weak = CALL_EXPR_ARG (exp, 3);
5952 is_weak = false;
5953 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5954 is_weak = true;
5956 if (target == const0_rtx)
5957 target = NULL;
5959 /* Lest the rtl backend create a race condition with an imporoper store
5960 to memory, always create a new pseudo for OLDVAL. */
5961 oldval = NULL;
5963 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5964 is_weak, success, failure))
5965 return NULL_RTX;
5967 /* Conditionally store back to EXPECT, lest we create a race condition
5968 with an improper store to memory. */
5969 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5970 the normal case where EXPECT is totally private, i.e. a register. At
5971 which point the store can be unconditional. */
5972 label = gen_label_rtx ();
5973 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5974 GET_MODE (target), 1, label);
5975 emit_move_insn (expect, oldval);
5976 emit_label (label);
5978 return target;
5981 /* Helper function for expand_ifn_atomic_compare_exchange - expand
5982 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
5983 call. The weak parameter must be dropped to match the expected parameter
5984 list and the expected argument changed from value to pointer to memory
5985 slot. */
5987 static void
5988 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
5990 unsigned int z;
5991 vec<tree, va_gc> *vec;
5993 vec_alloc (vec, 5);
5994 vec->quick_push (gimple_call_arg (call, 0));
5995 tree expected = gimple_call_arg (call, 1);
5996 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
5997 TREE_TYPE (expected));
5998 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
5999 if (expd != x)
6000 emit_move_insn (x, expd);
6001 tree v = make_tree (TREE_TYPE (expected), x);
6002 vec->quick_push (build1 (ADDR_EXPR,
6003 build_pointer_type (TREE_TYPE (expected)), v));
6004 vec->quick_push (gimple_call_arg (call, 2));
6005 /* Skip the boolean weak parameter. */
6006 for (z = 4; z < 6; z++)
6007 vec->quick_push (gimple_call_arg (call, z));
6008 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6009 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6010 gcc_assert (bytes_log2 < 5);
6011 built_in_function fncode
6012 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6013 + bytes_log2);
6014 tree fndecl = builtin_decl_explicit (fncode);
6015 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6016 fndecl);
6017 tree exp = build_call_vec (boolean_type_node, fn, vec);
6018 tree lhs = gimple_call_lhs (call);
6019 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6020 if (lhs)
6022 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6023 if (GET_MODE (boolret) != mode)
6024 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6025 x = force_reg (mode, x);
6026 write_complex_part (target, boolret, true);
6027 write_complex_part (target, x, false);
6031 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6033 void
6034 expand_ifn_atomic_compare_exchange (gcall *call)
6036 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6037 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6038 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6039 rtx expect, desired, mem, oldval, boolret;
6040 enum memmodel success, failure;
6041 tree lhs;
6042 bool is_weak;
6043 source_location loc
6044 = expansion_point_location_if_in_system_header (gimple_location (call));
6046 success = get_memmodel (gimple_call_arg (call, 4));
6047 failure = get_memmodel (gimple_call_arg (call, 5));
6049 if (failure > success)
6051 warning_at (loc, OPT_Winvalid_memory_model,
6052 "failure memory model cannot be stronger than success "
6053 "memory model for %<__atomic_compare_exchange%>");
6054 success = MEMMODEL_SEQ_CST;
6057 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6059 warning_at (loc, OPT_Winvalid_memory_model,
6060 "invalid failure memory model for "
6061 "%<__atomic_compare_exchange%>");
6062 failure = MEMMODEL_SEQ_CST;
6063 success = MEMMODEL_SEQ_CST;
6066 if (!flag_inline_atomics)
6068 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6069 return;
6072 /* Expand the operands. */
6073 mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6075 expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6076 desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6078 is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6080 boolret = NULL;
6081 oldval = NULL;
6083 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6084 is_weak, success, failure))
6086 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6087 return;
6090 lhs = gimple_call_lhs (call);
6091 if (lhs)
6093 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6094 if (GET_MODE (boolret) != mode)
6095 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6096 write_complex_part (target, boolret, true);
6097 write_complex_part (target, oldval, false);
6101 /* Expand the __atomic_load intrinsic:
6102 TYPE __atomic_load (TYPE *object, enum memmodel)
6103 EXP is the CALL_EXPR.
6104 TARGET is an optional place for us to store the results. */
6106 static rtx
6107 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6109 rtx mem;
6110 enum memmodel model;
6112 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6113 if (is_mm_release (model) || is_mm_acq_rel (model))
6115 source_location loc
6116 = expansion_point_location_if_in_system_header (input_location);
6117 warning_at (loc, OPT_Winvalid_memory_model,
6118 "invalid memory model for %<__atomic_load%>");
6119 model = MEMMODEL_SEQ_CST;
6122 if (!flag_inline_atomics)
6123 return NULL_RTX;
6125 /* Expand the operand. */
6126 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6128 return expand_atomic_load (target, mem, model);
6132 /* Expand the __atomic_store intrinsic:
6133 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6134 EXP is the CALL_EXPR.
6135 TARGET is an optional place for us to store the results. */
6137 static rtx
6138 expand_builtin_atomic_store (machine_mode mode, tree exp)
6140 rtx mem, val;
6141 enum memmodel model;
6143 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6144 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6145 || is_mm_release (model)))
6147 source_location loc
6148 = expansion_point_location_if_in_system_header (input_location);
6149 warning_at (loc, OPT_Winvalid_memory_model,
6150 "invalid memory model for %<__atomic_store%>");
6151 model = MEMMODEL_SEQ_CST;
6154 if (!flag_inline_atomics)
6155 return NULL_RTX;
6157 /* Expand the operands. */
6158 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6159 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6161 return expand_atomic_store (mem, val, model, false);
6164 /* Expand the __atomic_fetch_XXX intrinsic:
6165 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6166 EXP is the CALL_EXPR.
6167 TARGET is an optional place for us to store the results.
6168 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6169 FETCH_AFTER is true if returning the result of the operation.
6170 FETCH_AFTER is false if returning the value before the operation.
6171 IGNORE is true if the result is not used.
6172 EXT_CALL is the correct builtin for an external call if this cannot be
6173 resolved to an instruction sequence. */
6175 static rtx
6176 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6177 enum rtx_code code, bool fetch_after,
6178 bool ignore, enum built_in_function ext_call)
6180 rtx val, mem, ret;
6181 enum memmodel model;
6182 tree fndecl;
6183 tree addr;
6185 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6187 /* Expand the operands. */
6188 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6189 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6191 /* Only try generating instructions if inlining is turned on. */
6192 if (flag_inline_atomics)
6194 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6195 if (ret)
6196 return ret;
6199 /* Return if a different routine isn't needed for the library call. */
6200 if (ext_call == BUILT_IN_NONE)
6201 return NULL_RTX;
6203 /* Change the call to the specified function. */
6204 fndecl = get_callee_fndecl (exp);
6205 addr = CALL_EXPR_FN (exp);
6206 STRIP_NOPS (addr);
6208 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6209 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6211 /* If we will emit code after the call, the call can not be a tail call.
6212 If it is emitted as a tail call, a barrier is emitted after it, and
6213 then all trailing code is removed. */
6214 if (!ignore)
6215 CALL_EXPR_TAILCALL (exp) = 0;
6217 /* Expand the call here so we can emit trailing code. */
6218 ret = expand_call (exp, target, ignore);
6220 /* Replace the original function just in case it matters. */
6221 TREE_OPERAND (addr, 0) = fndecl;
6223 /* Then issue the arithmetic correction to return the right result. */
6224 if (!ignore)
6226 if (code == NOT)
6228 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6229 OPTAB_LIB_WIDEN);
6230 ret = expand_simple_unop (mode, NOT, ret, target, true);
6232 else
6233 ret = expand_simple_binop (mode, code, ret, val, target, true,
6234 OPTAB_LIB_WIDEN);
6236 return ret;
6239 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6241 void
6242 expand_ifn_atomic_bit_test_and (gcall *call)
6244 tree ptr = gimple_call_arg (call, 0);
6245 tree bit = gimple_call_arg (call, 1);
6246 tree flag = gimple_call_arg (call, 2);
6247 tree lhs = gimple_call_lhs (call);
6248 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6249 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6250 enum rtx_code code;
6251 optab optab;
6252 struct expand_operand ops[5];
6254 gcc_assert (flag_inline_atomics);
6256 if (gimple_call_num_args (call) == 4)
6257 model = get_memmodel (gimple_call_arg (call, 3));
6259 rtx mem = get_builtin_sync_mem (ptr, mode);
6260 rtx val = expand_expr_force_mode (bit, mode);
6262 switch (gimple_call_internal_fn (call))
6264 case IFN_ATOMIC_BIT_TEST_AND_SET:
6265 code = IOR;
6266 optab = atomic_bit_test_and_set_optab;
6267 break;
6268 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6269 code = XOR;
6270 optab = atomic_bit_test_and_complement_optab;
6271 break;
6272 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6273 code = AND;
6274 optab = atomic_bit_test_and_reset_optab;
6275 break;
6276 default:
6277 gcc_unreachable ();
6280 if (lhs == NULL_TREE)
6282 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6283 val, NULL_RTX, true, OPTAB_DIRECT);
6284 if (code == AND)
6285 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6286 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6287 return;
6290 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6291 enum insn_code icode = direct_optab_handler (optab, mode);
6292 gcc_assert (icode != CODE_FOR_nothing);
6293 create_output_operand (&ops[0], target, mode);
6294 create_fixed_operand (&ops[1], mem);
6295 create_convert_operand_to (&ops[2], val, mode, true);
6296 create_integer_operand (&ops[3], model);
6297 create_integer_operand (&ops[4], integer_onep (flag));
6298 if (maybe_expand_insn (icode, 5, ops))
6299 return;
6301 rtx bitval = val;
6302 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6303 val, NULL_RTX, true, OPTAB_DIRECT);
6304 rtx maskval = val;
6305 if (code == AND)
6306 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6307 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6308 code, model, false);
6309 if (integer_onep (flag))
6311 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6312 NULL_RTX, true, OPTAB_DIRECT);
6313 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6314 true, OPTAB_DIRECT);
6316 else
6317 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6318 OPTAB_DIRECT);
6319 if (result != target)
6320 emit_move_insn (target, result);
6323 /* Expand an atomic clear operation.
6324 void _atomic_clear (BOOL *obj, enum memmodel)
6325 EXP is the call expression. */
6327 static rtx
6328 expand_builtin_atomic_clear (tree exp)
6330 machine_mode mode;
6331 rtx mem, ret;
6332 enum memmodel model;
6334 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6335 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6336 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6338 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6340 source_location loc
6341 = expansion_point_location_if_in_system_header (input_location);
6342 warning_at (loc, OPT_Winvalid_memory_model,
6343 "invalid memory model for %<__atomic_store%>");
6344 model = MEMMODEL_SEQ_CST;
6347 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6348 Failing that, a store is issued by __atomic_store. The only way this can
6349 fail is if the bool type is larger than a word size. Unlikely, but
6350 handle it anyway for completeness. Assume a single threaded model since
6351 there is no atomic support in this case, and no barriers are required. */
6352 ret = expand_atomic_store (mem, const0_rtx, model, true);
6353 if (!ret)
6354 emit_move_insn (mem, const0_rtx);
6355 return const0_rtx;
6358 /* Expand an atomic test_and_set operation.
6359 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6360 EXP is the call expression. */
6362 static rtx
6363 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6365 rtx mem;
6366 enum memmodel model;
6367 machine_mode mode;
6369 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6370 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6371 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6373 return expand_atomic_test_and_set (target, mem, model);
6377 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6378 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6380 static tree
6381 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6383 int size;
6384 machine_mode mode;
6385 unsigned int mode_align, type_align;
6387 if (TREE_CODE (arg0) != INTEGER_CST)
6388 return NULL_TREE;
6390 /* We need a corresponding integer mode for the access to be lock-free. */
6391 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6392 if (!int_mode_for_size (size, 0).exists (&mode))
6393 return boolean_false_node;
6395 mode_align = GET_MODE_ALIGNMENT (mode);
6397 if (TREE_CODE (arg1) == INTEGER_CST)
6399 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6401 /* Either this argument is null, or it's a fake pointer encoding
6402 the alignment of the object. */
6403 val = least_bit_hwi (val);
6404 val *= BITS_PER_UNIT;
6406 if (val == 0 || mode_align < val)
6407 type_align = mode_align;
6408 else
6409 type_align = val;
6411 else
6413 tree ttype = TREE_TYPE (arg1);
6415 /* This function is usually invoked and folded immediately by the front
6416 end before anything else has a chance to look at it. The pointer
6417 parameter at this point is usually cast to a void *, so check for that
6418 and look past the cast. */
6419 if (CONVERT_EXPR_P (arg1)
6420 && POINTER_TYPE_P (ttype)
6421 && VOID_TYPE_P (TREE_TYPE (ttype))
6422 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6423 arg1 = TREE_OPERAND (arg1, 0);
6425 ttype = TREE_TYPE (arg1);
6426 gcc_assert (POINTER_TYPE_P (ttype));
6428 /* Get the underlying type of the object. */
6429 ttype = TREE_TYPE (ttype);
6430 type_align = TYPE_ALIGN (ttype);
6433 /* If the object has smaller alignment, the lock free routines cannot
6434 be used. */
6435 if (type_align < mode_align)
6436 return boolean_false_node;
6438 /* Check if a compare_and_swap pattern exists for the mode which represents
6439 the required size. The pattern is not allowed to fail, so the existence
6440 of the pattern indicates support is present. Also require that an
6441 atomic load exists for the required size. */
6442 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6443 return boolean_true_node;
6444 else
6445 return boolean_false_node;
6448 /* Return true if the parameters to call EXP represent an object which will
6449 always generate lock free instructions. The first argument represents the
6450 size of the object, and the second parameter is a pointer to the object
6451 itself. If NULL is passed for the object, then the result is based on
6452 typical alignment for an object of the specified size. Otherwise return
6453 false. */
6455 static rtx
6456 expand_builtin_atomic_always_lock_free (tree exp)
6458 tree size;
6459 tree arg0 = CALL_EXPR_ARG (exp, 0);
6460 tree arg1 = CALL_EXPR_ARG (exp, 1);
6462 if (TREE_CODE (arg0) != INTEGER_CST)
6464 error ("non-constant argument 1 to __atomic_always_lock_free");
6465 return const0_rtx;
6468 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6469 if (size == boolean_true_node)
6470 return const1_rtx;
6471 return const0_rtx;
6474 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6475 is lock free on this architecture. */
6477 static tree
6478 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6480 if (!flag_inline_atomics)
6481 return NULL_TREE;
6483 /* If it isn't always lock free, don't generate a result. */
6484 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6485 return boolean_true_node;
6487 return NULL_TREE;
6490 /* Return true if the parameters to call EXP represent an object which will
6491 always generate lock free instructions. The first argument represents the
6492 size of the object, and the second parameter is a pointer to the object
6493 itself. If NULL is passed for the object, then the result is based on
6494 typical alignment for an object of the specified size. Otherwise return
6495 NULL*/
6497 static rtx
6498 expand_builtin_atomic_is_lock_free (tree exp)
6500 tree size;
6501 tree arg0 = CALL_EXPR_ARG (exp, 0);
6502 tree arg1 = CALL_EXPR_ARG (exp, 1);
6504 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6506 error ("non-integer argument 1 to __atomic_is_lock_free");
6507 return NULL_RTX;
6510 if (!flag_inline_atomics)
6511 return NULL_RTX;
6513 /* If the value is known at compile time, return the RTX for it. */
6514 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6515 if (size == boolean_true_node)
6516 return const1_rtx;
6518 return NULL_RTX;
6521 /* Expand the __atomic_thread_fence intrinsic:
6522 void __atomic_thread_fence (enum memmodel)
6523 EXP is the CALL_EXPR. */
6525 static void
6526 expand_builtin_atomic_thread_fence (tree exp)
6528 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6529 expand_mem_thread_fence (model);
6532 /* Expand the __atomic_signal_fence intrinsic:
6533 void __atomic_signal_fence (enum memmodel)
6534 EXP is the CALL_EXPR. */
6536 static void
6537 expand_builtin_atomic_signal_fence (tree exp)
6539 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6540 expand_mem_signal_fence (model);
6543 /* Expand the __sync_synchronize intrinsic. */
6545 static void
6546 expand_builtin_sync_synchronize (void)
6548 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6551 static rtx
6552 expand_builtin_thread_pointer (tree exp, rtx target)
6554 enum insn_code icode;
6555 if (!validate_arglist (exp, VOID_TYPE))
6556 return const0_rtx;
6557 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6558 if (icode != CODE_FOR_nothing)
6560 struct expand_operand op;
6561 /* If the target is not sutitable then create a new target. */
6562 if (target == NULL_RTX
6563 || !REG_P (target)
6564 || GET_MODE (target) != Pmode)
6565 target = gen_reg_rtx (Pmode);
6566 create_output_operand (&op, target, Pmode);
6567 expand_insn (icode, 1, &op);
6568 return target;
6570 error ("__builtin_thread_pointer is not supported on this target");
6571 return const0_rtx;
6574 static void
6575 expand_builtin_set_thread_pointer (tree exp)
6577 enum insn_code icode;
6578 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6579 return;
6580 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6581 if (icode != CODE_FOR_nothing)
6583 struct expand_operand op;
6584 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6585 Pmode, EXPAND_NORMAL);
6586 create_input_operand (&op, val, Pmode);
6587 expand_insn (icode, 1, &op);
6588 return;
6590 error ("__builtin_set_thread_pointer is not supported on this target");
6594 /* Emit code to restore the current value of stack. */
6596 static void
6597 expand_stack_restore (tree var)
6599 rtx_insn *prev;
6600 rtx sa = expand_normal (var);
6602 sa = convert_memory_address (Pmode, sa);
6604 prev = get_last_insn ();
6605 emit_stack_restore (SAVE_BLOCK, sa);
6607 record_new_stack_level ();
6609 fixup_args_size_notes (prev, get_last_insn (), 0);
6612 /* Emit code to save the current value of stack. */
6614 static rtx
6615 expand_stack_save (void)
6617 rtx ret = NULL_RTX;
6619 emit_stack_save (SAVE_BLOCK, &ret);
6620 return ret;
6624 /* Expand an expression EXP that calls a built-in function,
6625 with result going to TARGET if that's convenient
6626 (and in mode MODE if that's convenient).
6627 SUBTARGET may be used as the target for computing one of EXP's operands.
6628 IGNORE is nonzero if the value is to be ignored. */
6631 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6632 int ignore)
6634 tree fndecl = get_callee_fndecl (exp);
6635 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6636 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6637 int flags;
6639 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6640 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6642 /* When ASan is enabled, we don't want to expand some memory/string
6643 builtins and rely on libsanitizer's hooks. This allows us to avoid
6644 redundant checks and be sure, that possible overflow will be detected
6645 by ASan. */
6647 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6648 return expand_call (exp, target, ignore);
6650 /* When not optimizing, generate calls to library functions for a certain
6651 set of builtins. */
6652 if (!optimize
6653 && !called_as_built_in (fndecl)
6654 && fcode != BUILT_IN_FORK
6655 && fcode != BUILT_IN_EXECL
6656 && fcode != BUILT_IN_EXECV
6657 && fcode != BUILT_IN_EXECLP
6658 && fcode != BUILT_IN_EXECLE
6659 && fcode != BUILT_IN_EXECVP
6660 && fcode != BUILT_IN_EXECVE
6661 && !ALLOCA_FUNCTION_CODE_P (fcode)
6662 && fcode != BUILT_IN_FREE
6663 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
6664 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
6665 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
6666 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
6667 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6668 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
6669 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
6670 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
6671 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
6672 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
6673 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
6674 && fcode != BUILT_IN_CHKP_BNDRET)
6675 return expand_call (exp, target, ignore);
6677 /* The built-in function expanders test for target == const0_rtx
6678 to determine whether the function's result will be ignored. */
6679 if (ignore)
6680 target = const0_rtx;
6682 /* If the result of a pure or const built-in function is ignored, and
6683 none of its arguments are volatile, we can avoid expanding the
6684 built-in call and just evaluate the arguments for side-effects. */
6685 if (target == const0_rtx
6686 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6687 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6689 bool volatilep = false;
6690 tree arg;
6691 call_expr_arg_iterator iter;
6693 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6694 if (TREE_THIS_VOLATILE (arg))
6696 volatilep = true;
6697 break;
6700 if (! volatilep)
6702 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6703 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6704 return const0_rtx;
6708 /* expand_builtin_with_bounds is supposed to be used for
6709 instrumented builtin calls. */
6710 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6712 switch (fcode)
6714 CASE_FLT_FN (BUILT_IN_FABS):
6715 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
6716 case BUILT_IN_FABSD32:
6717 case BUILT_IN_FABSD64:
6718 case BUILT_IN_FABSD128:
6719 target = expand_builtin_fabs (exp, target, subtarget);
6720 if (target)
6721 return target;
6722 break;
6724 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6725 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
6726 target = expand_builtin_copysign (exp, target, subtarget);
6727 if (target)
6728 return target;
6729 break;
6731 /* Just do a normal library call if we were unable to fold
6732 the values. */
6733 CASE_FLT_FN (BUILT_IN_CABS):
6734 break;
6736 CASE_FLT_FN (BUILT_IN_FMA):
6737 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
6738 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6739 if (target)
6740 return target;
6741 break;
6743 CASE_FLT_FN (BUILT_IN_ILOGB):
6744 if (! flag_unsafe_math_optimizations)
6745 break;
6746 gcc_fallthrough ();
6747 CASE_FLT_FN (BUILT_IN_ISINF):
6748 CASE_FLT_FN (BUILT_IN_FINITE):
6749 case BUILT_IN_ISFINITE:
6750 case BUILT_IN_ISNORMAL:
6751 target = expand_builtin_interclass_mathfn (exp, target);
6752 if (target)
6753 return target;
6754 break;
6756 CASE_FLT_FN (BUILT_IN_ICEIL):
6757 CASE_FLT_FN (BUILT_IN_LCEIL):
6758 CASE_FLT_FN (BUILT_IN_LLCEIL):
6759 CASE_FLT_FN (BUILT_IN_LFLOOR):
6760 CASE_FLT_FN (BUILT_IN_IFLOOR):
6761 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6762 target = expand_builtin_int_roundingfn (exp, target);
6763 if (target)
6764 return target;
6765 break;
6767 CASE_FLT_FN (BUILT_IN_IRINT):
6768 CASE_FLT_FN (BUILT_IN_LRINT):
6769 CASE_FLT_FN (BUILT_IN_LLRINT):
6770 CASE_FLT_FN (BUILT_IN_IROUND):
6771 CASE_FLT_FN (BUILT_IN_LROUND):
6772 CASE_FLT_FN (BUILT_IN_LLROUND):
6773 target = expand_builtin_int_roundingfn_2 (exp, target);
6774 if (target)
6775 return target;
6776 break;
6778 CASE_FLT_FN (BUILT_IN_POWI):
6779 target = expand_builtin_powi (exp, target);
6780 if (target)
6781 return target;
6782 break;
6784 CASE_FLT_FN (BUILT_IN_CEXPI):
6785 target = expand_builtin_cexpi (exp, target);
6786 gcc_assert (target);
6787 return target;
6789 CASE_FLT_FN (BUILT_IN_SIN):
6790 CASE_FLT_FN (BUILT_IN_COS):
6791 if (! flag_unsafe_math_optimizations)
6792 break;
6793 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6794 if (target)
6795 return target;
6796 break;
6798 CASE_FLT_FN (BUILT_IN_SINCOS):
6799 if (! flag_unsafe_math_optimizations)
6800 break;
6801 target = expand_builtin_sincos (exp);
6802 if (target)
6803 return target;
6804 break;
6806 case BUILT_IN_APPLY_ARGS:
6807 return expand_builtin_apply_args ();
6809 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6810 FUNCTION with a copy of the parameters described by
6811 ARGUMENTS, and ARGSIZE. It returns a block of memory
6812 allocated on the stack into which is stored all the registers
6813 that might possibly be used for returning the result of a
6814 function. ARGUMENTS is the value returned by
6815 __builtin_apply_args. ARGSIZE is the number of bytes of
6816 arguments that must be copied. ??? How should this value be
6817 computed? We'll also need a safe worst case value for varargs
6818 functions. */
6819 case BUILT_IN_APPLY:
6820 if (!validate_arglist (exp, POINTER_TYPE,
6821 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6822 && !validate_arglist (exp, REFERENCE_TYPE,
6823 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6824 return const0_rtx;
6825 else
6827 rtx ops[3];
6829 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6830 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6831 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6833 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6836 /* __builtin_return (RESULT) causes the function to return the
6837 value described by RESULT. RESULT is address of the block of
6838 memory returned by __builtin_apply. */
6839 case BUILT_IN_RETURN:
6840 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6841 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6842 return const0_rtx;
6844 case BUILT_IN_SAVEREGS:
6845 return expand_builtin_saveregs ();
6847 case BUILT_IN_VA_ARG_PACK:
6848 /* All valid uses of __builtin_va_arg_pack () are removed during
6849 inlining. */
6850 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6851 return const0_rtx;
6853 case BUILT_IN_VA_ARG_PACK_LEN:
6854 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6855 inlining. */
6856 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6857 return const0_rtx;
6859 /* Return the address of the first anonymous stack arg. */
6860 case BUILT_IN_NEXT_ARG:
6861 if (fold_builtin_next_arg (exp, false))
6862 return const0_rtx;
6863 return expand_builtin_next_arg ();
6865 case BUILT_IN_CLEAR_CACHE:
6866 target = expand_builtin___clear_cache (exp);
6867 if (target)
6868 return target;
6869 break;
6871 case BUILT_IN_CLASSIFY_TYPE:
6872 return expand_builtin_classify_type (exp);
6874 case BUILT_IN_CONSTANT_P:
6875 return const0_rtx;
6877 case BUILT_IN_FRAME_ADDRESS:
6878 case BUILT_IN_RETURN_ADDRESS:
6879 return expand_builtin_frame_address (fndecl, exp);
6881 /* Returns the address of the area where the structure is returned.
6882 0 otherwise. */
6883 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6884 if (call_expr_nargs (exp) != 0
6885 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6886 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6887 return const0_rtx;
6888 else
6889 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6891 CASE_BUILT_IN_ALLOCA:
6892 target = expand_builtin_alloca (exp);
6893 if (target)
6894 return target;
6895 break;
6897 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
6898 return expand_asan_emit_allocas_unpoison (exp);
6900 case BUILT_IN_STACK_SAVE:
6901 return expand_stack_save ();
6903 case BUILT_IN_STACK_RESTORE:
6904 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6905 return const0_rtx;
6907 case BUILT_IN_BSWAP16:
6908 case BUILT_IN_BSWAP32:
6909 case BUILT_IN_BSWAP64:
6910 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6911 if (target)
6912 return target;
6913 break;
6915 CASE_INT_FN (BUILT_IN_FFS):
6916 target = expand_builtin_unop (target_mode, exp, target,
6917 subtarget, ffs_optab);
6918 if (target)
6919 return target;
6920 break;
6922 CASE_INT_FN (BUILT_IN_CLZ):
6923 target = expand_builtin_unop (target_mode, exp, target,
6924 subtarget, clz_optab);
6925 if (target)
6926 return target;
6927 break;
6929 CASE_INT_FN (BUILT_IN_CTZ):
6930 target = expand_builtin_unop (target_mode, exp, target,
6931 subtarget, ctz_optab);
6932 if (target)
6933 return target;
6934 break;
6936 CASE_INT_FN (BUILT_IN_CLRSB):
6937 target = expand_builtin_unop (target_mode, exp, target,
6938 subtarget, clrsb_optab);
6939 if (target)
6940 return target;
6941 break;
6943 CASE_INT_FN (BUILT_IN_POPCOUNT):
6944 target = expand_builtin_unop (target_mode, exp, target,
6945 subtarget, popcount_optab);
6946 if (target)
6947 return target;
6948 break;
6950 CASE_INT_FN (BUILT_IN_PARITY):
6951 target = expand_builtin_unop (target_mode, exp, target,
6952 subtarget, parity_optab);
6953 if (target)
6954 return target;
6955 break;
6957 case BUILT_IN_STRLEN:
6958 target = expand_builtin_strlen (exp, target, target_mode);
6959 if (target)
6960 return target;
6961 break;
6963 case BUILT_IN_STRCAT:
6964 target = expand_builtin_strcat (exp, target);
6965 if (target)
6966 return target;
6967 break;
6969 case BUILT_IN_STRCPY:
6970 target = expand_builtin_strcpy (exp, target);
6971 if (target)
6972 return target;
6973 break;
6975 case BUILT_IN_STRNCAT:
6976 target = expand_builtin_strncat (exp, target);
6977 if (target)
6978 return target;
6979 break;
6981 case BUILT_IN_STRNCPY:
6982 target = expand_builtin_strncpy (exp, target);
6983 if (target)
6984 return target;
6985 break;
6987 case BUILT_IN_STPCPY:
6988 target = expand_builtin_stpcpy (exp, target, mode);
6989 if (target)
6990 return target;
6991 break;
6993 case BUILT_IN_STPNCPY:
6994 target = expand_builtin_stpncpy (exp, target);
6995 if (target)
6996 return target;
6997 break;
6999 case BUILT_IN_MEMCHR:
7000 target = expand_builtin_memchr (exp, target);
7001 if (target)
7002 return target;
7003 break;
7005 case BUILT_IN_MEMCPY:
7006 target = expand_builtin_memcpy (exp, target);
7007 if (target)
7008 return target;
7009 break;
7011 case BUILT_IN_MEMMOVE:
7012 target = expand_builtin_memmove (exp, target);
7013 if (target)
7014 return target;
7015 break;
7017 case BUILT_IN_MEMPCPY:
7018 target = expand_builtin_mempcpy (exp, target);
7019 if (target)
7020 return target;
7021 break;
7023 case BUILT_IN_MEMSET:
7024 target = expand_builtin_memset (exp, target, mode);
7025 if (target)
7026 return target;
7027 break;
7029 case BUILT_IN_BZERO:
7030 target = expand_builtin_bzero (exp);
7031 if (target)
7032 return target;
7033 break;
7035 case BUILT_IN_STRCMP:
7036 target = expand_builtin_strcmp (exp, target);
7037 if (target)
7038 return target;
7039 break;
7041 case BUILT_IN_STRNCMP:
7042 target = expand_builtin_strncmp (exp, target, mode);
7043 if (target)
7044 return target;
7045 break;
7047 case BUILT_IN_BCMP:
7048 case BUILT_IN_MEMCMP:
7049 case BUILT_IN_MEMCMP_EQ:
7050 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7051 if (target)
7052 return target;
7053 if (fcode == BUILT_IN_MEMCMP_EQ)
7055 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7056 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7058 break;
7060 case BUILT_IN_SETJMP:
7061 /* This should have been lowered to the builtins below. */
7062 gcc_unreachable ();
7064 case BUILT_IN_SETJMP_SETUP:
7065 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7066 and the receiver label. */
7067 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7069 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7070 VOIDmode, EXPAND_NORMAL);
7071 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7072 rtx_insn *label_r = label_rtx (label);
7074 /* This is copied from the handling of non-local gotos. */
7075 expand_builtin_setjmp_setup (buf_addr, label_r);
7076 nonlocal_goto_handler_labels
7077 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7078 nonlocal_goto_handler_labels);
7079 /* ??? Do not let expand_label treat us as such since we would
7080 not want to be both on the list of non-local labels and on
7081 the list of forced labels. */
7082 FORCED_LABEL (label) = 0;
7083 return const0_rtx;
7085 break;
7087 case BUILT_IN_SETJMP_RECEIVER:
7088 /* __builtin_setjmp_receiver is passed the receiver label. */
7089 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7091 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7092 rtx_insn *label_r = label_rtx (label);
7094 expand_builtin_setjmp_receiver (label_r);
7095 return const0_rtx;
7097 break;
7099 /* __builtin_longjmp is passed a pointer to an array of five words.
7100 It's similar to the C library longjmp function but works with
7101 __builtin_setjmp above. */
7102 case BUILT_IN_LONGJMP:
7103 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7105 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7106 VOIDmode, EXPAND_NORMAL);
7107 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7109 if (value != const1_rtx)
7111 error ("%<__builtin_longjmp%> second argument must be 1");
7112 return const0_rtx;
7115 expand_builtin_longjmp (buf_addr, value);
7116 return const0_rtx;
7118 break;
7120 case BUILT_IN_NONLOCAL_GOTO:
7121 target = expand_builtin_nonlocal_goto (exp);
7122 if (target)
7123 return target;
7124 break;
7126 /* This updates the setjmp buffer that is its argument with the value
7127 of the current stack pointer. */
7128 case BUILT_IN_UPDATE_SETJMP_BUF:
7129 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7131 rtx buf_addr
7132 = expand_normal (CALL_EXPR_ARG (exp, 0));
7134 expand_builtin_update_setjmp_buf (buf_addr);
7135 return const0_rtx;
7137 break;
7139 case BUILT_IN_TRAP:
7140 expand_builtin_trap ();
7141 return const0_rtx;
7143 case BUILT_IN_UNREACHABLE:
7144 expand_builtin_unreachable ();
7145 return const0_rtx;
7147 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7148 case BUILT_IN_SIGNBITD32:
7149 case BUILT_IN_SIGNBITD64:
7150 case BUILT_IN_SIGNBITD128:
7151 target = expand_builtin_signbit (exp, target);
7152 if (target)
7153 return target;
7154 break;
7156 /* Various hooks for the DWARF 2 __throw routine. */
7157 case BUILT_IN_UNWIND_INIT:
7158 expand_builtin_unwind_init ();
7159 return const0_rtx;
7160 case BUILT_IN_DWARF_CFA:
7161 return virtual_cfa_rtx;
7162 #ifdef DWARF2_UNWIND_INFO
7163 case BUILT_IN_DWARF_SP_COLUMN:
7164 return expand_builtin_dwarf_sp_column ();
7165 case BUILT_IN_INIT_DWARF_REG_SIZES:
7166 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7167 return const0_rtx;
7168 #endif
7169 case BUILT_IN_FROB_RETURN_ADDR:
7170 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7171 case BUILT_IN_EXTRACT_RETURN_ADDR:
7172 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7173 case BUILT_IN_EH_RETURN:
7174 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7175 CALL_EXPR_ARG (exp, 1));
7176 return const0_rtx;
7177 case BUILT_IN_EH_RETURN_DATA_REGNO:
7178 return expand_builtin_eh_return_data_regno (exp);
7179 case BUILT_IN_EXTEND_POINTER:
7180 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7181 case BUILT_IN_EH_POINTER:
7182 return expand_builtin_eh_pointer (exp);
7183 case BUILT_IN_EH_FILTER:
7184 return expand_builtin_eh_filter (exp);
7185 case BUILT_IN_EH_COPY_VALUES:
7186 return expand_builtin_eh_copy_values (exp);
7188 case BUILT_IN_VA_START:
7189 return expand_builtin_va_start (exp);
7190 case BUILT_IN_VA_END:
7191 return expand_builtin_va_end (exp);
7192 case BUILT_IN_VA_COPY:
7193 return expand_builtin_va_copy (exp);
7194 case BUILT_IN_EXPECT:
7195 return expand_builtin_expect (exp, target);
7196 case BUILT_IN_ASSUME_ALIGNED:
7197 return expand_builtin_assume_aligned (exp, target);
7198 case BUILT_IN_PREFETCH:
7199 expand_builtin_prefetch (exp);
7200 return const0_rtx;
7202 case BUILT_IN_INIT_TRAMPOLINE:
7203 return expand_builtin_init_trampoline (exp, true);
7204 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7205 return expand_builtin_init_trampoline (exp, false);
7206 case BUILT_IN_ADJUST_TRAMPOLINE:
7207 return expand_builtin_adjust_trampoline (exp);
7209 case BUILT_IN_INIT_DESCRIPTOR:
7210 return expand_builtin_init_descriptor (exp);
7211 case BUILT_IN_ADJUST_DESCRIPTOR:
7212 return expand_builtin_adjust_descriptor (exp);
7214 case BUILT_IN_FORK:
7215 case BUILT_IN_EXECL:
7216 case BUILT_IN_EXECV:
7217 case BUILT_IN_EXECLP:
7218 case BUILT_IN_EXECLE:
7219 case BUILT_IN_EXECVP:
7220 case BUILT_IN_EXECVE:
7221 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7222 if (target)
7223 return target;
7224 break;
7226 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7227 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7228 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7229 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7230 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7231 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7232 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7233 if (target)
7234 return target;
7235 break;
7237 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7238 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7239 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7240 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7241 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7242 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7243 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7244 if (target)
7245 return target;
7246 break;
7248 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7249 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7250 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7251 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7252 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7253 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7254 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7255 if (target)
7256 return target;
7257 break;
7259 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7260 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7261 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7262 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7263 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7264 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7265 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7266 if (target)
7267 return target;
7268 break;
7270 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7271 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7272 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7273 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7274 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7275 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7276 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7277 if (target)
7278 return target;
7279 break;
7281 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7282 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7283 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7284 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7285 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7286 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7287 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7288 if (target)
7289 return target;
7290 break;
7292 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7293 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7294 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7295 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7296 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7297 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7298 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7299 if (target)
7300 return target;
7301 break;
7303 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7304 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7305 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7306 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7307 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7308 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7309 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7310 if (target)
7311 return target;
7312 break;
7314 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7315 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7316 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7317 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7318 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7319 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7320 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7321 if (target)
7322 return target;
7323 break;
7325 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7326 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7327 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7328 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7329 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7330 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7331 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7332 if (target)
7333 return target;
7334 break;
7336 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7337 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7338 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7339 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7340 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7341 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7342 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7343 if (target)
7344 return target;
7345 break;
7347 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7348 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7349 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7350 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7351 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7352 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7353 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7354 if (target)
7355 return target;
7356 break;
7358 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7359 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7360 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7361 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7362 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7363 if (mode == VOIDmode)
7364 mode = TYPE_MODE (boolean_type_node);
7365 if (!target || !register_operand (target, mode))
7366 target = gen_reg_rtx (mode);
7368 mode = get_builtin_sync_mode
7369 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7370 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7371 if (target)
7372 return target;
7373 break;
7375 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7376 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7377 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7378 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7379 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7380 mode = get_builtin_sync_mode
7381 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7382 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7383 if (target)
7384 return target;
7385 break;
7387 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7388 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7389 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7390 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7391 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7392 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7393 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7394 if (target)
7395 return target;
7396 break;
7398 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7399 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7400 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7401 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7402 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7403 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7404 expand_builtin_sync_lock_release (mode, exp);
7405 return const0_rtx;
7407 case BUILT_IN_SYNC_SYNCHRONIZE:
7408 expand_builtin_sync_synchronize ();
7409 return const0_rtx;
7411 case BUILT_IN_ATOMIC_EXCHANGE_1:
7412 case BUILT_IN_ATOMIC_EXCHANGE_2:
7413 case BUILT_IN_ATOMIC_EXCHANGE_4:
7414 case BUILT_IN_ATOMIC_EXCHANGE_8:
7415 case BUILT_IN_ATOMIC_EXCHANGE_16:
7416 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7417 target = expand_builtin_atomic_exchange (mode, exp, target);
7418 if (target)
7419 return target;
7420 break;
7422 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7423 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7424 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7425 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7426 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7428 unsigned int nargs, z;
7429 vec<tree, va_gc> *vec;
7431 mode =
7432 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7433 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7434 if (target)
7435 return target;
7437 /* If this is turned into an external library call, the weak parameter
7438 must be dropped to match the expected parameter list. */
7439 nargs = call_expr_nargs (exp);
7440 vec_alloc (vec, nargs - 1);
7441 for (z = 0; z < 3; z++)
7442 vec->quick_push (CALL_EXPR_ARG (exp, z));
7443 /* Skip the boolean weak parameter. */
7444 for (z = 4; z < 6; z++)
7445 vec->quick_push (CALL_EXPR_ARG (exp, z));
7446 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7447 break;
7450 case BUILT_IN_ATOMIC_LOAD_1:
7451 case BUILT_IN_ATOMIC_LOAD_2:
7452 case BUILT_IN_ATOMIC_LOAD_4:
7453 case BUILT_IN_ATOMIC_LOAD_8:
7454 case BUILT_IN_ATOMIC_LOAD_16:
7455 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7456 target = expand_builtin_atomic_load (mode, exp, target);
7457 if (target)
7458 return target;
7459 break;
7461 case BUILT_IN_ATOMIC_STORE_1:
7462 case BUILT_IN_ATOMIC_STORE_2:
7463 case BUILT_IN_ATOMIC_STORE_4:
7464 case BUILT_IN_ATOMIC_STORE_8:
7465 case BUILT_IN_ATOMIC_STORE_16:
7466 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7467 target = expand_builtin_atomic_store (mode, exp);
7468 if (target)
7469 return const0_rtx;
7470 break;
7472 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7473 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7474 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7475 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7476 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7478 enum built_in_function lib;
7479 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7480 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7481 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7482 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7483 ignore, lib);
7484 if (target)
7485 return target;
7486 break;
7488 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7489 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7490 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7491 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7492 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7494 enum built_in_function lib;
7495 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7496 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7497 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7498 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7499 ignore, lib);
7500 if (target)
7501 return target;
7502 break;
7504 case BUILT_IN_ATOMIC_AND_FETCH_1:
7505 case BUILT_IN_ATOMIC_AND_FETCH_2:
7506 case BUILT_IN_ATOMIC_AND_FETCH_4:
7507 case BUILT_IN_ATOMIC_AND_FETCH_8:
7508 case BUILT_IN_ATOMIC_AND_FETCH_16:
7510 enum built_in_function lib;
7511 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7512 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7513 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7514 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7515 ignore, lib);
7516 if (target)
7517 return target;
7518 break;
7520 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7521 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7522 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7523 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7524 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7526 enum built_in_function lib;
7527 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7528 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7529 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7530 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7531 ignore, lib);
7532 if (target)
7533 return target;
7534 break;
7536 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7537 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7538 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7539 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7540 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7542 enum built_in_function lib;
7543 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7544 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7545 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7546 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7547 ignore, lib);
7548 if (target)
7549 return target;
7550 break;
7552 case BUILT_IN_ATOMIC_OR_FETCH_1:
7553 case BUILT_IN_ATOMIC_OR_FETCH_2:
7554 case BUILT_IN_ATOMIC_OR_FETCH_4:
7555 case BUILT_IN_ATOMIC_OR_FETCH_8:
7556 case BUILT_IN_ATOMIC_OR_FETCH_16:
7558 enum built_in_function lib;
7559 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7560 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7561 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7562 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7563 ignore, lib);
7564 if (target)
7565 return target;
7566 break;
7568 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7569 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7570 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7571 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7572 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7573 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7574 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7575 ignore, BUILT_IN_NONE);
7576 if (target)
7577 return target;
7578 break;
7580 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7581 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7582 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7583 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7584 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7585 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7586 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7587 ignore, BUILT_IN_NONE);
7588 if (target)
7589 return target;
7590 break;
7592 case BUILT_IN_ATOMIC_FETCH_AND_1:
7593 case BUILT_IN_ATOMIC_FETCH_AND_2:
7594 case BUILT_IN_ATOMIC_FETCH_AND_4:
7595 case BUILT_IN_ATOMIC_FETCH_AND_8:
7596 case BUILT_IN_ATOMIC_FETCH_AND_16:
7597 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7598 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7599 ignore, BUILT_IN_NONE);
7600 if (target)
7601 return target;
7602 break;
7604 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7605 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7606 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7607 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7608 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7609 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7610 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7611 ignore, BUILT_IN_NONE);
7612 if (target)
7613 return target;
7614 break;
7616 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7617 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7618 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7619 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7620 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7621 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7622 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7623 ignore, BUILT_IN_NONE);
7624 if (target)
7625 return target;
7626 break;
7628 case BUILT_IN_ATOMIC_FETCH_OR_1:
7629 case BUILT_IN_ATOMIC_FETCH_OR_2:
7630 case BUILT_IN_ATOMIC_FETCH_OR_4:
7631 case BUILT_IN_ATOMIC_FETCH_OR_8:
7632 case BUILT_IN_ATOMIC_FETCH_OR_16:
7633 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7634 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7635 ignore, BUILT_IN_NONE);
7636 if (target)
7637 return target;
7638 break;
7640 case BUILT_IN_ATOMIC_TEST_AND_SET:
7641 return expand_builtin_atomic_test_and_set (exp, target);
7643 case BUILT_IN_ATOMIC_CLEAR:
7644 return expand_builtin_atomic_clear (exp);
7646 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
7647 return expand_builtin_atomic_always_lock_free (exp);
7649 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
7650 target = expand_builtin_atomic_is_lock_free (exp);
7651 if (target)
7652 return target;
7653 break;
7655 case BUILT_IN_ATOMIC_THREAD_FENCE:
7656 expand_builtin_atomic_thread_fence (exp);
7657 return const0_rtx;
7659 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
7660 expand_builtin_atomic_signal_fence (exp);
7661 return const0_rtx;
7663 case BUILT_IN_OBJECT_SIZE:
7664 return expand_builtin_object_size (exp);
7666 case BUILT_IN_MEMCPY_CHK:
7667 case BUILT_IN_MEMPCPY_CHK:
7668 case BUILT_IN_MEMMOVE_CHK:
7669 case BUILT_IN_MEMSET_CHK:
7670 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7671 if (target)
7672 return target;
7673 break;
7675 case BUILT_IN_STRCPY_CHK:
7676 case BUILT_IN_STPCPY_CHK:
7677 case BUILT_IN_STRNCPY_CHK:
7678 case BUILT_IN_STPNCPY_CHK:
7679 case BUILT_IN_STRCAT_CHK:
7680 case BUILT_IN_STRNCAT_CHK:
7681 case BUILT_IN_SNPRINTF_CHK:
7682 case BUILT_IN_VSNPRINTF_CHK:
7683 maybe_emit_chk_warning (exp, fcode);
7684 break;
7686 case BUILT_IN_SPRINTF_CHK:
7687 case BUILT_IN_VSPRINTF_CHK:
7688 maybe_emit_sprintf_chk_warning (exp, fcode);
7689 break;
7691 case BUILT_IN_FREE:
7692 if (warn_free_nonheap_object)
7693 maybe_emit_free_warning (exp);
7694 break;
7696 case BUILT_IN_THREAD_POINTER:
7697 return expand_builtin_thread_pointer (exp, target);
7699 case BUILT_IN_SET_THREAD_POINTER:
7700 expand_builtin_set_thread_pointer (exp);
7701 return const0_rtx;
7703 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7704 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7705 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7706 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7707 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7708 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7709 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7710 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7711 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7712 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7713 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7714 /* We allow user CHKP builtins if Pointer Bounds
7715 Checker is off. */
7716 if (!chkp_function_instrumented_p (current_function_decl))
7718 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7719 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7720 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7721 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7722 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7723 return expand_normal (CALL_EXPR_ARG (exp, 0));
7724 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7725 return expand_normal (size_zero_node);
7726 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7727 return expand_normal (size_int (-1));
7728 else
7729 return const0_rtx;
7731 /* FALLTHROUGH */
7733 case BUILT_IN_CHKP_BNDMK:
7734 case BUILT_IN_CHKP_BNDSTX:
7735 case BUILT_IN_CHKP_BNDCL:
7736 case BUILT_IN_CHKP_BNDCU:
7737 case BUILT_IN_CHKP_BNDLDX:
7738 case BUILT_IN_CHKP_BNDRET:
7739 case BUILT_IN_CHKP_INTERSECT:
7740 case BUILT_IN_CHKP_NARROW:
7741 case BUILT_IN_CHKP_EXTRACT_LOWER:
7742 case BUILT_IN_CHKP_EXTRACT_UPPER:
7743 /* Software implementation of Pointer Bounds Checker is NYI.
7744 Target support is required. */
7745 error ("Your target platform does not support -fcheck-pointer-bounds");
7746 break;
7748 case BUILT_IN_ACC_ON_DEVICE:
7749 /* Do library call, if we failed to expand the builtin when
7750 folding. */
7751 break;
7753 default: /* just do library call, if unknown builtin */
7754 break;
7757 /* The switch statement above can drop through to cause the function
7758 to be called normally. */
7759 return expand_call (exp, target, ignore);
7762 /* Similar to expand_builtin but is used for instrumented calls. */
7765 expand_builtin_with_bounds (tree exp, rtx target,
7766 rtx subtarget ATTRIBUTE_UNUSED,
7767 machine_mode mode, int ignore)
7769 tree fndecl = get_callee_fndecl (exp);
7770 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7772 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7774 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7775 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7777 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7778 && fcode < END_CHKP_BUILTINS);
7780 switch (fcode)
7782 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7783 target = expand_builtin_memcpy_with_bounds (exp, target);
7784 if (target)
7785 return target;
7786 break;
7788 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7789 target = expand_builtin_mempcpy_with_bounds (exp, target);
7790 if (target)
7791 return target;
7792 break;
7794 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7795 target = expand_builtin_memset_with_bounds (exp, target, mode);
7796 if (target)
7797 return target;
7798 break;
7800 case BUILT_IN_MEMCPY_CHKP:
7801 case BUILT_IN_MEMMOVE_CHKP:
7802 case BUILT_IN_MEMPCPY_CHKP:
7803 if (call_expr_nargs (exp) > 3)
7805 /* memcpy_chkp (void *dst, size_t dstbnd,
7806 const void *src, size_t srcbnd, size_t n)
7807 and others take a pointer bound argument just after each
7808 pointer argument. */
7809 tree dest = CALL_EXPR_ARG (exp, 0);
7810 tree src = CALL_EXPR_ARG (exp, 2);
7811 tree len = CALL_EXPR_ARG (exp, 4);
7813 check_memop_access (exp, dest, src, len);
7814 break;
7817 default:
7818 break;
7821 /* The switch statement above can drop through to cause the function
7822 to be called normally. */
7823 return expand_call (exp, target, ignore);
7826 /* Determine whether a tree node represents a call to a built-in
7827 function. If the tree T is a call to a built-in function with
7828 the right number of arguments of the appropriate types, return
7829 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7830 Otherwise the return value is END_BUILTINS. */
7832 enum built_in_function
7833 builtin_mathfn_code (const_tree t)
7835 const_tree fndecl, arg, parmlist;
7836 const_tree argtype, parmtype;
7837 const_call_expr_arg_iterator iter;
7839 if (TREE_CODE (t) != CALL_EXPR
7840 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7841 return END_BUILTINS;
7843 fndecl = get_callee_fndecl (t);
7844 if (fndecl == NULL_TREE
7845 || TREE_CODE (fndecl) != FUNCTION_DECL
7846 || ! DECL_BUILT_IN (fndecl)
7847 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7848 return END_BUILTINS;
7850 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7851 init_const_call_expr_arg_iterator (t, &iter);
7852 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7854 /* If a function doesn't take a variable number of arguments,
7855 the last element in the list will have type `void'. */
7856 parmtype = TREE_VALUE (parmlist);
7857 if (VOID_TYPE_P (parmtype))
7859 if (more_const_call_expr_args_p (&iter))
7860 return END_BUILTINS;
7861 return DECL_FUNCTION_CODE (fndecl);
7864 if (! more_const_call_expr_args_p (&iter))
7865 return END_BUILTINS;
7867 arg = next_const_call_expr_arg (&iter);
7868 argtype = TREE_TYPE (arg);
7870 if (SCALAR_FLOAT_TYPE_P (parmtype))
7872 if (! SCALAR_FLOAT_TYPE_P (argtype))
7873 return END_BUILTINS;
7875 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7877 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7878 return END_BUILTINS;
7880 else if (POINTER_TYPE_P (parmtype))
7882 if (! POINTER_TYPE_P (argtype))
7883 return END_BUILTINS;
7885 else if (INTEGRAL_TYPE_P (parmtype))
7887 if (! INTEGRAL_TYPE_P (argtype))
7888 return END_BUILTINS;
7890 else
7891 return END_BUILTINS;
7894 /* Variable-length argument list. */
7895 return DECL_FUNCTION_CODE (fndecl);
7898 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7899 evaluate to a constant. */
7901 static tree
7902 fold_builtin_constant_p (tree arg)
7904 /* We return 1 for a numeric type that's known to be a constant
7905 value at compile-time or for an aggregate type that's a
7906 literal constant. */
7907 STRIP_NOPS (arg);
7909 /* If we know this is a constant, emit the constant of one. */
7910 if (CONSTANT_CLASS_P (arg)
7911 || (TREE_CODE (arg) == CONSTRUCTOR
7912 && TREE_CONSTANT (arg)))
7913 return integer_one_node;
7914 if (TREE_CODE (arg) == ADDR_EXPR)
7916 tree op = TREE_OPERAND (arg, 0);
7917 if (TREE_CODE (op) == STRING_CST
7918 || (TREE_CODE (op) == ARRAY_REF
7919 && integer_zerop (TREE_OPERAND (op, 1))
7920 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7921 return integer_one_node;
7924 /* If this expression has side effects, show we don't know it to be a
7925 constant. Likewise if it's a pointer or aggregate type since in
7926 those case we only want literals, since those are only optimized
7927 when generating RTL, not later.
7928 And finally, if we are compiling an initializer, not code, we
7929 need to return a definite result now; there's not going to be any
7930 more optimization done. */
7931 if (TREE_SIDE_EFFECTS (arg)
7932 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7933 || POINTER_TYPE_P (TREE_TYPE (arg))
7934 || cfun == 0
7935 || folding_initializer
7936 || force_folding_builtin_constant_p)
7937 return integer_zero_node;
7939 return NULL_TREE;
7942 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7943 return it as a truthvalue. */
7945 static tree
7946 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7947 tree predictor)
7949 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7951 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7952 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7953 ret_type = TREE_TYPE (TREE_TYPE (fn));
7954 pred_type = TREE_VALUE (arg_types);
7955 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7957 pred = fold_convert_loc (loc, pred_type, pred);
7958 expected = fold_convert_loc (loc, expected_type, expected);
7959 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7960 predictor);
7962 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7963 build_int_cst (ret_type, 0));
7966 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7967 NULL_TREE if no simplification is possible. */
7969 tree
7970 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7972 tree inner, fndecl, inner_arg0;
7973 enum tree_code code;
7975 /* Distribute the expected value over short-circuiting operators.
7976 See through the cast from truthvalue_type_node to long. */
7977 inner_arg0 = arg0;
7978 while (CONVERT_EXPR_P (inner_arg0)
7979 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7980 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7981 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7983 /* If this is a builtin_expect within a builtin_expect keep the
7984 inner one. See through a comparison against a constant. It
7985 might have been added to create a thruthvalue. */
7986 inner = inner_arg0;
7988 if (COMPARISON_CLASS_P (inner)
7989 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7990 inner = TREE_OPERAND (inner, 0);
7992 if (TREE_CODE (inner) == CALL_EXPR
7993 && (fndecl = get_callee_fndecl (inner))
7994 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7995 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7996 return arg0;
7998 inner = inner_arg0;
7999 code = TREE_CODE (inner);
8000 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8002 tree op0 = TREE_OPERAND (inner, 0);
8003 tree op1 = TREE_OPERAND (inner, 1);
8005 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
8006 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
8007 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8009 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8012 /* If the argument isn't invariant then there's nothing else we can do. */
8013 if (!TREE_CONSTANT (inner_arg0))
8014 return NULL_TREE;
8016 /* If we expect that a comparison against the argument will fold to
8017 a constant return the constant. In practice, this means a true
8018 constant or the address of a non-weak symbol. */
8019 inner = inner_arg0;
8020 STRIP_NOPS (inner);
8021 if (TREE_CODE (inner) == ADDR_EXPR)
8025 inner = TREE_OPERAND (inner, 0);
8027 while (TREE_CODE (inner) == COMPONENT_REF
8028 || TREE_CODE (inner) == ARRAY_REF);
8029 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8030 return NULL_TREE;
8033 /* Otherwise, ARG0 already has the proper type for the return value. */
8034 return arg0;
8037 /* Fold a call to __builtin_classify_type with argument ARG. */
8039 static tree
8040 fold_builtin_classify_type (tree arg)
8042 if (arg == 0)
8043 return build_int_cst (integer_type_node, no_type_class);
8045 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8048 /* Fold a call to __builtin_strlen with argument ARG. */
8050 static tree
8051 fold_builtin_strlen (location_t loc, tree type, tree arg)
8053 if (!validate_arg (arg, POINTER_TYPE))
8054 return NULL_TREE;
8055 else
8057 tree len = c_strlen (arg, 0);
8059 if (len)
8060 return fold_convert_loc (loc, type, len);
8062 return NULL_TREE;
8066 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8068 static tree
8069 fold_builtin_inf (location_t loc, tree type, int warn)
8071 REAL_VALUE_TYPE real;
8073 /* __builtin_inff is intended to be usable to define INFINITY on all
8074 targets. If an infinity is not available, INFINITY expands "to a
8075 positive constant of type float that overflows at translation
8076 time", footnote "In this case, using INFINITY will violate the
8077 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8078 Thus we pedwarn to ensure this constraint violation is
8079 diagnosed. */
8080 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8081 pedwarn (loc, 0, "target format does not support infinity");
8083 real_inf (&real);
8084 return build_real (type, real);
8087 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8088 NULL_TREE if no simplification can be made. */
8090 static tree
8091 fold_builtin_sincos (location_t loc,
8092 tree arg0, tree arg1, tree arg2)
8094 tree type;
8095 tree fndecl, call = NULL_TREE;
8097 if (!validate_arg (arg0, REAL_TYPE)
8098 || !validate_arg (arg1, POINTER_TYPE)
8099 || !validate_arg (arg2, POINTER_TYPE))
8100 return NULL_TREE;
8102 type = TREE_TYPE (arg0);
8104 /* Calculate the result when the argument is a constant. */
8105 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8106 if (fn == END_BUILTINS)
8107 return NULL_TREE;
8109 /* Canonicalize sincos to cexpi. */
8110 if (TREE_CODE (arg0) == REAL_CST)
8112 tree complex_type = build_complex_type (type);
8113 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8115 if (!call)
8117 if (!targetm.libc_has_function (function_c99_math_complex)
8118 || !builtin_decl_implicit_p (fn))
8119 return NULL_TREE;
8120 fndecl = builtin_decl_explicit (fn);
8121 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8122 call = builtin_save_expr (call);
8125 return build2 (COMPOUND_EXPR, void_type_node,
8126 build2 (MODIFY_EXPR, void_type_node,
8127 build_fold_indirect_ref_loc (loc, arg1),
8128 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8129 build2 (MODIFY_EXPR, void_type_node,
8130 build_fold_indirect_ref_loc (loc, arg2),
8131 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8134 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8135 Return NULL_TREE if no simplification can be made. */
8137 static tree
8138 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8140 if (!validate_arg (arg1, POINTER_TYPE)
8141 || !validate_arg (arg2, POINTER_TYPE)
8142 || !validate_arg (len, INTEGER_TYPE))
8143 return NULL_TREE;
8145 /* If the LEN parameter is zero, return zero. */
8146 if (integer_zerop (len))
8147 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8148 arg1, arg2);
8150 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8151 if (operand_equal_p (arg1, arg2, 0))
8152 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8154 /* If len parameter is one, return an expression corresponding to
8155 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8156 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8158 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8159 tree cst_uchar_ptr_node
8160 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8162 tree ind1
8163 = fold_convert_loc (loc, integer_type_node,
8164 build1 (INDIRECT_REF, cst_uchar_node,
8165 fold_convert_loc (loc,
8166 cst_uchar_ptr_node,
8167 arg1)));
8168 tree ind2
8169 = fold_convert_loc (loc, integer_type_node,
8170 build1 (INDIRECT_REF, cst_uchar_node,
8171 fold_convert_loc (loc,
8172 cst_uchar_ptr_node,
8173 arg2)));
8174 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8177 return NULL_TREE;
8180 /* Fold a call to builtin isascii with argument ARG. */
8182 static tree
8183 fold_builtin_isascii (location_t loc, tree arg)
8185 if (!validate_arg (arg, INTEGER_TYPE))
8186 return NULL_TREE;
8187 else
8189 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8190 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8191 build_int_cst (integer_type_node,
8192 ~ (unsigned HOST_WIDE_INT) 0x7f));
8193 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8194 arg, integer_zero_node);
8198 /* Fold a call to builtin toascii with argument ARG. */
8200 static tree
8201 fold_builtin_toascii (location_t loc, tree arg)
8203 if (!validate_arg (arg, INTEGER_TYPE))
8204 return NULL_TREE;
8206 /* Transform toascii(c) -> (c & 0x7f). */
8207 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8208 build_int_cst (integer_type_node, 0x7f));
8211 /* Fold a call to builtin isdigit with argument ARG. */
8213 static tree
8214 fold_builtin_isdigit (location_t loc, tree arg)
8216 if (!validate_arg (arg, INTEGER_TYPE))
8217 return NULL_TREE;
8218 else
8220 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8221 /* According to the C standard, isdigit is unaffected by locale.
8222 However, it definitely is affected by the target character set. */
8223 unsigned HOST_WIDE_INT target_digit0
8224 = lang_hooks.to_target_charset ('0');
8226 if (target_digit0 == 0)
8227 return NULL_TREE;
8229 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8230 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8231 build_int_cst (unsigned_type_node, target_digit0));
8232 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8233 build_int_cst (unsigned_type_node, 9));
8237 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8239 static tree
8240 fold_builtin_fabs (location_t loc, tree arg, tree type)
8242 if (!validate_arg (arg, REAL_TYPE))
8243 return NULL_TREE;
8245 arg = fold_convert_loc (loc, type, arg);
8246 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8249 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8251 static tree
8252 fold_builtin_abs (location_t loc, tree arg, tree type)
8254 if (!validate_arg (arg, INTEGER_TYPE))
8255 return NULL_TREE;
8257 arg = fold_convert_loc (loc, type, arg);
8258 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8261 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8263 static tree
8264 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
8266 /* ??? Only expand to FMA_EXPR if it's directly supported. */
8267 if (validate_arg (arg0, REAL_TYPE)
8268 && validate_arg (arg1, REAL_TYPE)
8269 && validate_arg (arg2, REAL_TYPE)
8270 && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
8271 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
8273 return NULL_TREE;
8276 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8278 static tree
8279 fold_builtin_carg (location_t loc, tree arg, tree type)
8281 if (validate_arg (arg, COMPLEX_TYPE)
8282 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8284 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8286 if (atan2_fn)
8288 tree new_arg = builtin_save_expr (arg);
8289 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8290 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8291 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8295 return NULL_TREE;
8298 /* Fold a call to builtin frexp, we can assume the base is 2. */
8300 static tree
8301 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8303 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8304 return NULL_TREE;
8306 STRIP_NOPS (arg0);
8308 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8309 return NULL_TREE;
8311 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8313 /* Proceed if a valid pointer type was passed in. */
8314 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8316 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8317 tree frac, exp;
8319 switch (value->cl)
8321 case rvc_zero:
8322 /* For +-0, return (*exp = 0, +-0). */
8323 exp = integer_zero_node;
8324 frac = arg0;
8325 break;
8326 case rvc_nan:
8327 case rvc_inf:
8328 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8329 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8330 case rvc_normal:
8332 /* Since the frexp function always expects base 2, and in
8333 GCC normalized significands are already in the range
8334 [0.5, 1.0), we have exactly what frexp wants. */
8335 REAL_VALUE_TYPE frac_rvt = *value;
8336 SET_REAL_EXP (&frac_rvt, 0);
8337 frac = build_real (rettype, frac_rvt);
8338 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8340 break;
8341 default:
8342 gcc_unreachable ();
8345 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8346 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8347 TREE_SIDE_EFFECTS (arg1) = 1;
8348 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8351 return NULL_TREE;
8354 /* Fold a call to builtin modf. */
8356 static tree
8357 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8359 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8360 return NULL_TREE;
8362 STRIP_NOPS (arg0);
8364 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8365 return NULL_TREE;
8367 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8369 /* Proceed if a valid pointer type was passed in. */
8370 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8372 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8373 REAL_VALUE_TYPE trunc, frac;
8375 switch (value->cl)
8377 case rvc_nan:
8378 case rvc_zero:
8379 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8380 trunc = frac = *value;
8381 break;
8382 case rvc_inf:
8383 /* For +-Inf, return (*arg1 = arg0, +-0). */
8384 frac = dconst0;
8385 frac.sign = value->sign;
8386 trunc = *value;
8387 break;
8388 case rvc_normal:
8389 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8390 real_trunc (&trunc, VOIDmode, value);
8391 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8392 /* If the original number was negative and already
8393 integral, then the fractional part is -0.0. */
8394 if (value->sign && frac.cl == rvc_zero)
8395 frac.sign = value->sign;
8396 break;
8399 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8400 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8401 build_real (rettype, trunc));
8402 TREE_SIDE_EFFECTS (arg1) = 1;
8403 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8404 build_real (rettype, frac));
8407 return NULL_TREE;
8410 /* Given a location LOC, an interclass builtin function decl FNDECL
8411 and its single argument ARG, return an folded expression computing
8412 the same, or NULL_TREE if we either couldn't or didn't want to fold
8413 (the latter happen if there's an RTL instruction available). */
8415 static tree
8416 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8418 machine_mode mode;
8420 if (!validate_arg (arg, REAL_TYPE))
8421 return NULL_TREE;
8423 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8424 return NULL_TREE;
8426 mode = TYPE_MODE (TREE_TYPE (arg));
8428 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8430 /* If there is no optab, try generic code. */
8431 switch (DECL_FUNCTION_CODE (fndecl))
8433 tree result;
8435 CASE_FLT_FN (BUILT_IN_ISINF):
8437 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8438 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8439 tree type = TREE_TYPE (arg);
8440 REAL_VALUE_TYPE r;
8441 char buf[128];
8443 if (is_ibm_extended)
8445 /* NaN and Inf are encoded in the high-order double value
8446 only. The low-order value is not significant. */
8447 type = double_type_node;
8448 mode = DFmode;
8449 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8451 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8452 real_from_string (&r, buf);
8453 result = build_call_expr (isgr_fn, 2,
8454 fold_build1_loc (loc, ABS_EXPR, type, arg),
8455 build_real (type, r));
8456 return result;
8458 CASE_FLT_FN (BUILT_IN_FINITE):
8459 case BUILT_IN_ISFINITE:
8461 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8462 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8463 tree type = TREE_TYPE (arg);
8464 REAL_VALUE_TYPE r;
8465 char buf[128];
8467 if (is_ibm_extended)
8469 /* NaN and Inf are encoded in the high-order double value
8470 only. The low-order value is not significant. */
8471 type = double_type_node;
8472 mode = DFmode;
8473 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8475 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8476 real_from_string (&r, buf);
8477 result = build_call_expr (isle_fn, 2,
8478 fold_build1_loc (loc, ABS_EXPR, type, arg),
8479 build_real (type, r));
8480 /*result = fold_build2_loc (loc, UNGT_EXPR,
8481 TREE_TYPE (TREE_TYPE (fndecl)),
8482 fold_build1_loc (loc, ABS_EXPR, type, arg),
8483 build_real (type, r));
8484 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8485 TREE_TYPE (TREE_TYPE (fndecl)),
8486 result);*/
8487 return result;
8489 case BUILT_IN_ISNORMAL:
8491 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8492 islessequal(fabs(x),DBL_MAX). */
8493 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8494 tree type = TREE_TYPE (arg);
8495 tree orig_arg, max_exp, min_exp;
8496 machine_mode orig_mode = mode;
8497 REAL_VALUE_TYPE rmax, rmin;
8498 char buf[128];
8500 orig_arg = arg = builtin_save_expr (arg);
8501 if (is_ibm_extended)
8503 /* Use double to test the normal range of IBM extended
8504 precision. Emin for IBM extended precision is
8505 different to emin for IEEE double, being 53 higher
8506 since the low double exponent is at least 53 lower
8507 than the high double exponent. */
8508 type = double_type_node;
8509 mode = DFmode;
8510 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8512 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8514 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8515 real_from_string (&rmax, buf);
8516 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8517 real_from_string (&rmin, buf);
8518 max_exp = build_real (type, rmax);
8519 min_exp = build_real (type, rmin);
8521 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8522 if (is_ibm_extended)
8524 /* Testing the high end of the range is done just using
8525 the high double, using the same test as isfinite().
8526 For the subnormal end of the range we first test the
8527 high double, then if its magnitude is equal to the
8528 limit of 0x1p-969, we test whether the low double is
8529 non-zero and opposite sign to the high double. */
8530 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8531 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8532 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8533 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8534 arg, min_exp);
8535 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8536 complex_double_type_node, orig_arg);
8537 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8538 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8539 tree zero = build_real (type, dconst0);
8540 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8541 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8542 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8543 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8544 fold_build3 (COND_EXPR,
8545 integer_type_node,
8546 hilt, logt, lolt));
8547 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8548 eq_min, ok_lo);
8549 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8550 gt_min, eq_min);
8552 else
8554 tree const isge_fn
8555 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8556 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8558 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8559 max_exp, min_exp);
8560 return result;
8562 default:
8563 break;
8566 return NULL_TREE;
8569 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8570 ARG is the argument for the call. */
8572 static tree
8573 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8575 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8577 if (!validate_arg (arg, REAL_TYPE))
8578 return NULL_TREE;
8580 switch (builtin_index)
8582 case BUILT_IN_ISINF:
8583 if (!HONOR_INFINITIES (arg))
8584 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8586 return NULL_TREE;
8588 case BUILT_IN_ISINF_SIGN:
8590 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8591 /* In a boolean context, GCC will fold the inner COND_EXPR to
8592 1. So e.g. "if (isinf_sign(x))" would be folded to just
8593 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8594 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8595 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8596 tree tmp = NULL_TREE;
8598 arg = builtin_save_expr (arg);
8600 if (signbit_fn && isinf_fn)
8602 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8603 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8605 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8606 signbit_call, integer_zero_node);
8607 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8608 isinf_call, integer_zero_node);
8610 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8611 integer_minus_one_node, integer_one_node);
8612 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8613 isinf_call, tmp,
8614 integer_zero_node);
8617 return tmp;
8620 case BUILT_IN_ISFINITE:
8621 if (!HONOR_NANS (arg)
8622 && !HONOR_INFINITIES (arg))
8623 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8625 return NULL_TREE;
8627 case BUILT_IN_ISNAN:
8628 if (!HONOR_NANS (arg))
8629 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8632 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8633 if (is_ibm_extended)
8635 /* NaN and Inf are encoded in the high-order double value
8636 only. The low-order value is not significant. */
8637 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8640 arg = builtin_save_expr (arg);
8641 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8643 default:
8644 gcc_unreachable ();
8648 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8649 This builtin will generate code to return the appropriate floating
8650 point classification depending on the value of the floating point
8651 number passed in. The possible return values must be supplied as
8652 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8653 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8654 one floating point argument which is "type generic". */
8656 static tree
8657 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8659 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8660 arg, type, res, tmp;
8661 machine_mode mode;
8662 REAL_VALUE_TYPE r;
8663 char buf[128];
8665 /* Verify the required arguments in the original call. */
8666 if (nargs != 6
8667 || !validate_arg (args[0], INTEGER_TYPE)
8668 || !validate_arg (args[1], INTEGER_TYPE)
8669 || !validate_arg (args[2], INTEGER_TYPE)
8670 || !validate_arg (args[3], INTEGER_TYPE)
8671 || !validate_arg (args[4], INTEGER_TYPE)
8672 || !validate_arg (args[5], REAL_TYPE))
8673 return NULL_TREE;
8675 fp_nan = args[0];
8676 fp_infinite = args[1];
8677 fp_normal = args[2];
8678 fp_subnormal = args[3];
8679 fp_zero = args[4];
8680 arg = args[5];
8681 type = TREE_TYPE (arg);
8682 mode = TYPE_MODE (type);
8683 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8685 /* fpclassify(x) ->
8686 isnan(x) ? FP_NAN :
8687 (fabs(x) == Inf ? FP_INFINITE :
8688 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8689 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8691 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8692 build_real (type, dconst0));
8693 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8694 tmp, fp_zero, fp_subnormal);
8696 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8697 real_from_string (&r, buf);
8698 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8699 arg, build_real (type, r));
8700 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8702 if (HONOR_INFINITIES (mode))
8704 real_inf (&r);
8705 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8706 build_real (type, r));
8707 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8708 fp_infinite, res);
8711 if (HONOR_NANS (mode))
8713 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8714 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8717 return res;
8720 /* Fold a call to an unordered comparison function such as
8721 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8722 being called and ARG0 and ARG1 are the arguments for the call.
8723 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8724 the opposite of the desired result. UNORDERED_CODE is used
8725 for modes that can hold NaNs and ORDERED_CODE is used for
8726 the rest. */
8728 static tree
8729 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
8730 enum tree_code unordered_code,
8731 enum tree_code ordered_code)
8733 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8734 enum tree_code code;
8735 tree type0, type1;
8736 enum tree_code code0, code1;
8737 tree cmp_type = NULL_TREE;
8739 type0 = TREE_TYPE (arg0);
8740 type1 = TREE_TYPE (arg1);
8742 code0 = TREE_CODE (type0);
8743 code1 = TREE_CODE (type1);
8745 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8746 /* Choose the wider of two real types. */
8747 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8748 ? type0 : type1;
8749 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8750 cmp_type = type0;
8751 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8752 cmp_type = type1;
8754 arg0 = fold_convert_loc (loc, cmp_type, arg0);
8755 arg1 = fold_convert_loc (loc, cmp_type, arg1);
8757 if (unordered_code == UNORDERED_EXPR)
8759 if (!HONOR_NANS (arg0))
8760 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
8761 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
8764 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
8765 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
8766 fold_build2_loc (loc, code, type, arg0, arg1));
8769 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8770 arithmetics if it can never overflow, or into internal functions that
8771 return both result of arithmetics and overflowed boolean flag in
8772 a complex integer result, or some other check for overflow.
8773 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
8774 checking part of that. */
8776 static tree
8777 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
8778 tree arg0, tree arg1, tree arg2)
8780 enum internal_fn ifn = IFN_LAST;
8781 /* The code of the expression corresponding to the type-generic
8782 built-in, or ERROR_MARK for the type-specific ones. */
8783 enum tree_code opcode = ERROR_MARK;
8784 bool ovf_only = false;
8786 switch (fcode)
8788 case BUILT_IN_ADD_OVERFLOW_P:
8789 ovf_only = true;
8790 /* FALLTHRU */
8791 case BUILT_IN_ADD_OVERFLOW:
8792 opcode = PLUS_EXPR;
8793 /* FALLTHRU */
8794 case BUILT_IN_SADD_OVERFLOW:
8795 case BUILT_IN_SADDL_OVERFLOW:
8796 case BUILT_IN_SADDLL_OVERFLOW:
8797 case BUILT_IN_UADD_OVERFLOW:
8798 case BUILT_IN_UADDL_OVERFLOW:
8799 case BUILT_IN_UADDLL_OVERFLOW:
8800 ifn = IFN_ADD_OVERFLOW;
8801 break;
8802 case BUILT_IN_SUB_OVERFLOW_P:
8803 ovf_only = true;
8804 /* FALLTHRU */
8805 case BUILT_IN_SUB_OVERFLOW:
8806 opcode = MINUS_EXPR;
8807 /* FALLTHRU */
8808 case BUILT_IN_SSUB_OVERFLOW:
8809 case BUILT_IN_SSUBL_OVERFLOW:
8810 case BUILT_IN_SSUBLL_OVERFLOW:
8811 case BUILT_IN_USUB_OVERFLOW:
8812 case BUILT_IN_USUBL_OVERFLOW:
8813 case BUILT_IN_USUBLL_OVERFLOW:
8814 ifn = IFN_SUB_OVERFLOW;
8815 break;
8816 case BUILT_IN_MUL_OVERFLOW_P:
8817 ovf_only = true;
8818 /* FALLTHRU */
8819 case BUILT_IN_MUL_OVERFLOW:
8820 opcode = MULT_EXPR;
8821 /* FALLTHRU */
8822 case BUILT_IN_SMUL_OVERFLOW:
8823 case BUILT_IN_SMULL_OVERFLOW:
8824 case BUILT_IN_SMULLL_OVERFLOW:
8825 case BUILT_IN_UMUL_OVERFLOW:
8826 case BUILT_IN_UMULL_OVERFLOW:
8827 case BUILT_IN_UMULLL_OVERFLOW:
8828 ifn = IFN_MUL_OVERFLOW;
8829 break;
8830 default:
8831 gcc_unreachable ();
8834 /* For the "generic" overloads, the first two arguments can have different
8835 types and the last argument determines the target type to use to check
8836 for overflow. The arguments of the other overloads all have the same
8837 type. */
8838 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
8840 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
8841 arguments are constant, attempt to fold the built-in call into a constant
8842 expression indicating whether or not it detected an overflow. */
8843 if (ovf_only
8844 && TREE_CODE (arg0) == INTEGER_CST
8845 && TREE_CODE (arg1) == INTEGER_CST)
8846 /* Perform the computation in the target type and check for overflow. */
8847 return omit_one_operand_loc (loc, boolean_type_node,
8848 arith_overflowed_p (opcode, type, arg0, arg1)
8849 ? boolean_true_node : boolean_false_node,
8850 arg2);
8852 tree ctype = build_complex_type (type);
8853 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
8854 2, arg0, arg1);
8855 tree tgt = save_expr (call);
8856 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
8857 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
8858 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
8860 if (ovf_only)
8861 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
8863 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
8864 tree store
8865 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
8866 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
8869 /* Fold a call to __builtin_FILE to a constant string. */
8871 static inline tree
8872 fold_builtin_FILE (location_t loc)
8874 if (const char *fname = LOCATION_FILE (loc))
8876 /* The documentation says this builtin is equivalent to the preprocessor
8877 __FILE__ macro so it appears appropriate to use the same file prefix
8878 mappings. */
8879 fname = remap_macro_filename (fname);
8880 return build_string_literal (strlen (fname) + 1, fname);
8883 return build_string_literal (1, "");
8886 /* Fold a call to __builtin_FUNCTION to a constant string. */
8888 static inline tree
8889 fold_builtin_FUNCTION ()
8891 const char *name = "";
8893 if (current_function_decl)
8894 name = lang_hooks.decl_printable_name (current_function_decl, 0);
8896 return build_string_literal (strlen (name) + 1, name);
8899 /* Fold a call to __builtin_LINE to an integer constant. */
8901 static inline tree
8902 fold_builtin_LINE (location_t loc, tree type)
8904 return build_int_cst (type, LOCATION_LINE (loc));
8907 /* Fold a call to built-in function FNDECL with 0 arguments.
8908 This function returns NULL_TREE if no simplification was possible. */
8910 static tree
8911 fold_builtin_0 (location_t loc, tree fndecl)
8913 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8914 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8915 switch (fcode)
8917 case BUILT_IN_FILE:
8918 return fold_builtin_FILE (loc);
8920 case BUILT_IN_FUNCTION:
8921 return fold_builtin_FUNCTION ();
8923 case BUILT_IN_LINE:
8924 return fold_builtin_LINE (loc, type);
8926 CASE_FLT_FN (BUILT_IN_INF):
8927 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
8928 case BUILT_IN_INFD32:
8929 case BUILT_IN_INFD64:
8930 case BUILT_IN_INFD128:
8931 return fold_builtin_inf (loc, type, true);
8933 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
8934 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
8935 return fold_builtin_inf (loc, type, false);
8937 case BUILT_IN_CLASSIFY_TYPE:
8938 return fold_builtin_classify_type (NULL_TREE);
8940 default:
8941 break;
8943 return NULL_TREE;
8946 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
8947 This function returns NULL_TREE if no simplification was possible. */
8949 static tree
8950 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
8952 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8953 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8955 if (TREE_CODE (arg0) == ERROR_MARK)
8956 return NULL_TREE;
8958 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
8959 return ret;
8961 switch (fcode)
8963 case BUILT_IN_CONSTANT_P:
8965 tree val = fold_builtin_constant_p (arg0);
8967 /* Gimplification will pull the CALL_EXPR for the builtin out of
8968 an if condition. When not optimizing, we'll not CSE it back.
8969 To avoid link error types of regressions, return false now. */
8970 if (!val && !optimize)
8971 val = integer_zero_node;
8973 return val;
8976 case BUILT_IN_CLASSIFY_TYPE:
8977 return fold_builtin_classify_type (arg0);
8979 case BUILT_IN_STRLEN:
8980 return fold_builtin_strlen (loc, type, arg0);
8982 CASE_FLT_FN (BUILT_IN_FABS):
8983 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
8984 case BUILT_IN_FABSD32:
8985 case BUILT_IN_FABSD64:
8986 case BUILT_IN_FABSD128:
8987 return fold_builtin_fabs (loc, arg0, type);
8989 case BUILT_IN_ABS:
8990 case BUILT_IN_LABS:
8991 case BUILT_IN_LLABS:
8992 case BUILT_IN_IMAXABS:
8993 return fold_builtin_abs (loc, arg0, type);
8995 CASE_FLT_FN (BUILT_IN_CONJ):
8996 if (validate_arg (arg0, COMPLEX_TYPE)
8997 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8998 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
8999 break;
9001 CASE_FLT_FN (BUILT_IN_CREAL):
9002 if (validate_arg (arg0, COMPLEX_TYPE)
9003 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9004 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9005 break;
9007 CASE_FLT_FN (BUILT_IN_CIMAG):
9008 if (validate_arg (arg0, COMPLEX_TYPE)
9009 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9010 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9011 break;
9013 CASE_FLT_FN (BUILT_IN_CARG):
9014 return fold_builtin_carg (loc, arg0, type);
9016 case BUILT_IN_ISASCII:
9017 return fold_builtin_isascii (loc, arg0);
9019 case BUILT_IN_TOASCII:
9020 return fold_builtin_toascii (loc, arg0);
9022 case BUILT_IN_ISDIGIT:
9023 return fold_builtin_isdigit (loc, arg0);
9025 CASE_FLT_FN (BUILT_IN_FINITE):
9026 case BUILT_IN_FINITED32:
9027 case BUILT_IN_FINITED64:
9028 case BUILT_IN_FINITED128:
9029 case BUILT_IN_ISFINITE:
9031 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9032 if (ret)
9033 return ret;
9034 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9037 CASE_FLT_FN (BUILT_IN_ISINF):
9038 case BUILT_IN_ISINFD32:
9039 case BUILT_IN_ISINFD64:
9040 case BUILT_IN_ISINFD128:
9042 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9043 if (ret)
9044 return ret;
9045 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9048 case BUILT_IN_ISNORMAL:
9049 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9051 case BUILT_IN_ISINF_SIGN:
9052 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9054 CASE_FLT_FN (BUILT_IN_ISNAN):
9055 case BUILT_IN_ISNAND32:
9056 case BUILT_IN_ISNAND64:
9057 case BUILT_IN_ISNAND128:
9058 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9060 case BUILT_IN_FREE:
9061 if (integer_zerop (arg0))
9062 return build_empty_stmt (loc);
9063 break;
9065 default:
9066 break;
9069 return NULL_TREE;
9073 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
9074 This function returns NULL_TREE if no simplification was possible. */
9076 static tree
9077 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
9079 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9080 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9082 if (TREE_CODE (arg0) == ERROR_MARK
9083 || TREE_CODE (arg1) == ERROR_MARK)
9084 return NULL_TREE;
9086 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9087 return ret;
9089 switch (fcode)
9091 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9092 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9093 if (validate_arg (arg0, REAL_TYPE)
9094 && validate_arg (arg1, POINTER_TYPE))
9095 return do_mpfr_lgamma_r (arg0, arg1, type);
9096 break;
9098 CASE_FLT_FN (BUILT_IN_FREXP):
9099 return fold_builtin_frexp (loc, arg0, arg1, type);
9101 CASE_FLT_FN (BUILT_IN_MODF):
9102 return fold_builtin_modf (loc, arg0, arg1, type);
9104 case BUILT_IN_STRSPN:
9105 return fold_builtin_strspn (loc, arg0, arg1);
9107 case BUILT_IN_STRCSPN:
9108 return fold_builtin_strcspn (loc, arg0, arg1);
9110 case BUILT_IN_STRPBRK:
9111 return fold_builtin_strpbrk (loc, arg0, arg1, type);
9113 case BUILT_IN_EXPECT:
9114 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
9116 case BUILT_IN_ISGREATER:
9117 return fold_builtin_unordered_cmp (loc, fndecl,
9118 arg0, arg1, UNLE_EXPR, LE_EXPR);
9119 case BUILT_IN_ISGREATEREQUAL:
9120 return fold_builtin_unordered_cmp (loc, fndecl,
9121 arg0, arg1, UNLT_EXPR, LT_EXPR);
9122 case BUILT_IN_ISLESS:
9123 return fold_builtin_unordered_cmp (loc, fndecl,
9124 arg0, arg1, UNGE_EXPR, GE_EXPR);
9125 case BUILT_IN_ISLESSEQUAL:
9126 return fold_builtin_unordered_cmp (loc, fndecl,
9127 arg0, arg1, UNGT_EXPR, GT_EXPR);
9128 case BUILT_IN_ISLESSGREATER:
9129 return fold_builtin_unordered_cmp (loc, fndecl,
9130 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9131 case BUILT_IN_ISUNORDERED:
9132 return fold_builtin_unordered_cmp (loc, fndecl,
9133 arg0, arg1, UNORDERED_EXPR,
9134 NOP_EXPR);
9136 /* We do the folding for va_start in the expander. */
9137 case BUILT_IN_VA_START:
9138 break;
9140 case BUILT_IN_OBJECT_SIZE:
9141 return fold_builtin_object_size (arg0, arg1);
9143 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9144 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9146 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9147 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9149 default:
9150 break;
9152 return NULL_TREE;
9155 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9156 and ARG2.
9157 This function returns NULL_TREE if no simplification was possible. */
9159 static tree
9160 fold_builtin_3 (location_t loc, tree fndecl,
9161 tree arg0, tree arg1, tree arg2)
9163 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9164 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9166 if (TREE_CODE (arg0) == ERROR_MARK
9167 || TREE_CODE (arg1) == ERROR_MARK
9168 || TREE_CODE (arg2) == ERROR_MARK)
9169 return NULL_TREE;
9171 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9172 arg0, arg1, arg2))
9173 return ret;
9175 switch (fcode)
9178 CASE_FLT_FN (BUILT_IN_SINCOS):
9179 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9181 CASE_FLT_FN (BUILT_IN_FMA):
9182 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
9183 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
9185 CASE_FLT_FN (BUILT_IN_REMQUO):
9186 if (validate_arg (arg0, REAL_TYPE)
9187 && validate_arg (arg1, REAL_TYPE)
9188 && validate_arg (arg2, POINTER_TYPE))
9189 return do_mpfr_remquo (arg0, arg1, arg2);
9190 break;
9192 case BUILT_IN_MEMCMP:
9193 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9195 case BUILT_IN_EXPECT:
9196 return fold_builtin_expect (loc, arg0, arg1, arg2);
9198 case BUILT_IN_ADD_OVERFLOW:
9199 case BUILT_IN_SUB_OVERFLOW:
9200 case BUILT_IN_MUL_OVERFLOW:
9201 case BUILT_IN_ADD_OVERFLOW_P:
9202 case BUILT_IN_SUB_OVERFLOW_P:
9203 case BUILT_IN_MUL_OVERFLOW_P:
9204 case BUILT_IN_SADD_OVERFLOW:
9205 case BUILT_IN_SADDL_OVERFLOW:
9206 case BUILT_IN_SADDLL_OVERFLOW:
9207 case BUILT_IN_SSUB_OVERFLOW:
9208 case BUILT_IN_SSUBL_OVERFLOW:
9209 case BUILT_IN_SSUBLL_OVERFLOW:
9210 case BUILT_IN_SMUL_OVERFLOW:
9211 case BUILT_IN_SMULL_OVERFLOW:
9212 case BUILT_IN_SMULLL_OVERFLOW:
9213 case BUILT_IN_UADD_OVERFLOW:
9214 case BUILT_IN_UADDL_OVERFLOW:
9215 case BUILT_IN_UADDLL_OVERFLOW:
9216 case BUILT_IN_USUB_OVERFLOW:
9217 case BUILT_IN_USUBL_OVERFLOW:
9218 case BUILT_IN_USUBLL_OVERFLOW:
9219 case BUILT_IN_UMUL_OVERFLOW:
9220 case BUILT_IN_UMULL_OVERFLOW:
9221 case BUILT_IN_UMULLL_OVERFLOW:
9222 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9224 default:
9225 break;
9227 return NULL_TREE;
9230 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
9231 arguments. IGNORE is true if the result of the
9232 function call is ignored. This function returns NULL_TREE if no
9233 simplification was possible. */
9235 tree
9236 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
9238 tree ret = NULL_TREE;
9240 switch (nargs)
9242 case 0:
9243 ret = fold_builtin_0 (loc, fndecl);
9244 break;
9245 case 1:
9246 ret = fold_builtin_1 (loc, fndecl, args[0]);
9247 break;
9248 case 2:
9249 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
9250 break;
9251 case 3:
9252 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9253 break;
9254 default:
9255 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9256 break;
9258 if (ret)
9260 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9261 SET_EXPR_LOCATION (ret, loc);
9262 TREE_NO_WARNING (ret) = 1;
9263 return ret;
9265 return NULL_TREE;
9268 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9269 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9270 of arguments in ARGS to be omitted. OLDNARGS is the number of
9271 elements in ARGS. */
9273 static tree
9274 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9275 int skip, tree fndecl, int n, va_list newargs)
9277 int nargs = oldnargs - skip + n;
9278 tree *buffer;
9280 if (n > 0)
9282 int i, j;
9284 buffer = XALLOCAVEC (tree, nargs);
9285 for (i = 0; i < n; i++)
9286 buffer[i] = va_arg (newargs, tree);
9287 for (j = skip; j < oldnargs; j++, i++)
9288 buffer[i] = args[j];
9290 else
9291 buffer = args + skip;
9293 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9296 /* Return true if FNDECL shouldn't be folded right now.
9297 If a built-in function has an inline attribute always_inline
9298 wrapper, defer folding it after always_inline functions have
9299 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9300 might not be performed. */
9302 bool
9303 avoid_folding_inline_builtin (tree fndecl)
9305 return (DECL_DECLARED_INLINE_P (fndecl)
9306 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9307 && cfun
9308 && !cfun->always_inline_functions_inlined
9309 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9312 /* A wrapper function for builtin folding that prevents warnings for
9313 "statement without effect" and the like, caused by removing the
9314 call node earlier than the warning is generated. */
9316 tree
9317 fold_call_expr (location_t loc, tree exp, bool ignore)
9319 tree ret = NULL_TREE;
9320 tree fndecl = get_callee_fndecl (exp);
9321 if (fndecl
9322 && TREE_CODE (fndecl) == FUNCTION_DECL
9323 && DECL_BUILT_IN (fndecl)
9324 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9325 yet. Defer folding until we see all the arguments
9326 (after inlining). */
9327 && !CALL_EXPR_VA_ARG_PACK (exp))
9329 int nargs = call_expr_nargs (exp);
9331 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9332 instead last argument is __builtin_va_arg_pack (). Defer folding
9333 even in that case, until arguments are finalized. */
9334 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9336 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9337 if (fndecl2
9338 && TREE_CODE (fndecl2) == FUNCTION_DECL
9339 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9340 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9341 return NULL_TREE;
9344 if (avoid_folding_inline_builtin (fndecl))
9345 return NULL_TREE;
9347 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9348 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9349 CALL_EXPR_ARGP (exp), ignore);
9350 else
9352 tree *args = CALL_EXPR_ARGP (exp);
9353 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9354 if (ret)
9355 return ret;
9358 return NULL_TREE;
9361 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9362 N arguments are passed in the array ARGARRAY. Return a folded
9363 expression or NULL_TREE if no simplification was possible. */
9365 tree
9366 fold_builtin_call_array (location_t loc, tree,
9367 tree fn,
9368 int n,
9369 tree *argarray)
9371 if (TREE_CODE (fn) != ADDR_EXPR)
9372 return NULL_TREE;
9374 tree fndecl = TREE_OPERAND (fn, 0);
9375 if (TREE_CODE (fndecl) == FUNCTION_DECL
9376 && DECL_BUILT_IN (fndecl))
9378 /* If last argument is __builtin_va_arg_pack (), arguments to this
9379 function are not finalized yet. Defer folding until they are. */
9380 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9382 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9383 if (fndecl2
9384 && TREE_CODE (fndecl2) == FUNCTION_DECL
9385 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9386 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9387 return NULL_TREE;
9389 if (avoid_folding_inline_builtin (fndecl))
9390 return NULL_TREE;
9391 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9392 return targetm.fold_builtin (fndecl, n, argarray, false);
9393 else
9394 return fold_builtin_n (loc, fndecl, argarray, n, false);
9397 return NULL_TREE;
9400 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9401 along with N new arguments specified as the "..." parameters. SKIP
9402 is the number of arguments in EXP to be omitted. This function is used
9403 to do varargs-to-varargs transformations. */
9405 static tree
9406 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9408 va_list ap;
9409 tree t;
9411 va_start (ap, n);
9412 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9413 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9414 va_end (ap);
9416 return t;
9419 /* Validate a single argument ARG against a tree code CODE representing
9420 a type. Return true when argument is valid. */
9422 static bool
9423 validate_arg (const_tree arg, enum tree_code code)
9425 if (!arg)
9426 return false;
9427 else if (code == POINTER_TYPE)
9428 return POINTER_TYPE_P (TREE_TYPE (arg));
9429 else if (code == INTEGER_TYPE)
9430 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9431 return code == TREE_CODE (TREE_TYPE (arg));
9434 /* This function validates the types of a function call argument list
9435 against a specified list of tree_codes. If the last specifier is a 0,
9436 that represents an ellipses, otherwise the last specifier must be a
9437 VOID_TYPE.
9439 This is the GIMPLE version of validate_arglist. Eventually we want to
9440 completely convert builtins.c to work from GIMPLEs and the tree based
9441 validate_arglist will then be removed. */
9443 bool
9444 validate_gimple_arglist (const gcall *call, ...)
9446 enum tree_code code;
9447 bool res = 0;
9448 va_list ap;
9449 const_tree arg;
9450 size_t i;
9452 va_start (ap, call);
9453 i = 0;
9457 code = (enum tree_code) va_arg (ap, int);
9458 switch (code)
9460 case 0:
9461 /* This signifies an ellipses, any further arguments are all ok. */
9462 res = true;
9463 goto end;
9464 case VOID_TYPE:
9465 /* This signifies an endlink, if no arguments remain, return
9466 true, otherwise return false. */
9467 res = (i == gimple_call_num_args (call));
9468 goto end;
9469 default:
9470 /* If no parameters remain or the parameter's code does not
9471 match the specified code, return false. Otherwise continue
9472 checking any remaining arguments. */
9473 arg = gimple_call_arg (call, i++);
9474 if (!validate_arg (arg, code))
9475 goto end;
9476 break;
9479 while (1);
9481 /* We need gotos here since we can only have one VA_CLOSE in a
9482 function. */
9483 end: ;
9484 va_end (ap);
9486 return res;
9489 /* Default target-specific builtin expander that does nothing. */
9492 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9493 rtx target ATTRIBUTE_UNUSED,
9494 rtx subtarget ATTRIBUTE_UNUSED,
9495 machine_mode mode ATTRIBUTE_UNUSED,
9496 int ignore ATTRIBUTE_UNUSED)
9498 return NULL_RTX;
9501 /* Returns true is EXP represents data that would potentially reside
9502 in a readonly section. */
9504 bool
9505 readonly_data_expr (tree exp)
9507 STRIP_NOPS (exp);
9509 if (TREE_CODE (exp) != ADDR_EXPR)
9510 return false;
9512 exp = get_base_address (TREE_OPERAND (exp, 0));
9513 if (!exp)
9514 return false;
9516 /* Make sure we call decl_readonly_section only for trees it
9517 can handle (since it returns true for everything it doesn't
9518 understand). */
9519 if (TREE_CODE (exp) == STRING_CST
9520 || TREE_CODE (exp) == CONSTRUCTOR
9521 || (VAR_P (exp) && TREE_STATIC (exp)))
9522 return decl_readonly_section (exp, 0);
9523 else
9524 return false;
9527 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9528 to the call, and TYPE is its return type.
9530 Return NULL_TREE if no simplification was possible, otherwise return the
9531 simplified form of the call as a tree.
9533 The simplified form may be a constant or other expression which
9534 computes the same value, but in a more efficient manner (including
9535 calls to other builtin functions).
9537 The call may contain arguments which need to be evaluated, but
9538 which are not useful to determine the result of the call. In
9539 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9540 COMPOUND_EXPR will be an argument which must be evaluated.
9541 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9542 COMPOUND_EXPR in the chain will contain the tree for the simplified
9543 form of the builtin function call. */
9545 static tree
9546 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
9548 if (!validate_arg (s1, POINTER_TYPE)
9549 || !validate_arg (s2, POINTER_TYPE))
9550 return NULL_TREE;
9551 else
9553 tree fn;
9554 const char *p1, *p2;
9556 p2 = c_getstr (s2);
9557 if (p2 == NULL)
9558 return NULL_TREE;
9560 p1 = c_getstr (s1);
9561 if (p1 != NULL)
9563 const char *r = strpbrk (p1, p2);
9564 tree tem;
9566 if (r == NULL)
9567 return build_int_cst (TREE_TYPE (s1), 0);
9569 /* Return an offset into the constant string argument. */
9570 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9571 return fold_convert_loc (loc, type, tem);
9574 if (p2[0] == '\0')
9575 /* strpbrk(x, "") == NULL.
9576 Evaluate and ignore s1 in case it had side-effects. */
9577 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
9579 if (p2[1] != '\0')
9580 return NULL_TREE; /* Really call strpbrk. */
9582 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9583 if (!fn)
9584 return NULL_TREE;
9586 /* New argument list transforming strpbrk(s1, s2) to
9587 strchr(s1, s2[0]). */
9588 return build_call_expr_loc (loc, fn, 2, s1,
9589 build_int_cst (integer_type_node, p2[0]));
9593 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9594 to the call.
9596 Return NULL_TREE if no simplification was possible, otherwise return the
9597 simplified form of the call as a tree.
9599 The simplified form may be a constant or other expression which
9600 computes the same value, but in a more efficient manner (including
9601 calls to other builtin functions).
9603 The call may contain arguments which need to be evaluated, but
9604 which are not useful to determine the result of the call. In
9605 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9606 COMPOUND_EXPR will be an argument which must be evaluated.
9607 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9608 COMPOUND_EXPR in the chain will contain the tree for the simplified
9609 form of the builtin function call. */
9611 static tree
9612 fold_builtin_strspn (location_t loc, tree s1, tree s2)
9614 if (!validate_arg (s1, POINTER_TYPE)
9615 || !validate_arg (s2, POINTER_TYPE))
9616 return NULL_TREE;
9617 else
9619 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9621 /* If either argument is "", return NULL_TREE. */
9622 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9623 /* Evaluate and ignore both arguments in case either one has
9624 side-effects. */
9625 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9626 s1, s2);
9627 return NULL_TREE;
9631 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9632 to the call.
9634 Return NULL_TREE if no simplification was possible, otherwise return the
9635 simplified form of the call as a tree.
9637 The simplified form may be a constant or other expression which
9638 computes the same value, but in a more efficient manner (including
9639 calls to other builtin functions).
9641 The call may contain arguments which need to be evaluated, but
9642 which are not useful to determine the result of the call. In
9643 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9644 COMPOUND_EXPR will be an argument which must be evaluated.
9645 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9646 COMPOUND_EXPR in the chain will contain the tree for the simplified
9647 form of the builtin function call. */
9649 static tree
9650 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
9652 if (!validate_arg (s1, POINTER_TYPE)
9653 || !validate_arg (s2, POINTER_TYPE))
9654 return NULL_TREE;
9655 else
9657 /* If the first argument is "", return NULL_TREE. */
9658 const char *p1 = c_getstr (s1);
9659 if (p1 && *p1 == '\0')
9661 /* Evaluate and ignore argument s2 in case it has
9662 side-effects. */
9663 return omit_one_operand_loc (loc, size_type_node,
9664 size_zero_node, s2);
9667 /* If the second argument is "", return __builtin_strlen(s1). */
9668 const char *p2 = c_getstr (s2);
9669 if (p2 && *p2 == '\0')
9671 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9673 /* If the replacement _DECL isn't initialized, don't do the
9674 transformation. */
9675 if (!fn)
9676 return NULL_TREE;
9678 return build_call_expr_loc (loc, fn, 1, s1);
9680 return NULL_TREE;
9684 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9685 produced. False otherwise. This is done so that we don't output the error
9686 or warning twice or three times. */
9688 bool
9689 fold_builtin_next_arg (tree exp, bool va_start_p)
9691 tree fntype = TREE_TYPE (current_function_decl);
9692 int nargs = call_expr_nargs (exp);
9693 tree arg;
9694 /* There is good chance the current input_location points inside the
9695 definition of the va_start macro (perhaps on the token for
9696 builtin) in a system header, so warnings will not be emitted.
9697 Use the location in real source code. */
9698 source_location current_location =
9699 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9700 NULL);
9702 if (!stdarg_p (fntype))
9704 error ("%<va_start%> used in function with fixed args");
9705 return true;
9708 if (va_start_p)
9710 if (va_start_p && (nargs != 2))
9712 error ("wrong number of arguments to function %<va_start%>");
9713 return true;
9715 arg = CALL_EXPR_ARG (exp, 1);
9717 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9718 when we checked the arguments and if needed issued a warning. */
9719 else
9721 if (nargs == 0)
9723 /* Evidently an out of date version of <stdarg.h>; can't validate
9724 va_start's second argument, but can still work as intended. */
9725 warning_at (current_location,
9726 OPT_Wvarargs,
9727 "%<__builtin_next_arg%> called without an argument");
9728 return true;
9730 else if (nargs > 1)
9732 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9733 return true;
9735 arg = CALL_EXPR_ARG (exp, 0);
9738 if (TREE_CODE (arg) == SSA_NAME)
9739 arg = SSA_NAME_VAR (arg);
9741 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9742 or __builtin_next_arg (0) the first time we see it, after checking
9743 the arguments and if needed issuing a warning. */
9744 if (!integer_zerop (arg))
9746 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9748 /* Strip off all nops for the sake of the comparison. This
9749 is not quite the same as STRIP_NOPS. It does more.
9750 We must also strip off INDIRECT_EXPR for C++ reference
9751 parameters. */
9752 while (CONVERT_EXPR_P (arg)
9753 || TREE_CODE (arg) == INDIRECT_REF)
9754 arg = TREE_OPERAND (arg, 0);
9755 if (arg != last_parm)
9757 /* FIXME: Sometimes with the tree optimizers we can get the
9758 not the last argument even though the user used the last
9759 argument. We just warn and set the arg to be the last
9760 argument so that we will get wrong-code because of
9761 it. */
9762 warning_at (current_location,
9763 OPT_Wvarargs,
9764 "second parameter of %<va_start%> not last named argument");
9767 /* Undefined by C99 7.15.1.4p4 (va_start):
9768 "If the parameter parmN is declared with the register storage
9769 class, with a function or array type, or with a type that is
9770 not compatible with the type that results after application of
9771 the default argument promotions, the behavior is undefined."
9773 else if (DECL_REGISTER (arg))
9775 warning_at (current_location,
9776 OPT_Wvarargs,
9777 "undefined behavior when second parameter of "
9778 "%<va_start%> is declared with %<register%> storage");
9781 /* We want to verify the second parameter just once before the tree
9782 optimizers are run and then avoid keeping it in the tree,
9783 as otherwise we could warn even for correct code like:
9784 void foo (int i, ...)
9785 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9786 if (va_start_p)
9787 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
9788 else
9789 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
9791 return false;
9795 /* Expand a call EXP to __builtin_object_size. */
9797 static rtx
9798 expand_builtin_object_size (tree exp)
9800 tree ost;
9801 int object_size_type;
9802 tree fndecl = get_callee_fndecl (exp);
9804 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9806 error ("%Kfirst argument of %qD must be a pointer, second integer constant",
9807 exp, fndecl);
9808 expand_builtin_trap ();
9809 return const0_rtx;
9812 ost = CALL_EXPR_ARG (exp, 1);
9813 STRIP_NOPS (ost);
9815 if (TREE_CODE (ost) != INTEGER_CST
9816 || tree_int_cst_sgn (ost) < 0
9817 || compare_tree_int (ost, 3) > 0)
9819 error ("%Klast argument of %qD is not integer constant between 0 and 3",
9820 exp, fndecl);
9821 expand_builtin_trap ();
9822 return const0_rtx;
9825 object_size_type = tree_to_shwi (ost);
9827 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9830 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9831 FCODE is the BUILT_IN_* to use.
9832 Return NULL_RTX if we failed; the caller should emit a normal call,
9833 otherwise try to get the result in TARGET, if convenient (and in
9834 mode MODE if that's convenient). */
9836 static rtx
9837 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
9838 enum built_in_function fcode)
9840 if (!validate_arglist (exp,
9841 POINTER_TYPE,
9842 fcode == BUILT_IN_MEMSET_CHK
9843 ? INTEGER_TYPE : POINTER_TYPE,
9844 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
9845 return NULL_RTX;
9847 tree dest = CALL_EXPR_ARG (exp, 0);
9848 tree src = CALL_EXPR_ARG (exp, 1);
9849 tree len = CALL_EXPR_ARG (exp, 2);
9850 tree size = CALL_EXPR_ARG (exp, 3);
9852 bool sizes_ok = check_access (exp, dest, src, len, /*maxread=*/NULL_TREE,
9853 /*str=*/NULL_TREE, size);
9855 if (!tree_fits_uhwi_p (size))
9856 return NULL_RTX;
9858 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
9860 /* Avoid transforming the checking call to an ordinary one when
9861 an overflow has been detected or when the call couldn't be
9862 validated because the size is not constant. */
9863 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
9864 return NULL_RTX;
9866 tree fn = NULL_TREE;
9867 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9868 mem{cpy,pcpy,move,set} is available. */
9869 switch (fcode)
9871 case BUILT_IN_MEMCPY_CHK:
9872 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
9873 break;
9874 case BUILT_IN_MEMPCPY_CHK:
9875 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
9876 break;
9877 case BUILT_IN_MEMMOVE_CHK:
9878 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9879 break;
9880 case BUILT_IN_MEMSET_CHK:
9881 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9882 break;
9883 default:
9884 break;
9887 if (! fn)
9888 return NULL_RTX;
9890 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9891 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9892 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9893 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9895 else if (fcode == BUILT_IN_MEMSET_CHK)
9896 return NULL_RTX;
9897 else
9899 unsigned int dest_align = get_pointer_alignment (dest);
9901 /* If DEST is not a pointer type, call the normal function. */
9902 if (dest_align == 0)
9903 return NULL_RTX;
9905 /* If SRC and DEST are the same (and not volatile), do nothing. */
9906 if (operand_equal_p (src, dest, 0))
9908 tree expr;
9910 if (fcode != BUILT_IN_MEMPCPY_CHK)
9912 /* Evaluate and ignore LEN in case it has side-effects. */
9913 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9914 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9917 expr = fold_build_pointer_plus (dest, len);
9918 return expand_expr (expr, target, mode, EXPAND_NORMAL);
9921 /* __memmove_chk special case. */
9922 if (fcode == BUILT_IN_MEMMOVE_CHK)
9924 unsigned int src_align = get_pointer_alignment (src);
9926 if (src_align == 0)
9927 return NULL_RTX;
9929 /* If src is categorized for a readonly section we can use
9930 normal __memcpy_chk. */
9931 if (readonly_data_expr (src))
9933 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
9934 if (!fn)
9935 return NULL_RTX;
9936 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
9937 dest, src, len, size);
9938 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9939 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9940 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9943 return NULL_RTX;
9947 /* Emit warning if a buffer overflow is detected at compile time. */
9949 static void
9950 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
9952 /* The source string. */
9953 tree srcstr = NULL_TREE;
9954 /* The size of the destination object. */
9955 tree objsize = NULL_TREE;
9956 /* The string that is being concatenated with (as in __strcat_chk)
9957 or null if it isn't. */
9958 tree catstr = NULL_TREE;
9959 /* The maximum length of the source sequence in a bounded operation
9960 (such as __strncat_chk) or null if the operation isn't bounded
9961 (such as __strcat_chk). */
9962 tree maxread = NULL_TREE;
9963 /* The exact size of the access (such as in __strncpy_chk). */
9964 tree size = NULL_TREE;
9966 switch (fcode)
9968 case BUILT_IN_STRCPY_CHK:
9969 case BUILT_IN_STPCPY_CHK:
9970 srcstr = CALL_EXPR_ARG (exp, 1);
9971 objsize = CALL_EXPR_ARG (exp, 2);
9972 break;
9974 case BUILT_IN_STRCAT_CHK:
9975 /* For __strcat_chk the warning will be emitted only if overflowing
9976 by at least strlen (dest) + 1 bytes. */
9977 catstr = CALL_EXPR_ARG (exp, 0);
9978 srcstr = CALL_EXPR_ARG (exp, 1);
9979 objsize = CALL_EXPR_ARG (exp, 2);
9980 break;
9982 case BUILT_IN_STRNCAT_CHK:
9983 catstr = CALL_EXPR_ARG (exp, 0);
9984 srcstr = CALL_EXPR_ARG (exp, 1);
9985 maxread = CALL_EXPR_ARG (exp, 2);
9986 objsize = CALL_EXPR_ARG (exp, 3);
9987 break;
9989 case BUILT_IN_STRNCPY_CHK:
9990 case BUILT_IN_STPNCPY_CHK:
9991 srcstr = CALL_EXPR_ARG (exp, 1);
9992 size = CALL_EXPR_ARG (exp, 2);
9993 objsize = CALL_EXPR_ARG (exp, 3);
9994 break;
9996 case BUILT_IN_SNPRINTF_CHK:
9997 case BUILT_IN_VSNPRINTF_CHK:
9998 maxread = CALL_EXPR_ARG (exp, 1);
9999 objsize = CALL_EXPR_ARG (exp, 3);
10000 break;
10001 default:
10002 gcc_unreachable ();
10005 if (catstr && maxread)
10007 /* Check __strncat_chk. There is no way to determine the length
10008 of the string to which the source string is being appended so
10009 just warn when the length of the source string is not known. */
10010 check_strncat_sizes (exp, objsize);
10011 return;
10014 /* The destination argument is the first one for all built-ins above. */
10015 tree dst = CALL_EXPR_ARG (exp, 0);
10017 check_access (exp, dst, srcstr, size, maxread, srcstr, objsize);
10020 /* Emit warning if a buffer overflow is detected at compile time
10021 in __sprintf_chk/__vsprintf_chk calls. */
10023 static void
10024 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10026 tree size, len, fmt;
10027 const char *fmt_str;
10028 int nargs = call_expr_nargs (exp);
10030 /* Verify the required arguments in the original call. */
10032 if (nargs < 4)
10033 return;
10034 size = CALL_EXPR_ARG (exp, 2);
10035 fmt = CALL_EXPR_ARG (exp, 3);
10037 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10038 return;
10040 /* Check whether the format is a literal string constant. */
10041 fmt_str = c_getstr (fmt);
10042 if (fmt_str == NULL)
10043 return;
10045 if (!init_target_chars ())
10046 return;
10048 /* If the format doesn't contain % args or %%, we know its size. */
10049 if (strchr (fmt_str, target_percent) == 0)
10050 len = build_int_cstu (size_type_node, strlen (fmt_str));
10051 /* If the format is "%s" and first ... argument is a string literal,
10052 we know it too. */
10053 else if (fcode == BUILT_IN_SPRINTF_CHK
10054 && strcmp (fmt_str, target_percent_s) == 0)
10056 tree arg;
10058 if (nargs < 5)
10059 return;
10060 arg = CALL_EXPR_ARG (exp, 4);
10061 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10062 return;
10064 len = c_strlen (arg, 1);
10065 if (!len || ! tree_fits_uhwi_p (len))
10066 return;
10068 else
10069 return;
10071 /* Add one for the terminating nul. */
10072 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10074 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, /*size=*/NULL_TREE,
10075 /*maxread=*/NULL_TREE, len, size);
10078 /* Emit warning if a free is called with address of a variable. */
10080 static void
10081 maybe_emit_free_warning (tree exp)
10083 tree arg = CALL_EXPR_ARG (exp, 0);
10085 STRIP_NOPS (arg);
10086 if (TREE_CODE (arg) != ADDR_EXPR)
10087 return;
10089 arg = get_base_address (TREE_OPERAND (arg, 0));
10090 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
10091 return;
10093 if (SSA_VAR_P (arg))
10094 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10095 "%Kattempt to free a non-heap object %qD", exp, arg);
10096 else
10097 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10098 "%Kattempt to free a non-heap object", exp);
10101 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10102 if possible. */
10104 static tree
10105 fold_builtin_object_size (tree ptr, tree ost)
10107 unsigned HOST_WIDE_INT bytes;
10108 int object_size_type;
10110 if (!validate_arg (ptr, POINTER_TYPE)
10111 || !validate_arg (ost, INTEGER_TYPE))
10112 return NULL_TREE;
10114 STRIP_NOPS (ost);
10116 if (TREE_CODE (ost) != INTEGER_CST
10117 || tree_int_cst_sgn (ost) < 0
10118 || compare_tree_int (ost, 3) > 0)
10119 return NULL_TREE;
10121 object_size_type = tree_to_shwi (ost);
10123 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10124 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10125 and (size_t) 0 for types 2 and 3. */
10126 if (TREE_SIDE_EFFECTS (ptr))
10127 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10129 if (TREE_CODE (ptr) == ADDR_EXPR)
10131 compute_builtin_object_size (ptr, object_size_type, &bytes);
10132 if (wi::fits_to_tree_p (bytes, size_type_node))
10133 return build_int_cstu (size_type_node, bytes);
10135 else if (TREE_CODE (ptr) == SSA_NAME)
10137 /* If object size is not known yet, delay folding until
10138 later. Maybe subsequent passes will help determining
10139 it. */
10140 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10141 && wi::fits_to_tree_p (bytes, size_type_node))
10142 return build_int_cstu (size_type_node, bytes);
10145 return NULL_TREE;
10148 /* Builtins with folding operations that operate on "..." arguments
10149 need special handling; we need to store the arguments in a convenient
10150 data structure before attempting any folding. Fortunately there are
10151 only a few builtins that fall into this category. FNDECL is the
10152 function, EXP is the CALL_EXPR for the call. */
10154 static tree
10155 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10157 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10158 tree ret = NULL_TREE;
10160 switch (fcode)
10162 case BUILT_IN_FPCLASSIFY:
10163 ret = fold_builtin_fpclassify (loc, args, nargs);
10164 break;
10166 default:
10167 break;
10169 if (ret)
10171 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10172 SET_EXPR_LOCATION (ret, loc);
10173 TREE_NO_WARNING (ret) = 1;
10174 return ret;
10176 return NULL_TREE;
10179 /* Initialize format string characters in the target charset. */
10181 bool
10182 init_target_chars (void)
10184 static bool init;
10185 if (!init)
10187 target_newline = lang_hooks.to_target_charset ('\n');
10188 target_percent = lang_hooks.to_target_charset ('%');
10189 target_c = lang_hooks.to_target_charset ('c');
10190 target_s = lang_hooks.to_target_charset ('s');
10191 if (target_newline == 0 || target_percent == 0 || target_c == 0
10192 || target_s == 0)
10193 return false;
10195 target_percent_c[0] = target_percent;
10196 target_percent_c[1] = target_c;
10197 target_percent_c[2] = '\0';
10199 target_percent_s[0] = target_percent;
10200 target_percent_s[1] = target_s;
10201 target_percent_s[2] = '\0';
10203 target_percent_s_newline[0] = target_percent;
10204 target_percent_s_newline[1] = target_s;
10205 target_percent_s_newline[2] = target_newline;
10206 target_percent_s_newline[3] = '\0';
10208 init = true;
10210 return true;
10213 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10214 and no overflow/underflow occurred. INEXACT is true if M was not
10215 exactly calculated. TYPE is the tree type for the result. This
10216 function assumes that you cleared the MPFR flags and then
10217 calculated M to see if anything subsequently set a flag prior to
10218 entering this function. Return NULL_TREE if any checks fail. */
10220 static tree
10221 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10223 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10224 overflow/underflow occurred. If -frounding-math, proceed iff the
10225 result of calling FUNC was exact. */
10226 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10227 && (!flag_rounding_math || !inexact))
10229 REAL_VALUE_TYPE rr;
10231 real_from_mpfr (&rr, m, type, GMP_RNDN);
10232 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10233 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10234 but the mpft_t is not, then we underflowed in the
10235 conversion. */
10236 if (real_isfinite (&rr)
10237 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10239 REAL_VALUE_TYPE rmode;
10241 real_convert (&rmode, TYPE_MODE (type), &rr);
10242 /* Proceed iff the specified mode can hold the value. */
10243 if (real_identical (&rmode, &rr))
10244 return build_real (type, rmode);
10247 return NULL_TREE;
10250 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10251 number and no overflow/underflow occurred. INEXACT is true if M
10252 was not exactly calculated. TYPE is the tree type for the result.
10253 This function assumes that you cleared the MPFR flags and then
10254 calculated M to see if anything subsequently set a flag prior to
10255 entering this function. Return NULL_TREE if any checks fail, if
10256 FORCE_CONVERT is true, then bypass the checks. */
10258 static tree
10259 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10261 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10262 overflow/underflow occurred. If -frounding-math, proceed iff the
10263 result of calling FUNC was exact. */
10264 if (force_convert
10265 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10266 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10267 && (!flag_rounding_math || !inexact)))
10269 REAL_VALUE_TYPE re, im;
10271 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
10272 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
10273 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10274 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10275 but the mpft_t is not, then we underflowed in the
10276 conversion. */
10277 if (force_convert
10278 || (real_isfinite (&re) && real_isfinite (&im)
10279 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10280 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10282 REAL_VALUE_TYPE re_mode, im_mode;
10284 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10285 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10286 /* Proceed iff the specified mode can hold the value. */
10287 if (force_convert
10288 || (real_identical (&re_mode, &re)
10289 && real_identical (&im_mode, &im)))
10290 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10291 build_real (TREE_TYPE (type), im_mode));
10294 return NULL_TREE;
10297 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10298 the pointer *(ARG_QUO) and return the result. The type is taken
10299 from the type of ARG0 and is used for setting the precision of the
10300 calculation and results. */
10302 static tree
10303 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10305 tree const type = TREE_TYPE (arg0);
10306 tree result = NULL_TREE;
10308 STRIP_NOPS (arg0);
10309 STRIP_NOPS (arg1);
10311 /* To proceed, MPFR must exactly represent the target floating point
10312 format, which only happens when the target base equals two. */
10313 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10314 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10315 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10317 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10318 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10320 if (real_isfinite (ra0) && real_isfinite (ra1))
10322 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10323 const int prec = fmt->p;
10324 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10325 tree result_rem;
10326 long integer_quo;
10327 mpfr_t m0, m1;
10329 mpfr_inits2 (prec, m0, m1, NULL);
10330 mpfr_from_real (m0, ra0, GMP_RNDN);
10331 mpfr_from_real (m1, ra1, GMP_RNDN);
10332 mpfr_clear_flags ();
10333 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10334 /* Remquo is independent of the rounding mode, so pass
10335 inexact=0 to do_mpfr_ckconv(). */
10336 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10337 mpfr_clears (m0, m1, NULL);
10338 if (result_rem)
10340 /* MPFR calculates quo in the host's long so it may
10341 return more bits in quo than the target int can hold
10342 if sizeof(host long) > sizeof(target int). This can
10343 happen even for native compilers in LP64 mode. In
10344 these cases, modulo the quo value with the largest
10345 number that the target int can hold while leaving one
10346 bit for the sign. */
10347 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10348 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10350 /* Dereference the quo pointer argument. */
10351 arg_quo = build_fold_indirect_ref (arg_quo);
10352 /* Proceed iff a valid pointer type was passed in. */
10353 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10355 /* Set the value. */
10356 tree result_quo
10357 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10358 build_int_cst (TREE_TYPE (arg_quo),
10359 integer_quo));
10360 TREE_SIDE_EFFECTS (result_quo) = 1;
10361 /* Combine the quo assignment with the rem. */
10362 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10363 result_quo, result_rem));
10368 return result;
10371 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10372 resulting value as a tree with type TYPE. The mpfr precision is
10373 set to the precision of TYPE. We assume that this mpfr function
10374 returns zero if the result could be calculated exactly within the
10375 requested precision. In addition, the integer pointer represented
10376 by ARG_SG will be dereferenced and set to the appropriate signgam
10377 (-1,1) value. */
10379 static tree
10380 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10382 tree result = NULL_TREE;
10384 STRIP_NOPS (arg);
10386 /* To proceed, MPFR must exactly represent the target floating point
10387 format, which only happens when the target base equals two. Also
10388 verify ARG is a constant and that ARG_SG is an int pointer. */
10389 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10390 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10391 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10392 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10394 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10396 /* In addition to NaN and Inf, the argument cannot be zero or a
10397 negative integer. */
10398 if (real_isfinite (ra)
10399 && ra->cl != rvc_zero
10400 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10402 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10403 const int prec = fmt->p;
10404 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10405 int inexact, sg;
10406 mpfr_t m;
10407 tree result_lg;
10409 mpfr_init2 (m, prec);
10410 mpfr_from_real (m, ra, GMP_RNDN);
10411 mpfr_clear_flags ();
10412 inexact = mpfr_lgamma (m, &sg, m, rnd);
10413 result_lg = do_mpfr_ckconv (m, type, inexact);
10414 mpfr_clear (m);
10415 if (result_lg)
10417 tree result_sg;
10419 /* Dereference the arg_sg pointer argument. */
10420 arg_sg = build_fold_indirect_ref (arg_sg);
10421 /* Assign the signgam value into *arg_sg. */
10422 result_sg = fold_build2 (MODIFY_EXPR,
10423 TREE_TYPE (arg_sg), arg_sg,
10424 build_int_cst (TREE_TYPE (arg_sg), sg));
10425 TREE_SIDE_EFFECTS (result_sg) = 1;
10426 /* Combine the signgam assignment with the lgamma result. */
10427 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10428 result_sg, result_lg));
10433 return result;
10436 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10437 mpc function FUNC on it and return the resulting value as a tree
10438 with type TYPE. The mpfr precision is set to the precision of
10439 TYPE. We assume that function FUNC returns zero if the result
10440 could be calculated exactly within the requested precision. If
10441 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10442 in the arguments and/or results. */
10444 tree
10445 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10446 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10448 tree result = NULL_TREE;
10450 STRIP_NOPS (arg0);
10451 STRIP_NOPS (arg1);
10453 /* To proceed, MPFR must exactly represent the target floating point
10454 format, which only happens when the target base equals two. */
10455 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10456 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10457 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10458 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10459 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10461 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10462 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10463 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10464 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10466 if (do_nonfinite
10467 || (real_isfinite (re0) && real_isfinite (im0)
10468 && real_isfinite (re1) && real_isfinite (im1)))
10470 const struct real_format *const fmt =
10471 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10472 const int prec = fmt->p;
10473 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
10474 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10475 int inexact;
10476 mpc_t m0, m1;
10478 mpc_init2 (m0, prec);
10479 mpc_init2 (m1, prec);
10480 mpfr_from_real (mpc_realref (m0), re0, rnd);
10481 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10482 mpfr_from_real (mpc_realref (m1), re1, rnd);
10483 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10484 mpfr_clear_flags ();
10485 inexact = func (m0, m0, m1, crnd);
10486 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10487 mpc_clear (m0);
10488 mpc_clear (m1);
10492 return result;
10495 /* A wrapper function for builtin folding that prevents warnings for
10496 "statement without effect" and the like, caused by removing the
10497 call node earlier than the warning is generated. */
10499 tree
10500 fold_call_stmt (gcall *stmt, bool ignore)
10502 tree ret = NULL_TREE;
10503 tree fndecl = gimple_call_fndecl (stmt);
10504 location_t loc = gimple_location (stmt);
10505 if (fndecl
10506 && TREE_CODE (fndecl) == FUNCTION_DECL
10507 && DECL_BUILT_IN (fndecl)
10508 && !gimple_call_va_arg_pack_p (stmt))
10510 int nargs = gimple_call_num_args (stmt);
10511 tree *args = (nargs > 0
10512 ? gimple_call_arg_ptr (stmt, 0)
10513 : &error_mark_node);
10515 if (avoid_folding_inline_builtin (fndecl))
10516 return NULL_TREE;
10517 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10519 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10521 else
10523 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10524 if (ret)
10526 /* Propagate location information from original call to
10527 expansion of builtin. Otherwise things like
10528 maybe_emit_chk_warning, that operate on the expansion
10529 of a builtin, will use the wrong location information. */
10530 if (gimple_has_location (stmt))
10532 tree realret = ret;
10533 if (TREE_CODE (ret) == NOP_EXPR)
10534 realret = TREE_OPERAND (ret, 0);
10535 if (CAN_HAVE_LOCATION_P (realret)
10536 && !EXPR_HAS_LOCATION (realret))
10537 SET_EXPR_LOCATION (realret, loc);
10538 return realret;
10540 return ret;
10544 return NULL_TREE;
10547 /* Look up the function in builtin_decl that corresponds to DECL
10548 and set ASMSPEC as its user assembler name. DECL must be a
10549 function decl that declares a builtin. */
10551 void
10552 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10554 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
10555 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
10556 && asmspec != 0);
10558 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10559 set_user_assembler_name (builtin, asmspec);
10561 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10562 && INT_TYPE_SIZE < BITS_PER_WORD)
10564 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
10565 set_user_assembler_libfunc ("ffs", asmspec);
10566 set_optab_libfunc (ffs_optab, mode, "ffs");
10570 /* Return true if DECL is a builtin that expands to a constant or similarly
10571 simple code. */
10572 bool
10573 is_simple_builtin (tree decl)
10575 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10576 switch (DECL_FUNCTION_CODE (decl))
10578 /* Builtins that expand to constants. */
10579 case BUILT_IN_CONSTANT_P:
10580 case BUILT_IN_EXPECT:
10581 case BUILT_IN_OBJECT_SIZE:
10582 case BUILT_IN_UNREACHABLE:
10583 /* Simple register moves or loads from stack. */
10584 case BUILT_IN_ASSUME_ALIGNED:
10585 case BUILT_IN_RETURN_ADDRESS:
10586 case BUILT_IN_EXTRACT_RETURN_ADDR:
10587 case BUILT_IN_FROB_RETURN_ADDR:
10588 case BUILT_IN_RETURN:
10589 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10590 case BUILT_IN_FRAME_ADDRESS:
10591 case BUILT_IN_VA_END:
10592 case BUILT_IN_STACK_SAVE:
10593 case BUILT_IN_STACK_RESTORE:
10594 /* Exception state returns or moves registers around. */
10595 case BUILT_IN_EH_FILTER:
10596 case BUILT_IN_EH_POINTER:
10597 case BUILT_IN_EH_COPY_VALUES:
10598 return true;
10600 default:
10601 return false;
10604 return false;
10607 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10608 most probably expanded inline into reasonably simple code. This is a
10609 superset of is_simple_builtin. */
10610 bool
10611 is_inexpensive_builtin (tree decl)
10613 if (!decl)
10614 return false;
10615 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10616 return true;
10617 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10618 switch (DECL_FUNCTION_CODE (decl))
10620 case BUILT_IN_ABS:
10621 CASE_BUILT_IN_ALLOCA:
10622 case BUILT_IN_BSWAP16:
10623 case BUILT_IN_BSWAP32:
10624 case BUILT_IN_BSWAP64:
10625 case BUILT_IN_CLZ:
10626 case BUILT_IN_CLZIMAX:
10627 case BUILT_IN_CLZL:
10628 case BUILT_IN_CLZLL:
10629 case BUILT_IN_CTZ:
10630 case BUILT_IN_CTZIMAX:
10631 case BUILT_IN_CTZL:
10632 case BUILT_IN_CTZLL:
10633 case BUILT_IN_FFS:
10634 case BUILT_IN_FFSIMAX:
10635 case BUILT_IN_FFSL:
10636 case BUILT_IN_FFSLL:
10637 case BUILT_IN_IMAXABS:
10638 case BUILT_IN_FINITE:
10639 case BUILT_IN_FINITEF:
10640 case BUILT_IN_FINITEL:
10641 case BUILT_IN_FINITED32:
10642 case BUILT_IN_FINITED64:
10643 case BUILT_IN_FINITED128:
10644 case BUILT_IN_FPCLASSIFY:
10645 case BUILT_IN_ISFINITE:
10646 case BUILT_IN_ISINF_SIGN:
10647 case BUILT_IN_ISINF:
10648 case BUILT_IN_ISINFF:
10649 case BUILT_IN_ISINFL:
10650 case BUILT_IN_ISINFD32:
10651 case BUILT_IN_ISINFD64:
10652 case BUILT_IN_ISINFD128:
10653 case BUILT_IN_ISNAN:
10654 case BUILT_IN_ISNANF:
10655 case BUILT_IN_ISNANL:
10656 case BUILT_IN_ISNAND32:
10657 case BUILT_IN_ISNAND64:
10658 case BUILT_IN_ISNAND128:
10659 case BUILT_IN_ISNORMAL:
10660 case BUILT_IN_ISGREATER:
10661 case BUILT_IN_ISGREATEREQUAL:
10662 case BUILT_IN_ISLESS:
10663 case BUILT_IN_ISLESSEQUAL:
10664 case BUILT_IN_ISLESSGREATER:
10665 case BUILT_IN_ISUNORDERED:
10666 case BUILT_IN_VA_ARG_PACK:
10667 case BUILT_IN_VA_ARG_PACK_LEN:
10668 case BUILT_IN_VA_COPY:
10669 case BUILT_IN_TRAP:
10670 case BUILT_IN_SAVEREGS:
10671 case BUILT_IN_POPCOUNTL:
10672 case BUILT_IN_POPCOUNTLL:
10673 case BUILT_IN_POPCOUNTIMAX:
10674 case BUILT_IN_POPCOUNT:
10675 case BUILT_IN_PARITYL:
10676 case BUILT_IN_PARITYLL:
10677 case BUILT_IN_PARITYIMAX:
10678 case BUILT_IN_PARITY:
10679 case BUILT_IN_LABS:
10680 case BUILT_IN_LLABS:
10681 case BUILT_IN_PREFETCH:
10682 case BUILT_IN_ACC_ON_DEVICE:
10683 return true;
10685 default:
10686 return is_simple_builtin (decl);
10689 return false;
10692 /* Return true if T is a constant and the value cast to a target char
10693 can be represented by a host char.
10694 Store the casted char constant in *P if so. */
10696 bool
10697 target_char_cst_p (tree t, char *p)
10699 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10700 return false;
10702 *p = (char)tree_to_uhwi (t);
10703 return true;
10706 /* Return the maximum object size. */
10708 tree
10709 max_object_size (void)
10711 /* To do: Make this a configurable parameter. */
10712 return TYPE_MAX_VALUE (ptrdiff_type_node);