* download_ecj: Remove.
[official-gcc.git] / gcc / builtins.c
blob5b76dfd9b2f250a082d40f7c47eb5f29cb049941
1 /* Expand builtin functions.
2 Copyright (C) 1988-2017 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 "stor-layout.h"
47 #include "calls.h"
48 #include "varasm.h"
49 #include "tree-object-size.h"
50 #include "realmpfr.h"
51 #include "cfgrtl.h"
52 #include "except.h"
53 #include "dojump.h"
54 #include "explow.h"
55 #include "stmt.h"
56 #include "expr.h"
57 #include "libfuncs.h"
58 #include "output.h"
59 #include "typeclass.h"
60 #include "langhooks.h"
61 #include "value-prof.h"
62 #include "builtins.h"
63 #include "asan.h"
64 #include "cilk.h"
65 #include "tree-chkp.h"
66 #include "rtl-chkp.h"
67 #include "internal-fn.h"
68 #include "case-cfn-macros.h"
69 #include "gimple-fold.h"
70 #include "intl.h"
72 struct target_builtins default_target_builtins;
73 #if SWITCHABLE_TARGET
74 struct target_builtins *this_target_builtins = &default_target_builtins;
75 #endif
77 /* Define the names of the builtin function types and codes. */
78 const char *const built_in_class_names[BUILT_IN_LAST]
79 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
81 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
82 const char * built_in_names[(int) END_BUILTINS] =
84 #include "builtins.def"
87 /* Setup an array of builtin_info_type, make sure each element decl is
88 initialized to NULL_TREE. */
89 builtin_info_type builtin_info[(int)END_BUILTINS];
91 /* Non-zero if __builtin_constant_p should be folded right away. */
92 bool force_folding_builtin_constant_p;
94 static rtx c_readstr (const char *, machine_mode);
95 static int target_char_cast (tree, char *);
96 static rtx get_memory_rtx (tree, tree);
97 static int apply_args_size (void);
98 static int apply_result_size (void);
99 static rtx result_vector (int, rtx);
100 static void expand_builtin_prefetch (tree);
101 static rtx expand_builtin_apply_args (void);
102 static rtx expand_builtin_apply_args_1 (void);
103 static rtx expand_builtin_apply (rtx, rtx, rtx);
104 static void expand_builtin_return (rtx);
105 static enum type_class type_to_class (tree);
106 static rtx expand_builtin_classify_type (tree);
107 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
109 static rtx expand_builtin_interclass_mathfn (tree, rtx);
110 static rtx expand_builtin_sincos (tree);
111 static rtx expand_builtin_cexpi (tree, rtx);
112 static rtx expand_builtin_int_roundingfn (tree, rtx);
113 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
114 static rtx expand_builtin_next_arg (void);
115 static rtx expand_builtin_va_start (tree);
116 static rtx expand_builtin_va_end (tree);
117 static rtx expand_builtin_va_copy (tree);
118 static rtx expand_builtin_strcmp (tree, rtx);
119 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
120 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
121 static rtx expand_builtin_memcpy (tree, rtx);
122 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
123 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
124 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
125 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
127 machine_mode, int, tree);
128 static rtx expand_builtin_strcat (tree, rtx);
129 static rtx expand_builtin_strcpy (tree, rtx);
130 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
131 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
132 static rtx expand_builtin_strncat (tree, rtx);
133 static rtx expand_builtin_strncpy (tree, rtx);
134 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
135 static rtx expand_builtin_memset (tree, rtx, machine_mode);
136 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
137 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
138 static rtx expand_builtin_bzero (tree);
139 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
140 static rtx expand_builtin_alloca (tree, bool);
141 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
142 static rtx expand_builtin_frame_address (tree, tree);
143 static tree stabilize_va_list_loc (location_t, tree, int);
144 static rtx expand_builtin_expect (tree, rtx);
145 static tree fold_builtin_constant_p (tree);
146 static tree fold_builtin_classify_type (tree);
147 static tree fold_builtin_strlen (location_t, tree, tree);
148 static tree fold_builtin_inf (location_t, tree, int);
149 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
150 static bool validate_arg (const_tree, enum tree_code code);
151 static rtx expand_builtin_fabs (tree, rtx, rtx);
152 static rtx expand_builtin_signbit (tree, rtx);
153 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
154 static tree fold_builtin_isascii (location_t, tree);
155 static tree fold_builtin_toascii (location_t, tree);
156 static tree fold_builtin_isdigit (location_t, tree);
157 static tree fold_builtin_fabs (location_t, tree, tree);
158 static tree fold_builtin_abs (location_t, tree, tree);
159 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
160 enum tree_code);
161 static tree fold_builtin_0 (location_t, tree);
162 static tree fold_builtin_1 (location_t, tree, tree);
163 static tree fold_builtin_2 (location_t, tree, tree, tree);
164 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
165 static tree fold_builtin_varargs (location_t, tree, tree*, int);
167 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
168 static tree fold_builtin_strspn (location_t, tree, tree);
169 static tree fold_builtin_strcspn (location_t, tree, tree);
171 static rtx expand_builtin_object_size (tree);
172 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
173 enum built_in_function);
174 static void maybe_emit_chk_warning (tree, enum built_in_function);
175 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
176 static void maybe_emit_free_warning (tree);
177 static tree fold_builtin_object_size (tree, tree);
179 unsigned HOST_WIDE_INT target_newline;
180 unsigned HOST_WIDE_INT target_percent;
181 static unsigned HOST_WIDE_INT target_c;
182 static unsigned HOST_WIDE_INT target_s;
183 char target_percent_c[3];
184 char target_percent_s[3];
185 char target_percent_s_newline[4];
186 static tree do_mpfr_remquo (tree, tree, tree);
187 static tree do_mpfr_lgamma_r (tree, tree, tree);
188 static void expand_builtin_sync_synchronize (void);
190 /* Return true if NAME starts with __builtin_ or __sync_. */
192 static bool
193 is_builtin_name (const char *name)
195 if (strncmp (name, "__builtin_", 10) == 0)
196 return true;
197 if (strncmp (name, "__sync_", 7) == 0)
198 return true;
199 if (strncmp (name, "__atomic_", 9) == 0)
200 return true;
201 if (flag_cilkplus
202 && (!strcmp (name, "__cilkrts_detach")
203 || !strcmp (name, "__cilkrts_pop_frame")))
204 return true;
205 return false;
209 /* Return true if DECL is a function symbol representing a built-in. */
211 bool
212 is_builtin_fn (tree decl)
214 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
217 /* Return true if NODE should be considered for inline expansion regardless
218 of the optimization level. This means whenever a function is invoked with
219 its "internal" name, which normally contains the prefix "__builtin". */
221 bool
222 called_as_built_in (tree node)
224 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
225 we want the name used to call the function, not the name it
226 will have. */
227 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
228 return is_builtin_name (name);
231 /* Compute values M and N such that M divides (address of EXP - N) and such
232 that N < M. If these numbers can be determined, store M in alignp and N in
233 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
234 *alignp and any bit-offset to *bitposp.
236 Note that the address (and thus the alignment) computed here is based
237 on the address to which a symbol resolves, whereas DECL_ALIGN is based
238 on the address at which an object is actually located. These two
239 addresses are not always the same. For example, on ARM targets,
240 the address &foo of a Thumb function foo() has the lowest bit set,
241 whereas foo() itself starts on an even address.
243 If ADDR_P is true we are taking the address of the memory reference EXP
244 and thus cannot rely on the access taking place. */
246 static bool
247 get_object_alignment_2 (tree exp, unsigned int *alignp,
248 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
250 HOST_WIDE_INT bitsize, bitpos;
251 tree offset;
252 machine_mode mode;
253 int unsignedp, reversep, volatilep;
254 unsigned int align = BITS_PER_UNIT;
255 bool known_alignment = false;
257 /* Get the innermost object and the constant (bitpos) and possibly
258 variable (offset) offset of the access. */
259 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
260 &unsignedp, &reversep, &volatilep);
262 /* Extract alignment information from the innermost object and
263 possibly adjust bitpos and offset. */
264 if (TREE_CODE (exp) == FUNCTION_DECL)
266 /* Function addresses can encode extra information besides their
267 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
268 allows the low bit to be used as a virtual bit, we know
269 that the address itself must be at least 2-byte aligned. */
270 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
271 align = 2 * BITS_PER_UNIT;
273 else if (TREE_CODE (exp) == LABEL_DECL)
275 else if (TREE_CODE (exp) == CONST_DECL)
277 /* The alignment of a CONST_DECL is determined by its initializer. */
278 exp = DECL_INITIAL (exp);
279 align = TYPE_ALIGN (TREE_TYPE (exp));
280 if (CONSTANT_CLASS_P (exp))
281 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
283 known_alignment = true;
285 else if (DECL_P (exp))
287 align = DECL_ALIGN (exp);
288 known_alignment = true;
290 else if (TREE_CODE (exp) == INDIRECT_REF
291 || TREE_CODE (exp) == MEM_REF
292 || TREE_CODE (exp) == TARGET_MEM_REF)
294 tree addr = TREE_OPERAND (exp, 0);
295 unsigned ptr_align;
296 unsigned HOST_WIDE_INT ptr_bitpos;
297 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
299 /* If the address is explicitely aligned, handle that. */
300 if (TREE_CODE (addr) == BIT_AND_EXPR
301 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
303 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
304 ptr_bitmask *= BITS_PER_UNIT;
305 align = least_bit_hwi (ptr_bitmask);
306 addr = TREE_OPERAND (addr, 0);
309 known_alignment
310 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
311 align = MAX (ptr_align, align);
313 /* Re-apply explicit alignment to the bitpos. */
314 ptr_bitpos &= ptr_bitmask;
316 /* The alignment of the pointer operand in a TARGET_MEM_REF
317 has to take the variable offset parts into account. */
318 if (TREE_CODE (exp) == TARGET_MEM_REF)
320 if (TMR_INDEX (exp))
322 unsigned HOST_WIDE_INT step = 1;
323 if (TMR_STEP (exp))
324 step = TREE_INT_CST_LOW (TMR_STEP (exp));
325 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
327 if (TMR_INDEX2 (exp))
328 align = BITS_PER_UNIT;
329 known_alignment = false;
332 /* When EXP is an actual memory reference then we can use
333 TYPE_ALIGN of a pointer indirection to derive alignment.
334 Do so only if get_pointer_alignment_1 did not reveal absolute
335 alignment knowledge and if using that alignment would
336 improve the situation. */
337 if (!addr_p && !known_alignment
338 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
339 align = TYPE_ALIGN (TREE_TYPE (exp));
340 else
342 /* Else adjust bitpos accordingly. */
343 bitpos += ptr_bitpos;
344 if (TREE_CODE (exp) == MEM_REF
345 || TREE_CODE (exp) == TARGET_MEM_REF)
346 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
349 else if (TREE_CODE (exp) == STRING_CST)
351 /* STRING_CST are the only constant objects we allow to be not
352 wrapped inside a CONST_DECL. */
353 align = TYPE_ALIGN (TREE_TYPE (exp));
354 if (CONSTANT_CLASS_P (exp))
355 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
357 known_alignment = true;
360 /* If there is a non-constant offset part extract the maximum
361 alignment that can prevail. */
362 if (offset)
364 unsigned int trailing_zeros = tree_ctz (offset);
365 if (trailing_zeros < HOST_BITS_PER_INT)
367 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
368 if (inner)
369 align = MIN (align, inner);
373 *alignp = align;
374 *bitposp = bitpos & (*alignp - 1);
375 return known_alignment;
378 /* For a memory reference expression EXP compute values M and N such that M
379 divides (&EXP - N) and such that N < M. If these numbers can be determined,
380 store M in alignp and N in *BITPOSP and return true. Otherwise return false
381 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
383 bool
384 get_object_alignment_1 (tree exp, unsigned int *alignp,
385 unsigned HOST_WIDE_INT *bitposp)
387 return get_object_alignment_2 (exp, alignp, bitposp, false);
390 /* Return the alignment in bits of EXP, an object. */
392 unsigned int
393 get_object_alignment (tree exp)
395 unsigned HOST_WIDE_INT bitpos = 0;
396 unsigned int align;
398 get_object_alignment_1 (exp, &align, &bitpos);
400 /* align and bitpos now specify known low bits of the pointer.
401 ptr & (align - 1) == bitpos. */
403 if (bitpos != 0)
404 align = least_bit_hwi (bitpos);
405 return align;
408 /* For a pointer valued expression EXP compute values M and N such that M
409 divides (EXP - N) and such that N < M. If these numbers can be determined,
410 store M in alignp and N in *BITPOSP and return true. Return false if
411 the results are just a conservative approximation.
413 If EXP is not a pointer, false is returned too. */
415 bool
416 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
417 unsigned HOST_WIDE_INT *bitposp)
419 STRIP_NOPS (exp);
421 if (TREE_CODE (exp) == ADDR_EXPR)
422 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
423 alignp, bitposp, true);
424 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
426 unsigned int align;
427 unsigned HOST_WIDE_INT bitpos;
428 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
429 &align, &bitpos);
430 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
431 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
432 else
434 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
435 if (trailing_zeros < HOST_BITS_PER_INT)
437 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
438 if (inner)
439 align = MIN (align, inner);
442 *alignp = align;
443 *bitposp = bitpos & (align - 1);
444 return res;
446 else if (TREE_CODE (exp) == SSA_NAME
447 && POINTER_TYPE_P (TREE_TYPE (exp)))
449 unsigned int ptr_align, ptr_misalign;
450 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
452 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
454 *bitposp = ptr_misalign * BITS_PER_UNIT;
455 *alignp = ptr_align * BITS_PER_UNIT;
456 /* Make sure to return a sensible alignment when the multiplication
457 by BITS_PER_UNIT overflowed. */
458 if (*alignp == 0)
459 *alignp = 1u << (HOST_BITS_PER_INT - 1);
460 /* We cannot really tell whether this result is an approximation. */
461 return false;
463 else
465 *bitposp = 0;
466 *alignp = BITS_PER_UNIT;
467 return false;
470 else if (TREE_CODE (exp) == INTEGER_CST)
472 *alignp = BIGGEST_ALIGNMENT;
473 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
474 & (BIGGEST_ALIGNMENT - 1));
475 return true;
478 *bitposp = 0;
479 *alignp = BITS_PER_UNIT;
480 return false;
483 /* Return the alignment in bits of EXP, a pointer valued expression.
484 The alignment returned is, by default, the alignment of the thing that
485 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
487 Otherwise, look at the expression to see if we can do better, i.e., if the
488 expression is actually pointing at an object whose alignment is tighter. */
490 unsigned int
491 get_pointer_alignment (tree exp)
493 unsigned HOST_WIDE_INT bitpos = 0;
494 unsigned int align;
496 get_pointer_alignment_1 (exp, &align, &bitpos);
498 /* align and bitpos now specify known low bits of the pointer.
499 ptr & (align - 1) == bitpos. */
501 if (bitpos != 0)
502 align = least_bit_hwi (bitpos);
504 return align;
507 /* Return the number of non-zero elements in the sequence
508 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
509 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
511 static unsigned
512 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
514 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
516 unsigned n;
518 if (eltsize == 1)
520 /* Optimize the common case of plain char. */
521 for (n = 0; n < maxelts; n++)
523 const char *elt = (const char*) ptr + n;
524 if (!*elt)
525 break;
528 else
530 for (n = 0; n < maxelts; n++)
532 const char *elt = (const char*) ptr + n * eltsize;
533 if (!memcmp (elt, "\0\0\0\0", eltsize))
534 break;
537 return n;
540 /* Compute the length of a null-terminated character string or wide
541 character string handling character sizes of 1, 2, and 4 bytes.
542 TREE_STRING_LENGTH is not the right way because it evaluates to
543 the size of the character array in bytes (as opposed to characters)
544 and because it can contain a zero byte in the middle.
546 ONLY_VALUE should be nonzero if the result is not going to be emitted
547 into the instruction stream and zero if it is going to be expanded.
548 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
549 is returned, otherwise NULL, since
550 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
551 evaluate the side-effects.
553 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
554 accesses. Note that this implies the result is not going to be emitted
555 into the instruction stream.
557 The value returned is of type `ssizetype'.
559 Unfortunately, string_constant can't access the values of const char
560 arrays with initializers, so neither can we do so here. */
562 tree
563 c_strlen (tree src, int only_value)
565 STRIP_NOPS (src);
566 if (TREE_CODE (src) == COND_EXPR
567 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
569 tree len1, len2;
571 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
572 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
573 if (tree_int_cst_equal (len1, len2))
574 return len1;
577 if (TREE_CODE (src) == COMPOUND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
579 return c_strlen (TREE_OPERAND (src, 1), only_value);
581 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
583 /* Offset from the beginning of the string in bytes. */
584 tree byteoff;
585 src = string_constant (src, &byteoff);
586 if (src == 0)
587 return NULL_TREE;
589 /* Determine the size of the string element. */
590 unsigned eltsize
591 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))));
593 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
594 length of SRC. */
595 unsigned maxelts = TREE_STRING_LENGTH (src) / eltsize - 1;
597 /* PTR can point to the byte representation of any string type, including
598 char* and wchar_t*. */
599 const char *ptr = TREE_STRING_POINTER (src);
601 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
603 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
604 compute the offset to the following null if we don't know where to
605 start searching for it. */
606 if (string_length (ptr, eltsize, maxelts) < maxelts)
608 /* Return when an embedded null character is found. */
609 return NULL_TREE;
612 /* We don't know the starting offset, but we do know that the string
613 has no internal zero bytes. We can assume that the offset falls
614 within the bounds of the string; otherwise, the programmer deserves
615 what he gets. Subtract the offset from the length of the string,
616 and return that. This would perhaps not be valid if we were dealing
617 with named arrays in addition to literal string constants. */
619 return size_diffop_loc (loc, size_int (maxelts * eltsize), byteoff);
622 /* Offset from the beginning of the string in elements. */
623 HOST_WIDE_INT eltoff;
625 /* We have a known offset into the string. Start searching there for
626 a null character if we can represent it as a single HOST_WIDE_INT. */
627 if (byteoff == 0)
628 eltoff = 0;
629 else if (! tree_fits_shwi_p (byteoff))
630 eltoff = -1;
631 else
632 eltoff = tree_to_shwi (byteoff) / eltsize;
634 /* If the offset is known to be out of bounds, warn, and call strlen at
635 runtime. */
636 if (eltoff < 0 || eltoff > maxelts)
638 /* Suppress multiple warnings for propagated constant strings. */
639 if (only_value != 2
640 && !TREE_NO_WARNING (src))
642 warning_at (loc, 0, "offset %qwi outside bounds of constant string",
643 eltoff);
644 TREE_NO_WARNING (src) = 1;
646 return NULL_TREE;
649 /* Use strlen to search for the first zero byte. Since any strings
650 constructed with build_string will have nulls appended, we win even
651 if we get handed something like (char[4])"abcd".
653 Since ELTOFF is our starting index into the string, no further
654 calculation is needed. */
655 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
656 maxelts - eltoff);
658 return ssize_int (len);
661 /* Return a constant integer corresponding to target reading
662 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
664 static rtx
665 c_readstr (const char *str, machine_mode mode)
667 HOST_WIDE_INT ch;
668 unsigned int i, j;
669 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
671 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
672 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
673 / HOST_BITS_PER_WIDE_INT;
675 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
676 for (i = 0; i < len; i++)
677 tmp[i] = 0;
679 ch = 1;
680 for (i = 0; i < GET_MODE_SIZE (mode); i++)
682 j = i;
683 if (WORDS_BIG_ENDIAN)
684 j = GET_MODE_SIZE (mode) - i - 1;
685 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
686 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
687 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
688 j *= BITS_PER_UNIT;
690 if (ch)
691 ch = (unsigned char) str[i];
692 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
695 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
696 return immed_wide_int_const (c, mode);
699 /* Cast a target constant CST to target CHAR and if that value fits into
700 host char type, return zero and put that value into variable pointed to by
701 P. */
703 static int
704 target_char_cast (tree cst, char *p)
706 unsigned HOST_WIDE_INT val, hostval;
708 if (TREE_CODE (cst) != INTEGER_CST
709 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
710 return 1;
712 /* Do not care if it fits or not right here. */
713 val = TREE_INT_CST_LOW (cst);
715 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
716 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
718 hostval = val;
719 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
720 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
722 if (val != hostval)
723 return 1;
725 *p = hostval;
726 return 0;
729 /* Similar to save_expr, but assumes that arbitrary code is not executed
730 in between the multiple evaluations. In particular, we assume that a
731 non-addressable local variable will not be modified. */
733 static tree
734 builtin_save_expr (tree exp)
736 if (TREE_CODE (exp) == SSA_NAME
737 || (TREE_ADDRESSABLE (exp) == 0
738 && (TREE_CODE (exp) == PARM_DECL
739 || (VAR_P (exp) && !TREE_STATIC (exp)))))
740 return exp;
742 return save_expr (exp);
745 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
746 times to get the address of either a higher stack frame, or a return
747 address located within it (depending on FNDECL_CODE). */
749 static rtx
750 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
752 int i;
753 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
754 if (tem == NULL_RTX)
756 /* For a zero count with __builtin_return_address, we don't care what
757 frame address we return, because target-specific definitions will
758 override us. Therefore frame pointer elimination is OK, and using
759 the soft frame pointer is OK.
761 For a nonzero count, or a zero count with __builtin_frame_address,
762 we require a stable offset from the current frame pointer to the
763 previous one, so we must use the hard frame pointer, and
764 we must disable frame pointer elimination. */
765 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
766 tem = frame_pointer_rtx;
767 else
769 tem = hard_frame_pointer_rtx;
771 /* Tell reload not to eliminate the frame pointer. */
772 crtl->accesses_prior_frames = 1;
776 if (count > 0)
777 SETUP_FRAME_ADDRESSES ();
779 /* On the SPARC, the return address is not in the frame, it is in a
780 register. There is no way to access it off of the current frame
781 pointer, but it can be accessed off the previous frame pointer by
782 reading the value from the register window save area. */
783 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
784 count--;
786 /* Scan back COUNT frames to the specified frame. */
787 for (i = 0; i < count; i++)
789 /* Assume the dynamic chain pointer is in the word that the
790 frame address points to, unless otherwise specified. */
791 tem = DYNAMIC_CHAIN_ADDRESS (tem);
792 tem = memory_address (Pmode, tem);
793 tem = gen_frame_mem (Pmode, tem);
794 tem = copy_to_reg (tem);
797 /* For __builtin_frame_address, return what we've got. But, on
798 the SPARC for example, we may have to add a bias. */
799 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
800 return FRAME_ADDR_RTX (tem);
802 /* For __builtin_return_address, get the return address from that frame. */
803 #ifdef RETURN_ADDR_RTX
804 tem = RETURN_ADDR_RTX (count, tem);
805 #else
806 tem = memory_address (Pmode,
807 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
808 tem = gen_frame_mem (Pmode, tem);
809 #endif
810 return tem;
813 /* Alias set used for setjmp buffer. */
814 static alias_set_type setjmp_alias_set = -1;
816 /* Construct the leading half of a __builtin_setjmp call. Control will
817 return to RECEIVER_LABEL. This is also called directly by the SJLJ
818 exception handling code. */
820 void
821 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
823 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
824 rtx stack_save;
825 rtx mem;
827 if (setjmp_alias_set == -1)
828 setjmp_alias_set = new_alias_set ();
830 buf_addr = convert_memory_address (Pmode, buf_addr);
832 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
834 /* We store the frame pointer and the address of receiver_label in
835 the buffer and use the rest of it for the stack save area, which
836 is machine-dependent. */
838 mem = gen_rtx_MEM (Pmode, buf_addr);
839 set_mem_alias_set (mem, setjmp_alias_set);
840 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
842 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
843 GET_MODE_SIZE (Pmode))),
844 set_mem_alias_set (mem, setjmp_alias_set);
846 emit_move_insn (validize_mem (mem),
847 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
849 stack_save = gen_rtx_MEM (sa_mode,
850 plus_constant (Pmode, buf_addr,
851 2 * GET_MODE_SIZE (Pmode)));
852 set_mem_alias_set (stack_save, setjmp_alias_set);
853 emit_stack_save (SAVE_NONLOCAL, &stack_save);
855 /* If there is further processing to do, do it. */
856 if (targetm.have_builtin_setjmp_setup ())
857 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
859 /* We have a nonlocal label. */
860 cfun->has_nonlocal_label = 1;
863 /* Construct the trailing part of a __builtin_setjmp call. This is
864 also called directly by the SJLJ exception handling code.
865 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
867 void
868 expand_builtin_setjmp_receiver (rtx receiver_label)
870 rtx chain;
872 /* Mark the FP as used when we get here, so we have to make sure it's
873 marked as used by this function. */
874 emit_use (hard_frame_pointer_rtx);
876 /* Mark the static chain as clobbered here so life information
877 doesn't get messed up for it. */
878 chain = targetm.calls.static_chain (current_function_decl, true);
879 if (chain && REG_P (chain))
880 emit_clobber (chain);
882 /* Now put in the code to restore the frame pointer, and argument
883 pointer, if needed. */
884 if (! targetm.have_nonlocal_goto ())
886 /* First adjust our frame pointer to its actual value. It was
887 previously set to the start of the virtual area corresponding to
888 the stacked variables when we branched here and now needs to be
889 adjusted to the actual hardware fp value.
891 Assignments to virtual registers are converted by
892 instantiate_virtual_regs into the corresponding assignment
893 to the underlying register (fp in this case) that makes
894 the original assignment true.
895 So the following insn will actually be decrementing fp by
896 STARTING_FRAME_OFFSET. */
897 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
899 /* Restoring the frame pointer also modifies the hard frame pointer.
900 Mark it used (so that the previous assignment remains live once
901 the frame pointer is eliminated) and clobbered (to represent the
902 implicit update from the assignment). */
903 emit_use (hard_frame_pointer_rtx);
904 emit_clobber (hard_frame_pointer_rtx);
907 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
909 /* If the argument pointer can be eliminated in favor of the
910 frame pointer, we don't need to restore it. We assume here
911 that if such an elimination is present, it can always be used.
912 This is the case on all known machines; if we don't make this
913 assumption, we do unnecessary saving on many machines. */
914 size_t i;
915 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
917 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
918 if (elim_regs[i].from == ARG_POINTER_REGNUM
919 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
920 break;
922 if (i == ARRAY_SIZE (elim_regs))
924 /* Now restore our arg pointer from the address at which it
925 was saved in our stack frame. */
926 emit_move_insn (crtl->args.internal_arg_pointer,
927 copy_to_reg (get_arg_pointer_save_area ()));
931 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
932 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
933 else if (targetm.have_nonlocal_goto_receiver ())
934 emit_insn (targetm.gen_nonlocal_goto_receiver ());
935 else
936 { /* Nothing */ }
938 /* We must not allow the code we just generated to be reordered by
939 scheduling. Specifically, the update of the frame pointer must
940 happen immediately, not later. */
941 emit_insn (gen_blockage ());
944 /* __builtin_longjmp is passed a pointer to an array of five words (not
945 all will be used on all machines). It operates similarly to the C
946 library function of the same name, but is more efficient. Much of
947 the code below is copied from the handling of non-local gotos. */
949 static void
950 expand_builtin_longjmp (rtx buf_addr, rtx value)
952 rtx fp, lab, stack;
953 rtx_insn *insn, *last;
954 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
956 /* DRAP is needed for stack realign if longjmp is expanded to current
957 function */
958 if (SUPPORTS_STACK_ALIGNMENT)
959 crtl->need_drap = true;
961 if (setjmp_alias_set == -1)
962 setjmp_alias_set = new_alias_set ();
964 buf_addr = convert_memory_address (Pmode, buf_addr);
966 buf_addr = force_reg (Pmode, buf_addr);
968 /* We require that the user must pass a second argument of 1, because
969 that is what builtin_setjmp will return. */
970 gcc_assert (value == const1_rtx);
972 last = get_last_insn ();
973 if (targetm.have_builtin_longjmp ())
974 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
975 else
977 fp = gen_rtx_MEM (Pmode, buf_addr);
978 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
979 GET_MODE_SIZE (Pmode)));
981 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
982 2 * GET_MODE_SIZE (Pmode)));
983 set_mem_alias_set (fp, setjmp_alias_set);
984 set_mem_alias_set (lab, setjmp_alias_set);
985 set_mem_alias_set (stack, setjmp_alias_set);
987 /* Pick up FP, label, and SP from the block and jump. This code is
988 from expand_goto in stmt.c; see there for detailed comments. */
989 if (targetm.have_nonlocal_goto ())
990 /* We have to pass a value to the nonlocal_goto pattern that will
991 get copied into the static_chain pointer, but it does not matter
992 what that value is, because builtin_setjmp does not use it. */
993 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
994 else
996 lab = copy_to_reg (lab);
998 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
999 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1001 emit_move_insn (hard_frame_pointer_rtx, fp);
1002 emit_stack_restore (SAVE_NONLOCAL, stack);
1004 emit_use (hard_frame_pointer_rtx);
1005 emit_use (stack_pointer_rtx);
1006 emit_indirect_jump (lab);
1010 /* Search backwards and mark the jump insn as a non-local goto.
1011 Note that this precludes the use of __builtin_longjmp to a
1012 __builtin_setjmp target in the same function. However, we've
1013 already cautioned the user that these functions are for
1014 internal exception handling use only. */
1015 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1017 gcc_assert (insn != last);
1019 if (JUMP_P (insn))
1021 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1022 break;
1024 else if (CALL_P (insn))
1025 break;
1029 static inline bool
1030 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1032 return (iter->i < iter->n);
1035 /* This function validates the types of a function call argument list
1036 against a specified list of tree_codes. If the last specifier is a 0,
1037 that represents an ellipsis, otherwise the last specifier must be a
1038 VOID_TYPE. */
1040 static bool
1041 validate_arglist (const_tree callexpr, ...)
1043 enum tree_code code;
1044 bool res = 0;
1045 va_list ap;
1046 const_call_expr_arg_iterator iter;
1047 const_tree arg;
1049 va_start (ap, callexpr);
1050 init_const_call_expr_arg_iterator (callexpr, &iter);
1052 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1053 tree fn = CALL_EXPR_FN (callexpr);
1054 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1056 for (unsigned argno = 1; ; ++argno)
1058 code = (enum tree_code) va_arg (ap, int);
1060 switch (code)
1062 case 0:
1063 /* This signifies an ellipses, any further arguments are all ok. */
1064 res = true;
1065 goto end;
1066 case VOID_TYPE:
1067 /* This signifies an endlink, if no arguments remain, return
1068 true, otherwise return false. */
1069 res = !more_const_call_expr_args_p (&iter);
1070 goto end;
1071 case POINTER_TYPE:
1072 /* The actual argument must be nonnull when either the whole
1073 called function has been declared nonnull, or when the formal
1074 argument corresponding to the actual argument has been. */
1075 if (argmap
1076 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1078 arg = next_const_call_expr_arg (&iter);
1079 if (!validate_arg (arg, code) || integer_zerop (arg))
1080 goto end;
1081 break;
1083 /* FALLTHRU */
1084 default:
1085 /* If no parameters remain or the parameter's code does not
1086 match the specified code, return false. Otherwise continue
1087 checking any remaining arguments. */
1088 arg = next_const_call_expr_arg (&iter);
1089 if (!validate_arg (arg, code))
1090 goto end;
1091 break;
1095 /* We need gotos here since we can only have one VA_CLOSE in a
1096 function. */
1097 end: ;
1098 va_end (ap);
1100 BITMAP_FREE (argmap);
1102 return res;
1105 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1106 and the address of the save area. */
1108 static rtx
1109 expand_builtin_nonlocal_goto (tree exp)
1111 tree t_label, t_save_area;
1112 rtx r_label, r_save_area, r_fp, r_sp;
1113 rtx_insn *insn;
1115 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1116 return NULL_RTX;
1118 t_label = CALL_EXPR_ARG (exp, 0);
1119 t_save_area = CALL_EXPR_ARG (exp, 1);
1121 r_label = expand_normal (t_label);
1122 r_label = convert_memory_address (Pmode, r_label);
1123 r_save_area = expand_normal (t_save_area);
1124 r_save_area = convert_memory_address (Pmode, r_save_area);
1125 /* Copy the address of the save location to a register just in case it was
1126 based on the frame pointer. */
1127 r_save_area = copy_to_reg (r_save_area);
1128 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1129 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1130 plus_constant (Pmode, r_save_area,
1131 GET_MODE_SIZE (Pmode)));
1133 crtl->has_nonlocal_goto = 1;
1135 /* ??? We no longer need to pass the static chain value, afaik. */
1136 if (targetm.have_nonlocal_goto ())
1137 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1138 else
1140 r_label = copy_to_reg (r_label);
1142 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1143 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1145 /* Restore frame pointer for containing function. */
1146 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1147 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1149 /* USE of hard_frame_pointer_rtx added for consistency;
1150 not clear if really needed. */
1151 emit_use (hard_frame_pointer_rtx);
1152 emit_use (stack_pointer_rtx);
1154 /* If the architecture is using a GP register, we must
1155 conservatively assume that the target function makes use of it.
1156 The prologue of functions with nonlocal gotos must therefore
1157 initialize the GP register to the appropriate value, and we
1158 must then make sure that this value is live at the point
1159 of the jump. (Note that this doesn't necessarily apply
1160 to targets with a nonlocal_goto pattern; they are free
1161 to implement it in their own way. Note also that this is
1162 a no-op if the GP register is a global invariant.) */
1163 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1164 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1165 emit_use (pic_offset_table_rtx);
1167 emit_indirect_jump (r_label);
1170 /* Search backwards to the jump insn and mark it as a
1171 non-local goto. */
1172 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1174 if (JUMP_P (insn))
1176 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1177 break;
1179 else if (CALL_P (insn))
1180 break;
1183 return const0_rtx;
1186 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1187 (not all will be used on all machines) that was passed to __builtin_setjmp.
1188 It updates the stack pointer in that block to the current value. This is
1189 also called directly by the SJLJ exception handling code. */
1191 void
1192 expand_builtin_update_setjmp_buf (rtx buf_addr)
1194 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1195 rtx stack_save
1196 = gen_rtx_MEM (sa_mode,
1197 memory_address
1198 (sa_mode,
1199 plus_constant (Pmode, buf_addr,
1200 2 * GET_MODE_SIZE (Pmode))));
1202 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1205 /* Expand a call to __builtin_prefetch. For a target that does not support
1206 data prefetch, evaluate the memory address argument in case it has side
1207 effects. */
1209 static void
1210 expand_builtin_prefetch (tree exp)
1212 tree arg0, arg1, arg2;
1213 int nargs;
1214 rtx op0, op1, op2;
1216 if (!validate_arglist (exp, POINTER_TYPE, 0))
1217 return;
1219 arg0 = CALL_EXPR_ARG (exp, 0);
1221 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1222 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1223 locality). */
1224 nargs = call_expr_nargs (exp);
1225 if (nargs > 1)
1226 arg1 = CALL_EXPR_ARG (exp, 1);
1227 else
1228 arg1 = integer_zero_node;
1229 if (nargs > 2)
1230 arg2 = CALL_EXPR_ARG (exp, 2);
1231 else
1232 arg2 = integer_three_node;
1234 /* Argument 0 is an address. */
1235 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1237 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1238 if (TREE_CODE (arg1) != INTEGER_CST)
1240 error ("second argument to %<__builtin_prefetch%> must be a constant");
1241 arg1 = integer_zero_node;
1243 op1 = expand_normal (arg1);
1244 /* Argument 1 must be either zero or one. */
1245 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1247 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1248 " using zero");
1249 op1 = const0_rtx;
1252 /* Argument 2 (locality) must be a compile-time constant int. */
1253 if (TREE_CODE (arg2) != INTEGER_CST)
1255 error ("third argument to %<__builtin_prefetch%> must be a constant");
1256 arg2 = integer_zero_node;
1258 op2 = expand_normal (arg2);
1259 /* Argument 2 must be 0, 1, 2, or 3. */
1260 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1262 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1263 op2 = const0_rtx;
1266 if (targetm.have_prefetch ())
1268 struct expand_operand ops[3];
1270 create_address_operand (&ops[0], op0);
1271 create_integer_operand (&ops[1], INTVAL (op1));
1272 create_integer_operand (&ops[2], INTVAL (op2));
1273 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1274 return;
1277 /* Don't do anything with direct references to volatile memory, but
1278 generate code to handle other side effects. */
1279 if (!MEM_P (op0) && side_effects_p (op0))
1280 emit_insn (op0);
1283 /* Get a MEM rtx for expression EXP which is the address of an operand
1284 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1285 the maximum length of the block of memory that might be accessed or
1286 NULL if unknown. */
1288 static rtx
1289 get_memory_rtx (tree exp, tree len)
1291 tree orig_exp = exp;
1292 rtx addr, mem;
1294 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1295 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1296 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1297 exp = TREE_OPERAND (exp, 0);
1299 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1300 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1302 /* Get an expression we can use to find the attributes to assign to MEM.
1303 First remove any nops. */
1304 while (CONVERT_EXPR_P (exp)
1305 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1306 exp = TREE_OPERAND (exp, 0);
1308 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1309 (as builtin stringops may alias with anything). */
1310 exp = fold_build2 (MEM_REF,
1311 build_array_type (char_type_node,
1312 build_range_type (sizetype,
1313 size_one_node, len)),
1314 exp, build_int_cst (ptr_type_node, 0));
1316 /* If the MEM_REF has no acceptable address, try to get the base object
1317 from the original address we got, and build an all-aliasing
1318 unknown-sized access to that one. */
1319 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1320 set_mem_attributes (mem, exp, 0);
1321 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1322 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1323 0))))
1325 exp = build_fold_addr_expr (exp);
1326 exp = fold_build2 (MEM_REF,
1327 build_array_type (char_type_node,
1328 build_range_type (sizetype,
1329 size_zero_node,
1330 NULL)),
1331 exp, build_int_cst (ptr_type_node, 0));
1332 set_mem_attributes (mem, exp, 0);
1334 set_mem_alias_set (mem, 0);
1335 return mem;
1338 /* Built-in functions to perform an untyped call and return. */
1340 #define apply_args_mode \
1341 (this_target_builtins->x_apply_args_mode)
1342 #define apply_result_mode \
1343 (this_target_builtins->x_apply_result_mode)
1345 /* Return the size required for the block returned by __builtin_apply_args,
1346 and initialize apply_args_mode. */
1348 static int
1349 apply_args_size (void)
1351 static int size = -1;
1352 int align;
1353 unsigned int regno;
1354 machine_mode mode;
1356 /* The values computed by this function never change. */
1357 if (size < 0)
1359 /* The first value is the incoming arg-pointer. */
1360 size = GET_MODE_SIZE (Pmode);
1362 /* The second value is the structure value address unless this is
1363 passed as an "invisible" first argument. */
1364 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1365 size += GET_MODE_SIZE (Pmode);
1367 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1368 if (FUNCTION_ARG_REGNO_P (regno))
1370 mode = targetm.calls.get_raw_arg_mode (regno);
1372 gcc_assert (mode != VOIDmode);
1374 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1375 if (size % align != 0)
1376 size = CEIL (size, align) * align;
1377 size += GET_MODE_SIZE (mode);
1378 apply_args_mode[regno] = mode;
1380 else
1382 apply_args_mode[regno] = VOIDmode;
1385 return size;
1388 /* Return the size required for the block returned by __builtin_apply,
1389 and initialize apply_result_mode. */
1391 static int
1392 apply_result_size (void)
1394 static int size = -1;
1395 int align, regno;
1396 machine_mode mode;
1398 /* The values computed by this function never change. */
1399 if (size < 0)
1401 size = 0;
1403 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1404 if (targetm.calls.function_value_regno_p (regno))
1406 mode = targetm.calls.get_raw_result_mode (regno);
1408 gcc_assert (mode != VOIDmode);
1410 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1411 if (size % align != 0)
1412 size = CEIL (size, align) * align;
1413 size += GET_MODE_SIZE (mode);
1414 apply_result_mode[regno] = mode;
1416 else
1417 apply_result_mode[regno] = VOIDmode;
1419 /* Allow targets that use untyped_call and untyped_return to override
1420 the size so that machine-specific information can be stored here. */
1421 #ifdef APPLY_RESULT_SIZE
1422 size = APPLY_RESULT_SIZE;
1423 #endif
1425 return size;
1428 /* Create a vector describing the result block RESULT. If SAVEP is true,
1429 the result block is used to save the values; otherwise it is used to
1430 restore the values. */
1432 static rtx
1433 result_vector (int savep, rtx result)
1435 int regno, size, align, nelts;
1436 machine_mode mode;
1437 rtx reg, mem;
1438 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1440 size = nelts = 0;
1441 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1442 if ((mode = apply_result_mode[regno]) != VOIDmode)
1444 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1445 if (size % align != 0)
1446 size = CEIL (size, align) * align;
1447 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1448 mem = adjust_address (result, mode, size);
1449 savevec[nelts++] = (savep
1450 ? gen_rtx_SET (mem, reg)
1451 : gen_rtx_SET (reg, mem));
1452 size += GET_MODE_SIZE (mode);
1454 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1457 /* Save the state required to perform an untyped call with the same
1458 arguments as were passed to the current function. */
1460 static rtx
1461 expand_builtin_apply_args_1 (void)
1463 rtx registers, tem;
1464 int size, align, regno;
1465 machine_mode mode;
1466 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1468 /* Create a block where the arg-pointer, structure value address,
1469 and argument registers can be saved. */
1470 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1472 /* Walk past the arg-pointer and structure value address. */
1473 size = GET_MODE_SIZE (Pmode);
1474 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1475 size += GET_MODE_SIZE (Pmode);
1477 /* Save each register used in calling a function to the block. */
1478 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1479 if ((mode = apply_args_mode[regno]) != VOIDmode)
1481 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1482 if (size % align != 0)
1483 size = CEIL (size, align) * align;
1485 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1487 emit_move_insn (adjust_address (registers, mode, size), tem);
1488 size += GET_MODE_SIZE (mode);
1491 /* Save the arg pointer to the block. */
1492 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1493 /* We need the pointer as the caller actually passed them to us, not
1494 as we might have pretended they were passed. Make sure it's a valid
1495 operand, as emit_move_insn isn't expected to handle a PLUS. */
1496 if (STACK_GROWS_DOWNWARD)
1498 = force_operand (plus_constant (Pmode, tem,
1499 crtl->args.pretend_args_size),
1500 NULL_RTX);
1501 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1503 size = GET_MODE_SIZE (Pmode);
1505 /* Save the structure value address unless this is passed as an
1506 "invisible" first argument. */
1507 if (struct_incoming_value)
1509 emit_move_insn (adjust_address (registers, Pmode, size),
1510 copy_to_reg (struct_incoming_value));
1511 size += GET_MODE_SIZE (Pmode);
1514 /* Return the address of the block. */
1515 return copy_addr_to_reg (XEXP (registers, 0));
1518 /* __builtin_apply_args returns block of memory allocated on
1519 the stack into which is stored the arg pointer, structure
1520 value address, static chain, and all the registers that might
1521 possibly be used in performing a function call. The code is
1522 moved to the start of the function so the incoming values are
1523 saved. */
1525 static rtx
1526 expand_builtin_apply_args (void)
1528 /* Don't do __builtin_apply_args more than once in a function.
1529 Save the result of the first call and reuse it. */
1530 if (apply_args_value != 0)
1531 return apply_args_value;
1533 /* When this function is called, it means that registers must be
1534 saved on entry to this function. So we migrate the
1535 call to the first insn of this function. */
1536 rtx temp;
1538 start_sequence ();
1539 temp = expand_builtin_apply_args_1 ();
1540 rtx_insn *seq = get_insns ();
1541 end_sequence ();
1543 apply_args_value = temp;
1545 /* Put the insns after the NOTE that starts the function.
1546 If this is inside a start_sequence, make the outer-level insn
1547 chain current, so the code is placed at the start of the
1548 function. If internal_arg_pointer is a non-virtual pseudo,
1549 it needs to be placed after the function that initializes
1550 that pseudo. */
1551 push_topmost_sequence ();
1552 if (REG_P (crtl->args.internal_arg_pointer)
1553 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1554 emit_insn_before (seq, parm_birth_insn);
1555 else
1556 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1557 pop_topmost_sequence ();
1558 return temp;
1562 /* Perform an untyped call and save the state required to perform an
1563 untyped return of whatever value was returned by the given function. */
1565 static rtx
1566 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1568 int size, align, regno;
1569 machine_mode mode;
1570 rtx incoming_args, result, reg, dest, src;
1571 rtx_call_insn *call_insn;
1572 rtx old_stack_level = 0;
1573 rtx call_fusage = 0;
1574 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1576 arguments = convert_memory_address (Pmode, arguments);
1578 /* Create a block where the return registers can be saved. */
1579 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1581 /* Fetch the arg pointer from the ARGUMENTS block. */
1582 incoming_args = gen_reg_rtx (Pmode);
1583 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1584 if (!STACK_GROWS_DOWNWARD)
1585 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1586 incoming_args, 0, OPTAB_LIB_WIDEN);
1588 /* Push a new argument block and copy the arguments. Do not allow
1589 the (potential) memcpy call below to interfere with our stack
1590 manipulations. */
1591 do_pending_stack_adjust ();
1592 NO_DEFER_POP;
1594 /* Save the stack with nonlocal if available. */
1595 if (targetm.have_save_stack_nonlocal ())
1596 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1597 else
1598 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1600 /* Allocate a block of memory onto the stack and copy the memory
1601 arguments to the outgoing arguments address. We can pass TRUE
1602 as the 4th argument because we just saved the stack pointer
1603 and will restore it right after the call. */
1604 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1606 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1607 may have already set current_function_calls_alloca to true.
1608 current_function_calls_alloca won't be set if argsize is zero,
1609 so we have to guarantee need_drap is true here. */
1610 if (SUPPORTS_STACK_ALIGNMENT)
1611 crtl->need_drap = true;
1613 dest = virtual_outgoing_args_rtx;
1614 if (!STACK_GROWS_DOWNWARD)
1616 if (CONST_INT_P (argsize))
1617 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1618 else
1619 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1621 dest = gen_rtx_MEM (BLKmode, dest);
1622 set_mem_align (dest, PARM_BOUNDARY);
1623 src = gen_rtx_MEM (BLKmode, incoming_args);
1624 set_mem_align (src, PARM_BOUNDARY);
1625 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1627 /* Refer to the argument block. */
1628 apply_args_size ();
1629 arguments = gen_rtx_MEM (BLKmode, arguments);
1630 set_mem_align (arguments, PARM_BOUNDARY);
1632 /* Walk past the arg-pointer and structure value address. */
1633 size = GET_MODE_SIZE (Pmode);
1634 if (struct_value)
1635 size += GET_MODE_SIZE (Pmode);
1637 /* Restore each of the registers previously saved. Make USE insns
1638 for each of these registers for use in making the call. */
1639 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1640 if ((mode = apply_args_mode[regno]) != VOIDmode)
1642 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1643 if (size % align != 0)
1644 size = CEIL (size, align) * align;
1645 reg = gen_rtx_REG (mode, regno);
1646 emit_move_insn (reg, adjust_address (arguments, mode, size));
1647 use_reg (&call_fusage, reg);
1648 size += GET_MODE_SIZE (mode);
1651 /* Restore the structure value address unless this is passed as an
1652 "invisible" first argument. */
1653 size = GET_MODE_SIZE (Pmode);
1654 if (struct_value)
1656 rtx value = gen_reg_rtx (Pmode);
1657 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1658 emit_move_insn (struct_value, value);
1659 if (REG_P (struct_value))
1660 use_reg (&call_fusage, struct_value);
1661 size += GET_MODE_SIZE (Pmode);
1664 /* All arguments and registers used for the call are set up by now! */
1665 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1667 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1668 and we don't want to load it into a register as an optimization,
1669 because prepare_call_address already did it if it should be done. */
1670 if (GET_CODE (function) != SYMBOL_REF)
1671 function = memory_address (FUNCTION_MODE, function);
1673 /* Generate the actual call instruction and save the return value. */
1674 if (targetm.have_untyped_call ())
1676 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1677 emit_call_insn (targetm.gen_untyped_call (mem, result,
1678 result_vector (1, result)));
1680 else if (targetm.have_call_value ())
1682 rtx valreg = 0;
1684 /* Locate the unique return register. It is not possible to
1685 express a call that sets more than one return register using
1686 call_value; use untyped_call for that. In fact, untyped_call
1687 only needs to save the return registers in the given block. */
1688 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1689 if ((mode = apply_result_mode[regno]) != VOIDmode)
1691 gcc_assert (!valreg); /* have_untyped_call required. */
1693 valreg = gen_rtx_REG (mode, regno);
1696 emit_insn (targetm.gen_call_value (valreg,
1697 gen_rtx_MEM (FUNCTION_MODE, function),
1698 const0_rtx, NULL_RTX, const0_rtx));
1700 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1702 else
1703 gcc_unreachable ();
1705 /* Find the CALL insn we just emitted, and attach the register usage
1706 information. */
1707 call_insn = last_call_insn ();
1708 add_function_usage_to (call_insn, call_fusage);
1710 /* Restore the stack. */
1711 if (targetm.have_save_stack_nonlocal ())
1712 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1713 else
1714 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1715 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1717 OK_DEFER_POP;
1719 /* Return the address of the result block. */
1720 result = copy_addr_to_reg (XEXP (result, 0));
1721 return convert_memory_address (ptr_mode, result);
1724 /* Perform an untyped return. */
1726 static void
1727 expand_builtin_return (rtx result)
1729 int size, align, regno;
1730 machine_mode mode;
1731 rtx reg;
1732 rtx_insn *call_fusage = 0;
1734 result = convert_memory_address (Pmode, result);
1736 apply_result_size ();
1737 result = gen_rtx_MEM (BLKmode, result);
1739 if (targetm.have_untyped_return ())
1741 rtx vector = result_vector (0, result);
1742 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1743 emit_barrier ();
1744 return;
1747 /* Restore the return value and note that each value is used. */
1748 size = 0;
1749 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1750 if ((mode = apply_result_mode[regno]) != VOIDmode)
1752 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1753 if (size % align != 0)
1754 size = CEIL (size, align) * align;
1755 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1756 emit_move_insn (reg, adjust_address (result, mode, size));
1758 push_to_sequence (call_fusage);
1759 emit_use (reg);
1760 call_fusage = get_insns ();
1761 end_sequence ();
1762 size += GET_MODE_SIZE (mode);
1765 /* Put the USE insns before the return. */
1766 emit_insn (call_fusage);
1768 /* Return whatever values was restored by jumping directly to the end
1769 of the function. */
1770 expand_naked_return ();
1773 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1775 static enum type_class
1776 type_to_class (tree type)
1778 switch (TREE_CODE (type))
1780 case VOID_TYPE: return void_type_class;
1781 case INTEGER_TYPE: return integer_type_class;
1782 case ENUMERAL_TYPE: return enumeral_type_class;
1783 case BOOLEAN_TYPE: return boolean_type_class;
1784 case POINTER_TYPE: return pointer_type_class;
1785 case REFERENCE_TYPE: return reference_type_class;
1786 case OFFSET_TYPE: return offset_type_class;
1787 case REAL_TYPE: return real_type_class;
1788 case COMPLEX_TYPE: return complex_type_class;
1789 case FUNCTION_TYPE: return function_type_class;
1790 case METHOD_TYPE: return method_type_class;
1791 case RECORD_TYPE: return record_type_class;
1792 case UNION_TYPE:
1793 case QUAL_UNION_TYPE: return union_type_class;
1794 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1795 ? string_type_class : array_type_class);
1796 case LANG_TYPE: return lang_type_class;
1797 default: return no_type_class;
1801 /* Expand a call EXP to __builtin_classify_type. */
1803 static rtx
1804 expand_builtin_classify_type (tree exp)
1806 if (call_expr_nargs (exp))
1807 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1808 return GEN_INT (no_type_class);
1811 /* This helper macro, meant to be used in mathfn_built_in below,
1812 determines which among a set of three builtin math functions is
1813 appropriate for a given type mode. The `F' and `L' cases are
1814 automatically generated from the `double' case. */
1815 #define CASE_MATHFN(MATHFN) \
1816 CASE_CFN_##MATHFN: \
1817 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1818 fcodel = BUILT_IN_##MATHFN##L ; break;
1819 /* Similar to above, but appends _R after any F/L suffix. */
1820 #define CASE_MATHFN_REENT(MATHFN) \
1821 case CFN_BUILT_IN_##MATHFN##_R: \
1822 case CFN_BUILT_IN_##MATHFN##F_R: \
1823 case CFN_BUILT_IN_##MATHFN##L_R: \
1824 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1825 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1827 /* Return a function equivalent to FN but operating on floating-point
1828 values of type TYPE, or END_BUILTINS if no such function exists.
1829 This is purely an operation on function codes; it does not guarantee
1830 that the target actually has an implementation of the function. */
1832 static built_in_function
1833 mathfn_built_in_2 (tree type, combined_fn fn)
1835 built_in_function fcode, fcodef, fcodel;
1837 switch (fn)
1839 CASE_MATHFN (ACOS)
1840 CASE_MATHFN (ACOSH)
1841 CASE_MATHFN (ASIN)
1842 CASE_MATHFN (ASINH)
1843 CASE_MATHFN (ATAN)
1844 CASE_MATHFN (ATAN2)
1845 CASE_MATHFN (ATANH)
1846 CASE_MATHFN (CBRT)
1847 CASE_MATHFN (CEIL)
1848 CASE_MATHFN (CEXPI)
1849 CASE_MATHFN (COPYSIGN)
1850 CASE_MATHFN (COS)
1851 CASE_MATHFN (COSH)
1852 CASE_MATHFN (DREM)
1853 CASE_MATHFN (ERF)
1854 CASE_MATHFN (ERFC)
1855 CASE_MATHFN (EXP)
1856 CASE_MATHFN (EXP10)
1857 CASE_MATHFN (EXP2)
1858 CASE_MATHFN (EXPM1)
1859 CASE_MATHFN (FABS)
1860 CASE_MATHFN (FDIM)
1861 CASE_MATHFN (FLOOR)
1862 CASE_MATHFN (FMA)
1863 CASE_MATHFN (FMAX)
1864 CASE_MATHFN (FMIN)
1865 CASE_MATHFN (FMOD)
1866 CASE_MATHFN (FREXP)
1867 CASE_MATHFN (GAMMA)
1868 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1869 CASE_MATHFN (HUGE_VAL)
1870 CASE_MATHFN (HYPOT)
1871 CASE_MATHFN (ILOGB)
1872 CASE_MATHFN (ICEIL)
1873 CASE_MATHFN (IFLOOR)
1874 CASE_MATHFN (INF)
1875 CASE_MATHFN (IRINT)
1876 CASE_MATHFN (IROUND)
1877 CASE_MATHFN (ISINF)
1878 CASE_MATHFN (J0)
1879 CASE_MATHFN (J1)
1880 CASE_MATHFN (JN)
1881 CASE_MATHFN (LCEIL)
1882 CASE_MATHFN (LDEXP)
1883 CASE_MATHFN (LFLOOR)
1884 CASE_MATHFN (LGAMMA)
1885 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1886 CASE_MATHFN (LLCEIL)
1887 CASE_MATHFN (LLFLOOR)
1888 CASE_MATHFN (LLRINT)
1889 CASE_MATHFN (LLROUND)
1890 CASE_MATHFN (LOG)
1891 CASE_MATHFN (LOG10)
1892 CASE_MATHFN (LOG1P)
1893 CASE_MATHFN (LOG2)
1894 CASE_MATHFN (LOGB)
1895 CASE_MATHFN (LRINT)
1896 CASE_MATHFN (LROUND)
1897 CASE_MATHFN (MODF)
1898 CASE_MATHFN (NAN)
1899 CASE_MATHFN (NANS)
1900 CASE_MATHFN (NEARBYINT)
1901 CASE_MATHFN (NEXTAFTER)
1902 CASE_MATHFN (NEXTTOWARD)
1903 CASE_MATHFN (POW)
1904 CASE_MATHFN (POWI)
1905 CASE_MATHFN (POW10)
1906 CASE_MATHFN (REMAINDER)
1907 CASE_MATHFN (REMQUO)
1908 CASE_MATHFN (RINT)
1909 CASE_MATHFN (ROUND)
1910 CASE_MATHFN (SCALB)
1911 CASE_MATHFN (SCALBLN)
1912 CASE_MATHFN (SCALBN)
1913 CASE_MATHFN (SIGNBIT)
1914 CASE_MATHFN (SIGNIFICAND)
1915 CASE_MATHFN (SIN)
1916 CASE_MATHFN (SINCOS)
1917 CASE_MATHFN (SINH)
1918 CASE_MATHFN (SQRT)
1919 CASE_MATHFN (TAN)
1920 CASE_MATHFN (TANH)
1921 CASE_MATHFN (TGAMMA)
1922 CASE_MATHFN (TRUNC)
1923 CASE_MATHFN (Y0)
1924 CASE_MATHFN (Y1)
1925 CASE_MATHFN (YN)
1927 default:
1928 return END_BUILTINS;
1931 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1932 return fcode;
1933 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1934 return fcodef;
1935 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1936 return fcodel;
1937 else
1938 return END_BUILTINS;
1941 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1942 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1943 otherwise use the explicit declaration. If we can't do the conversion,
1944 return null. */
1946 static tree
1947 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
1949 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
1950 if (fcode2 == END_BUILTINS)
1951 return NULL_TREE;
1953 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1954 return NULL_TREE;
1956 return builtin_decl_explicit (fcode2);
1959 /* Like mathfn_built_in_1, but always use the implicit array. */
1961 tree
1962 mathfn_built_in (tree type, combined_fn fn)
1964 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1967 /* Like mathfn_built_in_1, but take a built_in_function and
1968 always use the implicit array. */
1970 tree
1971 mathfn_built_in (tree type, enum built_in_function fn)
1973 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
1976 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
1977 return its code, otherwise return IFN_LAST. Note that this function
1978 only tests whether the function is defined in internals.def, not whether
1979 it is actually available on the target. */
1981 internal_fn
1982 associated_internal_fn (tree fndecl)
1984 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
1985 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
1986 switch (DECL_FUNCTION_CODE (fndecl))
1988 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
1989 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1990 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
1991 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1992 #include "internal-fn.def"
1994 CASE_FLT_FN (BUILT_IN_POW10):
1995 return IFN_EXP10;
1997 CASE_FLT_FN (BUILT_IN_DREM):
1998 return IFN_REMAINDER;
2000 CASE_FLT_FN (BUILT_IN_SCALBN):
2001 CASE_FLT_FN (BUILT_IN_SCALBLN):
2002 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2003 return IFN_LDEXP;
2004 return IFN_LAST;
2006 default:
2007 return IFN_LAST;
2011 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2012 on the current target by a call to an internal function, return the
2013 code of that internal function, otherwise return IFN_LAST. The caller
2014 is responsible for ensuring that any side-effects of the built-in
2015 call are dealt with correctly. E.g. if CALL sets errno, the caller
2016 must decide that the errno result isn't needed or make it available
2017 in some other way. */
2019 internal_fn
2020 replacement_internal_fn (gcall *call)
2022 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2024 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2025 if (ifn != IFN_LAST)
2027 tree_pair types = direct_internal_fn_types (ifn, call);
2028 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2029 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2030 return ifn;
2033 return IFN_LAST;
2036 /* Expand a call to the builtin trinary math functions (fma).
2037 Return NULL_RTX if a normal call should be emitted rather than expanding the
2038 function in-line. EXP is the expression that is a call to the builtin
2039 function; if convenient, the result should be placed in TARGET.
2040 SUBTARGET may be used as the target for computing one of EXP's
2041 operands. */
2043 static rtx
2044 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2046 optab builtin_optab;
2047 rtx op0, op1, op2, result;
2048 rtx_insn *insns;
2049 tree fndecl = get_callee_fndecl (exp);
2050 tree arg0, arg1, arg2;
2051 machine_mode mode;
2053 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2054 return NULL_RTX;
2056 arg0 = CALL_EXPR_ARG (exp, 0);
2057 arg1 = CALL_EXPR_ARG (exp, 1);
2058 arg2 = CALL_EXPR_ARG (exp, 2);
2060 switch (DECL_FUNCTION_CODE (fndecl))
2062 CASE_FLT_FN (BUILT_IN_FMA):
2063 builtin_optab = fma_optab; break;
2064 default:
2065 gcc_unreachable ();
2068 /* Make a suitable register to place result in. */
2069 mode = TYPE_MODE (TREE_TYPE (exp));
2071 /* Before working hard, check whether the instruction is available. */
2072 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2073 return NULL_RTX;
2075 result = gen_reg_rtx (mode);
2077 /* Always stabilize the argument list. */
2078 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2079 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2080 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2082 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2083 op1 = expand_normal (arg1);
2084 op2 = expand_normal (arg2);
2086 start_sequence ();
2088 /* Compute into RESULT.
2089 Set RESULT to wherever the result comes back. */
2090 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2091 result, 0);
2093 /* If we were unable to expand via the builtin, stop the sequence
2094 (without outputting the insns) and call to the library function
2095 with the stabilized argument list. */
2096 if (result == 0)
2098 end_sequence ();
2099 return expand_call (exp, target, target == const0_rtx);
2102 /* Output the entire sequence. */
2103 insns = get_insns ();
2104 end_sequence ();
2105 emit_insn (insns);
2107 return result;
2110 /* Expand a call to the builtin sin and cos math functions.
2111 Return NULL_RTX if a normal call should be emitted rather than expanding the
2112 function in-line. EXP is the expression that is a call to the builtin
2113 function; if convenient, the result should be placed in TARGET.
2114 SUBTARGET may be used as the target for computing one of EXP's
2115 operands. */
2117 static rtx
2118 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2120 optab builtin_optab;
2121 rtx op0;
2122 rtx_insn *insns;
2123 tree fndecl = get_callee_fndecl (exp);
2124 machine_mode mode;
2125 tree arg;
2127 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2128 return NULL_RTX;
2130 arg = CALL_EXPR_ARG (exp, 0);
2132 switch (DECL_FUNCTION_CODE (fndecl))
2134 CASE_FLT_FN (BUILT_IN_SIN):
2135 CASE_FLT_FN (BUILT_IN_COS):
2136 builtin_optab = sincos_optab; break;
2137 default:
2138 gcc_unreachable ();
2141 /* Make a suitable register to place result in. */
2142 mode = TYPE_MODE (TREE_TYPE (exp));
2144 /* Check if sincos insn is available, otherwise fallback
2145 to sin or cos insn. */
2146 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2147 switch (DECL_FUNCTION_CODE (fndecl))
2149 CASE_FLT_FN (BUILT_IN_SIN):
2150 builtin_optab = sin_optab; break;
2151 CASE_FLT_FN (BUILT_IN_COS):
2152 builtin_optab = cos_optab; break;
2153 default:
2154 gcc_unreachable ();
2157 /* Before working hard, check whether the instruction is available. */
2158 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2160 rtx result = gen_reg_rtx (mode);
2162 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2163 need to expand the argument again. This way, we will not perform
2164 side-effects more the once. */
2165 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2167 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2169 start_sequence ();
2171 /* Compute into RESULT.
2172 Set RESULT to wherever the result comes back. */
2173 if (builtin_optab == sincos_optab)
2175 int ok;
2177 switch (DECL_FUNCTION_CODE (fndecl))
2179 CASE_FLT_FN (BUILT_IN_SIN):
2180 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2181 break;
2182 CASE_FLT_FN (BUILT_IN_COS):
2183 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2184 break;
2185 default:
2186 gcc_unreachable ();
2188 gcc_assert (ok);
2190 else
2191 result = expand_unop (mode, builtin_optab, op0, result, 0);
2193 if (result != 0)
2195 /* Output the entire sequence. */
2196 insns = get_insns ();
2197 end_sequence ();
2198 emit_insn (insns);
2199 return result;
2202 /* If we were unable to expand via the builtin, stop the sequence
2203 (without outputting the insns) and call to the library function
2204 with the stabilized argument list. */
2205 end_sequence ();
2208 return expand_call (exp, target, target == const0_rtx);
2211 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2212 return an RTL instruction code that implements the functionality.
2213 If that isn't possible or available return CODE_FOR_nothing. */
2215 static enum insn_code
2216 interclass_mathfn_icode (tree arg, tree fndecl)
2218 bool errno_set = false;
2219 optab builtin_optab = unknown_optab;
2220 machine_mode mode;
2222 switch (DECL_FUNCTION_CODE (fndecl))
2224 CASE_FLT_FN (BUILT_IN_ILOGB):
2225 errno_set = true; builtin_optab = ilogb_optab; break;
2226 CASE_FLT_FN (BUILT_IN_ISINF):
2227 builtin_optab = isinf_optab; break;
2228 case BUILT_IN_ISNORMAL:
2229 case BUILT_IN_ISFINITE:
2230 CASE_FLT_FN (BUILT_IN_FINITE):
2231 case BUILT_IN_FINITED32:
2232 case BUILT_IN_FINITED64:
2233 case BUILT_IN_FINITED128:
2234 case BUILT_IN_ISINFD32:
2235 case BUILT_IN_ISINFD64:
2236 case BUILT_IN_ISINFD128:
2237 /* These builtins have no optabs (yet). */
2238 break;
2239 default:
2240 gcc_unreachable ();
2243 /* There's no easy way to detect the case we need to set EDOM. */
2244 if (flag_errno_math && errno_set)
2245 return CODE_FOR_nothing;
2247 /* Optab mode depends on the mode of the input argument. */
2248 mode = TYPE_MODE (TREE_TYPE (arg));
2250 if (builtin_optab)
2251 return optab_handler (builtin_optab, mode);
2252 return CODE_FOR_nothing;
2255 /* Expand a call to one of the builtin math functions that operate on
2256 floating point argument and output an integer result (ilogb, isinf,
2257 isnan, etc).
2258 Return 0 if a normal call should be emitted rather than expanding the
2259 function in-line. EXP is the expression that is a call to the builtin
2260 function; if convenient, the result should be placed in TARGET. */
2262 static rtx
2263 expand_builtin_interclass_mathfn (tree exp, rtx target)
2265 enum insn_code icode = CODE_FOR_nothing;
2266 rtx op0;
2267 tree fndecl = get_callee_fndecl (exp);
2268 machine_mode mode;
2269 tree arg;
2271 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2272 return NULL_RTX;
2274 arg = CALL_EXPR_ARG (exp, 0);
2275 icode = interclass_mathfn_icode (arg, fndecl);
2276 mode = TYPE_MODE (TREE_TYPE (arg));
2278 if (icode != CODE_FOR_nothing)
2280 struct expand_operand ops[1];
2281 rtx_insn *last = get_last_insn ();
2282 tree orig_arg = arg;
2284 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2285 need to expand the argument again. This way, we will not perform
2286 side-effects more the once. */
2287 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2289 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2291 if (mode != GET_MODE (op0))
2292 op0 = convert_to_mode (mode, op0, 0);
2294 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2295 if (maybe_legitimize_operands (icode, 0, 1, ops)
2296 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2297 return ops[0].value;
2299 delete_insns_since (last);
2300 CALL_EXPR_ARG (exp, 0) = orig_arg;
2303 return NULL_RTX;
2306 /* Expand a call to the builtin sincos math function.
2307 Return NULL_RTX if a normal call should be emitted rather than expanding the
2308 function in-line. EXP is the expression that is a call to the builtin
2309 function. */
2311 static rtx
2312 expand_builtin_sincos (tree exp)
2314 rtx op0, op1, op2, target1, target2;
2315 machine_mode mode;
2316 tree arg, sinp, cosp;
2317 int result;
2318 location_t loc = EXPR_LOCATION (exp);
2319 tree alias_type, alias_off;
2321 if (!validate_arglist (exp, REAL_TYPE,
2322 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2323 return NULL_RTX;
2325 arg = CALL_EXPR_ARG (exp, 0);
2326 sinp = CALL_EXPR_ARG (exp, 1);
2327 cosp = CALL_EXPR_ARG (exp, 2);
2329 /* Make a suitable register to place result in. */
2330 mode = TYPE_MODE (TREE_TYPE (arg));
2332 /* Check if sincos insn is available, otherwise emit the call. */
2333 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2334 return NULL_RTX;
2336 target1 = gen_reg_rtx (mode);
2337 target2 = gen_reg_rtx (mode);
2339 op0 = expand_normal (arg);
2340 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2341 alias_off = build_int_cst (alias_type, 0);
2342 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2343 sinp, alias_off));
2344 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2345 cosp, alias_off));
2347 /* Compute into target1 and target2.
2348 Set TARGET to wherever the result comes back. */
2349 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2350 gcc_assert (result);
2352 /* Move target1 and target2 to the memory locations indicated
2353 by op1 and op2. */
2354 emit_move_insn (op1, target1);
2355 emit_move_insn (op2, target2);
2357 return const0_rtx;
2360 /* Expand a call to the internal cexpi builtin to the sincos math function.
2361 EXP is the expression that is a call to the builtin function; if convenient,
2362 the result should be placed in TARGET. */
2364 static rtx
2365 expand_builtin_cexpi (tree exp, rtx target)
2367 tree fndecl = get_callee_fndecl (exp);
2368 tree arg, type;
2369 machine_mode mode;
2370 rtx op0, op1, op2;
2371 location_t loc = EXPR_LOCATION (exp);
2373 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2374 return NULL_RTX;
2376 arg = CALL_EXPR_ARG (exp, 0);
2377 type = TREE_TYPE (arg);
2378 mode = TYPE_MODE (TREE_TYPE (arg));
2380 /* Try expanding via a sincos optab, fall back to emitting a libcall
2381 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2382 is only generated from sincos, cexp or if we have either of them. */
2383 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2385 op1 = gen_reg_rtx (mode);
2386 op2 = gen_reg_rtx (mode);
2388 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2390 /* Compute into op1 and op2. */
2391 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2393 else if (targetm.libc_has_function (function_sincos))
2395 tree call, fn = NULL_TREE;
2396 tree top1, top2;
2397 rtx op1a, op2a;
2399 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2400 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2401 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2402 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2403 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2404 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2405 else
2406 gcc_unreachable ();
2408 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2409 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2410 op1a = copy_addr_to_reg (XEXP (op1, 0));
2411 op2a = copy_addr_to_reg (XEXP (op2, 0));
2412 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2413 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2415 /* Make sure not to fold the sincos call again. */
2416 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2417 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2418 call, 3, arg, top1, top2));
2420 else
2422 tree call, fn = NULL_TREE, narg;
2423 tree ctype = build_complex_type (type);
2425 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2426 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2427 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2428 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2429 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2430 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2431 else
2432 gcc_unreachable ();
2434 /* If we don't have a decl for cexp create one. This is the
2435 friendliest fallback if the user calls __builtin_cexpi
2436 without full target C99 function support. */
2437 if (fn == NULL_TREE)
2439 tree fntype;
2440 const char *name = NULL;
2442 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2443 name = "cexpf";
2444 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2445 name = "cexp";
2446 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2447 name = "cexpl";
2449 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2450 fn = build_fn_decl (name, fntype);
2453 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2454 build_real (type, dconst0), arg);
2456 /* Make sure not to fold the cexp call again. */
2457 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2458 return expand_expr (build_call_nary (ctype, call, 1, narg),
2459 target, VOIDmode, EXPAND_NORMAL);
2462 /* Now build the proper return type. */
2463 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2464 make_tree (TREE_TYPE (arg), op2),
2465 make_tree (TREE_TYPE (arg), op1)),
2466 target, VOIDmode, EXPAND_NORMAL);
2469 /* Conveniently construct a function call expression. FNDECL names the
2470 function to be called, N is the number of arguments, and the "..."
2471 parameters are the argument expressions. Unlike build_call_exr
2472 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2474 static tree
2475 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2477 va_list ap;
2478 tree fntype = TREE_TYPE (fndecl);
2479 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2481 va_start (ap, n);
2482 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2483 va_end (ap);
2484 SET_EXPR_LOCATION (fn, loc);
2485 return fn;
2488 /* Expand a call to one of the builtin rounding functions gcc defines
2489 as an extension (lfloor and lceil). As these are gcc extensions we
2490 do not need to worry about setting errno to EDOM.
2491 If expanding via optab fails, lower expression to (int)(floor(x)).
2492 EXP is the expression that is a call to the builtin function;
2493 if convenient, the result should be placed in TARGET. */
2495 static rtx
2496 expand_builtin_int_roundingfn (tree exp, rtx target)
2498 convert_optab builtin_optab;
2499 rtx op0, tmp;
2500 rtx_insn *insns;
2501 tree fndecl = get_callee_fndecl (exp);
2502 enum built_in_function fallback_fn;
2503 tree fallback_fndecl;
2504 machine_mode mode;
2505 tree arg;
2507 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2508 gcc_unreachable ();
2510 arg = CALL_EXPR_ARG (exp, 0);
2512 switch (DECL_FUNCTION_CODE (fndecl))
2514 CASE_FLT_FN (BUILT_IN_ICEIL):
2515 CASE_FLT_FN (BUILT_IN_LCEIL):
2516 CASE_FLT_FN (BUILT_IN_LLCEIL):
2517 builtin_optab = lceil_optab;
2518 fallback_fn = BUILT_IN_CEIL;
2519 break;
2521 CASE_FLT_FN (BUILT_IN_IFLOOR):
2522 CASE_FLT_FN (BUILT_IN_LFLOOR):
2523 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2524 builtin_optab = lfloor_optab;
2525 fallback_fn = BUILT_IN_FLOOR;
2526 break;
2528 default:
2529 gcc_unreachable ();
2532 /* Make a suitable register to place result in. */
2533 mode = TYPE_MODE (TREE_TYPE (exp));
2535 target = gen_reg_rtx (mode);
2537 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2538 need to expand the argument again. This way, we will not perform
2539 side-effects more the once. */
2540 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2542 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2544 start_sequence ();
2546 /* Compute into TARGET. */
2547 if (expand_sfix_optab (target, op0, builtin_optab))
2549 /* Output the entire sequence. */
2550 insns = get_insns ();
2551 end_sequence ();
2552 emit_insn (insns);
2553 return target;
2556 /* If we were unable to expand via the builtin, stop the sequence
2557 (without outputting the insns). */
2558 end_sequence ();
2560 /* Fall back to floating point rounding optab. */
2561 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2563 /* For non-C99 targets we may end up without a fallback fndecl here
2564 if the user called __builtin_lfloor directly. In this case emit
2565 a call to the floor/ceil variants nevertheless. This should result
2566 in the best user experience for not full C99 targets. */
2567 if (fallback_fndecl == NULL_TREE)
2569 tree fntype;
2570 const char *name = NULL;
2572 switch (DECL_FUNCTION_CODE (fndecl))
2574 case BUILT_IN_ICEIL:
2575 case BUILT_IN_LCEIL:
2576 case BUILT_IN_LLCEIL:
2577 name = "ceil";
2578 break;
2579 case BUILT_IN_ICEILF:
2580 case BUILT_IN_LCEILF:
2581 case BUILT_IN_LLCEILF:
2582 name = "ceilf";
2583 break;
2584 case BUILT_IN_ICEILL:
2585 case BUILT_IN_LCEILL:
2586 case BUILT_IN_LLCEILL:
2587 name = "ceill";
2588 break;
2589 case BUILT_IN_IFLOOR:
2590 case BUILT_IN_LFLOOR:
2591 case BUILT_IN_LLFLOOR:
2592 name = "floor";
2593 break;
2594 case BUILT_IN_IFLOORF:
2595 case BUILT_IN_LFLOORF:
2596 case BUILT_IN_LLFLOORF:
2597 name = "floorf";
2598 break;
2599 case BUILT_IN_IFLOORL:
2600 case BUILT_IN_LFLOORL:
2601 case BUILT_IN_LLFLOORL:
2602 name = "floorl";
2603 break;
2604 default:
2605 gcc_unreachable ();
2608 fntype = build_function_type_list (TREE_TYPE (arg),
2609 TREE_TYPE (arg), NULL_TREE);
2610 fallback_fndecl = build_fn_decl (name, fntype);
2613 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2615 tmp = expand_normal (exp);
2616 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2618 /* Truncate the result of floating point optab to integer
2619 via expand_fix (). */
2620 target = gen_reg_rtx (mode);
2621 expand_fix (target, tmp, 0);
2623 return target;
2626 /* Expand a call to one of the builtin math functions doing integer
2627 conversion (lrint).
2628 Return 0 if a normal call should be emitted rather than expanding the
2629 function in-line. EXP is the expression that is a call to the builtin
2630 function; if convenient, the result should be placed in TARGET. */
2632 static rtx
2633 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2635 convert_optab builtin_optab;
2636 rtx op0;
2637 rtx_insn *insns;
2638 tree fndecl = get_callee_fndecl (exp);
2639 tree arg;
2640 machine_mode mode;
2641 enum built_in_function fallback_fn = BUILT_IN_NONE;
2643 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2644 gcc_unreachable ();
2646 arg = CALL_EXPR_ARG (exp, 0);
2648 switch (DECL_FUNCTION_CODE (fndecl))
2650 CASE_FLT_FN (BUILT_IN_IRINT):
2651 fallback_fn = BUILT_IN_LRINT;
2652 gcc_fallthrough ();
2653 CASE_FLT_FN (BUILT_IN_LRINT):
2654 CASE_FLT_FN (BUILT_IN_LLRINT):
2655 builtin_optab = lrint_optab;
2656 break;
2658 CASE_FLT_FN (BUILT_IN_IROUND):
2659 fallback_fn = BUILT_IN_LROUND;
2660 gcc_fallthrough ();
2661 CASE_FLT_FN (BUILT_IN_LROUND):
2662 CASE_FLT_FN (BUILT_IN_LLROUND):
2663 builtin_optab = lround_optab;
2664 break;
2666 default:
2667 gcc_unreachable ();
2670 /* There's no easy way to detect the case we need to set EDOM. */
2671 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2672 return NULL_RTX;
2674 /* Make a suitable register to place result in. */
2675 mode = TYPE_MODE (TREE_TYPE (exp));
2677 /* There's no easy way to detect the case we need to set EDOM. */
2678 if (!flag_errno_math)
2680 rtx result = gen_reg_rtx (mode);
2682 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2683 need to expand the argument again. This way, we will not perform
2684 side-effects more the once. */
2685 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2687 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2689 start_sequence ();
2691 if (expand_sfix_optab (result, op0, builtin_optab))
2693 /* Output the entire sequence. */
2694 insns = get_insns ();
2695 end_sequence ();
2696 emit_insn (insns);
2697 return result;
2700 /* If we were unable to expand via the builtin, stop the sequence
2701 (without outputting the insns) and call to the library function
2702 with the stabilized argument list. */
2703 end_sequence ();
2706 if (fallback_fn != BUILT_IN_NONE)
2708 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2709 targets, (int) round (x) should never be transformed into
2710 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2711 a call to lround in the hope that the target provides at least some
2712 C99 functions. This should result in the best user experience for
2713 not full C99 targets. */
2714 tree fallback_fndecl = mathfn_built_in_1
2715 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2717 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2718 fallback_fndecl, 1, arg);
2720 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2721 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2722 return convert_to_mode (mode, target, 0);
2725 return expand_call (exp, target, target == const0_rtx);
2728 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2729 a normal call should be emitted rather than expanding the function
2730 in-line. EXP is the expression that is a call to the builtin
2731 function; if convenient, the result should be placed in TARGET. */
2733 static rtx
2734 expand_builtin_powi (tree exp, rtx target)
2736 tree arg0, arg1;
2737 rtx op0, op1;
2738 machine_mode mode;
2739 machine_mode mode2;
2741 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2742 return NULL_RTX;
2744 arg0 = CALL_EXPR_ARG (exp, 0);
2745 arg1 = CALL_EXPR_ARG (exp, 1);
2746 mode = TYPE_MODE (TREE_TYPE (exp));
2748 /* Emit a libcall to libgcc. */
2750 /* Mode of the 2nd argument must match that of an int. */
2751 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2753 if (target == NULL_RTX)
2754 target = gen_reg_rtx (mode);
2756 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2757 if (GET_MODE (op0) != mode)
2758 op0 = convert_to_mode (mode, op0, 0);
2759 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2760 if (GET_MODE (op1) != mode2)
2761 op1 = convert_to_mode (mode2, op1, 0);
2763 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2764 target, LCT_CONST, mode, 2,
2765 op0, mode, op1, mode2);
2767 return target;
2770 /* Expand expression EXP which is a call to the strlen builtin. Return
2771 NULL_RTX if we failed the caller should emit a normal call, otherwise
2772 try to get the result in TARGET, if convenient. */
2774 static rtx
2775 expand_builtin_strlen (tree exp, rtx target,
2776 machine_mode target_mode)
2778 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2779 return NULL_RTX;
2780 else
2782 struct expand_operand ops[4];
2783 rtx pat;
2784 tree len;
2785 tree src = CALL_EXPR_ARG (exp, 0);
2786 rtx src_reg;
2787 rtx_insn *before_strlen;
2788 machine_mode insn_mode = target_mode;
2789 enum insn_code icode = CODE_FOR_nothing;
2790 unsigned int align;
2792 /* If the length can be computed at compile-time, return it. */
2793 len = c_strlen (src, 0);
2794 if (len)
2795 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2797 /* If the length can be computed at compile-time and is constant
2798 integer, but there are side-effects in src, evaluate
2799 src for side-effects, then return len.
2800 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2801 can be optimized into: i++; x = 3; */
2802 len = c_strlen (src, 1);
2803 if (len && TREE_CODE (len) == INTEGER_CST)
2805 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2806 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2809 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2811 /* If SRC is not a pointer type, don't do this operation inline. */
2812 if (align == 0)
2813 return NULL_RTX;
2815 /* Bail out if we can't compute strlen in the right mode. */
2816 while (insn_mode != VOIDmode)
2818 icode = optab_handler (strlen_optab, insn_mode);
2819 if (icode != CODE_FOR_nothing)
2820 break;
2822 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2824 if (insn_mode == VOIDmode)
2825 return NULL_RTX;
2827 /* Make a place to hold the source address. We will not expand
2828 the actual source until we are sure that the expansion will
2829 not fail -- there are trees that cannot be expanded twice. */
2830 src_reg = gen_reg_rtx (Pmode);
2832 /* Mark the beginning of the strlen sequence so we can emit the
2833 source operand later. */
2834 before_strlen = get_last_insn ();
2836 create_output_operand (&ops[0], target, insn_mode);
2837 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2838 create_integer_operand (&ops[2], 0);
2839 create_integer_operand (&ops[3], align);
2840 if (!maybe_expand_insn (icode, 4, ops))
2841 return NULL_RTX;
2843 /* Now that we are assured of success, expand the source. */
2844 start_sequence ();
2845 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2846 if (pat != src_reg)
2848 #ifdef POINTERS_EXTEND_UNSIGNED
2849 if (GET_MODE (pat) != Pmode)
2850 pat = convert_to_mode (Pmode, pat,
2851 POINTERS_EXTEND_UNSIGNED);
2852 #endif
2853 emit_move_insn (src_reg, pat);
2855 pat = get_insns ();
2856 end_sequence ();
2858 if (before_strlen)
2859 emit_insn_after (pat, before_strlen);
2860 else
2861 emit_insn_before (pat, get_insns ());
2863 /* Return the value in the proper mode for this function. */
2864 if (GET_MODE (ops[0].value) == target_mode)
2865 target = ops[0].value;
2866 else if (target != 0)
2867 convert_move (target, ops[0].value, 0);
2868 else
2869 target = convert_to_mode (target_mode, ops[0].value, 0);
2871 return target;
2875 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2876 bytes from constant string DATA + OFFSET and return it as target
2877 constant. */
2879 static rtx
2880 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2881 machine_mode mode)
2883 const char *str = (const char *) data;
2885 gcc_assert (offset >= 0
2886 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2887 <= strlen (str) + 1));
2889 return c_readstr (str + offset, mode);
2892 /* LEN specify length of the block of memcpy/memset operation.
2893 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
2894 In some cases we can make very likely guess on max size, then we
2895 set it into PROBABLE_MAX_SIZE. */
2897 static void
2898 determine_block_size (tree len, rtx len_rtx,
2899 unsigned HOST_WIDE_INT *min_size,
2900 unsigned HOST_WIDE_INT *max_size,
2901 unsigned HOST_WIDE_INT *probable_max_size)
2903 if (CONST_INT_P (len_rtx))
2905 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
2906 return;
2908 else
2910 wide_int min, max;
2911 enum value_range_type range_type = VR_UNDEFINED;
2913 /* Determine bounds from the type. */
2914 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
2915 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
2916 else
2917 *min_size = 0;
2918 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
2919 *probable_max_size = *max_size
2920 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
2921 else
2922 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
2924 if (TREE_CODE (len) == SSA_NAME)
2925 range_type = get_range_info (len, &min, &max);
2926 if (range_type == VR_RANGE)
2928 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
2929 *min_size = min.to_uhwi ();
2930 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
2931 *probable_max_size = *max_size = max.to_uhwi ();
2933 else if (range_type == VR_ANTI_RANGE)
2935 /* Anti range 0...N lets us to determine minimal size to N+1. */
2936 if (min == 0)
2938 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
2939 *min_size = max.to_uhwi () + 1;
2941 /* Code like
2943 int n;
2944 if (n < 100)
2945 memcpy (a, b, n)
2947 Produce anti range allowing negative values of N. We still
2948 can use the information and make a guess that N is not negative.
2950 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
2951 *probable_max_size = min.to_uhwi () - 1;
2954 gcc_checking_assert (*max_size <=
2955 (unsigned HOST_WIDE_INT)
2956 GET_MODE_MASK (GET_MODE (len_rtx)));
2959 /* Helper function to do the actual work for expand_builtin_memcpy. */
2961 static rtx
2962 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
2964 const char *src_str;
2965 unsigned int src_align = get_pointer_alignment (src);
2966 unsigned int dest_align = get_pointer_alignment (dest);
2967 rtx dest_mem, src_mem, dest_addr, len_rtx;
2968 HOST_WIDE_INT expected_size = -1;
2969 unsigned int expected_align = 0;
2970 unsigned HOST_WIDE_INT min_size;
2971 unsigned HOST_WIDE_INT max_size;
2972 unsigned HOST_WIDE_INT probable_max_size;
2974 /* If DEST is not a pointer type, call the normal function. */
2975 if (dest_align == 0)
2976 return NULL_RTX;
2978 /* If either SRC is not a pointer type, don't do this
2979 operation in-line. */
2980 if (src_align == 0)
2981 return NULL_RTX;
2983 if (currently_expanding_gimple_stmt)
2984 stringop_block_profile (currently_expanding_gimple_stmt,
2985 &expected_align, &expected_size);
2987 if (expected_align < dest_align)
2988 expected_align = dest_align;
2989 dest_mem = get_memory_rtx (dest, len);
2990 set_mem_align (dest_mem, dest_align);
2991 len_rtx = expand_normal (len);
2992 determine_block_size (len, len_rtx, &min_size, &max_size,
2993 &probable_max_size);
2994 src_str = c_getstr (src);
2996 /* If SRC is a string constant and block move would be done
2997 by pieces, we can avoid loading the string from memory
2998 and only stored the computed constants. */
2999 if (src_str
3000 && CONST_INT_P (len_rtx)
3001 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3002 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3003 CONST_CAST (char *, src_str),
3004 dest_align, false))
3006 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3007 builtin_memcpy_read_str,
3008 CONST_CAST (char *, src_str),
3009 dest_align, false, 0);
3010 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3011 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3012 return dest_mem;
3015 src_mem = get_memory_rtx (src, len);
3016 set_mem_align (src_mem, src_align);
3018 /* Copy word part most expediently. */
3019 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3020 CALL_EXPR_TAILCALL (exp)
3021 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3022 expected_align, expected_size,
3023 min_size, max_size, probable_max_size);
3025 if (dest_addr == 0)
3027 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3028 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3031 return dest_addr;
3034 /* Fill the 2-element RANGE array with the minimum and maximum values
3035 EXP is known to have and return true, otherwise null and return
3036 false. */
3038 static bool
3039 get_size_range (tree exp, tree range[2])
3041 if (tree_fits_uhwi_p (exp))
3043 range[0] = range[1] = exp;
3044 return true;
3047 if (TREE_CODE (exp) == SSA_NAME)
3049 wide_int min, max;
3050 enum value_range_type range_type = get_range_info (exp, &min, &max);
3052 if (range_type == VR_RANGE)
3054 /* Interpret the bound in the variable's type. */
3055 range[0] = wide_int_to_tree (TREE_TYPE (exp), min);
3056 range[1] = wide_int_to_tree (TREE_TYPE (exp), max);
3057 return true;
3059 else if (range_type == VR_ANTI_RANGE)
3061 /* FIXME: Handle anti-ranges. */
3065 range[0] = NULL_TREE;
3066 range[1] = NULL_TREE;
3067 return false;
3070 /* Try to verify that the sizes and lengths of the arguments to a string
3071 manipulation function given by EXP are within valid bounds and that
3072 the operation does not lead to buffer overflow. Arguments other than
3073 EXP may be null. When non-null, the arguments have the following
3074 meaning:
3075 SIZE is the user-supplied size argument to the function (such as in
3076 memcpy(d, s, SIZE) or strncpy(d, s, SIZE). It specifies the exact
3077 number of bytes to write.
3078 MAXLEN is the user-supplied bound on the length of the source sequence
3079 (such as in strncat(d, s, N). It specifies the upper limit on the number
3080 of bytes to write.
3081 STR is the source string (such as in strcpy(d, s)) when the epxression
3082 EXP is a string function call (as opposed to a memory call like memcpy).
3083 As an exception, STR can also be an integer denoting the precomputed
3084 length of the source string.
3085 OBJSIZE is the size of the destination object specified by the last
3086 argument to the _chk builtins, typically resulting from the expansion
3087 of __builtin_object_size (such as in __builtin___strcpy_chk(d, s,
3088 OBJSIZE).
3090 When SIZE is null LEN is checked to verify that it doesn't exceed
3091 SIZE_MAX.
3093 If the call is successfully verified as safe from buffer overflow
3094 the function returns true, otherwise false.. */
3096 static bool
3097 check_sizes (int opt, tree exp, tree size, tree maxlen, tree str, tree objsize)
3099 /* The size of the largest object is half the address space, or
3100 SSIZE_MAX. (This is way too permissive.) */
3101 tree maxobjsize = TYPE_MAX_VALUE (ssizetype);
3103 tree slen = NULL_TREE;
3105 /* Set to true when the exact number of bytes written by a string
3106 function like strcpy is not known and the only thing that is
3107 known is that it must be at least one (for the terminating nul). */
3108 bool at_least_one = false;
3109 if (str)
3111 /* STR is normally a pointer to string but as a special case
3112 it can be an integer denoting the length of a string. */
3113 if (POINTER_TYPE_P (TREE_TYPE (str)))
3115 /* Try to determine the range of lengths the source string
3116 refers to. If it can be determined add one to it for
3117 the terminating nul. Otherwise, set it to one for
3118 the same reason. */
3119 tree lenrange[2];
3120 get_range_strlen (str, lenrange);
3121 if (lenrange[0])
3122 slen = fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3123 size_one_node);
3124 else
3126 at_least_one = true;
3127 slen = size_one_node;
3130 else
3131 slen = str;
3134 if (!size && !maxlen)
3136 /* When the only available piece of data is the object size
3137 there is nothing to do. */
3138 if (!slen)
3139 return true;
3141 /* Otherwise, when the length of the source sequence is known
3142 (as with with strlen), set SIZE to it. */
3143 size = slen;
3146 if (!objsize)
3147 objsize = maxobjsize;
3149 /* The SIZE is exact if it's non-null, constant, and in range of
3150 unsigned HOST_WIDE_INT. */
3151 bool exactsize = size && tree_fits_uhwi_p (size);
3153 tree range[2] = { NULL_TREE, NULL_TREE };
3154 if (size)
3155 get_size_range (size, range);
3157 /* First check the number of bytes to be written against the maximum
3158 object size. */
3159 if (range[0] && tree_int_cst_lt (maxobjsize, range[0]))
3161 location_t loc = tree_nonartificial_location (exp);
3163 if (range[0] == range[1])
3164 warning_at (loc, opt,
3165 "%K%qD: specified size %wu "
3166 "exceeds maximum object size %wu",
3167 exp, get_callee_fndecl (exp),
3168 tree_to_uhwi (range[0]),
3169 tree_to_uhwi (maxobjsize));
3170 else
3171 warning_at (loc, opt,
3172 "%K%qD: specified size between %wu and %wu "
3173 "exceeds maximum object size %wu",
3174 exp, get_callee_fndecl (exp),
3175 tree_to_uhwi (range[0]),
3176 tree_to_uhwi (range[1]),
3177 tree_to_uhwi (maxobjsize));
3178 return false;
3181 /* Next check the number of bytes to be written against the destination
3182 object size. */
3183 if (range[0] || !exactsize || integer_all_onesp (size))
3185 if (range[0]
3186 && ((tree_fits_uhwi_p (objsize)
3187 && tree_int_cst_lt (objsize, range[0]))
3188 || (tree_fits_uhwi_p (size)
3189 && tree_int_cst_lt (size, range[0]))))
3191 unsigned HOST_WIDE_INT uwir0 = tree_to_uhwi (range[0]);
3193 location_t loc = tree_nonartificial_location (exp);
3195 if (at_least_one)
3196 warning_at (loc, opt,
3197 "%K%qD: writing at least %wu byte into a region "
3198 "of size %wu overflows the destination",
3199 exp, get_callee_fndecl (exp), uwir0,
3200 tree_to_uhwi (objsize));
3201 else if (range[0] == range[1])
3202 warning_at (loc, opt,
3203 (uwir0 == 1
3204 ? G_("%K%qD: writing %wu byte into a region "
3205 "of size %wu overflows the destination")
3206 : G_("%K%qD writing %wu bytes into a region "
3207 "of size %wu overflows the destination")),
3208 exp, get_callee_fndecl (exp), uwir0,
3209 tree_to_uhwi (objsize));
3210 else
3211 warning_at (loc, opt,
3212 "%K%qD: writing between %wu and %wu bytes "
3213 "into a region of size %wu overflows "
3214 "the destination",
3215 exp, get_callee_fndecl (exp), uwir0,
3216 tree_to_uhwi (range[1]), tree_to_uhwi (objsize));
3218 /* Return error when an overflow has been detected. */
3219 return false;
3223 /* Check the maximum length of the source sequence against the size
3224 of the destination object if known, or against the maximum size
3225 of an object. */
3226 if (maxlen)
3228 get_size_range (maxlen, range);
3230 if (range[0] && objsize && tree_fits_uhwi_p (objsize))
3232 location_t loc = tree_nonartificial_location (exp);
3234 if (tree_int_cst_lt (maxobjsize, range[0]))
3236 /* Warn about crazy big sizes first since that's more
3237 likely to be meaningful than saying that the bound
3238 is greater than the object size if both are big. */
3239 if (range[0] == range[1])
3240 warning_at (loc, opt,
3241 "%K%qD: specified bound %wu "
3242 "exceeds maximum object size %wu",
3243 exp, get_callee_fndecl (exp),
3244 tree_to_uhwi (range[0]),
3245 tree_to_uhwi (maxobjsize));
3246 else
3247 warning_at (loc, opt,
3248 "%K%qD: specified bound between %wu and %wu "
3249 " exceeds maximum object size %wu",
3250 exp, get_callee_fndecl (exp),
3251 tree_to_uhwi (range[0]),
3252 tree_to_uhwi (range[1]),
3253 tree_to_uhwi (maxobjsize));
3255 return false;
3258 if (objsize != maxobjsize && tree_int_cst_lt (objsize, range[0]))
3260 if (range[0] == range[1])
3261 warning_at (loc, opt,
3262 "%K%qD: specified bound %wu "
3263 "exceeds the size %wu of the destination",
3264 exp, get_callee_fndecl (exp),
3265 tree_to_uhwi (range[0]),
3266 tree_to_uhwi (objsize));
3267 else
3268 warning_at (loc, opt,
3269 "%K%qD: specified bound between %wu and %wu "
3270 " exceeds the size %wu of the destination",
3271 exp, get_callee_fndecl (exp),
3272 tree_to_uhwi (range[0]),
3273 tree_to_uhwi (range[1]),
3274 tree_to_uhwi (objsize));
3275 return false;
3280 return true;
3283 /* Helper to compute the size of the object referenced by the DEST
3284 expression which must of of pointer type, using Object Size type
3285 OSTYPE (only the least significant 2 bits are used). Return
3286 the size of the object if successful or NULL when the size cannot
3287 be determined. */
3289 static inline tree
3290 compute_dest_size (tree dest, int ostype)
3292 unsigned HOST_WIDE_INT size;
3293 if (compute_builtin_object_size (dest, ostype & 3, &size))
3294 return build_int_cst (sizetype, size);
3296 return NULL_TREE;
3299 /* Helper to determine and check the sizes of the source and the destination
3300 of calls to __builtin_{bzero,memcpy,memset} calls. Use Object Size type-0
3301 regardless of the OPT_Wstringop_overflow_ setting. Returns true on success
3302 (no overflow or invalid sizes), false otherwise. */
3304 static bool
3305 check_memop_sizes (tree exp, tree dest, tree size)
3307 if (!warn_stringop_overflow)
3308 return true;
3310 /* For functions like memset and memcpy that operate on raw memory
3311 try to determine the size of the largest destination object using
3312 type-0 Object Size regardless of the object size type specified
3313 by the option. */
3314 tree objsize = compute_dest_size (dest, 0);
3316 return check_sizes (OPT_Wstringop_overflow_, exp,
3317 size, /*maxlen=*/NULL_TREE, /*str=*/NULL_TREE, objsize);
3320 /* Expand a call EXP to the memcpy builtin.
3321 Return NULL_RTX if we failed, the caller should emit a normal call,
3322 otherwise try to get the result in TARGET, if convenient (and in
3323 mode MODE if that's convenient). */
3325 static rtx
3326 expand_builtin_memcpy (tree exp, rtx target)
3328 if (!validate_arglist (exp,
3329 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3330 return NULL_RTX;
3332 tree dest = CALL_EXPR_ARG (exp, 0);
3333 tree src = CALL_EXPR_ARG (exp, 1);
3334 tree len = CALL_EXPR_ARG (exp, 2);
3336 check_memop_sizes (exp, dest, len);
3338 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3341 /* Expand an instrumented call EXP to the memcpy builtin.
3342 Return NULL_RTX if we failed, the caller should emit a normal call,
3343 otherwise try to get the result in TARGET, if convenient (and in
3344 mode MODE if that's convenient). */
3346 static rtx
3347 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3349 if (!validate_arglist (exp,
3350 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3351 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3352 INTEGER_TYPE, VOID_TYPE))
3353 return NULL_RTX;
3354 else
3356 tree dest = CALL_EXPR_ARG (exp, 0);
3357 tree src = CALL_EXPR_ARG (exp, 2);
3358 tree len = CALL_EXPR_ARG (exp, 4);
3359 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3361 /* Return src bounds with the result. */
3362 if (res)
3364 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3365 expand_normal (CALL_EXPR_ARG (exp, 1)));
3366 res = chkp_join_splitted_slot (res, bnd);
3368 return res;
3372 /* Expand a call EXP to the mempcpy builtin.
3373 Return NULL_RTX if we failed; the caller should emit a normal call,
3374 otherwise try to get the result in TARGET, if convenient (and in
3375 mode MODE if that's convenient). If ENDP is 0 return the
3376 destination pointer, if ENDP is 1 return the end pointer ala
3377 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3378 stpcpy. */
3380 static rtx
3381 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3383 if (!validate_arglist (exp,
3384 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3385 return NULL_RTX;
3387 tree dest = CALL_EXPR_ARG (exp, 0);
3388 tree src = CALL_EXPR_ARG (exp, 1);
3389 tree len = CALL_EXPR_ARG (exp, 2);
3391 /* Avoid expanding mempcpy into memcpy when the call is determined
3392 to overflow the buffer. This also prevents the same overflow
3393 from being diagnosed again when expanding memcpy. */
3394 if (!check_memop_sizes (exp, dest, len))
3395 return NULL_RTX;
3397 return expand_builtin_mempcpy_args (dest, src, len,
3398 target, mode, /*endp=*/ 1,
3399 exp);
3402 /* Expand an instrumented call EXP to the mempcpy builtin.
3403 Return NULL_RTX if we failed, the caller should emit a normal call,
3404 otherwise try to get the result in TARGET, if convenient (and in
3405 mode MODE if that's convenient). */
3407 static rtx
3408 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3410 if (!validate_arglist (exp,
3411 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3412 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3413 INTEGER_TYPE, VOID_TYPE))
3414 return NULL_RTX;
3415 else
3417 tree dest = CALL_EXPR_ARG (exp, 0);
3418 tree src = CALL_EXPR_ARG (exp, 2);
3419 tree len = CALL_EXPR_ARG (exp, 4);
3420 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3421 mode, 1, exp);
3423 /* Return src bounds with the result. */
3424 if (res)
3426 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3427 expand_normal (CALL_EXPR_ARG (exp, 1)));
3428 res = chkp_join_splitted_slot (res, bnd);
3430 return res;
3434 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3435 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3436 so that this can also be called without constructing an actual CALL_EXPR.
3437 The other arguments and return value are the same as for
3438 expand_builtin_mempcpy. */
3440 static rtx
3441 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3442 rtx target, machine_mode mode, int endp,
3443 tree orig_exp)
3445 tree fndecl = get_callee_fndecl (orig_exp);
3447 /* If return value is ignored, transform mempcpy into memcpy. */
3448 if (target == const0_rtx
3449 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3450 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3452 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3453 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3454 dest, src, len);
3455 return expand_expr (result, target, mode, EXPAND_NORMAL);
3457 else if (target == const0_rtx
3458 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3460 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3461 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3462 dest, src, len);
3463 return expand_expr (result, target, mode, EXPAND_NORMAL);
3465 else
3467 const char *src_str;
3468 unsigned int src_align = get_pointer_alignment (src);
3469 unsigned int dest_align = get_pointer_alignment (dest);
3470 rtx dest_mem, src_mem, len_rtx;
3472 /* If either SRC or DEST is not a pointer type, don't do this
3473 operation in-line. */
3474 if (dest_align == 0 || src_align == 0)
3475 return NULL_RTX;
3477 /* If LEN is not constant, call the normal function. */
3478 if (! tree_fits_uhwi_p (len))
3479 return NULL_RTX;
3481 len_rtx = expand_normal (len);
3482 src_str = c_getstr (src);
3484 /* If SRC is a string constant and block move would be done
3485 by pieces, we can avoid loading the string from memory
3486 and only stored the computed constants. */
3487 if (src_str
3488 && CONST_INT_P (len_rtx)
3489 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3490 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3491 CONST_CAST (char *, src_str),
3492 dest_align, false))
3494 dest_mem = get_memory_rtx (dest, len);
3495 set_mem_align (dest_mem, dest_align);
3496 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3497 builtin_memcpy_read_str,
3498 CONST_CAST (char *, src_str),
3499 dest_align, false, endp);
3500 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3501 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3502 return dest_mem;
3505 if (CONST_INT_P (len_rtx)
3506 && can_move_by_pieces (INTVAL (len_rtx),
3507 MIN (dest_align, src_align)))
3509 dest_mem = get_memory_rtx (dest, len);
3510 set_mem_align (dest_mem, dest_align);
3511 src_mem = get_memory_rtx (src, len);
3512 set_mem_align (src_mem, src_align);
3513 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3514 MIN (dest_align, src_align), endp);
3515 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3516 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3517 return dest_mem;
3520 return NULL_RTX;
3524 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3525 we failed, the caller should emit a normal call, otherwise try to
3526 get the result in TARGET, if convenient. If ENDP is 0 return the
3527 destination pointer, if ENDP is 1 return the end pointer ala
3528 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3529 stpcpy. */
3531 static rtx
3532 expand_movstr (tree dest, tree src, rtx target, int endp)
3534 struct expand_operand ops[3];
3535 rtx dest_mem;
3536 rtx src_mem;
3538 if (!targetm.have_movstr ())
3539 return NULL_RTX;
3541 dest_mem = get_memory_rtx (dest, NULL);
3542 src_mem = get_memory_rtx (src, NULL);
3543 if (!endp)
3545 target = force_reg (Pmode, XEXP (dest_mem, 0));
3546 dest_mem = replace_equiv_address (dest_mem, target);
3549 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3550 create_fixed_operand (&ops[1], dest_mem);
3551 create_fixed_operand (&ops[2], src_mem);
3552 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3553 return NULL_RTX;
3555 if (endp && target != const0_rtx)
3557 target = ops[0].value;
3558 /* movstr is supposed to set end to the address of the NUL
3559 terminator. If the caller requested a mempcpy-like return value,
3560 adjust it. */
3561 if (endp == 1)
3563 rtx tem = plus_constant (GET_MODE (target),
3564 gen_lowpart (GET_MODE (target), target), 1);
3565 emit_move_insn (target, force_operand (tem, NULL_RTX));
3568 return target;
3571 /* Do some very basic size validation of a call to the strcpy builtin
3572 given by EXP. Return NULL_RTX to have the built-in expand to a call
3573 to the library function. */
3575 static rtx
3576 expand_builtin_strcat (tree exp, rtx)
3578 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
3579 || !warn_stringop_overflow)
3580 return NULL_RTX;
3582 tree dest = CALL_EXPR_ARG (exp, 0);
3583 tree src = CALL_EXPR_ARG (exp, 1);
3585 /* There is no way here to determine the length of the string in
3586 the destination to which the SRC string is being appended so
3587 just diagnose cases when the souce string is longer than
3588 the destination object. */
3590 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3592 check_sizes (OPT_Wstringop_overflow_,
3593 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, src, destsize);
3595 return NULL_RTX;
3598 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3599 NULL_RTX if we failed the caller should emit a normal call, otherwise
3600 try to get the result in TARGET, if convenient (and in mode MODE if that's
3601 convenient). */
3603 static rtx
3604 expand_builtin_strcpy (tree exp, rtx target)
3606 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3607 return NULL_RTX;
3609 tree dest = CALL_EXPR_ARG (exp, 0);
3610 tree src = CALL_EXPR_ARG (exp, 1);
3612 if (warn_stringop_overflow)
3614 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3615 check_sizes (OPT_Wstringop_overflow_,
3616 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, src, destsize);
3619 return expand_builtin_strcpy_args (dest, src, target);
3622 /* Helper function to do the actual work for expand_builtin_strcpy. The
3623 arguments to the builtin_strcpy call DEST and SRC are broken out
3624 so that this can also be called without constructing an actual CALL_EXPR.
3625 The other arguments and return value are the same as for
3626 expand_builtin_strcpy. */
3628 static rtx
3629 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3631 return expand_movstr (dest, src, target, /*endp=*/0);
3634 /* Expand a call EXP to the stpcpy builtin.
3635 Return NULL_RTX if we failed the caller should emit a normal call,
3636 otherwise try to get the result in TARGET, if convenient (and in
3637 mode MODE if that's convenient). */
3639 static rtx
3640 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3642 tree dst, src;
3643 location_t loc = EXPR_LOCATION (exp);
3645 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3646 return NULL_RTX;
3648 dst = CALL_EXPR_ARG (exp, 0);
3649 src = CALL_EXPR_ARG (exp, 1);
3651 /* If return value is ignored, transform stpcpy into strcpy. */
3652 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3654 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3655 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3656 return expand_expr (result, target, mode, EXPAND_NORMAL);
3658 else
3660 tree len, lenp1;
3661 rtx ret;
3663 /* Ensure we get an actual string whose length can be evaluated at
3664 compile-time, not an expression containing a string. This is
3665 because the latter will potentially produce pessimized code
3666 when used to produce the return value. */
3667 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3668 return expand_movstr (dst, src, target, /*endp=*/2);
3670 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3671 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3672 target, mode, /*endp=*/2,
3673 exp);
3675 if (ret)
3676 return ret;
3678 if (TREE_CODE (len) == INTEGER_CST)
3680 rtx len_rtx = expand_normal (len);
3682 if (CONST_INT_P (len_rtx))
3684 ret = expand_builtin_strcpy_args (dst, src, target);
3686 if (ret)
3688 if (! target)
3690 if (mode != VOIDmode)
3691 target = gen_reg_rtx (mode);
3692 else
3693 target = gen_reg_rtx (GET_MODE (ret));
3695 if (GET_MODE (target) != GET_MODE (ret))
3696 ret = gen_lowpart (GET_MODE (target), ret);
3698 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3699 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3700 gcc_assert (ret);
3702 return target;
3707 return expand_movstr (dst, src, target, /*endp=*/2);
3711 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3712 bytes from constant string DATA + OFFSET and return it as target
3713 constant. */
3716 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3717 machine_mode mode)
3719 const char *str = (const char *) data;
3721 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3722 return const0_rtx;
3724 return c_readstr (str + offset, mode);
3727 /* Helper to check the sizes of sequences and the destination of calls
3728 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3729 success (no overflow or invalid sizes), false otherwise. */
3731 static bool
3732 check_strncat_sizes (tree exp, tree objsize)
3734 tree dest = CALL_EXPR_ARG (exp, 0);
3735 tree src = CALL_EXPR_ARG (exp, 1);
3736 tree maxlen = CALL_EXPR_ARG (exp, 2);
3738 /* Try to determine the range of lengths that the source expression
3739 refers to. */
3740 tree lenrange[2];
3741 get_range_strlen (src, lenrange);
3743 /* Try to verify that the destination is big enough for the shortest
3744 string. */
3746 if (!objsize && warn_stringop_overflow)
3748 /* If it hasn't been provided by __strncat_chk, try to determine
3749 the size of the destination object into which the source is
3750 being copied. */
3751 objsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3754 /* Add one for the terminating nul. */
3755 tree srclen = (lenrange[0]
3756 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3757 size_one_node)
3758 : NULL_TREE);
3760 /* Strncat copies at most MAXLEN bytes and always appends the terminating
3761 nul so the specified upper bound should never be equal to (or greater
3762 than) the size of the destination. */
3763 if (tree_fits_uhwi_p (maxlen) && tree_fits_uhwi_p (objsize)
3764 && tree_int_cst_equal (objsize, maxlen))
3766 warning_at (EXPR_LOCATION (exp), OPT_Wstringop_overflow_,
3767 "specified bound %wu "
3768 "equals the size of the destination",
3769 tree_to_uhwi (maxlen));
3771 return false;
3774 if (!srclen
3775 || (maxlen && tree_fits_uhwi_p (maxlen)
3776 && tree_fits_uhwi_p (srclen)
3777 && tree_int_cst_lt (maxlen, srclen)))
3778 srclen = maxlen;
3780 /* The number of bytes to write is LEN but check_sizes will also
3781 check SRCLEN if LEN's value isn't known. */
3782 return check_sizes (OPT_Wstringop_overflow_,
3783 exp, /*size=*/NULL_TREE, maxlen, srclen, objsize);
3786 /* Similar to expand_builtin_strcat, do some very basic size validation
3787 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
3788 the built-in expand to a call to the library function. */
3790 static rtx
3791 expand_builtin_strncat (tree exp, rtx)
3793 if (!validate_arglist (exp,
3794 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3795 || !warn_stringop_overflow)
3796 return NULL_RTX;
3798 tree dest = CALL_EXPR_ARG (exp, 0);
3799 tree src = CALL_EXPR_ARG (exp, 1);
3800 /* The upper bound on the number of bytes to write. */
3801 tree maxlen = CALL_EXPR_ARG (exp, 2);
3802 /* The length of the source sequence. */
3803 tree slen = c_strlen (src, 1);
3805 /* Try to determine the range of lengths that the source expression
3806 refers to. */
3807 tree lenrange[2];
3808 if (slen)
3809 lenrange[0] = lenrange[1] = slen;
3810 else
3811 get_range_strlen (src, lenrange);
3813 /* Try to verify that the destination is big enough for the shortest
3814 string. First try to determine the size of the destination object
3815 into which the source is being copied. */
3816 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3818 /* Add one for the terminating nul. */
3819 tree srclen = (lenrange[0]
3820 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3821 size_one_node)
3822 : NULL_TREE);
3824 /* Strncat copies at most MAXLEN bytes and always appends the terminating
3825 nul so the specified upper bound should never be equal to (or greater
3826 than) the size of the destination. */
3827 if (tree_fits_uhwi_p (maxlen) && tree_fits_uhwi_p (destsize)
3828 && tree_int_cst_equal (destsize, maxlen))
3830 warning_at (EXPR_LOCATION (exp), OPT_Wstringop_overflow_,
3831 "specified bound %wu "
3832 "equals the size of the destination",
3833 tree_to_uhwi (maxlen));
3835 return NULL_RTX;
3838 if (!srclen
3839 || (maxlen && tree_fits_uhwi_p (maxlen)
3840 && tree_fits_uhwi_p (srclen)
3841 && tree_int_cst_lt (maxlen, srclen)))
3842 srclen = maxlen;
3844 /* The number of bytes to write is LEN but check_sizes will also
3845 check SRCLEN if LEN's value isn't known. */
3846 check_sizes (OPT_Wstringop_overflow_,
3847 exp, /*size=*/NULL_TREE, maxlen, srclen, destsize);
3849 return NULL_RTX;
3852 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3853 NULL_RTX if we failed the caller should emit a normal call. */
3855 static rtx
3856 expand_builtin_strncpy (tree exp, rtx target)
3858 location_t loc = EXPR_LOCATION (exp);
3860 if (validate_arglist (exp,
3861 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3863 tree dest = CALL_EXPR_ARG (exp, 0);
3864 tree src = CALL_EXPR_ARG (exp, 1);
3865 /* The number of bytes to write (not the maximum). */
3866 tree len = CALL_EXPR_ARG (exp, 2);
3867 /* The length of the source sequence. */
3868 tree slen = c_strlen (src, 1);
3870 if (warn_stringop_overflow)
3872 /* Try to determine the range of lengths that the source expression
3873 refers to. */
3874 tree lenrange[2];
3875 if (slen)
3876 lenrange[0] = lenrange[1] = slen;
3877 else
3879 get_range_strlen (src, lenrange);
3880 slen = lenrange[0];
3883 tree destsize = compute_dest_size (dest,
3884 warn_stringop_overflow - 1);
3886 /* The number of bytes to write is LEN but check_sizes will also
3887 check SLEN if LEN's value isn't known. */
3888 check_sizes (OPT_Wstringop_overflow_,
3889 exp, len, /*maxlen=*/NULL_TREE, slen, destsize);
3892 /* We must be passed a constant len and src parameter. */
3893 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3894 return NULL_RTX;
3896 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3898 /* We're required to pad with trailing zeros if the requested
3899 len is greater than strlen(s2)+1. In that case try to
3900 use store_by_pieces, if it fails, punt. */
3901 if (tree_int_cst_lt (slen, len))
3903 unsigned int dest_align = get_pointer_alignment (dest);
3904 const char *p = c_getstr (src);
3905 rtx dest_mem;
3907 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3908 || !can_store_by_pieces (tree_to_uhwi (len),
3909 builtin_strncpy_read_str,
3910 CONST_CAST (char *, p),
3911 dest_align, false))
3912 return NULL_RTX;
3914 dest_mem = get_memory_rtx (dest, len);
3915 store_by_pieces (dest_mem, tree_to_uhwi (len),
3916 builtin_strncpy_read_str,
3917 CONST_CAST (char *, p), dest_align, false, 0);
3918 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3919 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3920 return dest_mem;
3923 return NULL_RTX;
3926 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3927 bytes from constant string DATA + OFFSET and return it as target
3928 constant. */
3931 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3932 machine_mode mode)
3934 const char *c = (const char *) data;
3935 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3937 memset (p, *c, GET_MODE_SIZE (mode));
3939 return c_readstr (p, mode);
3942 /* Callback routine for store_by_pieces. Return the RTL of a register
3943 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3944 char value given in the RTL register data. For example, if mode is
3945 4 bytes wide, return the RTL for 0x01010101*data. */
3947 static rtx
3948 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3949 machine_mode mode)
3951 rtx target, coeff;
3952 size_t size;
3953 char *p;
3955 size = GET_MODE_SIZE (mode);
3956 if (size == 1)
3957 return (rtx) data;
3959 p = XALLOCAVEC (char, size);
3960 memset (p, 1, size);
3961 coeff = c_readstr (p, mode);
3963 target = convert_to_mode (mode, (rtx) data, 1);
3964 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3965 return force_reg (mode, target);
3968 /* Expand expression EXP, which is a call to the memset builtin. Return
3969 NULL_RTX if we failed the caller should emit a normal call, otherwise
3970 try to get the result in TARGET, if convenient (and in mode MODE if that's
3971 convenient). */
3973 static rtx
3974 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3976 if (!validate_arglist (exp,
3977 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3978 return NULL_RTX;
3980 tree dest = CALL_EXPR_ARG (exp, 0);
3981 tree val = CALL_EXPR_ARG (exp, 1);
3982 tree len = CALL_EXPR_ARG (exp, 2);
3984 check_memop_sizes (exp, dest, len);
3986 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3989 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3990 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3991 try to get the result in TARGET, if convenient (and in mode MODE if that's
3992 convenient). */
3994 static rtx
3995 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3997 if (!validate_arglist (exp,
3998 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3999 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4000 return NULL_RTX;
4001 else
4003 tree dest = CALL_EXPR_ARG (exp, 0);
4004 tree val = CALL_EXPR_ARG (exp, 2);
4005 tree len = CALL_EXPR_ARG (exp, 3);
4006 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
4008 /* Return src bounds with the result. */
4009 if (res)
4011 rtx bnd = force_reg (targetm.chkp_bound_mode (),
4012 expand_normal (CALL_EXPR_ARG (exp, 1)));
4013 res = chkp_join_splitted_slot (res, bnd);
4015 return res;
4019 /* Helper function to do the actual work for expand_builtin_memset. The
4020 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4021 so that this can also be called without constructing an actual CALL_EXPR.
4022 The other arguments and return value are the same as for
4023 expand_builtin_memset. */
4025 static rtx
4026 expand_builtin_memset_args (tree dest, tree val, tree len,
4027 rtx target, machine_mode mode, tree orig_exp)
4029 tree fndecl, fn;
4030 enum built_in_function fcode;
4031 machine_mode val_mode;
4032 char c;
4033 unsigned int dest_align;
4034 rtx dest_mem, dest_addr, len_rtx;
4035 HOST_WIDE_INT expected_size = -1;
4036 unsigned int expected_align = 0;
4037 unsigned HOST_WIDE_INT min_size;
4038 unsigned HOST_WIDE_INT max_size;
4039 unsigned HOST_WIDE_INT probable_max_size;
4041 dest_align = get_pointer_alignment (dest);
4043 /* If DEST is not a pointer type, don't do this operation in-line. */
4044 if (dest_align == 0)
4045 return NULL_RTX;
4047 if (currently_expanding_gimple_stmt)
4048 stringop_block_profile (currently_expanding_gimple_stmt,
4049 &expected_align, &expected_size);
4051 if (expected_align < dest_align)
4052 expected_align = dest_align;
4054 /* If the LEN parameter is zero, return DEST. */
4055 if (integer_zerop (len))
4057 /* Evaluate and ignore VAL in case it has side-effects. */
4058 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4059 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4062 /* Stabilize the arguments in case we fail. */
4063 dest = builtin_save_expr (dest);
4064 val = builtin_save_expr (val);
4065 len = builtin_save_expr (len);
4067 len_rtx = expand_normal (len);
4068 determine_block_size (len, len_rtx, &min_size, &max_size,
4069 &probable_max_size);
4070 dest_mem = get_memory_rtx (dest, len);
4071 val_mode = TYPE_MODE (unsigned_char_type_node);
4073 if (TREE_CODE (val) != INTEGER_CST)
4075 rtx val_rtx;
4077 val_rtx = expand_normal (val);
4078 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4080 /* Assume that we can memset by pieces if we can store
4081 * the coefficients by pieces (in the required modes).
4082 * We can't pass builtin_memset_gen_str as that emits RTL. */
4083 c = 1;
4084 if (tree_fits_uhwi_p (len)
4085 && can_store_by_pieces (tree_to_uhwi (len),
4086 builtin_memset_read_str, &c, dest_align,
4087 true))
4089 val_rtx = force_reg (val_mode, val_rtx);
4090 store_by_pieces (dest_mem, tree_to_uhwi (len),
4091 builtin_memset_gen_str, val_rtx, dest_align,
4092 true, 0);
4094 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4095 dest_align, expected_align,
4096 expected_size, min_size, max_size,
4097 probable_max_size))
4098 goto do_libcall;
4100 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4101 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4102 return dest_mem;
4105 if (target_char_cast (val, &c))
4106 goto do_libcall;
4108 if (c)
4110 if (tree_fits_uhwi_p (len)
4111 && can_store_by_pieces (tree_to_uhwi (len),
4112 builtin_memset_read_str, &c, dest_align,
4113 true))
4114 store_by_pieces (dest_mem, tree_to_uhwi (len),
4115 builtin_memset_read_str, &c, dest_align, true, 0);
4116 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4117 gen_int_mode (c, val_mode),
4118 dest_align, expected_align,
4119 expected_size, min_size, max_size,
4120 probable_max_size))
4121 goto do_libcall;
4123 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4124 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4125 return dest_mem;
4128 set_mem_align (dest_mem, dest_align);
4129 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4130 CALL_EXPR_TAILCALL (orig_exp)
4131 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4132 expected_align, expected_size,
4133 min_size, max_size,
4134 probable_max_size);
4136 if (dest_addr == 0)
4138 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4139 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4142 return dest_addr;
4144 do_libcall:
4145 fndecl = get_callee_fndecl (orig_exp);
4146 fcode = DECL_FUNCTION_CODE (fndecl);
4147 if (fcode == BUILT_IN_MEMSET
4148 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
4149 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4150 dest, val, len);
4151 else if (fcode == BUILT_IN_BZERO)
4152 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4153 dest, len);
4154 else
4155 gcc_unreachable ();
4156 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4157 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4158 return expand_call (fn, target, target == const0_rtx);
4161 /* Expand expression EXP, which is a call to the bzero builtin. Return
4162 NULL_RTX if we failed the caller should emit a normal call. */
4164 static rtx
4165 expand_builtin_bzero (tree exp)
4167 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4168 return NULL_RTX;
4170 tree dest = CALL_EXPR_ARG (exp, 0);
4171 tree size = CALL_EXPR_ARG (exp, 1);
4173 check_memop_sizes (exp, dest, size);
4175 /* New argument list transforming bzero(ptr x, int y) to
4176 memset(ptr x, int 0, size_t y). This is done this way
4177 so that if it isn't expanded inline, we fallback to
4178 calling bzero instead of memset. */
4180 location_t loc = EXPR_LOCATION (exp);
4182 return expand_builtin_memset_args (dest, integer_zero_node,
4183 fold_convert_loc (loc,
4184 size_type_node, size),
4185 const0_rtx, VOIDmode, exp);
4188 /* Try to expand cmpstr operation ICODE with the given operands.
4189 Return the result rtx on success, otherwise return null. */
4191 static rtx
4192 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4193 HOST_WIDE_INT align)
4195 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4197 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4198 target = NULL_RTX;
4200 struct expand_operand ops[4];
4201 create_output_operand (&ops[0], target, insn_mode);
4202 create_fixed_operand (&ops[1], arg1_rtx);
4203 create_fixed_operand (&ops[2], arg2_rtx);
4204 create_integer_operand (&ops[3], align);
4205 if (maybe_expand_insn (icode, 4, ops))
4206 return ops[0].value;
4207 return NULL_RTX;
4210 /* Expand expression EXP, which is a call to the memcmp built-in function.
4211 Return NULL_RTX if we failed and the caller should emit a normal call,
4212 otherwise try to get the result in TARGET, if convenient.
4213 RESULT_EQ is true if we can relax the returned value to be either zero
4214 or nonzero, without caring about the sign. */
4216 static rtx
4217 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4219 if (!validate_arglist (exp,
4220 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4221 return NULL_RTX;
4223 tree arg1 = CALL_EXPR_ARG (exp, 0);
4224 tree arg2 = CALL_EXPR_ARG (exp, 1);
4225 tree len = CALL_EXPR_ARG (exp, 2);
4226 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4227 location_t loc = EXPR_LOCATION (exp);
4229 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4230 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4232 /* If we don't have POINTER_TYPE, call the function. */
4233 if (arg1_align == 0 || arg2_align == 0)
4234 return NULL_RTX;
4236 rtx arg1_rtx = get_memory_rtx (arg1, len);
4237 rtx arg2_rtx = get_memory_rtx (arg2, len);
4238 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4240 /* Set MEM_SIZE as appropriate. */
4241 if (CONST_INT_P (len_rtx))
4243 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4244 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4247 by_pieces_constfn constfn = NULL;
4249 const char *src_str = c_getstr (arg2);
4250 if (result_eq && src_str == NULL)
4252 src_str = c_getstr (arg1);
4253 if (src_str != NULL)
4254 std::swap (arg1_rtx, arg2_rtx);
4257 /* If SRC is a string constant and block move would be done
4258 by pieces, we can avoid loading the string from memory
4259 and only stored the computed constants. */
4260 if (src_str
4261 && CONST_INT_P (len_rtx)
4262 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1)
4263 constfn = builtin_memcpy_read_str;
4265 rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4266 TREE_TYPE (len), target,
4267 result_eq, constfn,
4268 CONST_CAST (char *, src_str));
4270 if (result)
4272 /* Return the value in the proper mode for this function. */
4273 if (GET_MODE (result) == mode)
4274 return result;
4276 if (target != 0)
4278 convert_move (target, result, 0);
4279 return target;
4282 return convert_to_mode (mode, result, 0);
4285 return NULL_RTX;
4288 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4289 if we failed the caller should emit a normal call, otherwise try to get
4290 the result in TARGET, if convenient. */
4292 static rtx
4293 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4295 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4296 return NULL_RTX;
4298 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4299 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4300 if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing)
4302 rtx arg1_rtx, arg2_rtx;
4303 tree fndecl, fn;
4304 tree arg1 = CALL_EXPR_ARG (exp, 0);
4305 tree arg2 = CALL_EXPR_ARG (exp, 1);
4306 rtx result = NULL_RTX;
4308 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4309 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4311 /* If we don't have POINTER_TYPE, call the function. */
4312 if (arg1_align == 0 || arg2_align == 0)
4313 return NULL_RTX;
4315 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4316 arg1 = builtin_save_expr (arg1);
4317 arg2 = builtin_save_expr (arg2);
4319 arg1_rtx = get_memory_rtx (arg1, NULL);
4320 arg2_rtx = get_memory_rtx (arg2, NULL);
4322 /* Try to call cmpstrsi. */
4323 if (cmpstr_icode != CODE_FOR_nothing)
4324 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4325 MIN (arg1_align, arg2_align));
4327 /* Try to determine at least one length and call cmpstrnsi. */
4328 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4330 tree len;
4331 rtx arg3_rtx;
4333 tree len1 = c_strlen (arg1, 1);
4334 tree len2 = c_strlen (arg2, 1);
4336 if (len1)
4337 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4338 if (len2)
4339 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4341 /* If we don't have a constant length for the first, use the length
4342 of the second, if we know it. We don't require a constant for
4343 this case; some cost analysis could be done if both are available
4344 but neither is constant. For now, assume they're equally cheap,
4345 unless one has side effects. If both strings have constant lengths,
4346 use the smaller. */
4348 if (!len1)
4349 len = len2;
4350 else if (!len2)
4351 len = len1;
4352 else if (TREE_SIDE_EFFECTS (len1))
4353 len = len2;
4354 else if (TREE_SIDE_EFFECTS (len2))
4355 len = len1;
4356 else if (TREE_CODE (len1) != INTEGER_CST)
4357 len = len2;
4358 else if (TREE_CODE (len2) != INTEGER_CST)
4359 len = len1;
4360 else if (tree_int_cst_lt (len1, len2))
4361 len = len1;
4362 else
4363 len = len2;
4365 /* If both arguments have side effects, we cannot optimize. */
4366 if (len && !TREE_SIDE_EFFECTS (len))
4368 arg3_rtx = expand_normal (len);
4369 result = expand_cmpstrn_or_cmpmem
4370 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4371 arg3_rtx, MIN (arg1_align, arg2_align));
4375 if (result)
4377 /* Return the value in the proper mode for this function. */
4378 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4379 if (GET_MODE (result) == mode)
4380 return result;
4381 if (target == 0)
4382 return convert_to_mode (mode, result, 0);
4383 convert_move (target, result, 0);
4384 return target;
4387 /* Expand the library call ourselves using a stabilized argument
4388 list to avoid re-evaluating the function's arguments twice. */
4389 fndecl = get_callee_fndecl (exp);
4390 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4391 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4392 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4393 return expand_call (fn, target, target == const0_rtx);
4395 return NULL_RTX;
4398 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4399 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4400 the result in TARGET, if convenient. */
4402 static rtx
4403 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4404 ATTRIBUTE_UNUSED machine_mode mode)
4406 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4408 if (!validate_arglist (exp,
4409 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4410 return NULL_RTX;
4412 /* If c_strlen can determine an expression for one of the string
4413 lengths, and it doesn't have side effects, then emit cmpstrnsi
4414 using length MIN(strlen(string)+1, arg3). */
4415 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4416 if (cmpstrn_icode != CODE_FOR_nothing)
4418 tree len, len1, len2, len3;
4419 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4420 rtx result;
4421 tree fndecl, fn;
4422 tree arg1 = CALL_EXPR_ARG (exp, 0);
4423 tree arg2 = CALL_EXPR_ARG (exp, 1);
4424 tree arg3 = CALL_EXPR_ARG (exp, 2);
4426 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4427 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4429 len1 = c_strlen (arg1, 1);
4430 len2 = c_strlen (arg2, 1);
4432 if (len1)
4433 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4434 if (len2)
4435 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4437 len3 = fold_convert_loc (loc, sizetype, arg3);
4439 /* If we don't have a constant length for the first, use the length
4440 of the second, if we know it. If neither string is constant length,
4441 use the given length argument. We don't require a constant for
4442 this case; some cost analysis could be done if both are available
4443 but neither is constant. For now, assume they're equally cheap,
4444 unless one has side effects. If both strings have constant lengths,
4445 use the smaller. */
4447 if (!len1 && !len2)
4448 len = len3;
4449 else if (!len1)
4450 len = len2;
4451 else if (!len2)
4452 len = len1;
4453 else if (TREE_SIDE_EFFECTS (len1))
4454 len = len2;
4455 else if (TREE_SIDE_EFFECTS (len2))
4456 len = len1;
4457 else if (TREE_CODE (len1) != INTEGER_CST)
4458 len = len2;
4459 else if (TREE_CODE (len2) != INTEGER_CST)
4460 len = len1;
4461 else if (tree_int_cst_lt (len1, len2))
4462 len = len1;
4463 else
4464 len = len2;
4466 /* If we are not using the given length, we must incorporate it here.
4467 The actual new length parameter will be MIN(len,arg3) in this case. */
4468 if (len != len3)
4469 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4470 arg1_rtx = get_memory_rtx (arg1, len);
4471 arg2_rtx = get_memory_rtx (arg2, len);
4472 arg3_rtx = expand_normal (len);
4473 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4474 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4475 MIN (arg1_align, arg2_align));
4476 if (result)
4478 /* Return the value in the proper mode for this function. */
4479 mode = TYPE_MODE (TREE_TYPE (exp));
4480 if (GET_MODE (result) == mode)
4481 return result;
4482 if (target == 0)
4483 return convert_to_mode (mode, result, 0);
4484 convert_move (target, result, 0);
4485 return target;
4488 /* Expand the library call ourselves using a stabilized argument
4489 list to avoid re-evaluating the function's arguments twice. */
4490 fndecl = get_callee_fndecl (exp);
4491 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4492 arg1, arg2, len);
4493 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4494 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4495 return expand_call (fn, target, target == const0_rtx);
4497 return NULL_RTX;
4500 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4501 if that's convenient. */
4504 expand_builtin_saveregs (void)
4506 rtx val;
4507 rtx_insn *seq;
4509 /* Don't do __builtin_saveregs more than once in a function.
4510 Save the result of the first call and reuse it. */
4511 if (saveregs_value != 0)
4512 return saveregs_value;
4514 /* When this function is called, it means that registers must be
4515 saved on entry to this function. So we migrate the call to the
4516 first insn of this function. */
4518 start_sequence ();
4520 /* Do whatever the machine needs done in this case. */
4521 val = targetm.calls.expand_builtin_saveregs ();
4523 seq = get_insns ();
4524 end_sequence ();
4526 saveregs_value = val;
4528 /* Put the insns after the NOTE that starts the function. If this
4529 is inside a start_sequence, make the outer-level insn chain current, so
4530 the code is placed at the start of the function. */
4531 push_topmost_sequence ();
4532 emit_insn_after (seq, entry_of_function ());
4533 pop_topmost_sequence ();
4535 return val;
4538 /* Expand a call to __builtin_next_arg. */
4540 static rtx
4541 expand_builtin_next_arg (void)
4543 /* Checking arguments is already done in fold_builtin_next_arg
4544 that must be called before this function. */
4545 return expand_binop (ptr_mode, add_optab,
4546 crtl->args.internal_arg_pointer,
4547 crtl->args.arg_offset_rtx,
4548 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4551 /* Make it easier for the backends by protecting the valist argument
4552 from multiple evaluations. */
4554 static tree
4555 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4557 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4559 /* The current way of determining the type of valist is completely
4560 bogus. We should have the information on the va builtin instead. */
4561 if (!vatype)
4562 vatype = targetm.fn_abi_va_list (cfun->decl);
4564 if (TREE_CODE (vatype) == ARRAY_TYPE)
4566 if (TREE_SIDE_EFFECTS (valist))
4567 valist = save_expr (valist);
4569 /* For this case, the backends will be expecting a pointer to
4570 vatype, but it's possible we've actually been given an array
4571 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4572 So fix it. */
4573 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4575 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4576 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4579 else
4581 tree pt = build_pointer_type (vatype);
4583 if (! needs_lvalue)
4585 if (! TREE_SIDE_EFFECTS (valist))
4586 return valist;
4588 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4589 TREE_SIDE_EFFECTS (valist) = 1;
4592 if (TREE_SIDE_EFFECTS (valist))
4593 valist = save_expr (valist);
4594 valist = fold_build2_loc (loc, MEM_REF,
4595 vatype, valist, build_int_cst (pt, 0));
4598 return valist;
4601 /* The "standard" definition of va_list is void*. */
4603 tree
4604 std_build_builtin_va_list (void)
4606 return ptr_type_node;
4609 /* The "standard" abi va_list is va_list_type_node. */
4611 tree
4612 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4614 return va_list_type_node;
4617 /* The "standard" type of va_list is va_list_type_node. */
4619 tree
4620 std_canonical_va_list_type (tree type)
4622 tree wtype, htype;
4624 wtype = va_list_type_node;
4625 htype = type;
4627 if (TREE_CODE (wtype) == ARRAY_TYPE)
4629 /* If va_list is an array type, the argument may have decayed
4630 to a pointer type, e.g. by being passed to another function.
4631 In that case, unwrap both types so that we can compare the
4632 underlying records. */
4633 if (TREE_CODE (htype) == ARRAY_TYPE
4634 || POINTER_TYPE_P (htype))
4636 wtype = TREE_TYPE (wtype);
4637 htype = TREE_TYPE (htype);
4640 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4641 return va_list_type_node;
4643 return NULL_TREE;
4646 /* The "standard" implementation of va_start: just assign `nextarg' to
4647 the variable. */
4649 void
4650 std_expand_builtin_va_start (tree valist, rtx nextarg)
4652 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4653 convert_move (va_r, nextarg, 0);
4655 /* We do not have any valid bounds for the pointer, so
4656 just store zero bounds for it. */
4657 if (chkp_function_instrumented_p (current_function_decl))
4658 chkp_expand_bounds_reset_for_mem (valist,
4659 make_tree (TREE_TYPE (valist),
4660 nextarg));
4663 /* Expand EXP, a call to __builtin_va_start. */
4665 static rtx
4666 expand_builtin_va_start (tree exp)
4668 rtx nextarg;
4669 tree valist;
4670 location_t loc = EXPR_LOCATION (exp);
4672 if (call_expr_nargs (exp) < 2)
4674 error_at (loc, "too few arguments to function %<va_start%>");
4675 return const0_rtx;
4678 if (fold_builtin_next_arg (exp, true))
4679 return const0_rtx;
4681 nextarg = expand_builtin_next_arg ();
4682 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4684 if (targetm.expand_builtin_va_start)
4685 targetm.expand_builtin_va_start (valist, nextarg);
4686 else
4687 std_expand_builtin_va_start (valist, nextarg);
4689 return const0_rtx;
4692 /* Expand EXP, a call to __builtin_va_end. */
4694 static rtx
4695 expand_builtin_va_end (tree exp)
4697 tree valist = CALL_EXPR_ARG (exp, 0);
4699 /* Evaluate for side effects, if needed. I hate macros that don't
4700 do that. */
4701 if (TREE_SIDE_EFFECTS (valist))
4702 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4704 return const0_rtx;
4707 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4708 builtin rather than just as an assignment in stdarg.h because of the
4709 nastiness of array-type va_list types. */
4711 static rtx
4712 expand_builtin_va_copy (tree exp)
4714 tree dst, src, t;
4715 location_t loc = EXPR_LOCATION (exp);
4717 dst = CALL_EXPR_ARG (exp, 0);
4718 src = CALL_EXPR_ARG (exp, 1);
4720 dst = stabilize_va_list_loc (loc, dst, 1);
4721 src = stabilize_va_list_loc (loc, src, 0);
4723 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4725 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4727 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4728 TREE_SIDE_EFFECTS (t) = 1;
4729 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4731 else
4733 rtx dstb, srcb, size;
4735 /* Evaluate to pointers. */
4736 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4737 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4738 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4739 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4741 dstb = convert_memory_address (Pmode, dstb);
4742 srcb = convert_memory_address (Pmode, srcb);
4744 /* "Dereference" to BLKmode memories. */
4745 dstb = gen_rtx_MEM (BLKmode, dstb);
4746 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4747 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4748 srcb = gen_rtx_MEM (BLKmode, srcb);
4749 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4750 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4752 /* Copy. */
4753 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4756 return const0_rtx;
4759 /* Expand a call to one of the builtin functions __builtin_frame_address or
4760 __builtin_return_address. */
4762 static rtx
4763 expand_builtin_frame_address (tree fndecl, tree exp)
4765 /* The argument must be a nonnegative integer constant.
4766 It counts the number of frames to scan up the stack.
4767 The value is either the frame pointer value or the return
4768 address saved in that frame. */
4769 if (call_expr_nargs (exp) == 0)
4770 /* Warning about missing arg was already issued. */
4771 return const0_rtx;
4772 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4774 error ("invalid argument to %qD", fndecl);
4775 return const0_rtx;
4777 else
4779 /* Number of frames to scan up the stack. */
4780 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4782 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4784 /* Some ports cannot access arbitrary stack frames. */
4785 if (tem == NULL)
4787 warning (0, "unsupported argument to %qD", fndecl);
4788 return const0_rtx;
4791 if (count)
4793 /* Warn since no effort is made to ensure that any frame
4794 beyond the current one exists or can be safely reached. */
4795 warning (OPT_Wframe_address, "calling %qD with "
4796 "a nonzero argument is unsafe", fndecl);
4799 /* For __builtin_frame_address, return what we've got. */
4800 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4801 return tem;
4803 if (!REG_P (tem)
4804 && ! CONSTANT_P (tem))
4805 tem = copy_addr_to_reg (tem);
4806 return tem;
4810 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4811 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4812 is the same as for allocate_dynamic_stack_space. */
4814 static rtx
4815 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4817 rtx op0;
4818 rtx result;
4819 unsigned int align;
4820 tree fndecl = get_callee_fndecl (exp);
4821 bool alloca_with_align = (DECL_FUNCTION_CODE (fndecl)
4822 == BUILT_IN_ALLOCA_WITH_ALIGN);
4824 bool valid_arglist
4825 = (alloca_with_align
4826 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4827 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4829 if (!valid_arglist)
4830 return NULL_RTX;
4832 if ((alloca_with_align && !warn_vla_limit)
4833 || (!alloca_with_align && !warn_alloca_limit))
4835 /* -Walloca-larger-than and -Wvla-larger-than settings override
4836 the more general -Walloc-size-larger-than so unless either of
4837 the former options is specified check the alloca arguments for
4838 overflow. */
4839 tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE };
4840 int idx[] = { 0, -1 };
4841 maybe_warn_alloc_args_overflow (fndecl, exp, args, idx);
4844 /* Compute the argument. */
4845 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4847 /* Compute the alignment. */
4848 align = (alloca_with_align
4849 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4850 : BIGGEST_ALIGNMENT);
4852 /* Allocate the desired space. */
4853 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4854 result = convert_memory_address (ptr_mode, result);
4856 return result;
4859 /* Expand a call to bswap builtin in EXP.
4860 Return NULL_RTX if a normal call should be emitted rather than expanding the
4861 function in-line. If convenient, the result should be placed in TARGET.
4862 SUBTARGET may be used as the target for computing one of EXP's operands. */
4864 static rtx
4865 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4866 rtx subtarget)
4868 tree arg;
4869 rtx op0;
4871 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4872 return NULL_RTX;
4874 arg = CALL_EXPR_ARG (exp, 0);
4875 op0 = expand_expr (arg,
4876 subtarget && GET_MODE (subtarget) == target_mode
4877 ? subtarget : NULL_RTX,
4878 target_mode, EXPAND_NORMAL);
4879 if (GET_MODE (op0) != target_mode)
4880 op0 = convert_to_mode (target_mode, op0, 1);
4882 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4884 gcc_assert (target);
4886 return convert_to_mode (target_mode, target, 1);
4889 /* Expand a call to a unary builtin in EXP.
4890 Return NULL_RTX if a normal call should be emitted rather than expanding the
4891 function in-line. If convenient, the result should be placed in TARGET.
4892 SUBTARGET may be used as the target for computing one of EXP's operands. */
4894 static rtx
4895 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4896 rtx subtarget, optab op_optab)
4898 rtx op0;
4900 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4901 return NULL_RTX;
4903 /* Compute the argument. */
4904 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4905 (subtarget
4906 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4907 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4908 VOIDmode, EXPAND_NORMAL);
4909 /* Compute op, into TARGET if possible.
4910 Set TARGET to wherever the result comes back. */
4911 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4912 op_optab, op0, target, op_optab != clrsb_optab);
4913 gcc_assert (target);
4915 return convert_to_mode (target_mode, target, 0);
4918 /* Expand a call to __builtin_expect. We just return our argument
4919 as the builtin_expect semantic should've been already executed by
4920 tree branch prediction pass. */
4922 static rtx
4923 expand_builtin_expect (tree exp, rtx target)
4925 tree arg;
4927 if (call_expr_nargs (exp) < 2)
4928 return const0_rtx;
4929 arg = CALL_EXPR_ARG (exp, 0);
4931 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4932 /* When guessing was done, the hints should be already stripped away. */
4933 gcc_assert (!flag_guess_branch_prob
4934 || optimize == 0 || seen_error ());
4935 return target;
4938 /* Expand a call to __builtin_assume_aligned. We just return our first
4939 argument as the builtin_assume_aligned semantic should've been already
4940 executed by CCP. */
4942 static rtx
4943 expand_builtin_assume_aligned (tree exp, rtx target)
4945 if (call_expr_nargs (exp) < 2)
4946 return const0_rtx;
4947 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4948 EXPAND_NORMAL);
4949 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4950 && (call_expr_nargs (exp) < 3
4951 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4952 return target;
4955 void
4956 expand_builtin_trap (void)
4958 if (targetm.have_trap ())
4960 rtx_insn *insn = emit_insn (targetm.gen_trap ());
4961 /* For trap insns when not accumulating outgoing args force
4962 REG_ARGS_SIZE note to prevent crossjumping of calls with
4963 different args sizes. */
4964 if (!ACCUMULATE_OUTGOING_ARGS)
4965 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4967 else
4969 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
4970 tree call_expr = build_call_expr (fn, 0);
4971 expand_call (call_expr, NULL_RTX, false);
4974 emit_barrier ();
4977 /* Expand a call to __builtin_unreachable. We do nothing except emit
4978 a barrier saying that control flow will not pass here.
4980 It is the responsibility of the program being compiled to ensure
4981 that control flow does never reach __builtin_unreachable. */
4982 static void
4983 expand_builtin_unreachable (void)
4985 emit_barrier ();
4988 /* Expand EXP, a call to fabs, fabsf or fabsl.
4989 Return NULL_RTX if a normal call should be emitted rather than expanding
4990 the function inline. If convenient, the result should be placed
4991 in TARGET. SUBTARGET may be used as the target for computing
4992 the operand. */
4994 static rtx
4995 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4997 machine_mode mode;
4998 tree arg;
4999 rtx op0;
5001 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5002 return NULL_RTX;
5004 arg = CALL_EXPR_ARG (exp, 0);
5005 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5006 mode = TYPE_MODE (TREE_TYPE (arg));
5007 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5008 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5011 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5012 Return NULL is a normal call should be emitted rather than expanding the
5013 function inline. If convenient, the result should be placed in TARGET.
5014 SUBTARGET may be used as the target for computing the operand. */
5016 static rtx
5017 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5019 rtx op0, op1;
5020 tree arg;
5022 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5023 return NULL_RTX;
5025 arg = CALL_EXPR_ARG (exp, 0);
5026 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5028 arg = CALL_EXPR_ARG (exp, 1);
5029 op1 = expand_normal (arg);
5031 return expand_copysign (op0, op1, target);
5034 /* Expand a call to __builtin___clear_cache. */
5036 static rtx
5037 expand_builtin___clear_cache (tree exp)
5039 if (!targetm.code_for_clear_cache)
5041 #ifdef CLEAR_INSN_CACHE
5042 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5043 does something. Just do the default expansion to a call to
5044 __clear_cache(). */
5045 return NULL_RTX;
5046 #else
5047 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5048 does nothing. There is no need to call it. Do nothing. */
5049 return const0_rtx;
5050 #endif /* CLEAR_INSN_CACHE */
5053 /* We have a "clear_cache" insn, and it will handle everything. */
5054 tree begin, end;
5055 rtx begin_rtx, end_rtx;
5057 /* We must not expand to a library call. If we did, any
5058 fallback library function in libgcc that might contain a call to
5059 __builtin___clear_cache() would recurse infinitely. */
5060 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5062 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5063 return const0_rtx;
5066 if (targetm.have_clear_cache ())
5068 struct expand_operand ops[2];
5070 begin = CALL_EXPR_ARG (exp, 0);
5071 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5073 end = CALL_EXPR_ARG (exp, 1);
5074 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5076 create_address_operand (&ops[0], begin_rtx);
5077 create_address_operand (&ops[1], end_rtx);
5078 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5079 return const0_rtx;
5081 return const0_rtx;
5084 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5086 static rtx
5087 round_trampoline_addr (rtx tramp)
5089 rtx temp, addend, mask;
5091 /* If we don't need too much alignment, we'll have been guaranteed
5092 proper alignment by get_trampoline_type. */
5093 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5094 return tramp;
5096 /* Round address up to desired boundary. */
5097 temp = gen_reg_rtx (Pmode);
5098 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5099 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5101 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5102 temp, 0, OPTAB_LIB_WIDEN);
5103 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5104 temp, 0, OPTAB_LIB_WIDEN);
5106 return tramp;
5109 static rtx
5110 expand_builtin_init_trampoline (tree exp, bool onstack)
5112 tree t_tramp, t_func, t_chain;
5113 rtx m_tramp, r_tramp, r_chain, tmp;
5115 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5116 POINTER_TYPE, VOID_TYPE))
5117 return NULL_RTX;
5119 t_tramp = CALL_EXPR_ARG (exp, 0);
5120 t_func = CALL_EXPR_ARG (exp, 1);
5121 t_chain = CALL_EXPR_ARG (exp, 2);
5123 r_tramp = expand_normal (t_tramp);
5124 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5125 MEM_NOTRAP_P (m_tramp) = 1;
5127 /* If ONSTACK, the TRAMP argument should be the address of a field
5128 within the local function's FRAME decl. Either way, let's see if
5129 we can fill in the MEM_ATTRs for this memory. */
5130 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5131 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5133 /* Creator of a heap trampoline is responsible for making sure the
5134 address is aligned to at least STACK_BOUNDARY. Normally malloc
5135 will ensure this anyhow. */
5136 tmp = round_trampoline_addr (r_tramp);
5137 if (tmp != r_tramp)
5139 m_tramp = change_address (m_tramp, BLKmode, tmp);
5140 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5141 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5144 /* The FUNC argument should be the address of the nested function.
5145 Extract the actual function decl to pass to the hook. */
5146 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5147 t_func = TREE_OPERAND (t_func, 0);
5148 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5150 r_chain = expand_normal (t_chain);
5152 /* Generate insns to initialize the trampoline. */
5153 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5155 if (onstack)
5157 trampolines_created = 1;
5159 if (targetm.calls.custom_function_descriptors != 0)
5160 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5161 "trampoline generated for nested function %qD", t_func);
5164 return const0_rtx;
5167 static rtx
5168 expand_builtin_adjust_trampoline (tree exp)
5170 rtx tramp;
5172 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5173 return NULL_RTX;
5175 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5176 tramp = round_trampoline_addr (tramp);
5177 if (targetm.calls.trampoline_adjust_address)
5178 tramp = targetm.calls.trampoline_adjust_address (tramp);
5180 return tramp;
5183 /* Expand a call to the builtin descriptor initialization routine.
5184 A descriptor is made up of a couple of pointers to the static
5185 chain and the code entry in this order. */
5187 static rtx
5188 expand_builtin_init_descriptor (tree exp)
5190 tree t_descr, t_func, t_chain;
5191 rtx m_descr, r_descr, r_func, r_chain;
5193 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5194 VOID_TYPE))
5195 return NULL_RTX;
5197 t_descr = CALL_EXPR_ARG (exp, 0);
5198 t_func = CALL_EXPR_ARG (exp, 1);
5199 t_chain = CALL_EXPR_ARG (exp, 2);
5201 r_descr = expand_normal (t_descr);
5202 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5203 MEM_NOTRAP_P (m_descr) = 1;
5205 r_func = expand_normal (t_func);
5206 r_chain = expand_normal (t_chain);
5208 /* Generate insns to initialize the descriptor. */
5209 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5210 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5211 POINTER_SIZE / BITS_PER_UNIT), r_func);
5213 return const0_rtx;
5216 /* Expand a call to the builtin descriptor adjustment routine. */
5218 static rtx
5219 expand_builtin_adjust_descriptor (tree exp)
5221 rtx tramp;
5223 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5224 return NULL_RTX;
5226 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5228 /* Unalign the descriptor to allow runtime identification. */
5229 tramp = plus_constant (ptr_mode, tramp,
5230 targetm.calls.custom_function_descriptors);
5232 return force_operand (tramp, NULL_RTX);
5235 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5236 function. The function first checks whether the back end provides
5237 an insn to implement signbit for the respective mode. If not, it
5238 checks whether the floating point format of the value is such that
5239 the sign bit can be extracted. If that is not the case, error out.
5240 EXP is the expression that is a call to the builtin function; if
5241 convenient, the result should be placed in TARGET. */
5242 static rtx
5243 expand_builtin_signbit (tree exp, rtx target)
5245 const struct real_format *fmt;
5246 machine_mode fmode, imode, rmode;
5247 tree arg;
5248 int word, bitpos;
5249 enum insn_code icode;
5250 rtx temp;
5251 location_t loc = EXPR_LOCATION (exp);
5253 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5254 return NULL_RTX;
5256 arg = CALL_EXPR_ARG (exp, 0);
5257 fmode = TYPE_MODE (TREE_TYPE (arg));
5258 rmode = TYPE_MODE (TREE_TYPE (exp));
5259 fmt = REAL_MODE_FORMAT (fmode);
5261 arg = builtin_save_expr (arg);
5263 /* Expand the argument yielding a RTX expression. */
5264 temp = expand_normal (arg);
5266 /* Check if the back end provides an insn that handles signbit for the
5267 argument's mode. */
5268 icode = optab_handler (signbit_optab, fmode);
5269 if (icode != CODE_FOR_nothing)
5271 rtx_insn *last = get_last_insn ();
5272 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5273 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5274 return target;
5275 delete_insns_since (last);
5278 /* For floating point formats without a sign bit, implement signbit
5279 as "ARG < 0.0". */
5280 bitpos = fmt->signbit_ro;
5281 if (bitpos < 0)
5283 /* But we can't do this if the format supports signed zero. */
5284 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5286 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5287 build_real (TREE_TYPE (arg), dconst0));
5288 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5291 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5293 imode = int_mode_for_mode (fmode);
5294 gcc_assert (imode != BLKmode);
5295 temp = gen_lowpart (imode, temp);
5297 else
5299 imode = word_mode;
5300 /* Handle targets with different FP word orders. */
5301 if (FLOAT_WORDS_BIG_ENDIAN)
5302 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5303 else
5304 word = bitpos / BITS_PER_WORD;
5305 temp = operand_subword_force (temp, word, fmode);
5306 bitpos = bitpos % BITS_PER_WORD;
5309 /* Force the intermediate word_mode (or narrower) result into a
5310 register. This avoids attempting to create paradoxical SUBREGs
5311 of floating point modes below. */
5312 temp = force_reg (imode, temp);
5314 /* If the bitpos is within the "result mode" lowpart, the operation
5315 can be implement with a single bitwise AND. Otherwise, we need
5316 a right shift and an AND. */
5318 if (bitpos < GET_MODE_BITSIZE (rmode))
5320 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5322 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5323 temp = gen_lowpart (rmode, temp);
5324 temp = expand_binop (rmode, and_optab, temp,
5325 immed_wide_int_const (mask, rmode),
5326 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5328 else
5330 /* Perform a logical right shift to place the signbit in the least
5331 significant bit, then truncate the result to the desired mode
5332 and mask just this bit. */
5333 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5334 temp = gen_lowpart (rmode, temp);
5335 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5336 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5339 return temp;
5342 /* Expand fork or exec calls. TARGET is the desired target of the
5343 call. EXP is the call. FN is the
5344 identificator of the actual function. IGNORE is nonzero if the
5345 value is to be ignored. */
5347 static rtx
5348 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5350 tree id, decl;
5351 tree call;
5353 /* If we are not profiling, just call the function. */
5354 if (!profile_arc_flag)
5355 return NULL_RTX;
5357 /* Otherwise call the wrapper. This should be equivalent for the rest of
5358 compiler, so the code does not diverge, and the wrapper may run the
5359 code necessary for keeping the profiling sane. */
5361 switch (DECL_FUNCTION_CODE (fn))
5363 case BUILT_IN_FORK:
5364 id = get_identifier ("__gcov_fork");
5365 break;
5367 case BUILT_IN_EXECL:
5368 id = get_identifier ("__gcov_execl");
5369 break;
5371 case BUILT_IN_EXECV:
5372 id = get_identifier ("__gcov_execv");
5373 break;
5375 case BUILT_IN_EXECLP:
5376 id = get_identifier ("__gcov_execlp");
5377 break;
5379 case BUILT_IN_EXECLE:
5380 id = get_identifier ("__gcov_execle");
5381 break;
5383 case BUILT_IN_EXECVP:
5384 id = get_identifier ("__gcov_execvp");
5385 break;
5387 case BUILT_IN_EXECVE:
5388 id = get_identifier ("__gcov_execve");
5389 break;
5391 default:
5392 gcc_unreachable ();
5395 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5396 FUNCTION_DECL, id, TREE_TYPE (fn));
5397 DECL_EXTERNAL (decl) = 1;
5398 TREE_PUBLIC (decl) = 1;
5399 DECL_ARTIFICIAL (decl) = 1;
5400 TREE_NOTHROW (decl) = 1;
5401 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5402 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5403 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5404 return expand_call (call, target, ignore);
5409 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5410 the pointer in these functions is void*, the tree optimizers may remove
5411 casts. The mode computed in expand_builtin isn't reliable either, due
5412 to __sync_bool_compare_and_swap.
5414 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5415 group of builtins. This gives us log2 of the mode size. */
5417 static inline machine_mode
5418 get_builtin_sync_mode (int fcode_diff)
5420 /* The size is not negotiable, so ask not to get BLKmode in return
5421 if the target indicates that a smaller size would be better. */
5422 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5425 /* Expand the memory expression LOC and return the appropriate memory operand
5426 for the builtin_sync operations. */
5428 static rtx
5429 get_builtin_sync_mem (tree loc, machine_mode mode)
5431 rtx addr, mem;
5433 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5434 addr = convert_memory_address (Pmode, addr);
5436 /* Note that we explicitly do not want any alias information for this
5437 memory, so that we kill all other live memories. Otherwise we don't
5438 satisfy the full barrier semantics of the intrinsic. */
5439 mem = validize_mem (gen_rtx_MEM (mode, addr));
5441 /* The alignment needs to be at least according to that of the mode. */
5442 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5443 get_pointer_alignment (loc)));
5444 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5445 MEM_VOLATILE_P (mem) = 1;
5447 return mem;
5450 /* Make sure an argument is in the right mode.
5451 EXP is the tree argument.
5452 MODE is the mode it should be in. */
5454 static rtx
5455 expand_expr_force_mode (tree exp, machine_mode mode)
5457 rtx val;
5458 machine_mode old_mode;
5460 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5461 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5462 of CONST_INTs, where we know the old_mode only from the call argument. */
5464 old_mode = GET_MODE (val);
5465 if (old_mode == VOIDmode)
5466 old_mode = TYPE_MODE (TREE_TYPE (exp));
5467 val = convert_modes (mode, old_mode, val, 1);
5468 return val;
5472 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5473 EXP is the CALL_EXPR. CODE is the rtx code
5474 that corresponds to the arithmetic or logical operation from the name;
5475 an exception here is that NOT actually means NAND. TARGET is an optional
5476 place for us to store the results; AFTER is true if this is the
5477 fetch_and_xxx form. */
5479 static rtx
5480 expand_builtin_sync_operation (machine_mode mode, tree exp,
5481 enum rtx_code code, bool after,
5482 rtx target)
5484 rtx val, mem;
5485 location_t loc = EXPR_LOCATION (exp);
5487 if (code == NOT && warn_sync_nand)
5489 tree fndecl = get_callee_fndecl (exp);
5490 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5492 static bool warned_f_a_n, warned_n_a_f;
5494 switch (fcode)
5496 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5497 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5498 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5499 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5500 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5501 if (warned_f_a_n)
5502 break;
5504 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5505 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5506 warned_f_a_n = true;
5507 break;
5509 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5510 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5511 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5512 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5513 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5514 if (warned_n_a_f)
5515 break;
5517 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5518 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5519 warned_n_a_f = true;
5520 break;
5522 default:
5523 gcc_unreachable ();
5527 /* Expand the operands. */
5528 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5529 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5531 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5532 after);
5535 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5536 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5537 true if this is the boolean form. TARGET is a place for us to store the
5538 results; this is NOT optional if IS_BOOL is true. */
5540 static rtx
5541 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5542 bool is_bool, rtx target)
5544 rtx old_val, new_val, mem;
5545 rtx *pbool, *poval;
5547 /* Expand the operands. */
5548 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5549 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5550 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5552 pbool = poval = NULL;
5553 if (target != const0_rtx)
5555 if (is_bool)
5556 pbool = &target;
5557 else
5558 poval = &target;
5560 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5561 false, MEMMODEL_SYNC_SEQ_CST,
5562 MEMMODEL_SYNC_SEQ_CST))
5563 return NULL_RTX;
5565 return target;
5568 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5569 general form is actually an atomic exchange, and some targets only
5570 support a reduced form with the second argument being a constant 1.
5571 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5572 the results. */
5574 static rtx
5575 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5576 rtx target)
5578 rtx val, mem;
5580 /* Expand the operands. */
5581 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5582 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5584 return expand_sync_lock_test_and_set (target, mem, val);
5587 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5589 static void
5590 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5592 rtx mem;
5594 /* Expand the operands. */
5595 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5597 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5600 /* Given an integer representing an ``enum memmodel'', verify its
5601 correctness and return the memory model enum. */
5603 static enum memmodel
5604 get_memmodel (tree exp)
5606 rtx op;
5607 unsigned HOST_WIDE_INT val;
5608 source_location loc
5609 = expansion_point_location_if_in_system_header (input_location);
5611 /* If the parameter is not a constant, it's a run time value so we'll just
5612 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5613 if (TREE_CODE (exp) != INTEGER_CST)
5614 return MEMMODEL_SEQ_CST;
5616 op = expand_normal (exp);
5618 val = INTVAL (op);
5619 if (targetm.memmodel_check)
5620 val = targetm.memmodel_check (val);
5621 else if (val & ~MEMMODEL_MASK)
5623 warning_at (loc, OPT_Winvalid_memory_model,
5624 "unknown architecture specifier in memory model to builtin");
5625 return MEMMODEL_SEQ_CST;
5628 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5629 if (memmodel_base (val) >= MEMMODEL_LAST)
5631 warning_at (loc, OPT_Winvalid_memory_model,
5632 "invalid memory model argument to builtin");
5633 return MEMMODEL_SEQ_CST;
5636 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5637 be conservative and promote consume to acquire. */
5638 if (val == MEMMODEL_CONSUME)
5639 val = MEMMODEL_ACQUIRE;
5641 return (enum memmodel) val;
5644 /* Expand the __atomic_exchange intrinsic:
5645 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5646 EXP is the CALL_EXPR.
5647 TARGET is an optional place for us to store the results. */
5649 static rtx
5650 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5652 rtx val, mem;
5653 enum memmodel model;
5655 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5657 if (!flag_inline_atomics)
5658 return NULL_RTX;
5660 /* Expand the operands. */
5661 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5662 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5664 return expand_atomic_exchange (target, mem, val, model);
5667 /* Expand the __atomic_compare_exchange intrinsic:
5668 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5669 TYPE desired, BOOL weak,
5670 enum memmodel success,
5671 enum memmodel failure)
5672 EXP is the CALL_EXPR.
5673 TARGET is an optional place for us to store the results. */
5675 static rtx
5676 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5677 rtx target)
5679 rtx expect, desired, mem, oldval;
5680 rtx_code_label *label;
5681 enum memmodel success, failure;
5682 tree weak;
5683 bool is_weak;
5684 source_location loc
5685 = expansion_point_location_if_in_system_header (input_location);
5687 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5688 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5690 if (failure > success)
5692 warning_at (loc, OPT_Winvalid_memory_model,
5693 "failure memory model cannot be stronger than success "
5694 "memory model for %<__atomic_compare_exchange%>");
5695 success = MEMMODEL_SEQ_CST;
5698 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5700 warning_at (loc, OPT_Winvalid_memory_model,
5701 "invalid failure memory model for "
5702 "%<__atomic_compare_exchange%>");
5703 failure = MEMMODEL_SEQ_CST;
5704 success = MEMMODEL_SEQ_CST;
5708 if (!flag_inline_atomics)
5709 return NULL_RTX;
5711 /* Expand the operands. */
5712 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5714 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5715 expect = convert_memory_address (Pmode, expect);
5716 expect = gen_rtx_MEM (mode, expect);
5717 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5719 weak = CALL_EXPR_ARG (exp, 3);
5720 is_weak = false;
5721 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5722 is_weak = true;
5724 if (target == const0_rtx)
5725 target = NULL;
5727 /* Lest the rtl backend create a race condition with an imporoper store
5728 to memory, always create a new pseudo for OLDVAL. */
5729 oldval = NULL;
5731 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5732 is_weak, success, failure))
5733 return NULL_RTX;
5735 /* Conditionally store back to EXPECT, lest we create a race condition
5736 with an improper store to memory. */
5737 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5738 the normal case where EXPECT is totally private, i.e. a register. At
5739 which point the store can be unconditional. */
5740 label = gen_label_rtx ();
5741 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5742 GET_MODE (target), 1, label);
5743 emit_move_insn (expect, oldval);
5744 emit_label (label);
5746 return target;
5749 /* Helper function for expand_ifn_atomic_compare_exchange - expand
5750 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
5751 call. The weak parameter must be dropped to match the expected parameter
5752 list and the expected argument changed from value to pointer to memory
5753 slot. */
5755 static void
5756 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
5758 unsigned int z;
5759 vec<tree, va_gc> *vec;
5761 vec_alloc (vec, 5);
5762 vec->quick_push (gimple_call_arg (call, 0));
5763 tree expected = gimple_call_arg (call, 1);
5764 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
5765 TREE_TYPE (expected));
5766 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
5767 if (expd != x)
5768 emit_move_insn (x, expd);
5769 tree v = make_tree (TREE_TYPE (expected), x);
5770 vec->quick_push (build1 (ADDR_EXPR,
5771 build_pointer_type (TREE_TYPE (expected)), v));
5772 vec->quick_push (gimple_call_arg (call, 2));
5773 /* Skip the boolean weak parameter. */
5774 for (z = 4; z < 6; z++)
5775 vec->quick_push (gimple_call_arg (call, z));
5776 built_in_function fncode
5777 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
5778 + exact_log2 (GET_MODE_SIZE (mode)));
5779 tree fndecl = builtin_decl_explicit (fncode);
5780 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
5781 fndecl);
5782 tree exp = build_call_vec (boolean_type_node, fn, vec);
5783 tree lhs = gimple_call_lhs (call);
5784 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
5785 if (lhs)
5787 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5788 if (GET_MODE (boolret) != mode)
5789 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
5790 x = force_reg (mode, x);
5791 write_complex_part (target, boolret, true);
5792 write_complex_part (target, x, false);
5796 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
5798 void
5799 expand_ifn_atomic_compare_exchange (gcall *call)
5801 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
5802 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
5803 machine_mode mode = mode_for_size (BITS_PER_UNIT * size, MODE_INT, 0);
5804 rtx expect, desired, mem, oldval, boolret;
5805 enum memmodel success, failure;
5806 tree lhs;
5807 bool is_weak;
5808 source_location loc
5809 = expansion_point_location_if_in_system_header (gimple_location (call));
5811 success = get_memmodel (gimple_call_arg (call, 4));
5812 failure = get_memmodel (gimple_call_arg (call, 5));
5814 if (failure > success)
5816 warning_at (loc, OPT_Winvalid_memory_model,
5817 "failure memory model cannot be stronger than success "
5818 "memory model for %<__atomic_compare_exchange%>");
5819 success = MEMMODEL_SEQ_CST;
5822 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5824 warning_at (loc, OPT_Winvalid_memory_model,
5825 "invalid failure memory model for "
5826 "%<__atomic_compare_exchange%>");
5827 failure = MEMMODEL_SEQ_CST;
5828 success = MEMMODEL_SEQ_CST;
5831 if (!flag_inline_atomics)
5833 expand_ifn_atomic_compare_exchange_into_call (call, mode);
5834 return;
5837 /* Expand the operands. */
5838 mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
5840 expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
5841 desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
5843 is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
5845 boolret = NULL;
5846 oldval = NULL;
5848 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
5849 is_weak, success, failure))
5851 expand_ifn_atomic_compare_exchange_into_call (call, mode);
5852 return;
5855 lhs = gimple_call_lhs (call);
5856 if (lhs)
5858 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5859 if (GET_MODE (boolret) != mode)
5860 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
5861 write_complex_part (target, boolret, true);
5862 write_complex_part (target, oldval, false);
5866 /* Expand the __atomic_load intrinsic:
5867 TYPE __atomic_load (TYPE *object, enum memmodel)
5868 EXP is the CALL_EXPR.
5869 TARGET is an optional place for us to store the results. */
5871 static rtx
5872 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5874 rtx mem;
5875 enum memmodel model;
5877 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5878 if (is_mm_release (model) || is_mm_acq_rel (model))
5880 source_location loc
5881 = expansion_point_location_if_in_system_header (input_location);
5882 warning_at (loc, OPT_Winvalid_memory_model,
5883 "invalid memory model for %<__atomic_load%>");
5884 model = MEMMODEL_SEQ_CST;
5887 if (!flag_inline_atomics)
5888 return NULL_RTX;
5890 /* Expand the operand. */
5891 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5893 return expand_atomic_load (target, mem, model);
5897 /* Expand the __atomic_store intrinsic:
5898 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5899 EXP is the CALL_EXPR.
5900 TARGET is an optional place for us to store the results. */
5902 static rtx
5903 expand_builtin_atomic_store (machine_mode mode, tree exp)
5905 rtx mem, val;
5906 enum memmodel model;
5908 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5909 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5910 || is_mm_release (model)))
5912 source_location loc
5913 = expansion_point_location_if_in_system_header (input_location);
5914 warning_at (loc, OPT_Winvalid_memory_model,
5915 "invalid memory model for %<__atomic_store%>");
5916 model = MEMMODEL_SEQ_CST;
5919 if (!flag_inline_atomics)
5920 return NULL_RTX;
5922 /* Expand the operands. */
5923 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5924 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5926 return expand_atomic_store (mem, val, model, false);
5929 /* Expand the __atomic_fetch_XXX intrinsic:
5930 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5931 EXP is the CALL_EXPR.
5932 TARGET is an optional place for us to store the results.
5933 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5934 FETCH_AFTER is true if returning the result of the operation.
5935 FETCH_AFTER is false if returning the value before the operation.
5936 IGNORE is true if the result is not used.
5937 EXT_CALL is the correct builtin for an external call if this cannot be
5938 resolved to an instruction sequence. */
5940 static rtx
5941 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5942 enum rtx_code code, bool fetch_after,
5943 bool ignore, enum built_in_function ext_call)
5945 rtx val, mem, ret;
5946 enum memmodel model;
5947 tree fndecl;
5948 tree addr;
5950 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5952 /* Expand the operands. */
5953 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5954 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5956 /* Only try generating instructions if inlining is turned on. */
5957 if (flag_inline_atomics)
5959 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5960 if (ret)
5961 return ret;
5964 /* Return if a different routine isn't needed for the library call. */
5965 if (ext_call == BUILT_IN_NONE)
5966 return NULL_RTX;
5968 /* Change the call to the specified function. */
5969 fndecl = get_callee_fndecl (exp);
5970 addr = CALL_EXPR_FN (exp);
5971 STRIP_NOPS (addr);
5973 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5974 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5976 /* Expand the call here so we can emit trailing code. */
5977 ret = expand_call (exp, target, ignore);
5979 /* Replace the original function just in case it matters. */
5980 TREE_OPERAND (addr, 0) = fndecl;
5982 /* Then issue the arithmetic correction to return the right result. */
5983 if (!ignore)
5985 if (code == NOT)
5987 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5988 OPTAB_LIB_WIDEN);
5989 ret = expand_simple_unop (mode, NOT, ret, target, true);
5991 else
5992 ret = expand_simple_binop (mode, code, ret, val, target, true,
5993 OPTAB_LIB_WIDEN);
5995 return ret;
5998 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6000 void
6001 expand_ifn_atomic_bit_test_and (gcall *call)
6003 tree ptr = gimple_call_arg (call, 0);
6004 tree bit = gimple_call_arg (call, 1);
6005 tree flag = gimple_call_arg (call, 2);
6006 tree lhs = gimple_call_lhs (call);
6007 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6008 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6009 enum rtx_code code;
6010 optab optab;
6011 struct expand_operand ops[5];
6013 gcc_assert (flag_inline_atomics);
6015 if (gimple_call_num_args (call) == 4)
6016 model = get_memmodel (gimple_call_arg (call, 3));
6018 rtx mem = get_builtin_sync_mem (ptr, mode);
6019 rtx val = expand_expr_force_mode (bit, mode);
6021 switch (gimple_call_internal_fn (call))
6023 case IFN_ATOMIC_BIT_TEST_AND_SET:
6024 code = IOR;
6025 optab = atomic_bit_test_and_set_optab;
6026 break;
6027 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6028 code = XOR;
6029 optab = atomic_bit_test_and_complement_optab;
6030 break;
6031 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6032 code = AND;
6033 optab = atomic_bit_test_and_reset_optab;
6034 break;
6035 default:
6036 gcc_unreachable ();
6039 if (lhs == NULL_TREE)
6041 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6042 val, NULL_RTX, true, OPTAB_DIRECT);
6043 if (code == AND)
6044 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6045 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6046 return;
6049 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6050 enum insn_code icode = direct_optab_handler (optab, mode);
6051 gcc_assert (icode != CODE_FOR_nothing);
6052 create_output_operand (&ops[0], target, mode);
6053 create_fixed_operand (&ops[1], mem);
6054 create_convert_operand_to (&ops[2], val, mode, true);
6055 create_integer_operand (&ops[3], model);
6056 create_integer_operand (&ops[4], integer_onep (flag));
6057 if (maybe_expand_insn (icode, 5, ops))
6058 return;
6060 rtx bitval = val;
6061 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6062 val, NULL_RTX, true, OPTAB_DIRECT);
6063 rtx maskval = val;
6064 if (code == AND)
6065 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6066 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6067 code, model, false);
6068 if (integer_onep (flag))
6070 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6071 NULL_RTX, true, OPTAB_DIRECT);
6072 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6073 true, OPTAB_DIRECT);
6075 else
6076 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6077 OPTAB_DIRECT);
6078 if (result != target)
6079 emit_move_insn (target, result);
6082 /* Expand an atomic clear operation.
6083 void _atomic_clear (BOOL *obj, enum memmodel)
6084 EXP is the call expression. */
6086 static rtx
6087 expand_builtin_atomic_clear (tree exp)
6089 machine_mode mode;
6090 rtx mem, ret;
6091 enum memmodel model;
6093 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
6094 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6095 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6097 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6099 source_location loc
6100 = expansion_point_location_if_in_system_header (input_location);
6101 warning_at (loc, OPT_Winvalid_memory_model,
6102 "invalid memory model for %<__atomic_store%>");
6103 model = MEMMODEL_SEQ_CST;
6106 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6107 Failing that, a store is issued by __atomic_store. The only way this can
6108 fail is if the bool type is larger than a word size. Unlikely, but
6109 handle it anyway for completeness. Assume a single threaded model since
6110 there is no atomic support in this case, and no barriers are required. */
6111 ret = expand_atomic_store (mem, const0_rtx, model, true);
6112 if (!ret)
6113 emit_move_insn (mem, const0_rtx);
6114 return const0_rtx;
6117 /* Expand an atomic test_and_set operation.
6118 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6119 EXP is the call expression. */
6121 static rtx
6122 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6124 rtx mem;
6125 enum memmodel model;
6126 machine_mode mode;
6128 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
6129 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6130 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6132 return expand_atomic_test_and_set (target, mem, model);
6136 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6137 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6139 static tree
6140 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6142 int size;
6143 machine_mode mode;
6144 unsigned int mode_align, type_align;
6146 if (TREE_CODE (arg0) != INTEGER_CST)
6147 return NULL_TREE;
6149 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6150 mode = mode_for_size (size, MODE_INT, 0);
6151 mode_align = GET_MODE_ALIGNMENT (mode);
6153 if (TREE_CODE (arg1) == INTEGER_CST)
6155 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6157 /* Either this argument is null, or it's a fake pointer encoding
6158 the alignment of the object. */
6159 val = least_bit_hwi (val);
6160 val *= BITS_PER_UNIT;
6162 if (val == 0 || mode_align < val)
6163 type_align = mode_align;
6164 else
6165 type_align = val;
6167 else
6169 tree ttype = TREE_TYPE (arg1);
6171 /* This function is usually invoked and folded immediately by the front
6172 end before anything else has a chance to look at it. The pointer
6173 parameter at this point is usually cast to a void *, so check for that
6174 and look past the cast. */
6175 if (CONVERT_EXPR_P (arg1)
6176 && POINTER_TYPE_P (ttype)
6177 && VOID_TYPE_P (TREE_TYPE (ttype))
6178 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6179 arg1 = TREE_OPERAND (arg1, 0);
6181 ttype = TREE_TYPE (arg1);
6182 gcc_assert (POINTER_TYPE_P (ttype));
6184 /* Get the underlying type of the object. */
6185 ttype = TREE_TYPE (ttype);
6186 type_align = TYPE_ALIGN (ttype);
6189 /* If the object has smaller alignment, the lock free routines cannot
6190 be used. */
6191 if (type_align < mode_align)
6192 return boolean_false_node;
6194 /* Check if a compare_and_swap pattern exists for the mode which represents
6195 the required size. The pattern is not allowed to fail, so the existence
6196 of the pattern indicates support is present. */
6197 if (can_compare_and_swap_p (mode, true))
6198 return boolean_true_node;
6199 else
6200 return boolean_false_node;
6203 /* Return true if the parameters to call EXP represent an object which will
6204 always generate lock free instructions. The first argument represents the
6205 size of the object, and the second parameter is a pointer to the object
6206 itself. If NULL is passed for the object, then the result is based on
6207 typical alignment for an object of the specified size. Otherwise return
6208 false. */
6210 static rtx
6211 expand_builtin_atomic_always_lock_free (tree exp)
6213 tree size;
6214 tree arg0 = CALL_EXPR_ARG (exp, 0);
6215 tree arg1 = CALL_EXPR_ARG (exp, 1);
6217 if (TREE_CODE (arg0) != INTEGER_CST)
6219 error ("non-constant argument 1 to __atomic_always_lock_free");
6220 return const0_rtx;
6223 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6224 if (size == boolean_true_node)
6225 return const1_rtx;
6226 return const0_rtx;
6229 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6230 is lock free on this architecture. */
6232 static tree
6233 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6235 if (!flag_inline_atomics)
6236 return NULL_TREE;
6238 /* If it isn't always lock free, don't generate a result. */
6239 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6240 return boolean_true_node;
6242 return NULL_TREE;
6245 /* Return true if the parameters to call EXP represent an object which will
6246 always generate lock free instructions. The first argument represents the
6247 size of the object, and the second parameter is a pointer to the object
6248 itself. If NULL is passed for the object, then the result is based on
6249 typical alignment for an object of the specified size. Otherwise return
6250 NULL*/
6252 static rtx
6253 expand_builtin_atomic_is_lock_free (tree exp)
6255 tree size;
6256 tree arg0 = CALL_EXPR_ARG (exp, 0);
6257 tree arg1 = CALL_EXPR_ARG (exp, 1);
6259 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6261 error ("non-integer argument 1 to __atomic_is_lock_free");
6262 return NULL_RTX;
6265 if (!flag_inline_atomics)
6266 return NULL_RTX;
6268 /* If the value is known at compile time, return the RTX for it. */
6269 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6270 if (size == boolean_true_node)
6271 return const1_rtx;
6273 return NULL_RTX;
6276 /* Expand the __atomic_thread_fence intrinsic:
6277 void __atomic_thread_fence (enum memmodel)
6278 EXP is the CALL_EXPR. */
6280 static void
6281 expand_builtin_atomic_thread_fence (tree exp)
6283 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6284 expand_mem_thread_fence (model);
6287 /* Expand the __atomic_signal_fence intrinsic:
6288 void __atomic_signal_fence (enum memmodel)
6289 EXP is the CALL_EXPR. */
6291 static void
6292 expand_builtin_atomic_signal_fence (tree exp)
6294 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6295 expand_mem_signal_fence (model);
6298 /* Expand the __sync_synchronize intrinsic. */
6300 static void
6301 expand_builtin_sync_synchronize (void)
6303 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6306 static rtx
6307 expand_builtin_thread_pointer (tree exp, rtx target)
6309 enum insn_code icode;
6310 if (!validate_arglist (exp, VOID_TYPE))
6311 return const0_rtx;
6312 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6313 if (icode != CODE_FOR_nothing)
6315 struct expand_operand op;
6316 /* If the target is not sutitable then create a new target. */
6317 if (target == NULL_RTX
6318 || !REG_P (target)
6319 || GET_MODE (target) != Pmode)
6320 target = gen_reg_rtx (Pmode);
6321 create_output_operand (&op, target, Pmode);
6322 expand_insn (icode, 1, &op);
6323 return target;
6325 error ("__builtin_thread_pointer is not supported on this target");
6326 return const0_rtx;
6329 static void
6330 expand_builtin_set_thread_pointer (tree exp)
6332 enum insn_code icode;
6333 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6334 return;
6335 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6336 if (icode != CODE_FOR_nothing)
6338 struct expand_operand op;
6339 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6340 Pmode, EXPAND_NORMAL);
6341 create_input_operand (&op, val, Pmode);
6342 expand_insn (icode, 1, &op);
6343 return;
6345 error ("__builtin_set_thread_pointer is not supported on this target");
6349 /* Emit code to restore the current value of stack. */
6351 static void
6352 expand_stack_restore (tree var)
6354 rtx_insn *prev;
6355 rtx sa = expand_normal (var);
6357 sa = convert_memory_address (Pmode, sa);
6359 prev = get_last_insn ();
6360 emit_stack_restore (SAVE_BLOCK, sa);
6362 record_new_stack_level ();
6364 fixup_args_size_notes (prev, get_last_insn (), 0);
6367 /* Emit code to save the current value of stack. */
6369 static rtx
6370 expand_stack_save (void)
6372 rtx ret = NULL_RTX;
6374 emit_stack_save (SAVE_BLOCK, &ret);
6375 return ret;
6379 /* Expand an expression EXP that calls a built-in function,
6380 with result going to TARGET if that's convenient
6381 (and in mode MODE if that's convenient).
6382 SUBTARGET may be used as the target for computing one of EXP's operands.
6383 IGNORE is nonzero if the value is to be ignored. */
6386 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6387 int ignore)
6389 tree fndecl = get_callee_fndecl (exp);
6390 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6391 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6392 int flags;
6394 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6395 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6397 /* When ASan is enabled, we don't want to expand some memory/string
6398 builtins and rely on libsanitizer's hooks. This allows us to avoid
6399 redundant checks and be sure, that possible overflow will be detected
6400 by ASan. */
6402 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6403 return expand_call (exp, target, ignore);
6405 /* When not optimizing, generate calls to library functions for a certain
6406 set of builtins. */
6407 if (!optimize
6408 && !called_as_built_in (fndecl)
6409 && fcode != BUILT_IN_FORK
6410 && fcode != BUILT_IN_EXECL
6411 && fcode != BUILT_IN_EXECV
6412 && fcode != BUILT_IN_EXECLP
6413 && fcode != BUILT_IN_EXECLE
6414 && fcode != BUILT_IN_EXECVP
6415 && fcode != BUILT_IN_EXECVE
6416 && fcode != BUILT_IN_ALLOCA
6417 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
6418 && fcode != BUILT_IN_FREE
6419 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
6420 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
6421 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
6422 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
6423 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6424 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
6425 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
6426 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
6427 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
6428 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
6429 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
6430 && fcode != BUILT_IN_CHKP_BNDRET)
6431 return expand_call (exp, target, ignore);
6433 /* The built-in function expanders test for target == const0_rtx
6434 to determine whether the function's result will be ignored. */
6435 if (ignore)
6436 target = const0_rtx;
6438 /* If the result of a pure or const built-in function is ignored, and
6439 none of its arguments are volatile, we can avoid expanding the
6440 built-in call and just evaluate the arguments for side-effects. */
6441 if (target == const0_rtx
6442 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6443 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6445 bool volatilep = false;
6446 tree arg;
6447 call_expr_arg_iterator iter;
6449 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6450 if (TREE_THIS_VOLATILE (arg))
6452 volatilep = true;
6453 break;
6456 if (! volatilep)
6458 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6459 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6460 return const0_rtx;
6464 /* expand_builtin_with_bounds is supposed to be used for
6465 instrumented builtin calls. */
6466 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6468 switch (fcode)
6470 CASE_FLT_FN (BUILT_IN_FABS):
6471 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
6472 case BUILT_IN_FABSD32:
6473 case BUILT_IN_FABSD64:
6474 case BUILT_IN_FABSD128:
6475 target = expand_builtin_fabs (exp, target, subtarget);
6476 if (target)
6477 return target;
6478 break;
6480 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6481 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
6482 target = expand_builtin_copysign (exp, target, subtarget);
6483 if (target)
6484 return target;
6485 break;
6487 /* Just do a normal library call if we were unable to fold
6488 the values. */
6489 CASE_FLT_FN (BUILT_IN_CABS):
6490 break;
6492 CASE_FLT_FN (BUILT_IN_FMA):
6493 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6494 if (target)
6495 return target;
6496 break;
6498 CASE_FLT_FN (BUILT_IN_ILOGB):
6499 if (! flag_unsafe_math_optimizations)
6500 break;
6501 gcc_fallthrough ();
6502 CASE_FLT_FN (BUILT_IN_ISINF):
6503 CASE_FLT_FN (BUILT_IN_FINITE):
6504 case BUILT_IN_ISFINITE:
6505 case BUILT_IN_ISNORMAL:
6506 target = expand_builtin_interclass_mathfn (exp, target);
6507 if (target)
6508 return target;
6509 break;
6511 CASE_FLT_FN (BUILT_IN_ICEIL):
6512 CASE_FLT_FN (BUILT_IN_LCEIL):
6513 CASE_FLT_FN (BUILT_IN_LLCEIL):
6514 CASE_FLT_FN (BUILT_IN_LFLOOR):
6515 CASE_FLT_FN (BUILT_IN_IFLOOR):
6516 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6517 target = expand_builtin_int_roundingfn (exp, target);
6518 if (target)
6519 return target;
6520 break;
6522 CASE_FLT_FN (BUILT_IN_IRINT):
6523 CASE_FLT_FN (BUILT_IN_LRINT):
6524 CASE_FLT_FN (BUILT_IN_LLRINT):
6525 CASE_FLT_FN (BUILT_IN_IROUND):
6526 CASE_FLT_FN (BUILT_IN_LROUND):
6527 CASE_FLT_FN (BUILT_IN_LLROUND):
6528 target = expand_builtin_int_roundingfn_2 (exp, target);
6529 if (target)
6530 return target;
6531 break;
6533 CASE_FLT_FN (BUILT_IN_POWI):
6534 target = expand_builtin_powi (exp, target);
6535 if (target)
6536 return target;
6537 break;
6539 CASE_FLT_FN (BUILT_IN_CEXPI):
6540 target = expand_builtin_cexpi (exp, target);
6541 gcc_assert (target);
6542 return target;
6544 CASE_FLT_FN (BUILT_IN_SIN):
6545 CASE_FLT_FN (BUILT_IN_COS):
6546 if (! flag_unsafe_math_optimizations)
6547 break;
6548 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6549 if (target)
6550 return target;
6551 break;
6553 CASE_FLT_FN (BUILT_IN_SINCOS):
6554 if (! flag_unsafe_math_optimizations)
6555 break;
6556 target = expand_builtin_sincos (exp);
6557 if (target)
6558 return target;
6559 break;
6561 case BUILT_IN_APPLY_ARGS:
6562 return expand_builtin_apply_args ();
6564 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6565 FUNCTION with a copy of the parameters described by
6566 ARGUMENTS, and ARGSIZE. It returns a block of memory
6567 allocated on the stack into which is stored all the registers
6568 that might possibly be used for returning the result of a
6569 function. ARGUMENTS is the value returned by
6570 __builtin_apply_args. ARGSIZE is the number of bytes of
6571 arguments that must be copied. ??? How should this value be
6572 computed? We'll also need a safe worst case value for varargs
6573 functions. */
6574 case BUILT_IN_APPLY:
6575 if (!validate_arglist (exp, POINTER_TYPE,
6576 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6577 && !validate_arglist (exp, REFERENCE_TYPE,
6578 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6579 return const0_rtx;
6580 else
6582 rtx ops[3];
6584 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6585 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6586 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6588 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6591 /* __builtin_return (RESULT) causes the function to return the
6592 value described by RESULT. RESULT is address of the block of
6593 memory returned by __builtin_apply. */
6594 case BUILT_IN_RETURN:
6595 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6596 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6597 return const0_rtx;
6599 case BUILT_IN_SAVEREGS:
6600 return expand_builtin_saveregs ();
6602 case BUILT_IN_VA_ARG_PACK:
6603 /* All valid uses of __builtin_va_arg_pack () are removed during
6604 inlining. */
6605 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6606 return const0_rtx;
6608 case BUILT_IN_VA_ARG_PACK_LEN:
6609 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6610 inlining. */
6611 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6612 return const0_rtx;
6614 /* Return the address of the first anonymous stack arg. */
6615 case BUILT_IN_NEXT_ARG:
6616 if (fold_builtin_next_arg (exp, false))
6617 return const0_rtx;
6618 return expand_builtin_next_arg ();
6620 case BUILT_IN_CLEAR_CACHE:
6621 target = expand_builtin___clear_cache (exp);
6622 if (target)
6623 return target;
6624 break;
6626 case BUILT_IN_CLASSIFY_TYPE:
6627 return expand_builtin_classify_type (exp);
6629 case BUILT_IN_CONSTANT_P:
6630 return const0_rtx;
6632 case BUILT_IN_FRAME_ADDRESS:
6633 case BUILT_IN_RETURN_ADDRESS:
6634 return expand_builtin_frame_address (fndecl, exp);
6636 /* Returns the address of the area where the structure is returned.
6637 0 otherwise. */
6638 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6639 if (call_expr_nargs (exp) != 0
6640 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6641 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6642 return const0_rtx;
6643 else
6644 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6646 case BUILT_IN_ALLOCA:
6647 case BUILT_IN_ALLOCA_WITH_ALIGN:
6648 /* If the allocation stems from the declaration of a variable-sized
6649 object, it cannot accumulate. */
6650 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6651 if (target)
6652 return target;
6653 break;
6655 case BUILT_IN_STACK_SAVE:
6656 return expand_stack_save ();
6658 case BUILT_IN_STACK_RESTORE:
6659 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6660 return const0_rtx;
6662 case BUILT_IN_BSWAP16:
6663 case BUILT_IN_BSWAP32:
6664 case BUILT_IN_BSWAP64:
6665 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6666 if (target)
6667 return target;
6668 break;
6670 CASE_INT_FN (BUILT_IN_FFS):
6671 target = expand_builtin_unop (target_mode, exp, target,
6672 subtarget, ffs_optab);
6673 if (target)
6674 return target;
6675 break;
6677 CASE_INT_FN (BUILT_IN_CLZ):
6678 target = expand_builtin_unop (target_mode, exp, target,
6679 subtarget, clz_optab);
6680 if (target)
6681 return target;
6682 break;
6684 CASE_INT_FN (BUILT_IN_CTZ):
6685 target = expand_builtin_unop (target_mode, exp, target,
6686 subtarget, ctz_optab);
6687 if (target)
6688 return target;
6689 break;
6691 CASE_INT_FN (BUILT_IN_CLRSB):
6692 target = expand_builtin_unop (target_mode, exp, target,
6693 subtarget, clrsb_optab);
6694 if (target)
6695 return target;
6696 break;
6698 CASE_INT_FN (BUILT_IN_POPCOUNT):
6699 target = expand_builtin_unop (target_mode, exp, target,
6700 subtarget, popcount_optab);
6701 if (target)
6702 return target;
6703 break;
6705 CASE_INT_FN (BUILT_IN_PARITY):
6706 target = expand_builtin_unop (target_mode, exp, target,
6707 subtarget, parity_optab);
6708 if (target)
6709 return target;
6710 break;
6712 case BUILT_IN_STRLEN:
6713 target = expand_builtin_strlen (exp, target, target_mode);
6714 if (target)
6715 return target;
6716 break;
6718 case BUILT_IN_STRCAT:
6719 target = expand_builtin_strcat (exp, target);
6720 if (target)
6721 return target;
6722 break;
6724 case BUILT_IN_STRCPY:
6725 target = expand_builtin_strcpy (exp, target);
6726 if (target)
6727 return target;
6728 break;
6730 case BUILT_IN_STRNCAT:
6731 target = expand_builtin_strncat (exp, target);
6732 if (target)
6733 return target;
6734 break;
6736 case BUILT_IN_STRNCPY:
6737 target = expand_builtin_strncpy (exp, target);
6738 if (target)
6739 return target;
6740 break;
6742 case BUILT_IN_STPCPY:
6743 target = expand_builtin_stpcpy (exp, target, mode);
6744 if (target)
6745 return target;
6746 break;
6748 case BUILT_IN_MEMCPY:
6749 target = expand_builtin_memcpy (exp, target);
6750 if (target)
6751 return target;
6752 break;
6754 case BUILT_IN_MEMPCPY:
6755 target = expand_builtin_mempcpy (exp, target, mode);
6756 if (target)
6757 return target;
6758 break;
6760 case BUILT_IN_MEMSET:
6761 target = expand_builtin_memset (exp, target, mode);
6762 if (target)
6763 return target;
6764 break;
6766 case BUILT_IN_BZERO:
6767 target = expand_builtin_bzero (exp);
6768 if (target)
6769 return target;
6770 break;
6772 case BUILT_IN_STRCMP:
6773 target = expand_builtin_strcmp (exp, target);
6774 if (target)
6775 return target;
6776 break;
6778 case BUILT_IN_STRNCMP:
6779 target = expand_builtin_strncmp (exp, target, mode);
6780 if (target)
6781 return target;
6782 break;
6784 case BUILT_IN_BCMP:
6785 case BUILT_IN_MEMCMP:
6786 case BUILT_IN_MEMCMP_EQ:
6787 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
6788 if (target)
6789 return target;
6790 if (fcode == BUILT_IN_MEMCMP_EQ)
6792 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
6793 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
6795 break;
6797 case BUILT_IN_SETJMP:
6798 /* This should have been lowered to the builtins below. */
6799 gcc_unreachable ();
6801 case BUILT_IN_SETJMP_SETUP:
6802 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6803 and the receiver label. */
6804 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6806 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6807 VOIDmode, EXPAND_NORMAL);
6808 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6809 rtx_insn *label_r = label_rtx (label);
6811 /* This is copied from the handling of non-local gotos. */
6812 expand_builtin_setjmp_setup (buf_addr, label_r);
6813 nonlocal_goto_handler_labels
6814 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6815 nonlocal_goto_handler_labels);
6816 /* ??? Do not let expand_label treat us as such since we would
6817 not want to be both on the list of non-local labels and on
6818 the list of forced labels. */
6819 FORCED_LABEL (label) = 0;
6820 return const0_rtx;
6822 break;
6824 case BUILT_IN_SETJMP_RECEIVER:
6825 /* __builtin_setjmp_receiver is passed the receiver label. */
6826 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6828 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6829 rtx_insn *label_r = label_rtx (label);
6831 expand_builtin_setjmp_receiver (label_r);
6832 return const0_rtx;
6834 break;
6836 /* __builtin_longjmp is passed a pointer to an array of five words.
6837 It's similar to the C library longjmp function but works with
6838 __builtin_setjmp above. */
6839 case BUILT_IN_LONGJMP:
6840 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6842 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6843 VOIDmode, EXPAND_NORMAL);
6844 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6846 if (value != const1_rtx)
6848 error ("%<__builtin_longjmp%> second argument must be 1");
6849 return const0_rtx;
6852 expand_builtin_longjmp (buf_addr, value);
6853 return const0_rtx;
6855 break;
6857 case BUILT_IN_NONLOCAL_GOTO:
6858 target = expand_builtin_nonlocal_goto (exp);
6859 if (target)
6860 return target;
6861 break;
6863 /* This updates the setjmp buffer that is its argument with the value
6864 of the current stack pointer. */
6865 case BUILT_IN_UPDATE_SETJMP_BUF:
6866 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6868 rtx buf_addr
6869 = expand_normal (CALL_EXPR_ARG (exp, 0));
6871 expand_builtin_update_setjmp_buf (buf_addr);
6872 return const0_rtx;
6874 break;
6876 case BUILT_IN_TRAP:
6877 expand_builtin_trap ();
6878 return const0_rtx;
6880 case BUILT_IN_UNREACHABLE:
6881 expand_builtin_unreachable ();
6882 return const0_rtx;
6884 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6885 case BUILT_IN_SIGNBITD32:
6886 case BUILT_IN_SIGNBITD64:
6887 case BUILT_IN_SIGNBITD128:
6888 target = expand_builtin_signbit (exp, target);
6889 if (target)
6890 return target;
6891 break;
6893 /* Various hooks for the DWARF 2 __throw routine. */
6894 case BUILT_IN_UNWIND_INIT:
6895 expand_builtin_unwind_init ();
6896 return const0_rtx;
6897 case BUILT_IN_DWARF_CFA:
6898 return virtual_cfa_rtx;
6899 #ifdef DWARF2_UNWIND_INFO
6900 case BUILT_IN_DWARF_SP_COLUMN:
6901 return expand_builtin_dwarf_sp_column ();
6902 case BUILT_IN_INIT_DWARF_REG_SIZES:
6903 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6904 return const0_rtx;
6905 #endif
6906 case BUILT_IN_FROB_RETURN_ADDR:
6907 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6908 case BUILT_IN_EXTRACT_RETURN_ADDR:
6909 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6910 case BUILT_IN_EH_RETURN:
6911 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6912 CALL_EXPR_ARG (exp, 1));
6913 return const0_rtx;
6914 case BUILT_IN_EH_RETURN_DATA_REGNO:
6915 return expand_builtin_eh_return_data_regno (exp);
6916 case BUILT_IN_EXTEND_POINTER:
6917 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6918 case BUILT_IN_EH_POINTER:
6919 return expand_builtin_eh_pointer (exp);
6920 case BUILT_IN_EH_FILTER:
6921 return expand_builtin_eh_filter (exp);
6922 case BUILT_IN_EH_COPY_VALUES:
6923 return expand_builtin_eh_copy_values (exp);
6925 case BUILT_IN_VA_START:
6926 return expand_builtin_va_start (exp);
6927 case BUILT_IN_VA_END:
6928 return expand_builtin_va_end (exp);
6929 case BUILT_IN_VA_COPY:
6930 return expand_builtin_va_copy (exp);
6931 case BUILT_IN_EXPECT:
6932 return expand_builtin_expect (exp, target);
6933 case BUILT_IN_ASSUME_ALIGNED:
6934 return expand_builtin_assume_aligned (exp, target);
6935 case BUILT_IN_PREFETCH:
6936 expand_builtin_prefetch (exp);
6937 return const0_rtx;
6939 case BUILT_IN_INIT_TRAMPOLINE:
6940 return expand_builtin_init_trampoline (exp, true);
6941 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6942 return expand_builtin_init_trampoline (exp, false);
6943 case BUILT_IN_ADJUST_TRAMPOLINE:
6944 return expand_builtin_adjust_trampoline (exp);
6946 case BUILT_IN_INIT_DESCRIPTOR:
6947 return expand_builtin_init_descriptor (exp);
6948 case BUILT_IN_ADJUST_DESCRIPTOR:
6949 return expand_builtin_adjust_descriptor (exp);
6951 case BUILT_IN_FORK:
6952 case BUILT_IN_EXECL:
6953 case BUILT_IN_EXECV:
6954 case BUILT_IN_EXECLP:
6955 case BUILT_IN_EXECLE:
6956 case BUILT_IN_EXECVP:
6957 case BUILT_IN_EXECVE:
6958 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6959 if (target)
6960 return target;
6961 break;
6963 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6964 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6965 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6966 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6967 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6968 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6969 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6970 if (target)
6971 return target;
6972 break;
6974 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6975 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6976 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6977 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6978 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6979 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6980 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6981 if (target)
6982 return target;
6983 break;
6985 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6986 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6987 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6988 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6989 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6990 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6991 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6992 if (target)
6993 return target;
6994 break;
6996 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6997 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6998 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6999 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7000 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7001 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7002 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7003 if (target)
7004 return target;
7005 break;
7007 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7008 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7009 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7010 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7011 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7012 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7013 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7014 if (target)
7015 return target;
7016 break;
7018 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7019 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7020 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7021 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7022 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7023 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7024 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7025 if (target)
7026 return target;
7027 break;
7029 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7030 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7031 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7032 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7033 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7034 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7035 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7036 if (target)
7037 return target;
7038 break;
7040 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7041 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7042 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7043 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7044 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7045 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7046 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7047 if (target)
7048 return target;
7049 break;
7051 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7052 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7053 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7054 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7055 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7056 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7057 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7058 if (target)
7059 return target;
7060 break;
7062 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7063 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7064 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7065 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7066 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7067 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7068 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7069 if (target)
7070 return target;
7071 break;
7073 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7074 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7075 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7076 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7077 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7078 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7079 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7080 if (target)
7081 return target;
7082 break;
7084 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7085 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7086 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7087 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7088 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7089 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7090 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7091 if (target)
7092 return target;
7093 break;
7095 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7096 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7097 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7098 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7099 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7100 if (mode == VOIDmode)
7101 mode = TYPE_MODE (boolean_type_node);
7102 if (!target || !register_operand (target, mode))
7103 target = gen_reg_rtx (mode);
7105 mode = get_builtin_sync_mode
7106 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7107 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7108 if (target)
7109 return target;
7110 break;
7112 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7113 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7114 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7115 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7116 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7117 mode = get_builtin_sync_mode
7118 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7119 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7120 if (target)
7121 return target;
7122 break;
7124 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7125 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7126 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7127 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7128 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7129 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7130 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7131 if (target)
7132 return target;
7133 break;
7135 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7136 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7137 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7138 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7139 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7140 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7141 expand_builtin_sync_lock_release (mode, exp);
7142 return const0_rtx;
7144 case BUILT_IN_SYNC_SYNCHRONIZE:
7145 expand_builtin_sync_synchronize ();
7146 return const0_rtx;
7148 case BUILT_IN_ATOMIC_EXCHANGE_1:
7149 case BUILT_IN_ATOMIC_EXCHANGE_2:
7150 case BUILT_IN_ATOMIC_EXCHANGE_4:
7151 case BUILT_IN_ATOMIC_EXCHANGE_8:
7152 case BUILT_IN_ATOMIC_EXCHANGE_16:
7153 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7154 target = expand_builtin_atomic_exchange (mode, exp, target);
7155 if (target)
7156 return target;
7157 break;
7159 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7160 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7161 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7162 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7163 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7165 unsigned int nargs, z;
7166 vec<tree, va_gc> *vec;
7168 mode =
7169 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7170 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7171 if (target)
7172 return target;
7174 /* If this is turned into an external library call, the weak parameter
7175 must be dropped to match the expected parameter list. */
7176 nargs = call_expr_nargs (exp);
7177 vec_alloc (vec, nargs - 1);
7178 for (z = 0; z < 3; z++)
7179 vec->quick_push (CALL_EXPR_ARG (exp, z));
7180 /* Skip the boolean weak parameter. */
7181 for (z = 4; z < 6; z++)
7182 vec->quick_push (CALL_EXPR_ARG (exp, z));
7183 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7184 break;
7187 case BUILT_IN_ATOMIC_LOAD_1:
7188 case BUILT_IN_ATOMIC_LOAD_2:
7189 case BUILT_IN_ATOMIC_LOAD_4:
7190 case BUILT_IN_ATOMIC_LOAD_8:
7191 case BUILT_IN_ATOMIC_LOAD_16:
7192 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7193 target = expand_builtin_atomic_load (mode, exp, target);
7194 if (target)
7195 return target;
7196 break;
7198 case BUILT_IN_ATOMIC_STORE_1:
7199 case BUILT_IN_ATOMIC_STORE_2:
7200 case BUILT_IN_ATOMIC_STORE_4:
7201 case BUILT_IN_ATOMIC_STORE_8:
7202 case BUILT_IN_ATOMIC_STORE_16:
7203 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7204 target = expand_builtin_atomic_store (mode, exp);
7205 if (target)
7206 return const0_rtx;
7207 break;
7209 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7210 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7211 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7212 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7213 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7215 enum built_in_function lib;
7216 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7217 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7218 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7219 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7220 ignore, lib);
7221 if (target)
7222 return target;
7223 break;
7225 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7226 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7227 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7228 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7229 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7231 enum built_in_function lib;
7232 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7233 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7234 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7235 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7236 ignore, lib);
7237 if (target)
7238 return target;
7239 break;
7241 case BUILT_IN_ATOMIC_AND_FETCH_1:
7242 case BUILT_IN_ATOMIC_AND_FETCH_2:
7243 case BUILT_IN_ATOMIC_AND_FETCH_4:
7244 case BUILT_IN_ATOMIC_AND_FETCH_8:
7245 case BUILT_IN_ATOMIC_AND_FETCH_16:
7247 enum built_in_function lib;
7248 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7249 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7250 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7251 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7252 ignore, lib);
7253 if (target)
7254 return target;
7255 break;
7257 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7258 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7259 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7260 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7261 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7263 enum built_in_function lib;
7264 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7265 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7266 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7267 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7268 ignore, lib);
7269 if (target)
7270 return target;
7271 break;
7273 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7274 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7275 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7276 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7277 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7279 enum built_in_function lib;
7280 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7281 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7282 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7283 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7284 ignore, lib);
7285 if (target)
7286 return target;
7287 break;
7289 case BUILT_IN_ATOMIC_OR_FETCH_1:
7290 case BUILT_IN_ATOMIC_OR_FETCH_2:
7291 case BUILT_IN_ATOMIC_OR_FETCH_4:
7292 case BUILT_IN_ATOMIC_OR_FETCH_8:
7293 case BUILT_IN_ATOMIC_OR_FETCH_16:
7295 enum built_in_function lib;
7296 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7297 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7298 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7299 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7300 ignore, lib);
7301 if (target)
7302 return target;
7303 break;
7305 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7306 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7307 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7308 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7309 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7310 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7311 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7312 ignore, BUILT_IN_NONE);
7313 if (target)
7314 return target;
7315 break;
7317 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7318 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7319 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7320 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7321 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7322 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7323 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7324 ignore, BUILT_IN_NONE);
7325 if (target)
7326 return target;
7327 break;
7329 case BUILT_IN_ATOMIC_FETCH_AND_1:
7330 case BUILT_IN_ATOMIC_FETCH_AND_2:
7331 case BUILT_IN_ATOMIC_FETCH_AND_4:
7332 case BUILT_IN_ATOMIC_FETCH_AND_8:
7333 case BUILT_IN_ATOMIC_FETCH_AND_16:
7334 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7335 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7336 ignore, BUILT_IN_NONE);
7337 if (target)
7338 return target;
7339 break;
7341 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7342 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7343 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7344 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7345 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7346 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7347 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7348 ignore, BUILT_IN_NONE);
7349 if (target)
7350 return target;
7351 break;
7353 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7354 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7355 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7356 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7357 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7358 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7359 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7360 ignore, BUILT_IN_NONE);
7361 if (target)
7362 return target;
7363 break;
7365 case BUILT_IN_ATOMIC_FETCH_OR_1:
7366 case BUILT_IN_ATOMIC_FETCH_OR_2:
7367 case BUILT_IN_ATOMIC_FETCH_OR_4:
7368 case BUILT_IN_ATOMIC_FETCH_OR_8:
7369 case BUILT_IN_ATOMIC_FETCH_OR_16:
7370 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7371 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7372 ignore, BUILT_IN_NONE);
7373 if (target)
7374 return target;
7375 break;
7377 case BUILT_IN_ATOMIC_TEST_AND_SET:
7378 return expand_builtin_atomic_test_and_set (exp, target);
7380 case BUILT_IN_ATOMIC_CLEAR:
7381 return expand_builtin_atomic_clear (exp);
7383 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
7384 return expand_builtin_atomic_always_lock_free (exp);
7386 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
7387 target = expand_builtin_atomic_is_lock_free (exp);
7388 if (target)
7389 return target;
7390 break;
7392 case BUILT_IN_ATOMIC_THREAD_FENCE:
7393 expand_builtin_atomic_thread_fence (exp);
7394 return const0_rtx;
7396 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
7397 expand_builtin_atomic_signal_fence (exp);
7398 return const0_rtx;
7400 case BUILT_IN_OBJECT_SIZE:
7401 return expand_builtin_object_size (exp);
7403 case BUILT_IN_MEMCPY_CHK:
7404 case BUILT_IN_MEMPCPY_CHK:
7405 case BUILT_IN_MEMMOVE_CHK:
7406 case BUILT_IN_MEMSET_CHK:
7407 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7408 if (target)
7409 return target;
7410 break;
7412 case BUILT_IN_STRCPY_CHK:
7413 case BUILT_IN_STPCPY_CHK:
7414 case BUILT_IN_STRNCPY_CHK:
7415 case BUILT_IN_STPNCPY_CHK:
7416 case BUILT_IN_STRCAT_CHK:
7417 case BUILT_IN_STRNCAT_CHK:
7418 case BUILT_IN_SNPRINTF_CHK:
7419 case BUILT_IN_VSNPRINTF_CHK:
7420 maybe_emit_chk_warning (exp, fcode);
7421 break;
7423 case BUILT_IN_SPRINTF_CHK:
7424 case BUILT_IN_VSPRINTF_CHK:
7425 maybe_emit_sprintf_chk_warning (exp, fcode);
7426 break;
7428 case BUILT_IN_FREE:
7429 if (warn_free_nonheap_object)
7430 maybe_emit_free_warning (exp);
7431 break;
7433 case BUILT_IN_THREAD_POINTER:
7434 return expand_builtin_thread_pointer (exp, target);
7436 case BUILT_IN_SET_THREAD_POINTER:
7437 expand_builtin_set_thread_pointer (exp);
7438 return const0_rtx;
7440 case BUILT_IN_CILK_DETACH:
7441 expand_builtin_cilk_detach (exp);
7442 return const0_rtx;
7444 case BUILT_IN_CILK_POP_FRAME:
7445 expand_builtin_cilk_pop_frame (exp);
7446 return const0_rtx;
7448 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7449 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7450 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7451 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7452 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7453 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7454 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7455 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7456 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7457 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7458 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7459 /* We allow user CHKP builtins if Pointer Bounds
7460 Checker is off. */
7461 if (!chkp_function_instrumented_p (current_function_decl))
7463 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7464 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7465 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7466 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7467 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7468 return expand_normal (CALL_EXPR_ARG (exp, 0));
7469 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7470 return expand_normal (size_zero_node);
7471 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7472 return expand_normal (size_int (-1));
7473 else
7474 return const0_rtx;
7476 /* FALLTHROUGH */
7478 case BUILT_IN_CHKP_BNDMK:
7479 case BUILT_IN_CHKP_BNDSTX:
7480 case BUILT_IN_CHKP_BNDCL:
7481 case BUILT_IN_CHKP_BNDCU:
7482 case BUILT_IN_CHKP_BNDLDX:
7483 case BUILT_IN_CHKP_BNDRET:
7484 case BUILT_IN_CHKP_INTERSECT:
7485 case BUILT_IN_CHKP_NARROW:
7486 case BUILT_IN_CHKP_EXTRACT_LOWER:
7487 case BUILT_IN_CHKP_EXTRACT_UPPER:
7488 /* Software implementation of Pointer Bounds Checker is NYI.
7489 Target support is required. */
7490 error ("Your target platform does not support -fcheck-pointer-bounds");
7491 break;
7493 case BUILT_IN_ACC_ON_DEVICE:
7494 /* Do library call, if we failed to expand the builtin when
7495 folding. */
7496 break;
7498 default: /* just do library call, if unknown builtin */
7499 break;
7502 /* The switch statement above can drop through to cause the function
7503 to be called normally. */
7504 return expand_call (exp, target, ignore);
7507 /* Similar to expand_builtin but is used for instrumented calls. */
7510 expand_builtin_with_bounds (tree exp, rtx target,
7511 rtx subtarget ATTRIBUTE_UNUSED,
7512 machine_mode mode, int ignore)
7514 tree fndecl = get_callee_fndecl (exp);
7515 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7517 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7519 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7520 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7522 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7523 && fcode < END_CHKP_BUILTINS);
7525 switch (fcode)
7527 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7528 target = expand_builtin_memcpy_with_bounds (exp, target);
7529 if (target)
7530 return target;
7531 break;
7533 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7534 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7535 if (target)
7536 return target;
7537 break;
7539 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7540 target = expand_builtin_memset_with_bounds (exp, target, mode);
7541 if (target)
7542 return target;
7543 break;
7545 default:
7546 break;
7549 /* The switch statement above can drop through to cause the function
7550 to be called normally. */
7551 return expand_call (exp, target, ignore);
7554 /* Determine whether a tree node represents a call to a built-in
7555 function. If the tree T is a call to a built-in function with
7556 the right number of arguments of the appropriate types, return
7557 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7558 Otherwise the return value is END_BUILTINS. */
7560 enum built_in_function
7561 builtin_mathfn_code (const_tree t)
7563 const_tree fndecl, arg, parmlist;
7564 const_tree argtype, parmtype;
7565 const_call_expr_arg_iterator iter;
7567 if (TREE_CODE (t) != CALL_EXPR
7568 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7569 return END_BUILTINS;
7571 fndecl = get_callee_fndecl (t);
7572 if (fndecl == NULL_TREE
7573 || TREE_CODE (fndecl) != FUNCTION_DECL
7574 || ! DECL_BUILT_IN (fndecl)
7575 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7576 return END_BUILTINS;
7578 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7579 init_const_call_expr_arg_iterator (t, &iter);
7580 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7582 /* If a function doesn't take a variable number of arguments,
7583 the last element in the list will have type `void'. */
7584 parmtype = TREE_VALUE (parmlist);
7585 if (VOID_TYPE_P (parmtype))
7587 if (more_const_call_expr_args_p (&iter))
7588 return END_BUILTINS;
7589 return DECL_FUNCTION_CODE (fndecl);
7592 if (! more_const_call_expr_args_p (&iter))
7593 return END_BUILTINS;
7595 arg = next_const_call_expr_arg (&iter);
7596 argtype = TREE_TYPE (arg);
7598 if (SCALAR_FLOAT_TYPE_P (parmtype))
7600 if (! SCALAR_FLOAT_TYPE_P (argtype))
7601 return END_BUILTINS;
7603 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7605 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7606 return END_BUILTINS;
7608 else if (POINTER_TYPE_P (parmtype))
7610 if (! POINTER_TYPE_P (argtype))
7611 return END_BUILTINS;
7613 else if (INTEGRAL_TYPE_P (parmtype))
7615 if (! INTEGRAL_TYPE_P (argtype))
7616 return END_BUILTINS;
7618 else
7619 return END_BUILTINS;
7622 /* Variable-length argument list. */
7623 return DECL_FUNCTION_CODE (fndecl);
7626 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7627 evaluate to a constant. */
7629 static tree
7630 fold_builtin_constant_p (tree arg)
7632 /* We return 1 for a numeric type that's known to be a constant
7633 value at compile-time or for an aggregate type that's a
7634 literal constant. */
7635 STRIP_NOPS (arg);
7637 /* If we know this is a constant, emit the constant of one. */
7638 if (CONSTANT_CLASS_P (arg)
7639 || (TREE_CODE (arg) == CONSTRUCTOR
7640 && TREE_CONSTANT (arg)))
7641 return integer_one_node;
7642 if (TREE_CODE (arg) == ADDR_EXPR)
7644 tree op = TREE_OPERAND (arg, 0);
7645 if (TREE_CODE (op) == STRING_CST
7646 || (TREE_CODE (op) == ARRAY_REF
7647 && integer_zerop (TREE_OPERAND (op, 1))
7648 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7649 return integer_one_node;
7652 /* If this expression has side effects, show we don't know it to be a
7653 constant. Likewise if it's a pointer or aggregate type since in
7654 those case we only want literals, since those are only optimized
7655 when generating RTL, not later.
7656 And finally, if we are compiling an initializer, not code, we
7657 need to return a definite result now; there's not going to be any
7658 more optimization done. */
7659 if (TREE_SIDE_EFFECTS (arg)
7660 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7661 || POINTER_TYPE_P (TREE_TYPE (arg))
7662 || cfun == 0
7663 || folding_initializer
7664 || force_folding_builtin_constant_p)
7665 return integer_zero_node;
7667 return NULL_TREE;
7670 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7671 return it as a truthvalue. */
7673 static tree
7674 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7675 tree predictor)
7677 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7679 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7680 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7681 ret_type = TREE_TYPE (TREE_TYPE (fn));
7682 pred_type = TREE_VALUE (arg_types);
7683 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7685 pred = fold_convert_loc (loc, pred_type, pred);
7686 expected = fold_convert_loc (loc, expected_type, expected);
7687 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7688 predictor);
7690 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7691 build_int_cst (ret_type, 0));
7694 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7695 NULL_TREE if no simplification is possible. */
7697 tree
7698 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7700 tree inner, fndecl, inner_arg0;
7701 enum tree_code code;
7703 /* Distribute the expected value over short-circuiting operators.
7704 See through the cast from truthvalue_type_node to long. */
7705 inner_arg0 = arg0;
7706 while (CONVERT_EXPR_P (inner_arg0)
7707 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7708 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7709 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7711 /* If this is a builtin_expect within a builtin_expect keep the
7712 inner one. See through a comparison against a constant. It
7713 might have been added to create a thruthvalue. */
7714 inner = inner_arg0;
7716 if (COMPARISON_CLASS_P (inner)
7717 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7718 inner = TREE_OPERAND (inner, 0);
7720 if (TREE_CODE (inner) == CALL_EXPR
7721 && (fndecl = get_callee_fndecl (inner))
7722 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7723 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7724 return arg0;
7726 inner = inner_arg0;
7727 code = TREE_CODE (inner);
7728 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7730 tree op0 = TREE_OPERAND (inner, 0);
7731 tree op1 = TREE_OPERAND (inner, 1);
7733 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7734 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7735 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7737 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7740 /* If the argument isn't invariant then there's nothing else we can do. */
7741 if (!TREE_CONSTANT (inner_arg0))
7742 return NULL_TREE;
7744 /* If we expect that a comparison against the argument will fold to
7745 a constant return the constant. In practice, this means a true
7746 constant or the address of a non-weak symbol. */
7747 inner = inner_arg0;
7748 STRIP_NOPS (inner);
7749 if (TREE_CODE (inner) == ADDR_EXPR)
7753 inner = TREE_OPERAND (inner, 0);
7755 while (TREE_CODE (inner) == COMPONENT_REF
7756 || TREE_CODE (inner) == ARRAY_REF);
7757 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
7758 return NULL_TREE;
7761 /* Otherwise, ARG0 already has the proper type for the return value. */
7762 return arg0;
7765 /* Fold a call to __builtin_classify_type with argument ARG. */
7767 static tree
7768 fold_builtin_classify_type (tree arg)
7770 if (arg == 0)
7771 return build_int_cst (integer_type_node, no_type_class);
7773 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7776 /* Fold a call to __builtin_strlen with argument ARG. */
7778 static tree
7779 fold_builtin_strlen (location_t loc, tree type, tree arg)
7781 if (!validate_arg (arg, POINTER_TYPE))
7782 return NULL_TREE;
7783 else
7785 tree len = c_strlen (arg, 0);
7787 if (len)
7788 return fold_convert_loc (loc, type, len);
7790 return NULL_TREE;
7794 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7796 static tree
7797 fold_builtin_inf (location_t loc, tree type, int warn)
7799 REAL_VALUE_TYPE real;
7801 /* __builtin_inff is intended to be usable to define INFINITY on all
7802 targets. If an infinity is not available, INFINITY expands "to a
7803 positive constant of type float that overflows at translation
7804 time", footnote "In this case, using INFINITY will violate the
7805 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7806 Thus we pedwarn to ensure this constraint violation is
7807 diagnosed. */
7808 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7809 pedwarn (loc, 0, "target format does not support infinity");
7811 real_inf (&real);
7812 return build_real (type, real);
7815 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7816 NULL_TREE if no simplification can be made. */
7818 static tree
7819 fold_builtin_sincos (location_t loc,
7820 tree arg0, tree arg1, tree arg2)
7822 tree type;
7823 tree fndecl, call = NULL_TREE;
7825 if (!validate_arg (arg0, REAL_TYPE)
7826 || !validate_arg (arg1, POINTER_TYPE)
7827 || !validate_arg (arg2, POINTER_TYPE))
7828 return NULL_TREE;
7830 type = TREE_TYPE (arg0);
7832 /* Calculate the result when the argument is a constant. */
7833 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
7834 if (fn == END_BUILTINS)
7835 return NULL_TREE;
7837 /* Canonicalize sincos to cexpi. */
7838 if (TREE_CODE (arg0) == REAL_CST)
7840 tree complex_type = build_complex_type (type);
7841 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
7843 if (!call)
7845 if (!targetm.libc_has_function (function_c99_math_complex)
7846 || !builtin_decl_implicit_p (fn))
7847 return NULL_TREE;
7848 fndecl = builtin_decl_explicit (fn);
7849 call = build_call_expr_loc (loc, fndecl, 1, arg0);
7850 call = builtin_save_expr (call);
7853 return build2 (COMPOUND_EXPR, void_type_node,
7854 build2 (MODIFY_EXPR, void_type_node,
7855 build_fold_indirect_ref_loc (loc, arg1),
7856 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
7857 build2 (MODIFY_EXPR, void_type_node,
7858 build_fold_indirect_ref_loc (loc, arg2),
7859 fold_build1_loc (loc, REALPART_EXPR, type, call)));
7862 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
7863 Return NULL_TREE if no simplification can be made. */
7865 static tree
7866 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
7868 if (!validate_arg (arg1, POINTER_TYPE)
7869 || !validate_arg (arg2, POINTER_TYPE)
7870 || !validate_arg (len, INTEGER_TYPE))
7871 return NULL_TREE;
7873 /* If the LEN parameter is zero, return zero. */
7874 if (integer_zerop (len))
7875 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
7876 arg1, arg2);
7878 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7879 if (operand_equal_p (arg1, arg2, 0))
7880 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
7882 /* If len parameter is one, return an expression corresponding to
7883 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7884 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
7886 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7887 tree cst_uchar_ptr_node
7888 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7890 tree ind1
7891 = fold_convert_loc (loc, integer_type_node,
7892 build1 (INDIRECT_REF, cst_uchar_node,
7893 fold_convert_loc (loc,
7894 cst_uchar_ptr_node,
7895 arg1)));
7896 tree ind2
7897 = fold_convert_loc (loc, integer_type_node,
7898 build1 (INDIRECT_REF, cst_uchar_node,
7899 fold_convert_loc (loc,
7900 cst_uchar_ptr_node,
7901 arg2)));
7902 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
7905 return NULL_TREE;
7908 /* Fold a call to builtin isascii with argument ARG. */
7910 static tree
7911 fold_builtin_isascii (location_t loc, tree arg)
7913 if (!validate_arg (arg, INTEGER_TYPE))
7914 return NULL_TREE;
7915 else
7917 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7918 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
7919 build_int_cst (integer_type_node,
7920 ~ (unsigned HOST_WIDE_INT) 0x7f));
7921 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
7922 arg, integer_zero_node);
7926 /* Fold a call to builtin toascii with argument ARG. */
7928 static tree
7929 fold_builtin_toascii (location_t loc, tree arg)
7931 if (!validate_arg (arg, INTEGER_TYPE))
7932 return NULL_TREE;
7934 /* Transform toascii(c) -> (c & 0x7f). */
7935 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
7936 build_int_cst (integer_type_node, 0x7f));
7939 /* Fold a call to builtin isdigit with argument ARG. */
7941 static tree
7942 fold_builtin_isdigit (location_t loc, tree arg)
7944 if (!validate_arg (arg, INTEGER_TYPE))
7945 return NULL_TREE;
7946 else
7948 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7949 /* According to the C standard, isdigit is unaffected by locale.
7950 However, it definitely is affected by the target character set. */
7951 unsigned HOST_WIDE_INT target_digit0
7952 = lang_hooks.to_target_charset ('0');
7954 if (target_digit0 == 0)
7955 return NULL_TREE;
7957 arg = fold_convert_loc (loc, unsigned_type_node, arg);
7958 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
7959 build_int_cst (unsigned_type_node, target_digit0));
7960 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
7961 build_int_cst (unsigned_type_node, 9));
7965 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
7967 static tree
7968 fold_builtin_fabs (location_t loc, tree arg, tree type)
7970 if (!validate_arg (arg, REAL_TYPE))
7971 return NULL_TREE;
7973 arg = fold_convert_loc (loc, type, arg);
7974 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7977 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
7979 static tree
7980 fold_builtin_abs (location_t loc, tree arg, tree type)
7982 if (!validate_arg (arg, INTEGER_TYPE))
7983 return NULL_TREE;
7985 arg = fold_convert_loc (loc, type, arg);
7986 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7989 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
7991 static tree
7992 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
7994 /* ??? Only expand to FMA_EXPR if it's directly supported. */
7995 if (validate_arg (arg0, REAL_TYPE)
7996 && validate_arg (arg1, REAL_TYPE)
7997 && validate_arg (arg2, REAL_TYPE)
7998 && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
7999 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
8001 return NULL_TREE;
8004 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8006 static tree
8007 fold_builtin_carg (location_t loc, tree arg, tree type)
8009 if (validate_arg (arg, COMPLEX_TYPE)
8010 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8012 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8014 if (atan2_fn)
8016 tree new_arg = builtin_save_expr (arg);
8017 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8018 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8019 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8023 return NULL_TREE;
8026 /* Fold a call to builtin frexp, we can assume the base is 2. */
8028 static tree
8029 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8031 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8032 return NULL_TREE;
8034 STRIP_NOPS (arg0);
8036 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8037 return NULL_TREE;
8039 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8041 /* Proceed if a valid pointer type was passed in. */
8042 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8044 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8045 tree frac, exp;
8047 switch (value->cl)
8049 case rvc_zero:
8050 /* For +-0, return (*exp = 0, +-0). */
8051 exp = integer_zero_node;
8052 frac = arg0;
8053 break;
8054 case rvc_nan:
8055 case rvc_inf:
8056 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8057 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8058 case rvc_normal:
8060 /* Since the frexp function always expects base 2, and in
8061 GCC normalized significands are already in the range
8062 [0.5, 1.0), we have exactly what frexp wants. */
8063 REAL_VALUE_TYPE frac_rvt = *value;
8064 SET_REAL_EXP (&frac_rvt, 0);
8065 frac = build_real (rettype, frac_rvt);
8066 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8068 break;
8069 default:
8070 gcc_unreachable ();
8073 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8074 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8075 TREE_SIDE_EFFECTS (arg1) = 1;
8076 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8079 return NULL_TREE;
8082 /* Fold a call to builtin modf. */
8084 static tree
8085 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8087 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8088 return NULL_TREE;
8090 STRIP_NOPS (arg0);
8092 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8093 return NULL_TREE;
8095 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8097 /* Proceed if a valid pointer type was passed in. */
8098 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8100 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8101 REAL_VALUE_TYPE trunc, frac;
8103 switch (value->cl)
8105 case rvc_nan:
8106 case rvc_zero:
8107 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8108 trunc = frac = *value;
8109 break;
8110 case rvc_inf:
8111 /* For +-Inf, return (*arg1 = arg0, +-0). */
8112 frac = dconst0;
8113 frac.sign = value->sign;
8114 trunc = *value;
8115 break;
8116 case rvc_normal:
8117 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8118 real_trunc (&trunc, VOIDmode, value);
8119 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8120 /* If the original number was negative and already
8121 integral, then the fractional part is -0.0. */
8122 if (value->sign && frac.cl == rvc_zero)
8123 frac.sign = value->sign;
8124 break;
8127 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8128 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8129 build_real (rettype, trunc));
8130 TREE_SIDE_EFFECTS (arg1) = 1;
8131 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8132 build_real (rettype, frac));
8135 return NULL_TREE;
8138 /* Given a location LOC, an interclass builtin function decl FNDECL
8139 and its single argument ARG, return an folded expression computing
8140 the same, or NULL_TREE if we either couldn't or didn't want to fold
8141 (the latter happen if there's an RTL instruction available). */
8143 static tree
8144 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8146 machine_mode mode;
8148 if (!validate_arg (arg, REAL_TYPE))
8149 return NULL_TREE;
8151 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8152 return NULL_TREE;
8154 mode = TYPE_MODE (TREE_TYPE (arg));
8156 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8158 /* If there is no optab, try generic code. */
8159 switch (DECL_FUNCTION_CODE (fndecl))
8161 tree result;
8163 CASE_FLT_FN (BUILT_IN_ISINF):
8165 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8166 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8167 tree type = TREE_TYPE (arg);
8168 REAL_VALUE_TYPE r;
8169 char buf[128];
8171 if (is_ibm_extended)
8173 /* NaN and Inf are encoded in the high-order double value
8174 only. The low-order value is not significant. */
8175 type = double_type_node;
8176 mode = DFmode;
8177 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8179 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8180 real_from_string (&r, buf);
8181 result = build_call_expr (isgr_fn, 2,
8182 fold_build1_loc (loc, ABS_EXPR, type, arg),
8183 build_real (type, r));
8184 return result;
8186 CASE_FLT_FN (BUILT_IN_FINITE):
8187 case BUILT_IN_ISFINITE:
8189 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8190 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8191 tree type = TREE_TYPE (arg);
8192 REAL_VALUE_TYPE r;
8193 char buf[128];
8195 if (is_ibm_extended)
8197 /* NaN and Inf are encoded in the high-order double value
8198 only. The low-order value is not significant. */
8199 type = double_type_node;
8200 mode = DFmode;
8201 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8203 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8204 real_from_string (&r, buf);
8205 result = build_call_expr (isle_fn, 2,
8206 fold_build1_loc (loc, ABS_EXPR, type, arg),
8207 build_real (type, r));
8208 /*result = fold_build2_loc (loc, UNGT_EXPR,
8209 TREE_TYPE (TREE_TYPE (fndecl)),
8210 fold_build1_loc (loc, ABS_EXPR, type, arg),
8211 build_real (type, r));
8212 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8213 TREE_TYPE (TREE_TYPE (fndecl)),
8214 result);*/
8215 return result;
8217 case BUILT_IN_ISNORMAL:
8219 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8220 islessequal(fabs(x),DBL_MAX). */
8221 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8222 tree type = TREE_TYPE (arg);
8223 tree orig_arg, max_exp, min_exp;
8224 machine_mode orig_mode = mode;
8225 REAL_VALUE_TYPE rmax, rmin;
8226 char buf[128];
8228 orig_arg = arg = builtin_save_expr (arg);
8229 if (is_ibm_extended)
8231 /* Use double to test the normal range of IBM extended
8232 precision. Emin for IBM extended precision is
8233 different to emin for IEEE double, being 53 higher
8234 since the low double exponent is at least 53 lower
8235 than the high double exponent. */
8236 type = double_type_node;
8237 mode = DFmode;
8238 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8240 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8242 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8243 real_from_string (&rmax, buf);
8244 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8245 real_from_string (&rmin, buf);
8246 max_exp = build_real (type, rmax);
8247 min_exp = build_real (type, rmin);
8249 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8250 if (is_ibm_extended)
8252 /* Testing the high end of the range is done just using
8253 the high double, using the same test as isfinite().
8254 For the subnormal end of the range we first test the
8255 high double, then if its magnitude is equal to the
8256 limit of 0x1p-969, we test whether the low double is
8257 non-zero and opposite sign to the high double. */
8258 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8259 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8260 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8261 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8262 arg, min_exp);
8263 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8264 complex_double_type_node, orig_arg);
8265 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8266 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8267 tree zero = build_real (type, dconst0);
8268 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8269 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8270 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8271 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8272 fold_build3 (COND_EXPR,
8273 integer_type_node,
8274 hilt, logt, lolt));
8275 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8276 eq_min, ok_lo);
8277 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8278 gt_min, eq_min);
8280 else
8282 tree const isge_fn
8283 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8284 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8286 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8287 max_exp, min_exp);
8288 return result;
8290 default:
8291 break;
8294 return NULL_TREE;
8297 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8298 ARG is the argument for the call. */
8300 static tree
8301 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8303 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8305 if (!validate_arg (arg, REAL_TYPE))
8306 return NULL_TREE;
8308 switch (builtin_index)
8310 case BUILT_IN_ISINF:
8311 if (!HONOR_INFINITIES (arg))
8312 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8314 return NULL_TREE;
8316 case BUILT_IN_ISINF_SIGN:
8318 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8319 /* In a boolean context, GCC will fold the inner COND_EXPR to
8320 1. So e.g. "if (isinf_sign(x))" would be folded to just
8321 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8322 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8323 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8324 tree tmp = NULL_TREE;
8326 arg = builtin_save_expr (arg);
8328 if (signbit_fn && isinf_fn)
8330 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8331 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8333 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8334 signbit_call, integer_zero_node);
8335 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8336 isinf_call, integer_zero_node);
8338 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8339 integer_minus_one_node, integer_one_node);
8340 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8341 isinf_call, tmp,
8342 integer_zero_node);
8345 return tmp;
8348 case BUILT_IN_ISFINITE:
8349 if (!HONOR_NANS (arg)
8350 && !HONOR_INFINITIES (arg))
8351 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8353 return NULL_TREE;
8355 case BUILT_IN_ISNAN:
8356 if (!HONOR_NANS (arg))
8357 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8360 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8361 if (is_ibm_extended)
8363 /* NaN and Inf are encoded in the high-order double value
8364 only. The low-order value is not significant. */
8365 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8368 arg = builtin_save_expr (arg);
8369 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8371 default:
8372 gcc_unreachable ();
8376 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8377 This builtin will generate code to return the appropriate floating
8378 point classification depending on the value of the floating point
8379 number passed in. The possible return values must be supplied as
8380 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8381 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8382 one floating point argument which is "type generic". */
8384 static tree
8385 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8387 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8388 arg, type, res, tmp;
8389 machine_mode mode;
8390 REAL_VALUE_TYPE r;
8391 char buf[128];
8393 /* Verify the required arguments in the original call. */
8394 if (nargs != 6
8395 || !validate_arg (args[0], INTEGER_TYPE)
8396 || !validate_arg (args[1], INTEGER_TYPE)
8397 || !validate_arg (args[2], INTEGER_TYPE)
8398 || !validate_arg (args[3], INTEGER_TYPE)
8399 || !validate_arg (args[4], INTEGER_TYPE)
8400 || !validate_arg (args[5], REAL_TYPE))
8401 return NULL_TREE;
8403 fp_nan = args[0];
8404 fp_infinite = args[1];
8405 fp_normal = args[2];
8406 fp_subnormal = args[3];
8407 fp_zero = args[4];
8408 arg = args[5];
8409 type = TREE_TYPE (arg);
8410 mode = TYPE_MODE (type);
8411 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8413 /* fpclassify(x) ->
8414 isnan(x) ? FP_NAN :
8415 (fabs(x) == Inf ? FP_INFINITE :
8416 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8417 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8419 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8420 build_real (type, dconst0));
8421 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8422 tmp, fp_zero, fp_subnormal);
8424 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8425 real_from_string (&r, buf);
8426 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8427 arg, build_real (type, r));
8428 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8430 if (HONOR_INFINITIES (mode))
8432 real_inf (&r);
8433 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8434 build_real (type, r));
8435 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8436 fp_infinite, res);
8439 if (HONOR_NANS (mode))
8441 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8442 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8445 return res;
8448 /* Fold a call to an unordered comparison function such as
8449 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8450 being called and ARG0 and ARG1 are the arguments for the call.
8451 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8452 the opposite of the desired result. UNORDERED_CODE is used
8453 for modes that can hold NaNs and ORDERED_CODE is used for
8454 the rest. */
8456 static tree
8457 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
8458 enum tree_code unordered_code,
8459 enum tree_code ordered_code)
8461 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8462 enum tree_code code;
8463 tree type0, type1;
8464 enum tree_code code0, code1;
8465 tree cmp_type = NULL_TREE;
8467 type0 = TREE_TYPE (arg0);
8468 type1 = TREE_TYPE (arg1);
8470 code0 = TREE_CODE (type0);
8471 code1 = TREE_CODE (type1);
8473 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8474 /* Choose the wider of two real types. */
8475 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8476 ? type0 : type1;
8477 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8478 cmp_type = type0;
8479 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8480 cmp_type = type1;
8482 arg0 = fold_convert_loc (loc, cmp_type, arg0);
8483 arg1 = fold_convert_loc (loc, cmp_type, arg1);
8485 if (unordered_code == UNORDERED_EXPR)
8487 if (!HONOR_NANS (arg0))
8488 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
8489 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
8492 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
8493 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
8494 fold_build2_loc (loc, code, type, arg0, arg1));
8497 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8498 arithmetics if it can never overflow, or into internal functions that
8499 return both result of arithmetics and overflowed boolean flag in
8500 a complex integer result, or some other check for overflow.
8501 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
8502 checking part of that. */
8504 static tree
8505 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
8506 tree arg0, tree arg1, tree arg2)
8508 enum internal_fn ifn = IFN_LAST;
8509 /* The code of the expression corresponding to the type-generic
8510 built-in, or ERROR_MARK for the type-specific ones. */
8511 enum tree_code opcode = ERROR_MARK;
8512 bool ovf_only = false;
8514 switch (fcode)
8516 case BUILT_IN_ADD_OVERFLOW_P:
8517 ovf_only = true;
8518 /* FALLTHRU */
8519 case BUILT_IN_ADD_OVERFLOW:
8520 opcode = PLUS_EXPR;
8521 /* FALLTHRU */
8522 case BUILT_IN_SADD_OVERFLOW:
8523 case BUILT_IN_SADDL_OVERFLOW:
8524 case BUILT_IN_SADDLL_OVERFLOW:
8525 case BUILT_IN_UADD_OVERFLOW:
8526 case BUILT_IN_UADDL_OVERFLOW:
8527 case BUILT_IN_UADDLL_OVERFLOW:
8528 ifn = IFN_ADD_OVERFLOW;
8529 break;
8530 case BUILT_IN_SUB_OVERFLOW_P:
8531 ovf_only = true;
8532 /* FALLTHRU */
8533 case BUILT_IN_SUB_OVERFLOW:
8534 opcode = MINUS_EXPR;
8535 /* FALLTHRU */
8536 case BUILT_IN_SSUB_OVERFLOW:
8537 case BUILT_IN_SSUBL_OVERFLOW:
8538 case BUILT_IN_SSUBLL_OVERFLOW:
8539 case BUILT_IN_USUB_OVERFLOW:
8540 case BUILT_IN_USUBL_OVERFLOW:
8541 case BUILT_IN_USUBLL_OVERFLOW:
8542 ifn = IFN_SUB_OVERFLOW;
8543 break;
8544 case BUILT_IN_MUL_OVERFLOW_P:
8545 ovf_only = true;
8546 /* FALLTHRU */
8547 case BUILT_IN_MUL_OVERFLOW:
8548 opcode = MULT_EXPR;
8549 /* FALLTHRU */
8550 case BUILT_IN_SMUL_OVERFLOW:
8551 case BUILT_IN_SMULL_OVERFLOW:
8552 case BUILT_IN_SMULLL_OVERFLOW:
8553 case BUILT_IN_UMUL_OVERFLOW:
8554 case BUILT_IN_UMULL_OVERFLOW:
8555 case BUILT_IN_UMULLL_OVERFLOW:
8556 ifn = IFN_MUL_OVERFLOW;
8557 break;
8558 default:
8559 gcc_unreachable ();
8562 /* For the "generic" overloads, the first two arguments can have different
8563 types and the last argument determines the target type to use to check
8564 for overflow. The arguments of the other overloads all have the same
8565 type. */
8566 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
8568 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
8569 arguments are constant, attempt to fold the built-in call into a constant
8570 expression indicating whether or not it detected an overflow. */
8571 if (ovf_only
8572 && TREE_CODE (arg0) == INTEGER_CST
8573 && TREE_CODE (arg1) == INTEGER_CST)
8574 /* Perform the computation in the target type and check for overflow. */
8575 return omit_one_operand_loc (loc, boolean_type_node,
8576 arith_overflowed_p (opcode, type, arg0, arg1)
8577 ? boolean_true_node : boolean_false_node,
8578 arg2);
8580 tree ctype = build_complex_type (type);
8581 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
8582 2, arg0, arg1);
8583 tree tgt = save_expr (call);
8584 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
8585 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
8586 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
8588 if (ovf_only)
8589 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
8591 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
8592 tree store
8593 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
8594 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
8597 /* Fold a call to __builtin_FILE to a constant string. */
8599 static inline tree
8600 fold_builtin_FILE (location_t loc)
8602 if (const char *fname = LOCATION_FILE (loc))
8603 return build_string_literal (strlen (fname) + 1, fname);
8605 return build_string_literal (1, "");
8608 /* Fold a call to __builtin_FUNCTION to a constant string. */
8610 static inline tree
8611 fold_builtin_FUNCTION ()
8613 if (current_function_decl)
8615 const char *name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
8616 return build_string_literal (strlen (name) + 1, name);
8619 return build_string_literal (1, "");
8622 /* Fold a call to __builtin_LINE to an integer constant. */
8624 static inline tree
8625 fold_builtin_LINE (location_t loc, tree type)
8627 return build_int_cst (type, LOCATION_LINE (loc));
8630 /* Fold a call to built-in function FNDECL with 0 arguments.
8631 This function returns NULL_TREE if no simplification was possible. */
8633 static tree
8634 fold_builtin_0 (location_t loc, tree fndecl)
8636 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8637 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8638 switch (fcode)
8640 case BUILT_IN_FILE:
8641 return fold_builtin_FILE (loc);
8643 case BUILT_IN_FUNCTION:
8644 return fold_builtin_FUNCTION ();
8646 case BUILT_IN_LINE:
8647 return fold_builtin_LINE (loc, type);
8649 CASE_FLT_FN (BUILT_IN_INF):
8650 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
8651 case BUILT_IN_INFD32:
8652 case BUILT_IN_INFD64:
8653 case BUILT_IN_INFD128:
8654 return fold_builtin_inf (loc, type, true);
8656 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
8657 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
8658 return fold_builtin_inf (loc, type, false);
8660 case BUILT_IN_CLASSIFY_TYPE:
8661 return fold_builtin_classify_type (NULL_TREE);
8663 default:
8664 break;
8666 return NULL_TREE;
8669 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
8670 This function returns NULL_TREE if no simplification was possible. */
8672 static tree
8673 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
8675 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8676 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8678 if (TREE_CODE (arg0) == ERROR_MARK)
8679 return NULL_TREE;
8681 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
8682 return ret;
8684 switch (fcode)
8686 case BUILT_IN_CONSTANT_P:
8688 tree val = fold_builtin_constant_p (arg0);
8690 /* Gimplification will pull the CALL_EXPR for the builtin out of
8691 an if condition. When not optimizing, we'll not CSE it back.
8692 To avoid link error types of regressions, return false now. */
8693 if (!val && !optimize)
8694 val = integer_zero_node;
8696 return val;
8699 case BUILT_IN_CLASSIFY_TYPE:
8700 return fold_builtin_classify_type (arg0);
8702 case BUILT_IN_STRLEN:
8703 return fold_builtin_strlen (loc, type, arg0);
8705 CASE_FLT_FN (BUILT_IN_FABS):
8706 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
8707 case BUILT_IN_FABSD32:
8708 case BUILT_IN_FABSD64:
8709 case BUILT_IN_FABSD128:
8710 return fold_builtin_fabs (loc, arg0, type);
8712 case BUILT_IN_ABS:
8713 case BUILT_IN_LABS:
8714 case BUILT_IN_LLABS:
8715 case BUILT_IN_IMAXABS:
8716 return fold_builtin_abs (loc, arg0, type);
8718 CASE_FLT_FN (BUILT_IN_CONJ):
8719 if (validate_arg (arg0, COMPLEX_TYPE)
8720 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8721 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
8722 break;
8724 CASE_FLT_FN (BUILT_IN_CREAL):
8725 if (validate_arg (arg0, COMPLEX_TYPE)
8726 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8727 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
8728 break;
8730 CASE_FLT_FN (BUILT_IN_CIMAG):
8731 if (validate_arg (arg0, COMPLEX_TYPE)
8732 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8733 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
8734 break;
8736 CASE_FLT_FN (BUILT_IN_CARG):
8737 return fold_builtin_carg (loc, arg0, type);
8739 case BUILT_IN_ISASCII:
8740 return fold_builtin_isascii (loc, arg0);
8742 case BUILT_IN_TOASCII:
8743 return fold_builtin_toascii (loc, arg0);
8745 case BUILT_IN_ISDIGIT:
8746 return fold_builtin_isdigit (loc, arg0);
8748 CASE_FLT_FN (BUILT_IN_FINITE):
8749 case BUILT_IN_FINITED32:
8750 case BUILT_IN_FINITED64:
8751 case BUILT_IN_FINITED128:
8752 case BUILT_IN_ISFINITE:
8754 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
8755 if (ret)
8756 return ret;
8757 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8760 CASE_FLT_FN (BUILT_IN_ISINF):
8761 case BUILT_IN_ISINFD32:
8762 case BUILT_IN_ISINFD64:
8763 case BUILT_IN_ISINFD128:
8765 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
8766 if (ret)
8767 return ret;
8768 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8771 case BUILT_IN_ISNORMAL:
8772 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8774 case BUILT_IN_ISINF_SIGN:
8775 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
8777 CASE_FLT_FN (BUILT_IN_ISNAN):
8778 case BUILT_IN_ISNAND32:
8779 case BUILT_IN_ISNAND64:
8780 case BUILT_IN_ISNAND128:
8781 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
8783 case BUILT_IN_FREE:
8784 if (integer_zerop (arg0))
8785 return build_empty_stmt (loc);
8786 break;
8788 default:
8789 break;
8792 return NULL_TREE;
8796 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
8797 This function returns NULL_TREE if no simplification was possible. */
8799 static tree
8800 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
8802 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8803 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8805 if (TREE_CODE (arg0) == ERROR_MARK
8806 || TREE_CODE (arg1) == ERROR_MARK)
8807 return NULL_TREE;
8809 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
8810 return ret;
8812 switch (fcode)
8814 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
8815 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
8816 if (validate_arg (arg0, REAL_TYPE)
8817 && validate_arg (arg1, POINTER_TYPE))
8818 return do_mpfr_lgamma_r (arg0, arg1, type);
8819 break;
8821 CASE_FLT_FN (BUILT_IN_FREXP):
8822 return fold_builtin_frexp (loc, arg0, arg1, type);
8824 CASE_FLT_FN (BUILT_IN_MODF):
8825 return fold_builtin_modf (loc, arg0, arg1, type);
8827 case BUILT_IN_STRSPN:
8828 return fold_builtin_strspn (loc, arg0, arg1);
8830 case BUILT_IN_STRCSPN:
8831 return fold_builtin_strcspn (loc, arg0, arg1);
8833 case BUILT_IN_STRPBRK:
8834 return fold_builtin_strpbrk (loc, arg0, arg1, type);
8836 case BUILT_IN_EXPECT:
8837 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
8839 case BUILT_IN_ISGREATER:
8840 return fold_builtin_unordered_cmp (loc, fndecl,
8841 arg0, arg1, UNLE_EXPR, LE_EXPR);
8842 case BUILT_IN_ISGREATEREQUAL:
8843 return fold_builtin_unordered_cmp (loc, fndecl,
8844 arg0, arg1, UNLT_EXPR, LT_EXPR);
8845 case BUILT_IN_ISLESS:
8846 return fold_builtin_unordered_cmp (loc, fndecl,
8847 arg0, arg1, UNGE_EXPR, GE_EXPR);
8848 case BUILT_IN_ISLESSEQUAL:
8849 return fold_builtin_unordered_cmp (loc, fndecl,
8850 arg0, arg1, UNGT_EXPR, GT_EXPR);
8851 case BUILT_IN_ISLESSGREATER:
8852 return fold_builtin_unordered_cmp (loc, fndecl,
8853 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
8854 case BUILT_IN_ISUNORDERED:
8855 return fold_builtin_unordered_cmp (loc, fndecl,
8856 arg0, arg1, UNORDERED_EXPR,
8857 NOP_EXPR);
8859 /* We do the folding for va_start in the expander. */
8860 case BUILT_IN_VA_START:
8861 break;
8863 case BUILT_IN_OBJECT_SIZE:
8864 return fold_builtin_object_size (arg0, arg1);
8866 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8867 return fold_builtin_atomic_always_lock_free (arg0, arg1);
8869 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8870 return fold_builtin_atomic_is_lock_free (arg0, arg1);
8872 default:
8873 break;
8875 return NULL_TREE;
8878 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
8879 and ARG2.
8880 This function returns NULL_TREE if no simplification was possible. */
8882 static tree
8883 fold_builtin_3 (location_t loc, tree fndecl,
8884 tree arg0, tree arg1, tree arg2)
8886 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8887 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8889 if (TREE_CODE (arg0) == ERROR_MARK
8890 || TREE_CODE (arg1) == ERROR_MARK
8891 || TREE_CODE (arg2) == ERROR_MARK)
8892 return NULL_TREE;
8894 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
8895 arg0, arg1, arg2))
8896 return ret;
8898 switch (fcode)
8901 CASE_FLT_FN (BUILT_IN_SINCOS):
8902 return fold_builtin_sincos (loc, arg0, arg1, arg2);
8904 CASE_FLT_FN (BUILT_IN_FMA):
8905 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
8907 CASE_FLT_FN (BUILT_IN_REMQUO):
8908 if (validate_arg (arg0, REAL_TYPE)
8909 && validate_arg (arg1, REAL_TYPE)
8910 && validate_arg (arg2, POINTER_TYPE))
8911 return do_mpfr_remquo (arg0, arg1, arg2);
8912 break;
8914 case BUILT_IN_BCMP:
8915 case BUILT_IN_MEMCMP:
8916 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
8918 case BUILT_IN_EXPECT:
8919 return fold_builtin_expect (loc, arg0, arg1, arg2);
8921 case BUILT_IN_ADD_OVERFLOW:
8922 case BUILT_IN_SUB_OVERFLOW:
8923 case BUILT_IN_MUL_OVERFLOW:
8924 case BUILT_IN_ADD_OVERFLOW_P:
8925 case BUILT_IN_SUB_OVERFLOW_P:
8926 case BUILT_IN_MUL_OVERFLOW_P:
8927 case BUILT_IN_SADD_OVERFLOW:
8928 case BUILT_IN_SADDL_OVERFLOW:
8929 case BUILT_IN_SADDLL_OVERFLOW:
8930 case BUILT_IN_SSUB_OVERFLOW:
8931 case BUILT_IN_SSUBL_OVERFLOW:
8932 case BUILT_IN_SSUBLL_OVERFLOW:
8933 case BUILT_IN_SMUL_OVERFLOW:
8934 case BUILT_IN_SMULL_OVERFLOW:
8935 case BUILT_IN_SMULLL_OVERFLOW:
8936 case BUILT_IN_UADD_OVERFLOW:
8937 case BUILT_IN_UADDL_OVERFLOW:
8938 case BUILT_IN_UADDLL_OVERFLOW:
8939 case BUILT_IN_USUB_OVERFLOW:
8940 case BUILT_IN_USUBL_OVERFLOW:
8941 case BUILT_IN_USUBLL_OVERFLOW:
8942 case BUILT_IN_UMUL_OVERFLOW:
8943 case BUILT_IN_UMULL_OVERFLOW:
8944 case BUILT_IN_UMULLL_OVERFLOW:
8945 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
8947 default:
8948 break;
8950 return NULL_TREE;
8953 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
8954 arguments. IGNORE is true if the result of the
8955 function call is ignored. This function returns NULL_TREE if no
8956 simplification was possible. */
8958 tree
8959 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
8961 tree ret = NULL_TREE;
8963 switch (nargs)
8965 case 0:
8966 ret = fold_builtin_0 (loc, fndecl);
8967 break;
8968 case 1:
8969 ret = fold_builtin_1 (loc, fndecl, args[0]);
8970 break;
8971 case 2:
8972 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
8973 break;
8974 case 3:
8975 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
8976 break;
8977 default:
8978 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
8979 break;
8981 if (ret)
8983 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
8984 SET_EXPR_LOCATION (ret, loc);
8985 TREE_NO_WARNING (ret) = 1;
8986 return ret;
8988 return NULL_TREE;
8991 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
8992 list ARGS along with N new arguments in NEWARGS. SKIP is the number
8993 of arguments in ARGS to be omitted. OLDNARGS is the number of
8994 elements in ARGS. */
8996 static tree
8997 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
8998 int skip, tree fndecl, int n, va_list newargs)
9000 int nargs = oldnargs - skip + n;
9001 tree *buffer;
9003 if (n > 0)
9005 int i, j;
9007 buffer = XALLOCAVEC (tree, nargs);
9008 for (i = 0; i < n; i++)
9009 buffer[i] = va_arg (newargs, tree);
9010 for (j = skip; j < oldnargs; j++, i++)
9011 buffer[i] = args[j];
9013 else
9014 buffer = args + skip;
9016 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9019 /* Return true if FNDECL shouldn't be folded right now.
9020 If a built-in function has an inline attribute always_inline
9021 wrapper, defer folding it after always_inline functions have
9022 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9023 might not be performed. */
9025 bool
9026 avoid_folding_inline_builtin (tree fndecl)
9028 return (DECL_DECLARED_INLINE_P (fndecl)
9029 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9030 && cfun
9031 && !cfun->always_inline_functions_inlined
9032 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9035 /* A wrapper function for builtin folding that prevents warnings for
9036 "statement without effect" and the like, caused by removing the
9037 call node earlier than the warning is generated. */
9039 tree
9040 fold_call_expr (location_t loc, tree exp, bool ignore)
9042 tree ret = NULL_TREE;
9043 tree fndecl = get_callee_fndecl (exp);
9044 if (fndecl
9045 && TREE_CODE (fndecl) == FUNCTION_DECL
9046 && DECL_BUILT_IN (fndecl)
9047 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9048 yet. Defer folding until we see all the arguments
9049 (after inlining). */
9050 && !CALL_EXPR_VA_ARG_PACK (exp))
9052 int nargs = call_expr_nargs (exp);
9054 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9055 instead last argument is __builtin_va_arg_pack (). Defer folding
9056 even in that case, until arguments are finalized. */
9057 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9059 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9060 if (fndecl2
9061 && TREE_CODE (fndecl2) == FUNCTION_DECL
9062 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9063 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9064 return NULL_TREE;
9067 if (avoid_folding_inline_builtin (fndecl))
9068 return NULL_TREE;
9070 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9071 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9072 CALL_EXPR_ARGP (exp), ignore);
9073 else
9075 tree *args = CALL_EXPR_ARGP (exp);
9076 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9077 if (ret)
9078 return ret;
9081 return NULL_TREE;
9084 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9085 N arguments are passed in the array ARGARRAY. Return a folded
9086 expression or NULL_TREE if no simplification was possible. */
9088 tree
9089 fold_builtin_call_array (location_t loc, tree,
9090 tree fn,
9091 int n,
9092 tree *argarray)
9094 if (TREE_CODE (fn) != ADDR_EXPR)
9095 return NULL_TREE;
9097 tree fndecl = TREE_OPERAND (fn, 0);
9098 if (TREE_CODE (fndecl) == FUNCTION_DECL
9099 && DECL_BUILT_IN (fndecl))
9101 /* If last argument is __builtin_va_arg_pack (), arguments to this
9102 function are not finalized yet. Defer folding until they are. */
9103 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9105 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9106 if (fndecl2
9107 && TREE_CODE (fndecl2) == FUNCTION_DECL
9108 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9109 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9110 return NULL_TREE;
9112 if (avoid_folding_inline_builtin (fndecl))
9113 return NULL_TREE;
9114 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9115 return targetm.fold_builtin (fndecl, n, argarray, false);
9116 else
9117 return fold_builtin_n (loc, fndecl, argarray, n, false);
9120 return NULL_TREE;
9123 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9124 along with N new arguments specified as the "..." parameters. SKIP
9125 is the number of arguments in EXP to be omitted. This function is used
9126 to do varargs-to-varargs transformations. */
9128 static tree
9129 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9131 va_list ap;
9132 tree t;
9134 va_start (ap, n);
9135 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9136 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9137 va_end (ap);
9139 return t;
9142 /* Validate a single argument ARG against a tree code CODE representing
9143 a type. Return true when argument is valid. */
9145 static bool
9146 validate_arg (const_tree arg, enum tree_code code)
9148 if (!arg)
9149 return false;
9150 else if (code == POINTER_TYPE)
9151 return POINTER_TYPE_P (TREE_TYPE (arg));
9152 else if (code == INTEGER_TYPE)
9153 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9154 return code == TREE_CODE (TREE_TYPE (arg));
9157 /* This function validates the types of a function call argument list
9158 against a specified list of tree_codes. If the last specifier is a 0,
9159 that represents an ellipses, otherwise the last specifier must be a
9160 VOID_TYPE.
9162 This is the GIMPLE version of validate_arglist. Eventually we want to
9163 completely convert builtins.c to work from GIMPLEs and the tree based
9164 validate_arglist will then be removed. */
9166 bool
9167 validate_gimple_arglist (const gcall *call, ...)
9169 enum tree_code code;
9170 bool res = 0;
9171 va_list ap;
9172 const_tree arg;
9173 size_t i;
9175 va_start (ap, call);
9176 i = 0;
9180 code = (enum tree_code) va_arg (ap, int);
9181 switch (code)
9183 case 0:
9184 /* This signifies an ellipses, any further arguments are all ok. */
9185 res = true;
9186 goto end;
9187 case VOID_TYPE:
9188 /* This signifies an endlink, if no arguments remain, return
9189 true, otherwise return false. */
9190 res = (i == gimple_call_num_args (call));
9191 goto end;
9192 default:
9193 /* If no parameters remain or the parameter's code does not
9194 match the specified code, return false. Otherwise continue
9195 checking any remaining arguments. */
9196 arg = gimple_call_arg (call, i++);
9197 if (!validate_arg (arg, code))
9198 goto end;
9199 break;
9202 while (1);
9204 /* We need gotos here since we can only have one VA_CLOSE in a
9205 function. */
9206 end: ;
9207 va_end (ap);
9209 return res;
9212 /* Default target-specific builtin expander that does nothing. */
9215 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9216 rtx target ATTRIBUTE_UNUSED,
9217 rtx subtarget ATTRIBUTE_UNUSED,
9218 machine_mode mode ATTRIBUTE_UNUSED,
9219 int ignore ATTRIBUTE_UNUSED)
9221 return NULL_RTX;
9224 /* Returns true is EXP represents data that would potentially reside
9225 in a readonly section. */
9227 bool
9228 readonly_data_expr (tree exp)
9230 STRIP_NOPS (exp);
9232 if (TREE_CODE (exp) != ADDR_EXPR)
9233 return false;
9235 exp = get_base_address (TREE_OPERAND (exp, 0));
9236 if (!exp)
9237 return false;
9239 /* Make sure we call decl_readonly_section only for trees it
9240 can handle (since it returns true for everything it doesn't
9241 understand). */
9242 if (TREE_CODE (exp) == STRING_CST
9243 || TREE_CODE (exp) == CONSTRUCTOR
9244 || (VAR_P (exp) && TREE_STATIC (exp)))
9245 return decl_readonly_section (exp, 0);
9246 else
9247 return false;
9250 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9251 to the call, and TYPE is its return type.
9253 Return NULL_TREE if no simplification was possible, otherwise return the
9254 simplified form of the call as a tree.
9256 The simplified form may be a constant or other expression which
9257 computes the same value, but in a more efficient manner (including
9258 calls to other builtin functions).
9260 The call may contain arguments which need to be evaluated, but
9261 which are not useful to determine the result of the call. In
9262 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9263 COMPOUND_EXPR will be an argument which must be evaluated.
9264 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9265 COMPOUND_EXPR in the chain will contain the tree for the simplified
9266 form of the builtin function call. */
9268 static tree
9269 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
9271 if (!validate_arg (s1, POINTER_TYPE)
9272 || !validate_arg (s2, POINTER_TYPE))
9273 return NULL_TREE;
9274 else
9276 tree fn;
9277 const char *p1, *p2;
9279 p2 = c_getstr (s2);
9280 if (p2 == NULL)
9281 return NULL_TREE;
9283 p1 = c_getstr (s1);
9284 if (p1 != NULL)
9286 const char *r = strpbrk (p1, p2);
9287 tree tem;
9289 if (r == NULL)
9290 return build_int_cst (TREE_TYPE (s1), 0);
9292 /* Return an offset into the constant string argument. */
9293 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9294 return fold_convert_loc (loc, type, tem);
9297 if (p2[0] == '\0')
9298 /* strpbrk(x, "") == NULL.
9299 Evaluate and ignore s1 in case it had side-effects. */
9300 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
9302 if (p2[1] != '\0')
9303 return NULL_TREE; /* Really call strpbrk. */
9305 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9306 if (!fn)
9307 return NULL_TREE;
9309 /* New argument list transforming strpbrk(s1, s2) to
9310 strchr(s1, s2[0]). */
9311 return build_call_expr_loc (loc, fn, 2, s1,
9312 build_int_cst (integer_type_node, p2[0]));
9316 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9317 to the call.
9319 Return NULL_TREE if no simplification was possible, otherwise return the
9320 simplified form of the call as a tree.
9322 The simplified form may be a constant or other expression which
9323 computes the same value, but in a more efficient manner (including
9324 calls to other builtin functions).
9326 The call may contain arguments which need to be evaluated, but
9327 which are not useful to determine the result of the call. In
9328 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9329 COMPOUND_EXPR will be an argument which must be evaluated.
9330 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9331 COMPOUND_EXPR in the chain will contain the tree for the simplified
9332 form of the builtin function call. */
9334 static tree
9335 fold_builtin_strspn (location_t loc, tree s1, tree s2)
9337 if (!validate_arg (s1, POINTER_TYPE)
9338 || !validate_arg (s2, POINTER_TYPE))
9339 return NULL_TREE;
9340 else
9342 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9344 /* If either argument is "", return NULL_TREE. */
9345 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9346 /* Evaluate and ignore both arguments in case either one has
9347 side-effects. */
9348 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9349 s1, s2);
9350 return NULL_TREE;
9354 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9355 to the call.
9357 Return NULL_TREE if no simplification was possible, otherwise return the
9358 simplified form of the call as a tree.
9360 The simplified form may be a constant or other expression which
9361 computes the same value, but in a more efficient manner (including
9362 calls to other builtin functions).
9364 The call may contain arguments which need to be evaluated, but
9365 which are not useful to determine the result of the call. In
9366 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9367 COMPOUND_EXPR will be an argument which must be evaluated.
9368 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9369 COMPOUND_EXPR in the chain will contain the tree for the simplified
9370 form of the builtin function call. */
9372 static tree
9373 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
9375 if (!validate_arg (s1, POINTER_TYPE)
9376 || !validate_arg (s2, POINTER_TYPE))
9377 return NULL_TREE;
9378 else
9380 /* If the first argument is "", return NULL_TREE. */
9381 const char *p1 = c_getstr (s1);
9382 if (p1 && *p1 == '\0')
9384 /* Evaluate and ignore argument s2 in case it has
9385 side-effects. */
9386 return omit_one_operand_loc (loc, size_type_node,
9387 size_zero_node, s2);
9390 /* If the second argument is "", return __builtin_strlen(s1). */
9391 const char *p2 = c_getstr (s2);
9392 if (p2 && *p2 == '\0')
9394 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9396 /* If the replacement _DECL isn't initialized, don't do the
9397 transformation. */
9398 if (!fn)
9399 return NULL_TREE;
9401 return build_call_expr_loc (loc, fn, 1, s1);
9403 return NULL_TREE;
9407 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9408 produced. False otherwise. This is done so that we don't output the error
9409 or warning twice or three times. */
9411 bool
9412 fold_builtin_next_arg (tree exp, bool va_start_p)
9414 tree fntype = TREE_TYPE (current_function_decl);
9415 int nargs = call_expr_nargs (exp);
9416 tree arg;
9417 /* There is good chance the current input_location points inside the
9418 definition of the va_start macro (perhaps on the token for
9419 builtin) in a system header, so warnings will not be emitted.
9420 Use the location in real source code. */
9421 source_location current_location =
9422 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9423 NULL);
9425 if (!stdarg_p (fntype))
9427 error ("%<va_start%> used in function with fixed args");
9428 return true;
9431 if (va_start_p)
9433 if (va_start_p && (nargs != 2))
9435 error ("wrong number of arguments to function %<va_start%>");
9436 return true;
9438 arg = CALL_EXPR_ARG (exp, 1);
9440 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9441 when we checked the arguments and if needed issued a warning. */
9442 else
9444 if (nargs == 0)
9446 /* Evidently an out of date version of <stdarg.h>; can't validate
9447 va_start's second argument, but can still work as intended. */
9448 warning_at (current_location,
9449 OPT_Wvarargs,
9450 "%<__builtin_next_arg%> called without an argument");
9451 return true;
9453 else if (nargs > 1)
9455 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9456 return true;
9458 arg = CALL_EXPR_ARG (exp, 0);
9461 if (TREE_CODE (arg) == SSA_NAME)
9462 arg = SSA_NAME_VAR (arg);
9464 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9465 or __builtin_next_arg (0) the first time we see it, after checking
9466 the arguments and if needed issuing a warning. */
9467 if (!integer_zerop (arg))
9469 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9471 /* Strip off all nops for the sake of the comparison. This
9472 is not quite the same as STRIP_NOPS. It does more.
9473 We must also strip off INDIRECT_EXPR for C++ reference
9474 parameters. */
9475 while (CONVERT_EXPR_P (arg)
9476 || TREE_CODE (arg) == INDIRECT_REF)
9477 arg = TREE_OPERAND (arg, 0);
9478 if (arg != last_parm)
9480 /* FIXME: Sometimes with the tree optimizers we can get the
9481 not the last argument even though the user used the last
9482 argument. We just warn and set the arg to be the last
9483 argument so that we will get wrong-code because of
9484 it. */
9485 warning_at (current_location,
9486 OPT_Wvarargs,
9487 "second parameter of %<va_start%> not last named argument");
9490 /* Undefined by C99 7.15.1.4p4 (va_start):
9491 "If the parameter parmN is declared with the register storage
9492 class, with a function or array type, or with a type that is
9493 not compatible with the type that results after application of
9494 the default argument promotions, the behavior is undefined."
9496 else if (DECL_REGISTER (arg))
9498 warning_at (current_location,
9499 OPT_Wvarargs,
9500 "undefined behavior when second parameter of "
9501 "%<va_start%> is declared with %<register%> storage");
9504 /* We want to verify the second parameter just once before the tree
9505 optimizers are run and then avoid keeping it in the tree,
9506 as otherwise we could warn even for correct code like:
9507 void foo (int i, ...)
9508 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9509 if (va_start_p)
9510 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
9511 else
9512 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
9514 return false;
9518 /* Expand a call EXP to __builtin_object_size. */
9520 static rtx
9521 expand_builtin_object_size (tree exp)
9523 tree ost;
9524 int object_size_type;
9525 tree fndecl = get_callee_fndecl (exp);
9527 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9529 error ("%Kfirst argument of %D must be a pointer, second integer constant",
9530 exp, fndecl);
9531 expand_builtin_trap ();
9532 return const0_rtx;
9535 ost = CALL_EXPR_ARG (exp, 1);
9536 STRIP_NOPS (ost);
9538 if (TREE_CODE (ost) != INTEGER_CST
9539 || tree_int_cst_sgn (ost) < 0
9540 || compare_tree_int (ost, 3) > 0)
9542 error ("%Klast argument of %D is not integer constant between 0 and 3",
9543 exp, fndecl);
9544 expand_builtin_trap ();
9545 return const0_rtx;
9548 object_size_type = tree_to_shwi (ost);
9550 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9553 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9554 FCODE is the BUILT_IN_* to use.
9555 Return NULL_RTX if we failed; the caller should emit a normal call,
9556 otherwise try to get the result in TARGET, if convenient (and in
9557 mode MODE if that's convenient). */
9559 static rtx
9560 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
9561 enum built_in_function fcode)
9563 tree dest, src, len, size;
9565 if (!validate_arglist (exp,
9566 POINTER_TYPE,
9567 fcode == BUILT_IN_MEMSET_CHK
9568 ? INTEGER_TYPE : POINTER_TYPE,
9569 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
9570 return NULL_RTX;
9572 dest = CALL_EXPR_ARG (exp, 0);
9573 src = CALL_EXPR_ARG (exp, 1);
9574 len = CALL_EXPR_ARG (exp, 2);
9575 size = CALL_EXPR_ARG (exp, 3);
9577 bool sizes_ok = check_sizes (OPT_Wstringop_overflow_,
9578 exp, len, /*maxlen=*/NULL_TREE,
9579 /*str=*/NULL_TREE, size);
9581 if (!tree_fits_uhwi_p (size))
9582 return NULL_RTX;
9584 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
9586 /* Avoid transforming the checking call to an ordinary one when
9587 an overflow has been detected or when the call couldn't be
9588 validated because the size is not constant. */
9589 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
9590 return NULL_RTX;
9592 tree fn = NULL_TREE;
9593 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9594 mem{cpy,pcpy,move,set} is available. */
9595 switch (fcode)
9597 case BUILT_IN_MEMCPY_CHK:
9598 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
9599 break;
9600 case BUILT_IN_MEMPCPY_CHK:
9601 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
9602 break;
9603 case BUILT_IN_MEMMOVE_CHK:
9604 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9605 break;
9606 case BUILT_IN_MEMSET_CHK:
9607 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9608 break;
9609 default:
9610 break;
9613 if (! fn)
9614 return NULL_RTX;
9616 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9617 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9618 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9619 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9621 else if (fcode == BUILT_IN_MEMSET_CHK)
9622 return NULL_RTX;
9623 else
9625 unsigned int dest_align = get_pointer_alignment (dest);
9627 /* If DEST is not a pointer type, call the normal function. */
9628 if (dest_align == 0)
9629 return NULL_RTX;
9631 /* If SRC and DEST are the same (and not volatile), do nothing. */
9632 if (operand_equal_p (src, dest, 0))
9634 tree expr;
9636 if (fcode != BUILT_IN_MEMPCPY_CHK)
9638 /* Evaluate and ignore LEN in case it has side-effects. */
9639 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9640 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9643 expr = fold_build_pointer_plus (dest, len);
9644 return expand_expr (expr, target, mode, EXPAND_NORMAL);
9647 /* __memmove_chk special case. */
9648 if (fcode == BUILT_IN_MEMMOVE_CHK)
9650 unsigned int src_align = get_pointer_alignment (src);
9652 if (src_align == 0)
9653 return NULL_RTX;
9655 /* If src is categorized for a readonly section we can use
9656 normal __memcpy_chk. */
9657 if (readonly_data_expr (src))
9659 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
9660 if (!fn)
9661 return NULL_RTX;
9662 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
9663 dest, src, len, size);
9664 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9665 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9666 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9669 return NULL_RTX;
9673 /* Emit warning if a buffer overflow is detected at compile time. */
9675 static void
9676 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
9678 /* The source string. */
9679 tree srcstr = NULL_TREE;
9680 /* The size of the destination object. */
9681 tree objsize = NULL_TREE;
9682 /* The string that is being concatenated with (as in __strcat_chk)
9683 or null if it isn't. */
9684 tree catstr = NULL_TREE;
9685 /* The maximum length of the source sequence in a bounded operation
9686 (such as __strncat_chk) or null if the operation isn't bounded
9687 (such as __strcat_chk). */
9688 tree maxlen = NULL_TREE;
9690 switch (fcode)
9692 case BUILT_IN_STRCPY_CHK:
9693 case BUILT_IN_STPCPY_CHK:
9694 srcstr = CALL_EXPR_ARG (exp, 1);
9695 objsize = CALL_EXPR_ARG (exp, 2);
9696 break;
9698 case BUILT_IN_STRCAT_CHK:
9699 /* For __strcat_chk the warning will be emitted only if overflowing
9700 by at least strlen (dest) + 1 bytes. */
9701 catstr = CALL_EXPR_ARG (exp, 0);
9702 srcstr = CALL_EXPR_ARG (exp, 1);
9703 objsize = CALL_EXPR_ARG (exp, 2);
9704 break;
9706 case BUILT_IN_STRNCAT_CHK:
9707 catstr = CALL_EXPR_ARG (exp, 0);
9708 srcstr = CALL_EXPR_ARG (exp, 1);
9709 maxlen = CALL_EXPR_ARG (exp, 2);
9710 objsize = CALL_EXPR_ARG (exp, 3);
9711 break;
9713 case BUILT_IN_STRNCPY_CHK:
9714 case BUILT_IN_STPNCPY_CHK:
9715 srcstr = CALL_EXPR_ARG (exp, 1);
9716 maxlen = CALL_EXPR_ARG (exp, 2);
9717 objsize = CALL_EXPR_ARG (exp, 3);
9718 break;
9720 case BUILT_IN_SNPRINTF_CHK:
9721 case BUILT_IN_VSNPRINTF_CHK:
9722 maxlen = CALL_EXPR_ARG (exp, 1);
9723 objsize = CALL_EXPR_ARG (exp, 3);
9724 break;
9725 default:
9726 gcc_unreachable ();
9729 if (catstr && maxlen)
9731 /* Check __strncat_chk. There is no way to determine the length
9732 of the string to which the source string is being appended so
9733 just warn when the length of the source string is not known. */
9734 if (!check_strncat_sizes (exp, objsize))
9735 return;
9738 check_sizes (OPT_Wstringop_overflow_, exp,
9739 /*size=*/NULL_TREE, maxlen, srcstr, objsize);
9742 /* Emit warning if a buffer overflow is detected at compile time
9743 in __sprintf_chk/__vsprintf_chk calls. */
9745 static void
9746 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
9748 tree size, len, fmt;
9749 const char *fmt_str;
9750 int nargs = call_expr_nargs (exp);
9752 /* Verify the required arguments in the original call. */
9754 if (nargs < 4)
9755 return;
9756 size = CALL_EXPR_ARG (exp, 2);
9757 fmt = CALL_EXPR_ARG (exp, 3);
9759 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
9760 return;
9762 /* Check whether the format is a literal string constant. */
9763 fmt_str = c_getstr (fmt);
9764 if (fmt_str == NULL)
9765 return;
9767 if (!init_target_chars ())
9768 return;
9770 /* If the format doesn't contain % args or %%, we know its size. */
9771 if (strchr (fmt_str, target_percent) == 0)
9772 len = build_int_cstu (size_type_node, strlen (fmt_str));
9773 /* If the format is "%s" and first ... argument is a string literal,
9774 we know it too. */
9775 else if (fcode == BUILT_IN_SPRINTF_CHK
9776 && strcmp (fmt_str, target_percent_s) == 0)
9778 tree arg;
9780 if (nargs < 5)
9781 return;
9782 arg = CALL_EXPR_ARG (exp, 4);
9783 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
9784 return;
9786 len = c_strlen (arg, 1);
9787 if (!len || ! tree_fits_uhwi_p (len))
9788 return;
9790 else
9791 return;
9793 /* Add one for the terminating nul. */
9794 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
9795 check_sizes (OPT_Wstringop_overflow_,
9796 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, len, size);
9799 /* Emit warning if a free is called with address of a variable. */
9801 static void
9802 maybe_emit_free_warning (tree exp)
9804 tree arg = CALL_EXPR_ARG (exp, 0);
9806 STRIP_NOPS (arg);
9807 if (TREE_CODE (arg) != ADDR_EXPR)
9808 return;
9810 arg = get_base_address (TREE_OPERAND (arg, 0));
9811 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
9812 return;
9814 if (SSA_VAR_P (arg))
9815 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9816 "%Kattempt to free a non-heap object %qD", exp, arg);
9817 else
9818 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9819 "%Kattempt to free a non-heap object", exp);
9822 /* Fold a call to __builtin_object_size with arguments PTR and OST,
9823 if possible. */
9825 static tree
9826 fold_builtin_object_size (tree ptr, tree ost)
9828 unsigned HOST_WIDE_INT bytes;
9829 int object_size_type;
9831 if (!validate_arg (ptr, POINTER_TYPE)
9832 || !validate_arg (ost, INTEGER_TYPE))
9833 return NULL_TREE;
9835 STRIP_NOPS (ost);
9837 if (TREE_CODE (ost) != INTEGER_CST
9838 || tree_int_cst_sgn (ost) < 0
9839 || compare_tree_int (ost, 3) > 0)
9840 return NULL_TREE;
9842 object_size_type = tree_to_shwi (ost);
9844 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
9845 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
9846 and (size_t) 0 for types 2 and 3. */
9847 if (TREE_SIDE_EFFECTS (ptr))
9848 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
9850 if (TREE_CODE (ptr) == ADDR_EXPR)
9852 compute_builtin_object_size (ptr, object_size_type, &bytes);
9853 if (wi::fits_to_tree_p (bytes, size_type_node))
9854 return build_int_cstu (size_type_node, bytes);
9856 else if (TREE_CODE (ptr) == SSA_NAME)
9858 /* If object size is not known yet, delay folding until
9859 later. Maybe subsequent passes will help determining
9860 it. */
9861 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
9862 && wi::fits_to_tree_p (bytes, size_type_node))
9863 return build_int_cstu (size_type_node, bytes);
9866 return NULL_TREE;
9869 /* Builtins with folding operations that operate on "..." arguments
9870 need special handling; we need to store the arguments in a convenient
9871 data structure before attempting any folding. Fortunately there are
9872 only a few builtins that fall into this category. FNDECL is the
9873 function, EXP is the CALL_EXPR for the call. */
9875 static tree
9876 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
9878 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9879 tree ret = NULL_TREE;
9881 switch (fcode)
9883 case BUILT_IN_FPCLASSIFY:
9884 ret = fold_builtin_fpclassify (loc, args, nargs);
9885 break;
9887 default:
9888 break;
9890 if (ret)
9892 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9893 SET_EXPR_LOCATION (ret, loc);
9894 TREE_NO_WARNING (ret) = 1;
9895 return ret;
9897 return NULL_TREE;
9900 /* Initialize format string characters in the target charset. */
9902 bool
9903 init_target_chars (void)
9905 static bool init;
9906 if (!init)
9908 target_newline = lang_hooks.to_target_charset ('\n');
9909 target_percent = lang_hooks.to_target_charset ('%');
9910 target_c = lang_hooks.to_target_charset ('c');
9911 target_s = lang_hooks.to_target_charset ('s');
9912 if (target_newline == 0 || target_percent == 0 || target_c == 0
9913 || target_s == 0)
9914 return false;
9916 target_percent_c[0] = target_percent;
9917 target_percent_c[1] = target_c;
9918 target_percent_c[2] = '\0';
9920 target_percent_s[0] = target_percent;
9921 target_percent_s[1] = target_s;
9922 target_percent_s[2] = '\0';
9924 target_percent_s_newline[0] = target_percent;
9925 target_percent_s_newline[1] = target_s;
9926 target_percent_s_newline[2] = target_newline;
9927 target_percent_s_newline[3] = '\0';
9929 init = true;
9931 return true;
9934 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
9935 and no overflow/underflow occurred. INEXACT is true if M was not
9936 exactly calculated. TYPE is the tree type for the result. This
9937 function assumes that you cleared the MPFR flags and then
9938 calculated M to see if anything subsequently set a flag prior to
9939 entering this function. Return NULL_TREE if any checks fail. */
9941 static tree
9942 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
9944 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9945 overflow/underflow occurred. If -frounding-math, proceed iff the
9946 result of calling FUNC was exact. */
9947 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
9948 && (!flag_rounding_math || !inexact))
9950 REAL_VALUE_TYPE rr;
9952 real_from_mpfr (&rr, m, type, GMP_RNDN);
9953 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
9954 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9955 but the mpft_t is not, then we underflowed in the
9956 conversion. */
9957 if (real_isfinite (&rr)
9958 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
9960 REAL_VALUE_TYPE rmode;
9962 real_convert (&rmode, TYPE_MODE (type), &rr);
9963 /* Proceed iff the specified mode can hold the value. */
9964 if (real_identical (&rmode, &rr))
9965 return build_real (type, rmode);
9968 return NULL_TREE;
9971 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
9972 number and no overflow/underflow occurred. INEXACT is true if M
9973 was not exactly calculated. TYPE is the tree type for the result.
9974 This function assumes that you cleared the MPFR flags and then
9975 calculated M to see if anything subsequently set a flag prior to
9976 entering this function. Return NULL_TREE if any checks fail, if
9977 FORCE_CONVERT is true, then bypass the checks. */
9979 static tree
9980 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
9982 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9983 overflow/underflow occurred. If -frounding-math, proceed iff the
9984 result of calling FUNC was exact. */
9985 if (force_convert
9986 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
9987 && !mpfr_overflow_p () && !mpfr_underflow_p ()
9988 && (!flag_rounding_math || !inexact)))
9990 REAL_VALUE_TYPE re, im;
9992 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
9993 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
9994 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
9995 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9996 but the mpft_t is not, then we underflowed in the
9997 conversion. */
9998 if (force_convert
9999 || (real_isfinite (&re) && real_isfinite (&im)
10000 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10001 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10003 REAL_VALUE_TYPE re_mode, im_mode;
10005 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10006 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10007 /* Proceed iff the specified mode can hold the value. */
10008 if (force_convert
10009 || (real_identical (&re_mode, &re)
10010 && real_identical (&im_mode, &im)))
10011 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10012 build_real (TREE_TYPE (type), im_mode));
10015 return NULL_TREE;
10018 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10019 the pointer *(ARG_QUO) and return the result. The type is taken
10020 from the type of ARG0 and is used for setting the precision of the
10021 calculation and results. */
10023 static tree
10024 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10026 tree const type = TREE_TYPE (arg0);
10027 tree result = NULL_TREE;
10029 STRIP_NOPS (arg0);
10030 STRIP_NOPS (arg1);
10032 /* To proceed, MPFR must exactly represent the target floating point
10033 format, which only happens when the target base equals two. */
10034 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10035 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10036 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10038 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10039 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10041 if (real_isfinite (ra0) && real_isfinite (ra1))
10043 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10044 const int prec = fmt->p;
10045 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10046 tree result_rem;
10047 long integer_quo;
10048 mpfr_t m0, m1;
10050 mpfr_inits2 (prec, m0, m1, NULL);
10051 mpfr_from_real (m0, ra0, GMP_RNDN);
10052 mpfr_from_real (m1, ra1, GMP_RNDN);
10053 mpfr_clear_flags ();
10054 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10055 /* Remquo is independent of the rounding mode, so pass
10056 inexact=0 to do_mpfr_ckconv(). */
10057 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10058 mpfr_clears (m0, m1, NULL);
10059 if (result_rem)
10061 /* MPFR calculates quo in the host's long so it may
10062 return more bits in quo than the target int can hold
10063 if sizeof(host long) > sizeof(target int). This can
10064 happen even for native compilers in LP64 mode. In
10065 these cases, modulo the quo value with the largest
10066 number that the target int can hold while leaving one
10067 bit for the sign. */
10068 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10069 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10071 /* Dereference the quo pointer argument. */
10072 arg_quo = build_fold_indirect_ref (arg_quo);
10073 /* Proceed iff a valid pointer type was passed in. */
10074 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10076 /* Set the value. */
10077 tree result_quo
10078 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10079 build_int_cst (TREE_TYPE (arg_quo),
10080 integer_quo));
10081 TREE_SIDE_EFFECTS (result_quo) = 1;
10082 /* Combine the quo assignment with the rem. */
10083 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10084 result_quo, result_rem));
10089 return result;
10092 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10093 resulting value as a tree with type TYPE. The mpfr precision is
10094 set to the precision of TYPE. We assume that this mpfr function
10095 returns zero if the result could be calculated exactly within the
10096 requested precision. In addition, the integer pointer represented
10097 by ARG_SG will be dereferenced and set to the appropriate signgam
10098 (-1,1) value. */
10100 static tree
10101 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10103 tree result = NULL_TREE;
10105 STRIP_NOPS (arg);
10107 /* To proceed, MPFR must exactly represent the target floating point
10108 format, which only happens when the target base equals two. Also
10109 verify ARG is a constant and that ARG_SG is an int pointer. */
10110 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10111 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10112 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10113 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10115 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10117 /* In addition to NaN and Inf, the argument cannot be zero or a
10118 negative integer. */
10119 if (real_isfinite (ra)
10120 && ra->cl != rvc_zero
10121 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10123 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10124 const int prec = fmt->p;
10125 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10126 int inexact, sg;
10127 mpfr_t m;
10128 tree result_lg;
10130 mpfr_init2 (m, prec);
10131 mpfr_from_real (m, ra, GMP_RNDN);
10132 mpfr_clear_flags ();
10133 inexact = mpfr_lgamma (m, &sg, m, rnd);
10134 result_lg = do_mpfr_ckconv (m, type, inexact);
10135 mpfr_clear (m);
10136 if (result_lg)
10138 tree result_sg;
10140 /* Dereference the arg_sg pointer argument. */
10141 arg_sg = build_fold_indirect_ref (arg_sg);
10142 /* Assign the signgam value into *arg_sg. */
10143 result_sg = fold_build2 (MODIFY_EXPR,
10144 TREE_TYPE (arg_sg), arg_sg,
10145 build_int_cst (TREE_TYPE (arg_sg), sg));
10146 TREE_SIDE_EFFECTS (result_sg) = 1;
10147 /* Combine the signgam assignment with the lgamma result. */
10148 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10149 result_sg, result_lg));
10154 return result;
10157 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10158 mpc function FUNC on it and return the resulting value as a tree
10159 with type TYPE. The mpfr precision is set to the precision of
10160 TYPE. We assume that function FUNC returns zero if the result
10161 could be calculated exactly within the requested precision. If
10162 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10163 in the arguments and/or results. */
10165 tree
10166 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10167 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10169 tree result = NULL_TREE;
10171 STRIP_NOPS (arg0);
10172 STRIP_NOPS (arg1);
10174 /* To proceed, MPFR must exactly represent the target floating point
10175 format, which only happens when the target base equals two. */
10176 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10177 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10178 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10179 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10180 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10182 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10183 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10184 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10185 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10187 if (do_nonfinite
10188 || (real_isfinite (re0) && real_isfinite (im0)
10189 && real_isfinite (re1) && real_isfinite (im1)))
10191 const struct real_format *const fmt =
10192 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10193 const int prec = fmt->p;
10194 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
10195 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10196 int inexact;
10197 mpc_t m0, m1;
10199 mpc_init2 (m0, prec);
10200 mpc_init2 (m1, prec);
10201 mpfr_from_real (mpc_realref (m0), re0, rnd);
10202 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10203 mpfr_from_real (mpc_realref (m1), re1, rnd);
10204 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10205 mpfr_clear_flags ();
10206 inexact = func (m0, m0, m1, crnd);
10207 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10208 mpc_clear (m0);
10209 mpc_clear (m1);
10213 return result;
10216 /* A wrapper function for builtin folding that prevents warnings for
10217 "statement without effect" and the like, caused by removing the
10218 call node earlier than the warning is generated. */
10220 tree
10221 fold_call_stmt (gcall *stmt, bool ignore)
10223 tree ret = NULL_TREE;
10224 tree fndecl = gimple_call_fndecl (stmt);
10225 location_t loc = gimple_location (stmt);
10226 if (fndecl
10227 && TREE_CODE (fndecl) == FUNCTION_DECL
10228 && DECL_BUILT_IN (fndecl)
10229 && !gimple_call_va_arg_pack_p (stmt))
10231 int nargs = gimple_call_num_args (stmt);
10232 tree *args = (nargs > 0
10233 ? gimple_call_arg_ptr (stmt, 0)
10234 : &error_mark_node);
10236 if (avoid_folding_inline_builtin (fndecl))
10237 return NULL_TREE;
10238 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10240 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10242 else
10244 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10245 if (ret)
10247 /* Propagate location information from original call to
10248 expansion of builtin. Otherwise things like
10249 maybe_emit_chk_warning, that operate on the expansion
10250 of a builtin, will use the wrong location information. */
10251 if (gimple_has_location (stmt))
10253 tree realret = ret;
10254 if (TREE_CODE (ret) == NOP_EXPR)
10255 realret = TREE_OPERAND (ret, 0);
10256 if (CAN_HAVE_LOCATION_P (realret)
10257 && !EXPR_HAS_LOCATION (realret))
10258 SET_EXPR_LOCATION (realret, loc);
10259 return realret;
10261 return ret;
10265 return NULL_TREE;
10268 /* Look up the function in builtin_decl that corresponds to DECL
10269 and set ASMSPEC as its user assembler name. DECL must be a
10270 function decl that declares a builtin. */
10272 void
10273 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10275 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
10276 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
10277 && asmspec != 0);
10279 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10280 set_user_assembler_name (builtin, asmspec);
10282 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10283 && INT_TYPE_SIZE < BITS_PER_WORD)
10285 set_user_assembler_libfunc ("ffs", asmspec);
10286 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
10287 "ffs");
10291 /* Return true if DECL is a builtin that expands to a constant or similarly
10292 simple code. */
10293 bool
10294 is_simple_builtin (tree decl)
10296 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10297 switch (DECL_FUNCTION_CODE (decl))
10299 /* Builtins that expand to constants. */
10300 case BUILT_IN_CONSTANT_P:
10301 case BUILT_IN_EXPECT:
10302 case BUILT_IN_OBJECT_SIZE:
10303 case BUILT_IN_UNREACHABLE:
10304 /* Simple register moves or loads from stack. */
10305 case BUILT_IN_ASSUME_ALIGNED:
10306 case BUILT_IN_RETURN_ADDRESS:
10307 case BUILT_IN_EXTRACT_RETURN_ADDR:
10308 case BUILT_IN_FROB_RETURN_ADDR:
10309 case BUILT_IN_RETURN:
10310 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10311 case BUILT_IN_FRAME_ADDRESS:
10312 case BUILT_IN_VA_END:
10313 case BUILT_IN_STACK_SAVE:
10314 case BUILT_IN_STACK_RESTORE:
10315 /* Exception state returns or moves registers around. */
10316 case BUILT_IN_EH_FILTER:
10317 case BUILT_IN_EH_POINTER:
10318 case BUILT_IN_EH_COPY_VALUES:
10319 return true;
10321 default:
10322 return false;
10325 return false;
10328 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10329 most probably expanded inline into reasonably simple code. This is a
10330 superset of is_simple_builtin. */
10331 bool
10332 is_inexpensive_builtin (tree decl)
10334 if (!decl)
10335 return false;
10336 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10337 return true;
10338 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10339 switch (DECL_FUNCTION_CODE (decl))
10341 case BUILT_IN_ABS:
10342 case BUILT_IN_ALLOCA:
10343 case BUILT_IN_ALLOCA_WITH_ALIGN:
10344 case BUILT_IN_BSWAP16:
10345 case BUILT_IN_BSWAP32:
10346 case BUILT_IN_BSWAP64:
10347 case BUILT_IN_CLZ:
10348 case BUILT_IN_CLZIMAX:
10349 case BUILT_IN_CLZL:
10350 case BUILT_IN_CLZLL:
10351 case BUILT_IN_CTZ:
10352 case BUILT_IN_CTZIMAX:
10353 case BUILT_IN_CTZL:
10354 case BUILT_IN_CTZLL:
10355 case BUILT_IN_FFS:
10356 case BUILT_IN_FFSIMAX:
10357 case BUILT_IN_FFSL:
10358 case BUILT_IN_FFSLL:
10359 case BUILT_IN_IMAXABS:
10360 case BUILT_IN_FINITE:
10361 case BUILT_IN_FINITEF:
10362 case BUILT_IN_FINITEL:
10363 case BUILT_IN_FINITED32:
10364 case BUILT_IN_FINITED64:
10365 case BUILT_IN_FINITED128:
10366 case BUILT_IN_FPCLASSIFY:
10367 case BUILT_IN_ISFINITE:
10368 case BUILT_IN_ISINF_SIGN:
10369 case BUILT_IN_ISINF:
10370 case BUILT_IN_ISINFF:
10371 case BUILT_IN_ISINFL:
10372 case BUILT_IN_ISINFD32:
10373 case BUILT_IN_ISINFD64:
10374 case BUILT_IN_ISINFD128:
10375 case BUILT_IN_ISNAN:
10376 case BUILT_IN_ISNANF:
10377 case BUILT_IN_ISNANL:
10378 case BUILT_IN_ISNAND32:
10379 case BUILT_IN_ISNAND64:
10380 case BUILT_IN_ISNAND128:
10381 case BUILT_IN_ISNORMAL:
10382 case BUILT_IN_ISGREATER:
10383 case BUILT_IN_ISGREATEREQUAL:
10384 case BUILT_IN_ISLESS:
10385 case BUILT_IN_ISLESSEQUAL:
10386 case BUILT_IN_ISLESSGREATER:
10387 case BUILT_IN_ISUNORDERED:
10388 case BUILT_IN_VA_ARG_PACK:
10389 case BUILT_IN_VA_ARG_PACK_LEN:
10390 case BUILT_IN_VA_COPY:
10391 case BUILT_IN_TRAP:
10392 case BUILT_IN_SAVEREGS:
10393 case BUILT_IN_POPCOUNTL:
10394 case BUILT_IN_POPCOUNTLL:
10395 case BUILT_IN_POPCOUNTIMAX:
10396 case BUILT_IN_POPCOUNT:
10397 case BUILT_IN_PARITYL:
10398 case BUILT_IN_PARITYLL:
10399 case BUILT_IN_PARITYIMAX:
10400 case BUILT_IN_PARITY:
10401 case BUILT_IN_LABS:
10402 case BUILT_IN_LLABS:
10403 case BUILT_IN_PREFETCH:
10404 case BUILT_IN_ACC_ON_DEVICE:
10405 return true;
10407 default:
10408 return is_simple_builtin (decl);
10411 return false;
10414 /* Return true if T is a constant and the value cast to a target char
10415 can be represented by a host char.
10416 Store the casted char constant in *P if so. */
10418 bool
10419 target_char_cst_p (tree t, char *p)
10421 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10422 return false;
10424 *p = (char)tree_to_uhwi (t);
10425 return true;