Daily bump.
[official-gcc.git] / gcc / builtins.c
blob0097d5bae97dcb480b831b537f92eff9524bb18a
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() */
74 #include "gomp-constants.h"
75 #include "omp-general.h"
77 struct target_builtins default_target_builtins;
78 #if SWITCHABLE_TARGET
79 struct target_builtins *this_target_builtins = &default_target_builtins;
80 #endif
82 /* Define the names of the builtin function types and codes. */
83 const char *const built_in_class_names[BUILT_IN_LAST]
84 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
86 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
87 const char * built_in_names[(int) END_BUILTINS] =
89 #include "builtins.def"
92 /* Setup an array of builtin_info_type, make sure each element decl is
93 initialized to NULL_TREE. */
94 builtin_info_type builtin_info[(int)END_BUILTINS];
96 /* Non-zero if __builtin_constant_p should be folded right away. */
97 bool force_folding_builtin_constant_p;
99 static rtx c_readstr (const char *, scalar_int_mode);
100 static int target_char_cast (tree, char *);
101 static rtx get_memory_rtx (tree, tree);
102 static int apply_args_size (void);
103 static int apply_result_size (void);
104 static rtx result_vector (int, rtx);
105 static void expand_builtin_prefetch (tree);
106 static rtx expand_builtin_apply_args (void);
107 static rtx expand_builtin_apply_args_1 (void);
108 static rtx expand_builtin_apply (rtx, rtx, rtx);
109 static void expand_builtin_return (rtx);
110 static enum type_class type_to_class (tree);
111 static rtx expand_builtin_classify_type (tree);
112 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
113 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
114 static rtx expand_builtin_interclass_mathfn (tree, rtx);
115 static rtx expand_builtin_sincos (tree);
116 static rtx expand_builtin_cexpi (tree, rtx);
117 static rtx expand_builtin_int_roundingfn (tree, rtx);
118 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
119 static rtx expand_builtin_next_arg (void);
120 static rtx expand_builtin_va_start (tree);
121 static rtx expand_builtin_va_end (tree);
122 static rtx expand_builtin_va_copy (tree);
123 static rtx expand_builtin_strcmp (tree, rtx);
124 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
125 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, scalar_int_mode);
126 static rtx expand_builtin_memchr (tree, rtx);
127 static rtx expand_builtin_memcpy (tree, rtx);
128 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
129 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
130 rtx target, tree exp, int endp);
131 static rtx expand_builtin_memmove (tree, rtx);
132 static rtx expand_builtin_mempcpy (tree, rtx);
133 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx);
134 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, int);
135 static rtx expand_builtin_strcat (tree, rtx);
136 static rtx expand_builtin_strcpy (tree, rtx);
137 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
138 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
139 static rtx expand_builtin_stpncpy (tree, rtx);
140 static rtx expand_builtin_strncat (tree, rtx);
141 static rtx expand_builtin_strncpy (tree, rtx);
142 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, scalar_int_mode);
143 static rtx expand_builtin_memset (tree, rtx, machine_mode);
144 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
145 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
146 static rtx expand_builtin_bzero (tree);
147 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
148 static rtx expand_builtin_alloca (tree);
149 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
150 static rtx expand_builtin_frame_address (tree, tree);
151 static tree stabilize_va_list_loc (location_t, tree, int);
152 static rtx expand_builtin_expect (tree, rtx);
153 static tree fold_builtin_constant_p (tree);
154 static tree fold_builtin_classify_type (tree);
155 static tree fold_builtin_strlen (location_t, tree, tree);
156 static tree fold_builtin_inf (location_t, tree, int);
157 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
158 static bool validate_arg (const_tree, enum tree_code code);
159 static rtx expand_builtin_fabs (tree, rtx, rtx);
160 static rtx expand_builtin_signbit (tree, rtx);
161 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
162 static tree fold_builtin_isascii (location_t, tree);
163 static tree fold_builtin_toascii (location_t, tree);
164 static tree fold_builtin_isdigit (location_t, tree);
165 static tree fold_builtin_fabs (location_t, tree, tree);
166 static tree fold_builtin_abs (location_t, tree, tree);
167 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
168 enum tree_code);
169 static tree fold_builtin_0 (location_t, tree);
170 static tree fold_builtin_1 (location_t, tree, tree);
171 static tree fold_builtin_2 (location_t, tree, tree, tree);
172 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
173 static tree fold_builtin_varargs (location_t, tree, tree*, int);
175 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
176 static tree fold_builtin_strspn (location_t, tree, tree);
177 static tree fold_builtin_strcspn (location_t, tree, tree);
179 static rtx expand_builtin_object_size (tree);
180 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
181 enum built_in_function);
182 static void maybe_emit_chk_warning (tree, enum built_in_function);
183 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
184 static void maybe_emit_free_warning (tree);
185 static tree fold_builtin_object_size (tree, tree);
187 unsigned HOST_WIDE_INT target_newline;
188 unsigned HOST_WIDE_INT target_percent;
189 static unsigned HOST_WIDE_INT target_c;
190 static unsigned HOST_WIDE_INT target_s;
191 char target_percent_c[3];
192 char target_percent_s[3];
193 char target_percent_s_newline[4];
194 static tree do_mpfr_remquo (tree, tree, tree);
195 static tree do_mpfr_lgamma_r (tree, tree, tree);
196 static void expand_builtin_sync_synchronize (void);
198 /* Return true if NAME starts with __builtin_ or __sync_. */
200 static bool
201 is_builtin_name (const char *name)
203 if (strncmp (name, "__builtin_", 10) == 0)
204 return true;
205 if (strncmp (name, "__sync_", 7) == 0)
206 return true;
207 if (strncmp (name, "__atomic_", 9) == 0)
208 return true;
209 return false;
213 /* Return true if DECL is a function symbol representing a built-in. */
215 bool
216 is_builtin_fn (tree decl)
218 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
221 /* Return true if NODE should be considered for inline expansion regardless
222 of the optimization level. This means whenever a function is invoked with
223 its "internal" name, which normally contains the prefix "__builtin". */
225 bool
226 called_as_built_in (tree node)
228 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
229 we want the name used to call the function, not the name it
230 will have. */
231 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
232 return is_builtin_name (name);
235 /* Compute values M and N such that M divides (address of EXP - N) and such
236 that N < M. If these numbers can be determined, store M in alignp and N in
237 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
238 *alignp and any bit-offset to *bitposp.
240 Note that the address (and thus the alignment) computed here is based
241 on the address to which a symbol resolves, whereas DECL_ALIGN is based
242 on the address at which an object is actually located. These two
243 addresses are not always the same. For example, on ARM targets,
244 the address &foo of a Thumb function foo() has the lowest bit set,
245 whereas foo() itself starts on an even address.
247 If ADDR_P is true we are taking the address of the memory reference EXP
248 and thus cannot rely on the access taking place. */
250 static bool
251 get_object_alignment_2 (tree exp, unsigned int *alignp,
252 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
254 poly_int64 bitsize, bitpos;
255 tree offset;
256 machine_mode mode;
257 int unsignedp, reversep, volatilep;
258 unsigned int align = BITS_PER_UNIT;
259 bool known_alignment = false;
261 /* Get the innermost object and the constant (bitpos) and possibly
262 variable (offset) offset of the access. */
263 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
264 &unsignedp, &reversep, &volatilep);
266 /* Extract alignment information from the innermost object and
267 possibly adjust bitpos and offset. */
268 if (TREE_CODE (exp) == FUNCTION_DECL)
270 /* Function addresses can encode extra information besides their
271 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
272 allows the low bit to be used as a virtual bit, we know
273 that the address itself must be at least 2-byte aligned. */
274 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
275 align = 2 * BITS_PER_UNIT;
277 else if (TREE_CODE (exp) == LABEL_DECL)
279 else if (TREE_CODE (exp) == CONST_DECL)
281 /* The alignment of a CONST_DECL is determined by its initializer. */
282 exp = DECL_INITIAL (exp);
283 align = TYPE_ALIGN (TREE_TYPE (exp));
284 if (CONSTANT_CLASS_P (exp))
285 align = targetm.constant_alignment (exp, align);
287 known_alignment = true;
289 else if (DECL_P (exp))
291 align = DECL_ALIGN (exp);
292 known_alignment = true;
294 else if (TREE_CODE (exp) == INDIRECT_REF
295 || TREE_CODE (exp) == MEM_REF
296 || TREE_CODE (exp) == TARGET_MEM_REF)
298 tree addr = TREE_OPERAND (exp, 0);
299 unsigned ptr_align;
300 unsigned HOST_WIDE_INT ptr_bitpos;
301 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
303 /* If the address is explicitely aligned, handle that. */
304 if (TREE_CODE (addr) == BIT_AND_EXPR
305 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
307 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
308 ptr_bitmask *= BITS_PER_UNIT;
309 align = least_bit_hwi (ptr_bitmask);
310 addr = TREE_OPERAND (addr, 0);
313 known_alignment
314 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
315 align = MAX (ptr_align, align);
317 /* Re-apply explicit alignment to the bitpos. */
318 ptr_bitpos &= ptr_bitmask;
320 /* The alignment of the pointer operand in a TARGET_MEM_REF
321 has to take the variable offset parts into account. */
322 if (TREE_CODE (exp) == TARGET_MEM_REF)
324 if (TMR_INDEX (exp))
326 unsigned HOST_WIDE_INT step = 1;
327 if (TMR_STEP (exp))
328 step = TREE_INT_CST_LOW (TMR_STEP (exp));
329 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
331 if (TMR_INDEX2 (exp))
332 align = BITS_PER_UNIT;
333 known_alignment = false;
336 /* When EXP is an actual memory reference then we can use
337 TYPE_ALIGN of a pointer indirection to derive alignment.
338 Do so only if get_pointer_alignment_1 did not reveal absolute
339 alignment knowledge and if using that alignment would
340 improve the situation. */
341 unsigned int talign;
342 if (!addr_p && !known_alignment
343 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
344 && talign > align)
345 align = talign;
346 else
348 /* Else adjust bitpos accordingly. */
349 bitpos += ptr_bitpos;
350 if (TREE_CODE (exp) == MEM_REF
351 || TREE_CODE (exp) == TARGET_MEM_REF)
352 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
355 else if (TREE_CODE (exp) == STRING_CST)
357 /* STRING_CST are the only constant objects we allow to be not
358 wrapped inside a CONST_DECL. */
359 align = TYPE_ALIGN (TREE_TYPE (exp));
360 if (CONSTANT_CLASS_P (exp))
361 align = targetm.constant_alignment (exp, align);
363 known_alignment = true;
366 /* If there is a non-constant offset part extract the maximum
367 alignment that can prevail. */
368 if (offset)
370 unsigned int trailing_zeros = tree_ctz (offset);
371 if (trailing_zeros < HOST_BITS_PER_INT)
373 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
374 if (inner)
375 align = MIN (align, inner);
379 /* Account for the alignment of runtime coefficients, so that the constant
380 bitpos is guaranteed to be accurate. */
381 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
382 if (alt_align != 0 && alt_align < align)
384 align = alt_align;
385 known_alignment = false;
388 *alignp = align;
389 *bitposp = bitpos.coeffs[0] & (align - 1);
390 return known_alignment;
393 /* For a memory reference expression EXP compute values M and N such that M
394 divides (&EXP - N) and such that N < M. If these numbers can be determined,
395 store M in alignp and N in *BITPOSP and return true. Otherwise return false
396 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
398 bool
399 get_object_alignment_1 (tree exp, unsigned int *alignp,
400 unsigned HOST_WIDE_INT *bitposp)
402 return get_object_alignment_2 (exp, alignp, bitposp, false);
405 /* Return the alignment in bits of EXP, an object. */
407 unsigned int
408 get_object_alignment (tree exp)
410 unsigned HOST_WIDE_INT bitpos = 0;
411 unsigned int align;
413 get_object_alignment_1 (exp, &align, &bitpos);
415 /* align and bitpos now specify known low bits of the pointer.
416 ptr & (align - 1) == bitpos. */
418 if (bitpos != 0)
419 align = least_bit_hwi (bitpos);
420 return align;
423 /* For a pointer valued expression EXP compute values M and N such that M
424 divides (EXP - N) and such that N < M. If these numbers can be determined,
425 store M in alignp and N in *BITPOSP and return true. Return false if
426 the results are just a conservative approximation.
428 If EXP is not a pointer, false is returned too. */
430 bool
431 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
432 unsigned HOST_WIDE_INT *bitposp)
434 STRIP_NOPS (exp);
436 if (TREE_CODE (exp) == ADDR_EXPR)
437 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
438 alignp, bitposp, true);
439 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
441 unsigned int align;
442 unsigned HOST_WIDE_INT bitpos;
443 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
444 &align, &bitpos);
445 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
446 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
447 else
449 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
450 if (trailing_zeros < HOST_BITS_PER_INT)
452 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
453 if (inner)
454 align = MIN (align, inner);
457 *alignp = align;
458 *bitposp = bitpos & (align - 1);
459 return res;
461 else if (TREE_CODE (exp) == SSA_NAME
462 && POINTER_TYPE_P (TREE_TYPE (exp)))
464 unsigned int ptr_align, ptr_misalign;
465 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
467 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
469 *bitposp = ptr_misalign * BITS_PER_UNIT;
470 *alignp = ptr_align * BITS_PER_UNIT;
471 /* Make sure to return a sensible alignment when the multiplication
472 by BITS_PER_UNIT overflowed. */
473 if (*alignp == 0)
474 *alignp = 1u << (HOST_BITS_PER_INT - 1);
475 /* We cannot really tell whether this result is an approximation. */
476 return false;
478 else
480 *bitposp = 0;
481 *alignp = BITS_PER_UNIT;
482 return false;
485 else if (TREE_CODE (exp) == INTEGER_CST)
487 *alignp = BIGGEST_ALIGNMENT;
488 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
489 & (BIGGEST_ALIGNMENT - 1));
490 return true;
493 *bitposp = 0;
494 *alignp = BITS_PER_UNIT;
495 return false;
498 /* Return the alignment in bits of EXP, a pointer valued expression.
499 The alignment returned is, by default, the alignment of the thing that
500 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
502 Otherwise, look at the expression to see if we can do better, i.e., if the
503 expression is actually pointing at an object whose alignment is tighter. */
505 unsigned int
506 get_pointer_alignment (tree exp)
508 unsigned HOST_WIDE_INT bitpos = 0;
509 unsigned int align;
511 get_pointer_alignment_1 (exp, &align, &bitpos);
513 /* align and bitpos now specify known low bits of the pointer.
514 ptr & (align - 1) == bitpos. */
516 if (bitpos != 0)
517 align = least_bit_hwi (bitpos);
519 return align;
522 /* Return the number of non-zero elements in the sequence
523 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
524 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
526 static unsigned
527 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
529 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
531 unsigned n;
533 if (eltsize == 1)
535 /* Optimize the common case of plain char. */
536 for (n = 0; n < maxelts; n++)
538 const char *elt = (const char*) ptr + n;
539 if (!*elt)
540 break;
543 else
545 for (n = 0; n < maxelts; n++)
547 const char *elt = (const char*) ptr + n * eltsize;
548 if (!memcmp (elt, "\0\0\0\0", eltsize))
549 break;
552 return n;
555 /* Compute the length of a null-terminated character string or wide
556 character string handling character sizes of 1, 2, and 4 bytes.
557 TREE_STRING_LENGTH is not the right way because it evaluates to
558 the size of the character array in bytes (as opposed to characters)
559 and because it can contain a zero byte in the middle.
561 ONLY_VALUE should be nonzero if the result is not going to be emitted
562 into the instruction stream and zero if it is going to be expanded.
563 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
564 is returned, otherwise NULL, since
565 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
566 evaluate the side-effects.
568 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
569 accesses. Note that this implies the result is not going to be emitted
570 into the instruction stream.
572 The value returned is of type `ssizetype'.
574 Unfortunately, string_constant can't access the values of const char
575 arrays with initializers, so neither can we do so here. */
577 tree
578 c_strlen (tree src, int only_value)
580 STRIP_NOPS (src);
581 if (TREE_CODE (src) == COND_EXPR
582 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
584 tree len1, len2;
586 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
587 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
588 if (tree_int_cst_equal (len1, len2))
589 return len1;
592 if (TREE_CODE (src) == COMPOUND_EXPR
593 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
594 return c_strlen (TREE_OPERAND (src, 1), only_value);
596 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
598 /* Offset from the beginning of the string in bytes. */
599 tree byteoff;
600 src = string_constant (src, &byteoff);
601 if (src == 0)
602 return NULL_TREE;
604 /* Determine the size of the string element. */
605 unsigned eltsize
606 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))));
608 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
609 length of SRC. */
610 unsigned maxelts = TREE_STRING_LENGTH (src) / eltsize - 1;
612 /* PTR can point to the byte representation of any string type, including
613 char* and wchar_t*. */
614 const char *ptr = TREE_STRING_POINTER (src);
616 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
618 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
619 compute the offset to the following null if we don't know where to
620 start searching for it. */
621 if (string_length (ptr, eltsize, maxelts) < maxelts)
623 /* Return when an embedded null character is found. */
624 return NULL_TREE;
627 if (!maxelts)
628 return ssize_int (0);
630 /* We don't know the starting offset, but we do know that the string
631 has no internal zero bytes. We can assume that the offset falls
632 within the bounds of the string; otherwise, the programmer deserves
633 what he gets. Subtract the offset from the length of the string,
634 and return that. This would perhaps not be valid if we were dealing
635 with named arrays in addition to literal string constants. */
637 return size_diffop_loc (loc, size_int (maxelts * eltsize), byteoff);
640 /* Offset from the beginning of the string in elements. */
641 HOST_WIDE_INT eltoff;
643 /* We have a known offset into the string. Start searching there for
644 a null character if we can represent it as a single HOST_WIDE_INT. */
645 if (byteoff == 0)
646 eltoff = 0;
647 else if (! tree_fits_shwi_p (byteoff))
648 eltoff = -1;
649 else
650 eltoff = tree_to_shwi (byteoff) / eltsize;
652 /* If the offset is known to be out of bounds, warn, and call strlen at
653 runtime. */
654 if (eltoff < 0 || eltoff > maxelts)
656 /* Suppress multiple warnings for propagated constant strings. */
657 if (only_value != 2
658 && !TREE_NO_WARNING (src))
660 warning_at (loc, OPT_Warray_bounds,
661 "offset %qwi outside bounds of constant string",
662 eltoff);
663 TREE_NO_WARNING (src) = 1;
665 return NULL_TREE;
668 /* Use strlen to search for the first zero byte. Since any strings
669 constructed with build_string will have nulls appended, we win even
670 if we get handed something like (char[4])"abcd".
672 Since ELTOFF is our starting index into the string, no further
673 calculation is needed. */
674 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
675 maxelts - eltoff);
677 return ssize_int (len);
680 /* Return a constant integer corresponding to target reading
681 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
683 static rtx
684 c_readstr (const char *str, scalar_int_mode mode)
686 HOST_WIDE_INT ch;
687 unsigned int i, j;
688 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
690 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
691 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
692 / HOST_BITS_PER_WIDE_INT;
694 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
695 for (i = 0; i < len; i++)
696 tmp[i] = 0;
698 ch = 1;
699 for (i = 0; i < GET_MODE_SIZE (mode); i++)
701 j = i;
702 if (WORDS_BIG_ENDIAN)
703 j = GET_MODE_SIZE (mode) - i - 1;
704 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
705 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
706 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
707 j *= BITS_PER_UNIT;
709 if (ch)
710 ch = (unsigned char) str[i];
711 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
714 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
715 return immed_wide_int_const (c, mode);
718 /* Cast a target constant CST to target CHAR and if that value fits into
719 host char type, return zero and put that value into variable pointed to by
720 P. */
722 static int
723 target_char_cast (tree cst, char *p)
725 unsigned HOST_WIDE_INT val, hostval;
727 if (TREE_CODE (cst) != INTEGER_CST
728 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
729 return 1;
731 /* Do not care if it fits or not right here. */
732 val = TREE_INT_CST_LOW (cst);
734 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
735 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
737 hostval = val;
738 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
739 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
741 if (val != hostval)
742 return 1;
744 *p = hostval;
745 return 0;
748 /* Similar to save_expr, but assumes that arbitrary code is not executed
749 in between the multiple evaluations. In particular, we assume that a
750 non-addressable local variable will not be modified. */
752 static tree
753 builtin_save_expr (tree exp)
755 if (TREE_CODE (exp) == SSA_NAME
756 || (TREE_ADDRESSABLE (exp) == 0
757 && (TREE_CODE (exp) == PARM_DECL
758 || (VAR_P (exp) && !TREE_STATIC (exp)))))
759 return exp;
761 return save_expr (exp);
764 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
765 times to get the address of either a higher stack frame, or a return
766 address located within it (depending on FNDECL_CODE). */
768 static rtx
769 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
771 int i;
772 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
773 if (tem == NULL_RTX)
775 /* For a zero count with __builtin_return_address, we don't care what
776 frame address we return, because target-specific definitions will
777 override us. Therefore frame pointer elimination is OK, and using
778 the soft frame pointer is OK.
780 For a nonzero count, or a zero count with __builtin_frame_address,
781 we require a stable offset from the current frame pointer to the
782 previous one, so we must use the hard frame pointer, and
783 we must disable frame pointer elimination. */
784 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
785 tem = frame_pointer_rtx;
786 else
788 tem = hard_frame_pointer_rtx;
790 /* Tell reload not to eliminate the frame pointer. */
791 crtl->accesses_prior_frames = 1;
795 if (count > 0)
796 SETUP_FRAME_ADDRESSES ();
798 /* On the SPARC, the return address is not in the frame, it is in a
799 register. There is no way to access it off of the current frame
800 pointer, but it can be accessed off the previous frame pointer by
801 reading the value from the register window save area. */
802 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
803 count--;
805 /* Scan back COUNT frames to the specified frame. */
806 for (i = 0; i < count; i++)
808 /* Assume the dynamic chain pointer is in the word that the
809 frame address points to, unless otherwise specified. */
810 tem = DYNAMIC_CHAIN_ADDRESS (tem);
811 tem = memory_address (Pmode, tem);
812 tem = gen_frame_mem (Pmode, tem);
813 tem = copy_to_reg (tem);
816 /* For __builtin_frame_address, return what we've got. But, on
817 the SPARC for example, we may have to add a bias. */
818 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
819 return FRAME_ADDR_RTX (tem);
821 /* For __builtin_return_address, get the return address from that frame. */
822 #ifdef RETURN_ADDR_RTX
823 tem = RETURN_ADDR_RTX (count, tem);
824 #else
825 tem = memory_address (Pmode,
826 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
827 tem = gen_frame_mem (Pmode, tem);
828 #endif
829 return tem;
832 /* Alias set used for setjmp buffer. */
833 static alias_set_type setjmp_alias_set = -1;
835 /* Construct the leading half of a __builtin_setjmp call. Control will
836 return to RECEIVER_LABEL. This is also called directly by the SJLJ
837 exception handling code. */
839 void
840 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
842 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
843 rtx stack_save;
844 rtx mem;
846 if (setjmp_alias_set == -1)
847 setjmp_alias_set = new_alias_set ();
849 buf_addr = convert_memory_address (Pmode, buf_addr);
851 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
853 /* We store the frame pointer and the address of receiver_label in
854 the buffer and use the rest of it for the stack save area, which
855 is machine-dependent. */
857 mem = gen_rtx_MEM (Pmode, buf_addr);
858 set_mem_alias_set (mem, setjmp_alias_set);
859 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
861 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
862 GET_MODE_SIZE (Pmode))),
863 set_mem_alias_set (mem, setjmp_alias_set);
865 emit_move_insn (validize_mem (mem),
866 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
868 stack_save = gen_rtx_MEM (sa_mode,
869 plus_constant (Pmode, buf_addr,
870 2 * GET_MODE_SIZE (Pmode)));
871 set_mem_alias_set (stack_save, setjmp_alias_set);
872 emit_stack_save (SAVE_NONLOCAL, &stack_save);
874 /* If there is further processing to do, do it. */
875 if (targetm.have_builtin_setjmp_setup ())
876 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
878 /* We have a nonlocal label. */
879 cfun->has_nonlocal_label = 1;
882 /* Construct the trailing part of a __builtin_setjmp call. This is
883 also called directly by the SJLJ exception handling code.
884 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
886 void
887 expand_builtin_setjmp_receiver (rtx receiver_label)
889 rtx chain;
891 /* Mark the FP as used when we get here, so we have to make sure it's
892 marked as used by this function. */
893 emit_use (hard_frame_pointer_rtx);
895 /* Mark the static chain as clobbered here so life information
896 doesn't get messed up for it. */
897 chain = rtx_for_static_chain (current_function_decl, true);
898 if (chain && REG_P (chain))
899 emit_clobber (chain);
901 /* Now put in the code to restore the frame pointer, and argument
902 pointer, if needed. */
903 if (! targetm.have_nonlocal_goto ())
905 /* First adjust our frame pointer to its actual value. It was
906 previously set to the start of the virtual area corresponding to
907 the stacked variables when we branched here and now needs to be
908 adjusted to the actual hardware fp value.
910 Assignments to virtual registers are converted by
911 instantiate_virtual_regs into the corresponding assignment
912 to the underlying register (fp in this case) that makes
913 the original assignment true.
914 So the following insn will actually be decrementing fp by
915 TARGET_STARTING_FRAME_OFFSET. */
916 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
918 /* Restoring the frame pointer also modifies the hard frame pointer.
919 Mark it used (so that the previous assignment remains live once
920 the frame pointer is eliminated) and clobbered (to represent the
921 implicit update from the assignment). */
922 emit_use (hard_frame_pointer_rtx);
923 emit_clobber (hard_frame_pointer_rtx);
926 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
928 /* If the argument pointer can be eliminated in favor of the
929 frame pointer, we don't need to restore it. We assume here
930 that if such an elimination is present, it can always be used.
931 This is the case on all known machines; if we don't make this
932 assumption, we do unnecessary saving on many machines. */
933 size_t i;
934 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
936 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
937 if (elim_regs[i].from == ARG_POINTER_REGNUM
938 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
939 break;
941 if (i == ARRAY_SIZE (elim_regs))
943 /* Now restore our arg pointer from the address at which it
944 was saved in our stack frame. */
945 emit_move_insn (crtl->args.internal_arg_pointer,
946 copy_to_reg (get_arg_pointer_save_area ()));
950 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
951 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
952 else if (targetm.have_nonlocal_goto_receiver ())
953 emit_insn (targetm.gen_nonlocal_goto_receiver ());
954 else
955 { /* Nothing */ }
957 /* We must not allow the code we just generated to be reordered by
958 scheduling. Specifically, the update of the frame pointer must
959 happen immediately, not later. */
960 emit_insn (gen_blockage ());
963 /* __builtin_longjmp is passed a pointer to an array of five words (not
964 all will be used on all machines). It operates similarly to the C
965 library function of the same name, but is more efficient. Much of
966 the code below is copied from the handling of non-local gotos. */
968 static void
969 expand_builtin_longjmp (rtx buf_addr, rtx value)
971 rtx fp, lab, stack;
972 rtx_insn *insn, *last;
973 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
975 /* DRAP is needed for stack realign if longjmp is expanded to current
976 function */
977 if (SUPPORTS_STACK_ALIGNMENT)
978 crtl->need_drap = true;
980 if (setjmp_alias_set == -1)
981 setjmp_alias_set = new_alias_set ();
983 buf_addr = convert_memory_address (Pmode, buf_addr);
985 buf_addr = force_reg (Pmode, buf_addr);
987 /* We require that the user must pass a second argument of 1, because
988 that is what builtin_setjmp will return. */
989 gcc_assert (value == const1_rtx);
991 last = get_last_insn ();
992 if (targetm.have_builtin_longjmp ())
993 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
994 else
996 fp = gen_rtx_MEM (Pmode, buf_addr);
997 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
998 GET_MODE_SIZE (Pmode)));
1000 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1001 2 * GET_MODE_SIZE (Pmode)));
1002 set_mem_alias_set (fp, setjmp_alias_set);
1003 set_mem_alias_set (lab, setjmp_alias_set);
1004 set_mem_alias_set (stack, setjmp_alias_set);
1006 /* Pick up FP, label, and SP from the block and jump. This code is
1007 from expand_goto in stmt.c; see there for detailed comments. */
1008 if (targetm.have_nonlocal_goto ())
1009 /* We have to pass a value to the nonlocal_goto pattern that will
1010 get copied into the static_chain pointer, but it does not matter
1011 what that value is, because builtin_setjmp does not use it. */
1012 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1013 else
1015 lab = copy_to_reg (lab);
1017 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1018 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1020 emit_move_insn (hard_frame_pointer_rtx, fp);
1021 emit_stack_restore (SAVE_NONLOCAL, stack);
1023 emit_use (hard_frame_pointer_rtx);
1024 emit_use (stack_pointer_rtx);
1025 emit_indirect_jump (lab);
1029 /* Search backwards and mark the jump insn as a non-local goto.
1030 Note that this precludes the use of __builtin_longjmp to a
1031 __builtin_setjmp target in the same function. However, we've
1032 already cautioned the user that these functions are for
1033 internal exception handling use only. */
1034 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1036 gcc_assert (insn != last);
1038 if (JUMP_P (insn))
1040 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1041 break;
1043 else if (CALL_P (insn))
1044 break;
1048 static inline bool
1049 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1051 return (iter->i < iter->n);
1054 /* This function validates the types of a function call argument list
1055 against a specified list of tree_codes. If the last specifier is a 0,
1056 that represents an ellipsis, otherwise the last specifier must be a
1057 VOID_TYPE. */
1059 static bool
1060 validate_arglist (const_tree callexpr, ...)
1062 enum tree_code code;
1063 bool res = 0;
1064 va_list ap;
1065 const_call_expr_arg_iterator iter;
1066 const_tree arg;
1068 va_start (ap, callexpr);
1069 init_const_call_expr_arg_iterator (callexpr, &iter);
1071 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1072 tree fn = CALL_EXPR_FN (callexpr);
1073 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1075 for (unsigned argno = 1; ; ++argno)
1077 code = (enum tree_code) va_arg (ap, int);
1079 switch (code)
1081 case 0:
1082 /* This signifies an ellipses, any further arguments are all ok. */
1083 res = true;
1084 goto end;
1085 case VOID_TYPE:
1086 /* This signifies an endlink, if no arguments remain, return
1087 true, otherwise return false. */
1088 res = !more_const_call_expr_args_p (&iter);
1089 goto end;
1090 case POINTER_TYPE:
1091 /* The actual argument must be nonnull when either the whole
1092 called function has been declared nonnull, or when the formal
1093 argument corresponding to the actual argument has been. */
1094 if (argmap
1095 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1097 arg = next_const_call_expr_arg (&iter);
1098 if (!validate_arg (arg, code) || integer_zerop (arg))
1099 goto end;
1100 break;
1102 /* FALLTHRU */
1103 default:
1104 /* If no parameters remain or the parameter's code does not
1105 match the specified code, return false. Otherwise continue
1106 checking any remaining arguments. */
1107 arg = next_const_call_expr_arg (&iter);
1108 if (!validate_arg (arg, code))
1109 goto end;
1110 break;
1114 /* We need gotos here since we can only have one VA_CLOSE in a
1115 function. */
1116 end: ;
1117 va_end (ap);
1119 BITMAP_FREE (argmap);
1121 return res;
1124 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1125 and the address of the save area. */
1127 static rtx
1128 expand_builtin_nonlocal_goto (tree exp)
1130 tree t_label, t_save_area;
1131 rtx r_label, r_save_area, r_fp, r_sp;
1132 rtx_insn *insn;
1134 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1135 return NULL_RTX;
1137 t_label = CALL_EXPR_ARG (exp, 0);
1138 t_save_area = CALL_EXPR_ARG (exp, 1);
1140 r_label = expand_normal (t_label);
1141 r_label = convert_memory_address (Pmode, r_label);
1142 r_save_area = expand_normal (t_save_area);
1143 r_save_area = convert_memory_address (Pmode, r_save_area);
1144 /* Copy the address of the save location to a register just in case it was
1145 based on the frame pointer. */
1146 r_save_area = copy_to_reg (r_save_area);
1147 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1148 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1149 plus_constant (Pmode, r_save_area,
1150 GET_MODE_SIZE (Pmode)));
1152 crtl->has_nonlocal_goto = 1;
1154 /* ??? We no longer need to pass the static chain value, afaik. */
1155 if (targetm.have_nonlocal_goto ())
1156 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1157 else
1159 r_label = copy_to_reg (r_label);
1161 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1162 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1164 /* Restore frame pointer for containing function. */
1165 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1166 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1168 /* USE of hard_frame_pointer_rtx added for consistency;
1169 not clear if really needed. */
1170 emit_use (hard_frame_pointer_rtx);
1171 emit_use (stack_pointer_rtx);
1173 /* If the architecture is using a GP register, we must
1174 conservatively assume that the target function makes use of it.
1175 The prologue of functions with nonlocal gotos must therefore
1176 initialize the GP register to the appropriate value, and we
1177 must then make sure that this value is live at the point
1178 of the jump. (Note that this doesn't necessarily apply
1179 to targets with a nonlocal_goto pattern; they are free
1180 to implement it in their own way. Note also that this is
1181 a no-op if the GP register is a global invariant.) */
1182 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1183 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1184 emit_use (pic_offset_table_rtx);
1186 emit_indirect_jump (r_label);
1189 /* Search backwards to the jump insn and mark it as a
1190 non-local goto. */
1191 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1193 if (JUMP_P (insn))
1195 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1196 break;
1198 else if (CALL_P (insn))
1199 break;
1202 return const0_rtx;
1205 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1206 (not all will be used on all machines) that was passed to __builtin_setjmp.
1207 It updates the stack pointer in that block to the current value. This is
1208 also called directly by the SJLJ exception handling code. */
1210 void
1211 expand_builtin_update_setjmp_buf (rtx buf_addr)
1213 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1214 buf_addr = convert_memory_address (Pmode, buf_addr);
1215 rtx stack_save
1216 = gen_rtx_MEM (sa_mode,
1217 memory_address
1218 (sa_mode,
1219 plus_constant (Pmode, buf_addr,
1220 2 * GET_MODE_SIZE (Pmode))));
1222 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1225 /* Expand a call to __builtin_prefetch. For a target that does not support
1226 data prefetch, evaluate the memory address argument in case it has side
1227 effects. */
1229 static void
1230 expand_builtin_prefetch (tree exp)
1232 tree arg0, arg1, arg2;
1233 int nargs;
1234 rtx op0, op1, op2;
1236 if (!validate_arglist (exp, POINTER_TYPE, 0))
1237 return;
1239 arg0 = CALL_EXPR_ARG (exp, 0);
1241 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1242 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1243 locality). */
1244 nargs = call_expr_nargs (exp);
1245 if (nargs > 1)
1246 arg1 = CALL_EXPR_ARG (exp, 1);
1247 else
1248 arg1 = integer_zero_node;
1249 if (nargs > 2)
1250 arg2 = CALL_EXPR_ARG (exp, 2);
1251 else
1252 arg2 = integer_three_node;
1254 /* Argument 0 is an address. */
1255 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1257 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1258 if (TREE_CODE (arg1) != INTEGER_CST)
1260 error ("second argument to %<__builtin_prefetch%> must be a constant");
1261 arg1 = integer_zero_node;
1263 op1 = expand_normal (arg1);
1264 /* Argument 1 must be either zero or one. */
1265 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1267 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1268 " using zero");
1269 op1 = const0_rtx;
1272 /* Argument 2 (locality) must be a compile-time constant int. */
1273 if (TREE_CODE (arg2) != INTEGER_CST)
1275 error ("third argument to %<__builtin_prefetch%> must be a constant");
1276 arg2 = integer_zero_node;
1278 op2 = expand_normal (arg2);
1279 /* Argument 2 must be 0, 1, 2, or 3. */
1280 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1282 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1283 op2 = const0_rtx;
1286 if (targetm.have_prefetch ())
1288 struct expand_operand ops[3];
1290 create_address_operand (&ops[0], op0);
1291 create_integer_operand (&ops[1], INTVAL (op1));
1292 create_integer_operand (&ops[2], INTVAL (op2));
1293 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1294 return;
1297 /* Don't do anything with direct references to volatile memory, but
1298 generate code to handle other side effects. */
1299 if (!MEM_P (op0) && side_effects_p (op0))
1300 emit_insn (op0);
1303 /* Get a MEM rtx for expression EXP which is the address of an operand
1304 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1305 the maximum length of the block of memory that might be accessed or
1306 NULL if unknown. */
1308 static rtx
1309 get_memory_rtx (tree exp, tree len)
1311 tree orig_exp = exp;
1312 rtx addr, mem;
1314 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1315 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1316 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1317 exp = TREE_OPERAND (exp, 0);
1319 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1320 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1322 /* Get an expression we can use to find the attributes to assign to MEM.
1323 First remove any nops. */
1324 while (CONVERT_EXPR_P (exp)
1325 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1326 exp = TREE_OPERAND (exp, 0);
1328 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1329 (as builtin stringops may alias with anything). */
1330 exp = fold_build2 (MEM_REF,
1331 build_array_type (char_type_node,
1332 build_range_type (sizetype,
1333 size_one_node, len)),
1334 exp, build_int_cst (ptr_type_node, 0));
1336 /* If the MEM_REF has no acceptable address, try to get the base object
1337 from the original address we got, and build an all-aliasing
1338 unknown-sized access to that one. */
1339 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1340 set_mem_attributes (mem, exp, 0);
1341 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1342 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1343 0))))
1345 exp = build_fold_addr_expr (exp);
1346 exp = fold_build2 (MEM_REF,
1347 build_array_type (char_type_node,
1348 build_range_type (sizetype,
1349 size_zero_node,
1350 NULL)),
1351 exp, build_int_cst (ptr_type_node, 0));
1352 set_mem_attributes (mem, exp, 0);
1354 set_mem_alias_set (mem, 0);
1355 return mem;
1358 /* Built-in functions to perform an untyped call and return. */
1360 #define apply_args_mode \
1361 (this_target_builtins->x_apply_args_mode)
1362 #define apply_result_mode \
1363 (this_target_builtins->x_apply_result_mode)
1365 /* Return the size required for the block returned by __builtin_apply_args,
1366 and initialize apply_args_mode. */
1368 static int
1369 apply_args_size (void)
1371 static int size = -1;
1372 int align;
1373 unsigned int regno;
1375 /* The values computed by this function never change. */
1376 if (size < 0)
1378 /* The first value is the incoming arg-pointer. */
1379 size = GET_MODE_SIZE (Pmode);
1381 /* The second value is the structure value address unless this is
1382 passed as an "invisible" first argument. */
1383 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1384 size += GET_MODE_SIZE (Pmode);
1386 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1387 if (FUNCTION_ARG_REGNO_P (regno))
1389 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1391 gcc_assert (mode != VOIDmode);
1393 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1394 if (size % align != 0)
1395 size = CEIL (size, align) * align;
1396 size += GET_MODE_SIZE (mode);
1397 apply_args_mode[regno] = mode;
1399 else
1401 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1404 return size;
1407 /* Return the size required for the block returned by __builtin_apply,
1408 and initialize apply_result_mode. */
1410 static int
1411 apply_result_size (void)
1413 static int size = -1;
1414 int align, regno;
1416 /* The values computed by this function never change. */
1417 if (size < 0)
1419 size = 0;
1421 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1422 if (targetm.calls.function_value_regno_p (regno))
1424 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1426 gcc_assert (mode != VOIDmode);
1428 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1429 if (size % align != 0)
1430 size = CEIL (size, align) * align;
1431 size += GET_MODE_SIZE (mode);
1432 apply_result_mode[regno] = mode;
1434 else
1435 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1437 /* Allow targets that use untyped_call and untyped_return to override
1438 the size so that machine-specific information can be stored here. */
1439 #ifdef APPLY_RESULT_SIZE
1440 size = APPLY_RESULT_SIZE;
1441 #endif
1443 return size;
1446 /* Create a vector describing the result block RESULT. If SAVEP is true,
1447 the result block is used to save the values; otherwise it is used to
1448 restore the values. */
1450 static rtx
1451 result_vector (int savep, rtx result)
1453 int regno, size, align, nelts;
1454 fixed_size_mode mode;
1455 rtx reg, mem;
1456 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1458 size = nelts = 0;
1459 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1460 if ((mode = apply_result_mode[regno]) != VOIDmode)
1462 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1463 if (size % align != 0)
1464 size = CEIL (size, align) * align;
1465 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1466 mem = adjust_address (result, mode, size);
1467 savevec[nelts++] = (savep
1468 ? gen_rtx_SET (mem, reg)
1469 : gen_rtx_SET (reg, mem));
1470 size += GET_MODE_SIZE (mode);
1472 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1475 /* Save the state required to perform an untyped call with the same
1476 arguments as were passed to the current function. */
1478 static rtx
1479 expand_builtin_apply_args_1 (void)
1481 rtx registers, tem;
1482 int size, align, regno;
1483 fixed_size_mode mode;
1484 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1486 /* Create a block where the arg-pointer, structure value address,
1487 and argument registers can be saved. */
1488 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1490 /* Walk past the arg-pointer and structure value address. */
1491 size = GET_MODE_SIZE (Pmode);
1492 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1493 size += GET_MODE_SIZE (Pmode);
1495 /* Save each register used in calling a function to the block. */
1496 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1497 if ((mode = apply_args_mode[regno]) != VOIDmode)
1499 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1500 if (size % align != 0)
1501 size = CEIL (size, align) * align;
1503 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1505 emit_move_insn (adjust_address (registers, mode, size), tem);
1506 size += GET_MODE_SIZE (mode);
1509 /* Save the arg pointer to the block. */
1510 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1511 /* We need the pointer as the caller actually passed them to us, not
1512 as we might have pretended they were passed. Make sure it's a valid
1513 operand, as emit_move_insn isn't expected to handle a PLUS. */
1514 if (STACK_GROWS_DOWNWARD)
1516 = force_operand (plus_constant (Pmode, tem,
1517 crtl->args.pretend_args_size),
1518 NULL_RTX);
1519 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1521 size = GET_MODE_SIZE (Pmode);
1523 /* Save the structure value address unless this is passed as an
1524 "invisible" first argument. */
1525 if (struct_incoming_value)
1527 emit_move_insn (adjust_address (registers, Pmode, size),
1528 copy_to_reg (struct_incoming_value));
1529 size += GET_MODE_SIZE (Pmode);
1532 /* Return the address of the block. */
1533 return copy_addr_to_reg (XEXP (registers, 0));
1536 /* __builtin_apply_args returns block of memory allocated on
1537 the stack into which is stored the arg pointer, structure
1538 value address, static chain, and all the registers that might
1539 possibly be used in performing a function call. The code is
1540 moved to the start of the function so the incoming values are
1541 saved. */
1543 static rtx
1544 expand_builtin_apply_args (void)
1546 /* Don't do __builtin_apply_args more than once in a function.
1547 Save the result of the first call and reuse it. */
1548 if (apply_args_value != 0)
1549 return apply_args_value;
1551 /* When this function is called, it means that registers must be
1552 saved on entry to this function. So we migrate the
1553 call to the first insn of this function. */
1554 rtx temp;
1556 start_sequence ();
1557 temp = expand_builtin_apply_args_1 ();
1558 rtx_insn *seq = get_insns ();
1559 end_sequence ();
1561 apply_args_value = temp;
1563 /* Put the insns after the NOTE that starts the function.
1564 If this is inside a start_sequence, make the outer-level insn
1565 chain current, so the code is placed at the start of the
1566 function. If internal_arg_pointer is a non-virtual pseudo,
1567 it needs to be placed after the function that initializes
1568 that pseudo. */
1569 push_topmost_sequence ();
1570 if (REG_P (crtl->args.internal_arg_pointer)
1571 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1572 emit_insn_before (seq, parm_birth_insn);
1573 else
1574 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1575 pop_topmost_sequence ();
1576 return temp;
1580 /* Perform an untyped call and save the state required to perform an
1581 untyped return of whatever value was returned by the given function. */
1583 static rtx
1584 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1586 int size, align, regno;
1587 fixed_size_mode mode;
1588 rtx incoming_args, result, reg, dest, src;
1589 rtx_call_insn *call_insn;
1590 rtx old_stack_level = 0;
1591 rtx call_fusage = 0;
1592 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1594 arguments = convert_memory_address (Pmode, arguments);
1596 /* Create a block where the return registers can be saved. */
1597 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1599 /* Fetch the arg pointer from the ARGUMENTS block. */
1600 incoming_args = gen_reg_rtx (Pmode);
1601 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1602 if (!STACK_GROWS_DOWNWARD)
1603 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1604 incoming_args, 0, OPTAB_LIB_WIDEN);
1606 /* Push a new argument block and copy the arguments. Do not allow
1607 the (potential) memcpy call below to interfere with our stack
1608 manipulations. */
1609 do_pending_stack_adjust ();
1610 NO_DEFER_POP;
1612 /* Save the stack with nonlocal if available. */
1613 if (targetm.have_save_stack_nonlocal ())
1614 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1615 else
1616 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1618 /* Allocate a block of memory onto the stack and copy the memory
1619 arguments to the outgoing arguments address. We can pass TRUE
1620 as the 4th argument because we just saved the stack pointer
1621 and will restore it right after the call. */
1622 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1624 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1625 may have already set current_function_calls_alloca to true.
1626 current_function_calls_alloca won't be set if argsize is zero,
1627 so we have to guarantee need_drap is true here. */
1628 if (SUPPORTS_STACK_ALIGNMENT)
1629 crtl->need_drap = true;
1631 dest = virtual_outgoing_args_rtx;
1632 if (!STACK_GROWS_DOWNWARD)
1634 if (CONST_INT_P (argsize))
1635 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1636 else
1637 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1639 dest = gen_rtx_MEM (BLKmode, dest);
1640 set_mem_align (dest, PARM_BOUNDARY);
1641 src = gen_rtx_MEM (BLKmode, incoming_args);
1642 set_mem_align (src, PARM_BOUNDARY);
1643 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1645 /* Refer to the argument block. */
1646 apply_args_size ();
1647 arguments = gen_rtx_MEM (BLKmode, arguments);
1648 set_mem_align (arguments, PARM_BOUNDARY);
1650 /* Walk past the arg-pointer and structure value address. */
1651 size = GET_MODE_SIZE (Pmode);
1652 if (struct_value)
1653 size += GET_MODE_SIZE (Pmode);
1655 /* Restore each of the registers previously saved. Make USE insns
1656 for each of these registers for use in making the call. */
1657 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1658 if ((mode = apply_args_mode[regno]) != VOIDmode)
1660 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1661 if (size % align != 0)
1662 size = CEIL (size, align) * align;
1663 reg = gen_rtx_REG (mode, regno);
1664 emit_move_insn (reg, adjust_address (arguments, mode, size));
1665 use_reg (&call_fusage, reg);
1666 size += GET_MODE_SIZE (mode);
1669 /* Restore the structure value address unless this is passed as an
1670 "invisible" first argument. */
1671 size = GET_MODE_SIZE (Pmode);
1672 if (struct_value)
1674 rtx value = gen_reg_rtx (Pmode);
1675 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1676 emit_move_insn (struct_value, value);
1677 if (REG_P (struct_value))
1678 use_reg (&call_fusage, struct_value);
1679 size += GET_MODE_SIZE (Pmode);
1682 /* All arguments and registers used for the call are set up by now! */
1683 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1685 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1686 and we don't want to load it into a register as an optimization,
1687 because prepare_call_address already did it if it should be done. */
1688 if (GET_CODE (function) != SYMBOL_REF)
1689 function = memory_address (FUNCTION_MODE, function);
1691 /* Generate the actual call instruction and save the return value. */
1692 if (targetm.have_untyped_call ())
1694 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1695 emit_call_insn (targetm.gen_untyped_call (mem, result,
1696 result_vector (1, result)));
1698 else if (targetm.have_call_value ())
1700 rtx valreg = 0;
1702 /* Locate the unique return register. It is not possible to
1703 express a call that sets more than one return register using
1704 call_value; use untyped_call for that. In fact, untyped_call
1705 only needs to save the return registers in the given block. */
1706 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1707 if ((mode = apply_result_mode[regno]) != VOIDmode)
1709 gcc_assert (!valreg); /* have_untyped_call required. */
1711 valreg = gen_rtx_REG (mode, regno);
1714 emit_insn (targetm.gen_call_value (valreg,
1715 gen_rtx_MEM (FUNCTION_MODE, function),
1716 const0_rtx, NULL_RTX, const0_rtx));
1718 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1720 else
1721 gcc_unreachable ();
1723 /* Find the CALL insn we just emitted, and attach the register usage
1724 information. */
1725 call_insn = last_call_insn ();
1726 add_function_usage_to (call_insn, call_fusage);
1728 /* Restore the stack. */
1729 if (targetm.have_save_stack_nonlocal ())
1730 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1731 else
1732 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1733 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1735 OK_DEFER_POP;
1737 /* Return the address of the result block. */
1738 result = copy_addr_to_reg (XEXP (result, 0));
1739 return convert_memory_address (ptr_mode, result);
1742 /* Perform an untyped return. */
1744 static void
1745 expand_builtin_return (rtx result)
1747 int size, align, regno;
1748 fixed_size_mode mode;
1749 rtx reg;
1750 rtx_insn *call_fusage = 0;
1752 result = convert_memory_address (Pmode, result);
1754 apply_result_size ();
1755 result = gen_rtx_MEM (BLKmode, result);
1757 if (targetm.have_untyped_return ())
1759 rtx vector = result_vector (0, result);
1760 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1761 emit_barrier ();
1762 return;
1765 /* Restore the return value and note that each value is used. */
1766 size = 0;
1767 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1768 if ((mode = apply_result_mode[regno]) != VOIDmode)
1770 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1771 if (size % align != 0)
1772 size = CEIL (size, align) * align;
1773 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1774 emit_move_insn (reg, adjust_address (result, mode, size));
1776 push_to_sequence (call_fusage);
1777 emit_use (reg);
1778 call_fusage = get_insns ();
1779 end_sequence ();
1780 size += GET_MODE_SIZE (mode);
1783 /* Put the USE insns before the return. */
1784 emit_insn (call_fusage);
1786 /* Return whatever values was restored by jumping directly to the end
1787 of the function. */
1788 expand_naked_return ();
1791 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1793 static enum type_class
1794 type_to_class (tree type)
1796 switch (TREE_CODE (type))
1798 case VOID_TYPE: return void_type_class;
1799 case INTEGER_TYPE: return integer_type_class;
1800 case ENUMERAL_TYPE: return enumeral_type_class;
1801 case BOOLEAN_TYPE: return boolean_type_class;
1802 case POINTER_TYPE: return pointer_type_class;
1803 case REFERENCE_TYPE: return reference_type_class;
1804 case OFFSET_TYPE: return offset_type_class;
1805 case REAL_TYPE: return real_type_class;
1806 case COMPLEX_TYPE: return complex_type_class;
1807 case FUNCTION_TYPE: return function_type_class;
1808 case METHOD_TYPE: return method_type_class;
1809 case RECORD_TYPE: return record_type_class;
1810 case UNION_TYPE:
1811 case QUAL_UNION_TYPE: return union_type_class;
1812 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1813 ? string_type_class : array_type_class);
1814 case LANG_TYPE: return lang_type_class;
1815 default: return no_type_class;
1819 /* Expand a call EXP to __builtin_classify_type. */
1821 static rtx
1822 expand_builtin_classify_type (tree exp)
1824 if (call_expr_nargs (exp))
1825 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1826 return GEN_INT (no_type_class);
1829 /* This helper macro, meant to be used in mathfn_built_in below, determines
1830 which among a set of builtin math functions is appropriate for a given type
1831 mode. The `F' (float) and `L' (long double) are automatically generated
1832 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1833 types, there are additional types that are considered with 'F32', 'F64',
1834 'F128', etc. suffixes. */
1835 #define CASE_MATHFN(MATHFN) \
1836 CASE_CFN_##MATHFN: \
1837 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1838 fcodel = BUILT_IN_##MATHFN##L ; break;
1839 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1840 types. */
1841 #define CASE_MATHFN_FLOATN(MATHFN) \
1842 CASE_CFN_##MATHFN: \
1843 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1844 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1845 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1846 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1847 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1848 break;
1849 /* Similar to above, but appends _R after any F/L suffix. */
1850 #define CASE_MATHFN_REENT(MATHFN) \
1851 case CFN_BUILT_IN_##MATHFN##_R: \
1852 case CFN_BUILT_IN_##MATHFN##F_R: \
1853 case CFN_BUILT_IN_##MATHFN##L_R: \
1854 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1855 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1857 /* Return a function equivalent to FN but operating on floating-point
1858 values of type TYPE, or END_BUILTINS if no such function exists.
1859 This is purely an operation on function codes; it does not guarantee
1860 that the target actually has an implementation of the function. */
1862 static built_in_function
1863 mathfn_built_in_2 (tree type, combined_fn fn)
1865 tree mtype;
1866 built_in_function fcode, fcodef, fcodel;
1867 built_in_function fcodef16 = END_BUILTINS;
1868 built_in_function fcodef32 = END_BUILTINS;
1869 built_in_function fcodef64 = END_BUILTINS;
1870 built_in_function fcodef128 = END_BUILTINS;
1871 built_in_function fcodef32x = END_BUILTINS;
1872 built_in_function fcodef64x = END_BUILTINS;
1873 built_in_function fcodef128x = END_BUILTINS;
1875 switch (fn)
1877 CASE_MATHFN (ACOS)
1878 CASE_MATHFN (ACOSH)
1879 CASE_MATHFN (ASIN)
1880 CASE_MATHFN (ASINH)
1881 CASE_MATHFN (ATAN)
1882 CASE_MATHFN (ATAN2)
1883 CASE_MATHFN (ATANH)
1884 CASE_MATHFN (CBRT)
1885 CASE_MATHFN_FLOATN (CEIL)
1886 CASE_MATHFN (CEXPI)
1887 CASE_MATHFN_FLOATN (COPYSIGN)
1888 CASE_MATHFN (COS)
1889 CASE_MATHFN (COSH)
1890 CASE_MATHFN (DREM)
1891 CASE_MATHFN (ERF)
1892 CASE_MATHFN (ERFC)
1893 CASE_MATHFN (EXP)
1894 CASE_MATHFN (EXP10)
1895 CASE_MATHFN (EXP2)
1896 CASE_MATHFN (EXPM1)
1897 CASE_MATHFN (FABS)
1898 CASE_MATHFN (FDIM)
1899 CASE_MATHFN_FLOATN (FLOOR)
1900 CASE_MATHFN_FLOATN (FMA)
1901 CASE_MATHFN_FLOATN (FMAX)
1902 CASE_MATHFN_FLOATN (FMIN)
1903 CASE_MATHFN (FMOD)
1904 CASE_MATHFN (FREXP)
1905 CASE_MATHFN (GAMMA)
1906 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1907 CASE_MATHFN (HUGE_VAL)
1908 CASE_MATHFN (HYPOT)
1909 CASE_MATHFN (ILOGB)
1910 CASE_MATHFN (ICEIL)
1911 CASE_MATHFN (IFLOOR)
1912 CASE_MATHFN (INF)
1913 CASE_MATHFN (IRINT)
1914 CASE_MATHFN (IROUND)
1915 CASE_MATHFN (ISINF)
1916 CASE_MATHFN (J0)
1917 CASE_MATHFN (J1)
1918 CASE_MATHFN (JN)
1919 CASE_MATHFN (LCEIL)
1920 CASE_MATHFN (LDEXP)
1921 CASE_MATHFN (LFLOOR)
1922 CASE_MATHFN (LGAMMA)
1923 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1924 CASE_MATHFN (LLCEIL)
1925 CASE_MATHFN (LLFLOOR)
1926 CASE_MATHFN (LLRINT)
1927 CASE_MATHFN (LLROUND)
1928 CASE_MATHFN (LOG)
1929 CASE_MATHFN (LOG10)
1930 CASE_MATHFN (LOG1P)
1931 CASE_MATHFN (LOG2)
1932 CASE_MATHFN (LOGB)
1933 CASE_MATHFN (LRINT)
1934 CASE_MATHFN (LROUND)
1935 CASE_MATHFN (MODF)
1936 CASE_MATHFN (NAN)
1937 CASE_MATHFN (NANS)
1938 CASE_MATHFN_FLOATN (NEARBYINT)
1939 CASE_MATHFN (NEXTAFTER)
1940 CASE_MATHFN (NEXTTOWARD)
1941 CASE_MATHFN (POW)
1942 CASE_MATHFN (POWI)
1943 CASE_MATHFN (POW10)
1944 CASE_MATHFN (REMAINDER)
1945 CASE_MATHFN (REMQUO)
1946 CASE_MATHFN_FLOATN (RINT)
1947 CASE_MATHFN_FLOATN (ROUND)
1948 CASE_MATHFN (SCALB)
1949 CASE_MATHFN (SCALBLN)
1950 CASE_MATHFN (SCALBN)
1951 CASE_MATHFN (SIGNBIT)
1952 CASE_MATHFN (SIGNIFICAND)
1953 CASE_MATHFN (SIN)
1954 CASE_MATHFN (SINCOS)
1955 CASE_MATHFN (SINH)
1956 CASE_MATHFN_FLOATN (SQRT)
1957 CASE_MATHFN (TAN)
1958 CASE_MATHFN (TANH)
1959 CASE_MATHFN (TGAMMA)
1960 CASE_MATHFN_FLOATN (TRUNC)
1961 CASE_MATHFN (Y0)
1962 CASE_MATHFN (Y1)
1963 CASE_MATHFN (YN)
1965 default:
1966 return END_BUILTINS;
1969 mtype = TYPE_MAIN_VARIANT (type);
1970 if (mtype == double_type_node)
1971 return fcode;
1972 else if (mtype == float_type_node)
1973 return fcodef;
1974 else if (mtype == long_double_type_node)
1975 return fcodel;
1976 else if (mtype == float16_type_node)
1977 return fcodef16;
1978 else if (mtype == float32_type_node)
1979 return fcodef32;
1980 else if (mtype == float64_type_node)
1981 return fcodef64;
1982 else if (mtype == float128_type_node)
1983 return fcodef128;
1984 else if (mtype == float32x_type_node)
1985 return fcodef32x;
1986 else if (mtype == float64x_type_node)
1987 return fcodef64x;
1988 else if (mtype == float128x_type_node)
1989 return fcodef128x;
1990 else
1991 return END_BUILTINS;
1994 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1995 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1996 otherwise use the explicit declaration. If we can't do the conversion,
1997 return null. */
1999 static tree
2000 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2002 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2003 if (fcode2 == END_BUILTINS)
2004 return NULL_TREE;
2006 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2007 return NULL_TREE;
2009 return builtin_decl_explicit (fcode2);
2012 /* Like mathfn_built_in_1, but always use the implicit array. */
2014 tree
2015 mathfn_built_in (tree type, combined_fn fn)
2017 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2020 /* Like mathfn_built_in_1, but take a built_in_function and
2021 always use the implicit array. */
2023 tree
2024 mathfn_built_in (tree type, enum built_in_function fn)
2026 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2029 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2030 return its code, otherwise return IFN_LAST. Note that this function
2031 only tests whether the function is defined in internals.def, not whether
2032 it is actually available on the target. */
2034 internal_fn
2035 associated_internal_fn (tree fndecl)
2037 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2038 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
2039 switch (DECL_FUNCTION_CODE (fndecl))
2041 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2042 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2043 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2044 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2045 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2046 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2047 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2048 #include "internal-fn.def"
2050 CASE_FLT_FN (BUILT_IN_POW10):
2051 return IFN_EXP10;
2053 CASE_FLT_FN (BUILT_IN_DREM):
2054 return IFN_REMAINDER;
2056 CASE_FLT_FN (BUILT_IN_SCALBN):
2057 CASE_FLT_FN (BUILT_IN_SCALBLN):
2058 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2059 return IFN_LDEXP;
2060 return IFN_LAST;
2062 default:
2063 return IFN_LAST;
2067 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2068 on the current target by a call to an internal function, return the
2069 code of that internal function, otherwise return IFN_LAST. The caller
2070 is responsible for ensuring that any side-effects of the built-in
2071 call are dealt with correctly. E.g. if CALL sets errno, the caller
2072 must decide that the errno result isn't needed or make it available
2073 in some other way. */
2075 internal_fn
2076 replacement_internal_fn (gcall *call)
2078 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2080 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2081 if (ifn != IFN_LAST)
2083 tree_pair types = direct_internal_fn_types (ifn, call);
2084 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2085 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2086 return ifn;
2089 return IFN_LAST;
2092 /* Expand a call to the builtin trinary math functions (fma).
2093 Return NULL_RTX if a normal call should be emitted rather than expanding the
2094 function in-line. EXP is the expression that is a call to the builtin
2095 function; if convenient, the result should be placed in TARGET.
2096 SUBTARGET may be used as the target for computing one of EXP's
2097 operands. */
2099 static rtx
2100 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2102 optab builtin_optab;
2103 rtx op0, op1, op2, result;
2104 rtx_insn *insns;
2105 tree fndecl = get_callee_fndecl (exp);
2106 tree arg0, arg1, arg2;
2107 machine_mode mode;
2109 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2110 return NULL_RTX;
2112 arg0 = CALL_EXPR_ARG (exp, 0);
2113 arg1 = CALL_EXPR_ARG (exp, 1);
2114 arg2 = CALL_EXPR_ARG (exp, 2);
2116 switch (DECL_FUNCTION_CODE (fndecl))
2118 CASE_FLT_FN (BUILT_IN_FMA):
2119 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2120 builtin_optab = fma_optab; break;
2121 default:
2122 gcc_unreachable ();
2125 /* Make a suitable register to place result in. */
2126 mode = TYPE_MODE (TREE_TYPE (exp));
2128 /* Before working hard, check whether the instruction is available. */
2129 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2130 return NULL_RTX;
2132 result = gen_reg_rtx (mode);
2134 /* Always stabilize the argument list. */
2135 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2136 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2137 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2139 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2140 op1 = expand_normal (arg1);
2141 op2 = expand_normal (arg2);
2143 start_sequence ();
2145 /* Compute into RESULT.
2146 Set RESULT to wherever the result comes back. */
2147 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2148 result, 0);
2150 /* If we were unable to expand via the builtin, stop the sequence
2151 (without outputting the insns) and call to the library function
2152 with the stabilized argument list. */
2153 if (result == 0)
2155 end_sequence ();
2156 return expand_call (exp, target, target == const0_rtx);
2159 /* Output the entire sequence. */
2160 insns = get_insns ();
2161 end_sequence ();
2162 emit_insn (insns);
2164 return result;
2167 /* Expand a call to the builtin sin and cos math functions.
2168 Return NULL_RTX if a normal call should be emitted rather than expanding the
2169 function in-line. EXP is the expression that is a call to the builtin
2170 function; if convenient, the result should be placed in TARGET.
2171 SUBTARGET may be used as the target for computing one of EXP's
2172 operands. */
2174 static rtx
2175 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2177 optab builtin_optab;
2178 rtx op0;
2179 rtx_insn *insns;
2180 tree fndecl = get_callee_fndecl (exp);
2181 machine_mode mode;
2182 tree arg;
2184 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2185 return NULL_RTX;
2187 arg = CALL_EXPR_ARG (exp, 0);
2189 switch (DECL_FUNCTION_CODE (fndecl))
2191 CASE_FLT_FN (BUILT_IN_SIN):
2192 CASE_FLT_FN (BUILT_IN_COS):
2193 builtin_optab = sincos_optab; break;
2194 default:
2195 gcc_unreachable ();
2198 /* Make a suitable register to place result in. */
2199 mode = TYPE_MODE (TREE_TYPE (exp));
2201 /* Check if sincos insn is available, otherwise fallback
2202 to sin or cos insn. */
2203 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2204 switch (DECL_FUNCTION_CODE (fndecl))
2206 CASE_FLT_FN (BUILT_IN_SIN):
2207 builtin_optab = sin_optab; break;
2208 CASE_FLT_FN (BUILT_IN_COS):
2209 builtin_optab = cos_optab; break;
2210 default:
2211 gcc_unreachable ();
2214 /* Before working hard, check whether the instruction is available. */
2215 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2217 rtx result = gen_reg_rtx (mode);
2219 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2220 need to expand the argument again. This way, we will not perform
2221 side-effects more the once. */
2222 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2224 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2226 start_sequence ();
2228 /* Compute into RESULT.
2229 Set RESULT to wherever the result comes back. */
2230 if (builtin_optab == sincos_optab)
2232 int ok;
2234 switch (DECL_FUNCTION_CODE (fndecl))
2236 CASE_FLT_FN (BUILT_IN_SIN):
2237 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2238 break;
2239 CASE_FLT_FN (BUILT_IN_COS):
2240 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2241 break;
2242 default:
2243 gcc_unreachable ();
2245 gcc_assert (ok);
2247 else
2248 result = expand_unop (mode, builtin_optab, op0, result, 0);
2250 if (result != 0)
2252 /* Output the entire sequence. */
2253 insns = get_insns ();
2254 end_sequence ();
2255 emit_insn (insns);
2256 return result;
2259 /* If we were unable to expand via the builtin, stop the sequence
2260 (without outputting the insns) and call to the library function
2261 with the stabilized argument list. */
2262 end_sequence ();
2265 return expand_call (exp, target, target == const0_rtx);
2268 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2269 return an RTL instruction code that implements the functionality.
2270 If that isn't possible or available return CODE_FOR_nothing. */
2272 static enum insn_code
2273 interclass_mathfn_icode (tree arg, tree fndecl)
2275 bool errno_set = false;
2276 optab builtin_optab = unknown_optab;
2277 machine_mode mode;
2279 switch (DECL_FUNCTION_CODE (fndecl))
2281 CASE_FLT_FN (BUILT_IN_ILOGB):
2282 errno_set = true; builtin_optab = ilogb_optab; break;
2283 CASE_FLT_FN (BUILT_IN_ISINF):
2284 builtin_optab = isinf_optab; break;
2285 case BUILT_IN_ISNORMAL:
2286 case BUILT_IN_ISFINITE:
2287 CASE_FLT_FN (BUILT_IN_FINITE):
2288 case BUILT_IN_FINITED32:
2289 case BUILT_IN_FINITED64:
2290 case BUILT_IN_FINITED128:
2291 case BUILT_IN_ISINFD32:
2292 case BUILT_IN_ISINFD64:
2293 case BUILT_IN_ISINFD128:
2294 /* These builtins have no optabs (yet). */
2295 break;
2296 default:
2297 gcc_unreachable ();
2300 /* There's no easy way to detect the case we need to set EDOM. */
2301 if (flag_errno_math && errno_set)
2302 return CODE_FOR_nothing;
2304 /* Optab mode depends on the mode of the input argument. */
2305 mode = TYPE_MODE (TREE_TYPE (arg));
2307 if (builtin_optab)
2308 return optab_handler (builtin_optab, mode);
2309 return CODE_FOR_nothing;
2312 /* Expand a call to one of the builtin math functions that operate on
2313 floating point argument and output an integer result (ilogb, isinf,
2314 isnan, etc).
2315 Return 0 if a normal call should be emitted rather than expanding the
2316 function in-line. EXP is the expression that is a call to the builtin
2317 function; if convenient, the result should be placed in TARGET. */
2319 static rtx
2320 expand_builtin_interclass_mathfn (tree exp, rtx target)
2322 enum insn_code icode = CODE_FOR_nothing;
2323 rtx op0;
2324 tree fndecl = get_callee_fndecl (exp);
2325 machine_mode mode;
2326 tree arg;
2328 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2329 return NULL_RTX;
2331 arg = CALL_EXPR_ARG (exp, 0);
2332 icode = interclass_mathfn_icode (arg, fndecl);
2333 mode = TYPE_MODE (TREE_TYPE (arg));
2335 if (icode != CODE_FOR_nothing)
2337 struct expand_operand ops[1];
2338 rtx_insn *last = get_last_insn ();
2339 tree orig_arg = arg;
2341 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2342 need to expand the argument again. This way, we will not perform
2343 side-effects more the once. */
2344 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2346 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2348 if (mode != GET_MODE (op0))
2349 op0 = convert_to_mode (mode, op0, 0);
2351 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2352 if (maybe_legitimize_operands (icode, 0, 1, ops)
2353 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2354 return ops[0].value;
2356 delete_insns_since (last);
2357 CALL_EXPR_ARG (exp, 0) = orig_arg;
2360 return NULL_RTX;
2363 /* Expand a call to the builtin sincos math function.
2364 Return NULL_RTX if a normal call should be emitted rather than expanding the
2365 function in-line. EXP is the expression that is a call to the builtin
2366 function. */
2368 static rtx
2369 expand_builtin_sincos (tree exp)
2371 rtx op0, op1, op2, target1, target2;
2372 machine_mode mode;
2373 tree arg, sinp, cosp;
2374 int result;
2375 location_t loc = EXPR_LOCATION (exp);
2376 tree alias_type, alias_off;
2378 if (!validate_arglist (exp, REAL_TYPE,
2379 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2380 return NULL_RTX;
2382 arg = CALL_EXPR_ARG (exp, 0);
2383 sinp = CALL_EXPR_ARG (exp, 1);
2384 cosp = CALL_EXPR_ARG (exp, 2);
2386 /* Make a suitable register to place result in. */
2387 mode = TYPE_MODE (TREE_TYPE (arg));
2389 /* Check if sincos insn is available, otherwise emit the call. */
2390 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2391 return NULL_RTX;
2393 target1 = gen_reg_rtx (mode);
2394 target2 = gen_reg_rtx (mode);
2396 op0 = expand_normal (arg);
2397 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2398 alias_off = build_int_cst (alias_type, 0);
2399 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2400 sinp, alias_off));
2401 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2402 cosp, alias_off));
2404 /* Compute into target1 and target2.
2405 Set TARGET to wherever the result comes back. */
2406 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2407 gcc_assert (result);
2409 /* Move target1 and target2 to the memory locations indicated
2410 by op1 and op2. */
2411 emit_move_insn (op1, target1);
2412 emit_move_insn (op2, target2);
2414 return const0_rtx;
2417 /* Expand a call to the internal cexpi builtin to the sincos math function.
2418 EXP is the expression that is a call to the builtin function; if convenient,
2419 the result should be placed in TARGET. */
2421 static rtx
2422 expand_builtin_cexpi (tree exp, rtx target)
2424 tree fndecl = get_callee_fndecl (exp);
2425 tree arg, type;
2426 machine_mode mode;
2427 rtx op0, op1, op2;
2428 location_t loc = EXPR_LOCATION (exp);
2430 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2431 return NULL_RTX;
2433 arg = CALL_EXPR_ARG (exp, 0);
2434 type = TREE_TYPE (arg);
2435 mode = TYPE_MODE (TREE_TYPE (arg));
2437 /* Try expanding via a sincos optab, fall back to emitting a libcall
2438 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2439 is only generated from sincos, cexp or if we have either of them. */
2440 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2442 op1 = gen_reg_rtx (mode);
2443 op2 = gen_reg_rtx (mode);
2445 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2447 /* Compute into op1 and op2. */
2448 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2450 else if (targetm.libc_has_function (function_sincos))
2452 tree call, fn = NULL_TREE;
2453 tree top1, top2;
2454 rtx op1a, op2a;
2456 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2457 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2458 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2459 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2460 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2461 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2462 else
2463 gcc_unreachable ();
2465 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2466 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2467 op1a = copy_addr_to_reg (XEXP (op1, 0));
2468 op2a = copy_addr_to_reg (XEXP (op2, 0));
2469 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2470 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2472 /* Make sure not to fold the sincos call again. */
2473 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2474 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2475 call, 3, arg, top1, top2));
2477 else
2479 tree call, fn = NULL_TREE, narg;
2480 tree ctype = build_complex_type (type);
2482 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2483 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2484 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2485 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2486 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2487 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2488 else
2489 gcc_unreachable ();
2491 /* If we don't have a decl for cexp create one. This is the
2492 friendliest fallback if the user calls __builtin_cexpi
2493 without full target C99 function support. */
2494 if (fn == NULL_TREE)
2496 tree fntype;
2497 const char *name = NULL;
2499 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2500 name = "cexpf";
2501 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2502 name = "cexp";
2503 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2504 name = "cexpl";
2506 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2507 fn = build_fn_decl (name, fntype);
2510 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2511 build_real (type, dconst0), arg);
2513 /* Make sure not to fold the cexp call again. */
2514 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2515 return expand_expr (build_call_nary (ctype, call, 1, narg),
2516 target, VOIDmode, EXPAND_NORMAL);
2519 /* Now build the proper return type. */
2520 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2521 make_tree (TREE_TYPE (arg), op2),
2522 make_tree (TREE_TYPE (arg), op1)),
2523 target, VOIDmode, EXPAND_NORMAL);
2526 /* Conveniently construct a function call expression. FNDECL names the
2527 function to be called, N is the number of arguments, and the "..."
2528 parameters are the argument expressions. Unlike build_call_exr
2529 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2531 static tree
2532 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2534 va_list ap;
2535 tree fntype = TREE_TYPE (fndecl);
2536 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2538 va_start (ap, n);
2539 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2540 va_end (ap);
2541 SET_EXPR_LOCATION (fn, loc);
2542 return fn;
2545 /* Expand a call to one of the builtin rounding functions gcc defines
2546 as an extension (lfloor and lceil). As these are gcc extensions we
2547 do not need to worry about setting errno to EDOM.
2548 If expanding via optab fails, lower expression to (int)(floor(x)).
2549 EXP is the expression that is a call to the builtin function;
2550 if convenient, the result should be placed in TARGET. */
2552 static rtx
2553 expand_builtin_int_roundingfn (tree exp, rtx target)
2555 convert_optab builtin_optab;
2556 rtx op0, tmp;
2557 rtx_insn *insns;
2558 tree fndecl = get_callee_fndecl (exp);
2559 enum built_in_function fallback_fn;
2560 tree fallback_fndecl;
2561 machine_mode mode;
2562 tree arg;
2564 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2565 gcc_unreachable ();
2567 arg = CALL_EXPR_ARG (exp, 0);
2569 switch (DECL_FUNCTION_CODE (fndecl))
2571 CASE_FLT_FN (BUILT_IN_ICEIL):
2572 CASE_FLT_FN (BUILT_IN_LCEIL):
2573 CASE_FLT_FN (BUILT_IN_LLCEIL):
2574 builtin_optab = lceil_optab;
2575 fallback_fn = BUILT_IN_CEIL;
2576 break;
2578 CASE_FLT_FN (BUILT_IN_IFLOOR):
2579 CASE_FLT_FN (BUILT_IN_LFLOOR):
2580 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2581 builtin_optab = lfloor_optab;
2582 fallback_fn = BUILT_IN_FLOOR;
2583 break;
2585 default:
2586 gcc_unreachable ();
2589 /* Make a suitable register to place result in. */
2590 mode = TYPE_MODE (TREE_TYPE (exp));
2592 target = gen_reg_rtx (mode);
2594 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2595 need to expand the argument again. This way, we will not perform
2596 side-effects more the once. */
2597 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2599 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2601 start_sequence ();
2603 /* Compute into TARGET. */
2604 if (expand_sfix_optab (target, op0, builtin_optab))
2606 /* Output the entire sequence. */
2607 insns = get_insns ();
2608 end_sequence ();
2609 emit_insn (insns);
2610 return target;
2613 /* If we were unable to expand via the builtin, stop the sequence
2614 (without outputting the insns). */
2615 end_sequence ();
2617 /* Fall back to floating point rounding optab. */
2618 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2620 /* For non-C99 targets we may end up without a fallback fndecl here
2621 if the user called __builtin_lfloor directly. In this case emit
2622 a call to the floor/ceil variants nevertheless. This should result
2623 in the best user experience for not full C99 targets. */
2624 if (fallback_fndecl == NULL_TREE)
2626 tree fntype;
2627 const char *name = NULL;
2629 switch (DECL_FUNCTION_CODE (fndecl))
2631 case BUILT_IN_ICEIL:
2632 case BUILT_IN_LCEIL:
2633 case BUILT_IN_LLCEIL:
2634 name = "ceil";
2635 break;
2636 case BUILT_IN_ICEILF:
2637 case BUILT_IN_LCEILF:
2638 case BUILT_IN_LLCEILF:
2639 name = "ceilf";
2640 break;
2641 case BUILT_IN_ICEILL:
2642 case BUILT_IN_LCEILL:
2643 case BUILT_IN_LLCEILL:
2644 name = "ceill";
2645 break;
2646 case BUILT_IN_IFLOOR:
2647 case BUILT_IN_LFLOOR:
2648 case BUILT_IN_LLFLOOR:
2649 name = "floor";
2650 break;
2651 case BUILT_IN_IFLOORF:
2652 case BUILT_IN_LFLOORF:
2653 case BUILT_IN_LLFLOORF:
2654 name = "floorf";
2655 break;
2656 case BUILT_IN_IFLOORL:
2657 case BUILT_IN_LFLOORL:
2658 case BUILT_IN_LLFLOORL:
2659 name = "floorl";
2660 break;
2661 default:
2662 gcc_unreachable ();
2665 fntype = build_function_type_list (TREE_TYPE (arg),
2666 TREE_TYPE (arg), NULL_TREE);
2667 fallback_fndecl = build_fn_decl (name, fntype);
2670 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2672 tmp = expand_normal (exp);
2673 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2675 /* Truncate the result of floating point optab to integer
2676 via expand_fix (). */
2677 target = gen_reg_rtx (mode);
2678 expand_fix (target, tmp, 0);
2680 return target;
2683 /* Expand a call to one of the builtin math functions doing integer
2684 conversion (lrint).
2685 Return 0 if a normal call should be emitted rather than expanding the
2686 function in-line. EXP is the expression that is a call to the builtin
2687 function; if convenient, the result should be placed in TARGET. */
2689 static rtx
2690 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2692 convert_optab builtin_optab;
2693 rtx op0;
2694 rtx_insn *insns;
2695 tree fndecl = get_callee_fndecl (exp);
2696 tree arg;
2697 machine_mode mode;
2698 enum built_in_function fallback_fn = BUILT_IN_NONE;
2700 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2701 gcc_unreachable ();
2703 arg = CALL_EXPR_ARG (exp, 0);
2705 switch (DECL_FUNCTION_CODE (fndecl))
2707 CASE_FLT_FN (BUILT_IN_IRINT):
2708 fallback_fn = BUILT_IN_LRINT;
2709 gcc_fallthrough ();
2710 CASE_FLT_FN (BUILT_IN_LRINT):
2711 CASE_FLT_FN (BUILT_IN_LLRINT):
2712 builtin_optab = lrint_optab;
2713 break;
2715 CASE_FLT_FN (BUILT_IN_IROUND):
2716 fallback_fn = BUILT_IN_LROUND;
2717 gcc_fallthrough ();
2718 CASE_FLT_FN (BUILT_IN_LROUND):
2719 CASE_FLT_FN (BUILT_IN_LLROUND):
2720 builtin_optab = lround_optab;
2721 break;
2723 default:
2724 gcc_unreachable ();
2727 /* There's no easy way to detect the case we need to set EDOM. */
2728 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2729 return NULL_RTX;
2731 /* Make a suitable register to place result in. */
2732 mode = TYPE_MODE (TREE_TYPE (exp));
2734 /* There's no easy way to detect the case we need to set EDOM. */
2735 if (!flag_errno_math)
2737 rtx result = gen_reg_rtx (mode);
2739 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2740 need to expand the argument again. This way, we will not perform
2741 side-effects more the once. */
2742 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2744 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2746 start_sequence ();
2748 if (expand_sfix_optab (result, op0, builtin_optab))
2750 /* Output the entire sequence. */
2751 insns = get_insns ();
2752 end_sequence ();
2753 emit_insn (insns);
2754 return result;
2757 /* If we were unable to expand via the builtin, stop the sequence
2758 (without outputting the insns) and call to the library function
2759 with the stabilized argument list. */
2760 end_sequence ();
2763 if (fallback_fn != BUILT_IN_NONE)
2765 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2766 targets, (int) round (x) should never be transformed into
2767 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2768 a call to lround in the hope that the target provides at least some
2769 C99 functions. This should result in the best user experience for
2770 not full C99 targets. */
2771 tree fallback_fndecl = mathfn_built_in_1
2772 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2774 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2775 fallback_fndecl, 1, arg);
2777 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2778 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2779 return convert_to_mode (mode, target, 0);
2782 return expand_call (exp, target, target == const0_rtx);
2785 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2786 a normal call should be emitted rather than expanding the function
2787 in-line. EXP is the expression that is a call to the builtin
2788 function; if convenient, the result should be placed in TARGET. */
2790 static rtx
2791 expand_builtin_powi (tree exp, rtx target)
2793 tree arg0, arg1;
2794 rtx op0, op1;
2795 machine_mode mode;
2796 machine_mode mode2;
2798 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2799 return NULL_RTX;
2801 arg0 = CALL_EXPR_ARG (exp, 0);
2802 arg1 = CALL_EXPR_ARG (exp, 1);
2803 mode = TYPE_MODE (TREE_TYPE (exp));
2805 /* Emit a libcall to libgcc. */
2807 /* Mode of the 2nd argument must match that of an int. */
2808 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
2810 if (target == NULL_RTX)
2811 target = gen_reg_rtx (mode);
2813 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2814 if (GET_MODE (op0) != mode)
2815 op0 = convert_to_mode (mode, op0, 0);
2816 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2817 if (GET_MODE (op1) != mode2)
2818 op1 = convert_to_mode (mode2, op1, 0);
2820 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2821 target, LCT_CONST, mode,
2822 op0, mode, op1, mode2);
2824 return target;
2827 /* Expand expression EXP which is a call to the strlen builtin. Return
2828 NULL_RTX if we failed the caller should emit a normal call, otherwise
2829 try to get the result in TARGET, if convenient. */
2831 static rtx
2832 expand_builtin_strlen (tree exp, rtx target,
2833 machine_mode target_mode)
2835 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2836 return NULL_RTX;
2838 struct expand_operand ops[4];
2839 rtx pat;
2840 tree len;
2841 tree src = CALL_EXPR_ARG (exp, 0);
2842 rtx src_reg;
2843 rtx_insn *before_strlen;
2844 machine_mode insn_mode;
2845 enum insn_code icode = CODE_FOR_nothing;
2846 unsigned int align;
2848 /* If the length can be computed at compile-time, return it. */
2849 len = c_strlen (src, 0);
2850 if (len)
2851 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2853 /* If the length can be computed at compile-time and is constant
2854 integer, but there are side-effects in src, evaluate
2855 src for side-effects, then return len.
2856 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2857 can be optimized into: i++; x = 3; */
2858 len = c_strlen (src, 1);
2859 if (len && TREE_CODE (len) == INTEGER_CST)
2861 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2862 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2865 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2867 /* If SRC is not a pointer type, don't do this operation inline. */
2868 if (align == 0)
2869 return NULL_RTX;
2871 /* Bail out if we can't compute strlen in the right mode. */
2872 FOR_EACH_MODE_FROM (insn_mode, target_mode)
2874 icode = optab_handler (strlen_optab, insn_mode);
2875 if (icode != CODE_FOR_nothing)
2876 break;
2878 if (insn_mode == VOIDmode)
2879 return NULL_RTX;
2881 /* Make a place to hold the source address. We will not expand
2882 the actual source until we are sure that the expansion will
2883 not fail -- there are trees that cannot be expanded twice. */
2884 src_reg = gen_reg_rtx (Pmode);
2886 /* Mark the beginning of the strlen sequence so we can emit the
2887 source operand later. */
2888 before_strlen = get_last_insn ();
2890 create_output_operand (&ops[0], target, insn_mode);
2891 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2892 create_integer_operand (&ops[2], 0);
2893 create_integer_operand (&ops[3], align);
2894 if (!maybe_expand_insn (icode, 4, ops))
2895 return NULL_RTX;
2897 /* Check to see if the argument was declared attribute nonstring
2898 and if so, issue a warning since at this point it's not known
2899 to be nul-terminated. */
2900 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
2902 /* Now that we are assured of success, expand the source. */
2903 start_sequence ();
2904 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2905 if (pat != src_reg)
2907 #ifdef POINTERS_EXTEND_UNSIGNED
2908 if (GET_MODE (pat) != Pmode)
2909 pat = convert_to_mode (Pmode, pat,
2910 POINTERS_EXTEND_UNSIGNED);
2911 #endif
2912 emit_move_insn (src_reg, pat);
2914 pat = get_insns ();
2915 end_sequence ();
2917 if (before_strlen)
2918 emit_insn_after (pat, before_strlen);
2919 else
2920 emit_insn_before (pat, get_insns ());
2922 /* Return the value in the proper mode for this function. */
2923 if (GET_MODE (ops[0].value) == target_mode)
2924 target = ops[0].value;
2925 else if (target != 0)
2926 convert_move (target, ops[0].value, 0);
2927 else
2928 target = convert_to_mode (target_mode, ops[0].value, 0);
2930 return target;
2933 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2934 bytes from constant string DATA + OFFSET and return it as target
2935 constant. */
2937 static rtx
2938 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2939 scalar_int_mode mode)
2941 const char *str = (const char *) data;
2943 gcc_assert (offset >= 0
2944 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2945 <= strlen (str) + 1));
2947 return c_readstr (str + offset, mode);
2950 /* LEN specify length of the block of memcpy/memset operation.
2951 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
2952 In some cases we can make very likely guess on max size, then we
2953 set it into PROBABLE_MAX_SIZE. */
2955 static void
2956 determine_block_size (tree len, rtx len_rtx,
2957 unsigned HOST_WIDE_INT *min_size,
2958 unsigned HOST_WIDE_INT *max_size,
2959 unsigned HOST_WIDE_INT *probable_max_size)
2961 if (CONST_INT_P (len_rtx))
2963 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
2964 return;
2966 else
2968 wide_int min, max;
2969 enum value_range_type range_type = VR_UNDEFINED;
2971 /* Determine bounds from the type. */
2972 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
2973 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
2974 else
2975 *min_size = 0;
2976 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
2977 *probable_max_size = *max_size
2978 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
2979 else
2980 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
2982 if (TREE_CODE (len) == SSA_NAME)
2983 range_type = get_range_info (len, &min, &max);
2984 if (range_type == VR_RANGE)
2986 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
2987 *min_size = min.to_uhwi ();
2988 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
2989 *probable_max_size = *max_size = max.to_uhwi ();
2991 else if (range_type == VR_ANTI_RANGE)
2993 /* Anti range 0...N lets us to determine minimal size to N+1. */
2994 if (min == 0)
2996 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
2997 *min_size = max.to_uhwi () + 1;
2999 /* Code like
3001 int n;
3002 if (n < 100)
3003 memcpy (a, b, n)
3005 Produce anti range allowing negative values of N. We still
3006 can use the information and make a guess that N is not negative.
3008 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3009 *probable_max_size = min.to_uhwi () - 1;
3012 gcc_checking_assert (*max_size <=
3013 (unsigned HOST_WIDE_INT)
3014 GET_MODE_MASK (GET_MODE (len_rtx)));
3017 /* Try to verify that the sizes and lengths of the arguments to a string
3018 manipulation function given by EXP are within valid bounds and that
3019 the operation does not lead to buffer overflow or read past the end.
3020 Arguments other than EXP may be null. When non-null, the arguments
3021 have the following meaning:
3022 DST is the destination of a copy call or NULL otherwise.
3023 SRC is the source of a copy call or NULL otherwise.
3024 DSTWRITE is the number of bytes written into the destination obtained
3025 from the user-supplied size argument to the function (such as in
3026 memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
3027 MAXREAD is the user-supplied bound on the length of the source sequence
3028 (such as in strncat(d, s, N). It specifies the upper limit on the number
3029 of bytes to write. If NULL, it's taken to be the same as DSTWRITE.
3030 SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
3031 expression EXP is a string function call (as opposed to a memory call
3032 like memcpy). As an exception, SRCSTR can also be an integer denoting
3033 the precomputed size of the source string or object (for functions like
3034 memcpy).
3035 DSTSIZE is the size of the destination object specified by the last
3036 argument to the _chk builtins, typically resulting from the expansion
3037 of __builtin_object_size (such as in __builtin___strcpy_chk(DST, SRC,
3038 DSTSIZE).
3040 When DSTWRITE is null LEN is checked to verify that it doesn't exceed
3041 SIZE_MAX.
3043 If the call is successfully verified as safe return true, otherwise
3044 return false. */
3046 static bool
3047 check_access (tree exp, tree, tree, tree dstwrite,
3048 tree maxread, tree srcstr, tree dstsize)
3050 int opt = OPT_Wstringop_overflow_;
3052 /* The size of the largest object is half the address space, or
3053 PTRDIFF_MAX. (This is way too permissive.) */
3054 tree maxobjsize = max_object_size ();
3056 /* Either the length of the source string for string functions or
3057 the size of the source object for raw memory functions. */
3058 tree slen = NULL_TREE;
3060 tree range[2] = { NULL_TREE, NULL_TREE };
3062 /* Set to true when the exact number of bytes written by a string
3063 function like strcpy is not known and the only thing that is
3064 known is that it must be at least one (for the terminating nul). */
3065 bool at_least_one = false;
3066 if (srcstr)
3068 /* SRCSTR is normally a pointer to string but as a special case
3069 it can be an integer denoting the length of a string. */
3070 if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
3072 /* Try to determine the range of lengths the source string
3073 refers to. If it can be determined and is less than
3074 the upper bound given by MAXREAD add one to it for
3075 the terminating nul. Otherwise, set it to one for
3076 the same reason, or to MAXREAD as appropriate. */
3077 get_range_strlen (srcstr, range);
3078 if (range[0] && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
3080 if (maxread && tree_int_cst_le (maxread, range[0]))
3081 range[0] = range[1] = maxread;
3082 else
3083 range[0] = fold_build2 (PLUS_EXPR, size_type_node,
3084 range[0], size_one_node);
3086 if (maxread && tree_int_cst_le (maxread, range[1]))
3087 range[1] = maxread;
3088 else if (!integer_all_onesp (range[1]))
3089 range[1] = fold_build2 (PLUS_EXPR, size_type_node,
3090 range[1], size_one_node);
3092 slen = range[0];
3094 else
3096 at_least_one = true;
3097 slen = size_one_node;
3100 else
3101 slen = srcstr;
3104 if (!dstwrite && !maxread)
3106 /* When the only available piece of data is the object size
3107 there is nothing to do. */
3108 if (!slen)
3109 return true;
3111 /* Otherwise, when the length of the source sequence is known
3112 (as with strlen), set DSTWRITE to it. */
3113 if (!range[0])
3114 dstwrite = slen;
3117 if (!dstsize)
3118 dstsize = maxobjsize;
3120 if (dstwrite)
3121 get_size_range (dstwrite, range);
3123 tree func = get_callee_fndecl (exp);
3125 /* First check the number of bytes to be written against the maximum
3126 object size. */
3127 if (range[0] && tree_int_cst_lt (maxobjsize, range[0]))
3129 location_t loc = tree_nonartificial_location (exp);
3130 loc = expansion_point_location_if_in_system_header (loc);
3132 if (range[0] == range[1])
3133 warning_at (loc, opt,
3134 "%K%qD specified size %E "
3135 "exceeds maximum object size %E",
3136 exp, func, range[0], maxobjsize);
3137 else
3138 warning_at (loc, opt,
3139 "%K%qD specified size between %E and %E "
3140 "exceeds maximum object size %E",
3141 exp, func,
3142 range[0], range[1], maxobjsize);
3143 return false;
3146 /* The number of bytes to write is "exact" if DSTWRITE is non-null,
3147 constant, and in range of unsigned HOST_WIDE_INT. */
3148 bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite);
3150 /* Next check the number of bytes to be written against the destination
3151 object size. */
3152 if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
3154 if (range[0]
3155 && ((tree_fits_uhwi_p (dstsize)
3156 && tree_int_cst_lt (dstsize, range[0]))
3157 || (tree_fits_uhwi_p (dstwrite)
3158 && tree_int_cst_lt (dstwrite, range[0]))))
3160 if (TREE_NO_WARNING (exp))
3161 return false;
3163 location_t loc = tree_nonartificial_location (exp);
3164 loc = expansion_point_location_if_in_system_header (loc);
3166 if (dstwrite == slen && at_least_one)
3168 /* This is a call to strcpy with a destination of 0 size
3169 and a source of unknown length. The call will write
3170 at least one byte past the end of the destination. */
3171 warning_at (loc, opt,
3172 "%K%qD writing %E or more bytes into a region "
3173 "of size %E overflows the destination",
3174 exp, func, range[0], dstsize);
3176 else if (tree_int_cst_equal (range[0], range[1]))
3177 warning_n (loc, opt, tree_to_uhwi (range[0]),
3178 "%K%qD writing %E byte into a region "
3179 "of size %E overflows the destination",
3180 "%K%qD writing %E bytes into a region "
3181 "of size %E overflows the destination",
3182 exp, func, range[0], dstsize);
3183 else if (tree_int_cst_sign_bit (range[1]))
3185 /* Avoid printing the upper bound if it's invalid. */
3186 warning_at (loc, opt,
3187 "%K%qD writing %E or more bytes into a region "
3188 "of size %E overflows the destination",
3189 exp, func, range[0], dstsize);
3191 else
3192 warning_at (loc, opt,
3193 "%K%qD writing between %E and %E bytes into "
3194 "a region of size %E overflows the destination",
3195 exp, func, range[0], range[1],
3196 dstsize);
3198 /* Return error when an overflow has been detected. */
3199 return false;
3203 /* Check the maximum length of the source sequence against the size
3204 of the destination object if known, or against the maximum size
3205 of an object. */
3206 if (maxread)
3208 get_size_range (maxread, range);
3210 /* Use the lower end for MAXREAD from now on. */
3211 if (range[0])
3212 maxread = range[0];
3214 if (range[0] && dstsize && tree_fits_uhwi_p (dstsize))
3216 location_t loc = tree_nonartificial_location (exp);
3217 loc = expansion_point_location_if_in_system_header (loc);
3219 if (tree_int_cst_lt (maxobjsize, range[0]))
3221 if (TREE_NO_WARNING (exp))
3222 return false;
3224 /* Warn about crazy big sizes first since that's more
3225 likely to be meaningful than saying that the bound
3226 is greater than the object size if both are big. */
3227 if (range[0] == range[1])
3228 warning_at (loc, opt,
3229 "%K%qD specified bound %E "
3230 "exceeds maximum object size %E",
3231 exp, func,
3232 range[0], maxobjsize);
3233 else
3234 warning_at (loc, opt,
3235 "%K%qD specified bound between %E and %E "
3236 "exceeds maximum object size %E",
3237 exp, func,
3238 range[0], range[1], maxobjsize);
3240 return false;
3243 if (dstsize != maxobjsize && tree_int_cst_lt (dstsize, range[0]))
3245 if (TREE_NO_WARNING (exp))
3246 return false;
3248 if (tree_int_cst_equal (range[0], range[1]))
3249 warning_at (loc, opt,
3250 "%K%qD specified bound %E "
3251 "exceeds destination size %E",
3252 exp, func,
3253 range[0], dstsize);
3254 else
3255 warning_at (loc, opt,
3256 "%K%qD specified bound between %E and %E "
3257 "exceeds destination size %E",
3258 exp, func,
3259 range[0], range[1], dstsize);
3260 return false;
3265 /* Check for reading past the end of SRC. */
3266 if (slen
3267 && slen == srcstr
3268 && dstwrite && range[0]
3269 && tree_int_cst_lt (slen, range[0]))
3271 if (TREE_NO_WARNING (exp))
3272 return false;
3274 location_t loc = tree_nonartificial_location (exp);
3276 if (tree_int_cst_equal (range[0], range[1]))
3277 warning_n (loc, opt, tree_to_uhwi (range[0]),
3278 "%K%qD reading %E byte from a region of size %E",
3279 "%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 (TREE_OPERAND (dest, 0)))
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 enum block_op_methods method = BLOCK_OP_NORMAL;
3656 if (CALL_EXPR_TAILCALL (exp) && (endp == 0 || target == const0_rtx))
3657 method = BLOCK_OP_TAILCALL;
3658 if (endp == 1 && target != const0_rtx)
3659 method = BLOCK_OP_NO_LIBCALL_RET;
3660 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3661 expected_align, expected_size,
3662 min_size, max_size, probable_max_size);
3663 if (dest_addr == pc_rtx)
3664 return NULL_RTX;
3666 if (dest_addr == 0)
3668 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3669 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3672 if (endp && target != const0_rtx)
3674 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3675 /* stpcpy pointer to last byte. */
3676 if (endp == 2)
3677 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3680 return dest_addr;
3683 static rtx
3684 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3685 rtx target, tree orig_exp, int endp)
3687 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3688 endp);
3691 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3692 we failed, the caller should emit a normal call, otherwise try to
3693 get the result in TARGET, if convenient. If ENDP is 0 return the
3694 destination pointer, if ENDP is 1 return the end pointer ala
3695 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3696 stpcpy. */
3698 static rtx
3699 expand_movstr (tree dest, tree src, rtx target, int endp)
3701 struct expand_operand ops[3];
3702 rtx dest_mem;
3703 rtx src_mem;
3705 if (!targetm.have_movstr ())
3706 return NULL_RTX;
3708 dest_mem = get_memory_rtx (dest, NULL);
3709 src_mem = get_memory_rtx (src, NULL);
3710 if (!endp)
3712 target = force_reg (Pmode, XEXP (dest_mem, 0));
3713 dest_mem = replace_equiv_address (dest_mem, target);
3716 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3717 create_fixed_operand (&ops[1], dest_mem);
3718 create_fixed_operand (&ops[2], src_mem);
3719 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3720 return NULL_RTX;
3722 if (endp && target != const0_rtx)
3724 target = ops[0].value;
3725 /* movstr is supposed to set end to the address of the NUL
3726 terminator. If the caller requested a mempcpy-like return value,
3727 adjust it. */
3728 if (endp == 1)
3730 rtx tem = plus_constant (GET_MODE (target),
3731 gen_lowpart (GET_MODE (target), target), 1);
3732 emit_move_insn (target, force_operand (tem, NULL_RTX));
3735 return target;
3738 /* Do some very basic size validation of a call to the strcpy builtin
3739 given by EXP. Return NULL_RTX to have the built-in expand to a call
3740 to the library function. */
3742 static rtx
3743 expand_builtin_strcat (tree exp, rtx)
3745 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
3746 || !warn_stringop_overflow)
3747 return NULL_RTX;
3749 tree dest = CALL_EXPR_ARG (exp, 0);
3750 tree src = CALL_EXPR_ARG (exp, 1);
3752 /* There is no way here to determine the length of the string in
3753 the destination to which the SRC string is being appended so
3754 just diagnose cases when the souce string is longer than
3755 the destination object. */
3757 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3759 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, src,
3760 destsize);
3762 return NULL_RTX;
3765 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3766 NULL_RTX if we failed the caller should emit a normal call, otherwise
3767 try to get the result in TARGET, if convenient (and in mode MODE if that's
3768 convenient). */
3770 static rtx
3771 expand_builtin_strcpy (tree exp, rtx target)
3773 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3774 return NULL_RTX;
3776 tree dest = CALL_EXPR_ARG (exp, 0);
3777 tree src = CALL_EXPR_ARG (exp, 1);
3779 if (warn_stringop_overflow)
3781 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3782 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3783 src, destsize);
3786 return expand_builtin_strcpy_args (dest, src, target);
3789 /* Helper function to do the actual work for expand_builtin_strcpy. The
3790 arguments to the builtin_strcpy call DEST and SRC are broken out
3791 so that this can also be called without constructing an actual CALL_EXPR.
3792 The other arguments and return value are the same as for
3793 expand_builtin_strcpy. */
3795 static rtx
3796 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3798 return expand_movstr (dest, src, target, /*endp=*/0);
3801 /* Expand a call EXP to the stpcpy builtin.
3802 Return NULL_RTX if we failed the caller should emit a normal call,
3803 otherwise try to get the result in TARGET, if convenient (and in
3804 mode MODE if that's convenient). */
3806 static rtx
3807 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3809 tree dst, src;
3810 location_t loc = EXPR_LOCATION (exp);
3812 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3813 return NULL_RTX;
3815 dst = CALL_EXPR_ARG (exp, 0);
3816 src = CALL_EXPR_ARG (exp, 1);
3818 if (warn_stringop_overflow)
3820 tree destsize = compute_objsize (dst, warn_stringop_overflow - 1);
3821 check_access (exp, dst, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3822 src, destsize);
3825 /* If return value is ignored, transform stpcpy into strcpy. */
3826 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3828 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3829 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3830 return expand_expr (result, target, mode, EXPAND_NORMAL);
3832 else
3834 tree len, lenp1;
3835 rtx ret;
3837 /* Ensure we get an actual string whose length can be evaluated at
3838 compile-time, not an expression containing a string. This is
3839 because the latter will potentially produce pessimized code
3840 when used to produce the return value. */
3841 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3842 return expand_movstr (dst, src, target, /*endp=*/2);
3844 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3845 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3846 target, exp, /*endp=*/2);
3848 if (ret)
3849 return ret;
3851 if (TREE_CODE (len) == INTEGER_CST)
3853 rtx len_rtx = expand_normal (len);
3855 if (CONST_INT_P (len_rtx))
3857 ret = expand_builtin_strcpy_args (dst, src, target);
3859 if (ret)
3861 if (! target)
3863 if (mode != VOIDmode)
3864 target = gen_reg_rtx (mode);
3865 else
3866 target = gen_reg_rtx (GET_MODE (ret));
3868 if (GET_MODE (target) != GET_MODE (ret))
3869 ret = gen_lowpart (GET_MODE (target), ret);
3871 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3872 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3873 gcc_assert (ret);
3875 return target;
3880 return expand_movstr (dst, src, target, /*endp=*/2);
3884 /* Check a call EXP to the stpncpy built-in for validity.
3885 Return NULL_RTX on both success and failure. */
3887 static rtx
3888 expand_builtin_stpncpy (tree exp, rtx)
3890 if (!validate_arglist (exp,
3891 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3892 || !warn_stringop_overflow)
3893 return NULL_RTX;
3895 /* The source and destination of the call. */
3896 tree dest = CALL_EXPR_ARG (exp, 0);
3897 tree src = CALL_EXPR_ARG (exp, 1);
3899 /* The exact number of bytes to write (not the maximum). */
3900 tree len = CALL_EXPR_ARG (exp, 2);
3902 /* The size of the destination object. */
3903 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3905 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src, destsize);
3907 return NULL_RTX;
3910 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3911 bytes from constant string DATA + OFFSET and return it as target
3912 constant. */
3915 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3916 scalar_int_mode mode)
3918 const char *str = (const char *) data;
3920 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3921 return const0_rtx;
3923 return c_readstr (str + offset, mode);
3926 /* Helper to check the sizes of sequences and the destination of calls
3927 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3928 success (no overflow or invalid sizes), false otherwise. */
3930 static bool
3931 check_strncat_sizes (tree exp, tree objsize)
3933 tree dest = CALL_EXPR_ARG (exp, 0);
3934 tree src = CALL_EXPR_ARG (exp, 1);
3935 tree maxread = CALL_EXPR_ARG (exp, 2);
3937 /* Try to determine the range of lengths that the source expression
3938 refers to. */
3939 tree lenrange[2];
3940 get_range_strlen (src, lenrange);
3942 /* Try to verify that the destination is big enough for the shortest
3943 string. */
3945 if (!objsize && warn_stringop_overflow)
3947 /* If it hasn't been provided by __strncat_chk, try to determine
3948 the size of the destination object into which the source is
3949 being copied. */
3950 objsize = compute_objsize (dest, warn_stringop_overflow - 1);
3953 /* Add one for the terminating nul. */
3954 tree srclen = (lenrange[0]
3955 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3956 size_one_node)
3957 : NULL_TREE);
3959 /* The strncat function copies at most MAXREAD bytes and always appends
3960 the terminating nul so the specified upper bound should never be equal
3961 to (or greater than) the size of the destination. */
3962 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
3963 && tree_int_cst_equal (objsize, maxread))
3965 location_t loc = tree_nonartificial_location (exp);
3966 loc = expansion_point_location_if_in_system_header (loc);
3968 warning_at (loc, OPT_Wstringop_overflow_,
3969 "%K%qD specified bound %E equals destination size",
3970 exp, get_callee_fndecl (exp), maxread);
3972 return false;
3975 if (!srclen
3976 || (maxread && tree_fits_uhwi_p (maxread)
3977 && tree_fits_uhwi_p (srclen)
3978 && tree_int_cst_lt (maxread, srclen)))
3979 srclen = maxread;
3981 /* The number of bytes to write is LEN but check_access will also
3982 check SRCLEN if LEN's value isn't known. */
3983 return check_access (exp, dest, src, /*size=*/NULL_TREE, maxread, srclen,
3984 objsize);
3987 /* Similar to expand_builtin_strcat, do some very basic size validation
3988 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
3989 the built-in expand to a call to the library function. */
3991 static rtx
3992 expand_builtin_strncat (tree exp, rtx)
3994 if (!validate_arglist (exp,
3995 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3996 || !warn_stringop_overflow)
3997 return NULL_RTX;
3999 tree dest = CALL_EXPR_ARG (exp, 0);
4000 tree src = CALL_EXPR_ARG (exp, 1);
4001 /* The upper bound on the number of bytes to write. */
4002 tree maxread = CALL_EXPR_ARG (exp, 2);
4003 /* The length of the source sequence. */
4004 tree slen = c_strlen (src, 1);
4006 /* Try to determine the range of lengths that the source expression
4007 refers to. */
4008 tree lenrange[2];
4009 if (slen)
4010 lenrange[0] = lenrange[1] = slen;
4011 else
4012 get_range_strlen (src, lenrange);
4014 /* Try to verify that the destination is big enough for the shortest
4015 string. First try to determine the size of the destination object
4016 into which the source is being copied. */
4017 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
4019 /* Add one for the terminating nul. */
4020 tree srclen = (lenrange[0]
4021 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
4022 size_one_node)
4023 : NULL_TREE);
4025 /* The strncat function copies at most MAXREAD bytes and always appends
4026 the terminating nul so the specified upper bound should never be equal
4027 to (or greater than) the size of the destination. */
4028 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize)
4029 && tree_int_cst_equal (destsize, maxread))
4031 location_t loc = tree_nonartificial_location (exp);
4032 loc = expansion_point_location_if_in_system_header (loc);
4034 warning_at (loc, OPT_Wstringop_overflow_,
4035 "%K%qD specified bound %E equals destination size",
4036 exp, get_callee_fndecl (exp), maxread);
4038 return NULL_RTX;
4041 if (!srclen
4042 || (maxread && tree_fits_uhwi_p (maxread)
4043 && tree_fits_uhwi_p (srclen)
4044 && tree_int_cst_lt (maxread, srclen)))
4045 srclen = maxread;
4047 /* The number of bytes to write is SRCLEN. */
4048 check_access (exp, dest, src, NULL_TREE, maxread, srclen, destsize);
4050 return NULL_RTX;
4053 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4054 NULL_RTX if we failed the caller should emit a normal call. */
4056 static rtx
4057 expand_builtin_strncpy (tree exp, rtx target)
4059 location_t loc = EXPR_LOCATION (exp);
4061 if (validate_arglist (exp,
4062 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4064 tree dest = CALL_EXPR_ARG (exp, 0);
4065 tree src = CALL_EXPR_ARG (exp, 1);
4066 /* The number of bytes to write (not the maximum). */
4067 tree len = CALL_EXPR_ARG (exp, 2);
4068 /* The length of the source sequence. */
4069 tree slen = c_strlen (src, 1);
4071 if (warn_stringop_overflow)
4073 tree destsize = compute_objsize (dest,
4074 warn_stringop_overflow - 1);
4076 /* The number of bytes to write is LEN but check_access will also
4077 check SLEN if LEN's value isn't known. */
4078 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src,
4079 destsize);
4082 /* We must be passed a constant len and src parameter. */
4083 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4084 return NULL_RTX;
4086 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4088 /* We're required to pad with trailing zeros if the requested
4089 len is greater than strlen(s2)+1. In that case try to
4090 use store_by_pieces, if it fails, punt. */
4091 if (tree_int_cst_lt (slen, len))
4093 unsigned int dest_align = get_pointer_alignment (dest);
4094 const char *p = c_getstr (src);
4095 rtx dest_mem;
4097 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4098 || !can_store_by_pieces (tree_to_uhwi (len),
4099 builtin_strncpy_read_str,
4100 CONST_CAST (char *, p),
4101 dest_align, false))
4102 return NULL_RTX;
4104 dest_mem = get_memory_rtx (dest, len);
4105 store_by_pieces (dest_mem, tree_to_uhwi (len),
4106 builtin_strncpy_read_str,
4107 CONST_CAST (char *, p), dest_align, false, 0);
4108 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4109 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4110 return dest_mem;
4113 return NULL_RTX;
4116 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4117 bytes from constant string DATA + OFFSET and return it as target
4118 constant. */
4121 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4122 scalar_int_mode mode)
4124 const char *c = (const char *) data;
4125 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
4127 memset (p, *c, GET_MODE_SIZE (mode));
4129 return c_readstr (p, mode);
4132 /* Callback routine for store_by_pieces. Return the RTL of a register
4133 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4134 char value given in the RTL register data. For example, if mode is
4135 4 bytes wide, return the RTL for 0x01010101*data. */
4137 static rtx
4138 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4139 scalar_int_mode mode)
4141 rtx target, coeff;
4142 size_t size;
4143 char *p;
4145 size = GET_MODE_SIZE (mode);
4146 if (size == 1)
4147 return (rtx) data;
4149 p = XALLOCAVEC (char, size);
4150 memset (p, 1, size);
4151 coeff = c_readstr (p, mode);
4153 target = convert_to_mode (mode, (rtx) data, 1);
4154 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4155 return force_reg (mode, target);
4158 /* Expand expression EXP, which is a call to the memset builtin. Return
4159 NULL_RTX if we failed the caller should emit a normal call, otherwise
4160 try to get the result in TARGET, if convenient (and in mode MODE if that's
4161 convenient). */
4163 static rtx
4164 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4166 if (!validate_arglist (exp,
4167 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4168 return NULL_RTX;
4170 tree dest = CALL_EXPR_ARG (exp, 0);
4171 tree val = CALL_EXPR_ARG (exp, 1);
4172 tree len = CALL_EXPR_ARG (exp, 2);
4174 check_memop_access (exp, dest, NULL_TREE, len);
4176 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4179 /* Expand expression EXP, which is an instrumented call to the memset builtin.
4180 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
4181 try to get the result in TARGET, if convenient (and in mode MODE if that's
4182 convenient). */
4184 static rtx
4185 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
4187 if (!validate_arglist (exp,
4188 POINTER_TYPE, POINTER_BOUNDS_TYPE,
4189 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4190 return NULL_RTX;
4191 else
4193 tree dest = CALL_EXPR_ARG (exp, 0);
4194 tree val = CALL_EXPR_ARG (exp, 2);
4195 tree len = CALL_EXPR_ARG (exp, 3);
4196 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
4198 /* Return src bounds with the result. */
4199 if (res)
4201 rtx bnd = force_reg (targetm.chkp_bound_mode (),
4202 expand_normal (CALL_EXPR_ARG (exp, 1)));
4203 res = chkp_join_splitted_slot (res, bnd);
4205 return res;
4209 /* Helper function to do the actual work for expand_builtin_memset. The
4210 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4211 so that this can also be called without constructing an actual CALL_EXPR.
4212 The other arguments and return value are the same as for
4213 expand_builtin_memset. */
4215 static rtx
4216 expand_builtin_memset_args (tree dest, tree val, tree len,
4217 rtx target, machine_mode mode, tree orig_exp)
4219 tree fndecl, fn;
4220 enum built_in_function fcode;
4221 machine_mode val_mode;
4222 char c;
4223 unsigned int dest_align;
4224 rtx dest_mem, dest_addr, len_rtx;
4225 HOST_WIDE_INT expected_size = -1;
4226 unsigned int expected_align = 0;
4227 unsigned HOST_WIDE_INT min_size;
4228 unsigned HOST_WIDE_INT max_size;
4229 unsigned HOST_WIDE_INT probable_max_size;
4231 dest_align = get_pointer_alignment (dest);
4233 /* If DEST is not a pointer type, don't do this operation in-line. */
4234 if (dest_align == 0)
4235 return NULL_RTX;
4237 if (currently_expanding_gimple_stmt)
4238 stringop_block_profile (currently_expanding_gimple_stmt,
4239 &expected_align, &expected_size);
4241 if (expected_align < dest_align)
4242 expected_align = dest_align;
4244 /* If the LEN parameter is zero, return DEST. */
4245 if (integer_zerop (len))
4247 /* Evaluate and ignore VAL in case it has side-effects. */
4248 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4249 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4252 /* Stabilize the arguments in case we fail. */
4253 dest = builtin_save_expr (dest);
4254 val = builtin_save_expr (val);
4255 len = builtin_save_expr (len);
4257 len_rtx = expand_normal (len);
4258 determine_block_size (len, len_rtx, &min_size, &max_size,
4259 &probable_max_size);
4260 dest_mem = get_memory_rtx (dest, len);
4261 val_mode = TYPE_MODE (unsigned_char_type_node);
4263 if (TREE_CODE (val) != INTEGER_CST)
4265 rtx val_rtx;
4267 val_rtx = expand_normal (val);
4268 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4270 /* Assume that we can memset by pieces if we can store
4271 * the coefficients by pieces (in the required modes).
4272 * We can't pass builtin_memset_gen_str as that emits RTL. */
4273 c = 1;
4274 if (tree_fits_uhwi_p (len)
4275 && can_store_by_pieces (tree_to_uhwi (len),
4276 builtin_memset_read_str, &c, dest_align,
4277 true))
4279 val_rtx = force_reg (val_mode, val_rtx);
4280 store_by_pieces (dest_mem, tree_to_uhwi (len),
4281 builtin_memset_gen_str, val_rtx, dest_align,
4282 true, 0);
4284 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4285 dest_align, expected_align,
4286 expected_size, min_size, max_size,
4287 probable_max_size))
4288 goto do_libcall;
4290 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4291 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4292 return dest_mem;
4295 if (target_char_cast (val, &c))
4296 goto do_libcall;
4298 if (c)
4300 if (tree_fits_uhwi_p (len)
4301 && can_store_by_pieces (tree_to_uhwi (len),
4302 builtin_memset_read_str, &c, dest_align,
4303 true))
4304 store_by_pieces (dest_mem, tree_to_uhwi (len),
4305 builtin_memset_read_str, &c, dest_align, true, 0);
4306 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4307 gen_int_mode (c, val_mode),
4308 dest_align, expected_align,
4309 expected_size, min_size, max_size,
4310 probable_max_size))
4311 goto do_libcall;
4313 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4314 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4315 return dest_mem;
4318 set_mem_align (dest_mem, dest_align);
4319 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4320 CALL_EXPR_TAILCALL (orig_exp)
4321 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4322 expected_align, expected_size,
4323 min_size, max_size,
4324 probable_max_size);
4326 if (dest_addr == 0)
4328 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4329 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4332 return dest_addr;
4334 do_libcall:
4335 fndecl = get_callee_fndecl (orig_exp);
4336 fcode = DECL_FUNCTION_CODE (fndecl);
4337 if (fcode == BUILT_IN_MEMSET
4338 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
4339 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4340 dest, val, len);
4341 else if (fcode == BUILT_IN_BZERO)
4342 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4343 dest, len);
4344 else
4345 gcc_unreachable ();
4346 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4347 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4348 return expand_call (fn, target, target == const0_rtx);
4351 /* Expand expression EXP, which is a call to the bzero builtin. Return
4352 NULL_RTX if we failed the caller should emit a normal call. */
4354 static rtx
4355 expand_builtin_bzero (tree exp)
4357 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4358 return NULL_RTX;
4360 tree dest = CALL_EXPR_ARG (exp, 0);
4361 tree size = CALL_EXPR_ARG (exp, 1);
4363 check_memop_access (exp, dest, NULL_TREE, size);
4365 /* New argument list transforming bzero(ptr x, int y) to
4366 memset(ptr x, int 0, size_t y). This is done this way
4367 so that if it isn't expanded inline, we fallback to
4368 calling bzero instead of memset. */
4370 location_t loc = EXPR_LOCATION (exp);
4372 return expand_builtin_memset_args (dest, integer_zero_node,
4373 fold_convert_loc (loc,
4374 size_type_node, size),
4375 const0_rtx, VOIDmode, exp);
4378 /* Try to expand cmpstr operation ICODE with the given operands.
4379 Return the result rtx on success, otherwise return null. */
4381 static rtx
4382 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4383 HOST_WIDE_INT align)
4385 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4387 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4388 target = NULL_RTX;
4390 struct expand_operand ops[4];
4391 create_output_operand (&ops[0], target, insn_mode);
4392 create_fixed_operand (&ops[1], arg1_rtx);
4393 create_fixed_operand (&ops[2], arg2_rtx);
4394 create_integer_operand (&ops[3], align);
4395 if (maybe_expand_insn (icode, 4, ops))
4396 return ops[0].value;
4397 return NULL_RTX;
4400 /* Expand expression EXP, which is a call to the memcmp built-in function.
4401 Return NULL_RTX if we failed and the caller should emit a normal call,
4402 otherwise try to get the result in TARGET, if convenient.
4403 RESULT_EQ is true if we can relax the returned value to be either zero
4404 or nonzero, without caring about the sign. */
4406 static rtx
4407 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4409 if (!validate_arglist (exp,
4410 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4411 return NULL_RTX;
4413 tree arg1 = CALL_EXPR_ARG (exp, 0);
4414 tree arg2 = CALL_EXPR_ARG (exp, 1);
4415 tree len = CALL_EXPR_ARG (exp, 2);
4417 /* Diagnose calls where the specified length exceeds the size of either
4418 object. */
4419 if (warn_stringop_overflow)
4421 tree size = compute_objsize (arg1, 0);
4422 if (check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4423 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE))
4425 size = compute_objsize (arg2, 0);
4426 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4427 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE);
4431 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4432 location_t loc = EXPR_LOCATION (exp);
4434 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4435 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4437 /* If we don't have POINTER_TYPE, call the function. */
4438 if (arg1_align == 0 || arg2_align == 0)
4439 return NULL_RTX;
4441 rtx arg1_rtx = get_memory_rtx (arg1, len);
4442 rtx arg2_rtx = get_memory_rtx (arg2, len);
4443 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4445 /* Set MEM_SIZE as appropriate. */
4446 if (CONST_INT_P (len_rtx))
4448 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4449 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4452 by_pieces_constfn constfn = NULL;
4454 const char *src_str = c_getstr (arg2);
4455 if (result_eq && src_str == NULL)
4457 src_str = c_getstr (arg1);
4458 if (src_str != NULL)
4459 std::swap (arg1_rtx, arg2_rtx);
4462 /* If SRC is a string constant and block move would be done
4463 by pieces, we can avoid loading the string from memory
4464 and only stored the computed constants. */
4465 if (src_str
4466 && CONST_INT_P (len_rtx)
4467 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1)
4468 constfn = builtin_memcpy_read_str;
4470 rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4471 TREE_TYPE (len), target,
4472 result_eq, constfn,
4473 CONST_CAST (char *, src_str));
4475 if (result)
4477 /* Return the value in the proper mode for this function. */
4478 if (GET_MODE (result) == mode)
4479 return result;
4481 if (target != 0)
4483 convert_move (target, result, 0);
4484 return target;
4487 return convert_to_mode (mode, result, 0);
4490 return NULL_RTX;
4493 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4494 if we failed the caller should emit a normal call, otherwise try to get
4495 the result in TARGET, if convenient. */
4497 static rtx
4498 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4500 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4501 return NULL_RTX;
4503 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4504 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4505 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4506 return NULL_RTX;
4508 tree arg1 = CALL_EXPR_ARG (exp, 0);
4509 tree arg2 = CALL_EXPR_ARG (exp, 1);
4511 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4512 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4514 /* If we don't have POINTER_TYPE, call the function. */
4515 if (arg1_align == 0 || arg2_align == 0)
4516 return NULL_RTX;
4518 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4519 arg1 = builtin_save_expr (arg1);
4520 arg2 = builtin_save_expr (arg2);
4522 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4523 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4525 rtx result = NULL_RTX;
4526 /* Try to call cmpstrsi. */
4527 if (cmpstr_icode != CODE_FOR_nothing)
4528 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4529 MIN (arg1_align, arg2_align));
4531 /* Try to determine at least one length and call cmpstrnsi. */
4532 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4534 tree len;
4535 rtx arg3_rtx;
4537 tree len1 = c_strlen (arg1, 1);
4538 tree len2 = c_strlen (arg2, 1);
4540 if (len1)
4541 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4542 if (len2)
4543 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4545 /* If we don't have a constant length for the first, use the length
4546 of the second, if we know it. We don't require a constant for
4547 this case; some cost analysis could be done if both are available
4548 but neither is constant. For now, assume they're equally cheap,
4549 unless one has side effects. If both strings have constant lengths,
4550 use the smaller. */
4552 if (!len1)
4553 len = len2;
4554 else if (!len2)
4555 len = len1;
4556 else if (TREE_SIDE_EFFECTS (len1))
4557 len = len2;
4558 else if (TREE_SIDE_EFFECTS (len2))
4559 len = len1;
4560 else if (TREE_CODE (len1) != INTEGER_CST)
4561 len = len2;
4562 else if (TREE_CODE (len2) != INTEGER_CST)
4563 len = len1;
4564 else if (tree_int_cst_lt (len1, len2))
4565 len = len1;
4566 else
4567 len = len2;
4569 /* If both arguments have side effects, we cannot optimize. */
4570 if (len && !TREE_SIDE_EFFECTS (len))
4572 arg3_rtx = expand_normal (len);
4573 result = expand_cmpstrn_or_cmpmem
4574 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4575 arg3_rtx, MIN (arg1_align, arg2_align));
4579 /* Check to see if the argument was declared attribute nonstring
4580 and if so, issue a warning since at this point it's not known
4581 to be nul-terminated. */
4582 tree fndecl = get_callee_fndecl (exp);
4583 maybe_warn_nonstring_arg (fndecl, exp);
4585 if (result)
4587 /* Return the value in the proper mode for this function. */
4588 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4589 if (GET_MODE (result) == mode)
4590 return result;
4591 if (target == 0)
4592 return convert_to_mode (mode, result, 0);
4593 convert_move (target, result, 0);
4594 return target;
4597 /* Expand the library call ourselves using a stabilized argument
4598 list to avoid re-evaluating the function's arguments twice. */
4599 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4600 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4601 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4602 return expand_call (fn, target, target == const0_rtx);
4605 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4606 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4607 the result in TARGET, if convenient. */
4609 static rtx
4610 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4611 ATTRIBUTE_UNUSED machine_mode mode)
4613 if (!validate_arglist (exp,
4614 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4615 return NULL_RTX;
4617 /* If c_strlen can determine an expression for one of the string
4618 lengths, and it doesn't have side effects, then emit cmpstrnsi
4619 using length MIN(strlen(string)+1, arg3). */
4620 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4621 if (cmpstrn_icode == CODE_FOR_nothing)
4622 return NULL_RTX;
4624 tree len;
4626 tree arg1 = CALL_EXPR_ARG (exp, 0);
4627 tree arg2 = CALL_EXPR_ARG (exp, 1);
4628 tree arg3 = CALL_EXPR_ARG (exp, 2);
4630 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4631 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4633 tree len1 = c_strlen (arg1, 1);
4634 tree len2 = c_strlen (arg2, 1);
4636 location_t loc = EXPR_LOCATION (exp);
4638 if (len1)
4639 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4640 if (len2)
4641 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4643 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4645 /* If we don't have a constant length for the first, use the length
4646 of the second, if we know it. If neither string is constant length,
4647 use the given length argument. We don't require a constant for
4648 this case; some cost analysis could be done if both are available
4649 but neither is constant. For now, assume they're equally cheap,
4650 unless one has side effects. If both strings have constant lengths,
4651 use the smaller. */
4653 if (!len1 && !len2)
4654 len = len3;
4655 else if (!len1)
4656 len = len2;
4657 else if (!len2)
4658 len = len1;
4659 else if (TREE_SIDE_EFFECTS (len1))
4660 len = len2;
4661 else if (TREE_SIDE_EFFECTS (len2))
4662 len = len1;
4663 else if (TREE_CODE (len1) != INTEGER_CST)
4664 len = len2;
4665 else if (TREE_CODE (len2) != INTEGER_CST)
4666 len = len1;
4667 else if (tree_int_cst_lt (len1, len2))
4668 len = len1;
4669 else
4670 len = len2;
4672 /* If we are not using the given length, we must incorporate it here.
4673 The actual new length parameter will be MIN(len,arg3) in this case. */
4674 if (len != len3)
4675 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4676 rtx arg1_rtx = get_memory_rtx (arg1, len);
4677 rtx arg2_rtx = get_memory_rtx (arg2, len);
4678 rtx arg3_rtx = expand_normal (len);
4679 rtx result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4680 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4681 MIN (arg1_align, arg2_align));
4683 /* Check to see if the argument was declared attribute nonstring
4684 and if so, issue a warning since at this point it's not known
4685 to be nul-terminated. */
4686 tree fndecl = get_callee_fndecl (exp);
4687 maybe_warn_nonstring_arg (fndecl, exp);
4689 if (result)
4691 /* Return the value in the proper mode for this function. */
4692 mode = TYPE_MODE (TREE_TYPE (exp));
4693 if (GET_MODE (result) == mode)
4694 return result;
4695 if (target == 0)
4696 return convert_to_mode (mode, result, 0);
4697 convert_move (target, result, 0);
4698 return target;
4701 /* Expand the library call ourselves using a stabilized argument
4702 list to avoid re-evaluating the function's arguments twice. */
4703 tree fn = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4704 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4705 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4706 return expand_call (fn, target, target == const0_rtx);
4709 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4710 if that's convenient. */
4713 expand_builtin_saveregs (void)
4715 rtx val;
4716 rtx_insn *seq;
4718 /* Don't do __builtin_saveregs more than once in a function.
4719 Save the result of the first call and reuse it. */
4720 if (saveregs_value != 0)
4721 return saveregs_value;
4723 /* When this function is called, it means that registers must be
4724 saved on entry to this function. So we migrate the call to the
4725 first insn of this function. */
4727 start_sequence ();
4729 /* Do whatever the machine needs done in this case. */
4730 val = targetm.calls.expand_builtin_saveregs ();
4732 seq = get_insns ();
4733 end_sequence ();
4735 saveregs_value = val;
4737 /* Put the insns after the NOTE that starts the function. If this
4738 is inside a start_sequence, make the outer-level insn chain current, so
4739 the code is placed at the start of the function. */
4740 push_topmost_sequence ();
4741 emit_insn_after (seq, entry_of_function ());
4742 pop_topmost_sequence ();
4744 return val;
4747 /* Expand a call to __builtin_next_arg. */
4749 static rtx
4750 expand_builtin_next_arg (void)
4752 /* Checking arguments is already done in fold_builtin_next_arg
4753 that must be called before this function. */
4754 return expand_binop (ptr_mode, add_optab,
4755 crtl->args.internal_arg_pointer,
4756 crtl->args.arg_offset_rtx,
4757 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4760 /* Make it easier for the backends by protecting the valist argument
4761 from multiple evaluations. */
4763 static tree
4764 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4766 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4768 /* The current way of determining the type of valist is completely
4769 bogus. We should have the information on the va builtin instead. */
4770 if (!vatype)
4771 vatype = targetm.fn_abi_va_list (cfun->decl);
4773 if (TREE_CODE (vatype) == ARRAY_TYPE)
4775 if (TREE_SIDE_EFFECTS (valist))
4776 valist = save_expr (valist);
4778 /* For this case, the backends will be expecting a pointer to
4779 vatype, but it's possible we've actually been given an array
4780 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4781 So fix it. */
4782 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4784 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4785 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4788 else
4790 tree pt = build_pointer_type (vatype);
4792 if (! needs_lvalue)
4794 if (! TREE_SIDE_EFFECTS (valist))
4795 return valist;
4797 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4798 TREE_SIDE_EFFECTS (valist) = 1;
4801 if (TREE_SIDE_EFFECTS (valist))
4802 valist = save_expr (valist);
4803 valist = fold_build2_loc (loc, MEM_REF,
4804 vatype, valist, build_int_cst (pt, 0));
4807 return valist;
4810 /* The "standard" definition of va_list is void*. */
4812 tree
4813 std_build_builtin_va_list (void)
4815 return ptr_type_node;
4818 /* The "standard" abi va_list is va_list_type_node. */
4820 tree
4821 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4823 return va_list_type_node;
4826 /* The "standard" type of va_list is va_list_type_node. */
4828 tree
4829 std_canonical_va_list_type (tree type)
4831 tree wtype, htype;
4833 wtype = va_list_type_node;
4834 htype = type;
4836 if (TREE_CODE (wtype) == ARRAY_TYPE)
4838 /* If va_list is an array type, the argument may have decayed
4839 to a pointer type, e.g. by being passed to another function.
4840 In that case, unwrap both types so that we can compare the
4841 underlying records. */
4842 if (TREE_CODE (htype) == ARRAY_TYPE
4843 || POINTER_TYPE_P (htype))
4845 wtype = TREE_TYPE (wtype);
4846 htype = TREE_TYPE (htype);
4849 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4850 return va_list_type_node;
4852 return NULL_TREE;
4855 /* The "standard" implementation of va_start: just assign `nextarg' to
4856 the variable. */
4858 void
4859 std_expand_builtin_va_start (tree valist, rtx nextarg)
4861 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4862 convert_move (va_r, nextarg, 0);
4864 /* We do not have any valid bounds for the pointer, so
4865 just store zero bounds for it. */
4866 if (chkp_function_instrumented_p (current_function_decl))
4867 chkp_expand_bounds_reset_for_mem (valist,
4868 make_tree (TREE_TYPE (valist),
4869 nextarg));
4872 /* Expand EXP, a call to __builtin_va_start. */
4874 static rtx
4875 expand_builtin_va_start (tree exp)
4877 rtx nextarg;
4878 tree valist;
4879 location_t loc = EXPR_LOCATION (exp);
4881 if (call_expr_nargs (exp) < 2)
4883 error_at (loc, "too few arguments to function %<va_start%>");
4884 return const0_rtx;
4887 if (fold_builtin_next_arg (exp, true))
4888 return const0_rtx;
4890 nextarg = expand_builtin_next_arg ();
4891 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4893 if (targetm.expand_builtin_va_start)
4894 targetm.expand_builtin_va_start (valist, nextarg);
4895 else
4896 std_expand_builtin_va_start (valist, nextarg);
4898 return const0_rtx;
4901 /* Expand EXP, a call to __builtin_va_end. */
4903 static rtx
4904 expand_builtin_va_end (tree exp)
4906 tree valist = CALL_EXPR_ARG (exp, 0);
4908 /* Evaluate for side effects, if needed. I hate macros that don't
4909 do that. */
4910 if (TREE_SIDE_EFFECTS (valist))
4911 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4913 return const0_rtx;
4916 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4917 builtin rather than just as an assignment in stdarg.h because of the
4918 nastiness of array-type va_list types. */
4920 static rtx
4921 expand_builtin_va_copy (tree exp)
4923 tree dst, src, t;
4924 location_t loc = EXPR_LOCATION (exp);
4926 dst = CALL_EXPR_ARG (exp, 0);
4927 src = CALL_EXPR_ARG (exp, 1);
4929 dst = stabilize_va_list_loc (loc, dst, 1);
4930 src = stabilize_va_list_loc (loc, src, 0);
4932 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4934 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4936 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4937 TREE_SIDE_EFFECTS (t) = 1;
4938 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4940 else
4942 rtx dstb, srcb, size;
4944 /* Evaluate to pointers. */
4945 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4946 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4947 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4948 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4950 dstb = convert_memory_address (Pmode, dstb);
4951 srcb = convert_memory_address (Pmode, srcb);
4953 /* "Dereference" to BLKmode memories. */
4954 dstb = gen_rtx_MEM (BLKmode, dstb);
4955 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4956 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4957 srcb = gen_rtx_MEM (BLKmode, srcb);
4958 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4959 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4961 /* Copy. */
4962 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4965 return const0_rtx;
4968 /* Expand a call to one of the builtin functions __builtin_frame_address or
4969 __builtin_return_address. */
4971 static rtx
4972 expand_builtin_frame_address (tree fndecl, tree exp)
4974 /* The argument must be a nonnegative integer constant.
4975 It counts the number of frames to scan up the stack.
4976 The value is either the frame pointer value or the return
4977 address saved in that frame. */
4978 if (call_expr_nargs (exp) == 0)
4979 /* Warning about missing arg was already issued. */
4980 return const0_rtx;
4981 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4983 error ("invalid argument to %qD", fndecl);
4984 return const0_rtx;
4986 else
4988 /* Number of frames to scan up the stack. */
4989 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4991 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4993 /* Some ports cannot access arbitrary stack frames. */
4994 if (tem == NULL)
4996 warning (0, "unsupported argument to %qD", fndecl);
4997 return const0_rtx;
5000 if (count)
5002 /* Warn since no effort is made to ensure that any frame
5003 beyond the current one exists or can be safely reached. */
5004 warning (OPT_Wframe_address, "calling %qD with "
5005 "a nonzero argument is unsafe", fndecl);
5008 /* For __builtin_frame_address, return what we've got. */
5009 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5010 return tem;
5012 if (!REG_P (tem)
5013 && ! CONSTANT_P (tem))
5014 tem = copy_addr_to_reg (tem);
5015 return tem;
5019 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5020 failed and the caller should emit a normal call. */
5022 static rtx
5023 expand_builtin_alloca (tree exp)
5025 rtx op0;
5026 rtx result;
5027 unsigned int align;
5028 tree fndecl = get_callee_fndecl (exp);
5029 HOST_WIDE_INT max_size;
5030 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5031 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5032 bool valid_arglist
5033 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5034 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5035 VOID_TYPE)
5036 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5037 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5038 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5040 if (!valid_arglist)
5041 return NULL_RTX;
5043 if ((alloca_for_var && !warn_vla_limit)
5044 || (!alloca_for_var && !warn_alloca_limit))
5046 /* -Walloca-larger-than and -Wvla-larger-than settings override
5047 the more general -Walloc-size-larger-than so unless either of
5048 the former options is specified check the alloca arguments for
5049 overflow. */
5050 tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE };
5051 int idx[] = { 0, -1 };
5052 maybe_warn_alloc_args_overflow (fndecl, exp, args, idx);
5055 /* Compute the argument. */
5056 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5058 /* Compute the alignment. */
5059 align = (fcode == BUILT_IN_ALLOCA
5060 ? BIGGEST_ALIGNMENT
5061 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5063 /* Compute the maximum size. */
5064 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5065 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5066 : -1);
5068 /* Allocate the desired space. If the allocation stems from the declaration
5069 of a variable-sized object, it cannot accumulate. */
5070 result
5071 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5072 result = convert_memory_address (ptr_mode, result);
5074 return result;
5077 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5078 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5079 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5080 handle_builtin_stack_restore function. */
5082 static rtx
5083 expand_asan_emit_allocas_unpoison (tree exp)
5085 tree arg0 = CALL_EXPR_ARG (exp, 0);
5086 tree arg1 = CALL_EXPR_ARG (exp, 1);
5087 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5088 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5089 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5090 stack_pointer_rtx, NULL_RTX, 0,
5091 OPTAB_LIB_WIDEN);
5092 off = convert_modes (ptr_mode, Pmode, off, 0);
5093 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5094 OPTAB_LIB_WIDEN);
5095 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5096 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5097 top, ptr_mode, bot, ptr_mode);
5098 return ret;
5101 /* Expand a call to bswap builtin in EXP.
5102 Return NULL_RTX if a normal call should be emitted rather than expanding the
5103 function in-line. If convenient, the result should be placed in TARGET.
5104 SUBTARGET may be used as the target for computing one of EXP's operands. */
5106 static rtx
5107 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5108 rtx subtarget)
5110 tree arg;
5111 rtx op0;
5113 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5114 return NULL_RTX;
5116 arg = CALL_EXPR_ARG (exp, 0);
5117 op0 = expand_expr (arg,
5118 subtarget && GET_MODE (subtarget) == target_mode
5119 ? subtarget : NULL_RTX,
5120 target_mode, EXPAND_NORMAL);
5121 if (GET_MODE (op0) != target_mode)
5122 op0 = convert_to_mode (target_mode, op0, 1);
5124 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5126 gcc_assert (target);
5128 return convert_to_mode (target_mode, target, 1);
5131 /* Expand a call to a unary builtin in EXP.
5132 Return NULL_RTX if a normal call should be emitted rather than expanding the
5133 function in-line. If convenient, the result should be placed in TARGET.
5134 SUBTARGET may be used as the target for computing one of EXP's operands. */
5136 static rtx
5137 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5138 rtx subtarget, optab op_optab)
5140 rtx op0;
5142 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5143 return NULL_RTX;
5145 /* Compute the argument. */
5146 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5147 (subtarget
5148 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5149 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5150 VOIDmode, EXPAND_NORMAL);
5151 /* Compute op, into TARGET if possible.
5152 Set TARGET to wherever the result comes back. */
5153 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5154 op_optab, op0, target, op_optab != clrsb_optab);
5155 gcc_assert (target);
5157 return convert_to_mode (target_mode, target, 0);
5160 /* Expand a call to __builtin_expect. We just return our argument
5161 as the builtin_expect semantic should've been already executed by
5162 tree branch prediction pass. */
5164 static rtx
5165 expand_builtin_expect (tree exp, rtx target)
5167 tree arg;
5169 if (call_expr_nargs (exp) < 2)
5170 return const0_rtx;
5171 arg = CALL_EXPR_ARG (exp, 0);
5173 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5174 /* When guessing was done, the hints should be already stripped away. */
5175 gcc_assert (!flag_guess_branch_prob
5176 || optimize == 0 || seen_error ());
5177 return target;
5180 /* Expand a call to __builtin_assume_aligned. We just return our first
5181 argument as the builtin_assume_aligned semantic should've been already
5182 executed by CCP. */
5184 static rtx
5185 expand_builtin_assume_aligned (tree exp, rtx target)
5187 if (call_expr_nargs (exp) < 2)
5188 return const0_rtx;
5189 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5190 EXPAND_NORMAL);
5191 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5192 && (call_expr_nargs (exp) < 3
5193 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5194 return target;
5197 void
5198 expand_builtin_trap (void)
5200 if (targetm.have_trap ())
5202 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5203 /* For trap insns when not accumulating outgoing args force
5204 REG_ARGS_SIZE note to prevent crossjumping of calls with
5205 different args sizes. */
5206 if (!ACCUMULATE_OUTGOING_ARGS)
5207 add_args_size_note (insn, stack_pointer_delta);
5209 else
5211 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5212 tree call_expr = build_call_expr (fn, 0);
5213 expand_call (call_expr, NULL_RTX, false);
5216 emit_barrier ();
5219 /* Expand a call to __builtin_unreachable. We do nothing except emit
5220 a barrier saying that control flow will not pass here.
5222 It is the responsibility of the program being compiled to ensure
5223 that control flow does never reach __builtin_unreachable. */
5224 static void
5225 expand_builtin_unreachable (void)
5227 emit_barrier ();
5230 /* Expand EXP, a call to fabs, fabsf or fabsl.
5231 Return NULL_RTX if a normal call should be emitted rather than expanding
5232 the function inline. If convenient, the result should be placed
5233 in TARGET. SUBTARGET may be used as the target for computing
5234 the operand. */
5236 static rtx
5237 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5239 machine_mode mode;
5240 tree arg;
5241 rtx op0;
5243 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5244 return NULL_RTX;
5246 arg = CALL_EXPR_ARG (exp, 0);
5247 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5248 mode = TYPE_MODE (TREE_TYPE (arg));
5249 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5250 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5253 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5254 Return NULL is a normal call should be emitted rather than expanding the
5255 function inline. If convenient, the result should be placed in TARGET.
5256 SUBTARGET may be used as the target for computing the operand. */
5258 static rtx
5259 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5261 rtx op0, op1;
5262 tree arg;
5264 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5265 return NULL_RTX;
5267 arg = CALL_EXPR_ARG (exp, 0);
5268 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5270 arg = CALL_EXPR_ARG (exp, 1);
5271 op1 = expand_normal (arg);
5273 return expand_copysign (op0, op1, target);
5276 /* Expand a call to __builtin___clear_cache. */
5278 static rtx
5279 expand_builtin___clear_cache (tree exp)
5281 if (!targetm.code_for_clear_cache)
5283 #ifdef CLEAR_INSN_CACHE
5284 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5285 does something. Just do the default expansion to a call to
5286 __clear_cache(). */
5287 return NULL_RTX;
5288 #else
5289 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5290 does nothing. There is no need to call it. Do nothing. */
5291 return const0_rtx;
5292 #endif /* CLEAR_INSN_CACHE */
5295 /* We have a "clear_cache" insn, and it will handle everything. */
5296 tree begin, end;
5297 rtx begin_rtx, end_rtx;
5299 /* We must not expand to a library call. If we did, any
5300 fallback library function in libgcc that might contain a call to
5301 __builtin___clear_cache() would recurse infinitely. */
5302 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5304 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5305 return const0_rtx;
5308 if (targetm.have_clear_cache ())
5310 struct expand_operand ops[2];
5312 begin = CALL_EXPR_ARG (exp, 0);
5313 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5315 end = CALL_EXPR_ARG (exp, 1);
5316 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5318 create_address_operand (&ops[0], begin_rtx);
5319 create_address_operand (&ops[1], end_rtx);
5320 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5321 return const0_rtx;
5323 return const0_rtx;
5326 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5328 static rtx
5329 round_trampoline_addr (rtx tramp)
5331 rtx temp, addend, mask;
5333 /* If we don't need too much alignment, we'll have been guaranteed
5334 proper alignment by get_trampoline_type. */
5335 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5336 return tramp;
5338 /* Round address up to desired boundary. */
5339 temp = gen_reg_rtx (Pmode);
5340 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5341 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5343 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5344 temp, 0, OPTAB_LIB_WIDEN);
5345 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5346 temp, 0, OPTAB_LIB_WIDEN);
5348 return tramp;
5351 static rtx
5352 expand_builtin_init_trampoline (tree exp, bool onstack)
5354 tree t_tramp, t_func, t_chain;
5355 rtx m_tramp, r_tramp, r_chain, tmp;
5357 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5358 POINTER_TYPE, VOID_TYPE))
5359 return NULL_RTX;
5361 t_tramp = CALL_EXPR_ARG (exp, 0);
5362 t_func = CALL_EXPR_ARG (exp, 1);
5363 t_chain = CALL_EXPR_ARG (exp, 2);
5365 r_tramp = expand_normal (t_tramp);
5366 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5367 MEM_NOTRAP_P (m_tramp) = 1;
5369 /* If ONSTACK, the TRAMP argument should be the address of a field
5370 within the local function's FRAME decl. Either way, let's see if
5371 we can fill in the MEM_ATTRs for this memory. */
5372 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5373 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5375 /* Creator of a heap trampoline is responsible for making sure the
5376 address is aligned to at least STACK_BOUNDARY. Normally malloc
5377 will ensure this anyhow. */
5378 tmp = round_trampoline_addr (r_tramp);
5379 if (tmp != r_tramp)
5381 m_tramp = change_address (m_tramp, BLKmode, tmp);
5382 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5383 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5386 /* The FUNC argument should be the address of the nested function.
5387 Extract the actual function decl to pass to the hook. */
5388 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5389 t_func = TREE_OPERAND (t_func, 0);
5390 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5392 r_chain = expand_normal (t_chain);
5394 /* Generate insns to initialize the trampoline. */
5395 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5397 if (onstack)
5399 trampolines_created = 1;
5401 if (targetm.calls.custom_function_descriptors != 0)
5402 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5403 "trampoline generated for nested function %qD", t_func);
5406 return const0_rtx;
5409 static rtx
5410 expand_builtin_adjust_trampoline (tree exp)
5412 rtx tramp;
5414 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5415 return NULL_RTX;
5417 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5418 tramp = round_trampoline_addr (tramp);
5419 if (targetm.calls.trampoline_adjust_address)
5420 tramp = targetm.calls.trampoline_adjust_address (tramp);
5422 return tramp;
5425 /* Expand a call to the builtin descriptor initialization routine.
5426 A descriptor is made up of a couple of pointers to the static
5427 chain and the code entry in this order. */
5429 static rtx
5430 expand_builtin_init_descriptor (tree exp)
5432 tree t_descr, t_func, t_chain;
5433 rtx m_descr, r_descr, r_func, r_chain;
5435 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5436 VOID_TYPE))
5437 return NULL_RTX;
5439 t_descr = CALL_EXPR_ARG (exp, 0);
5440 t_func = CALL_EXPR_ARG (exp, 1);
5441 t_chain = CALL_EXPR_ARG (exp, 2);
5443 r_descr = expand_normal (t_descr);
5444 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5445 MEM_NOTRAP_P (m_descr) = 1;
5447 r_func = expand_normal (t_func);
5448 r_chain = expand_normal (t_chain);
5450 /* Generate insns to initialize the descriptor. */
5451 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5452 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5453 POINTER_SIZE / BITS_PER_UNIT), r_func);
5455 return const0_rtx;
5458 /* Expand a call to the builtin descriptor adjustment routine. */
5460 static rtx
5461 expand_builtin_adjust_descriptor (tree exp)
5463 rtx tramp;
5465 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5466 return NULL_RTX;
5468 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5470 /* Unalign the descriptor to allow runtime identification. */
5471 tramp = plus_constant (ptr_mode, tramp,
5472 targetm.calls.custom_function_descriptors);
5474 return force_operand (tramp, NULL_RTX);
5477 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5478 function. The function first checks whether the back end provides
5479 an insn to implement signbit for the respective mode. If not, it
5480 checks whether the floating point format of the value is such that
5481 the sign bit can be extracted. If that is not the case, error out.
5482 EXP is the expression that is a call to the builtin function; if
5483 convenient, the result should be placed in TARGET. */
5484 static rtx
5485 expand_builtin_signbit (tree exp, rtx target)
5487 const struct real_format *fmt;
5488 scalar_float_mode fmode;
5489 scalar_int_mode rmode, imode;
5490 tree arg;
5491 int word, bitpos;
5492 enum insn_code icode;
5493 rtx temp;
5494 location_t loc = EXPR_LOCATION (exp);
5496 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5497 return NULL_RTX;
5499 arg = CALL_EXPR_ARG (exp, 0);
5500 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5501 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5502 fmt = REAL_MODE_FORMAT (fmode);
5504 arg = builtin_save_expr (arg);
5506 /* Expand the argument yielding a RTX expression. */
5507 temp = expand_normal (arg);
5509 /* Check if the back end provides an insn that handles signbit for the
5510 argument's mode. */
5511 icode = optab_handler (signbit_optab, fmode);
5512 if (icode != CODE_FOR_nothing)
5514 rtx_insn *last = get_last_insn ();
5515 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5516 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5517 return target;
5518 delete_insns_since (last);
5521 /* For floating point formats without a sign bit, implement signbit
5522 as "ARG < 0.0". */
5523 bitpos = fmt->signbit_ro;
5524 if (bitpos < 0)
5526 /* But we can't do this if the format supports signed zero. */
5527 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5529 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5530 build_real (TREE_TYPE (arg), dconst0));
5531 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5534 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5536 imode = int_mode_for_mode (fmode).require ();
5537 temp = gen_lowpart (imode, temp);
5539 else
5541 imode = word_mode;
5542 /* Handle targets with different FP word orders. */
5543 if (FLOAT_WORDS_BIG_ENDIAN)
5544 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5545 else
5546 word = bitpos / BITS_PER_WORD;
5547 temp = operand_subword_force (temp, word, fmode);
5548 bitpos = bitpos % BITS_PER_WORD;
5551 /* Force the intermediate word_mode (or narrower) result into a
5552 register. This avoids attempting to create paradoxical SUBREGs
5553 of floating point modes below. */
5554 temp = force_reg (imode, temp);
5556 /* If the bitpos is within the "result mode" lowpart, the operation
5557 can be implement with a single bitwise AND. Otherwise, we need
5558 a right shift and an AND. */
5560 if (bitpos < GET_MODE_BITSIZE (rmode))
5562 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5564 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5565 temp = gen_lowpart (rmode, temp);
5566 temp = expand_binop (rmode, and_optab, temp,
5567 immed_wide_int_const (mask, rmode),
5568 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5570 else
5572 /* Perform a logical right shift to place the signbit in the least
5573 significant bit, then truncate the result to the desired mode
5574 and mask just this bit. */
5575 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5576 temp = gen_lowpart (rmode, temp);
5577 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5578 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5581 return temp;
5584 /* Expand fork or exec calls. TARGET is the desired target of the
5585 call. EXP is the call. FN is the
5586 identificator of the actual function. IGNORE is nonzero if the
5587 value is to be ignored. */
5589 static rtx
5590 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5592 tree id, decl;
5593 tree call;
5595 /* If we are not profiling, just call the function. */
5596 if (!profile_arc_flag)
5597 return NULL_RTX;
5599 /* Otherwise call the wrapper. This should be equivalent for the rest of
5600 compiler, so the code does not diverge, and the wrapper may run the
5601 code necessary for keeping the profiling sane. */
5603 switch (DECL_FUNCTION_CODE (fn))
5605 case BUILT_IN_FORK:
5606 id = get_identifier ("__gcov_fork");
5607 break;
5609 case BUILT_IN_EXECL:
5610 id = get_identifier ("__gcov_execl");
5611 break;
5613 case BUILT_IN_EXECV:
5614 id = get_identifier ("__gcov_execv");
5615 break;
5617 case BUILT_IN_EXECLP:
5618 id = get_identifier ("__gcov_execlp");
5619 break;
5621 case BUILT_IN_EXECLE:
5622 id = get_identifier ("__gcov_execle");
5623 break;
5625 case BUILT_IN_EXECVP:
5626 id = get_identifier ("__gcov_execvp");
5627 break;
5629 case BUILT_IN_EXECVE:
5630 id = get_identifier ("__gcov_execve");
5631 break;
5633 default:
5634 gcc_unreachable ();
5637 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5638 FUNCTION_DECL, id, TREE_TYPE (fn));
5639 DECL_EXTERNAL (decl) = 1;
5640 TREE_PUBLIC (decl) = 1;
5641 DECL_ARTIFICIAL (decl) = 1;
5642 TREE_NOTHROW (decl) = 1;
5643 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5644 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5645 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5646 return expand_call (call, target, ignore);
5651 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5652 the pointer in these functions is void*, the tree optimizers may remove
5653 casts. The mode computed in expand_builtin isn't reliable either, due
5654 to __sync_bool_compare_and_swap.
5656 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5657 group of builtins. This gives us log2 of the mode size. */
5659 static inline machine_mode
5660 get_builtin_sync_mode (int fcode_diff)
5662 /* The size is not negotiable, so ask not to get BLKmode in return
5663 if the target indicates that a smaller size would be better. */
5664 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5667 /* Expand the memory expression LOC and return the appropriate memory operand
5668 for the builtin_sync operations. */
5670 static rtx
5671 get_builtin_sync_mem (tree loc, machine_mode mode)
5673 rtx addr, mem;
5675 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5676 addr = convert_memory_address (Pmode, addr);
5678 /* Note that we explicitly do not want any alias information for this
5679 memory, so that we kill all other live memories. Otherwise we don't
5680 satisfy the full barrier semantics of the intrinsic. */
5681 mem = validize_mem (gen_rtx_MEM (mode, addr));
5683 /* The alignment needs to be at least according to that of the mode. */
5684 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5685 get_pointer_alignment (loc)));
5686 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5687 MEM_VOLATILE_P (mem) = 1;
5689 return mem;
5692 /* Make sure an argument is in the right mode.
5693 EXP is the tree argument.
5694 MODE is the mode it should be in. */
5696 static rtx
5697 expand_expr_force_mode (tree exp, machine_mode mode)
5699 rtx val;
5700 machine_mode old_mode;
5702 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5703 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5704 of CONST_INTs, where we know the old_mode only from the call argument. */
5706 old_mode = GET_MODE (val);
5707 if (old_mode == VOIDmode)
5708 old_mode = TYPE_MODE (TREE_TYPE (exp));
5709 val = convert_modes (mode, old_mode, val, 1);
5710 return val;
5714 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5715 EXP is the CALL_EXPR. CODE is the rtx code
5716 that corresponds to the arithmetic or logical operation from the name;
5717 an exception here is that NOT actually means NAND. TARGET is an optional
5718 place for us to store the results; AFTER is true if this is the
5719 fetch_and_xxx form. */
5721 static rtx
5722 expand_builtin_sync_operation (machine_mode mode, tree exp,
5723 enum rtx_code code, bool after,
5724 rtx target)
5726 rtx val, mem;
5727 location_t loc = EXPR_LOCATION (exp);
5729 if (code == NOT && warn_sync_nand)
5731 tree fndecl = get_callee_fndecl (exp);
5732 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5734 static bool warned_f_a_n, warned_n_a_f;
5736 switch (fcode)
5738 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5739 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5740 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5741 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5742 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5743 if (warned_f_a_n)
5744 break;
5746 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5747 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5748 warned_f_a_n = true;
5749 break;
5751 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5752 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5753 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5754 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5755 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5756 if (warned_n_a_f)
5757 break;
5759 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5760 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5761 warned_n_a_f = true;
5762 break;
5764 default:
5765 gcc_unreachable ();
5769 /* Expand the operands. */
5770 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5771 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5773 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5774 after);
5777 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5778 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5779 true if this is the boolean form. TARGET is a place for us to store the
5780 results; this is NOT optional if IS_BOOL is true. */
5782 static rtx
5783 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5784 bool is_bool, rtx target)
5786 rtx old_val, new_val, mem;
5787 rtx *pbool, *poval;
5789 /* Expand the operands. */
5790 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5791 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5792 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5794 pbool = poval = NULL;
5795 if (target != const0_rtx)
5797 if (is_bool)
5798 pbool = &target;
5799 else
5800 poval = &target;
5802 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5803 false, MEMMODEL_SYNC_SEQ_CST,
5804 MEMMODEL_SYNC_SEQ_CST))
5805 return NULL_RTX;
5807 return target;
5810 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5811 general form is actually an atomic exchange, and some targets only
5812 support a reduced form with the second argument being a constant 1.
5813 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5814 the results. */
5816 static rtx
5817 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5818 rtx target)
5820 rtx val, mem;
5822 /* Expand the operands. */
5823 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5824 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5826 return expand_sync_lock_test_and_set (target, mem, val);
5829 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5831 static void
5832 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5834 rtx mem;
5836 /* Expand the operands. */
5837 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5839 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5842 /* Given an integer representing an ``enum memmodel'', verify its
5843 correctness and return the memory model enum. */
5845 static enum memmodel
5846 get_memmodel (tree exp)
5848 rtx op;
5849 unsigned HOST_WIDE_INT val;
5850 source_location loc
5851 = expansion_point_location_if_in_system_header (input_location);
5853 /* If the parameter is not a constant, it's a run time value so we'll just
5854 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5855 if (TREE_CODE (exp) != INTEGER_CST)
5856 return MEMMODEL_SEQ_CST;
5858 op = expand_normal (exp);
5860 val = INTVAL (op);
5861 if (targetm.memmodel_check)
5862 val = targetm.memmodel_check (val);
5863 else if (val & ~MEMMODEL_MASK)
5865 warning_at (loc, OPT_Winvalid_memory_model,
5866 "unknown architecture specifier in memory model to builtin");
5867 return MEMMODEL_SEQ_CST;
5870 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5871 if (memmodel_base (val) >= MEMMODEL_LAST)
5873 warning_at (loc, OPT_Winvalid_memory_model,
5874 "invalid memory model argument to builtin");
5875 return MEMMODEL_SEQ_CST;
5878 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5879 be conservative and promote consume to acquire. */
5880 if (val == MEMMODEL_CONSUME)
5881 val = MEMMODEL_ACQUIRE;
5883 return (enum memmodel) val;
5886 /* Expand the __atomic_exchange intrinsic:
5887 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5888 EXP is the CALL_EXPR.
5889 TARGET is an optional place for us to store the results. */
5891 static rtx
5892 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5894 rtx val, mem;
5895 enum memmodel model;
5897 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5899 if (!flag_inline_atomics)
5900 return NULL_RTX;
5902 /* Expand the operands. */
5903 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5904 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5906 return expand_atomic_exchange (target, mem, val, model);
5909 /* Expand the __atomic_compare_exchange intrinsic:
5910 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5911 TYPE desired, BOOL weak,
5912 enum memmodel success,
5913 enum memmodel failure)
5914 EXP is the CALL_EXPR.
5915 TARGET is an optional place for us to store the results. */
5917 static rtx
5918 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5919 rtx target)
5921 rtx expect, desired, mem, oldval;
5922 rtx_code_label *label;
5923 enum memmodel success, failure;
5924 tree weak;
5925 bool is_weak;
5926 source_location loc
5927 = expansion_point_location_if_in_system_header (input_location);
5929 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5930 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5932 if (failure > success)
5934 warning_at (loc, OPT_Winvalid_memory_model,
5935 "failure memory model cannot be stronger than success "
5936 "memory model for %<__atomic_compare_exchange%>");
5937 success = MEMMODEL_SEQ_CST;
5940 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5942 warning_at (loc, OPT_Winvalid_memory_model,
5943 "invalid failure memory model for "
5944 "%<__atomic_compare_exchange%>");
5945 failure = MEMMODEL_SEQ_CST;
5946 success = MEMMODEL_SEQ_CST;
5950 if (!flag_inline_atomics)
5951 return NULL_RTX;
5953 /* Expand the operands. */
5954 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5956 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5957 expect = convert_memory_address (Pmode, expect);
5958 expect = gen_rtx_MEM (mode, expect);
5959 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5961 weak = CALL_EXPR_ARG (exp, 3);
5962 is_weak = false;
5963 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5964 is_weak = true;
5966 if (target == const0_rtx)
5967 target = NULL;
5969 /* Lest the rtl backend create a race condition with an imporoper store
5970 to memory, always create a new pseudo for OLDVAL. */
5971 oldval = NULL;
5973 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5974 is_weak, success, failure))
5975 return NULL_RTX;
5977 /* Conditionally store back to EXPECT, lest we create a race condition
5978 with an improper store to memory. */
5979 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5980 the normal case where EXPECT is totally private, i.e. a register. At
5981 which point the store can be unconditional. */
5982 label = gen_label_rtx ();
5983 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5984 GET_MODE (target), 1, label);
5985 emit_move_insn (expect, oldval);
5986 emit_label (label);
5988 return target;
5991 /* Helper function for expand_ifn_atomic_compare_exchange - expand
5992 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
5993 call. The weak parameter must be dropped to match the expected parameter
5994 list and the expected argument changed from value to pointer to memory
5995 slot. */
5997 static void
5998 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6000 unsigned int z;
6001 vec<tree, va_gc> *vec;
6003 vec_alloc (vec, 5);
6004 vec->quick_push (gimple_call_arg (call, 0));
6005 tree expected = gimple_call_arg (call, 1);
6006 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6007 TREE_TYPE (expected));
6008 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6009 if (expd != x)
6010 emit_move_insn (x, expd);
6011 tree v = make_tree (TREE_TYPE (expected), x);
6012 vec->quick_push (build1 (ADDR_EXPR,
6013 build_pointer_type (TREE_TYPE (expected)), v));
6014 vec->quick_push (gimple_call_arg (call, 2));
6015 /* Skip the boolean weak parameter. */
6016 for (z = 4; z < 6; z++)
6017 vec->quick_push (gimple_call_arg (call, z));
6018 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6019 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6020 gcc_assert (bytes_log2 < 5);
6021 built_in_function fncode
6022 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6023 + bytes_log2);
6024 tree fndecl = builtin_decl_explicit (fncode);
6025 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6026 fndecl);
6027 tree exp = build_call_vec (boolean_type_node, fn, vec);
6028 tree lhs = gimple_call_lhs (call);
6029 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6030 if (lhs)
6032 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6033 if (GET_MODE (boolret) != mode)
6034 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6035 x = force_reg (mode, x);
6036 write_complex_part (target, boolret, true);
6037 write_complex_part (target, x, false);
6041 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6043 void
6044 expand_ifn_atomic_compare_exchange (gcall *call)
6046 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6047 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6048 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6049 rtx expect, desired, mem, oldval, boolret;
6050 enum memmodel success, failure;
6051 tree lhs;
6052 bool is_weak;
6053 source_location loc
6054 = expansion_point_location_if_in_system_header (gimple_location (call));
6056 success = get_memmodel (gimple_call_arg (call, 4));
6057 failure = get_memmodel (gimple_call_arg (call, 5));
6059 if (failure > success)
6061 warning_at (loc, OPT_Winvalid_memory_model,
6062 "failure memory model cannot be stronger than success "
6063 "memory model for %<__atomic_compare_exchange%>");
6064 success = MEMMODEL_SEQ_CST;
6067 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6069 warning_at (loc, OPT_Winvalid_memory_model,
6070 "invalid failure memory model for "
6071 "%<__atomic_compare_exchange%>");
6072 failure = MEMMODEL_SEQ_CST;
6073 success = MEMMODEL_SEQ_CST;
6076 if (!flag_inline_atomics)
6078 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6079 return;
6082 /* Expand the operands. */
6083 mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6085 expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6086 desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6088 is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6090 boolret = NULL;
6091 oldval = NULL;
6093 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6094 is_weak, success, failure))
6096 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6097 return;
6100 lhs = gimple_call_lhs (call);
6101 if (lhs)
6103 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6104 if (GET_MODE (boolret) != mode)
6105 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6106 write_complex_part (target, boolret, true);
6107 write_complex_part (target, oldval, false);
6111 /* Expand the __atomic_load intrinsic:
6112 TYPE __atomic_load (TYPE *object, enum memmodel)
6113 EXP is the CALL_EXPR.
6114 TARGET is an optional place for us to store the results. */
6116 static rtx
6117 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6119 rtx mem;
6120 enum memmodel model;
6122 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6123 if (is_mm_release (model) || is_mm_acq_rel (model))
6125 source_location loc
6126 = expansion_point_location_if_in_system_header (input_location);
6127 warning_at (loc, OPT_Winvalid_memory_model,
6128 "invalid memory model for %<__atomic_load%>");
6129 model = MEMMODEL_SEQ_CST;
6132 if (!flag_inline_atomics)
6133 return NULL_RTX;
6135 /* Expand the operand. */
6136 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6138 return expand_atomic_load (target, mem, model);
6142 /* Expand the __atomic_store intrinsic:
6143 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6144 EXP is the CALL_EXPR.
6145 TARGET is an optional place for us to store the results. */
6147 static rtx
6148 expand_builtin_atomic_store (machine_mode mode, tree exp)
6150 rtx mem, val;
6151 enum memmodel model;
6153 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6154 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6155 || is_mm_release (model)))
6157 source_location loc
6158 = expansion_point_location_if_in_system_header (input_location);
6159 warning_at (loc, OPT_Winvalid_memory_model,
6160 "invalid memory model for %<__atomic_store%>");
6161 model = MEMMODEL_SEQ_CST;
6164 if (!flag_inline_atomics)
6165 return NULL_RTX;
6167 /* Expand the operands. */
6168 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6169 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6171 return expand_atomic_store (mem, val, model, false);
6174 /* Expand the __atomic_fetch_XXX intrinsic:
6175 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6176 EXP is the CALL_EXPR.
6177 TARGET is an optional place for us to store the results.
6178 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6179 FETCH_AFTER is true if returning the result of the operation.
6180 FETCH_AFTER is false if returning the value before the operation.
6181 IGNORE is true if the result is not used.
6182 EXT_CALL is the correct builtin for an external call if this cannot be
6183 resolved to an instruction sequence. */
6185 static rtx
6186 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6187 enum rtx_code code, bool fetch_after,
6188 bool ignore, enum built_in_function ext_call)
6190 rtx val, mem, ret;
6191 enum memmodel model;
6192 tree fndecl;
6193 tree addr;
6195 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6197 /* Expand the operands. */
6198 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6199 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6201 /* Only try generating instructions if inlining is turned on. */
6202 if (flag_inline_atomics)
6204 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6205 if (ret)
6206 return ret;
6209 /* Return if a different routine isn't needed for the library call. */
6210 if (ext_call == BUILT_IN_NONE)
6211 return NULL_RTX;
6213 /* Change the call to the specified function. */
6214 fndecl = get_callee_fndecl (exp);
6215 addr = CALL_EXPR_FN (exp);
6216 STRIP_NOPS (addr);
6218 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6219 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6221 /* If we will emit code after the call, the call can not be a tail call.
6222 If it is emitted as a tail call, a barrier is emitted after it, and
6223 then all trailing code is removed. */
6224 if (!ignore)
6225 CALL_EXPR_TAILCALL (exp) = 0;
6227 /* Expand the call here so we can emit trailing code. */
6228 ret = expand_call (exp, target, ignore);
6230 /* Replace the original function just in case it matters. */
6231 TREE_OPERAND (addr, 0) = fndecl;
6233 /* Then issue the arithmetic correction to return the right result. */
6234 if (!ignore)
6236 if (code == NOT)
6238 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6239 OPTAB_LIB_WIDEN);
6240 ret = expand_simple_unop (mode, NOT, ret, target, true);
6242 else
6243 ret = expand_simple_binop (mode, code, ret, val, target, true,
6244 OPTAB_LIB_WIDEN);
6246 return ret;
6249 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6251 void
6252 expand_ifn_atomic_bit_test_and (gcall *call)
6254 tree ptr = gimple_call_arg (call, 0);
6255 tree bit = gimple_call_arg (call, 1);
6256 tree flag = gimple_call_arg (call, 2);
6257 tree lhs = gimple_call_lhs (call);
6258 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6259 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6260 enum rtx_code code;
6261 optab optab;
6262 struct expand_operand ops[5];
6264 gcc_assert (flag_inline_atomics);
6266 if (gimple_call_num_args (call) == 4)
6267 model = get_memmodel (gimple_call_arg (call, 3));
6269 rtx mem = get_builtin_sync_mem (ptr, mode);
6270 rtx val = expand_expr_force_mode (bit, mode);
6272 switch (gimple_call_internal_fn (call))
6274 case IFN_ATOMIC_BIT_TEST_AND_SET:
6275 code = IOR;
6276 optab = atomic_bit_test_and_set_optab;
6277 break;
6278 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6279 code = XOR;
6280 optab = atomic_bit_test_and_complement_optab;
6281 break;
6282 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6283 code = AND;
6284 optab = atomic_bit_test_and_reset_optab;
6285 break;
6286 default:
6287 gcc_unreachable ();
6290 if (lhs == NULL_TREE)
6292 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6293 val, NULL_RTX, true, OPTAB_DIRECT);
6294 if (code == AND)
6295 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6296 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6297 return;
6300 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6301 enum insn_code icode = direct_optab_handler (optab, mode);
6302 gcc_assert (icode != CODE_FOR_nothing);
6303 create_output_operand (&ops[0], target, mode);
6304 create_fixed_operand (&ops[1], mem);
6305 create_convert_operand_to (&ops[2], val, mode, true);
6306 create_integer_operand (&ops[3], model);
6307 create_integer_operand (&ops[4], integer_onep (flag));
6308 if (maybe_expand_insn (icode, 5, ops))
6309 return;
6311 rtx bitval = val;
6312 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6313 val, NULL_RTX, true, OPTAB_DIRECT);
6314 rtx maskval = val;
6315 if (code == AND)
6316 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6317 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6318 code, model, false);
6319 if (integer_onep (flag))
6321 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6322 NULL_RTX, true, OPTAB_DIRECT);
6323 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6324 true, OPTAB_DIRECT);
6326 else
6327 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6328 OPTAB_DIRECT);
6329 if (result != target)
6330 emit_move_insn (target, result);
6333 /* Expand an atomic clear operation.
6334 void _atomic_clear (BOOL *obj, enum memmodel)
6335 EXP is the call expression. */
6337 static rtx
6338 expand_builtin_atomic_clear (tree exp)
6340 machine_mode mode;
6341 rtx mem, ret;
6342 enum memmodel model;
6344 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6345 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6346 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6348 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6350 source_location loc
6351 = expansion_point_location_if_in_system_header (input_location);
6352 warning_at (loc, OPT_Winvalid_memory_model,
6353 "invalid memory model for %<__atomic_store%>");
6354 model = MEMMODEL_SEQ_CST;
6357 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6358 Failing that, a store is issued by __atomic_store. The only way this can
6359 fail is if the bool type is larger than a word size. Unlikely, but
6360 handle it anyway for completeness. Assume a single threaded model since
6361 there is no atomic support in this case, and no barriers are required. */
6362 ret = expand_atomic_store (mem, const0_rtx, model, true);
6363 if (!ret)
6364 emit_move_insn (mem, const0_rtx);
6365 return const0_rtx;
6368 /* Expand an atomic test_and_set operation.
6369 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6370 EXP is the call expression. */
6372 static rtx
6373 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6375 rtx mem;
6376 enum memmodel model;
6377 machine_mode mode;
6379 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6380 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6381 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6383 return expand_atomic_test_and_set (target, mem, model);
6387 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6388 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6390 static tree
6391 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6393 int size;
6394 machine_mode mode;
6395 unsigned int mode_align, type_align;
6397 if (TREE_CODE (arg0) != INTEGER_CST)
6398 return NULL_TREE;
6400 /* We need a corresponding integer mode for the access to be lock-free. */
6401 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6402 if (!int_mode_for_size (size, 0).exists (&mode))
6403 return boolean_false_node;
6405 mode_align = GET_MODE_ALIGNMENT (mode);
6407 if (TREE_CODE (arg1) == INTEGER_CST)
6409 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6411 /* Either this argument is null, or it's a fake pointer encoding
6412 the alignment of the object. */
6413 val = least_bit_hwi (val);
6414 val *= BITS_PER_UNIT;
6416 if (val == 0 || mode_align < val)
6417 type_align = mode_align;
6418 else
6419 type_align = val;
6421 else
6423 tree ttype = TREE_TYPE (arg1);
6425 /* This function is usually invoked and folded immediately by the front
6426 end before anything else has a chance to look at it. The pointer
6427 parameter at this point is usually cast to a void *, so check for that
6428 and look past the cast. */
6429 if (CONVERT_EXPR_P (arg1)
6430 && POINTER_TYPE_P (ttype)
6431 && VOID_TYPE_P (TREE_TYPE (ttype))
6432 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6433 arg1 = TREE_OPERAND (arg1, 0);
6435 ttype = TREE_TYPE (arg1);
6436 gcc_assert (POINTER_TYPE_P (ttype));
6438 /* Get the underlying type of the object. */
6439 ttype = TREE_TYPE (ttype);
6440 type_align = TYPE_ALIGN (ttype);
6443 /* If the object has smaller alignment, the lock free routines cannot
6444 be used. */
6445 if (type_align < mode_align)
6446 return boolean_false_node;
6448 /* Check if a compare_and_swap pattern exists for the mode which represents
6449 the required size. The pattern is not allowed to fail, so the existence
6450 of the pattern indicates support is present. Also require that an
6451 atomic load exists for the required size. */
6452 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6453 return boolean_true_node;
6454 else
6455 return boolean_false_node;
6458 /* Return true if the parameters to call EXP represent an object which will
6459 always generate lock free instructions. The first argument represents the
6460 size of the object, and the second parameter is a pointer to the object
6461 itself. If NULL is passed for the object, then the result is based on
6462 typical alignment for an object of the specified size. Otherwise return
6463 false. */
6465 static rtx
6466 expand_builtin_atomic_always_lock_free (tree exp)
6468 tree size;
6469 tree arg0 = CALL_EXPR_ARG (exp, 0);
6470 tree arg1 = CALL_EXPR_ARG (exp, 1);
6472 if (TREE_CODE (arg0) != INTEGER_CST)
6474 error ("non-constant argument 1 to __atomic_always_lock_free");
6475 return const0_rtx;
6478 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6479 if (size == boolean_true_node)
6480 return const1_rtx;
6481 return const0_rtx;
6484 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6485 is lock free on this architecture. */
6487 static tree
6488 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6490 if (!flag_inline_atomics)
6491 return NULL_TREE;
6493 /* If it isn't always lock free, don't generate a result. */
6494 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6495 return boolean_true_node;
6497 return NULL_TREE;
6500 /* Return true if the parameters to call EXP represent an object which will
6501 always generate lock free instructions. The first argument represents the
6502 size of the object, and the second parameter is a pointer to the object
6503 itself. If NULL is passed for the object, then the result is based on
6504 typical alignment for an object of the specified size. Otherwise return
6505 NULL*/
6507 static rtx
6508 expand_builtin_atomic_is_lock_free (tree exp)
6510 tree size;
6511 tree arg0 = CALL_EXPR_ARG (exp, 0);
6512 tree arg1 = CALL_EXPR_ARG (exp, 1);
6514 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6516 error ("non-integer argument 1 to __atomic_is_lock_free");
6517 return NULL_RTX;
6520 if (!flag_inline_atomics)
6521 return NULL_RTX;
6523 /* If the value is known at compile time, return the RTX for it. */
6524 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6525 if (size == boolean_true_node)
6526 return const1_rtx;
6528 return NULL_RTX;
6531 /* Expand the __atomic_thread_fence intrinsic:
6532 void __atomic_thread_fence (enum memmodel)
6533 EXP is the CALL_EXPR. */
6535 static void
6536 expand_builtin_atomic_thread_fence (tree exp)
6538 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6539 expand_mem_thread_fence (model);
6542 /* Expand the __atomic_signal_fence intrinsic:
6543 void __atomic_signal_fence (enum memmodel)
6544 EXP is the CALL_EXPR. */
6546 static void
6547 expand_builtin_atomic_signal_fence (tree exp)
6549 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6550 expand_mem_signal_fence (model);
6553 /* Expand the __sync_synchronize intrinsic. */
6555 static void
6556 expand_builtin_sync_synchronize (void)
6558 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6561 static rtx
6562 expand_builtin_thread_pointer (tree exp, rtx target)
6564 enum insn_code icode;
6565 if (!validate_arglist (exp, VOID_TYPE))
6566 return const0_rtx;
6567 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6568 if (icode != CODE_FOR_nothing)
6570 struct expand_operand op;
6571 /* If the target is not sutitable then create a new target. */
6572 if (target == NULL_RTX
6573 || !REG_P (target)
6574 || GET_MODE (target) != Pmode)
6575 target = gen_reg_rtx (Pmode);
6576 create_output_operand (&op, target, Pmode);
6577 expand_insn (icode, 1, &op);
6578 return target;
6580 error ("__builtin_thread_pointer is not supported on this target");
6581 return const0_rtx;
6584 static void
6585 expand_builtin_set_thread_pointer (tree exp)
6587 enum insn_code icode;
6588 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6589 return;
6590 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6591 if (icode != CODE_FOR_nothing)
6593 struct expand_operand op;
6594 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6595 Pmode, EXPAND_NORMAL);
6596 create_input_operand (&op, val, Pmode);
6597 expand_insn (icode, 1, &op);
6598 return;
6600 error ("__builtin_set_thread_pointer is not supported on this target");
6604 /* Emit code to restore the current value of stack. */
6606 static void
6607 expand_stack_restore (tree var)
6609 rtx_insn *prev;
6610 rtx sa = expand_normal (var);
6612 sa = convert_memory_address (Pmode, sa);
6614 prev = get_last_insn ();
6615 emit_stack_restore (SAVE_BLOCK, sa);
6617 record_new_stack_level ();
6619 fixup_args_size_notes (prev, get_last_insn (), 0);
6622 /* Emit code to save the current value of stack. */
6624 static rtx
6625 expand_stack_save (void)
6627 rtx ret = NULL_RTX;
6629 emit_stack_save (SAVE_BLOCK, &ret);
6630 return ret;
6633 /* Emit code to get the openacc gang, worker or vector id or size. */
6635 static rtx
6636 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
6638 const char *name;
6639 rtx fallback_retval;
6640 rtx_insn *(*gen_fn) (rtx, rtx);
6641 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
6643 case BUILT_IN_GOACC_PARLEVEL_ID:
6644 name = "__builtin_goacc_parlevel_id";
6645 fallback_retval = const0_rtx;
6646 gen_fn = targetm.gen_oacc_dim_pos;
6647 break;
6648 case BUILT_IN_GOACC_PARLEVEL_SIZE:
6649 name = "__builtin_goacc_parlevel_size";
6650 fallback_retval = const1_rtx;
6651 gen_fn = targetm.gen_oacc_dim_size;
6652 break;
6653 default:
6654 gcc_unreachable ();
6657 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
6659 error ("%qs only supported in OpenACC code", name);
6660 return const0_rtx;
6663 tree arg = CALL_EXPR_ARG (exp, 0);
6664 if (TREE_CODE (arg) != INTEGER_CST)
6666 error ("non-constant argument 0 to %qs", name);
6667 return const0_rtx;
6670 int dim = TREE_INT_CST_LOW (arg);
6671 switch (dim)
6673 case GOMP_DIM_GANG:
6674 case GOMP_DIM_WORKER:
6675 case GOMP_DIM_VECTOR:
6676 break;
6677 default:
6678 error ("illegal argument 0 to %qs", name);
6679 return const0_rtx;
6682 if (ignore)
6683 return target;
6685 if (target == NULL_RTX)
6686 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6688 if (!targetm.have_oacc_dim_size ())
6690 emit_move_insn (target, fallback_retval);
6691 return target;
6694 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
6695 emit_insn (gen_fn (reg, GEN_INT (dim)));
6696 if (reg != target)
6697 emit_move_insn (target, reg);
6699 return target;
6702 /* Expand an expression EXP that calls a built-in function,
6703 with result going to TARGET if that's convenient
6704 (and in mode MODE if that's convenient).
6705 SUBTARGET may be used as the target for computing one of EXP's operands.
6706 IGNORE is nonzero if the value is to be ignored. */
6709 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6710 int ignore)
6712 tree fndecl = get_callee_fndecl (exp);
6713 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6714 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6715 int flags;
6717 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6718 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6720 /* When ASan is enabled, we don't want to expand some memory/string
6721 builtins and rely on libsanitizer's hooks. This allows us to avoid
6722 redundant checks and be sure, that possible overflow will be detected
6723 by ASan. */
6725 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6726 return expand_call (exp, target, ignore);
6728 /* When not optimizing, generate calls to library functions for a certain
6729 set of builtins. */
6730 if (!optimize
6731 && !called_as_built_in (fndecl)
6732 && fcode != BUILT_IN_FORK
6733 && fcode != BUILT_IN_EXECL
6734 && fcode != BUILT_IN_EXECV
6735 && fcode != BUILT_IN_EXECLP
6736 && fcode != BUILT_IN_EXECLE
6737 && fcode != BUILT_IN_EXECVP
6738 && fcode != BUILT_IN_EXECVE
6739 && !ALLOCA_FUNCTION_CODE_P (fcode)
6740 && fcode != BUILT_IN_FREE
6741 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
6742 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
6743 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
6744 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
6745 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6746 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
6747 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
6748 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
6749 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
6750 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
6751 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
6752 && fcode != BUILT_IN_CHKP_BNDRET)
6753 return expand_call (exp, target, ignore);
6755 /* The built-in function expanders test for target == const0_rtx
6756 to determine whether the function's result will be ignored. */
6757 if (ignore)
6758 target = const0_rtx;
6760 /* If the result of a pure or const built-in function is ignored, and
6761 none of its arguments are volatile, we can avoid expanding the
6762 built-in call and just evaluate the arguments for side-effects. */
6763 if (target == const0_rtx
6764 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6765 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6767 bool volatilep = false;
6768 tree arg;
6769 call_expr_arg_iterator iter;
6771 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6772 if (TREE_THIS_VOLATILE (arg))
6774 volatilep = true;
6775 break;
6778 if (! volatilep)
6780 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6781 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6782 return const0_rtx;
6786 /* expand_builtin_with_bounds is supposed to be used for
6787 instrumented builtin calls. */
6788 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6790 switch (fcode)
6792 CASE_FLT_FN (BUILT_IN_FABS):
6793 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
6794 case BUILT_IN_FABSD32:
6795 case BUILT_IN_FABSD64:
6796 case BUILT_IN_FABSD128:
6797 target = expand_builtin_fabs (exp, target, subtarget);
6798 if (target)
6799 return target;
6800 break;
6802 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6803 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
6804 target = expand_builtin_copysign (exp, target, subtarget);
6805 if (target)
6806 return target;
6807 break;
6809 /* Just do a normal library call if we were unable to fold
6810 the values. */
6811 CASE_FLT_FN (BUILT_IN_CABS):
6812 break;
6814 CASE_FLT_FN (BUILT_IN_FMA):
6815 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
6816 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6817 if (target)
6818 return target;
6819 break;
6821 CASE_FLT_FN (BUILT_IN_ILOGB):
6822 if (! flag_unsafe_math_optimizations)
6823 break;
6824 gcc_fallthrough ();
6825 CASE_FLT_FN (BUILT_IN_ISINF):
6826 CASE_FLT_FN (BUILT_IN_FINITE):
6827 case BUILT_IN_ISFINITE:
6828 case BUILT_IN_ISNORMAL:
6829 target = expand_builtin_interclass_mathfn (exp, target);
6830 if (target)
6831 return target;
6832 break;
6834 CASE_FLT_FN (BUILT_IN_ICEIL):
6835 CASE_FLT_FN (BUILT_IN_LCEIL):
6836 CASE_FLT_FN (BUILT_IN_LLCEIL):
6837 CASE_FLT_FN (BUILT_IN_LFLOOR):
6838 CASE_FLT_FN (BUILT_IN_IFLOOR):
6839 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6840 target = expand_builtin_int_roundingfn (exp, target);
6841 if (target)
6842 return target;
6843 break;
6845 CASE_FLT_FN (BUILT_IN_IRINT):
6846 CASE_FLT_FN (BUILT_IN_LRINT):
6847 CASE_FLT_FN (BUILT_IN_LLRINT):
6848 CASE_FLT_FN (BUILT_IN_IROUND):
6849 CASE_FLT_FN (BUILT_IN_LROUND):
6850 CASE_FLT_FN (BUILT_IN_LLROUND):
6851 target = expand_builtin_int_roundingfn_2 (exp, target);
6852 if (target)
6853 return target;
6854 break;
6856 CASE_FLT_FN (BUILT_IN_POWI):
6857 target = expand_builtin_powi (exp, target);
6858 if (target)
6859 return target;
6860 break;
6862 CASE_FLT_FN (BUILT_IN_CEXPI):
6863 target = expand_builtin_cexpi (exp, target);
6864 gcc_assert (target);
6865 return target;
6867 CASE_FLT_FN (BUILT_IN_SIN):
6868 CASE_FLT_FN (BUILT_IN_COS):
6869 if (! flag_unsafe_math_optimizations)
6870 break;
6871 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6872 if (target)
6873 return target;
6874 break;
6876 CASE_FLT_FN (BUILT_IN_SINCOS):
6877 if (! flag_unsafe_math_optimizations)
6878 break;
6879 target = expand_builtin_sincos (exp);
6880 if (target)
6881 return target;
6882 break;
6884 case BUILT_IN_APPLY_ARGS:
6885 return expand_builtin_apply_args ();
6887 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6888 FUNCTION with a copy of the parameters described by
6889 ARGUMENTS, and ARGSIZE. It returns a block of memory
6890 allocated on the stack into which is stored all the registers
6891 that might possibly be used for returning the result of a
6892 function. ARGUMENTS is the value returned by
6893 __builtin_apply_args. ARGSIZE is the number of bytes of
6894 arguments that must be copied. ??? How should this value be
6895 computed? We'll also need a safe worst case value for varargs
6896 functions. */
6897 case BUILT_IN_APPLY:
6898 if (!validate_arglist (exp, POINTER_TYPE,
6899 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6900 && !validate_arglist (exp, REFERENCE_TYPE,
6901 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6902 return const0_rtx;
6903 else
6905 rtx ops[3];
6907 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6908 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6909 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6911 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6914 /* __builtin_return (RESULT) causes the function to return the
6915 value described by RESULT. RESULT is address of the block of
6916 memory returned by __builtin_apply. */
6917 case BUILT_IN_RETURN:
6918 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6919 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6920 return const0_rtx;
6922 case BUILT_IN_SAVEREGS:
6923 return expand_builtin_saveregs ();
6925 case BUILT_IN_VA_ARG_PACK:
6926 /* All valid uses of __builtin_va_arg_pack () are removed during
6927 inlining. */
6928 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6929 return const0_rtx;
6931 case BUILT_IN_VA_ARG_PACK_LEN:
6932 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6933 inlining. */
6934 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6935 return const0_rtx;
6937 /* Return the address of the first anonymous stack arg. */
6938 case BUILT_IN_NEXT_ARG:
6939 if (fold_builtin_next_arg (exp, false))
6940 return const0_rtx;
6941 return expand_builtin_next_arg ();
6943 case BUILT_IN_CLEAR_CACHE:
6944 target = expand_builtin___clear_cache (exp);
6945 if (target)
6946 return target;
6947 break;
6949 case BUILT_IN_CLASSIFY_TYPE:
6950 return expand_builtin_classify_type (exp);
6952 case BUILT_IN_CONSTANT_P:
6953 return const0_rtx;
6955 case BUILT_IN_FRAME_ADDRESS:
6956 case BUILT_IN_RETURN_ADDRESS:
6957 return expand_builtin_frame_address (fndecl, exp);
6959 /* Returns the address of the area where the structure is returned.
6960 0 otherwise. */
6961 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6962 if (call_expr_nargs (exp) != 0
6963 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6964 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6965 return const0_rtx;
6966 else
6967 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6969 CASE_BUILT_IN_ALLOCA:
6970 target = expand_builtin_alloca (exp);
6971 if (target)
6972 return target;
6973 break;
6975 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
6976 return expand_asan_emit_allocas_unpoison (exp);
6978 case BUILT_IN_STACK_SAVE:
6979 return expand_stack_save ();
6981 case BUILT_IN_STACK_RESTORE:
6982 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6983 return const0_rtx;
6985 case BUILT_IN_BSWAP16:
6986 case BUILT_IN_BSWAP32:
6987 case BUILT_IN_BSWAP64:
6988 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6989 if (target)
6990 return target;
6991 break;
6993 CASE_INT_FN (BUILT_IN_FFS):
6994 target = expand_builtin_unop (target_mode, exp, target,
6995 subtarget, ffs_optab);
6996 if (target)
6997 return target;
6998 break;
7000 CASE_INT_FN (BUILT_IN_CLZ):
7001 target = expand_builtin_unop (target_mode, exp, target,
7002 subtarget, clz_optab);
7003 if (target)
7004 return target;
7005 break;
7007 CASE_INT_FN (BUILT_IN_CTZ):
7008 target = expand_builtin_unop (target_mode, exp, target,
7009 subtarget, ctz_optab);
7010 if (target)
7011 return target;
7012 break;
7014 CASE_INT_FN (BUILT_IN_CLRSB):
7015 target = expand_builtin_unop (target_mode, exp, target,
7016 subtarget, clrsb_optab);
7017 if (target)
7018 return target;
7019 break;
7021 CASE_INT_FN (BUILT_IN_POPCOUNT):
7022 target = expand_builtin_unop (target_mode, exp, target,
7023 subtarget, popcount_optab);
7024 if (target)
7025 return target;
7026 break;
7028 CASE_INT_FN (BUILT_IN_PARITY):
7029 target = expand_builtin_unop (target_mode, exp, target,
7030 subtarget, parity_optab);
7031 if (target)
7032 return target;
7033 break;
7035 case BUILT_IN_STRLEN:
7036 target = expand_builtin_strlen (exp, target, target_mode);
7037 if (target)
7038 return target;
7039 break;
7041 case BUILT_IN_STRCAT:
7042 target = expand_builtin_strcat (exp, target);
7043 if (target)
7044 return target;
7045 break;
7047 case BUILT_IN_STRCPY:
7048 target = expand_builtin_strcpy (exp, target);
7049 if (target)
7050 return target;
7051 break;
7053 case BUILT_IN_STRNCAT:
7054 target = expand_builtin_strncat (exp, target);
7055 if (target)
7056 return target;
7057 break;
7059 case BUILT_IN_STRNCPY:
7060 target = expand_builtin_strncpy (exp, target);
7061 if (target)
7062 return target;
7063 break;
7065 case BUILT_IN_STPCPY:
7066 target = expand_builtin_stpcpy (exp, target, mode);
7067 if (target)
7068 return target;
7069 break;
7071 case BUILT_IN_STPNCPY:
7072 target = expand_builtin_stpncpy (exp, target);
7073 if (target)
7074 return target;
7075 break;
7077 case BUILT_IN_MEMCHR:
7078 target = expand_builtin_memchr (exp, target);
7079 if (target)
7080 return target;
7081 break;
7083 case BUILT_IN_MEMCPY:
7084 target = expand_builtin_memcpy (exp, target);
7085 if (target)
7086 return target;
7087 break;
7089 case BUILT_IN_MEMMOVE:
7090 target = expand_builtin_memmove (exp, target);
7091 if (target)
7092 return target;
7093 break;
7095 case BUILT_IN_MEMPCPY:
7096 target = expand_builtin_mempcpy (exp, target);
7097 if (target)
7098 return target;
7099 break;
7101 case BUILT_IN_MEMSET:
7102 target = expand_builtin_memset (exp, target, mode);
7103 if (target)
7104 return target;
7105 break;
7107 case BUILT_IN_BZERO:
7108 target = expand_builtin_bzero (exp);
7109 if (target)
7110 return target;
7111 break;
7113 case BUILT_IN_STRCMP:
7114 target = expand_builtin_strcmp (exp, target);
7115 if (target)
7116 return target;
7117 break;
7119 case BUILT_IN_STRNCMP:
7120 target = expand_builtin_strncmp (exp, target, mode);
7121 if (target)
7122 return target;
7123 break;
7125 case BUILT_IN_BCMP:
7126 case BUILT_IN_MEMCMP:
7127 case BUILT_IN_MEMCMP_EQ:
7128 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7129 if (target)
7130 return target;
7131 if (fcode == BUILT_IN_MEMCMP_EQ)
7133 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7134 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7136 break;
7138 case BUILT_IN_SETJMP:
7139 /* This should have been lowered to the builtins below. */
7140 gcc_unreachable ();
7142 case BUILT_IN_SETJMP_SETUP:
7143 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7144 and the receiver label. */
7145 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7147 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7148 VOIDmode, EXPAND_NORMAL);
7149 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7150 rtx_insn *label_r = label_rtx (label);
7152 /* This is copied from the handling of non-local gotos. */
7153 expand_builtin_setjmp_setup (buf_addr, label_r);
7154 nonlocal_goto_handler_labels
7155 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7156 nonlocal_goto_handler_labels);
7157 /* ??? Do not let expand_label treat us as such since we would
7158 not want to be both on the list of non-local labels and on
7159 the list of forced labels. */
7160 FORCED_LABEL (label) = 0;
7161 return const0_rtx;
7163 break;
7165 case BUILT_IN_SETJMP_RECEIVER:
7166 /* __builtin_setjmp_receiver is passed the receiver label. */
7167 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7169 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7170 rtx_insn *label_r = label_rtx (label);
7172 expand_builtin_setjmp_receiver (label_r);
7173 return const0_rtx;
7175 break;
7177 /* __builtin_longjmp is passed a pointer to an array of five words.
7178 It's similar to the C library longjmp function but works with
7179 __builtin_setjmp above. */
7180 case BUILT_IN_LONGJMP:
7181 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7183 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7184 VOIDmode, EXPAND_NORMAL);
7185 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7187 if (value != const1_rtx)
7189 error ("%<__builtin_longjmp%> second argument must be 1");
7190 return const0_rtx;
7193 expand_builtin_longjmp (buf_addr, value);
7194 return const0_rtx;
7196 break;
7198 case BUILT_IN_NONLOCAL_GOTO:
7199 target = expand_builtin_nonlocal_goto (exp);
7200 if (target)
7201 return target;
7202 break;
7204 /* This updates the setjmp buffer that is its argument with the value
7205 of the current stack pointer. */
7206 case BUILT_IN_UPDATE_SETJMP_BUF:
7207 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7209 rtx buf_addr
7210 = expand_normal (CALL_EXPR_ARG (exp, 0));
7212 expand_builtin_update_setjmp_buf (buf_addr);
7213 return const0_rtx;
7215 break;
7217 case BUILT_IN_TRAP:
7218 expand_builtin_trap ();
7219 return const0_rtx;
7221 case BUILT_IN_UNREACHABLE:
7222 expand_builtin_unreachable ();
7223 return const0_rtx;
7225 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7226 case BUILT_IN_SIGNBITD32:
7227 case BUILT_IN_SIGNBITD64:
7228 case BUILT_IN_SIGNBITD128:
7229 target = expand_builtin_signbit (exp, target);
7230 if (target)
7231 return target;
7232 break;
7234 /* Various hooks for the DWARF 2 __throw routine. */
7235 case BUILT_IN_UNWIND_INIT:
7236 expand_builtin_unwind_init ();
7237 return const0_rtx;
7238 case BUILT_IN_DWARF_CFA:
7239 return virtual_cfa_rtx;
7240 #ifdef DWARF2_UNWIND_INFO
7241 case BUILT_IN_DWARF_SP_COLUMN:
7242 return expand_builtin_dwarf_sp_column ();
7243 case BUILT_IN_INIT_DWARF_REG_SIZES:
7244 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7245 return const0_rtx;
7246 #endif
7247 case BUILT_IN_FROB_RETURN_ADDR:
7248 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7249 case BUILT_IN_EXTRACT_RETURN_ADDR:
7250 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7251 case BUILT_IN_EH_RETURN:
7252 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7253 CALL_EXPR_ARG (exp, 1));
7254 return const0_rtx;
7255 case BUILT_IN_EH_RETURN_DATA_REGNO:
7256 return expand_builtin_eh_return_data_regno (exp);
7257 case BUILT_IN_EXTEND_POINTER:
7258 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7259 case BUILT_IN_EH_POINTER:
7260 return expand_builtin_eh_pointer (exp);
7261 case BUILT_IN_EH_FILTER:
7262 return expand_builtin_eh_filter (exp);
7263 case BUILT_IN_EH_COPY_VALUES:
7264 return expand_builtin_eh_copy_values (exp);
7266 case BUILT_IN_VA_START:
7267 return expand_builtin_va_start (exp);
7268 case BUILT_IN_VA_END:
7269 return expand_builtin_va_end (exp);
7270 case BUILT_IN_VA_COPY:
7271 return expand_builtin_va_copy (exp);
7272 case BUILT_IN_EXPECT:
7273 return expand_builtin_expect (exp, target);
7274 case BUILT_IN_ASSUME_ALIGNED:
7275 return expand_builtin_assume_aligned (exp, target);
7276 case BUILT_IN_PREFETCH:
7277 expand_builtin_prefetch (exp);
7278 return const0_rtx;
7280 case BUILT_IN_INIT_TRAMPOLINE:
7281 return expand_builtin_init_trampoline (exp, true);
7282 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7283 return expand_builtin_init_trampoline (exp, false);
7284 case BUILT_IN_ADJUST_TRAMPOLINE:
7285 return expand_builtin_adjust_trampoline (exp);
7287 case BUILT_IN_INIT_DESCRIPTOR:
7288 return expand_builtin_init_descriptor (exp);
7289 case BUILT_IN_ADJUST_DESCRIPTOR:
7290 return expand_builtin_adjust_descriptor (exp);
7292 case BUILT_IN_FORK:
7293 case BUILT_IN_EXECL:
7294 case BUILT_IN_EXECV:
7295 case BUILT_IN_EXECLP:
7296 case BUILT_IN_EXECLE:
7297 case BUILT_IN_EXECVP:
7298 case BUILT_IN_EXECVE:
7299 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7300 if (target)
7301 return target;
7302 break;
7304 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7305 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7306 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7307 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7308 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7309 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7310 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7311 if (target)
7312 return target;
7313 break;
7315 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7316 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7317 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7318 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7319 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7320 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7321 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7322 if (target)
7323 return target;
7324 break;
7326 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7327 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7328 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7329 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7330 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7331 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7332 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7333 if (target)
7334 return target;
7335 break;
7337 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7338 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7339 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7340 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7341 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7342 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7343 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7344 if (target)
7345 return target;
7346 break;
7348 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7349 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7350 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7351 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7352 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7353 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7354 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7355 if (target)
7356 return target;
7357 break;
7359 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7360 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7361 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7362 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7363 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7364 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7365 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7366 if (target)
7367 return target;
7368 break;
7370 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7371 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7372 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7373 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7374 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7375 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7376 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7377 if (target)
7378 return target;
7379 break;
7381 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7382 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7383 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7384 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7385 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7386 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7387 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7388 if (target)
7389 return target;
7390 break;
7392 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7393 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7394 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7395 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7396 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7397 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7398 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7399 if (target)
7400 return target;
7401 break;
7403 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7404 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7405 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7406 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7407 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7408 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7409 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7410 if (target)
7411 return target;
7412 break;
7414 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7415 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7416 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7417 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7418 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7419 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7420 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7421 if (target)
7422 return target;
7423 break;
7425 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7426 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7427 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7428 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7429 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7430 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7431 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7432 if (target)
7433 return target;
7434 break;
7436 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7437 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7438 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7439 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7440 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7441 if (mode == VOIDmode)
7442 mode = TYPE_MODE (boolean_type_node);
7443 if (!target || !register_operand (target, mode))
7444 target = gen_reg_rtx (mode);
7446 mode = get_builtin_sync_mode
7447 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7448 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7449 if (target)
7450 return target;
7451 break;
7453 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7454 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7455 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7456 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7457 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7458 mode = get_builtin_sync_mode
7459 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7460 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7461 if (target)
7462 return target;
7463 break;
7465 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7466 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7467 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7468 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7469 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7470 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7471 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7472 if (target)
7473 return target;
7474 break;
7476 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7477 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7478 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7479 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7480 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7481 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7482 expand_builtin_sync_lock_release (mode, exp);
7483 return const0_rtx;
7485 case BUILT_IN_SYNC_SYNCHRONIZE:
7486 expand_builtin_sync_synchronize ();
7487 return const0_rtx;
7489 case BUILT_IN_ATOMIC_EXCHANGE_1:
7490 case BUILT_IN_ATOMIC_EXCHANGE_2:
7491 case BUILT_IN_ATOMIC_EXCHANGE_4:
7492 case BUILT_IN_ATOMIC_EXCHANGE_8:
7493 case BUILT_IN_ATOMIC_EXCHANGE_16:
7494 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7495 target = expand_builtin_atomic_exchange (mode, exp, target);
7496 if (target)
7497 return target;
7498 break;
7500 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7501 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7502 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7503 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7504 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7506 unsigned int nargs, z;
7507 vec<tree, va_gc> *vec;
7509 mode =
7510 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7511 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7512 if (target)
7513 return target;
7515 /* If this is turned into an external library call, the weak parameter
7516 must be dropped to match the expected parameter list. */
7517 nargs = call_expr_nargs (exp);
7518 vec_alloc (vec, nargs - 1);
7519 for (z = 0; z < 3; z++)
7520 vec->quick_push (CALL_EXPR_ARG (exp, z));
7521 /* Skip the boolean weak parameter. */
7522 for (z = 4; z < 6; z++)
7523 vec->quick_push (CALL_EXPR_ARG (exp, z));
7524 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7525 break;
7528 case BUILT_IN_ATOMIC_LOAD_1:
7529 case BUILT_IN_ATOMIC_LOAD_2:
7530 case BUILT_IN_ATOMIC_LOAD_4:
7531 case BUILT_IN_ATOMIC_LOAD_8:
7532 case BUILT_IN_ATOMIC_LOAD_16:
7533 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7534 target = expand_builtin_atomic_load (mode, exp, target);
7535 if (target)
7536 return target;
7537 break;
7539 case BUILT_IN_ATOMIC_STORE_1:
7540 case BUILT_IN_ATOMIC_STORE_2:
7541 case BUILT_IN_ATOMIC_STORE_4:
7542 case BUILT_IN_ATOMIC_STORE_8:
7543 case BUILT_IN_ATOMIC_STORE_16:
7544 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7545 target = expand_builtin_atomic_store (mode, exp);
7546 if (target)
7547 return const0_rtx;
7548 break;
7550 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7551 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7552 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7553 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7554 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7556 enum built_in_function lib;
7557 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7558 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7559 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7560 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7561 ignore, lib);
7562 if (target)
7563 return target;
7564 break;
7566 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7567 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7568 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7569 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7570 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7572 enum built_in_function lib;
7573 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7574 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7575 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7576 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7577 ignore, lib);
7578 if (target)
7579 return target;
7580 break;
7582 case BUILT_IN_ATOMIC_AND_FETCH_1:
7583 case BUILT_IN_ATOMIC_AND_FETCH_2:
7584 case BUILT_IN_ATOMIC_AND_FETCH_4:
7585 case BUILT_IN_ATOMIC_AND_FETCH_8:
7586 case BUILT_IN_ATOMIC_AND_FETCH_16:
7588 enum built_in_function lib;
7589 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7590 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7591 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7592 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7593 ignore, lib);
7594 if (target)
7595 return target;
7596 break;
7598 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7599 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7600 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7601 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7602 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7604 enum built_in_function lib;
7605 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7606 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7607 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7608 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7609 ignore, lib);
7610 if (target)
7611 return target;
7612 break;
7614 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7615 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7616 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7617 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7618 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7620 enum built_in_function lib;
7621 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7622 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7623 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7624 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7625 ignore, lib);
7626 if (target)
7627 return target;
7628 break;
7630 case BUILT_IN_ATOMIC_OR_FETCH_1:
7631 case BUILT_IN_ATOMIC_OR_FETCH_2:
7632 case BUILT_IN_ATOMIC_OR_FETCH_4:
7633 case BUILT_IN_ATOMIC_OR_FETCH_8:
7634 case BUILT_IN_ATOMIC_OR_FETCH_16:
7636 enum built_in_function lib;
7637 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7638 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7639 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7640 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7641 ignore, lib);
7642 if (target)
7643 return target;
7644 break;
7646 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7647 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7648 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7649 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7650 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7651 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7652 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7653 ignore, BUILT_IN_NONE);
7654 if (target)
7655 return target;
7656 break;
7658 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7659 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7660 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7661 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7662 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7663 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7664 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7665 ignore, BUILT_IN_NONE);
7666 if (target)
7667 return target;
7668 break;
7670 case BUILT_IN_ATOMIC_FETCH_AND_1:
7671 case BUILT_IN_ATOMIC_FETCH_AND_2:
7672 case BUILT_IN_ATOMIC_FETCH_AND_4:
7673 case BUILT_IN_ATOMIC_FETCH_AND_8:
7674 case BUILT_IN_ATOMIC_FETCH_AND_16:
7675 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7676 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7677 ignore, BUILT_IN_NONE);
7678 if (target)
7679 return target;
7680 break;
7682 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7683 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7684 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7685 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7686 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7687 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7688 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7689 ignore, BUILT_IN_NONE);
7690 if (target)
7691 return target;
7692 break;
7694 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7695 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7696 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7697 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7698 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7699 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7700 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7701 ignore, BUILT_IN_NONE);
7702 if (target)
7703 return target;
7704 break;
7706 case BUILT_IN_ATOMIC_FETCH_OR_1:
7707 case BUILT_IN_ATOMIC_FETCH_OR_2:
7708 case BUILT_IN_ATOMIC_FETCH_OR_4:
7709 case BUILT_IN_ATOMIC_FETCH_OR_8:
7710 case BUILT_IN_ATOMIC_FETCH_OR_16:
7711 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7712 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7713 ignore, BUILT_IN_NONE);
7714 if (target)
7715 return target;
7716 break;
7718 case BUILT_IN_ATOMIC_TEST_AND_SET:
7719 return expand_builtin_atomic_test_and_set (exp, target);
7721 case BUILT_IN_ATOMIC_CLEAR:
7722 return expand_builtin_atomic_clear (exp);
7724 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
7725 return expand_builtin_atomic_always_lock_free (exp);
7727 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
7728 target = expand_builtin_atomic_is_lock_free (exp);
7729 if (target)
7730 return target;
7731 break;
7733 case BUILT_IN_ATOMIC_THREAD_FENCE:
7734 expand_builtin_atomic_thread_fence (exp);
7735 return const0_rtx;
7737 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
7738 expand_builtin_atomic_signal_fence (exp);
7739 return const0_rtx;
7741 case BUILT_IN_OBJECT_SIZE:
7742 return expand_builtin_object_size (exp);
7744 case BUILT_IN_MEMCPY_CHK:
7745 case BUILT_IN_MEMPCPY_CHK:
7746 case BUILT_IN_MEMMOVE_CHK:
7747 case BUILT_IN_MEMSET_CHK:
7748 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7749 if (target)
7750 return target;
7751 break;
7753 case BUILT_IN_STRCPY_CHK:
7754 case BUILT_IN_STPCPY_CHK:
7755 case BUILT_IN_STRNCPY_CHK:
7756 case BUILT_IN_STPNCPY_CHK:
7757 case BUILT_IN_STRCAT_CHK:
7758 case BUILT_IN_STRNCAT_CHK:
7759 case BUILT_IN_SNPRINTF_CHK:
7760 case BUILT_IN_VSNPRINTF_CHK:
7761 maybe_emit_chk_warning (exp, fcode);
7762 break;
7764 case BUILT_IN_SPRINTF_CHK:
7765 case BUILT_IN_VSPRINTF_CHK:
7766 maybe_emit_sprintf_chk_warning (exp, fcode);
7767 break;
7769 case BUILT_IN_FREE:
7770 if (warn_free_nonheap_object)
7771 maybe_emit_free_warning (exp);
7772 break;
7774 case BUILT_IN_THREAD_POINTER:
7775 return expand_builtin_thread_pointer (exp, target);
7777 case BUILT_IN_SET_THREAD_POINTER:
7778 expand_builtin_set_thread_pointer (exp);
7779 return const0_rtx;
7781 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7782 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7783 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7784 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7785 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7786 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7787 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7788 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7789 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7790 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7791 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7792 /* We allow user CHKP builtins if Pointer Bounds
7793 Checker is off. */
7794 if (!chkp_function_instrumented_p (current_function_decl))
7796 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7797 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7798 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7799 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7800 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7801 return expand_normal (CALL_EXPR_ARG (exp, 0));
7802 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7803 return expand_normal (size_zero_node);
7804 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7805 return expand_normal (size_int (-1));
7806 else
7807 return const0_rtx;
7809 /* FALLTHROUGH */
7811 case BUILT_IN_CHKP_BNDMK:
7812 case BUILT_IN_CHKP_BNDSTX:
7813 case BUILT_IN_CHKP_BNDCL:
7814 case BUILT_IN_CHKP_BNDCU:
7815 case BUILT_IN_CHKP_BNDLDX:
7816 case BUILT_IN_CHKP_BNDRET:
7817 case BUILT_IN_CHKP_INTERSECT:
7818 case BUILT_IN_CHKP_NARROW:
7819 case BUILT_IN_CHKP_EXTRACT_LOWER:
7820 case BUILT_IN_CHKP_EXTRACT_UPPER:
7821 /* Software implementation of Pointer Bounds Checker is NYI.
7822 Target support is required. */
7823 error ("Your target platform does not support -fcheck-pointer-bounds");
7824 break;
7826 case BUILT_IN_ACC_ON_DEVICE:
7827 /* Do library call, if we failed to expand the builtin when
7828 folding. */
7829 break;
7831 case BUILT_IN_GOACC_PARLEVEL_ID:
7832 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7833 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
7835 default: /* just do library call, if unknown builtin */
7836 break;
7839 /* The switch statement above can drop through to cause the function
7840 to be called normally. */
7841 return expand_call (exp, target, ignore);
7844 /* Similar to expand_builtin but is used for instrumented calls. */
7847 expand_builtin_with_bounds (tree exp, rtx target,
7848 rtx subtarget ATTRIBUTE_UNUSED,
7849 machine_mode mode, int ignore)
7851 tree fndecl = get_callee_fndecl (exp);
7852 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7854 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7856 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7857 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7859 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7860 && fcode < END_CHKP_BUILTINS);
7862 switch (fcode)
7864 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7865 target = expand_builtin_memcpy_with_bounds (exp, target);
7866 if (target)
7867 return target;
7868 break;
7870 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7871 target = expand_builtin_mempcpy_with_bounds (exp, target);
7872 if (target)
7873 return target;
7874 break;
7876 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7877 target = expand_builtin_memset_with_bounds (exp, target, mode);
7878 if (target)
7879 return target;
7880 break;
7882 case BUILT_IN_MEMCPY_CHKP:
7883 case BUILT_IN_MEMMOVE_CHKP:
7884 case BUILT_IN_MEMPCPY_CHKP:
7885 if (call_expr_nargs (exp) > 3)
7887 /* memcpy_chkp (void *dst, size_t dstbnd,
7888 const void *src, size_t srcbnd, size_t n)
7889 and others take a pointer bound argument just after each
7890 pointer argument. */
7891 tree dest = CALL_EXPR_ARG (exp, 0);
7892 tree src = CALL_EXPR_ARG (exp, 2);
7893 tree len = CALL_EXPR_ARG (exp, 4);
7895 check_memop_access (exp, dest, src, len);
7896 break;
7899 default:
7900 break;
7903 /* The switch statement above can drop through to cause the function
7904 to be called normally. */
7905 return expand_call (exp, target, ignore);
7908 /* Determine whether a tree node represents a call to a built-in
7909 function. If the tree T is a call to a built-in function with
7910 the right number of arguments of the appropriate types, return
7911 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7912 Otherwise the return value is END_BUILTINS. */
7914 enum built_in_function
7915 builtin_mathfn_code (const_tree t)
7917 const_tree fndecl, arg, parmlist;
7918 const_tree argtype, parmtype;
7919 const_call_expr_arg_iterator iter;
7921 if (TREE_CODE (t) != CALL_EXPR)
7922 return END_BUILTINS;
7924 fndecl = get_callee_fndecl (t);
7925 if (fndecl == NULL_TREE
7926 || TREE_CODE (fndecl) != FUNCTION_DECL
7927 || ! DECL_BUILT_IN (fndecl)
7928 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7929 return END_BUILTINS;
7931 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7932 init_const_call_expr_arg_iterator (t, &iter);
7933 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7935 /* If a function doesn't take a variable number of arguments,
7936 the last element in the list will have type `void'. */
7937 parmtype = TREE_VALUE (parmlist);
7938 if (VOID_TYPE_P (parmtype))
7940 if (more_const_call_expr_args_p (&iter))
7941 return END_BUILTINS;
7942 return DECL_FUNCTION_CODE (fndecl);
7945 if (! more_const_call_expr_args_p (&iter))
7946 return END_BUILTINS;
7948 arg = next_const_call_expr_arg (&iter);
7949 argtype = TREE_TYPE (arg);
7951 if (SCALAR_FLOAT_TYPE_P (parmtype))
7953 if (! SCALAR_FLOAT_TYPE_P (argtype))
7954 return END_BUILTINS;
7956 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7958 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7959 return END_BUILTINS;
7961 else if (POINTER_TYPE_P (parmtype))
7963 if (! POINTER_TYPE_P (argtype))
7964 return END_BUILTINS;
7966 else if (INTEGRAL_TYPE_P (parmtype))
7968 if (! INTEGRAL_TYPE_P (argtype))
7969 return END_BUILTINS;
7971 else
7972 return END_BUILTINS;
7975 /* Variable-length argument list. */
7976 return DECL_FUNCTION_CODE (fndecl);
7979 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7980 evaluate to a constant. */
7982 static tree
7983 fold_builtin_constant_p (tree arg)
7985 /* We return 1 for a numeric type that's known to be a constant
7986 value at compile-time or for an aggregate type that's a
7987 literal constant. */
7988 STRIP_NOPS (arg);
7990 /* If we know this is a constant, emit the constant of one. */
7991 if (CONSTANT_CLASS_P (arg)
7992 || (TREE_CODE (arg) == CONSTRUCTOR
7993 && TREE_CONSTANT (arg)))
7994 return integer_one_node;
7995 if (TREE_CODE (arg) == ADDR_EXPR)
7997 tree op = TREE_OPERAND (arg, 0);
7998 if (TREE_CODE (op) == STRING_CST
7999 || (TREE_CODE (op) == ARRAY_REF
8000 && integer_zerop (TREE_OPERAND (op, 1))
8001 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8002 return integer_one_node;
8005 /* If this expression has side effects, show we don't know it to be a
8006 constant. Likewise if it's a pointer or aggregate type since in
8007 those case we only want literals, since those are only optimized
8008 when generating RTL, not later.
8009 And finally, if we are compiling an initializer, not code, we
8010 need to return a definite result now; there's not going to be any
8011 more optimization done. */
8012 if (TREE_SIDE_EFFECTS (arg)
8013 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8014 || POINTER_TYPE_P (TREE_TYPE (arg))
8015 || cfun == 0
8016 || folding_initializer
8017 || force_folding_builtin_constant_p)
8018 return integer_zero_node;
8020 return NULL_TREE;
8023 /* Create builtin_expect with PRED and EXPECTED as its arguments and
8024 return it as a truthvalue. */
8026 static tree
8027 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8028 tree predictor)
8030 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8032 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
8033 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8034 ret_type = TREE_TYPE (TREE_TYPE (fn));
8035 pred_type = TREE_VALUE (arg_types);
8036 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8038 pred = fold_convert_loc (loc, pred_type, pred);
8039 expected = fold_convert_loc (loc, expected_type, expected);
8040 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8041 predictor);
8043 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8044 build_int_cst (ret_type, 0));
8047 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
8048 NULL_TREE if no simplification is possible. */
8050 tree
8051 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
8053 tree inner, fndecl, inner_arg0;
8054 enum tree_code code;
8056 /* Distribute the expected value over short-circuiting operators.
8057 See through the cast from truthvalue_type_node to long. */
8058 inner_arg0 = arg0;
8059 while (CONVERT_EXPR_P (inner_arg0)
8060 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8061 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8062 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8064 /* If this is a builtin_expect within a builtin_expect keep the
8065 inner one. See through a comparison against a constant. It
8066 might have been added to create a thruthvalue. */
8067 inner = inner_arg0;
8069 if (COMPARISON_CLASS_P (inner)
8070 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8071 inner = TREE_OPERAND (inner, 0);
8073 if (TREE_CODE (inner) == CALL_EXPR
8074 && (fndecl = get_callee_fndecl (inner))
8075 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
8076 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
8077 return arg0;
8079 inner = inner_arg0;
8080 code = TREE_CODE (inner);
8081 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8083 tree op0 = TREE_OPERAND (inner, 0);
8084 tree op1 = TREE_OPERAND (inner, 1);
8085 arg1 = save_expr (arg1);
8087 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
8088 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
8089 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8091 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8094 /* If the argument isn't invariant then there's nothing else we can do. */
8095 if (!TREE_CONSTANT (inner_arg0))
8096 return NULL_TREE;
8098 /* If we expect that a comparison against the argument will fold to
8099 a constant return the constant. In practice, this means a true
8100 constant or the address of a non-weak symbol. */
8101 inner = inner_arg0;
8102 STRIP_NOPS (inner);
8103 if (TREE_CODE (inner) == ADDR_EXPR)
8107 inner = TREE_OPERAND (inner, 0);
8109 while (TREE_CODE (inner) == COMPONENT_REF
8110 || TREE_CODE (inner) == ARRAY_REF);
8111 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8112 return NULL_TREE;
8115 /* Otherwise, ARG0 already has the proper type for the return value. */
8116 return arg0;
8119 /* Fold a call to __builtin_classify_type with argument ARG. */
8121 static tree
8122 fold_builtin_classify_type (tree arg)
8124 if (arg == 0)
8125 return build_int_cst (integer_type_node, no_type_class);
8127 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8130 /* Fold a call to __builtin_strlen with argument ARG. */
8132 static tree
8133 fold_builtin_strlen (location_t loc, tree type, tree arg)
8135 if (!validate_arg (arg, POINTER_TYPE))
8136 return NULL_TREE;
8137 else
8139 tree len = c_strlen (arg, 0);
8141 if (len)
8142 return fold_convert_loc (loc, type, len);
8144 return NULL_TREE;
8148 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8150 static tree
8151 fold_builtin_inf (location_t loc, tree type, int warn)
8153 REAL_VALUE_TYPE real;
8155 /* __builtin_inff is intended to be usable to define INFINITY on all
8156 targets. If an infinity is not available, INFINITY expands "to a
8157 positive constant of type float that overflows at translation
8158 time", footnote "In this case, using INFINITY will violate the
8159 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8160 Thus we pedwarn to ensure this constraint violation is
8161 diagnosed. */
8162 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8163 pedwarn (loc, 0, "target format does not support infinity");
8165 real_inf (&real);
8166 return build_real (type, real);
8169 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8170 NULL_TREE if no simplification can be made. */
8172 static tree
8173 fold_builtin_sincos (location_t loc,
8174 tree arg0, tree arg1, tree arg2)
8176 tree type;
8177 tree fndecl, call = NULL_TREE;
8179 if (!validate_arg (arg0, REAL_TYPE)
8180 || !validate_arg (arg1, POINTER_TYPE)
8181 || !validate_arg (arg2, POINTER_TYPE))
8182 return NULL_TREE;
8184 type = TREE_TYPE (arg0);
8186 /* Calculate the result when the argument is a constant. */
8187 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8188 if (fn == END_BUILTINS)
8189 return NULL_TREE;
8191 /* Canonicalize sincos to cexpi. */
8192 if (TREE_CODE (arg0) == REAL_CST)
8194 tree complex_type = build_complex_type (type);
8195 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8197 if (!call)
8199 if (!targetm.libc_has_function (function_c99_math_complex)
8200 || !builtin_decl_implicit_p (fn))
8201 return NULL_TREE;
8202 fndecl = builtin_decl_explicit (fn);
8203 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8204 call = builtin_save_expr (call);
8207 return build2 (COMPOUND_EXPR, void_type_node,
8208 build2 (MODIFY_EXPR, void_type_node,
8209 build_fold_indirect_ref_loc (loc, arg1),
8210 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8211 build2 (MODIFY_EXPR, void_type_node,
8212 build_fold_indirect_ref_loc (loc, arg2),
8213 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8216 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8217 Return NULL_TREE if no simplification can be made. */
8219 static tree
8220 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8222 if (!validate_arg (arg1, POINTER_TYPE)
8223 || !validate_arg (arg2, POINTER_TYPE)
8224 || !validate_arg (len, INTEGER_TYPE))
8225 return NULL_TREE;
8227 /* If the LEN parameter is zero, return zero. */
8228 if (integer_zerop (len))
8229 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8230 arg1, arg2);
8232 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8233 if (operand_equal_p (arg1, arg2, 0))
8234 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8236 /* If len parameter is one, return an expression corresponding to
8237 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8238 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8240 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8241 tree cst_uchar_ptr_node
8242 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8244 tree ind1
8245 = fold_convert_loc (loc, integer_type_node,
8246 build1 (INDIRECT_REF, cst_uchar_node,
8247 fold_convert_loc (loc,
8248 cst_uchar_ptr_node,
8249 arg1)));
8250 tree ind2
8251 = fold_convert_loc (loc, integer_type_node,
8252 build1 (INDIRECT_REF, cst_uchar_node,
8253 fold_convert_loc (loc,
8254 cst_uchar_ptr_node,
8255 arg2)));
8256 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8259 return NULL_TREE;
8262 /* Fold a call to builtin isascii with argument ARG. */
8264 static tree
8265 fold_builtin_isascii (location_t loc, tree arg)
8267 if (!validate_arg (arg, INTEGER_TYPE))
8268 return NULL_TREE;
8269 else
8271 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8272 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8273 build_int_cst (integer_type_node,
8274 ~ (unsigned HOST_WIDE_INT) 0x7f));
8275 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8276 arg, integer_zero_node);
8280 /* Fold a call to builtin toascii with argument ARG. */
8282 static tree
8283 fold_builtin_toascii (location_t loc, tree arg)
8285 if (!validate_arg (arg, INTEGER_TYPE))
8286 return NULL_TREE;
8288 /* Transform toascii(c) -> (c & 0x7f). */
8289 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8290 build_int_cst (integer_type_node, 0x7f));
8293 /* Fold a call to builtin isdigit with argument ARG. */
8295 static tree
8296 fold_builtin_isdigit (location_t loc, tree arg)
8298 if (!validate_arg (arg, INTEGER_TYPE))
8299 return NULL_TREE;
8300 else
8302 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8303 /* According to the C standard, isdigit is unaffected by locale.
8304 However, it definitely is affected by the target character set. */
8305 unsigned HOST_WIDE_INT target_digit0
8306 = lang_hooks.to_target_charset ('0');
8308 if (target_digit0 == 0)
8309 return NULL_TREE;
8311 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8312 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8313 build_int_cst (unsigned_type_node, target_digit0));
8314 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8315 build_int_cst (unsigned_type_node, 9));
8319 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8321 static tree
8322 fold_builtin_fabs (location_t loc, tree arg, tree type)
8324 if (!validate_arg (arg, REAL_TYPE))
8325 return NULL_TREE;
8327 arg = fold_convert_loc (loc, type, arg);
8328 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8331 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8333 static tree
8334 fold_builtin_abs (location_t loc, tree arg, tree type)
8336 if (!validate_arg (arg, INTEGER_TYPE))
8337 return NULL_TREE;
8339 arg = fold_convert_loc (loc, type, arg);
8340 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8343 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8345 static tree
8346 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
8348 /* ??? Only expand to FMA_EXPR if it's directly supported. */
8349 if (validate_arg (arg0, REAL_TYPE)
8350 && validate_arg (arg1, REAL_TYPE)
8351 && validate_arg (arg2, REAL_TYPE)
8352 && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
8353 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
8355 return NULL_TREE;
8358 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8360 static tree
8361 fold_builtin_carg (location_t loc, tree arg, tree type)
8363 if (validate_arg (arg, COMPLEX_TYPE)
8364 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8366 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8368 if (atan2_fn)
8370 tree new_arg = builtin_save_expr (arg);
8371 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8372 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8373 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8377 return NULL_TREE;
8380 /* Fold a call to builtin frexp, we can assume the base is 2. */
8382 static tree
8383 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8385 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8386 return NULL_TREE;
8388 STRIP_NOPS (arg0);
8390 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8391 return NULL_TREE;
8393 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8395 /* Proceed if a valid pointer type was passed in. */
8396 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8398 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8399 tree frac, exp;
8401 switch (value->cl)
8403 case rvc_zero:
8404 /* For +-0, return (*exp = 0, +-0). */
8405 exp = integer_zero_node;
8406 frac = arg0;
8407 break;
8408 case rvc_nan:
8409 case rvc_inf:
8410 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8411 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8412 case rvc_normal:
8414 /* Since the frexp function always expects base 2, and in
8415 GCC normalized significands are already in the range
8416 [0.5, 1.0), we have exactly what frexp wants. */
8417 REAL_VALUE_TYPE frac_rvt = *value;
8418 SET_REAL_EXP (&frac_rvt, 0);
8419 frac = build_real (rettype, frac_rvt);
8420 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8422 break;
8423 default:
8424 gcc_unreachable ();
8427 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8428 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8429 TREE_SIDE_EFFECTS (arg1) = 1;
8430 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8433 return NULL_TREE;
8436 /* Fold a call to builtin modf. */
8438 static tree
8439 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8441 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8442 return NULL_TREE;
8444 STRIP_NOPS (arg0);
8446 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8447 return NULL_TREE;
8449 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8451 /* Proceed if a valid pointer type was passed in. */
8452 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8454 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8455 REAL_VALUE_TYPE trunc, frac;
8457 switch (value->cl)
8459 case rvc_nan:
8460 case rvc_zero:
8461 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8462 trunc = frac = *value;
8463 break;
8464 case rvc_inf:
8465 /* For +-Inf, return (*arg1 = arg0, +-0). */
8466 frac = dconst0;
8467 frac.sign = value->sign;
8468 trunc = *value;
8469 break;
8470 case rvc_normal:
8471 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8472 real_trunc (&trunc, VOIDmode, value);
8473 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8474 /* If the original number was negative and already
8475 integral, then the fractional part is -0.0. */
8476 if (value->sign && frac.cl == rvc_zero)
8477 frac.sign = value->sign;
8478 break;
8481 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8482 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8483 build_real (rettype, trunc));
8484 TREE_SIDE_EFFECTS (arg1) = 1;
8485 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8486 build_real (rettype, frac));
8489 return NULL_TREE;
8492 /* Given a location LOC, an interclass builtin function decl FNDECL
8493 and its single argument ARG, return an folded expression computing
8494 the same, or NULL_TREE if we either couldn't or didn't want to fold
8495 (the latter happen if there's an RTL instruction available). */
8497 static tree
8498 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8500 machine_mode mode;
8502 if (!validate_arg (arg, REAL_TYPE))
8503 return NULL_TREE;
8505 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8506 return NULL_TREE;
8508 mode = TYPE_MODE (TREE_TYPE (arg));
8510 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8512 /* If there is no optab, try generic code. */
8513 switch (DECL_FUNCTION_CODE (fndecl))
8515 tree result;
8517 CASE_FLT_FN (BUILT_IN_ISINF):
8519 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8520 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8521 tree type = TREE_TYPE (arg);
8522 REAL_VALUE_TYPE r;
8523 char buf[128];
8525 if (is_ibm_extended)
8527 /* NaN and Inf are encoded in the high-order double value
8528 only. The low-order value is not significant. */
8529 type = double_type_node;
8530 mode = DFmode;
8531 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8533 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8534 real_from_string (&r, buf);
8535 result = build_call_expr (isgr_fn, 2,
8536 fold_build1_loc (loc, ABS_EXPR, type, arg),
8537 build_real (type, r));
8538 return result;
8540 CASE_FLT_FN (BUILT_IN_FINITE):
8541 case BUILT_IN_ISFINITE:
8543 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8544 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8545 tree type = TREE_TYPE (arg);
8546 REAL_VALUE_TYPE r;
8547 char buf[128];
8549 if (is_ibm_extended)
8551 /* NaN and Inf are encoded in the high-order double value
8552 only. The low-order value is not significant. */
8553 type = double_type_node;
8554 mode = DFmode;
8555 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8557 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8558 real_from_string (&r, buf);
8559 result = build_call_expr (isle_fn, 2,
8560 fold_build1_loc (loc, ABS_EXPR, type, arg),
8561 build_real (type, r));
8562 /*result = fold_build2_loc (loc, UNGT_EXPR,
8563 TREE_TYPE (TREE_TYPE (fndecl)),
8564 fold_build1_loc (loc, ABS_EXPR, type, arg),
8565 build_real (type, r));
8566 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8567 TREE_TYPE (TREE_TYPE (fndecl)),
8568 result);*/
8569 return result;
8571 case BUILT_IN_ISNORMAL:
8573 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8574 islessequal(fabs(x),DBL_MAX). */
8575 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8576 tree type = TREE_TYPE (arg);
8577 tree orig_arg, max_exp, min_exp;
8578 machine_mode orig_mode = mode;
8579 REAL_VALUE_TYPE rmax, rmin;
8580 char buf[128];
8582 orig_arg = arg = builtin_save_expr (arg);
8583 if (is_ibm_extended)
8585 /* Use double to test the normal range of IBM extended
8586 precision. Emin for IBM extended precision is
8587 different to emin for IEEE double, being 53 higher
8588 since the low double exponent is at least 53 lower
8589 than the high double exponent. */
8590 type = double_type_node;
8591 mode = DFmode;
8592 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8594 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8596 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8597 real_from_string (&rmax, buf);
8598 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8599 real_from_string (&rmin, buf);
8600 max_exp = build_real (type, rmax);
8601 min_exp = build_real (type, rmin);
8603 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8604 if (is_ibm_extended)
8606 /* Testing the high end of the range is done just using
8607 the high double, using the same test as isfinite().
8608 For the subnormal end of the range we first test the
8609 high double, then if its magnitude is equal to the
8610 limit of 0x1p-969, we test whether the low double is
8611 non-zero and opposite sign to the high double. */
8612 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8613 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8614 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8615 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8616 arg, min_exp);
8617 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8618 complex_double_type_node, orig_arg);
8619 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8620 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8621 tree zero = build_real (type, dconst0);
8622 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8623 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8624 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8625 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8626 fold_build3 (COND_EXPR,
8627 integer_type_node,
8628 hilt, logt, lolt));
8629 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8630 eq_min, ok_lo);
8631 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8632 gt_min, eq_min);
8634 else
8636 tree const isge_fn
8637 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8638 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8640 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8641 max_exp, min_exp);
8642 return result;
8644 default:
8645 break;
8648 return NULL_TREE;
8651 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8652 ARG is the argument for the call. */
8654 static tree
8655 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8657 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8659 if (!validate_arg (arg, REAL_TYPE))
8660 return NULL_TREE;
8662 switch (builtin_index)
8664 case BUILT_IN_ISINF:
8665 if (!HONOR_INFINITIES (arg))
8666 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8668 return NULL_TREE;
8670 case BUILT_IN_ISINF_SIGN:
8672 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8673 /* In a boolean context, GCC will fold the inner COND_EXPR to
8674 1. So e.g. "if (isinf_sign(x))" would be folded to just
8675 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8676 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8677 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8678 tree tmp = NULL_TREE;
8680 arg = builtin_save_expr (arg);
8682 if (signbit_fn && isinf_fn)
8684 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8685 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8687 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8688 signbit_call, integer_zero_node);
8689 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8690 isinf_call, integer_zero_node);
8692 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8693 integer_minus_one_node, integer_one_node);
8694 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8695 isinf_call, tmp,
8696 integer_zero_node);
8699 return tmp;
8702 case BUILT_IN_ISFINITE:
8703 if (!HONOR_NANS (arg)
8704 && !HONOR_INFINITIES (arg))
8705 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8707 return NULL_TREE;
8709 case BUILT_IN_ISNAN:
8710 if (!HONOR_NANS (arg))
8711 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8714 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8715 if (is_ibm_extended)
8717 /* NaN and Inf are encoded in the high-order double value
8718 only. The low-order value is not significant. */
8719 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8722 arg = builtin_save_expr (arg);
8723 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8725 default:
8726 gcc_unreachable ();
8730 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8731 This builtin will generate code to return the appropriate floating
8732 point classification depending on the value of the floating point
8733 number passed in. The possible return values must be supplied as
8734 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8735 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8736 one floating point argument which is "type generic". */
8738 static tree
8739 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8741 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8742 arg, type, res, tmp;
8743 machine_mode mode;
8744 REAL_VALUE_TYPE r;
8745 char buf[128];
8747 /* Verify the required arguments in the original call. */
8748 if (nargs != 6
8749 || !validate_arg (args[0], INTEGER_TYPE)
8750 || !validate_arg (args[1], INTEGER_TYPE)
8751 || !validate_arg (args[2], INTEGER_TYPE)
8752 || !validate_arg (args[3], INTEGER_TYPE)
8753 || !validate_arg (args[4], INTEGER_TYPE)
8754 || !validate_arg (args[5], REAL_TYPE))
8755 return NULL_TREE;
8757 fp_nan = args[0];
8758 fp_infinite = args[1];
8759 fp_normal = args[2];
8760 fp_subnormal = args[3];
8761 fp_zero = args[4];
8762 arg = args[5];
8763 type = TREE_TYPE (arg);
8764 mode = TYPE_MODE (type);
8765 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8767 /* fpclassify(x) ->
8768 isnan(x) ? FP_NAN :
8769 (fabs(x) == Inf ? FP_INFINITE :
8770 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8771 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8773 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8774 build_real (type, dconst0));
8775 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8776 tmp, fp_zero, fp_subnormal);
8778 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8779 real_from_string (&r, buf);
8780 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8781 arg, build_real (type, r));
8782 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8784 if (HONOR_INFINITIES (mode))
8786 real_inf (&r);
8787 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8788 build_real (type, r));
8789 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8790 fp_infinite, res);
8793 if (HONOR_NANS (mode))
8795 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8796 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8799 return res;
8802 /* Fold a call to an unordered comparison function such as
8803 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8804 being called and ARG0 and ARG1 are the arguments for the call.
8805 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8806 the opposite of the desired result. UNORDERED_CODE is used
8807 for modes that can hold NaNs and ORDERED_CODE is used for
8808 the rest. */
8810 static tree
8811 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
8812 enum tree_code unordered_code,
8813 enum tree_code ordered_code)
8815 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8816 enum tree_code code;
8817 tree type0, type1;
8818 enum tree_code code0, code1;
8819 tree cmp_type = NULL_TREE;
8821 type0 = TREE_TYPE (arg0);
8822 type1 = TREE_TYPE (arg1);
8824 code0 = TREE_CODE (type0);
8825 code1 = TREE_CODE (type1);
8827 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8828 /* Choose the wider of two real types. */
8829 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8830 ? type0 : type1;
8831 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8832 cmp_type = type0;
8833 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8834 cmp_type = type1;
8836 arg0 = fold_convert_loc (loc, cmp_type, arg0);
8837 arg1 = fold_convert_loc (loc, cmp_type, arg1);
8839 if (unordered_code == UNORDERED_EXPR)
8841 if (!HONOR_NANS (arg0))
8842 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
8843 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
8846 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
8847 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
8848 fold_build2_loc (loc, code, type, arg0, arg1));
8851 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8852 arithmetics if it can never overflow, or into internal functions that
8853 return both result of arithmetics and overflowed boolean flag in
8854 a complex integer result, or some other check for overflow.
8855 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
8856 checking part of that. */
8858 static tree
8859 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
8860 tree arg0, tree arg1, tree arg2)
8862 enum internal_fn ifn = IFN_LAST;
8863 /* The code of the expression corresponding to the type-generic
8864 built-in, or ERROR_MARK for the type-specific ones. */
8865 enum tree_code opcode = ERROR_MARK;
8866 bool ovf_only = false;
8868 switch (fcode)
8870 case BUILT_IN_ADD_OVERFLOW_P:
8871 ovf_only = true;
8872 /* FALLTHRU */
8873 case BUILT_IN_ADD_OVERFLOW:
8874 opcode = PLUS_EXPR;
8875 /* FALLTHRU */
8876 case BUILT_IN_SADD_OVERFLOW:
8877 case BUILT_IN_SADDL_OVERFLOW:
8878 case BUILT_IN_SADDLL_OVERFLOW:
8879 case BUILT_IN_UADD_OVERFLOW:
8880 case BUILT_IN_UADDL_OVERFLOW:
8881 case BUILT_IN_UADDLL_OVERFLOW:
8882 ifn = IFN_ADD_OVERFLOW;
8883 break;
8884 case BUILT_IN_SUB_OVERFLOW_P:
8885 ovf_only = true;
8886 /* FALLTHRU */
8887 case BUILT_IN_SUB_OVERFLOW:
8888 opcode = MINUS_EXPR;
8889 /* FALLTHRU */
8890 case BUILT_IN_SSUB_OVERFLOW:
8891 case BUILT_IN_SSUBL_OVERFLOW:
8892 case BUILT_IN_SSUBLL_OVERFLOW:
8893 case BUILT_IN_USUB_OVERFLOW:
8894 case BUILT_IN_USUBL_OVERFLOW:
8895 case BUILT_IN_USUBLL_OVERFLOW:
8896 ifn = IFN_SUB_OVERFLOW;
8897 break;
8898 case BUILT_IN_MUL_OVERFLOW_P:
8899 ovf_only = true;
8900 /* FALLTHRU */
8901 case BUILT_IN_MUL_OVERFLOW:
8902 opcode = MULT_EXPR;
8903 /* FALLTHRU */
8904 case BUILT_IN_SMUL_OVERFLOW:
8905 case BUILT_IN_SMULL_OVERFLOW:
8906 case BUILT_IN_SMULLL_OVERFLOW:
8907 case BUILT_IN_UMUL_OVERFLOW:
8908 case BUILT_IN_UMULL_OVERFLOW:
8909 case BUILT_IN_UMULLL_OVERFLOW:
8910 ifn = IFN_MUL_OVERFLOW;
8911 break;
8912 default:
8913 gcc_unreachable ();
8916 /* For the "generic" overloads, the first two arguments can have different
8917 types and the last argument determines the target type to use to check
8918 for overflow. The arguments of the other overloads all have the same
8919 type. */
8920 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
8922 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
8923 arguments are constant, attempt to fold the built-in call into a constant
8924 expression indicating whether or not it detected an overflow. */
8925 if (ovf_only
8926 && TREE_CODE (arg0) == INTEGER_CST
8927 && TREE_CODE (arg1) == INTEGER_CST)
8928 /* Perform the computation in the target type and check for overflow. */
8929 return omit_one_operand_loc (loc, boolean_type_node,
8930 arith_overflowed_p (opcode, type, arg0, arg1)
8931 ? boolean_true_node : boolean_false_node,
8932 arg2);
8934 tree ctype = build_complex_type (type);
8935 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
8936 2, arg0, arg1);
8937 tree tgt = save_expr (call);
8938 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
8939 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
8940 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
8942 if (ovf_only)
8943 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
8945 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
8946 tree store
8947 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
8948 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
8951 /* Fold a call to __builtin_FILE to a constant string. */
8953 static inline tree
8954 fold_builtin_FILE (location_t loc)
8956 if (const char *fname = LOCATION_FILE (loc))
8958 /* The documentation says this builtin is equivalent to the preprocessor
8959 __FILE__ macro so it appears appropriate to use the same file prefix
8960 mappings. */
8961 fname = remap_macro_filename (fname);
8962 return build_string_literal (strlen (fname) + 1, fname);
8965 return build_string_literal (1, "");
8968 /* Fold a call to __builtin_FUNCTION to a constant string. */
8970 static inline tree
8971 fold_builtin_FUNCTION ()
8973 const char *name = "";
8975 if (current_function_decl)
8976 name = lang_hooks.decl_printable_name (current_function_decl, 0);
8978 return build_string_literal (strlen (name) + 1, name);
8981 /* Fold a call to __builtin_LINE to an integer constant. */
8983 static inline tree
8984 fold_builtin_LINE (location_t loc, tree type)
8986 return build_int_cst (type, LOCATION_LINE (loc));
8989 /* Fold a call to built-in function FNDECL with 0 arguments.
8990 This function returns NULL_TREE if no simplification was possible. */
8992 static tree
8993 fold_builtin_0 (location_t loc, tree fndecl)
8995 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8996 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8997 switch (fcode)
8999 case BUILT_IN_FILE:
9000 return fold_builtin_FILE (loc);
9002 case BUILT_IN_FUNCTION:
9003 return fold_builtin_FUNCTION ();
9005 case BUILT_IN_LINE:
9006 return fold_builtin_LINE (loc, type);
9008 CASE_FLT_FN (BUILT_IN_INF):
9009 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9010 case BUILT_IN_INFD32:
9011 case BUILT_IN_INFD64:
9012 case BUILT_IN_INFD128:
9013 return fold_builtin_inf (loc, type, true);
9015 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9016 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9017 return fold_builtin_inf (loc, type, false);
9019 case BUILT_IN_CLASSIFY_TYPE:
9020 return fold_builtin_classify_type (NULL_TREE);
9022 default:
9023 break;
9025 return NULL_TREE;
9028 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9029 This function returns NULL_TREE if no simplification was possible. */
9031 static tree
9032 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
9034 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9035 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9037 if (TREE_CODE (arg0) == ERROR_MARK)
9038 return NULL_TREE;
9040 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9041 return ret;
9043 switch (fcode)
9045 case BUILT_IN_CONSTANT_P:
9047 tree val = fold_builtin_constant_p (arg0);
9049 /* Gimplification will pull the CALL_EXPR for the builtin out of
9050 an if condition. When not optimizing, we'll not CSE it back.
9051 To avoid link error types of regressions, return false now. */
9052 if (!val && !optimize)
9053 val = integer_zero_node;
9055 return val;
9058 case BUILT_IN_CLASSIFY_TYPE:
9059 return fold_builtin_classify_type (arg0);
9061 case BUILT_IN_STRLEN:
9062 return fold_builtin_strlen (loc, type, arg0);
9064 CASE_FLT_FN (BUILT_IN_FABS):
9065 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9066 case BUILT_IN_FABSD32:
9067 case BUILT_IN_FABSD64:
9068 case BUILT_IN_FABSD128:
9069 return fold_builtin_fabs (loc, arg0, type);
9071 case BUILT_IN_ABS:
9072 case BUILT_IN_LABS:
9073 case BUILT_IN_LLABS:
9074 case BUILT_IN_IMAXABS:
9075 return fold_builtin_abs (loc, arg0, type);
9077 CASE_FLT_FN (BUILT_IN_CONJ):
9078 if (validate_arg (arg0, COMPLEX_TYPE)
9079 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9080 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9081 break;
9083 CASE_FLT_FN (BUILT_IN_CREAL):
9084 if (validate_arg (arg0, COMPLEX_TYPE)
9085 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9086 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9087 break;
9089 CASE_FLT_FN (BUILT_IN_CIMAG):
9090 if (validate_arg (arg0, COMPLEX_TYPE)
9091 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9092 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9093 break;
9095 CASE_FLT_FN (BUILT_IN_CARG):
9096 return fold_builtin_carg (loc, arg0, type);
9098 case BUILT_IN_ISASCII:
9099 return fold_builtin_isascii (loc, arg0);
9101 case BUILT_IN_TOASCII:
9102 return fold_builtin_toascii (loc, arg0);
9104 case BUILT_IN_ISDIGIT:
9105 return fold_builtin_isdigit (loc, arg0);
9107 CASE_FLT_FN (BUILT_IN_FINITE):
9108 case BUILT_IN_FINITED32:
9109 case BUILT_IN_FINITED64:
9110 case BUILT_IN_FINITED128:
9111 case BUILT_IN_ISFINITE:
9113 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9114 if (ret)
9115 return ret;
9116 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9119 CASE_FLT_FN (BUILT_IN_ISINF):
9120 case BUILT_IN_ISINFD32:
9121 case BUILT_IN_ISINFD64:
9122 case BUILT_IN_ISINFD128:
9124 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9125 if (ret)
9126 return ret;
9127 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9130 case BUILT_IN_ISNORMAL:
9131 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9133 case BUILT_IN_ISINF_SIGN:
9134 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9136 CASE_FLT_FN (BUILT_IN_ISNAN):
9137 case BUILT_IN_ISNAND32:
9138 case BUILT_IN_ISNAND64:
9139 case BUILT_IN_ISNAND128:
9140 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9142 case BUILT_IN_FREE:
9143 if (integer_zerop (arg0))
9144 return build_empty_stmt (loc);
9145 break;
9147 default:
9148 break;
9151 return NULL_TREE;
9155 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
9156 This function returns NULL_TREE if no simplification was possible. */
9158 static tree
9159 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
9161 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9162 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9164 if (TREE_CODE (arg0) == ERROR_MARK
9165 || TREE_CODE (arg1) == ERROR_MARK)
9166 return NULL_TREE;
9168 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9169 return ret;
9171 switch (fcode)
9173 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9174 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9175 if (validate_arg (arg0, REAL_TYPE)
9176 && validate_arg (arg1, POINTER_TYPE))
9177 return do_mpfr_lgamma_r (arg0, arg1, type);
9178 break;
9180 CASE_FLT_FN (BUILT_IN_FREXP):
9181 return fold_builtin_frexp (loc, arg0, arg1, type);
9183 CASE_FLT_FN (BUILT_IN_MODF):
9184 return fold_builtin_modf (loc, arg0, arg1, type);
9186 case BUILT_IN_STRSPN:
9187 return fold_builtin_strspn (loc, arg0, arg1);
9189 case BUILT_IN_STRCSPN:
9190 return fold_builtin_strcspn (loc, arg0, arg1);
9192 case BUILT_IN_STRPBRK:
9193 return fold_builtin_strpbrk (loc, arg0, arg1, type);
9195 case BUILT_IN_EXPECT:
9196 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
9198 case BUILT_IN_ISGREATER:
9199 return fold_builtin_unordered_cmp (loc, fndecl,
9200 arg0, arg1, UNLE_EXPR, LE_EXPR);
9201 case BUILT_IN_ISGREATEREQUAL:
9202 return fold_builtin_unordered_cmp (loc, fndecl,
9203 arg0, arg1, UNLT_EXPR, LT_EXPR);
9204 case BUILT_IN_ISLESS:
9205 return fold_builtin_unordered_cmp (loc, fndecl,
9206 arg0, arg1, UNGE_EXPR, GE_EXPR);
9207 case BUILT_IN_ISLESSEQUAL:
9208 return fold_builtin_unordered_cmp (loc, fndecl,
9209 arg0, arg1, UNGT_EXPR, GT_EXPR);
9210 case BUILT_IN_ISLESSGREATER:
9211 return fold_builtin_unordered_cmp (loc, fndecl,
9212 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9213 case BUILT_IN_ISUNORDERED:
9214 return fold_builtin_unordered_cmp (loc, fndecl,
9215 arg0, arg1, UNORDERED_EXPR,
9216 NOP_EXPR);
9218 /* We do the folding for va_start in the expander. */
9219 case BUILT_IN_VA_START:
9220 break;
9222 case BUILT_IN_OBJECT_SIZE:
9223 return fold_builtin_object_size (arg0, arg1);
9225 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9226 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9228 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9229 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9231 default:
9232 break;
9234 return NULL_TREE;
9237 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9238 and ARG2.
9239 This function returns NULL_TREE if no simplification was possible. */
9241 static tree
9242 fold_builtin_3 (location_t loc, tree fndecl,
9243 tree arg0, tree arg1, tree arg2)
9245 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9246 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9248 if (TREE_CODE (arg0) == ERROR_MARK
9249 || TREE_CODE (arg1) == ERROR_MARK
9250 || TREE_CODE (arg2) == ERROR_MARK)
9251 return NULL_TREE;
9253 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9254 arg0, arg1, arg2))
9255 return ret;
9257 switch (fcode)
9260 CASE_FLT_FN (BUILT_IN_SINCOS):
9261 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9263 CASE_FLT_FN (BUILT_IN_FMA):
9264 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
9265 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
9267 CASE_FLT_FN (BUILT_IN_REMQUO):
9268 if (validate_arg (arg0, REAL_TYPE)
9269 && validate_arg (arg1, REAL_TYPE)
9270 && validate_arg (arg2, POINTER_TYPE))
9271 return do_mpfr_remquo (arg0, arg1, arg2);
9272 break;
9274 case BUILT_IN_MEMCMP:
9275 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9277 case BUILT_IN_EXPECT:
9278 return fold_builtin_expect (loc, arg0, arg1, arg2);
9280 case BUILT_IN_ADD_OVERFLOW:
9281 case BUILT_IN_SUB_OVERFLOW:
9282 case BUILT_IN_MUL_OVERFLOW:
9283 case BUILT_IN_ADD_OVERFLOW_P:
9284 case BUILT_IN_SUB_OVERFLOW_P:
9285 case BUILT_IN_MUL_OVERFLOW_P:
9286 case BUILT_IN_SADD_OVERFLOW:
9287 case BUILT_IN_SADDL_OVERFLOW:
9288 case BUILT_IN_SADDLL_OVERFLOW:
9289 case BUILT_IN_SSUB_OVERFLOW:
9290 case BUILT_IN_SSUBL_OVERFLOW:
9291 case BUILT_IN_SSUBLL_OVERFLOW:
9292 case BUILT_IN_SMUL_OVERFLOW:
9293 case BUILT_IN_SMULL_OVERFLOW:
9294 case BUILT_IN_SMULLL_OVERFLOW:
9295 case BUILT_IN_UADD_OVERFLOW:
9296 case BUILT_IN_UADDL_OVERFLOW:
9297 case BUILT_IN_UADDLL_OVERFLOW:
9298 case BUILT_IN_USUB_OVERFLOW:
9299 case BUILT_IN_USUBL_OVERFLOW:
9300 case BUILT_IN_USUBLL_OVERFLOW:
9301 case BUILT_IN_UMUL_OVERFLOW:
9302 case BUILT_IN_UMULL_OVERFLOW:
9303 case BUILT_IN_UMULLL_OVERFLOW:
9304 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9306 default:
9307 break;
9309 return NULL_TREE;
9312 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
9313 arguments. IGNORE is true if the result of the
9314 function call is ignored. This function returns NULL_TREE if no
9315 simplification was possible. */
9317 tree
9318 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
9320 tree ret = NULL_TREE;
9322 switch (nargs)
9324 case 0:
9325 ret = fold_builtin_0 (loc, fndecl);
9326 break;
9327 case 1:
9328 ret = fold_builtin_1 (loc, fndecl, args[0]);
9329 break;
9330 case 2:
9331 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
9332 break;
9333 case 3:
9334 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9335 break;
9336 default:
9337 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9338 break;
9340 if (ret)
9342 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9343 SET_EXPR_LOCATION (ret, loc);
9344 TREE_NO_WARNING (ret) = 1;
9345 return ret;
9347 return NULL_TREE;
9350 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9351 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9352 of arguments in ARGS to be omitted. OLDNARGS is the number of
9353 elements in ARGS. */
9355 static tree
9356 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9357 int skip, tree fndecl, int n, va_list newargs)
9359 int nargs = oldnargs - skip + n;
9360 tree *buffer;
9362 if (n > 0)
9364 int i, j;
9366 buffer = XALLOCAVEC (tree, nargs);
9367 for (i = 0; i < n; i++)
9368 buffer[i] = va_arg (newargs, tree);
9369 for (j = skip; j < oldnargs; j++, i++)
9370 buffer[i] = args[j];
9372 else
9373 buffer = args + skip;
9375 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9378 /* Return true if FNDECL shouldn't be folded right now.
9379 If a built-in function has an inline attribute always_inline
9380 wrapper, defer folding it after always_inline functions have
9381 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9382 might not be performed. */
9384 bool
9385 avoid_folding_inline_builtin (tree fndecl)
9387 return (DECL_DECLARED_INLINE_P (fndecl)
9388 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9389 && cfun
9390 && !cfun->always_inline_functions_inlined
9391 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9394 /* A wrapper function for builtin folding that prevents warnings for
9395 "statement without effect" and the like, caused by removing the
9396 call node earlier than the warning is generated. */
9398 tree
9399 fold_call_expr (location_t loc, tree exp, bool ignore)
9401 tree ret = NULL_TREE;
9402 tree fndecl = get_callee_fndecl (exp);
9403 if (fndecl
9404 && TREE_CODE (fndecl) == FUNCTION_DECL
9405 && DECL_BUILT_IN (fndecl)
9406 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9407 yet. Defer folding until we see all the arguments
9408 (after inlining). */
9409 && !CALL_EXPR_VA_ARG_PACK (exp))
9411 int nargs = call_expr_nargs (exp);
9413 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9414 instead last argument is __builtin_va_arg_pack (). Defer folding
9415 even in that case, until arguments are finalized. */
9416 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9418 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9419 if (fndecl2
9420 && TREE_CODE (fndecl2) == FUNCTION_DECL
9421 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9422 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9423 return NULL_TREE;
9426 if (avoid_folding_inline_builtin (fndecl))
9427 return NULL_TREE;
9429 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9430 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9431 CALL_EXPR_ARGP (exp), ignore);
9432 else
9434 tree *args = CALL_EXPR_ARGP (exp);
9435 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9436 if (ret)
9437 return ret;
9440 return NULL_TREE;
9443 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9444 N arguments are passed in the array ARGARRAY. Return a folded
9445 expression or NULL_TREE if no simplification was possible. */
9447 tree
9448 fold_builtin_call_array (location_t loc, tree,
9449 tree fn,
9450 int n,
9451 tree *argarray)
9453 if (TREE_CODE (fn) != ADDR_EXPR)
9454 return NULL_TREE;
9456 tree fndecl = TREE_OPERAND (fn, 0);
9457 if (TREE_CODE (fndecl) == FUNCTION_DECL
9458 && DECL_BUILT_IN (fndecl))
9460 /* If last argument is __builtin_va_arg_pack (), arguments to this
9461 function are not finalized yet. Defer folding until they are. */
9462 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9464 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9465 if (fndecl2
9466 && TREE_CODE (fndecl2) == FUNCTION_DECL
9467 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9468 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9469 return NULL_TREE;
9471 if (avoid_folding_inline_builtin (fndecl))
9472 return NULL_TREE;
9473 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9474 return targetm.fold_builtin (fndecl, n, argarray, false);
9475 else
9476 return fold_builtin_n (loc, fndecl, argarray, n, false);
9479 return NULL_TREE;
9482 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9483 along with N new arguments specified as the "..." parameters. SKIP
9484 is the number of arguments in EXP to be omitted. This function is used
9485 to do varargs-to-varargs transformations. */
9487 static tree
9488 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9490 va_list ap;
9491 tree t;
9493 va_start (ap, n);
9494 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9495 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9496 va_end (ap);
9498 return t;
9501 /* Validate a single argument ARG against a tree code CODE representing
9502 a type. Return true when argument is valid. */
9504 static bool
9505 validate_arg (const_tree arg, enum tree_code code)
9507 if (!arg)
9508 return false;
9509 else if (code == POINTER_TYPE)
9510 return POINTER_TYPE_P (TREE_TYPE (arg));
9511 else if (code == INTEGER_TYPE)
9512 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9513 return code == TREE_CODE (TREE_TYPE (arg));
9516 /* This function validates the types of a function call argument list
9517 against a specified list of tree_codes. If the last specifier is a 0,
9518 that represents an ellipses, otherwise the last specifier must be a
9519 VOID_TYPE.
9521 This is the GIMPLE version of validate_arglist. Eventually we want to
9522 completely convert builtins.c to work from GIMPLEs and the tree based
9523 validate_arglist will then be removed. */
9525 bool
9526 validate_gimple_arglist (const gcall *call, ...)
9528 enum tree_code code;
9529 bool res = 0;
9530 va_list ap;
9531 const_tree arg;
9532 size_t i;
9534 va_start (ap, call);
9535 i = 0;
9539 code = (enum tree_code) va_arg (ap, int);
9540 switch (code)
9542 case 0:
9543 /* This signifies an ellipses, any further arguments are all ok. */
9544 res = true;
9545 goto end;
9546 case VOID_TYPE:
9547 /* This signifies an endlink, if no arguments remain, return
9548 true, otherwise return false. */
9549 res = (i == gimple_call_num_args (call));
9550 goto end;
9551 default:
9552 /* If no parameters remain or the parameter's code does not
9553 match the specified code, return false. Otherwise continue
9554 checking any remaining arguments. */
9555 arg = gimple_call_arg (call, i++);
9556 if (!validate_arg (arg, code))
9557 goto end;
9558 break;
9561 while (1);
9563 /* We need gotos here since we can only have one VA_CLOSE in a
9564 function. */
9565 end: ;
9566 va_end (ap);
9568 return res;
9571 /* Default target-specific builtin expander that does nothing. */
9574 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9575 rtx target ATTRIBUTE_UNUSED,
9576 rtx subtarget ATTRIBUTE_UNUSED,
9577 machine_mode mode ATTRIBUTE_UNUSED,
9578 int ignore ATTRIBUTE_UNUSED)
9580 return NULL_RTX;
9583 /* Returns true is EXP represents data that would potentially reside
9584 in a readonly section. */
9586 bool
9587 readonly_data_expr (tree exp)
9589 STRIP_NOPS (exp);
9591 if (TREE_CODE (exp) != ADDR_EXPR)
9592 return false;
9594 exp = get_base_address (TREE_OPERAND (exp, 0));
9595 if (!exp)
9596 return false;
9598 /* Make sure we call decl_readonly_section only for trees it
9599 can handle (since it returns true for everything it doesn't
9600 understand). */
9601 if (TREE_CODE (exp) == STRING_CST
9602 || TREE_CODE (exp) == CONSTRUCTOR
9603 || (VAR_P (exp) && TREE_STATIC (exp)))
9604 return decl_readonly_section (exp, 0);
9605 else
9606 return false;
9609 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9610 to the call, and TYPE is its return type.
9612 Return NULL_TREE if no simplification was possible, otherwise return the
9613 simplified form of the call as a tree.
9615 The simplified form may be a constant or other expression which
9616 computes the same value, but in a more efficient manner (including
9617 calls to other builtin functions).
9619 The call may contain arguments which need to be evaluated, but
9620 which are not useful to determine the result of the call. In
9621 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9622 COMPOUND_EXPR will be an argument which must be evaluated.
9623 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9624 COMPOUND_EXPR in the chain will contain the tree for the simplified
9625 form of the builtin function call. */
9627 static tree
9628 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
9630 if (!validate_arg (s1, POINTER_TYPE)
9631 || !validate_arg (s2, POINTER_TYPE))
9632 return NULL_TREE;
9633 else
9635 tree fn;
9636 const char *p1, *p2;
9638 p2 = c_getstr (s2);
9639 if (p2 == NULL)
9640 return NULL_TREE;
9642 p1 = c_getstr (s1);
9643 if (p1 != NULL)
9645 const char *r = strpbrk (p1, p2);
9646 tree tem;
9648 if (r == NULL)
9649 return build_int_cst (TREE_TYPE (s1), 0);
9651 /* Return an offset into the constant string argument. */
9652 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9653 return fold_convert_loc (loc, type, tem);
9656 if (p2[0] == '\0')
9657 /* strpbrk(x, "") == NULL.
9658 Evaluate and ignore s1 in case it had side-effects. */
9659 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
9661 if (p2[1] != '\0')
9662 return NULL_TREE; /* Really call strpbrk. */
9664 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9665 if (!fn)
9666 return NULL_TREE;
9668 /* New argument list transforming strpbrk(s1, s2) to
9669 strchr(s1, s2[0]). */
9670 return build_call_expr_loc (loc, fn, 2, s1,
9671 build_int_cst (integer_type_node, p2[0]));
9675 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9676 to the call.
9678 Return NULL_TREE if no simplification was possible, otherwise return the
9679 simplified form of the call as a tree.
9681 The simplified form may be a constant or other expression which
9682 computes the same value, but in a more efficient manner (including
9683 calls to other builtin functions).
9685 The call may contain arguments which need to be evaluated, but
9686 which are not useful to determine the result of the call. In
9687 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9688 COMPOUND_EXPR will be an argument which must be evaluated.
9689 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9690 COMPOUND_EXPR in the chain will contain the tree for the simplified
9691 form of the builtin function call. */
9693 static tree
9694 fold_builtin_strspn (location_t loc, tree s1, tree s2)
9696 if (!validate_arg (s1, POINTER_TYPE)
9697 || !validate_arg (s2, POINTER_TYPE))
9698 return NULL_TREE;
9699 else
9701 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9703 /* If either argument is "", return NULL_TREE. */
9704 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9705 /* Evaluate and ignore both arguments in case either one has
9706 side-effects. */
9707 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9708 s1, s2);
9709 return NULL_TREE;
9713 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9714 to the call.
9716 Return NULL_TREE if no simplification was possible, otherwise return the
9717 simplified form of the call as a tree.
9719 The simplified form may be a constant or other expression which
9720 computes the same value, but in a more efficient manner (including
9721 calls to other builtin functions).
9723 The call may contain arguments which need to be evaluated, but
9724 which are not useful to determine the result of the call. In
9725 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9726 COMPOUND_EXPR will be an argument which must be evaluated.
9727 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9728 COMPOUND_EXPR in the chain will contain the tree for the simplified
9729 form of the builtin function call. */
9731 static tree
9732 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
9734 if (!validate_arg (s1, POINTER_TYPE)
9735 || !validate_arg (s2, POINTER_TYPE))
9736 return NULL_TREE;
9737 else
9739 /* If the first argument is "", return NULL_TREE. */
9740 const char *p1 = c_getstr (s1);
9741 if (p1 && *p1 == '\0')
9743 /* Evaluate and ignore argument s2 in case it has
9744 side-effects. */
9745 return omit_one_operand_loc (loc, size_type_node,
9746 size_zero_node, s2);
9749 /* If the second argument is "", return __builtin_strlen(s1). */
9750 const char *p2 = c_getstr (s2);
9751 if (p2 && *p2 == '\0')
9753 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9755 /* If the replacement _DECL isn't initialized, don't do the
9756 transformation. */
9757 if (!fn)
9758 return NULL_TREE;
9760 return build_call_expr_loc (loc, fn, 1, s1);
9762 return NULL_TREE;
9766 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9767 produced. False otherwise. This is done so that we don't output the error
9768 or warning twice or three times. */
9770 bool
9771 fold_builtin_next_arg (tree exp, bool va_start_p)
9773 tree fntype = TREE_TYPE (current_function_decl);
9774 int nargs = call_expr_nargs (exp);
9775 tree arg;
9776 /* There is good chance the current input_location points inside the
9777 definition of the va_start macro (perhaps on the token for
9778 builtin) in a system header, so warnings will not be emitted.
9779 Use the location in real source code. */
9780 source_location current_location =
9781 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9782 NULL);
9784 if (!stdarg_p (fntype))
9786 error ("%<va_start%> used in function with fixed args");
9787 return true;
9790 if (va_start_p)
9792 if (va_start_p && (nargs != 2))
9794 error ("wrong number of arguments to function %<va_start%>");
9795 return true;
9797 arg = CALL_EXPR_ARG (exp, 1);
9799 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9800 when we checked the arguments and if needed issued a warning. */
9801 else
9803 if (nargs == 0)
9805 /* Evidently an out of date version of <stdarg.h>; can't validate
9806 va_start's second argument, but can still work as intended. */
9807 warning_at (current_location,
9808 OPT_Wvarargs,
9809 "%<__builtin_next_arg%> called without an argument");
9810 return true;
9812 else if (nargs > 1)
9814 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9815 return true;
9817 arg = CALL_EXPR_ARG (exp, 0);
9820 if (TREE_CODE (arg) == SSA_NAME)
9821 arg = SSA_NAME_VAR (arg);
9823 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9824 or __builtin_next_arg (0) the first time we see it, after checking
9825 the arguments and if needed issuing a warning. */
9826 if (!integer_zerop (arg))
9828 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9830 /* Strip off all nops for the sake of the comparison. This
9831 is not quite the same as STRIP_NOPS. It does more.
9832 We must also strip off INDIRECT_EXPR for C++ reference
9833 parameters. */
9834 while (CONVERT_EXPR_P (arg)
9835 || TREE_CODE (arg) == INDIRECT_REF)
9836 arg = TREE_OPERAND (arg, 0);
9837 if (arg != last_parm)
9839 /* FIXME: Sometimes with the tree optimizers we can get the
9840 not the last argument even though the user used the last
9841 argument. We just warn and set the arg to be the last
9842 argument so that we will get wrong-code because of
9843 it. */
9844 warning_at (current_location,
9845 OPT_Wvarargs,
9846 "second parameter of %<va_start%> not last named argument");
9849 /* Undefined by C99 7.15.1.4p4 (va_start):
9850 "If the parameter parmN is declared with the register storage
9851 class, with a function or array type, or with a type that is
9852 not compatible with the type that results after application of
9853 the default argument promotions, the behavior is undefined."
9855 else if (DECL_REGISTER (arg))
9857 warning_at (current_location,
9858 OPT_Wvarargs,
9859 "undefined behavior when second parameter of "
9860 "%<va_start%> is declared with %<register%> storage");
9863 /* We want to verify the second parameter just once before the tree
9864 optimizers are run and then avoid keeping it in the tree,
9865 as otherwise we could warn even for correct code like:
9866 void foo (int i, ...)
9867 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9868 if (va_start_p)
9869 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
9870 else
9871 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
9873 return false;
9877 /* Expand a call EXP to __builtin_object_size. */
9879 static rtx
9880 expand_builtin_object_size (tree exp)
9882 tree ost;
9883 int object_size_type;
9884 tree fndecl = get_callee_fndecl (exp);
9886 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9888 error ("%Kfirst argument of %qD must be a pointer, second integer constant",
9889 exp, fndecl);
9890 expand_builtin_trap ();
9891 return const0_rtx;
9894 ost = CALL_EXPR_ARG (exp, 1);
9895 STRIP_NOPS (ost);
9897 if (TREE_CODE (ost) != INTEGER_CST
9898 || tree_int_cst_sgn (ost) < 0
9899 || compare_tree_int (ost, 3) > 0)
9901 error ("%Klast argument of %qD is not integer constant between 0 and 3",
9902 exp, fndecl);
9903 expand_builtin_trap ();
9904 return const0_rtx;
9907 object_size_type = tree_to_shwi (ost);
9909 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9912 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9913 FCODE is the BUILT_IN_* to use.
9914 Return NULL_RTX if we failed; the caller should emit a normal call,
9915 otherwise try to get the result in TARGET, if convenient (and in
9916 mode MODE if that's convenient). */
9918 static rtx
9919 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
9920 enum built_in_function fcode)
9922 if (!validate_arglist (exp,
9923 POINTER_TYPE,
9924 fcode == BUILT_IN_MEMSET_CHK
9925 ? INTEGER_TYPE : POINTER_TYPE,
9926 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
9927 return NULL_RTX;
9929 tree dest = CALL_EXPR_ARG (exp, 0);
9930 tree src = CALL_EXPR_ARG (exp, 1);
9931 tree len = CALL_EXPR_ARG (exp, 2);
9932 tree size = CALL_EXPR_ARG (exp, 3);
9934 bool sizes_ok = check_access (exp, dest, src, len, /*maxread=*/NULL_TREE,
9935 /*str=*/NULL_TREE, size);
9937 if (!tree_fits_uhwi_p (size))
9938 return NULL_RTX;
9940 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
9942 /* Avoid transforming the checking call to an ordinary one when
9943 an overflow has been detected or when the call couldn't be
9944 validated because the size is not constant. */
9945 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
9946 return NULL_RTX;
9948 tree fn = NULL_TREE;
9949 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9950 mem{cpy,pcpy,move,set} is available. */
9951 switch (fcode)
9953 case BUILT_IN_MEMCPY_CHK:
9954 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
9955 break;
9956 case BUILT_IN_MEMPCPY_CHK:
9957 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
9958 break;
9959 case BUILT_IN_MEMMOVE_CHK:
9960 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9961 break;
9962 case BUILT_IN_MEMSET_CHK:
9963 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9964 break;
9965 default:
9966 break;
9969 if (! fn)
9970 return NULL_RTX;
9972 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9973 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9974 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9975 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9977 else if (fcode == BUILT_IN_MEMSET_CHK)
9978 return NULL_RTX;
9979 else
9981 unsigned int dest_align = get_pointer_alignment (dest);
9983 /* If DEST is not a pointer type, call the normal function. */
9984 if (dest_align == 0)
9985 return NULL_RTX;
9987 /* If SRC and DEST are the same (and not volatile), do nothing. */
9988 if (operand_equal_p (src, dest, 0))
9990 tree expr;
9992 if (fcode != BUILT_IN_MEMPCPY_CHK)
9994 /* Evaluate and ignore LEN in case it has side-effects. */
9995 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9996 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9999 expr = fold_build_pointer_plus (dest, len);
10000 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10003 /* __memmove_chk special case. */
10004 if (fcode == BUILT_IN_MEMMOVE_CHK)
10006 unsigned int src_align = get_pointer_alignment (src);
10008 if (src_align == 0)
10009 return NULL_RTX;
10011 /* If src is categorized for a readonly section we can use
10012 normal __memcpy_chk. */
10013 if (readonly_data_expr (src))
10015 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10016 if (!fn)
10017 return NULL_RTX;
10018 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10019 dest, src, len, size);
10020 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10021 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10022 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10025 return NULL_RTX;
10029 /* Emit warning if a buffer overflow is detected at compile time. */
10031 static void
10032 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10034 /* The source string. */
10035 tree srcstr = NULL_TREE;
10036 /* The size of the destination object. */
10037 tree objsize = NULL_TREE;
10038 /* The string that is being concatenated with (as in __strcat_chk)
10039 or null if it isn't. */
10040 tree catstr = NULL_TREE;
10041 /* The maximum length of the source sequence in a bounded operation
10042 (such as __strncat_chk) or null if the operation isn't bounded
10043 (such as __strcat_chk). */
10044 tree maxread = NULL_TREE;
10045 /* The exact size of the access (such as in __strncpy_chk). */
10046 tree size = NULL_TREE;
10048 switch (fcode)
10050 case BUILT_IN_STRCPY_CHK:
10051 case BUILT_IN_STPCPY_CHK:
10052 srcstr = CALL_EXPR_ARG (exp, 1);
10053 objsize = CALL_EXPR_ARG (exp, 2);
10054 break;
10056 case BUILT_IN_STRCAT_CHK:
10057 /* For __strcat_chk the warning will be emitted only if overflowing
10058 by at least strlen (dest) + 1 bytes. */
10059 catstr = CALL_EXPR_ARG (exp, 0);
10060 srcstr = CALL_EXPR_ARG (exp, 1);
10061 objsize = CALL_EXPR_ARG (exp, 2);
10062 break;
10064 case BUILT_IN_STRNCAT_CHK:
10065 catstr = CALL_EXPR_ARG (exp, 0);
10066 srcstr = CALL_EXPR_ARG (exp, 1);
10067 maxread = CALL_EXPR_ARG (exp, 2);
10068 objsize = CALL_EXPR_ARG (exp, 3);
10069 break;
10071 case BUILT_IN_STRNCPY_CHK:
10072 case BUILT_IN_STPNCPY_CHK:
10073 srcstr = CALL_EXPR_ARG (exp, 1);
10074 size = CALL_EXPR_ARG (exp, 2);
10075 objsize = CALL_EXPR_ARG (exp, 3);
10076 break;
10078 case BUILT_IN_SNPRINTF_CHK:
10079 case BUILT_IN_VSNPRINTF_CHK:
10080 maxread = CALL_EXPR_ARG (exp, 1);
10081 objsize = CALL_EXPR_ARG (exp, 3);
10082 break;
10083 default:
10084 gcc_unreachable ();
10087 if (catstr && maxread)
10089 /* Check __strncat_chk. There is no way to determine the length
10090 of the string to which the source string is being appended so
10091 just warn when the length of the source string is not known. */
10092 check_strncat_sizes (exp, objsize);
10093 return;
10096 /* The destination argument is the first one for all built-ins above. */
10097 tree dst = CALL_EXPR_ARG (exp, 0);
10099 check_access (exp, dst, srcstr, size, maxread, srcstr, objsize);
10102 /* Emit warning if a buffer overflow is detected at compile time
10103 in __sprintf_chk/__vsprintf_chk calls. */
10105 static void
10106 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10108 tree size, len, fmt;
10109 const char *fmt_str;
10110 int nargs = call_expr_nargs (exp);
10112 /* Verify the required arguments in the original call. */
10114 if (nargs < 4)
10115 return;
10116 size = CALL_EXPR_ARG (exp, 2);
10117 fmt = CALL_EXPR_ARG (exp, 3);
10119 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10120 return;
10122 /* Check whether the format is a literal string constant. */
10123 fmt_str = c_getstr (fmt);
10124 if (fmt_str == NULL)
10125 return;
10127 if (!init_target_chars ())
10128 return;
10130 /* If the format doesn't contain % args or %%, we know its size. */
10131 if (strchr (fmt_str, target_percent) == 0)
10132 len = build_int_cstu (size_type_node, strlen (fmt_str));
10133 /* If the format is "%s" and first ... argument is a string literal,
10134 we know it too. */
10135 else if (fcode == BUILT_IN_SPRINTF_CHK
10136 && strcmp (fmt_str, target_percent_s) == 0)
10138 tree arg;
10140 if (nargs < 5)
10141 return;
10142 arg = CALL_EXPR_ARG (exp, 4);
10143 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10144 return;
10146 len = c_strlen (arg, 1);
10147 if (!len || ! tree_fits_uhwi_p (len))
10148 return;
10150 else
10151 return;
10153 /* Add one for the terminating nul. */
10154 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10156 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, /*size=*/NULL_TREE,
10157 /*maxread=*/NULL_TREE, len, size);
10160 /* Emit warning if a free is called with address of a variable. */
10162 static void
10163 maybe_emit_free_warning (tree exp)
10165 tree arg = CALL_EXPR_ARG (exp, 0);
10167 STRIP_NOPS (arg);
10168 if (TREE_CODE (arg) != ADDR_EXPR)
10169 return;
10171 arg = get_base_address (TREE_OPERAND (arg, 0));
10172 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
10173 return;
10175 if (SSA_VAR_P (arg))
10176 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10177 "%Kattempt to free a non-heap object %qD", exp, arg);
10178 else
10179 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10180 "%Kattempt to free a non-heap object", exp);
10183 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10184 if possible. */
10186 static tree
10187 fold_builtin_object_size (tree ptr, tree ost)
10189 unsigned HOST_WIDE_INT bytes;
10190 int object_size_type;
10192 if (!validate_arg (ptr, POINTER_TYPE)
10193 || !validate_arg (ost, INTEGER_TYPE))
10194 return NULL_TREE;
10196 STRIP_NOPS (ost);
10198 if (TREE_CODE (ost) != INTEGER_CST
10199 || tree_int_cst_sgn (ost) < 0
10200 || compare_tree_int (ost, 3) > 0)
10201 return NULL_TREE;
10203 object_size_type = tree_to_shwi (ost);
10205 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10206 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10207 and (size_t) 0 for types 2 and 3. */
10208 if (TREE_SIDE_EFFECTS (ptr))
10209 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10211 if (TREE_CODE (ptr) == ADDR_EXPR)
10213 compute_builtin_object_size (ptr, object_size_type, &bytes);
10214 if (wi::fits_to_tree_p (bytes, size_type_node))
10215 return build_int_cstu (size_type_node, bytes);
10217 else if (TREE_CODE (ptr) == SSA_NAME)
10219 /* If object size is not known yet, delay folding until
10220 later. Maybe subsequent passes will help determining
10221 it. */
10222 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10223 && wi::fits_to_tree_p (bytes, size_type_node))
10224 return build_int_cstu (size_type_node, bytes);
10227 return NULL_TREE;
10230 /* Builtins with folding operations that operate on "..." arguments
10231 need special handling; we need to store the arguments in a convenient
10232 data structure before attempting any folding. Fortunately there are
10233 only a few builtins that fall into this category. FNDECL is the
10234 function, EXP is the CALL_EXPR for the call. */
10236 static tree
10237 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10239 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10240 tree ret = NULL_TREE;
10242 switch (fcode)
10244 case BUILT_IN_FPCLASSIFY:
10245 ret = fold_builtin_fpclassify (loc, args, nargs);
10246 break;
10248 default:
10249 break;
10251 if (ret)
10253 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10254 SET_EXPR_LOCATION (ret, loc);
10255 TREE_NO_WARNING (ret) = 1;
10256 return ret;
10258 return NULL_TREE;
10261 /* Initialize format string characters in the target charset. */
10263 bool
10264 init_target_chars (void)
10266 static bool init;
10267 if (!init)
10269 target_newline = lang_hooks.to_target_charset ('\n');
10270 target_percent = lang_hooks.to_target_charset ('%');
10271 target_c = lang_hooks.to_target_charset ('c');
10272 target_s = lang_hooks.to_target_charset ('s');
10273 if (target_newline == 0 || target_percent == 0 || target_c == 0
10274 || target_s == 0)
10275 return false;
10277 target_percent_c[0] = target_percent;
10278 target_percent_c[1] = target_c;
10279 target_percent_c[2] = '\0';
10281 target_percent_s[0] = target_percent;
10282 target_percent_s[1] = target_s;
10283 target_percent_s[2] = '\0';
10285 target_percent_s_newline[0] = target_percent;
10286 target_percent_s_newline[1] = target_s;
10287 target_percent_s_newline[2] = target_newline;
10288 target_percent_s_newline[3] = '\0';
10290 init = true;
10292 return true;
10295 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10296 and no overflow/underflow occurred. INEXACT is true if M was not
10297 exactly calculated. TYPE is the tree type for the result. This
10298 function assumes that you cleared the MPFR flags and then
10299 calculated M to see if anything subsequently set a flag prior to
10300 entering this function. Return NULL_TREE if any checks fail. */
10302 static tree
10303 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10305 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10306 overflow/underflow occurred. If -frounding-math, proceed iff the
10307 result of calling FUNC was exact. */
10308 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10309 && (!flag_rounding_math || !inexact))
10311 REAL_VALUE_TYPE rr;
10313 real_from_mpfr (&rr, m, type, GMP_RNDN);
10314 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10315 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10316 but the mpft_t is not, then we underflowed in the
10317 conversion. */
10318 if (real_isfinite (&rr)
10319 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10321 REAL_VALUE_TYPE rmode;
10323 real_convert (&rmode, TYPE_MODE (type), &rr);
10324 /* Proceed iff the specified mode can hold the value. */
10325 if (real_identical (&rmode, &rr))
10326 return build_real (type, rmode);
10329 return NULL_TREE;
10332 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10333 number and no overflow/underflow occurred. INEXACT is true if M
10334 was not exactly calculated. TYPE is the tree type for the result.
10335 This function assumes that you cleared the MPFR flags and then
10336 calculated M to see if anything subsequently set a flag prior to
10337 entering this function. Return NULL_TREE if any checks fail, if
10338 FORCE_CONVERT is true, then bypass the checks. */
10340 static tree
10341 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10343 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10344 overflow/underflow occurred. If -frounding-math, proceed iff the
10345 result of calling FUNC was exact. */
10346 if (force_convert
10347 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10348 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10349 && (!flag_rounding_math || !inexact)))
10351 REAL_VALUE_TYPE re, im;
10353 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
10354 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
10355 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10356 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10357 but the mpft_t is not, then we underflowed in the
10358 conversion. */
10359 if (force_convert
10360 || (real_isfinite (&re) && real_isfinite (&im)
10361 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10362 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10364 REAL_VALUE_TYPE re_mode, im_mode;
10366 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10367 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10368 /* Proceed iff the specified mode can hold the value. */
10369 if (force_convert
10370 || (real_identical (&re_mode, &re)
10371 && real_identical (&im_mode, &im)))
10372 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10373 build_real (TREE_TYPE (type), im_mode));
10376 return NULL_TREE;
10379 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10380 the pointer *(ARG_QUO) and return the result. The type is taken
10381 from the type of ARG0 and is used for setting the precision of the
10382 calculation and results. */
10384 static tree
10385 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10387 tree const type = TREE_TYPE (arg0);
10388 tree result = NULL_TREE;
10390 STRIP_NOPS (arg0);
10391 STRIP_NOPS (arg1);
10393 /* To proceed, MPFR must exactly represent the target floating point
10394 format, which only happens when the target base equals two. */
10395 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10396 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10397 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10399 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10400 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10402 if (real_isfinite (ra0) && real_isfinite (ra1))
10404 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10405 const int prec = fmt->p;
10406 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10407 tree result_rem;
10408 long integer_quo;
10409 mpfr_t m0, m1;
10411 mpfr_inits2 (prec, m0, m1, NULL);
10412 mpfr_from_real (m0, ra0, GMP_RNDN);
10413 mpfr_from_real (m1, ra1, GMP_RNDN);
10414 mpfr_clear_flags ();
10415 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10416 /* Remquo is independent of the rounding mode, so pass
10417 inexact=0 to do_mpfr_ckconv(). */
10418 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10419 mpfr_clears (m0, m1, NULL);
10420 if (result_rem)
10422 /* MPFR calculates quo in the host's long so it may
10423 return more bits in quo than the target int can hold
10424 if sizeof(host long) > sizeof(target int). This can
10425 happen even for native compilers in LP64 mode. In
10426 these cases, modulo the quo value with the largest
10427 number that the target int can hold while leaving one
10428 bit for the sign. */
10429 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10430 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10432 /* Dereference the quo pointer argument. */
10433 arg_quo = build_fold_indirect_ref (arg_quo);
10434 /* Proceed iff a valid pointer type was passed in. */
10435 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10437 /* Set the value. */
10438 tree result_quo
10439 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10440 build_int_cst (TREE_TYPE (arg_quo),
10441 integer_quo));
10442 TREE_SIDE_EFFECTS (result_quo) = 1;
10443 /* Combine the quo assignment with the rem. */
10444 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10445 result_quo, result_rem));
10450 return result;
10453 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10454 resulting value as a tree with type TYPE. The mpfr precision is
10455 set to the precision of TYPE. We assume that this mpfr function
10456 returns zero if the result could be calculated exactly within the
10457 requested precision. In addition, the integer pointer represented
10458 by ARG_SG will be dereferenced and set to the appropriate signgam
10459 (-1,1) value. */
10461 static tree
10462 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10464 tree result = NULL_TREE;
10466 STRIP_NOPS (arg);
10468 /* To proceed, MPFR must exactly represent the target floating point
10469 format, which only happens when the target base equals two. Also
10470 verify ARG is a constant and that ARG_SG is an int pointer. */
10471 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10472 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10473 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10474 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10476 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10478 /* In addition to NaN and Inf, the argument cannot be zero or a
10479 negative integer. */
10480 if (real_isfinite (ra)
10481 && ra->cl != rvc_zero
10482 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10484 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10485 const int prec = fmt->p;
10486 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10487 int inexact, sg;
10488 mpfr_t m;
10489 tree result_lg;
10491 mpfr_init2 (m, prec);
10492 mpfr_from_real (m, ra, GMP_RNDN);
10493 mpfr_clear_flags ();
10494 inexact = mpfr_lgamma (m, &sg, m, rnd);
10495 result_lg = do_mpfr_ckconv (m, type, inexact);
10496 mpfr_clear (m);
10497 if (result_lg)
10499 tree result_sg;
10501 /* Dereference the arg_sg pointer argument. */
10502 arg_sg = build_fold_indirect_ref (arg_sg);
10503 /* Assign the signgam value into *arg_sg. */
10504 result_sg = fold_build2 (MODIFY_EXPR,
10505 TREE_TYPE (arg_sg), arg_sg,
10506 build_int_cst (TREE_TYPE (arg_sg), sg));
10507 TREE_SIDE_EFFECTS (result_sg) = 1;
10508 /* Combine the signgam assignment with the lgamma result. */
10509 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10510 result_sg, result_lg));
10515 return result;
10518 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10519 mpc function FUNC on it and return the resulting value as a tree
10520 with type TYPE. The mpfr precision is set to the precision of
10521 TYPE. We assume that function FUNC returns zero if the result
10522 could be calculated exactly within the requested precision. If
10523 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10524 in the arguments and/or results. */
10526 tree
10527 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10528 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10530 tree result = NULL_TREE;
10532 STRIP_NOPS (arg0);
10533 STRIP_NOPS (arg1);
10535 /* To proceed, MPFR must exactly represent the target floating point
10536 format, which only happens when the target base equals two. */
10537 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10538 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10539 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10540 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10541 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10543 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10544 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10545 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10546 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10548 if (do_nonfinite
10549 || (real_isfinite (re0) && real_isfinite (im0)
10550 && real_isfinite (re1) && real_isfinite (im1)))
10552 const struct real_format *const fmt =
10553 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10554 const int prec = fmt->p;
10555 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
10556 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10557 int inexact;
10558 mpc_t m0, m1;
10560 mpc_init2 (m0, prec);
10561 mpc_init2 (m1, prec);
10562 mpfr_from_real (mpc_realref (m0), re0, rnd);
10563 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10564 mpfr_from_real (mpc_realref (m1), re1, rnd);
10565 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10566 mpfr_clear_flags ();
10567 inexact = func (m0, m0, m1, crnd);
10568 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10569 mpc_clear (m0);
10570 mpc_clear (m1);
10574 return result;
10577 /* A wrapper function for builtin folding that prevents warnings for
10578 "statement without effect" and the like, caused by removing the
10579 call node earlier than the warning is generated. */
10581 tree
10582 fold_call_stmt (gcall *stmt, bool ignore)
10584 tree ret = NULL_TREE;
10585 tree fndecl = gimple_call_fndecl (stmt);
10586 location_t loc = gimple_location (stmt);
10587 if (fndecl
10588 && TREE_CODE (fndecl) == FUNCTION_DECL
10589 && DECL_BUILT_IN (fndecl)
10590 && !gimple_call_va_arg_pack_p (stmt))
10592 int nargs = gimple_call_num_args (stmt);
10593 tree *args = (nargs > 0
10594 ? gimple_call_arg_ptr (stmt, 0)
10595 : &error_mark_node);
10597 if (avoid_folding_inline_builtin (fndecl))
10598 return NULL_TREE;
10599 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10601 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10603 else
10605 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10606 if (ret)
10608 /* Propagate location information from original call to
10609 expansion of builtin. Otherwise things like
10610 maybe_emit_chk_warning, that operate on the expansion
10611 of a builtin, will use the wrong location information. */
10612 if (gimple_has_location (stmt))
10614 tree realret = ret;
10615 if (TREE_CODE (ret) == NOP_EXPR)
10616 realret = TREE_OPERAND (ret, 0);
10617 if (CAN_HAVE_LOCATION_P (realret)
10618 && !EXPR_HAS_LOCATION (realret))
10619 SET_EXPR_LOCATION (realret, loc);
10620 return realret;
10622 return ret;
10626 return NULL_TREE;
10629 /* Look up the function in builtin_decl that corresponds to DECL
10630 and set ASMSPEC as its user assembler name. DECL must be a
10631 function decl that declares a builtin. */
10633 void
10634 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10636 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
10637 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
10638 && asmspec != 0);
10640 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10641 set_user_assembler_name (builtin, asmspec);
10643 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10644 && INT_TYPE_SIZE < BITS_PER_WORD)
10646 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
10647 set_user_assembler_libfunc ("ffs", asmspec);
10648 set_optab_libfunc (ffs_optab, mode, "ffs");
10652 /* Return true if DECL is a builtin that expands to a constant or similarly
10653 simple code. */
10654 bool
10655 is_simple_builtin (tree decl)
10657 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10658 switch (DECL_FUNCTION_CODE (decl))
10660 /* Builtins that expand to constants. */
10661 case BUILT_IN_CONSTANT_P:
10662 case BUILT_IN_EXPECT:
10663 case BUILT_IN_OBJECT_SIZE:
10664 case BUILT_IN_UNREACHABLE:
10665 /* Simple register moves or loads from stack. */
10666 case BUILT_IN_ASSUME_ALIGNED:
10667 case BUILT_IN_RETURN_ADDRESS:
10668 case BUILT_IN_EXTRACT_RETURN_ADDR:
10669 case BUILT_IN_FROB_RETURN_ADDR:
10670 case BUILT_IN_RETURN:
10671 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10672 case BUILT_IN_FRAME_ADDRESS:
10673 case BUILT_IN_VA_END:
10674 case BUILT_IN_STACK_SAVE:
10675 case BUILT_IN_STACK_RESTORE:
10676 /* Exception state returns or moves registers around. */
10677 case BUILT_IN_EH_FILTER:
10678 case BUILT_IN_EH_POINTER:
10679 case BUILT_IN_EH_COPY_VALUES:
10680 return true;
10682 default:
10683 return false;
10686 return false;
10689 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10690 most probably expanded inline into reasonably simple code. This is a
10691 superset of is_simple_builtin. */
10692 bool
10693 is_inexpensive_builtin (tree decl)
10695 if (!decl)
10696 return false;
10697 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10698 return true;
10699 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10700 switch (DECL_FUNCTION_CODE (decl))
10702 case BUILT_IN_ABS:
10703 CASE_BUILT_IN_ALLOCA:
10704 case BUILT_IN_BSWAP16:
10705 case BUILT_IN_BSWAP32:
10706 case BUILT_IN_BSWAP64:
10707 case BUILT_IN_CLZ:
10708 case BUILT_IN_CLZIMAX:
10709 case BUILT_IN_CLZL:
10710 case BUILT_IN_CLZLL:
10711 case BUILT_IN_CTZ:
10712 case BUILT_IN_CTZIMAX:
10713 case BUILT_IN_CTZL:
10714 case BUILT_IN_CTZLL:
10715 case BUILT_IN_FFS:
10716 case BUILT_IN_FFSIMAX:
10717 case BUILT_IN_FFSL:
10718 case BUILT_IN_FFSLL:
10719 case BUILT_IN_IMAXABS:
10720 case BUILT_IN_FINITE:
10721 case BUILT_IN_FINITEF:
10722 case BUILT_IN_FINITEL:
10723 case BUILT_IN_FINITED32:
10724 case BUILT_IN_FINITED64:
10725 case BUILT_IN_FINITED128:
10726 case BUILT_IN_FPCLASSIFY:
10727 case BUILT_IN_ISFINITE:
10728 case BUILT_IN_ISINF_SIGN:
10729 case BUILT_IN_ISINF:
10730 case BUILT_IN_ISINFF:
10731 case BUILT_IN_ISINFL:
10732 case BUILT_IN_ISINFD32:
10733 case BUILT_IN_ISINFD64:
10734 case BUILT_IN_ISINFD128:
10735 case BUILT_IN_ISNAN:
10736 case BUILT_IN_ISNANF:
10737 case BUILT_IN_ISNANL:
10738 case BUILT_IN_ISNAND32:
10739 case BUILT_IN_ISNAND64:
10740 case BUILT_IN_ISNAND128:
10741 case BUILT_IN_ISNORMAL:
10742 case BUILT_IN_ISGREATER:
10743 case BUILT_IN_ISGREATEREQUAL:
10744 case BUILT_IN_ISLESS:
10745 case BUILT_IN_ISLESSEQUAL:
10746 case BUILT_IN_ISLESSGREATER:
10747 case BUILT_IN_ISUNORDERED:
10748 case BUILT_IN_VA_ARG_PACK:
10749 case BUILT_IN_VA_ARG_PACK_LEN:
10750 case BUILT_IN_VA_COPY:
10751 case BUILT_IN_TRAP:
10752 case BUILT_IN_SAVEREGS:
10753 case BUILT_IN_POPCOUNTL:
10754 case BUILT_IN_POPCOUNTLL:
10755 case BUILT_IN_POPCOUNTIMAX:
10756 case BUILT_IN_POPCOUNT:
10757 case BUILT_IN_PARITYL:
10758 case BUILT_IN_PARITYLL:
10759 case BUILT_IN_PARITYIMAX:
10760 case BUILT_IN_PARITY:
10761 case BUILT_IN_LABS:
10762 case BUILT_IN_LLABS:
10763 case BUILT_IN_PREFETCH:
10764 case BUILT_IN_ACC_ON_DEVICE:
10765 return true;
10767 default:
10768 return is_simple_builtin (decl);
10771 return false;
10774 /* Return true if T is a constant and the value cast to a target char
10775 can be represented by a host char.
10776 Store the casted char constant in *P if so. */
10778 bool
10779 target_char_cst_p (tree t, char *p)
10781 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10782 return false;
10784 *p = (char)tree_to_uhwi (t);
10785 return true;
10788 /* Return the maximum object size. */
10790 tree
10791 max_object_size (void)
10793 /* To do: Make this a configurable parameter. */
10794 return TYPE_MAX_VALUE (ptrdiff_type_node);