[RTL-ifcvt] PR rtl-optimization/68506: Fix emitting order of insns in IF-THEN-JOIN...
[official-gcc.git] / gcc / builtins.c
blobdf5c49309973337619098a533e6724584f7d9678
1 /* Expand builtin functions.
2 Copyright (C) 1988-2015 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 "gimple.h"
32 #include "predict.h"
33 #include "tm_p.h"
34 #include "stringpool.h"
35 #include "tree-ssanames.h"
36 #include "expmed.h"
37 #include "optabs.h"
38 #include "emit-rtl.h"
39 #include "recog.h"
40 #include "diagnostic-core.h"
41 #include "alias.h"
42 #include "fold-const.h"
43 #include "fold-const-call.h"
44 #include "stor-layout.h"
45 #include "calls.h"
46 #include "varasm.h"
47 #include "tree-object-size.h"
48 #include "realmpfr.h"
49 #include "cfgrtl.h"
50 #include "except.h"
51 #include "dojump.h"
52 #include "explow.h"
53 #include "stmt.h"
54 #include "expr.h"
55 #include "libfuncs.h"
56 #include "output.h"
57 #include "typeclass.h"
58 #include "langhooks.h"
59 #include "value-prof.h"
60 #include "builtins.h"
61 #include "asan.h"
62 #include "cilk.h"
63 #include "tree-chkp.h"
64 #include "rtl-chkp.h"
65 #include "internal-fn.h"
66 #include "case-cfn-macros.h"
69 struct target_builtins default_target_builtins;
70 #if SWITCHABLE_TARGET
71 struct target_builtins *this_target_builtins = &default_target_builtins;
72 #endif
74 /* Define the names of the builtin function types and codes. */
75 const char *const built_in_class_names[BUILT_IN_LAST]
76 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
78 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
79 const char * built_in_names[(int) END_BUILTINS] =
81 #include "builtins.def"
84 /* Setup an array of builtin_info_type, make sure each element decl is
85 initialized to NULL_TREE. */
86 builtin_info_type builtin_info[(int)END_BUILTINS];
88 /* Non-zero if __builtin_constant_p should be folded right away. */
89 bool force_folding_builtin_constant_p;
91 static rtx c_readstr (const char *, machine_mode);
92 static int target_char_cast (tree, char *);
93 static rtx get_memory_rtx (tree, tree);
94 static int apply_args_size (void);
95 static int apply_result_size (void);
96 static rtx result_vector (int, rtx);
97 static void expand_builtin_prefetch (tree);
98 static rtx expand_builtin_apply_args (void);
99 static rtx expand_builtin_apply_args_1 (void);
100 static rtx expand_builtin_apply (rtx, rtx, rtx);
101 static void expand_builtin_return (rtx);
102 static enum type_class type_to_class (tree);
103 static rtx expand_builtin_classify_type (tree);
104 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
105 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
106 static rtx expand_builtin_interclass_mathfn (tree, rtx);
107 static rtx expand_builtin_sincos (tree);
108 static rtx expand_builtin_cexpi (tree, rtx);
109 static rtx expand_builtin_int_roundingfn (tree, rtx);
110 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
111 static rtx expand_builtin_next_arg (void);
112 static rtx expand_builtin_va_start (tree);
113 static rtx expand_builtin_va_end (tree);
114 static rtx expand_builtin_va_copy (tree);
115 static rtx expand_builtin_strcmp (tree, rtx);
116 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
117 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
118 static rtx expand_builtin_memcpy (tree, rtx);
119 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
120 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
121 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
122 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
123 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
124 machine_mode, int, tree);
125 static rtx expand_builtin_strcpy (tree, rtx);
126 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
127 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
128 static rtx expand_builtin_strncpy (tree, rtx);
129 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
130 static rtx expand_builtin_memset (tree, rtx, machine_mode);
131 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
132 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
133 static rtx expand_builtin_bzero (tree);
134 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
135 static rtx expand_builtin_alloca (tree, bool);
136 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
137 static rtx expand_builtin_frame_address (tree, tree);
138 static tree stabilize_va_list_loc (location_t, tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (location_t, tree, tree);
143 static tree fold_builtin_inf (location_t, tree, int);
144 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
145 static bool validate_arg (const_tree, enum tree_code code);
146 static rtx expand_builtin_fabs (tree, rtx, rtx);
147 static rtx expand_builtin_signbit (tree, rtx);
148 static tree fold_builtin_strchr (location_t, tree, tree, tree);
149 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
151 static tree fold_builtin_strcmp (location_t, tree, tree);
152 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
153 static tree fold_builtin_isascii (location_t, tree);
154 static tree fold_builtin_toascii (location_t, tree);
155 static tree fold_builtin_isdigit (location_t, tree);
156 static tree fold_builtin_fabs (location_t, tree, tree);
157 static tree fold_builtin_abs (location_t, tree, tree);
158 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
159 enum tree_code);
160 static tree fold_builtin_0 (location_t, tree);
161 static tree fold_builtin_1 (location_t, tree, tree);
162 static tree fold_builtin_2 (location_t, tree, tree, tree);
163 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
164 static tree fold_builtin_varargs (location_t, tree, tree*, int);
166 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
167 static tree fold_builtin_strstr (location_t, tree, tree, tree);
168 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
169 static tree fold_builtin_strspn (location_t, tree, tree);
170 static tree fold_builtin_strcspn (location_t, tree, tree);
172 static rtx expand_builtin_object_size (tree);
173 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
174 enum built_in_function);
175 static void maybe_emit_chk_warning (tree, enum built_in_function);
176 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
177 static void maybe_emit_free_warning (tree);
178 static tree fold_builtin_object_size (tree, tree);
180 unsigned HOST_WIDE_INT target_newline;
181 unsigned HOST_WIDE_INT target_percent;
182 static unsigned HOST_WIDE_INT target_c;
183 static unsigned HOST_WIDE_INT target_s;
184 char target_percent_c[3];
185 char target_percent_s[3];
186 char target_percent_s_newline[4];
187 static tree do_mpfr_remquo (tree, tree, tree);
188 static tree do_mpfr_lgamma_r (tree, tree, tree);
189 static void expand_builtin_sync_synchronize (void);
191 /* Return true if NAME starts with __builtin_ or __sync_. */
193 static bool
194 is_builtin_name (const char *name)
196 if (strncmp (name, "__builtin_", 10) == 0)
197 return true;
198 if (strncmp (name, "__sync_", 7) == 0)
199 return true;
200 if (strncmp (name, "__atomic_", 9) == 0)
201 return true;
202 if (flag_cilkplus
203 && (!strcmp (name, "__cilkrts_detach")
204 || !strcmp (name, "__cilkrts_pop_frame")))
205 return true;
206 return false;
210 /* Return true if DECL is a function symbol representing a built-in. */
212 bool
213 is_builtin_fn (tree decl)
215 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
218 /* Return true if NODE should be considered for inline expansion regardless
219 of the optimization level. This means whenever a function is invoked with
220 its "internal" name, which normally contains the prefix "__builtin". */
222 bool
223 called_as_built_in (tree node)
225 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
226 we want the name used to call the function, not the name it
227 will have. */
228 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
229 return is_builtin_name (name);
232 /* Compute values M and N such that M divides (address of EXP - N) and such
233 that N < M. If these numbers can be determined, store M in alignp and N in
234 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
235 *alignp and any bit-offset to *bitposp.
237 Note that the address (and thus the alignment) computed here is based
238 on the address to which a symbol resolves, whereas DECL_ALIGN is based
239 on the address at which an object is actually located. These two
240 addresses are not always the same. For example, on ARM targets,
241 the address &foo of a Thumb function foo() has the lowest bit set,
242 whereas foo() itself starts on an even address.
244 If ADDR_P is true we are taking the address of the memory reference EXP
245 and thus cannot rely on the access taking place. */
247 static bool
248 get_object_alignment_2 (tree exp, unsigned int *alignp,
249 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
251 HOST_WIDE_INT bitsize, bitpos;
252 tree offset;
253 machine_mode mode;
254 int unsignedp, reversep, volatilep;
255 unsigned int align = BITS_PER_UNIT;
256 bool known_alignment = false;
258 /* Get the innermost object and the constant (bitpos) and possibly
259 variable (offset) offset of the access. */
260 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
261 &unsignedp, &reversep, &volatilep, true);
263 /* Extract alignment information from the innermost object and
264 possibly adjust bitpos and offset. */
265 if (TREE_CODE (exp) == FUNCTION_DECL)
267 /* Function addresses can encode extra information besides their
268 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
269 allows the low bit to be used as a virtual bit, we know
270 that the address itself must be at least 2-byte aligned. */
271 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
272 align = 2 * BITS_PER_UNIT;
274 else if (TREE_CODE (exp) == LABEL_DECL)
276 else if (TREE_CODE (exp) == CONST_DECL)
278 /* The alignment of a CONST_DECL is determined by its initializer. */
279 exp = DECL_INITIAL (exp);
280 align = TYPE_ALIGN (TREE_TYPE (exp));
281 if (CONSTANT_CLASS_P (exp))
282 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
284 known_alignment = true;
286 else if (DECL_P (exp))
288 align = DECL_ALIGN (exp);
289 known_alignment = true;
291 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
293 align = TYPE_ALIGN (TREE_TYPE (exp));
295 else if (TREE_CODE (exp) == INDIRECT_REF
296 || TREE_CODE (exp) == MEM_REF
297 || TREE_CODE (exp) == TARGET_MEM_REF)
299 tree addr = TREE_OPERAND (exp, 0);
300 unsigned ptr_align;
301 unsigned HOST_WIDE_INT ptr_bitpos;
302 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
304 /* If the address is explicitely aligned, handle that. */
305 if (TREE_CODE (addr) == BIT_AND_EXPR
306 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
308 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
309 ptr_bitmask *= BITS_PER_UNIT;
310 align = ptr_bitmask & -ptr_bitmask;
311 addr = TREE_OPERAND (addr, 0);
314 known_alignment
315 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
316 align = MAX (ptr_align, align);
318 /* Re-apply explicit alignment to the bitpos. */
319 ptr_bitpos &= ptr_bitmask;
321 /* The alignment of the pointer operand in a TARGET_MEM_REF
322 has to take the variable offset parts into account. */
323 if (TREE_CODE (exp) == TARGET_MEM_REF)
325 if (TMR_INDEX (exp))
327 unsigned HOST_WIDE_INT step = 1;
328 if (TMR_STEP (exp))
329 step = TREE_INT_CST_LOW (TMR_STEP (exp));
330 align = MIN (align, (step & -step) * BITS_PER_UNIT);
332 if (TMR_INDEX2 (exp))
333 align = BITS_PER_UNIT;
334 known_alignment = false;
337 /* When EXP is an actual memory reference then we can use
338 TYPE_ALIGN of a pointer indirection to derive alignment.
339 Do so only if get_pointer_alignment_1 did not reveal absolute
340 alignment knowledge and if using that alignment would
341 improve the situation. */
342 if (!addr_p && !known_alignment
343 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
344 align = TYPE_ALIGN (TREE_TYPE (exp));
345 else
347 /* Else adjust bitpos accordingly. */
348 bitpos += ptr_bitpos;
349 if (TREE_CODE (exp) == MEM_REF
350 || TREE_CODE (exp) == TARGET_MEM_REF)
351 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
354 else if (TREE_CODE (exp) == STRING_CST)
356 /* STRING_CST are the only constant objects we allow to be not
357 wrapped inside a CONST_DECL. */
358 align = TYPE_ALIGN (TREE_TYPE (exp));
359 if (CONSTANT_CLASS_P (exp))
360 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
362 known_alignment = true;
365 /* If there is a non-constant offset part extract the maximum
366 alignment that can prevail. */
367 if (offset)
369 unsigned int trailing_zeros = tree_ctz (offset);
370 if (trailing_zeros < HOST_BITS_PER_INT)
372 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
373 if (inner)
374 align = MIN (align, inner);
378 *alignp = align;
379 *bitposp = bitpos & (*alignp - 1);
380 return known_alignment;
383 /* For a memory reference expression EXP compute values M and N such that M
384 divides (&EXP - N) and such that N < M. If these numbers can be determined,
385 store M in alignp and N in *BITPOSP and return true. Otherwise return false
386 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
388 bool
389 get_object_alignment_1 (tree exp, unsigned int *alignp,
390 unsigned HOST_WIDE_INT *bitposp)
392 return get_object_alignment_2 (exp, alignp, bitposp, false);
395 /* Return the alignment in bits of EXP, an object. */
397 unsigned int
398 get_object_alignment (tree exp)
400 unsigned HOST_WIDE_INT bitpos = 0;
401 unsigned int align;
403 get_object_alignment_1 (exp, &align, &bitpos);
405 /* align and bitpos now specify known low bits of the pointer.
406 ptr & (align - 1) == bitpos. */
408 if (bitpos != 0)
409 align = (bitpos & -bitpos);
410 return align;
413 /* For a pointer valued expression EXP compute values M and N such that M
414 divides (EXP - N) and such that N < M. If these numbers can be determined,
415 store M in alignp and N in *BITPOSP and return true. Return false if
416 the results are just a conservative approximation.
418 If EXP is not a pointer, false is returned too. */
420 bool
421 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
422 unsigned HOST_WIDE_INT *bitposp)
424 STRIP_NOPS (exp);
426 if (TREE_CODE (exp) == ADDR_EXPR)
427 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
428 alignp, bitposp, true);
429 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
431 unsigned int align;
432 unsigned HOST_WIDE_INT bitpos;
433 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
434 &align, &bitpos);
435 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
436 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
437 else
439 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
440 if (trailing_zeros < HOST_BITS_PER_INT)
442 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
443 if (inner)
444 align = MIN (align, inner);
447 *alignp = align;
448 *bitposp = bitpos & (align - 1);
449 return res;
451 else if (TREE_CODE (exp) == SSA_NAME
452 && POINTER_TYPE_P (TREE_TYPE (exp)))
454 unsigned int ptr_align, ptr_misalign;
455 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
457 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
459 *bitposp = ptr_misalign * BITS_PER_UNIT;
460 *alignp = ptr_align * BITS_PER_UNIT;
461 /* We cannot really tell whether this result is an approximation. */
462 return true;
464 else
466 *bitposp = 0;
467 *alignp = BITS_PER_UNIT;
468 return false;
471 else if (TREE_CODE (exp) == INTEGER_CST)
473 *alignp = BIGGEST_ALIGNMENT;
474 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
475 & (BIGGEST_ALIGNMENT - 1));
476 return true;
479 *bitposp = 0;
480 *alignp = BITS_PER_UNIT;
481 return false;
484 /* Return the alignment in bits of EXP, a pointer valued expression.
485 The alignment returned is, by default, the alignment of the thing that
486 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
488 Otherwise, look at the expression to see if we can do better, i.e., if the
489 expression is actually pointing at an object whose alignment is tighter. */
491 unsigned int
492 get_pointer_alignment (tree exp)
494 unsigned HOST_WIDE_INT bitpos = 0;
495 unsigned int align;
497 get_pointer_alignment_1 (exp, &align, &bitpos);
499 /* align and bitpos now specify known low bits of the pointer.
500 ptr & (align - 1) == bitpos. */
502 if (bitpos != 0)
503 align = (bitpos & -bitpos);
505 return align;
508 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
509 way, because it could contain a zero byte in the middle.
510 TREE_STRING_LENGTH is the size of the character array, not the string.
512 ONLY_VALUE should be nonzero if the result is not going to be emitted
513 into the instruction stream and zero if it is going to be expanded.
514 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
515 is returned, otherwise NULL, since
516 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
517 evaluate the side-effects.
519 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
520 accesses. Note that this implies the result is not going to be emitted
521 into the instruction stream.
523 The value returned is of type `ssizetype'.
525 Unfortunately, string_constant can't access the values of const char
526 arrays with initializers, so neither can we do so here. */
528 tree
529 c_strlen (tree src, int only_value)
531 tree offset_node;
532 HOST_WIDE_INT offset;
533 int max;
534 const char *ptr;
535 location_t loc;
537 STRIP_NOPS (src);
538 if (TREE_CODE (src) == COND_EXPR
539 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
541 tree len1, len2;
543 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
544 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
545 if (tree_int_cst_equal (len1, len2))
546 return len1;
549 if (TREE_CODE (src) == COMPOUND_EXPR
550 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
551 return c_strlen (TREE_OPERAND (src, 1), only_value);
553 loc = EXPR_LOC_OR_LOC (src, input_location);
555 src = string_constant (src, &offset_node);
556 if (src == 0)
557 return NULL_TREE;
559 max = TREE_STRING_LENGTH (src) - 1;
560 ptr = TREE_STRING_POINTER (src);
562 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
564 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
565 compute the offset to the following null if we don't know where to
566 start searching for it. */
567 int i;
569 for (i = 0; i < max; i++)
570 if (ptr[i] == 0)
571 return NULL_TREE;
573 /* We don't know the starting offset, but we do know that the string
574 has no internal zero bytes. We can assume that the offset falls
575 within the bounds of the string; otherwise, the programmer deserves
576 what he gets. Subtract the offset from the length of the string,
577 and return that. This would perhaps not be valid if we were dealing
578 with named arrays in addition to literal string constants. */
580 return size_diffop_loc (loc, size_int (max), offset_node);
583 /* We have a known offset into the string. Start searching there for
584 a null character if we can represent it as a single HOST_WIDE_INT. */
585 if (offset_node == 0)
586 offset = 0;
587 else if (! tree_fits_shwi_p (offset_node))
588 offset = -1;
589 else
590 offset = tree_to_shwi (offset_node);
592 /* If the offset is known to be out of bounds, warn, and call strlen at
593 runtime. */
594 if (offset < 0 || offset > max)
596 /* Suppress multiple warnings for propagated constant strings. */
597 if (only_value != 2
598 && !TREE_NO_WARNING (src))
600 warning_at (loc, 0, "offset outside bounds of constant string");
601 TREE_NO_WARNING (src) = 1;
603 return NULL_TREE;
606 /* Use strlen to search for the first zero byte. Since any strings
607 constructed with build_string will have nulls appended, we win even
608 if we get handed something like (char[4])"abcd".
610 Since OFFSET is our starting index into the string, no further
611 calculation is needed. */
612 return ssize_int (strlen (ptr + offset));
615 /* Return a constant integer corresponding to target reading
616 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
618 static rtx
619 c_readstr (const char *str, machine_mode mode)
621 HOST_WIDE_INT ch;
622 unsigned int i, j;
623 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
625 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
626 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
627 / HOST_BITS_PER_WIDE_INT;
629 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
630 for (i = 0; i < len; i++)
631 tmp[i] = 0;
633 ch = 1;
634 for (i = 0; i < GET_MODE_SIZE (mode); i++)
636 j = i;
637 if (WORDS_BIG_ENDIAN)
638 j = GET_MODE_SIZE (mode) - i - 1;
639 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
640 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
641 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
642 j *= BITS_PER_UNIT;
644 if (ch)
645 ch = (unsigned char) str[i];
646 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
649 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
650 return immed_wide_int_const (c, mode);
653 /* Cast a target constant CST to target CHAR and if that value fits into
654 host char type, return zero and put that value into variable pointed to by
655 P. */
657 static int
658 target_char_cast (tree cst, char *p)
660 unsigned HOST_WIDE_INT val, hostval;
662 if (TREE_CODE (cst) != INTEGER_CST
663 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
664 return 1;
666 /* Do not care if it fits or not right here. */
667 val = TREE_INT_CST_LOW (cst);
669 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
670 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
672 hostval = val;
673 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
674 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
676 if (val != hostval)
677 return 1;
679 *p = hostval;
680 return 0;
683 /* Similar to save_expr, but assumes that arbitrary code is not executed
684 in between the multiple evaluations. In particular, we assume that a
685 non-addressable local variable will not be modified. */
687 static tree
688 builtin_save_expr (tree exp)
690 if (TREE_CODE (exp) == SSA_NAME
691 || (TREE_ADDRESSABLE (exp) == 0
692 && (TREE_CODE (exp) == PARM_DECL
693 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
694 return exp;
696 return save_expr (exp);
699 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
700 times to get the address of either a higher stack frame, or a return
701 address located within it (depending on FNDECL_CODE). */
703 static rtx
704 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
706 int i;
707 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
708 if (tem == NULL_RTX)
710 /* For a zero count with __builtin_return_address, we don't care what
711 frame address we return, because target-specific definitions will
712 override us. Therefore frame pointer elimination is OK, and using
713 the soft frame pointer is OK.
715 For a nonzero count, or a zero count with __builtin_frame_address,
716 we require a stable offset from the current frame pointer to the
717 previous one, so we must use the hard frame pointer, and
718 we must disable frame pointer elimination. */
719 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
720 tem = frame_pointer_rtx;
721 else
723 tem = hard_frame_pointer_rtx;
725 /* Tell reload not to eliminate the frame pointer. */
726 crtl->accesses_prior_frames = 1;
730 if (count > 0)
731 SETUP_FRAME_ADDRESSES ();
733 /* On the SPARC, the return address is not in the frame, it is in a
734 register. There is no way to access it off of the current frame
735 pointer, but it can be accessed off the previous frame pointer by
736 reading the value from the register window save area. */
737 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
738 count--;
740 /* Scan back COUNT frames to the specified frame. */
741 for (i = 0; i < count; i++)
743 /* Assume the dynamic chain pointer is in the word that the
744 frame address points to, unless otherwise specified. */
745 tem = DYNAMIC_CHAIN_ADDRESS (tem);
746 tem = memory_address (Pmode, tem);
747 tem = gen_frame_mem (Pmode, tem);
748 tem = copy_to_reg (tem);
751 /* For __builtin_frame_address, return what we've got. But, on
752 the SPARC for example, we may have to add a bias. */
753 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
754 return FRAME_ADDR_RTX (tem);
756 /* For __builtin_return_address, get the return address from that frame. */
757 #ifdef RETURN_ADDR_RTX
758 tem = RETURN_ADDR_RTX (count, tem);
759 #else
760 tem = memory_address (Pmode,
761 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
762 tem = gen_frame_mem (Pmode, tem);
763 #endif
764 return tem;
767 /* Alias set used for setjmp buffer. */
768 static alias_set_type setjmp_alias_set = -1;
770 /* Construct the leading half of a __builtin_setjmp call. Control will
771 return to RECEIVER_LABEL. This is also called directly by the SJLJ
772 exception handling code. */
774 void
775 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
777 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
778 rtx stack_save;
779 rtx mem;
781 if (setjmp_alias_set == -1)
782 setjmp_alias_set = new_alias_set ();
784 buf_addr = convert_memory_address (Pmode, buf_addr);
786 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
788 /* We store the frame pointer and the address of receiver_label in
789 the buffer and use the rest of it for the stack save area, which
790 is machine-dependent. */
792 mem = gen_rtx_MEM (Pmode, buf_addr);
793 set_mem_alias_set (mem, setjmp_alias_set);
794 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
796 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
797 GET_MODE_SIZE (Pmode))),
798 set_mem_alias_set (mem, setjmp_alias_set);
800 emit_move_insn (validize_mem (mem),
801 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
803 stack_save = gen_rtx_MEM (sa_mode,
804 plus_constant (Pmode, buf_addr,
805 2 * GET_MODE_SIZE (Pmode)));
806 set_mem_alias_set (stack_save, setjmp_alias_set);
807 emit_stack_save (SAVE_NONLOCAL, &stack_save);
809 /* If there is further processing to do, do it. */
810 if (targetm.have_builtin_setjmp_setup ())
811 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
813 /* We have a nonlocal label. */
814 cfun->has_nonlocal_label = 1;
817 /* Construct the trailing part of a __builtin_setjmp call. This is
818 also called directly by the SJLJ exception handling code.
819 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
821 void
822 expand_builtin_setjmp_receiver (rtx receiver_label)
824 rtx chain;
826 /* Mark the FP as used when we get here, so we have to make sure it's
827 marked as used by this function. */
828 emit_use (hard_frame_pointer_rtx);
830 /* Mark the static chain as clobbered here so life information
831 doesn't get messed up for it. */
832 chain = targetm.calls.static_chain (current_function_decl, true);
833 if (chain && REG_P (chain))
834 emit_clobber (chain);
836 /* Now put in the code to restore the frame pointer, and argument
837 pointer, if needed. */
838 if (! targetm.have_nonlocal_goto ())
840 /* First adjust our frame pointer to its actual value. It was
841 previously set to the start of the virtual area corresponding to
842 the stacked variables when we branched here and now needs to be
843 adjusted to the actual hardware fp value.
845 Assignments to virtual registers are converted by
846 instantiate_virtual_regs into the corresponding assignment
847 to the underlying register (fp in this case) that makes
848 the original assignment true.
849 So the following insn will actually be decrementing fp by
850 STARTING_FRAME_OFFSET. */
851 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
853 /* Restoring the frame pointer also modifies the hard frame pointer.
854 Mark it used (so that the previous assignment remains live once
855 the frame pointer is eliminated) and clobbered (to represent the
856 implicit update from the assignment). */
857 emit_use (hard_frame_pointer_rtx);
858 emit_clobber (hard_frame_pointer_rtx);
861 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
863 #ifdef ELIMINABLE_REGS
864 /* If the argument pointer can be eliminated in favor of the
865 frame pointer, we don't need to restore it. We assume here
866 that if such an elimination is present, it can always be used.
867 This is the case on all known machines; if we don't make this
868 assumption, we do unnecessary saving on many machines. */
869 size_t i;
870 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
872 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
873 if (elim_regs[i].from == ARG_POINTER_REGNUM
874 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
875 break;
877 if (i == ARRAY_SIZE (elim_regs))
878 #endif
880 /* Now restore our arg pointer from the address at which it
881 was saved in our stack frame. */
882 emit_move_insn (crtl->args.internal_arg_pointer,
883 copy_to_reg (get_arg_pointer_save_area ()));
887 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
888 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
889 else if (targetm.have_nonlocal_goto_receiver ())
890 emit_insn (targetm.gen_nonlocal_goto_receiver ());
891 else
892 { /* Nothing */ }
894 /* We must not allow the code we just generated to be reordered by
895 scheduling. Specifically, the update of the frame pointer must
896 happen immediately, not later. */
897 emit_insn (gen_blockage ());
900 /* __builtin_longjmp is passed a pointer to an array of five words (not
901 all will be used on all machines). It operates similarly to the C
902 library function of the same name, but is more efficient. Much of
903 the code below is copied from the handling of non-local gotos. */
905 static void
906 expand_builtin_longjmp (rtx buf_addr, rtx value)
908 rtx fp, lab, stack;
909 rtx_insn *insn, *last;
910 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
912 /* DRAP is needed for stack realign if longjmp is expanded to current
913 function */
914 if (SUPPORTS_STACK_ALIGNMENT)
915 crtl->need_drap = true;
917 if (setjmp_alias_set == -1)
918 setjmp_alias_set = new_alias_set ();
920 buf_addr = convert_memory_address (Pmode, buf_addr);
922 buf_addr = force_reg (Pmode, buf_addr);
924 /* We require that the user must pass a second argument of 1, because
925 that is what builtin_setjmp will return. */
926 gcc_assert (value == const1_rtx);
928 last = get_last_insn ();
929 if (targetm.have_builtin_longjmp ())
930 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
931 else
933 fp = gen_rtx_MEM (Pmode, buf_addr);
934 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
935 GET_MODE_SIZE (Pmode)));
937 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
938 2 * GET_MODE_SIZE (Pmode)));
939 set_mem_alias_set (fp, setjmp_alias_set);
940 set_mem_alias_set (lab, setjmp_alias_set);
941 set_mem_alias_set (stack, setjmp_alias_set);
943 /* Pick up FP, label, and SP from the block and jump. This code is
944 from expand_goto in stmt.c; see there for detailed comments. */
945 if (targetm.have_nonlocal_goto ())
946 /* We have to pass a value to the nonlocal_goto pattern that will
947 get copied into the static_chain pointer, but it does not matter
948 what that value is, because builtin_setjmp does not use it. */
949 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
950 else
952 lab = copy_to_reg (lab);
954 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
955 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
957 emit_move_insn (hard_frame_pointer_rtx, fp);
958 emit_stack_restore (SAVE_NONLOCAL, stack);
960 emit_use (hard_frame_pointer_rtx);
961 emit_use (stack_pointer_rtx);
962 emit_indirect_jump (lab);
966 /* Search backwards and mark the jump insn as a non-local goto.
967 Note that this precludes the use of __builtin_longjmp to a
968 __builtin_setjmp target in the same function. However, we've
969 already cautioned the user that these functions are for
970 internal exception handling use only. */
971 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
973 gcc_assert (insn != last);
975 if (JUMP_P (insn))
977 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
978 break;
980 else if (CALL_P (insn))
981 break;
985 static inline bool
986 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
988 return (iter->i < iter->n);
991 /* This function validates the types of a function call argument list
992 against a specified list of tree_codes. If the last specifier is a 0,
993 that represents an ellipses, otherwise the last specifier must be a
994 VOID_TYPE. */
996 static bool
997 validate_arglist (const_tree callexpr, ...)
999 enum tree_code code;
1000 bool res = 0;
1001 va_list ap;
1002 const_call_expr_arg_iterator iter;
1003 const_tree arg;
1005 va_start (ap, callexpr);
1006 init_const_call_expr_arg_iterator (callexpr, &iter);
1010 code = (enum tree_code) va_arg (ap, int);
1011 switch (code)
1013 case 0:
1014 /* This signifies an ellipses, any further arguments are all ok. */
1015 res = true;
1016 goto end;
1017 case VOID_TYPE:
1018 /* This signifies an endlink, if no arguments remain, return
1019 true, otherwise return false. */
1020 res = !more_const_call_expr_args_p (&iter);
1021 goto end;
1022 default:
1023 /* If no parameters remain or the parameter's code does not
1024 match the specified code, return false. Otherwise continue
1025 checking any remaining arguments. */
1026 arg = next_const_call_expr_arg (&iter);
1027 if (!validate_arg (arg, code))
1028 goto end;
1029 break;
1032 while (1);
1034 /* We need gotos here since we can only have one VA_CLOSE in a
1035 function. */
1036 end: ;
1037 va_end (ap);
1039 return res;
1042 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1043 and the address of the save area. */
1045 static rtx
1046 expand_builtin_nonlocal_goto (tree exp)
1048 tree t_label, t_save_area;
1049 rtx r_label, r_save_area, r_fp, r_sp;
1050 rtx_insn *insn;
1052 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1053 return NULL_RTX;
1055 t_label = CALL_EXPR_ARG (exp, 0);
1056 t_save_area = CALL_EXPR_ARG (exp, 1);
1058 r_label = expand_normal (t_label);
1059 r_label = convert_memory_address (Pmode, r_label);
1060 r_save_area = expand_normal (t_save_area);
1061 r_save_area = convert_memory_address (Pmode, r_save_area);
1062 /* Copy the address of the save location to a register just in case it was
1063 based on the frame pointer. */
1064 r_save_area = copy_to_reg (r_save_area);
1065 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1066 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1067 plus_constant (Pmode, r_save_area,
1068 GET_MODE_SIZE (Pmode)));
1070 crtl->has_nonlocal_goto = 1;
1072 /* ??? We no longer need to pass the static chain value, afaik. */
1073 if (targetm.have_nonlocal_goto ())
1074 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1075 else
1077 r_label = copy_to_reg (r_label);
1079 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1080 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1082 /* Restore frame pointer for containing function. */
1083 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1084 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1086 /* USE of hard_frame_pointer_rtx added for consistency;
1087 not clear if really needed. */
1088 emit_use (hard_frame_pointer_rtx);
1089 emit_use (stack_pointer_rtx);
1091 /* If the architecture is using a GP register, we must
1092 conservatively assume that the target function makes use of it.
1093 The prologue of functions with nonlocal gotos must therefore
1094 initialize the GP register to the appropriate value, and we
1095 must then make sure that this value is live at the point
1096 of the jump. (Note that this doesn't necessarily apply
1097 to targets with a nonlocal_goto pattern; they are free
1098 to implement it in their own way. Note also that this is
1099 a no-op if the GP register is a global invariant.) */
1100 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1101 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1102 emit_use (pic_offset_table_rtx);
1104 emit_indirect_jump (r_label);
1107 /* Search backwards to the jump insn and mark it as a
1108 non-local goto. */
1109 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1111 if (JUMP_P (insn))
1113 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1114 break;
1116 else if (CALL_P (insn))
1117 break;
1120 return const0_rtx;
1123 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1124 (not all will be used on all machines) that was passed to __builtin_setjmp.
1125 It updates the stack pointer in that block to the current value. This is
1126 also called directly by the SJLJ exception handling code. */
1128 void
1129 expand_builtin_update_setjmp_buf (rtx buf_addr)
1131 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1132 rtx stack_save
1133 = gen_rtx_MEM (sa_mode,
1134 memory_address
1135 (sa_mode,
1136 plus_constant (Pmode, buf_addr,
1137 2 * GET_MODE_SIZE (Pmode))));
1139 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1142 /* Expand a call to __builtin_prefetch. For a target that does not support
1143 data prefetch, evaluate the memory address argument in case it has side
1144 effects. */
1146 static void
1147 expand_builtin_prefetch (tree exp)
1149 tree arg0, arg1, arg2;
1150 int nargs;
1151 rtx op0, op1, op2;
1153 if (!validate_arglist (exp, POINTER_TYPE, 0))
1154 return;
1156 arg0 = CALL_EXPR_ARG (exp, 0);
1158 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1159 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1160 locality). */
1161 nargs = call_expr_nargs (exp);
1162 if (nargs > 1)
1163 arg1 = CALL_EXPR_ARG (exp, 1);
1164 else
1165 arg1 = integer_zero_node;
1166 if (nargs > 2)
1167 arg2 = CALL_EXPR_ARG (exp, 2);
1168 else
1169 arg2 = integer_three_node;
1171 /* Argument 0 is an address. */
1172 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1174 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1175 if (TREE_CODE (arg1) != INTEGER_CST)
1177 error ("second argument to %<__builtin_prefetch%> must be a constant");
1178 arg1 = integer_zero_node;
1180 op1 = expand_normal (arg1);
1181 /* Argument 1 must be either zero or one. */
1182 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1184 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1185 " using zero");
1186 op1 = const0_rtx;
1189 /* Argument 2 (locality) must be a compile-time constant int. */
1190 if (TREE_CODE (arg2) != INTEGER_CST)
1192 error ("third argument to %<__builtin_prefetch%> must be a constant");
1193 arg2 = integer_zero_node;
1195 op2 = expand_normal (arg2);
1196 /* Argument 2 must be 0, 1, 2, or 3. */
1197 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1199 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1200 op2 = const0_rtx;
1203 if (targetm.have_prefetch ())
1205 struct expand_operand ops[3];
1207 create_address_operand (&ops[0], op0);
1208 create_integer_operand (&ops[1], INTVAL (op1));
1209 create_integer_operand (&ops[2], INTVAL (op2));
1210 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1211 return;
1214 /* Don't do anything with direct references to volatile memory, but
1215 generate code to handle other side effects. */
1216 if (!MEM_P (op0) && side_effects_p (op0))
1217 emit_insn (op0);
1220 /* Get a MEM rtx for expression EXP which is the address of an operand
1221 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1222 the maximum length of the block of memory that might be accessed or
1223 NULL if unknown. */
1225 static rtx
1226 get_memory_rtx (tree exp, tree len)
1228 tree orig_exp = exp;
1229 rtx addr, mem;
1231 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1232 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1233 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1234 exp = TREE_OPERAND (exp, 0);
1236 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1237 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1239 /* Get an expression we can use to find the attributes to assign to MEM.
1240 First remove any nops. */
1241 while (CONVERT_EXPR_P (exp)
1242 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1243 exp = TREE_OPERAND (exp, 0);
1245 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1246 (as builtin stringops may alias with anything). */
1247 exp = fold_build2 (MEM_REF,
1248 build_array_type (char_type_node,
1249 build_range_type (sizetype,
1250 size_one_node, len)),
1251 exp, build_int_cst (ptr_type_node, 0));
1253 /* If the MEM_REF has no acceptable address, try to get the base object
1254 from the original address we got, and build an all-aliasing
1255 unknown-sized access to that one. */
1256 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1257 set_mem_attributes (mem, exp, 0);
1258 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1259 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1260 0))))
1262 exp = build_fold_addr_expr (exp);
1263 exp = fold_build2 (MEM_REF,
1264 build_array_type (char_type_node,
1265 build_range_type (sizetype,
1266 size_zero_node,
1267 NULL)),
1268 exp, build_int_cst (ptr_type_node, 0));
1269 set_mem_attributes (mem, exp, 0);
1271 set_mem_alias_set (mem, 0);
1272 return mem;
1275 /* Built-in functions to perform an untyped call and return. */
1277 #define apply_args_mode \
1278 (this_target_builtins->x_apply_args_mode)
1279 #define apply_result_mode \
1280 (this_target_builtins->x_apply_result_mode)
1282 /* Return the size required for the block returned by __builtin_apply_args,
1283 and initialize apply_args_mode. */
1285 static int
1286 apply_args_size (void)
1288 static int size = -1;
1289 int align;
1290 unsigned int regno;
1291 machine_mode mode;
1293 /* The values computed by this function never change. */
1294 if (size < 0)
1296 /* The first value is the incoming arg-pointer. */
1297 size = GET_MODE_SIZE (Pmode);
1299 /* The second value is the structure value address unless this is
1300 passed as an "invisible" first argument. */
1301 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1302 size += GET_MODE_SIZE (Pmode);
1304 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1305 if (FUNCTION_ARG_REGNO_P (regno))
1307 mode = targetm.calls.get_raw_arg_mode (regno);
1309 gcc_assert (mode != VOIDmode);
1311 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1312 if (size % align != 0)
1313 size = CEIL (size, align) * align;
1314 size += GET_MODE_SIZE (mode);
1315 apply_args_mode[regno] = mode;
1317 else
1319 apply_args_mode[regno] = VOIDmode;
1322 return size;
1325 /* Return the size required for the block returned by __builtin_apply,
1326 and initialize apply_result_mode. */
1328 static int
1329 apply_result_size (void)
1331 static int size = -1;
1332 int align, regno;
1333 machine_mode mode;
1335 /* The values computed by this function never change. */
1336 if (size < 0)
1338 size = 0;
1340 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1341 if (targetm.calls.function_value_regno_p (regno))
1343 mode = targetm.calls.get_raw_result_mode (regno);
1345 gcc_assert (mode != VOIDmode);
1347 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1348 if (size % align != 0)
1349 size = CEIL (size, align) * align;
1350 size += GET_MODE_SIZE (mode);
1351 apply_result_mode[regno] = mode;
1353 else
1354 apply_result_mode[regno] = VOIDmode;
1356 /* Allow targets that use untyped_call and untyped_return to override
1357 the size so that machine-specific information can be stored here. */
1358 #ifdef APPLY_RESULT_SIZE
1359 size = APPLY_RESULT_SIZE;
1360 #endif
1362 return size;
1365 /* Create a vector describing the result block RESULT. If SAVEP is true,
1366 the result block is used to save the values; otherwise it is used to
1367 restore the values. */
1369 static rtx
1370 result_vector (int savep, rtx result)
1372 int regno, size, align, nelts;
1373 machine_mode mode;
1374 rtx reg, mem;
1375 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1377 size = nelts = 0;
1378 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1379 if ((mode = apply_result_mode[regno]) != VOIDmode)
1381 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1382 if (size % align != 0)
1383 size = CEIL (size, align) * align;
1384 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1385 mem = adjust_address (result, mode, size);
1386 savevec[nelts++] = (savep
1387 ? gen_rtx_SET (mem, reg)
1388 : gen_rtx_SET (reg, mem));
1389 size += GET_MODE_SIZE (mode);
1391 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1394 /* Save the state required to perform an untyped call with the same
1395 arguments as were passed to the current function. */
1397 static rtx
1398 expand_builtin_apply_args_1 (void)
1400 rtx registers, tem;
1401 int size, align, regno;
1402 machine_mode mode;
1403 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1405 /* Create a block where the arg-pointer, structure value address,
1406 and argument registers can be saved. */
1407 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1409 /* Walk past the arg-pointer and structure value address. */
1410 size = GET_MODE_SIZE (Pmode);
1411 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1412 size += GET_MODE_SIZE (Pmode);
1414 /* Save each register used in calling a function to the block. */
1415 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1416 if ((mode = apply_args_mode[regno]) != VOIDmode)
1418 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1419 if (size % align != 0)
1420 size = CEIL (size, align) * align;
1422 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1424 emit_move_insn (adjust_address (registers, mode, size), tem);
1425 size += GET_MODE_SIZE (mode);
1428 /* Save the arg pointer to the block. */
1429 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1430 /* We need the pointer as the caller actually passed them to us, not
1431 as we might have pretended they were passed. Make sure it's a valid
1432 operand, as emit_move_insn isn't expected to handle a PLUS. */
1433 if (STACK_GROWS_DOWNWARD)
1435 = force_operand (plus_constant (Pmode, tem,
1436 crtl->args.pretend_args_size),
1437 NULL_RTX);
1438 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1440 size = GET_MODE_SIZE (Pmode);
1442 /* Save the structure value address unless this is passed as an
1443 "invisible" first argument. */
1444 if (struct_incoming_value)
1446 emit_move_insn (adjust_address (registers, Pmode, size),
1447 copy_to_reg (struct_incoming_value));
1448 size += GET_MODE_SIZE (Pmode);
1451 /* Return the address of the block. */
1452 return copy_addr_to_reg (XEXP (registers, 0));
1455 /* __builtin_apply_args returns block of memory allocated on
1456 the stack into which is stored the arg pointer, structure
1457 value address, static chain, and all the registers that might
1458 possibly be used in performing a function call. The code is
1459 moved to the start of the function so the incoming values are
1460 saved. */
1462 static rtx
1463 expand_builtin_apply_args (void)
1465 /* Don't do __builtin_apply_args more than once in a function.
1466 Save the result of the first call and reuse it. */
1467 if (apply_args_value != 0)
1468 return apply_args_value;
1470 /* When this function is called, it means that registers must be
1471 saved on entry to this function. So we migrate the
1472 call to the first insn of this function. */
1473 rtx temp;
1475 start_sequence ();
1476 temp = expand_builtin_apply_args_1 ();
1477 rtx_insn *seq = get_insns ();
1478 end_sequence ();
1480 apply_args_value = temp;
1482 /* Put the insns after the NOTE that starts the function.
1483 If this is inside a start_sequence, make the outer-level insn
1484 chain current, so the code is placed at the start of the
1485 function. If internal_arg_pointer is a non-virtual pseudo,
1486 it needs to be placed after the function that initializes
1487 that pseudo. */
1488 push_topmost_sequence ();
1489 if (REG_P (crtl->args.internal_arg_pointer)
1490 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1491 emit_insn_before (seq, parm_birth_insn);
1492 else
1493 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1494 pop_topmost_sequence ();
1495 return temp;
1499 /* Perform an untyped call and save the state required to perform an
1500 untyped return of whatever value was returned by the given function. */
1502 static rtx
1503 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1505 int size, align, regno;
1506 machine_mode mode;
1507 rtx incoming_args, result, reg, dest, src;
1508 rtx_call_insn *call_insn;
1509 rtx old_stack_level = 0;
1510 rtx call_fusage = 0;
1511 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1513 arguments = convert_memory_address (Pmode, arguments);
1515 /* Create a block where the return registers can be saved. */
1516 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1518 /* Fetch the arg pointer from the ARGUMENTS block. */
1519 incoming_args = gen_reg_rtx (Pmode);
1520 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1521 if (!STACK_GROWS_DOWNWARD)
1522 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1523 incoming_args, 0, OPTAB_LIB_WIDEN);
1525 /* Push a new argument block and copy the arguments. Do not allow
1526 the (potential) memcpy call below to interfere with our stack
1527 manipulations. */
1528 do_pending_stack_adjust ();
1529 NO_DEFER_POP;
1531 /* Save the stack with nonlocal if available. */
1532 if (targetm.have_save_stack_nonlocal ())
1533 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1534 else
1535 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1537 /* Allocate a block of memory onto the stack and copy the memory
1538 arguments to the outgoing arguments address. We can pass TRUE
1539 as the 4th argument because we just saved the stack pointer
1540 and will restore it right after the call. */
1541 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1543 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1544 may have already set current_function_calls_alloca to true.
1545 current_function_calls_alloca won't be set if argsize is zero,
1546 so we have to guarantee need_drap is true here. */
1547 if (SUPPORTS_STACK_ALIGNMENT)
1548 crtl->need_drap = true;
1550 dest = virtual_outgoing_args_rtx;
1551 if (!STACK_GROWS_DOWNWARD)
1553 if (CONST_INT_P (argsize))
1554 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1555 else
1556 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1558 dest = gen_rtx_MEM (BLKmode, dest);
1559 set_mem_align (dest, PARM_BOUNDARY);
1560 src = gen_rtx_MEM (BLKmode, incoming_args);
1561 set_mem_align (src, PARM_BOUNDARY);
1562 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1564 /* Refer to the argument block. */
1565 apply_args_size ();
1566 arguments = gen_rtx_MEM (BLKmode, arguments);
1567 set_mem_align (arguments, PARM_BOUNDARY);
1569 /* Walk past the arg-pointer and structure value address. */
1570 size = GET_MODE_SIZE (Pmode);
1571 if (struct_value)
1572 size += GET_MODE_SIZE (Pmode);
1574 /* Restore each of the registers previously saved. Make USE insns
1575 for each of these registers for use in making the call. */
1576 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1577 if ((mode = apply_args_mode[regno]) != VOIDmode)
1579 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1580 if (size % align != 0)
1581 size = CEIL (size, align) * align;
1582 reg = gen_rtx_REG (mode, regno);
1583 emit_move_insn (reg, adjust_address (arguments, mode, size));
1584 use_reg (&call_fusage, reg);
1585 size += GET_MODE_SIZE (mode);
1588 /* Restore the structure value address unless this is passed as an
1589 "invisible" first argument. */
1590 size = GET_MODE_SIZE (Pmode);
1591 if (struct_value)
1593 rtx value = gen_reg_rtx (Pmode);
1594 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1595 emit_move_insn (struct_value, value);
1596 if (REG_P (struct_value))
1597 use_reg (&call_fusage, struct_value);
1598 size += GET_MODE_SIZE (Pmode);
1601 /* All arguments and registers used for the call are set up by now! */
1602 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1604 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1605 and we don't want to load it into a register as an optimization,
1606 because prepare_call_address already did it if it should be done. */
1607 if (GET_CODE (function) != SYMBOL_REF)
1608 function = memory_address (FUNCTION_MODE, function);
1610 /* Generate the actual call instruction and save the return value. */
1611 if (targetm.have_untyped_call ())
1613 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1614 emit_call_insn (targetm.gen_untyped_call (mem, result,
1615 result_vector (1, result)));
1617 else if (targetm.have_call_value ())
1619 rtx valreg = 0;
1621 /* Locate the unique return register. It is not possible to
1622 express a call that sets more than one return register using
1623 call_value; use untyped_call for that. In fact, untyped_call
1624 only needs to save the return registers in the given block. */
1625 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1626 if ((mode = apply_result_mode[regno]) != VOIDmode)
1628 gcc_assert (!valreg); /* have_untyped_call required. */
1630 valreg = gen_rtx_REG (mode, regno);
1633 emit_insn (targetm.gen_call_value (valreg,
1634 gen_rtx_MEM (FUNCTION_MODE, function),
1635 const0_rtx, NULL_RTX, const0_rtx));
1637 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1639 else
1640 gcc_unreachable ();
1642 /* Find the CALL insn we just emitted, and attach the register usage
1643 information. */
1644 call_insn = last_call_insn ();
1645 add_function_usage_to (call_insn, call_fusage);
1647 /* Restore the stack. */
1648 if (targetm.have_save_stack_nonlocal ())
1649 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1650 else
1651 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1652 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1654 OK_DEFER_POP;
1656 /* Return the address of the result block. */
1657 result = copy_addr_to_reg (XEXP (result, 0));
1658 return convert_memory_address (ptr_mode, result);
1661 /* Perform an untyped return. */
1663 static void
1664 expand_builtin_return (rtx result)
1666 int size, align, regno;
1667 machine_mode mode;
1668 rtx reg;
1669 rtx_insn *call_fusage = 0;
1671 result = convert_memory_address (Pmode, result);
1673 apply_result_size ();
1674 result = gen_rtx_MEM (BLKmode, result);
1676 if (targetm.have_untyped_return ())
1678 rtx vector = result_vector (0, result);
1679 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1680 emit_barrier ();
1681 return;
1684 /* Restore the return value and note that each value is used. */
1685 size = 0;
1686 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1687 if ((mode = apply_result_mode[regno]) != VOIDmode)
1689 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1690 if (size % align != 0)
1691 size = CEIL (size, align) * align;
1692 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1693 emit_move_insn (reg, adjust_address (result, mode, size));
1695 push_to_sequence (call_fusage);
1696 emit_use (reg);
1697 call_fusage = get_insns ();
1698 end_sequence ();
1699 size += GET_MODE_SIZE (mode);
1702 /* Put the USE insns before the return. */
1703 emit_insn (call_fusage);
1705 /* Return whatever values was restored by jumping directly to the end
1706 of the function. */
1707 expand_naked_return ();
1710 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1712 static enum type_class
1713 type_to_class (tree type)
1715 switch (TREE_CODE (type))
1717 case VOID_TYPE: return void_type_class;
1718 case INTEGER_TYPE: return integer_type_class;
1719 case ENUMERAL_TYPE: return enumeral_type_class;
1720 case BOOLEAN_TYPE: return boolean_type_class;
1721 case POINTER_TYPE: return pointer_type_class;
1722 case REFERENCE_TYPE: return reference_type_class;
1723 case OFFSET_TYPE: return offset_type_class;
1724 case REAL_TYPE: return real_type_class;
1725 case COMPLEX_TYPE: return complex_type_class;
1726 case FUNCTION_TYPE: return function_type_class;
1727 case METHOD_TYPE: return method_type_class;
1728 case RECORD_TYPE: return record_type_class;
1729 case UNION_TYPE:
1730 case QUAL_UNION_TYPE: return union_type_class;
1731 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1732 ? string_type_class : array_type_class);
1733 case LANG_TYPE: return lang_type_class;
1734 default: return no_type_class;
1738 /* Expand a call EXP to __builtin_classify_type. */
1740 static rtx
1741 expand_builtin_classify_type (tree exp)
1743 if (call_expr_nargs (exp))
1744 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1745 return GEN_INT (no_type_class);
1748 /* This helper macro, meant to be used in mathfn_built_in below,
1749 determines which among a set of three builtin math functions is
1750 appropriate for a given type mode. The `F' and `L' cases are
1751 automatically generated from the `double' case. */
1752 #define CASE_MATHFN(MATHFN) \
1753 CASE_CFN_##MATHFN: \
1754 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1755 fcodel = BUILT_IN_##MATHFN##L ; break;
1756 /* Similar to above, but appends _R after any F/L suffix. */
1757 #define CASE_MATHFN_REENT(MATHFN) \
1758 case CFN_BUILT_IN_##MATHFN##_R: \
1759 case CFN_BUILT_IN_##MATHFN##F_R: \
1760 case CFN_BUILT_IN_##MATHFN##L_R: \
1761 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1762 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1764 /* Return a function equivalent to FN but operating on floating-point
1765 values of type TYPE, or END_BUILTINS if no such function exists.
1766 This is purely an operation on function codes; it does not guarantee
1767 that the target actually has an implementation of the function. */
1769 static built_in_function
1770 mathfn_built_in_2 (tree type, combined_fn fn)
1772 built_in_function fcode, fcodef, fcodel;
1774 switch (fn)
1776 CASE_MATHFN (ACOS)
1777 CASE_MATHFN (ACOSH)
1778 CASE_MATHFN (ASIN)
1779 CASE_MATHFN (ASINH)
1780 CASE_MATHFN (ATAN)
1781 CASE_MATHFN (ATAN2)
1782 CASE_MATHFN (ATANH)
1783 CASE_MATHFN (CBRT)
1784 CASE_MATHFN (CEIL)
1785 CASE_MATHFN (CEXPI)
1786 CASE_MATHFN (COPYSIGN)
1787 CASE_MATHFN (COS)
1788 CASE_MATHFN (COSH)
1789 CASE_MATHFN (DREM)
1790 CASE_MATHFN (ERF)
1791 CASE_MATHFN (ERFC)
1792 CASE_MATHFN (EXP)
1793 CASE_MATHFN (EXP10)
1794 CASE_MATHFN (EXP2)
1795 CASE_MATHFN (EXPM1)
1796 CASE_MATHFN (FABS)
1797 CASE_MATHFN (FDIM)
1798 CASE_MATHFN (FLOOR)
1799 CASE_MATHFN (FMA)
1800 CASE_MATHFN (FMAX)
1801 CASE_MATHFN (FMIN)
1802 CASE_MATHFN (FMOD)
1803 CASE_MATHFN (FREXP)
1804 CASE_MATHFN (GAMMA)
1805 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1806 CASE_MATHFN (HUGE_VAL)
1807 CASE_MATHFN (HYPOT)
1808 CASE_MATHFN (ILOGB)
1809 CASE_MATHFN (ICEIL)
1810 CASE_MATHFN (IFLOOR)
1811 CASE_MATHFN (INF)
1812 CASE_MATHFN (IRINT)
1813 CASE_MATHFN (IROUND)
1814 CASE_MATHFN (ISINF)
1815 CASE_MATHFN (J0)
1816 CASE_MATHFN (J1)
1817 CASE_MATHFN (JN)
1818 CASE_MATHFN (LCEIL)
1819 CASE_MATHFN (LDEXP)
1820 CASE_MATHFN (LFLOOR)
1821 CASE_MATHFN (LGAMMA)
1822 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1823 CASE_MATHFN (LLCEIL)
1824 CASE_MATHFN (LLFLOOR)
1825 CASE_MATHFN (LLRINT)
1826 CASE_MATHFN (LLROUND)
1827 CASE_MATHFN (LOG)
1828 CASE_MATHFN (LOG10)
1829 CASE_MATHFN (LOG1P)
1830 CASE_MATHFN (LOG2)
1831 CASE_MATHFN (LOGB)
1832 CASE_MATHFN (LRINT)
1833 CASE_MATHFN (LROUND)
1834 CASE_MATHFN (MODF)
1835 CASE_MATHFN (NAN)
1836 CASE_MATHFN (NANS)
1837 CASE_MATHFN (NEARBYINT)
1838 CASE_MATHFN (NEXTAFTER)
1839 CASE_MATHFN (NEXTTOWARD)
1840 CASE_MATHFN (POW)
1841 CASE_MATHFN (POWI)
1842 CASE_MATHFN (POW10)
1843 CASE_MATHFN (REMAINDER)
1844 CASE_MATHFN (REMQUO)
1845 CASE_MATHFN (RINT)
1846 CASE_MATHFN (ROUND)
1847 CASE_MATHFN (SCALB)
1848 CASE_MATHFN (SCALBLN)
1849 CASE_MATHFN (SCALBN)
1850 CASE_MATHFN (SIGNBIT)
1851 CASE_MATHFN (SIGNIFICAND)
1852 CASE_MATHFN (SIN)
1853 CASE_MATHFN (SINCOS)
1854 CASE_MATHFN (SINH)
1855 CASE_MATHFN (SQRT)
1856 CASE_MATHFN (TAN)
1857 CASE_MATHFN (TANH)
1858 CASE_MATHFN (TGAMMA)
1859 CASE_MATHFN (TRUNC)
1860 CASE_MATHFN (Y0)
1861 CASE_MATHFN (Y1)
1862 CASE_MATHFN (YN)
1864 default:
1865 return END_BUILTINS;
1868 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1869 return fcode;
1870 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1871 return fcodef;
1872 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1873 return fcodel;
1874 else
1875 return END_BUILTINS;
1878 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1879 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1880 otherwise use the explicit declaration. If we can't do the conversion,
1881 return null. */
1883 static tree
1884 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
1886 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
1887 if (fcode2 == END_BUILTINS)
1888 return NULL_TREE;
1890 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1891 return NULL_TREE;
1893 return builtin_decl_explicit (fcode2);
1896 /* Like mathfn_built_in_1, but always use the implicit array. */
1898 tree
1899 mathfn_built_in (tree type, combined_fn fn)
1901 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1904 /* Like mathfn_built_in_1, but take a built_in_function and
1905 always use the implicit array. */
1907 tree
1908 mathfn_built_in (tree type, enum built_in_function fn)
1910 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
1913 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
1914 return its code, otherwise return IFN_LAST. Note that this function
1915 only tests whether the function is defined in internals.def, not whether
1916 it is actually available on the target. */
1918 internal_fn
1919 associated_internal_fn (tree fndecl)
1921 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
1922 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
1923 switch (DECL_FUNCTION_CODE (fndecl))
1925 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
1926 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1927 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
1928 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1929 #include "internal-fn.def"
1931 CASE_FLT_FN (BUILT_IN_POW10):
1932 return IFN_EXP10;
1934 CASE_FLT_FN (BUILT_IN_DREM):
1935 return IFN_REMAINDER;
1937 CASE_FLT_FN (BUILT_IN_SCALBN):
1938 CASE_FLT_FN (BUILT_IN_SCALBLN):
1939 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
1940 return IFN_LDEXP;
1941 return IFN_LAST;
1943 default:
1944 return IFN_LAST;
1948 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
1949 on the current target by a call to an internal function, return the
1950 code of that internal function, otherwise return IFN_LAST. The caller
1951 is responsible for ensuring that any side-effects of the built-in
1952 call are dealt with correctly. E.g. if CALL sets errno, the caller
1953 must decide that the errno result isn't needed or make it available
1954 in some other way. */
1956 internal_fn
1957 replacement_internal_fn (gcall *call)
1959 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1961 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
1962 if (ifn != IFN_LAST)
1964 tree_pair types = direct_internal_fn_types (ifn, call);
1965 if (direct_internal_fn_supported_p (ifn, types))
1966 return ifn;
1969 return IFN_LAST;
1972 /* Expand a call to the builtin trinary math functions (fma).
1973 Return NULL_RTX if a normal call should be emitted rather than expanding the
1974 function in-line. EXP is the expression that is a call to the builtin
1975 function; if convenient, the result should be placed in TARGET.
1976 SUBTARGET may be used as the target for computing one of EXP's
1977 operands. */
1979 static rtx
1980 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
1982 optab builtin_optab;
1983 rtx op0, op1, op2, result;
1984 rtx_insn *insns;
1985 tree fndecl = get_callee_fndecl (exp);
1986 tree arg0, arg1, arg2;
1987 machine_mode mode;
1989 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1990 return NULL_RTX;
1992 arg0 = CALL_EXPR_ARG (exp, 0);
1993 arg1 = CALL_EXPR_ARG (exp, 1);
1994 arg2 = CALL_EXPR_ARG (exp, 2);
1996 switch (DECL_FUNCTION_CODE (fndecl))
1998 CASE_FLT_FN (BUILT_IN_FMA):
1999 builtin_optab = fma_optab; break;
2000 default:
2001 gcc_unreachable ();
2004 /* Make a suitable register to place result in. */
2005 mode = TYPE_MODE (TREE_TYPE (exp));
2007 /* Before working hard, check whether the instruction is available. */
2008 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2009 return NULL_RTX;
2011 result = gen_reg_rtx (mode);
2013 /* Always stabilize the argument list. */
2014 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2015 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2016 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2018 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2019 op1 = expand_normal (arg1);
2020 op2 = expand_normal (arg2);
2022 start_sequence ();
2024 /* Compute into RESULT.
2025 Set RESULT to wherever the result comes back. */
2026 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2027 result, 0);
2029 /* If we were unable to expand via the builtin, stop the sequence
2030 (without outputting the insns) and call to the library function
2031 with the stabilized argument list. */
2032 if (result == 0)
2034 end_sequence ();
2035 return expand_call (exp, target, target == const0_rtx);
2038 /* Output the entire sequence. */
2039 insns = get_insns ();
2040 end_sequence ();
2041 emit_insn (insns);
2043 return result;
2046 /* Expand a call to the builtin sin and cos math functions.
2047 Return NULL_RTX if a normal call should be emitted rather than expanding the
2048 function in-line. EXP is the expression that is a call to the builtin
2049 function; if convenient, the result should be placed in TARGET.
2050 SUBTARGET may be used as the target for computing one of EXP's
2051 operands. */
2053 static rtx
2054 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2056 optab builtin_optab;
2057 rtx op0;
2058 rtx_insn *insns;
2059 tree fndecl = get_callee_fndecl (exp);
2060 machine_mode mode;
2061 tree arg;
2063 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2064 return NULL_RTX;
2066 arg = CALL_EXPR_ARG (exp, 0);
2068 switch (DECL_FUNCTION_CODE (fndecl))
2070 CASE_FLT_FN (BUILT_IN_SIN):
2071 CASE_FLT_FN (BUILT_IN_COS):
2072 builtin_optab = sincos_optab; break;
2073 default:
2074 gcc_unreachable ();
2077 /* Make a suitable register to place result in. */
2078 mode = TYPE_MODE (TREE_TYPE (exp));
2080 /* Check if sincos insn is available, otherwise fallback
2081 to sin or cos insn. */
2082 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2083 switch (DECL_FUNCTION_CODE (fndecl))
2085 CASE_FLT_FN (BUILT_IN_SIN):
2086 builtin_optab = sin_optab; break;
2087 CASE_FLT_FN (BUILT_IN_COS):
2088 builtin_optab = cos_optab; break;
2089 default:
2090 gcc_unreachable ();
2093 /* Before working hard, check whether the instruction is available. */
2094 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2096 rtx result = gen_reg_rtx (mode);
2098 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2099 need to expand the argument again. This way, we will not perform
2100 side-effects more the once. */
2101 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2103 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2105 start_sequence ();
2107 /* Compute into RESULT.
2108 Set RESULT to wherever the result comes back. */
2109 if (builtin_optab == sincos_optab)
2111 int ok;
2113 switch (DECL_FUNCTION_CODE (fndecl))
2115 CASE_FLT_FN (BUILT_IN_SIN):
2116 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2117 break;
2118 CASE_FLT_FN (BUILT_IN_COS):
2119 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2120 break;
2121 default:
2122 gcc_unreachable ();
2124 gcc_assert (ok);
2126 else
2127 result = expand_unop (mode, builtin_optab, op0, result, 0);
2129 if (result != 0)
2131 /* Output the entire sequence. */
2132 insns = get_insns ();
2133 end_sequence ();
2134 emit_insn (insns);
2135 return result;
2138 /* If we were unable to expand via the builtin, stop the sequence
2139 (without outputting the insns) and call to the library function
2140 with the stabilized argument list. */
2141 end_sequence ();
2144 return expand_call (exp, target, target == const0_rtx);
2147 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2148 return an RTL instruction code that implements the functionality.
2149 If that isn't possible or available return CODE_FOR_nothing. */
2151 static enum insn_code
2152 interclass_mathfn_icode (tree arg, tree fndecl)
2154 bool errno_set = false;
2155 optab builtin_optab = unknown_optab;
2156 machine_mode mode;
2158 switch (DECL_FUNCTION_CODE (fndecl))
2160 CASE_FLT_FN (BUILT_IN_ILOGB):
2161 errno_set = true; builtin_optab = ilogb_optab; break;
2162 CASE_FLT_FN (BUILT_IN_ISINF):
2163 builtin_optab = isinf_optab; break;
2164 case BUILT_IN_ISNORMAL:
2165 case BUILT_IN_ISFINITE:
2166 CASE_FLT_FN (BUILT_IN_FINITE):
2167 case BUILT_IN_FINITED32:
2168 case BUILT_IN_FINITED64:
2169 case BUILT_IN_FINITED128:
2170 case BUILT_IN_ISINFD32:
2171 case BUILT_IN_ISINFD64:
2172 case BUILT_IN_ISINFD128:
2173 /* These builtins have no optabs (yet). */
2174 break;
2175 default:
2176 gcc_unreachable ();
2179 /* There's no easy way to detect the case we need to set EDOM. */
2180 if (flag_errno_math && errno_set)
2181 return CODE_FOR_nothing;
2183 /* Optab mode depends on the mode of the input argument. */
2184 mode = TYPE_MODE (TREE_TYPE (arg));
2186 if (builtin_optab)
2187 return optab_handler (builtin_optab, mode);
2188 return CODE_FOR_nothing;
2191 /* Expand a call to one of the builtin math functions that operate on
2192 floating point argument and output an integer result (ilogb, isinf,
2193 isnan, etc).
2194 Return 0 if a normal call should be emitted rather than expanding the
2195 function in-line. EXP is the expression that is a call to the builtin
2196 function; if convenient, the result should be placed in TARGET. */
2198 static rtx
2199 expand_builtin_interclass_mathfn (tree exp, rtx target)
2201 enum insn_code icode = CODE_FOR_nothing;
2202 rtx op0;
2203 tree fndecl = get_callee_fndecl (exp);
2204 machine_mode mode;
2205 tree arg;
2207 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2208 return NULL_RTX;
2210 arg = CALL_EXPR_ARG (exp, 0);
2211 icode = interclass_mathfn_icode (arg, fndecl);
2212 mode = TYPE_MODE (TREE_TYPE (arg));
2214 if (icode != CODE_FOR_nothing)
2216 struct expand_operand ops[1];
2217 rtx_insn *last = get_last_insn ();
2218 tree orig_arg = arg;
2220 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2221 need to expand the argument again. This way, we will not perform
2222 side-effects more the once. */
2223 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2225 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2227 if (mode != GET_MODE (op0))
2228 op0 = convert_to_mode (mode, op0, 0);
2230 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2231 if (maybe_legitimize_operands (icode, 0, 1, ops)
2232 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2233 return ops[0].value;
2235 delete_insns_since (last);
2236 CALL_EXPR_ARG (exp, 0) = orig_arg;
2239 return NULL_RTX;
2242 /* Expand a call to the builtin sincos math function.
2243 Return NULL_RTX if a normal call should be emitted rather than expanding the
2244 function in-line. EXP is the expression that is a call to the builtin
2245 function. */
2247 static rtx
2248 expand_builtin_sincos (tree exp)
2250 rtx op0, op1, op2, target1, target2;
2251 machine_mode mode;
2252 tree arg, sinp, cosp;
2253 int result;
2254 location_t loc = EXPR_LOCATION (exp);
2255 tree alias_type, alias_off;
2257 if (!validate_arglist (exp, REAL_TYPE,
2258 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2259 return NULL_RTX;
2261 arg = CALL_EXPR_ARG (exp, 0);
2262 sinp = CALL_EXPR_ARG (exp, 1);
2263 cosp = CALL_EXPR_ARG (exp, 2);
2265 /* Make a suitable register to place result in. */
2266 mode = TYPE_MODE (TREE_TYPE (arg));
2268 /* Check if sincos insn is available, otherwise emit the call. */
2269 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2270 return NULL_RTX;
2272 target1 = gen_reg_rtx (mode);
2273 target2 = gen_reg_rtx (mode);
2275 op0 = expand_normal (arg);
2276 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2277 alias_off = build_int_cst (alias_type, 0);
2278 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2279 sinp, alias_off));
2280 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2281 cosp, alias_off));
2283 /* Compute into target1 and target2.
2284 Set TARGET to wherever the result comes back. */
2285 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2286 gcc_assert (result);
2288 /* Move target1 and target2 to the memory locations indicated
2289 by op1 and op2. */
2290 emit_move_insn (op1, target1);
2291 emit_move_insn (op2, target2);
2293 return const0_rtx;
2296 /* Expand a call to the internal cexpi builtin to the sincos math function.
2297 EXP is the expression that is a call to the builtin function; if convenient,
2298 the result should be placed in TARGET. */
2300 static rtx
2301 expand_builtin_cexpi (tree exp, rtx target)
2303 tree fndecl = get_callee_fndecl (exp);
2304 tree arg, type;
2305 machine_mode mode;
2306 rtx op0, op1, op2;
2307 location_t loc = EXPR_LOCATION (exp);
2309 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2310 return NULL_RTX;
2312 arg = CALL_EXPR_ARG (exp, 0);
2313 type = TREE_TYPE (arg);
2314 mode = TYPE_MODE (TREE_TYPE (arg));
2316 /* Try expanding via a sincos optab, fall back to emitting a libcall
2317 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2318 is only generated from sincos, cexp or if we have either of them. */
2319 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2321 op1 = gen_reg_rtx (mode);
2322 op2 = gen_reg_rtx (mode);
2324 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2326 /* Compute into op1 and op2. */
2327 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2329 else if (targetm.libc_has_function (function_sincos))
2331 tree call, fn = NULL_TREE;
2332 tree top1, top2;
2333 rtx op1a, op2a;
2335 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2336 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2337 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2338 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2339 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2340 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2341 else
2342 gcc_unreachable ();
2344 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2345 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2346 op1a = copy_addr_to_reg (XEXP (op1, 0));
2347 op2a = copy_addr_to_reg (XEXP (op2, 0));
2348 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2349 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2351 /* Make sure not to fold the sincos call again. */
2352 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2353 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2354 call, 3, arg, top1, top2));
2356 else
2358 tree call, fn = NULL_TREE, narg;
2359 tree ctype = build_complex_type (type);
2361 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2362 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2363 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2364 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2365 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2366 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2367 else
2368 gcc_unreachable ();
2370 /* If we don't have a decl for cexp create one. This is the
2371 friendliest fallback if the user calls __builtin_cexpi
2372 without full target C99 function support. */
2373 if (fn == NULL_TREE)
2375 tree fntype;
2376 const char *name = NULL;
2378 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2379 name = "cexpf";
2380 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2381 name = "cexp";
2382 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2383 name = "cexpl";
2385 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2386 fn = build_fn_decl (name, fntype);
2389 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2390 build_real (type, dconst0), arg);
2392 /* Make sure not to fold the cexp call again. */
2393 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2394 return expand_expr (build_call_nary (ctype, call, 1, narg),
2395 target, VOIDmode, EXPAND_NORMAL);
2398 /* Now build the proper return type. */
2399 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2400 make_tree (TREE_TYPE (arg), op2),
2401 make_tree (TREE_TYPE (arg), op1)),
2402 target, VOIDmode, EXPAND_NORMAL);
2405 /* Conveniently construct a function call expression. FNDECL names the
2406 function to be called, N is the number of arguments, and the "..."
2407 parameters are the argument expressions. Unlike build_call_exr
2408 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2410 static tree
2411 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2413 va_list ap;
2414 tree fntype = TREE_TYPE (fndecl);
2415 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2417 va_start (ap, n);
2418 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2419 va_end (ap);
2420 SET_EXPR_LOCATION (fn, loc);
2421 return fn;
2424 /* Expand a call to one of the builtin rounding functions gcc defines
2425 as an extension (lfloor and lceil). As these are gcc extensions we
2426 do not need to worry about setting errno to EDOM.
2427 If expanding via optab fails, lower expression to (int)(floor(x)).
2428 EXP is the expression that is a call to the builtin function;
2429 if convenient, the result should be placed in TARGET. */
2431 static rtx
2432 expand_builtin_int_roundingfn (tree exp, rtx target)
2434 convert_optab builtin_optab;
2435 rtx op0, tmp;
2436 rtx_insn *insns;
2437 tree fndecl = get_callee_fndecl (exp);
2438 enum built_in_function fallback_fn;
2439 tree fallback_fndecl;
2440 machine_mode mode;
2441 tree arg;
2443 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2444 gcc_unreachable ();
2446 arg = CALL_EXPR_ARG (exp, 0);
2448 switch (DECL_FUNCTION_CODE (fndecl))
2450 CASE_FLT_FN (BUILT_IN_ICEIL):
2451 CASE_FLT_FN (BUILT_IN_LCEIL):
2452 CASE_FLT_FN (BUILT_IN_LLCEIL):
2453 builtin_optab = lceil_optab;
2454 fallback_fn = BUILT_IN_CEIL;
2455 break;
2457 CASE_FLT_FN (BUILT_IN_IFLOOR):
2458 CASE_FLT_FN (BUILT_IN_LFLOOR):
2459 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2460 builtin_optab = lfloor_optab;
2461 fallback_fn = BUILT_IN_FLOOR;
2462 break;
2464 default:
2465 gcc_unreachable ();
2468 /* Make a suitable register to place result in. */
2469 mode = TYPE_MODE (TREE_TYPE (exp));
2471 target = gen_reg_rtx (mode);
2473 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2474 need to expand the argument again. This way, we will not perform
2475 side-effects more the once. */
2476 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2478 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2480 start_sequence ();
2482 /* Compute into TARGET. */
2483 if (expand_sfix_optab (target, op0, builtin_optab))
2485 /* Output the entire sequence. */
2486 insns = get_insns ();
2487 end_sequence ();
2488 emit_insn (insns);
2489 return target;
2492 /* If we were unable to expand via the builtin, stop the sequence
2493 (without outputting the insns). */
2494 end_sequence ();
2496 /* Fall back to floating point rounding optab. */
2497 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2499 /* For non-C99 targets we may end up without a fallback fndecl here
2500 if the user called __builtin_lfloor directly. In this case emit
2501 a call to the floor/ceil variants nevertheless. This should result
2502 in the best user experience for not full C99 targets. */
2503 if (fallback_fndecl == NULL_TREE)
2505 tree fntype;
2506 const char *name = NULL;
2508 switch (DECL_FUNCTION_CODE (fndecl))
2510 case BUILT_IN_ICEIL:
2511 case BUILT_IN_LCEIL:
2512 case BUILT_IN_LLCEIL:
2513 name = "ceil";
2514 break;
2515 case BUILT_IN_ICEILF:
2516 case BUILT_IN_LCEILF:
2517 case BUILT_IN_LLCEILF:
2518 name = "ceilf";
2519 break;
2520 case BUILT_IN_ICEILL:
2521 case BUILT_IN_LCEILL:
2522 case BUILT_IN_LLCEILL:
2523 name = "ceill";
2524 break;
2525 case BUILT_IN_IFLOOR:
2526 case BUILT_IN_LFLOOR:
2527 case BUILT_IN_LLFLOOR:
2528 name = "floor";
2529 break;
2530 case BUILT_IN_IFLOORF:
2531 case BUILT_IN_LFLOORF:
2532 case BUILT_IN_LLFLOORF:
2533 name = "floorf";
2534 break;
2535 case BUILT_IN_IFLOORL:
2536 case BUILT_IN_LFLOORL:
2537 case BUILT_IN_LLFLOORL:
2538 name = "floorl";
2539 break;
2540 default:
2541 gcc_unreachable ();
2544 fntype = build_function_type_list (TREE_TYPE (arg),
2545 TREE_TYPE (arg), NULL_TREE);
2546 fallback_fndecl = build_fn_decl (name, fntype);
2549 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2551 tmp = expand_normal (exp);
2552 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2554 /* Truncate the result of floating point optab to integer
2555 via expand_fix (). */
2556 target = gen_reg_rtx (mode);
2557 expand_fix (target, tmp, 0);
2559 return target;
2562 /* Expand a call to one of the builtin math functions doing integer
2563 conversion (lrint).
2564 Return 0 if a normal call should be emitted rather than expanding the
2565 function in-line. EXP is the expression that is a call to the builtin
2566 function; if convenient, the result should be placed in TARGET. */
2568 static rtx
2569 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2571 convert_optab builtin_optab;
2572 rtx op0;
2573 rtx_insn *insns;
2574 tree fndecl = get_callee_fndecl (exp);
2575 tree arg;
2576 machine_mode mode;
2577 enum built_in_function fallback_fn = BUILT_IN_NONE;
2579 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2580 gcc_unreachable ();
2582 arg = CALL_EXPR_ARG (exp, 0);
2584 switch (DECL_FUNCTION_CODE (fndecl))
2586 CASE_FLT_FN (BUILT_IN_IRINT):
2587 fallback_fn = BUILT_IN_LRINT;
2588 /* FALLTHRU */
2589 CASE_FLT_FN (BUILT_IN_LRINT):
2590 CASE_FLT_FN (BUILT_IN_LLRINT):
2591 builtin_optab = lrint_optab;
2592 break;
2594 CASE_FLT_FN (BUILT_IN_IROUND):
2595 fallback_fn = BUILT_IN_LROUND;
2596 /* FALLTHRU */
2597 CASE_FLT_FN (BUILT_IN_LROUND):
2598 CASE_FLT_FN (BUILT_IN_LLROUND):
2599 builtin_optab = lround_optab;
2600 break;
2602 default:
2603 gcc_unreachable ();
2606 /* There's no easy way to detect the case we need to set EDOM. */
2607 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2608 return NULL_RTX;
2610 /* Make a suitable register to place result in. */
2611 mode = TYPE_MODE (TREE_TYPE (exp));
2613 /* There's no easy way to detect the case we need to set EDOM. */
2614 if (!flag_errno_math)
2616 rtx result = gen_reg_rtx (mode);
2618 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2619 need to expand the argument again. This way, we will not perform
2620 side-effects more the once. */
2621 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2623 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2625 start_sequence ();
2627 if (expand_sfix_optab (result, op0, builtin_optab))
2629 /* Output the entire sequence. */
2630 insns = get_insns ();
2631 end_sequence ();
2632 emit_insn (insns);
2633 return result;
2636 /* If we were unable to expand via the builtin, stop the sequence
2637 (without outputting the insns) and call to the library function
2638 with the stabilized argument list. */
2639 end_sequence ();
2642 if (fallback_fn != BUILT_IN_NONE)
2644 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2645 targets, (int) round (x) should never be transformed into
2646 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2647 a call to lround in the hope that the target provides at least some
2648 C99 functions. This should result in the best user experience for
2649 not full C99 targets. */
2650 tree fallback_fndecl = mathfn_built_in_1
2651 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2653 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2654 fallback_fndecl, 1, arg);
2656 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2657 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2658 return convert_to_mode (mode, target, 0);
2661 return expand_call (exp, target, target == const0_rtx);
2664 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2665 a normal call should be emitted rather than expanding the function
2666 in-line. EXP is the expression that is a call to the builtin
2667 function; if convenient, the result should be placed in TARGET. */
2669 static rtx
2670 expand_builtin_powi (tree exp, rtx target)
2672 tree arg0, arg1;
2673 rtx op0, op1;
2674 machine_mode mode;
2675 machine_mode mode2;
2677 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2678 return NULL_RTX;
2680 arg0 = CALL_EXPR_ARG (exp, 0);
2681 arg1 = CALL_EXPR_ARG (exp, 1);
2682 mode = TYPE_MODE (TREE_TYPE (exp));
2684 /* Emit a libcall to libgcc. */
2686 /* Mode of the 2nd argument must match that of an int. */
2687 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2689 if (target == NULL_RTX)
2690 target = gen_reg_rtx (mode);
2692 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2693 if (GET_MODE (op0) != mode)
2694 op0 = convert_to_mode (mode, op0, 0);
2695 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2696 if (GET_MODE (op1) != mode2)
2697 op1 = convert_to_mode (mode2, op1, 0);
2699 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2700 target, LCT_CONST, mode, 2,
2701 op0, mode, op1, mode2);
2703 return target;
2706 /* Expand expression EXP which is a call to the strlen builtin. Return
2707 NULL_RTX if we failed the caller should emit a normal call, otherwise
2708 try to get the result in TARGET, if convenient. */
2710 static rtx
2711 expand_builtin_strlen (tree exp, rtx target,
2712 machine_mode target_mode)
2714 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2715 return NULL_RTX;
2716 else
2718 struct expand_operand ops[4];
2719 rtx pat;
2720 tree len;
2721 tree src = CALL_EXPR_ARG (exp, 0);
2722 rtx src_reg;
2723 rtx_insn *before_strlen;
2724 machine_mode insn_mode = target_mode;
2725 enum insn_code icode = CODE_FOR_nothing;
2726 unsigned int align;
2728 /* If the length can be computed at compile-time, return it. */
2729 len = c_strlen (src, 0);
2730 if (len)
2731 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2733 /* If the length can be computed at compile-time and is constant
2734 integer, but there are side-effects in src, evaluate
2735 src for side-effects, then return len.
2736 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2737 can be optimized into: i++; x = 3; */
2738 len = c_strlen (src, 1);
2739 if (len && TREE_CODE (len) == INTEGER_CST)
2741 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2742 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2745 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2747 /* If SRC is not a pointer type, don't do this operation inline. */
2748 if (align == 0)
2749 return NULL_RTX;
2751 /* Bail out if we can't compute strlen in the right mode. */
2752 while (insn_mode != VOIDmode)
2754 icode = optab_handler (strlen_optab, insn_mode);
2755 if (icode != CODE_FOR_nothing)
2756 break;
2758 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2760 if (insn_mode == VOIDmode)
2761 return NULL_RTX;
2763 /* Make a place to hold the source address. We will not expand
2764 the actual source until we are sure that the expansion will
2765 not fail -- there are trees that cannot be expanded twice. */
2766 src_reg = gen_reg_rtx (Pmode);
2768 /* Mark the beginning of the strlen sequence so we can emit the
2769 source operand later. */
2770 before_strlen = get_last_insn ();
2772 create_output_operand (&ops[0], target, insn_mode);
2773 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2774 create_integer_operand (&ops[2], 0);
2775 create_integer_operand (&ops[3], align);
2776 if (!maybe_expand_insn (icode, 4, ops))
2777 return NULL_RTX;
2779 /* Now that we are assured of success, expand the source. */
2780 start_sequence ();
2781 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2782 if (pat != src_reg)
2784 #ifdef POINTERS_EXTEND_UNSIGNED
2785 if (GET_MODE (pat) != Pmode)
2786 pat = convert_to_mode (Pmode, pat,
2787 POINTERS_EXTEND_UNSIGNED);
2788 #endif
2789 emit_move_insn (src_reg, pat);
2791 pat = get_insns ();
2792 end_sequence ();
2794 if (before_strlen)
2795 emit_insn_after (pat, before_strlen);
2796 else
2797 emit_insn_before (pat, get_insns ());
2799 /* Return the value in the proper mode for this function. */
2800 if (GET_MODE (ops[0].value) == target_mode)
2801 target = ops[0].value;
2802 else if (target != 0)
2803 convert_move (target, ops[0].value, 0);
2804 else
2805 target = convert_to_mode (target_mode, ops[0].value, 0);
2807 return target;
2811 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2812 bytes from constant string DATA + OFFSET and return it as target
2813 constant. */
2815 static rtx
2816 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2817 machine_mode mode)
2819 const char *str = (const char *) data;
2821 gcc_assert (offset >= 0
2822 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2823 <= strlen (str) + 1));
2825 return c_readstr (str + offset, mode);
2828 /* LEN specify length of the block of memcpy/memset operation.
2829 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
2830 In some cases we can make very likely guess on max size, then we
2831 set it into PROBABLE_MAX_SIZE. */
2833 static void
2834 determine_block_size (tree len, rtx len_rtx,
2835 unsigned HOST_WIDE_INT *min_size,
2836 unsigned HOST_WIDE_INT *max_size,
2837 unsigned HOST_WIDE_INT *probable_max_size)
2839 if (CONST_INT_P (len_rtx))
2841 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
2842 return;
2844 else
2846 wide_int min, max;
2847 enum value_range_type range_type = VR_UNDEFINED;
2849 /* Determine bounds from the type. */
2850 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
2851 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
2852 else
2853 *min_size = 0;
2854 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
2855 *probable_max_size = *max_size
2856 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
2857 else
2858 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
2860 if (TREE_CODE (len) == SSA_NAME)
2861 range_type = get_range_info (len, &min, &max);
2862 if (range_type == VR_RANGE)
2864 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
2865 *min_size = min.to_uhwi ();
2866 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
2867 *probable_max_size = *max_size = max.to_uhwi ();
2869 else if (range_type == VR_ANTI_RANGE)
2871 /* Anti range 0...N lets us to determine minimal size to N+1. */
2872 if (min == 0)
2874 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
2875 *min_size = max.to_uhwi () + 1;
2877 /* Code like
2879 int n;
2880 if (n < 100)
2881 memcpy (a, b, n)
2883 Produce anti range allowing negative values of N. We still
2884 can use the information and make a guess that N is not negative.
2886 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
2887 *probable_max_size = min.to_uhwi () - 1;
2890 gcc_checking_assert (*max_size <=
2891 (unsigned HOST_WIDE_INT)
2892 GET_MODE_MASK (GET_MODE (len_rtx)));
2895 /* Helper function to do the actual work for expand_builtin_memcpy. */
2897 static rtx
2898 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
2900 const char *src_str;
2901 unsigned int src_align = get_pointer_alignment (src);
2902 unsigned int dest_align = get_pointer_alignment (dest);
2903 rtx dest_mem, src_mem, dest_addr, len_rtx;
2904 HOST_WIDE_INT expected_size = -1;
2905 unsigned int expected_align = 0;
2906 unsigned HOST_WIDE_INT min_size;
2907 unsigned HOST_WIDE_INT max_size;
2908 unsigned HOST_WIDE_INT probable_max_size;
2910 /* If DEST is not a pointer type, call the normal function. */
2911 if (dest_align == 0)
2912 return NULL_RTX;
2914 /* If either SRC is not a pointer type, don't do this
2915 operation in-line. */
2916 if (src_align == 0)
2917 return NULL_RTX;
2919 if (currently_expanding_gimple_stmt)
2920 stringop_block_profile (currently_expanding_gimple_stmt,
2921 &expected_align, &expected_size);
2923 if (expected_align < dest_align)
2924 expected_align = dest_align;
2925 dest_mem = get_memory_rtx (dest, len);
2926 set_mem_align (dest_mem, dest_align);
2927 len_rtx = expand_normal (len);
2928 determine_block_size (len, len_rtx, &min_size, &max_size,
2929 &probable_max_size);
2930 src_str = c_getstr (src);
2932 /* If SRC is a string constant and block move would be done
2933 by pieces, we can avoid loading the string from memory
2934 and only stored the computed constants. */
2935 if (src_str
2936 && CONST_INT_P (len_rtx)
2937 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2938 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2939 CONST_CAST (char *, src_str),
2940 dest_align, false))
2942 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2943 builtin_memcpy_read_str,
2944 CONST_CAST (char *, src_str),
2945 dest_align, false, 0);
2946 dest_mem = force_operand (XEXP (dest_mem, 0), target);
2947 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2948 return dest_mem;
2951 src_mem = get_memory_rtx (src, len);
2952 set_mem_align (src_mem, src_align);
2954 /* Copy word part most expediently. */
2955 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
2956 CALL_EXPR_TAILCALL (exp)
2957 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
2958 expected_align, expected_size,
2959 min_size, max_size, probable_max_size);
2961 if (dest_addr == 0)
2963 dest_addr = force_operand (XEXP (dest_mem, 0), target);
2964 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2967 return dest_addr;
2970 /* Expand a call EXP to the memcpy builtin.
2971 Return NULL_RTX if we failed, the caller should emit a normal call,
2972 otherwise try to get the result in TARGET, if convenient (and in
2973 mode MODE if that's convenient). */
2975 static rtx
2976 expand_builtin_memcpy (tree exp, rtx target)
2978 if (!validate_arglist (exp,
2979 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2980 return NULL_RTX;
2981 else
2983 tree dest = CALL_EXPR_ARG (exp, 0);
2984 tree src = CALL_EXPR_ARG (exp, 1);
2985 tree len = CALL_EXPR_ARG (exp, 2);
2986 return expand_builtin_memcpy_args (dest, src, len, target, exp);
2990 /* Expand an instrumented call EXP to the memcpy builtin.
2991 Return NULL_RTX if we failed, the caller should emit a normal call,
2992 otherwise try to get the result in TARGET, if convenient (and in
2993 mode MODE if that's convenient). */
2995 static rtx
2996 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
2998 if (!validate_arglist (exp,
2999 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3000 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3001 INTEGER_TYPE, VOID_TYPE))
3002 return NULL_RTX;
3003 else
3005 tree dest = CALL_EXPR_ARG (exp, 0);
3006 tree src = CALL_EXPR_ARG (exp, 2);
3007 tree len = CALL_EXPR_ARG (exp, 4);
3008 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3010 /* Return src bounds with the result. */
3011 if (res)
3013 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3014 expand_normal (CALL_EXPR_ARG (exp, 1)));
3015 res = chkp_join_splitted_slot (res, bnd);
3017 return res;
3021 /* Expand a call EXP to the mempcpy builtin.
3022 Return NULL_RTX if we failed; the caller should emit a normal call,
3023 otherwise try to get the result in TARGET, if convenient (and in
3024 mode MODE if that's convenient). If ENDP is 0 return the
3025 destination pointer, if ENDP is 1 return the end pointer ala
3026 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3027 stpcpy. */
3029 static rtx
3030 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3032 if (!validate_arglist (exp,
3033 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3034 return NULL_RTX;
3035 else
3037 tree dest = CALL_EXPR_ARG (exp, 0);
3038 tree src = CALL_EXPR_ARG (exp, 1);
3039 tree len = CALL_EXPR_ARG (exp, 2);
3040 return expand_builtin_mempcpy_args (dest, src, len,
3041 target, mode, /*endp=*/ 1,
3042 exp);
3046 /* Expand an instrumented call EXP to the mempcpy builtin.
3047 Return NULL_RTX if we failed, the caller should emit a normal call,
3048 otherwise try to get the result in TARGET, if convenient (and in
3049 mode MODE if that's convenient). */
3051 static rtx
3052 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3054 if (!validate_arglist (exp,
3055 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3056 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3057 INTEGER_TYPE, VOID_TYPE))
3058 return NULL_RTX;
3059 else
3061 tree dest = CALL_EXPR_ARG (exp, 0);
3062 tree src = CALL_EXPR_ARG (exp, 2);
3063 tree len = CALL_EXPR_ARG (exp, 4);
3064 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3065 mode, 1, exp);
3067 /* Return src bounds with the result. */
3068 if (res)
3070 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3071 expand_normal (CALL_EXPR_ARG (exp, 1)));
3072 res = chkp_join_splitted_slot (res, bnd);
3074 return res;
3078 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3079 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3080 so that this can also be called without constructing an actual CALL_EXPR.
3081 The other arguments and return value are the same as for
3082 expand_builtin_mempcpy. */
3084 static rtx
3085 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3086 rtx target, machine_mode mode, int endp,
3087 tree orig_exp)
3089 tree fndecl = get_callee_fndecl (orig_exp);
3091 /* If return value is ignored, transform mempcpy into memcpy. */
3092 if (target == const0_rtx
3093 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3094 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3096 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3097 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3098 dest, src, len);
3099 return expand_expr (result, target, mode, EXPAND_NORMAL);
3101 else if (target == const0_rtx
3102 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3104 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3105 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3106 dest, src, len);
3107 return expand_expr (result, target, mode, EXPAND_NORMAL);
3109 else
3111 const char *src_str;
3112 unsigned int src_align = get_pointer_alignment (src);
3113 unsigned int dest_align = get_pointer_alignment (dest);
3114 rtx dest_mem, src_mem, len_rtx;
3116 /* If either SRC or DEST is not a pointer type, don't do this
3117 operation in-line. */
3118 if (dest_align == 0 || src_align == 0)
3119 return NULL_RTX;
3121 /* If LEN is not constant, call the normal function. */
3122 if (! tree_fits_uhwi_p (len))
3123 return NULL_RTX;
3125 len_rtx = expand_normal (len);
3126 src_str = c_getstr (src);
3128 /* If SRC is a string constant and block move would be done
3129 by pieces, we can avoid loading the string from memory
3130 and only stored the computed constants. */
3131 if (src_str
3132 && CONST_INT_P (len_rtx)
3133 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3134 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3135 CONST_CAST (char *, src_str),
3136 dest_align, false))
3138 dest_mem = get_memory_rtx (dest, len);
3139 set_mem_align (dest_mem, dest_align);
3140 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3141 builtin_memcpy_read_str,
3142 CONST_CAST (char *, src_str),
3143 dest_align, false, endp);
3144 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3145 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3146 return dest_mem;
3149 if (CONST_INT_P (len_rtx)
3150 && can_move_by_pieces (INTVAL (len_rtx),
3151 MIN (dest_align, src_align)))
3153 dest_mem = get_memory_rtx (dest, len);
3154 set_mem_align (dest_mem, dest_align);
3155 src_mem = get_memory_rtx (src, len);
3156 set_mem_align (src_mem, src_align);
3157 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3158 MIN (dest_align, src_align), endp);
3159 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3160 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3161 return dest_mem;
3164 return NULL_RTX;
3168 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3169 we failed, the caller should emit a normal call, otherwise try to
3170 get the result in TARGET, if convenient. If ENDP is 0 return the
3171 destination pointer, if ENDP is 1 return the end pointer ala
3172 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3173 stpcpy. */
3175 static rtx
3176 expand_movstr (tree dest, tree src, rtx target, int endp)
3178 struct expand_operand ops[3];
3179 rtx dest_mem;
3180 rtx src_mem;
3182 if (!targetm.have_movstr ())
3183 return NULL_RTX;
3185 dest_mem = get_memory_rtx (dest, NULL);
3186 src_mem = get_memory_rtx (src, NULL);
3187 if (!endp)
3189 target = force_reg (Pmode, XEXP (dest_mem, 0));
3190 dest_mem = replace_equiv_address (dest_mem, target);
3193 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3194 create_fixed_operand (&ops[1], dest_mem);
3195 create_fixed_operand (&ops[2], src_mem);
3196 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3197 return NULL_RTX;
3199 if (endp && target != const0_rtx)
3201 target = ops[0].value;
3202 /* movstr is supposed to set end to the address of the NUL
3203 terminator. If the caller requested a mempcpy-like return value,
3204 adjust it. */
3205 if (endp == 1)
3207 rtx tem = plus_constant (GET_MODE (target),
3208 gen_lowpart (GET_MODE (target), target), 1);
3209 emit_move_insn (target, force_operand (tem, NULL_RTX));
3212 return target;
3215 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3216 NULL_RTX if we failed the caller should emit a normal call, otherwise
3217 try to get the result in TARGET, if convenient (and in mode MODE if that's
3218 convenient). */
3220 static rtx
3221 expand_builtin_strcpy (tree exp, rtx target)
3223 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3225 tree dest = CALL_EXPR_ARG (exp, 0);
3226 tree src = CALL_EXPR_ARG (exp, 1);
3227 return expand_builtin_strcpy_args (dest, src, target);
3229 return NULL_RTX;
3232 /* Helper function to do the actual work for expand_builtin_strcpy. The
3233 arguments to the builtin_strcpy call DEST and SRC are broken out
3234 so that this can also be called without constructing an actual CALL_EXPR.
3235 The other arguments and return value are the same as for
3236 expand_builtin_strcpy. */
3238 static rtx
3239 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3241 return expand_movstr (dest, src, target, /*endp=*/0);
3244 /* Expand a call EXP to the stpcpy builtin.
3245 Return NULL_RTX if we failed the caller should emit a normal call,
3246 otherwise try to get the result in TARGET, if convenient (and in
3247 mode MODE if that's convenient). */
3249 static rtx
3250 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3252 tree dst, src;
3253 location_t loc = EXPR_LOCATION (exp);
3255 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3256 return NULL_RTX;
3258 dst = CALL_EXPR_ARG (exp, 0);
3259 src = CALL_EXPR_ARG (exp, 1);
3261 /* If return value is ignored, transform stpcpy into strcpy. */
3262 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3264 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3265 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3266 return expand_expr (result, target, mode, EXPAND_NORMAL);
3268 else
3270 tree len, lenp1;
3271 rtx ret;
3273 /* Ensure we get an actual string whose length can be evaluated at
3274 compile-time, not an expression containing a string. This is
3275 because the latter will potentially produce pessimized code
3276 when used to produce the return value. */
3277 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3278 return expand_movstr (dst, src, target, /*endp=*/2);
3280 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3281 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3282 target, mode, /*endp=*/2,
3283 exp);
3285 if (ret)
3286 return ret;
3288 if (TREE_CODE (len) == INTEGER_CST)
3290 rtx len_rtx = expand_normal (len);
3292 if (CONST_INT_P (len_rtx))
3294 ret = expand_builtin_strcpy_args (dst, src, target);
3296 if (ret)
3298 if (! target)
3300 if (mode != VOIDmode)
3301 target = gen_reg_rtx (mode);
3302 else
3303 target = gen_reg_rtx (GET_MODE (ret));
3305 if (GET_MODE (target) != GET_MODE (ret))
3306 ret = gen_lowpart (GET_MODE (target), ret);
3308 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3309 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3310 gcc_assert (ret);
3312 return target;
3317 return expand_movstr (dst, src, target, /*endp=*/2);
3321 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3322 bytes from constant string DATA + OFFSET and return it as target
3323 constant. */
3326 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3327 machine_mode mode)
3329 const char *str = (const char *) data;
3331 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3332 return const0_rtx;
3334 return c_readstr (str + offset, mode);
3337 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3338 NULL_RTX if we failed the caller should emit a normal call. */
3340 static rtx
3341 expand_builtin_strncpy (tree exp, rtx target)
3343 location_t loc = EXPR_LOCATION (exp);
3345 if (validate_arglist (exp,
3346 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3348 tree dest = CALL_EXPR_ARG (exp, 0);
3349 tree src = CALL_EXPR_ARG (exp, 1);
3350 tree len = CALL_EXPR_ARG (exp, 2);
3351 tree slen = c_strlen (src, 1);
3353 /* We must be passed a constant len and src parameter. */
3354 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3355 return NULL_RTX;
3357 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3359 /* We're required to pad with trailing zeros if the requested
3360 len is greater than strlen(s2)+1. In that case try to
3361 use store_by_pieces, if it fails, punt. */
3362 if (tree_int_cst_lt (slen, len))
3364 unsigned int dest_align = get_pointer_alignment (dest);
3365 const char *p = c_getstr (src);
3366 rtx dest_mem;
3368 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3369 || !can_store_by_pieces (tree_to_uhwi (len),
3370 builtin_strncpy_read_str,
3371 CONST_CAST (char *, p),
3372 dest_align, false))
3373 return NULL_RTX;
3375 dest_mem = get_memory_rtx (dest, len);
3376 store_by_pieces (dest_mem, tree_to_uhwi (len),
3377 builtin_strncpy_read_str,
3378 CONST_CAST (char *, p), dest_align, false, 0);
3379 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3380 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3381 return dest_mem;
3384 return NULL_RTX;
3387 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3388 bytes from constant string DATA + OFFSET and return it as target
3389 constant. */
3392 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3393 machine_mode mode)
3395 const char *c = (const char *) data;
3396 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3398 memset (p, *c, GET_MODE_SIZE (mode));
3400 return c_readstr (p, mode);
3403 /* Callback routine for store_by_pieces. Return the RTL of a register
3404 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3405 char value given in the RTL register data. For example, if mode is
3406 4 bytes wide, return the RTL for 0x01010101*data. */
3408 static rtx
3409 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3410 machine_mode mode)
3412 rtx target, coeff;
3413 size_t size;
3414 char *p;
3416 size = GET_MODE_SIZE (mode);
3417 if (size == 1)
3418 return (rtx) data;
3420 p = XALLOCAVEC (char, size);
3421 memset (p, 1, size);
3422 coeff = c_readstr (p, mode);
3424 target = convert_to_mode (mode, (rtx) data, 1);
3425 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3426 return force_reg (mode, target);
3429 /* Expand expression EXP, which is a call to the memset builtin. Return
3430 NULL_RTX if we failed the caller should emit a normal call, otherwise
3431 try to get the result in TARGET, if convenient (and in mode MODE if that's
3432 convenient). */
3434 static rtx
3435 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3437 if (!validate_arglist (exp,
3438 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3439 return NULL_RTX;
3440 else
3442 tree dest = CALL_EXPR_ARG (exp, 0);
3443 tree val = CALL_EXPR_ARG (exp, 1);
3444 tree len = CALL_EXPR_ARG (exp, 2);
3445 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3449 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3450 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3451 try to get the result in TARGET, if convenient (and in mode MODE if that's
3452 convenient). */
3454 static rtx
3455 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3457 if (!validate_arglist (exp,
3458 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3459 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3460 return NULL_RTX;
3461 else
3463 tree dest = CALL_EXPR_ARG (exp, 0);
3464 tree val = CALL_EXPR_ARG (exp, 2);
3465 tree len = CALL_EXPR_ARG (exp, 3);
3466 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
3468 /* Return src bounds with the result. */
3469 if (res)
3471 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3472 expand_normal (CALL_EXPR_ARG (exp, 1)));
3473 res = chkp_join_splitted_slot (res, bnd);
3475 return res;
3479 /* Helper function to do the actual work for expand_builtin_memset. The
3480 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3481 so that this can also be called without constructing an actual CALL_EXPR.
3482 The other arguments and return value are the same as for
3483 expand_builtin_memset. */
3485 static rtx
3486 expand_builtin_memset_args (tree dest, tree val, tree len,
3487 rtx target, machine_mode mode, tree orig_exp)
3489 tree fndecl, fn;
3490 enum built_in_function fcode;
3491 machine_mode val_mode;
3492 char c;
3493 unsigned int dest_align;
3494 rtx dest_mem, dest_addr, len_rtx;
3495 HOST_WIDE_INT expected_size = -1;
3496 unsigned int expected_align = 0;
3497 unsigned HOST_WIDE_INT min_size;
3498 unsigned HOST_WIDE_INT max_size;
3499 unsigned HOST_WIDE_INT probable_max_size;
3501 dest_align = get_pointer_alignment (dest);
3503 /* If DEST is not a pointer type, don't do this operation in-line. */
3504 if (dest_align == 0)
3505 return NULL_RTX;
3507 if (currently_expanding_gimple_stmt)
3508 stringop_block_profile (currently_expanding_gimple_stmt,
3509 &expected_align, &expected_size);
3511 if (expected_align < dest_align)
3512 expected_align = dest_align;
3514 /* If the LEN parameter is zero, return DEST. */
3515 if (integer_zerop (len))
3517 /* Evaluate and ignore VAL in case it has side-effects. */
3518 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3519 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3522 /* Stabilize the arguments in case we fail. */
3523 dest = builtin_save_expr (dest);
3524 val = builtin_save_expr (val);
3525 len = builtin_save_expr (len);
3527 len_rtx = expand_normal (len);
3528 determine_block_size (len, len_rtx, &min_size, &max_size,
3529 &probable_max_size);
3530 dest_mem = get_memory_rtx (dest, len);
3531 val_mode = TYPE_MODE (unsigned_char_type_node);
3533 if (TREE_CODE (val) != INTEGER_CST)
3535 rtx val_rtx;
3537 val_rtx = expand_normal (val);
3538 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3540 /* Assume that we can memset by pieces if we can store
3541 * the coefficients by pieces (in the required modes).
3542 * We can't pass builtin_memset_gen_str as that emits RTL. */
3543 c = 1;
3544 if (tree_fits_uhwi_p (len)
3545 && can_store_by_pieces (tree_to_uhwi (len),
3546 builtin_memset_read_str, &c, dest_align,
3547 true))
3549 val_rtx = force_reg (val_mode, val_rtx);
3550 store_by_pieces (dest_mem, tree_to_uhwi (len),
3551 builtin_memset_gen_str, val_rtx, dest_align,
3552 true, 0);
3554 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3555 dest_align, expected_align,
3556 expected_size, min_size, max_size,
3557 probable_max_size))
3558 goto do_libcall;
3560 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3561 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3562 return dest_mem;
3565 if (target_char_cast (val, &c))
3566 goto do_libcall;
3568 if (c)
3570 if (tree_fits_uhwi_p (len)
3571 && can_store_by_pieces (tree_to_uhwi (len),
3572 builtin_memset_read_str, &c, dest_align,
3573 true))
3574 store_by_pieces (dest_mem, tree_to_uhwi (len),
3575 builtin_memset_read_str, &c, dest_align, true, 0);
3576 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3577 gen_int_mode (c, val_mode),
3578 dest_align, expected_align,
3579 expected_size, min_size, max_size,
3580 probable_max_size))
3581 goto do_libcall;
3583 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3584 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3585 return dest_mem;
3588 set_mem_align (dest_mem, dest_align);
3589 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3590 CALL_EXPR_TAILCALL (orig_exp)
3591 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3592 expected_align, expected_size,
3593 min_size, max_size,
3594 probable_max_size);
3596 if (dest_addr == 0)
3598 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3599 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3602 return dest_addr;
3604 do_libcall:
3605 fndecl = get_callee_fndecl (orig_exp);
3606 fcode = DECL_FUNCTION_CODE (fndecl);
3607 if (fcode == BUILT_IN_MEMSET
3608 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
3609 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3610 dest, val, len);
3611 else if (fcode == BUILT_IN_BZERO)
3612 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3613 dest, len);
3614 else
3615 gcc_unreachable ();
3616 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3617 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3618 return expand_call (fn, target, target == const0_rtx);
3621 /* Expand expression EXP, which is a call to the bzero builtin. Return
3622 NULL_RTX if we failed the caller should emit a normal call. */
3624 static rtx
3625 expand_builtin_bzero (tree exp)
3627 tree dest, size;
3628 location_t loc = EXPR_LOCATION (exp);
3630 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3631 return NULL_RTX;
3633 dest = CALL_EXPR_ARG (exp, 0);
3634 size = CALL_EXPR_ARG (exp, 1);
3636 /* New argument list transforming bzero(ptr x, int y) to
3637 memset(ptr x, int 0, size_t y). This is done this way
3638 so that if it isn't expanded inline, we fallback to
3639 calling bzero instead of memset. */
3641 return expand_builtin_memset_args (dest, integer_zero_node,
3642 fold_convert_loc (loc,
3643 size_type_node, size),
3644 const0_rtx, VOIDmode, exp);
3647 /* Try to expand cmpstr operation ICODE with the given operands.
3648 Return the result rtx on success, otherwise return null. */
3650 static rtx
3651 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
3652 HOST_WIDE_INT align)
3654 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3656 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3657 target = NULL_RTX;
3659 struct expand_operand ops[4];
3660 create_output_operand (&ops[0], target, insn_mode);
3661 create_fixed_operand (&ops[1], arg1_rtx);
3662 create_fixed_operand (&ops[2], arg2_rtx);
3663 create_integer_operand (&ops[3], align);
3664 if (maybe_expand_insn (icode, 4, ops))
3665 return ops[0].value;
3666 return NULL_RTX;
3669 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
3670 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
3671 otherwise return null. */
3673 static rtx
3674 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
3675 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
3676 HOST_WIDE_INT align)
3678 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3680 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3681 target = NULL_RTX;
3683 struct expand_operand ops[5];
3684 create_output_operand (&ops[0], target, insn_mode);
3685 create_fixed_operand (&ops[1], arg1_rtx);
3686 create_fixed_operand (&ops[2], arg2_rtx);
3687 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
3688 TYPE_UNSIGNED (arg3_type));
3689 create_integer_operand (&ops[4], align);
3690 if (maybe_expand_insn (icode, 5, ops))
3691 return ops[0].value;
3692 return NULL_RTX;
3695 /* Expand expression EXP, which is a call to the memcmp built-in function.
3696 Return NULL_RTX if we failed and the caller should emit a normal call,
3697 otherwise try to get the result in TARGET, if convenient. */
3699 static rtx
3700 expand_builtin_memcmp (tree exp, rtx target)
3702 if (!validate_arglist (exp,
3703 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3704 return NULL_RTX;
3706 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3707 implementing memcmp because it will stop if it encounters two
3708 zero bytes. */
3709 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
3710 if (icode == CODE_FOR_nothing)
3711 return NULL_RTX;
3713 tree arg1 = CALL_EXPR_ARG (exp, 0);
3714 tree arg2 = CALL_EXPR_ARG (exp, 1);
3715 tree len = CALL_EXPR_ARG (exp, 2);
3717 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3718 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3720 /* If we don't have POINTER_TYPE, call the function. */
3721 if (arg1_align == 0 || arg2_align == 0)
3722 return NULL_RTX;
3724 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3725 location_t loc = EXPR_LOCATION (exp);
3726 rtx arg1_rtx = get_memory_rtx (arg1, len);
3727 rtx arg2_rtx = get_memory_rtx (arg2, len);
3728 rtx arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3730 /* Set MEM_SIZE as appropriate. */
3731 if (CONST_INT_P (arg3_rtx))
3733 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3734 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3737 rtx result = expand_cmpstrn_or_cmpmem (icode, target, arg1_rtx, arg2_rtx,
3738 TREE_TYPE (len), arg3_rtx,
3739 MIN (arg1_align, arg2_align));
3740 if (result)
3742 /* Return the value in the proper mode for this function. */
3743 if (GET_MODE (result) == mode)
3744 return result;
3746 if (target != 0)
3748 convert_move (target, result, 0);
3749 return target;
3752 return convert_to_mode (mode, result, 0);
3755 result = target;
3756 if (! (result != 0
3757 && REG_P (result) && GET_MODE (result) == mode
3758 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3759 result = gen_reg_rtx (mode);
3761 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3762 TYPE_MODE (integer_type_node), 3,
3763 XEXP (arg1_rtx, 0), Pmode,
3764 XEXP (arg2_rtx, 0), Pmode,
3765 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3766 TYPE_UNSIGNED (sizetype)),
3767 TYPE_MODE (sizetype));
3768 return result;
3771 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3772 if we failed the caller should emit a normal call, otherwise try to get
3773 the result in TARGET, if convenient. */
3775 static rtx
3776 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3778 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3779 return NULL_RTX;
3781 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
3782 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
3783 if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing)
3785 rtx arg1_rtx, arg2_rtx;
3786 tree fndecl, fn;
3787 tree arg1 = CALL_EXPR_ARG (exp, 0);
3788 tree arg2 = CALL_EXPR_ARG (exp, 1);
3789 rtx result = NULL_RTX;
3791 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3792 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3794 /* If we don't have POINTER_TYPE, call the function. */
3795 if (arg1_align == 0 || arg2_align == 0)
3796 return NULL_RTX;
3798 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3799 arg1 = builtin_save_expr (arg1);
3800 arg2 = builtin_save_expr (arg2);
3802 arg1_rtx = get_memory_rtx (arg1, NULL);
3803 arg2_rtx = get_memory_rtx (arg2, NULL);
3805 /* Try to call cmpstrsi. */
3806 if (cmpstr_icode != CODE_FOR_nothing)
3807 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
3808 MIN (arg1_align, arg2_align));
3810 /* Try to determine at least one length and call cmpstrnsi. */
3811 if (!result && cmpstrn_icode != CODE_FOR_nothing)
3813 tree len;
3814 rtx arg3_rtx;
3816 tree len1 = c_strlen (arg1, 1);
3817 tree len2 = c_strlen (arg2, 1);
3819 if (len1)
3820 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3821 if (len2)
3822 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3824 /* If we don't have a constant length for the first, use the length
3825 of the second, if we know it. We don't require a constant for
3826 this case; some cost analysis could be done if both are available
3827 but neither is constant. For now, assume they're equally cheap,
3828 unless one has side effects. If both strings have constant lengths,
3829 use the smaller. */
3831 if (!len1)
3832 len = len2;
3833 else if (!len2)
3834 len = len1;
3835 else if (TREE_SIDE_EFFECTS (len1))
3836 len = len2;
3837 else if (TREE_SIDE_EFFECTS (len2))
3838 len = len1;
3839 else if (TREE_CODE (len1) != INTEGER_CST)
3840 len = len2;
3841 else if (TREE_CODE (len2) != INTEGER_CST)
3842 len = len1;
3843 else if (tree_int_cst_lt (len1, len2))
3844 len = len1;
3845 else
3846 len = len2;
3848 /* If both arguments have side effects, we cannot optimize. */
3849 if (len && !TREE_SIDE_EFFECTS (len))
3851 arg3_rtx = expand_normal (len);
3852 result = expand_cmpstrn_or_cmpmem
3853 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
3854 arg3_rtx, MIN (arg1_align, arg2_align));
3858 if (result)
3860 /* Return the value in the proper mode for this function. */
3861 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3862 if (GET_MODE (result) == mode)
3863 return result;
3864 if (target == 0)
3865 return convert_to_mode (mode, result, 0);
3866 convert_move (target, result, 0);
3867 return target;
3870 /* Expand the library call ourselves using a stabilized argument
3871 list to avoid re-evaluating the function's arguments twice. */
3872 fndecl = get_callee_fndecl (exp);
3873 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3874 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3875 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3876 return expand_call (fn, target, target == const0_rtx);
3878 return NULL_RTX;
3881 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3882 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3883 the result in TARGET, if convenient. */
3885 static rtx
3886 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3887 ATTRIBUTE_UNUSED machine_mode mode)
3889 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3891 if (!validate_arglist (exp,
3892 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3893 return NULL_RTX;
3895 /* If c_strlen can determine an expression for one of the string
3896 lengths, and it doesn't have side effects, then emit cmpstrnsi
3897 using length MIN(strlen(string)+1, arg3). */
3898 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
3899 if (cmpstrn_icode != CODE_FOR_nothing)
3901 tree len, len1, len2;
3902 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3903 rtx result;
3904 tree fndecl, fn;
3905 tree arg1 = CALL_EXPR_ARG (exp, 0);
3906 tree arg2 = CALL_EXPR_ARG (exp, 1);
3907 tree arg3 = CALL_EXPR_ARG (exp, 2);
3909 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3910 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3912 len1 = c_strlen (arg1, 1);
3913 len2 = c_strlen (arg2, 1);
3915 if (len1)
3916 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3917 if (len2)
3918 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3920 /* If we don't have a constant length for the first, use the length
3921 of the second, if we know it. We don't require a constant for
3922 this case; some cost analysis could be done if both are available
3923 but neither is constant. For now, assume they're equally cheap,
3924 unless one has side effects. If both strings have constant lengths,
3925 use the smaller. */
3927 if (!len1)
3928 len = len2;
3929 else if (!len2)
3930 len = len1;
3931 else if (TREE_SIDE_EFFECTS (len1))
3932 len = len2;
3933 else if (TREE_SIDE_EFFECTS (len2))
3934 len = len1;
3935 else if (TREE_CODE (len1) != INTEGER_CST)
3936 len = len2;
3937 else if (TREE_CODE (len2) != INTEGER_CST)
3938 len = len1;
3939 else if (tree_int_cst_lt (len1, len2))
3940 len = len1;
3941 else
3942 len = len2;
3944 /* If both arguments have side effects, we cannot optimize. */
3945 if (!len || TREE_SIDE_EFFECTS (len))
3946 return NULL_RTX;
3948 /* The actual new length parameter is MIN(len,arg3). */
3949 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3950 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3952 /* If we don't have POINTER_TYPE, call the function. */
3953 if (arg1_align == 0 || arg2_align == 0)
3954 return NULL_RTX;
3956 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3957 arg1 = builtin_save_expr (arg1);
3958 arg2 = builtin_save_expr (arg2);
3959 len = builtin_save_expr (len);
3961 arg1_rtx = get_memory_rtx (arg1, len);
3962 arg2_rtx = get_memory_rtx (arg2, len);
3963 arg3_rtx = expand_normal (len);
3964 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
3965 arg2_rtx, TREE_TYPE (len), arg3_rtx,
3966 MIN (arg1_align, arg2_align));
3967 if (result)
3969 /* Return the value in the proper mode for this function. */
3970 mode = TYPE_MODE (TREE_TYPE (exp));
3971 if (GET_MODE (result) == mode)
3972 return result;
3973 if (target == 0)
3974 return convert_to_mode (mode, result, 0);
3975 convert_move (target, result, 0);
3976 return target;
3979 /* Expand the library call ourselves using a stabilized argument
3980 list to avoid re-evaluating the function's arguments twice. */
3981 fndecl = get_callee_fndecl (exp);
3982 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
3983 arg1, arg2, len);
3984 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3985 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3986 return expand_call (fn, target, target == const0_rtx);
3988 return NULL_RTX;
3991 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3992 if that's convenient. */
3995 expand_builtin_saveregs (void)
3997 rtx val;
3998 rtx_insn *seq;
4000 /* Don't do __builtin_saveregs more than once in a function.
4001 Save the result of the first call and reuse it. */
4002 if (saveregs_value != 0)
4003 return saveregs_value;
4005 /* When this function is called, it means that registers must be
4006 saved on entry to this function. So we migrate the call to the
4007 first insn of this function. */
4009 start_sequence ();
4011 /* Do whatever the machine needs done in this case. */
4012 val = targetm.calls.expand_builtin_saveregs ();
4014 seq = get_insns ();
4015 end_sequence ();
4017 saveregs_value = val;
4019 /* Put the insns after the NOTE that starts the function. If this
4020 is inside a start_sequence, make the outer-level insn chain current, so
4021 the code is placed at the start of the function. */
4022 push_topmost_sequence ();
4023 emit_insn_after (seq, entry_of_function ());
4024 pop_topmost_sequence ();
4026 return val;
4029 /* Expand a call to __builtin_next_arg. */
4031 static rtx
4032 expand_builtin_next_arg (void)
4034 /* Checking arguments is already done in fold_builtin_next_arg
4035 that must be called before this function. */
4036 return expand_binop (ptr_mode, add_optab,
4037 crtl->args.internal_arg_pointer,
4038 crtl->args.arg_offset_rtx,
4039 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4042 /* Make it easier for the backends by protecting the valist argument
4043 from multiple evaluations. */
4045 static tree
4046 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4048 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4050 /* The current way of determining the type of valist is completely
4051 bogus. We should have the information on the va builtin instead. */
4052 if (!vatype)
4053 vatype = targetm.fn_abi_va_list (cfun->decl);
4055 if (TREE_CODE (vatype) == ARRAY_TYPE)
4057 if (TREE_SIDE_EFFECTS (valist))
4058 valist = save_expr (valist);
4060 /* For this case, the backends will be expecting a pointer to
4061 vatype, but it's possible we've actually been given an array
4062 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4063 So fix it. */
4064 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4066 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4067 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4070 else
4072 tree pt = build_pointer_type (vatype);
4074 if (! needs_lvalue)
4076 if (! TREE_SIDE_EFFECTS (valist))
4077 return valist;
4079 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4080 TREE_SIDE_EFFECTS (valist) = 1;
4083 if (TREE_SIDE_EFFECTS (valist))
4084 valist = save_expr (valist);
4085 valist = fold_build2_loc (loc, MEM_REF,
4086 vatype, valist, build_int_cst (pt, 0));
4089 return valist;
4092 /* The "standard" definition of va_list is void*. */
4094 tree
4095 std_build_builtin_va_list (void)
4097 return ptr_type_node;
4100 /* The "standard" abi va_list is va_list_type_node. */
4102 tree
4103 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4105 return va_list_type_node;
4108 /* The "standard" type of va_list is va_list_type_node. */
4110 tree
4111 std_canonical_va_list_type (tree type)
4113 tree wtype, htype;
4115 if (INDIRECT_REF_P (type))
4116 type = TREE_TYPE (type);
4117 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4118 type = TREE_TYPE (type);
4119 wtype = va_list_type_node;
4120 htype = type;
4121 /* Treat structure va_list types. */
4122 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4123 htype = TREE_TYPE (htype);
4124 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4126 /* If va_list is an array type, the argument may have decayed
4127 to a pointer type, e.g. by being passed to another function.
4128 In that case, unwrap both types so that we can compare the
4129 underlying records. */
4130 if (TREE_CODE (htype) == ARRAY_TYPE
4131 || POINTER_TYPE_P (htype))
4133 wtype = TREE_TYPE (wtype);
4134 htype = TREE_TYPE (htype);
4137 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4138 return va_list_type_node;
4140 return NULL_TREE;
4143 /* The "standard" implementation of va_start: just assign `nextarg' to
4144 the variable. */
4146 void
4147 std_expand_builtin_va_start (tree valist, rtx nextarg)
4149 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4150 convert_move (va_r, nextarg, 0);
4152 /* We do not have any valid bounds for the pointer, so
4153 just store zero bounds for it. */
4154 if (chkp_function_instrumented_p (current_function_decl))
4155 chkp_expand_bounds_reset_for_mem (valist,
4156 make_tree (TREE_TYPE (valist),
4157 nextarg));
4160 /* Expand EXP, a call to __builtin_va_start. */
4162 static rtx
4163 expand_builtin_va_start (tree exp)
4165 rtx nextarg;
4166 tree valist;
4167 location_t loc = EXPR_LOCATION (exp);
4169 if (call_expr_nargs (exp) < 2)
4171 error_at (loc, "too few arguments to function %<va_start%>");
4172 return const0_rtx;
4175 if (fold_builtin_next_arg (exp, true))
4176 return const0_rtx;
4178 nextarg = expand_builtin_next_arg ();
4179 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4181 if (targetm.expand_builtin_va_start)
4182 targetm.expand_builtin_va_start (valist, nextarg);
4183 else
4184 std_expand_builtin_va_start (valist, nextarg);
4186 return const0_rtx;
4189 /* Expand EXP, a call to __builtin_va_end. */
4191 static rtx
4192 expand_builtin_va_end (tree exp)
4194 tree valist = CALL_EXPR_ARG (exp, 0);
4196 /* Evaluate for side effects, if needed. I hate macros that don't
4197 do that. */
4198 if (TREE_SIDE_EFFECTS (valist))
4199 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4201 return const0_rtx;
4204 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4205 builtin rather than just as an assignment in stdarg.h because of the
4206 nastiness of array-type va_list types. */
4208 static rtx
4209 expand_builtin_va_copy (tree exp)
4211 tree dst, src, t;
4212 location_t loc = EXPR_LOCATION (exp);
4214 dst = CALL_EXPR_ARG (exp, 0);
4215 src = CALL_EXPR_ARG (exp, 1);
4217 dst = stabilize_va_list_loc (loc, dst, 1);
4218 src = stabilize_va_list_loc (loc, src, 0);
4220 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4222 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4224 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4225 TREE_SIDE_EFFECTS (t) = 1;
4226 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4228 else
4230 rtx dstb, srcb, size;
4232 /* Evaluate to pointers. */
4233 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4234 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4235 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4236 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4238 dstb = convert_memory_address (Pmode, dstb);
4239 srcb = convert_memory_address (Pmode, srcb);
4241 /* "Dereference" to BLKmode memories. */
4242 dstb = gen_rtx_MEM (BLKmode, dstb);
4243 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4244 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4245 srcb = gen_rtx_MEM (BLKmode, srcb);
4246 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4247 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4249 /* Copy. */
4250 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4253 return const0_rtx;
4256 /* Expand a call to one of the builtin functions __builtin_frame_address or
4257 __builtin_return_address. */
4259 static rtx
4260 expand_builtin_frame_address (tree fndecl, tree exp)
4262 /* The argument must be a nonnegative integer constant.
4263 It counts the number of frames to scan up the stack.
4264 The value is either the frame pointer value or the return
4265 address saved in that frame. */
4266 if (call_expr_nargs (exp) == 0)
4267 /* Warning about missing arg was already issued. */
4268 return const0_rtx;
4269 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4271 error ("invalid argument to %qD", fndecl);
4272 return const0_rtx;
4274 else
4276 /* Number of frames to scan up the stack. */
4277 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4279 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4281 /* Some ports cannot access arbitrary stack frames. */
4282 if (tem == NULL)
4284 warning (0, "unsupported argument to %qD", fndecl);
4285 return const0_rtx;
4288 if (count)
4290 /* Warn since no effort is made to ensure that any frame
4291 beyond the current one exists or can be safely reached. */
4292 warning (OPT_Wframe_address, "calling %qD with "
4293 "a nonzero argument is unsafe", fndecl);
4296 /* For __builtin_frame_address, return what we've got. */
4297 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4298 return tem;
4300 if (!REG_P (tem)
4301 && ! CONSTANT_P (tem))
4302 tem = copy_addr_to_reg (tem);
4303 return tem;
4307 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4308 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4309 is the same as for allocate_dynamic_stack_space. */
4311 static rtx
4312 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4314 rtx op0;
4315 rtx result;
4316 bool valid_arglist;
4317 unsigned int align;
4318 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4319 == BUILT_IN_ALLOCA_WITH_ALIGN);
4321 valid_arglist
4322 = (alloca_with_align
4323 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4324 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4326 if (!valid_arglist)
4327 return NULL_RTX;
4329 /* Compute the argument. */
4330 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4332 /* Compute the alignment. */
4333 align = (alloca_with_align
4334 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4335 : BIGGEST_ALIGNMENT);
4337 /* Allocate the desired space. */
4338 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4339 result = convert_memory_address (ptr_mode, result);
4341 return result;
4344 /* Expand a call to bswap builtin in EXP.
4345 Return NULL_RTX if a normal call should be emitted rather than expanding the
4346 function in-line. If convenient, the result should be placed in TARGET.
4347 SUBTARGET may be used as the target for computing one of EXP's operands. */
4349 static rtx
4350 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4351 rtx subtarget)
4353 tree arg;
4354 rtx op0;
4356 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4357 return NULL_RTX;
4359 arg = CALL_EXPR_ARG (exp, 0);
4360 op0 = expand_expr (arg,
4361 subtarget && GET_MODE (subtarget) == target_mode
4362 ? subtarget : NULL_RTX,
4363 target_mode, EXPAND_NORMAL);
4364 if (GET_MODE (op0) != target_mode)
4365 op0 = convert_to_mode (target_mode, op0, 1);
4367 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4369 gcc_assert (target);
4371 return convert_to_mode (target_mode, target, 1);
4374 /* Expand a call to a unary builtin in EXP.
4375 Return NULL_RTX if a normal call should be emitted rather than expanding the
4376 function in-line. If convenient, the result should be placed in TARGET.
4377 SUBTARGET may be used as the target for computing one of EXP's operands. */
4379 static rtx
4380 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4381 rtx subtarget, optab op_optab)
4383 rtx op0;
4385 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4386 return NULL_RTX;
4388 /* Compute the argument. */
4389 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4390 (subtarget
4391 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4392 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4393 VOIDmode, EXPAND_NORMAL);
4394 /* Compute op, into TARGET if possible.
4395 Set TARGET to wherever the result comes back. */
4396 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4397 op_optab, op0, target, op_optab != clrsb_optab);
4398 gcc_assert (target);
4400 return convert_to_mode (target_mode, target, 0);
4403 /* Expand a call to __builtin_expect. We just return our argument
4404 as the builtin_expect semantic should've been already executed by
4405 tree branch prediction pass. */
4407 static rtx
4408 expand_builtin_expect (tree exp, rtx target)
4410 tree arg;
4412 if (call_expr_nargs (exp) < 2)
4413 return const0_rtx;
4414 arg = CALL_EXPR_ARG (exp, 0);
4416 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4417 /* When guessing was done, the hints should be already stripped away. */
4418 gcc_assert (!flag_guess_branch_prob
4419 || optimize == 0 || seen_error ());
4420 return target;
4423 /* Expand a call to __builtin_assume_aligned. We just return our first
4424 argument as the builtin_assume_aligned semantic should've been already
4425 executed by CCP. */
4427 static rtx
4428 expand_builtin_assume_aligned (tree exp, rtx target)
4430 if (call_expr_nargs (exp) < 2)
4431 return const0_rtx;
4432 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4433 EXPAND_NORMAL);
4434 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4435 && (call_expr_nargs (exp) < 3
4436 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4437 return target;
4440 void
4441 expand_builtin_trap (void)
4443 if (targetm.have_trap ())
4445 rtx_insn *insn = emit_insn (targetm.gen_trap ());
4446 /* For trap insns when not accumulating outgoing args force
4447 REG_ARGS_SIZE note to prevent crossjumping of calls with
4448 different args sizes. */
4449 if (!ACCUMULATE_OUTGOING_ARGS)
4450 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4452 else
4453 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4454 emit_barrier ();
4457 /* Expand a call to __builtin_unreachable. We do nothing except emit
4458 a barrier saying that control flow will not pass here.
4460 It is the responsibility of the program being compiled to ensure
4461 that control flow does never reach __builtin_unreachable. */
4462 static void
4463 expand_builtin_unreachable (void)
4465 emit_barrier ();
4468 /* Expand EXP, a call to fabs, fabsf or fabsl.
4469 Return NULL_RTX if a normal call should be emitted rather than expanding
4470 the function inline. If convenient, the result should be placed
4471 in TARGET. SUBTARGET may be used as the target for computing
4472 the operand. */
4474 static rtx
4475 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4477 machine_mode mode;
4478 tree arg;
4479 rtx op0;
4481 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4482 return NULL_RTX;
4484 arg = CALL_EXPR_ARG (exp, 0);
4485 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4486 mode = TYPE_MODE (TREE_TYPE (arg));
4487 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4488 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4491 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4492 Return NULL is a normal call should be emitted rather than expanding the
4493 function inline. If convenient, the result should be placed in TARGET.
4494 SUBTARGET may be used as the target for computing the operand. */
4496 static rtx
4497 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4499 rtx op0, op1;
4500 tree arg;
4502 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4503 return NULL_RTX;
4505 arg = CALL_EXPR_ARG (exp, 0);
4506 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4508 arg = CALL_EXPR_ARG (exp, 1);
4509 op1 = expand_normal (arg);
4511 return expand_copysign (op0, op1, target);
4514 /* Expand a call to __builtin___clear_cache. */
4516 static rtx
4517 expand_builtin___clear_cache (tree exp)
4519 if (!targetm.code_for_clear_cache)
4521 #ifdef CLEAR_INSN_CACHE
4522 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4523 does something. Just do the default expansion to a call to
4524 __clear_cache(). */
4525 return NULL_RTX;
4526 #else
4527 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4528 does nothing. There is no need to call it. Do nothing. */
4529 return const0_rtx;
4530 #endif /* CLEAR_INSN_CACHE */
4533 /* We have a "clear_cache" insn, and it will handle everything. */
4534 tree begin, end;
4535 rtx begin_rtx, end_rtx;
4537 /* We must not expand to a library call. If we did, any
4538 fallback library function in libgcc that might contain a call to
4539 __builtin___clear_cache() would recurse infinitely. */
4540 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4542 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4543 return const0_rtx;
4546 if (targetm.have_clear_cache ())
4548 struct expand_operand ops[2];
4550 begin = CALL_EXPR_ARG (exp, 0);
4551 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4553 end = CALL_EXPR_ARG (exp, 1);
4554 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4556 create_address_operand (&ops[0], begin_rtx);
4557 create_address_operand (&ops[1], end_rtx);
4558 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
4559 return const0_rtx;
4561 return const0_rtx;
4564 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4566 static rtx
4567 round_trampoline_addr (rtx tramp)
4569 rtx temp, addend, mask;
4571 /* If we don't need too much alignment, we'll have been guaranteed
4572 proper alignment by get_trampoline_type. */
4573 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4574 return tramp;
4576 /* Round address up to desired boundary. */
4577 temp = gen_reg_rtx (Pmode);
4578 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4579 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4581 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4582 temp, 0, OPTAB_LIB_WIDEN);
4583 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4584 temp, 0, OPTAB_LIB_WIDEN);
4586 return tramp;
4589 static rtx
4590 expand_builtin_init_trampoline (tree exp, bool onstack)
4592 tree t_tramp, t_func, t_chain;
4593 rtx m_tramp, r_tramp, r_chain, tmp;
4595 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4596 POINTER_TYPE, VOID_TYPE))
4597 return NULL_RTX;
4599 t_tramp = CALL_EXPR_ARG (exp, 0);
4600 t_func = CALL_EXPR_ARG (exp, 1);
4601 t_chain = CALL_EXPR_ARG (exp, 2);
4603 r_tramp = expand_normal (t_tramp);
4604 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4605 MEM_NOTRAP_P (m_tramp) = 1;
4607 /* If ONSTACK, the TRAMP argument should be the address of a field
4608 within the local function's FRAME decl. Either way, let's see if
4609 we can fill in the MEM_ATTRs for this memory. */
4610 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4611 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4613 /* Creator of a heap trampoline is responsible for making sure the
4614 address is aligned to at least STACK_BOUNDARY. Normally malloc
4615 will ensure this anyhow. */
4616 tmp = round_trampoline_addr (r_tramp);
4617 if (tmp != r_tramp)
4619 m_tramp = change_address (m_tramp, BLKmode, tmp);
4620 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4621 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4624 /* The FUNC argument should be the address of the nested function.
4625 Extract the actual function decl to pass to the hook. */
4626 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4627 t_func = TREE_OPERAND (t_func, 0);
4628 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4630 r_chain = expand_normal (t_chain);
4632 /* Generate insns to initialize the trampoline. */
4633 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4635 if (onstack)
4637 trampolines_created = 1;
4639 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4640 "trampoline generated for nested function %qD", t_func);
4643 return const0_rtx;
4646 static rtx
4647 expand_builtin_adjust_trampoline (tree exp)
4649 rtx tramp;
4651 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4652 return NULL_RTX;
4654 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4655 tramp = round_trampoline_addr (tramp);
4656 if (targetm.calls.trampoline_adjust_address)
4657 tramp = targetm.calls.trampoline_adjust_address (tramp);
4659 return tramp;
4662 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4663 function. The function first checks whether the back end provides
4664 an insn to implement signbit for the respective mode. If not, it
4665 checks whether the floating point format of the value is such that
4666 the sign bit can be extracted. If that is not the case, error out.
4667 EXP is the expression that is a call to the builtin function; if
4668 convenient, the result should be placed in TARGET. */
4669 static rtx
4670 expand_builtin_signbit (tree exp, rtx target)
4672 const struct real_format *fmt;
4673 machine_mode fmode, imode, rmode;
4674 tree arg;
4675 int word, bitpos;
4676 enum insn_code icode;
4677 rtx temp;
4678 location_t loc = EXPR_LOCATION (exp);
4680 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4681 return NULL_RTX;
4683 arg = CALL_EXPR_ARG (exp, 0);
4684 fmode = TYPE_MODE (TREE_TYPE (arg));
4685 rmode = TYPE_MODE (TREE_TYPE (exp));
4686 fmt = REAL_MODE_FORMAT (fmode);
4688 arg = builtin_save_expr (arg);
4690 /* Expand the argument yielding a RTX expression. */
4691 temp = expand_normal (arg);
4693 /* Check if the back end provides an insn that handles signbit for the
4694 argument's mode. */
4695 icode = optab_handler (signbit_optab, fmode);
4696 if (icode != CODE_FOR_nothing)
4698 rtx_insn *last = get_last_insn ();
4699 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4700 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4701 return target;
4702 delete_insns_since (last);
4705 /* For floating point formats without a sign bit, implement signbit
4706 as "ARG < 0.0". */
4707 bitpos = fmt->signbit_ro;
4708 if (bitpos < 0)
4710 /* But we can't do this if the format supports signed zero. */
4711 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
4713 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4714 build_real (TREE_TYPE (arg), dconst0));
4715 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4718 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4720 imode = int_mode_for_mode (fmode);
4721 gcc_assert (imode != BLKmode);
4722 temp = gen_lowpart (imode, temp);
4724 else
4726 imode = word_mode;
4727 /* Handle targets with different FP word orders. */
4728 if (FLOAT_WORDS_BIG_ENDIAN)
4729 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4730 else
4731 word = bitpos / BITS_PER_WORD;
4732 temp = operand_subword_force (temp, word, fmode);
4733 bitpos = bitpos % BITS_PER_WORD;
4736 /* Force the intermediate word_mode (or narrower) result into a
4737 register. This avoids attempting to create paradoxical SUBREGs
4738 of floating point modes below. */
4739 temp = force_reg (imode, temp);
4741 /* If the bitpos is within the "result mode" lowpart, the operation
4742 can be implement with a single bitwise AND. Otherwise, we need
4743 a right shift and an AND. */
4745 if (bitpos < GET_MODE_BITSIZE (rmode))
4747 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4749 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4750 temp = gen_lowpart (rmode, temp);
4751 temp = expand_binop (rmode, and_optab, temp,
4752 immed_wide_int_const (mask, rmode),
4753 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4755 else
4757 /* Perform a logical right shift to place the signbit in the least
4758 significant bit, then truncate the result to the desired mode
4759 and mask just this bit. */
4760 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4761 temp = gen_lowpart (rmode, temp);
4762 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4763 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4766 return temp;
4769 /* Expand fork or exec calls. TARGET is the desired target of the
4770 call. EXP is the call. FN is the
4771 identificator of the actual function. IGNORE is nonzero if the
4772 value is to be ignored. */
4774 static rtx
4775 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4777 tree id, decl;
4778 tree call;
4780 /* If we are not profiling, just call the function. */
4781 if (!profile_arc_flag)
4782 return NULL_RTX;
4784 /* Otherwise call the wrapper. This should be equivalent for the rest of
4785 compiler, so the code does not diverge, and the wrapper may run the
4786 code necessary for keeping the profiling sane. */
4788 switch (DECL_FUNCTION_CODE (fn))
4790 case BUILT_IN_FORK:
4791 id = get_identifier ("__gcov_fork");
4792 break;
4794 case BUILT_IN_EXECL:
4795 id = get_identifier ("__gcov_execl");
4796 break;
4798 case BUILT_IN_EXECV:
4799 id = get_identifier ("__gcov_execv");
4800 break;
4802 case BUILT_IN_EXECLP:
4803 id = get_identifier ("__gcov_execlp");
4804 break;
4806 case BUILT_IN_EXECLE:
4807 id = get_identifier ("__gcov_execle");
4808 break;
4810 case BUILT_IN_EXECVP:
4811 id = get_identifier ("__gcov_execvp");
4812 break;
4814 case BUILT_IN_EXECVE:
4815 id = get_identifier ("__gcov_execve");
4816 break;
4818 default:
4819 gcc_unreachable ();
4822 decl = build_decl (DECL_SOURCE_LOCATION (fn),
4823 FUNCTION_DECL, id, TREE_TYPE (fn));
4824 DECL_EXTERNAL (decl) = 1;
4825 TREE_PUBLIC (decl) = 1;
4826 DECL_ARTIFICIAL (decl) = 1;
4827 TREE_NOTHROW (decl) = 1;
4828 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4829 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4830 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
4831 return expand_call (call, target, ignore);
4836 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
4837 the pointer in these functions is void*, the tree optimizers may remove
4838 casts. The mode computed in expand_builtin isn't reliable either, due
4839 to __sync_bool_compare_and_swap.
4841 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
4842 group of builtins. This gives us log2 of the mode size. */
4844 static inline machine_mode
4845 get_builtin_sync_mode (int fcode_diff)
4847 /* The size is not negotiable, so ask not to get BLKmode in return
4848 if the target indicates that a smaller size would be better. */
4849 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
4852 /* Expand the memory expression LOC and return the appropriate memory operand
4853 for the builtin_sync operations. */
4855 static rtx
4856 get_builtin_sync_mem (tree loc, machine_mode mode)
4858 rtx addr, mem;
4860 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
4861 addr = convert_memory_address (Pmode, addr);
4863 /* Note that we explicitly do not want any alias information for this
4864 memory, so that we kill all other live memories. Otherwise we don't
4865 satisfy the full barrier semantics of the intrinsic. */
4866 mem = validize_mem (gen_rtx_MEM (mode, addr));
4868 /* The alignment needs to be at least according to that of the mode. */
4869 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
4870 get_pointer_alignment (loc)));
4871 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
4872 MEM_VOLATILE_P (mem) = 1;
4874 return mem;
4877 /* Make sure an argument is in the right mode.
4878 EXP is the tree argument.
4879 MODE is the mode it should be in. */
4881 static rtx
4882 expand_expr_force_mode (tree exp, machine_mode mode)
4884 rtx val;
4885 machine_mode old_mode;
4887 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
4888 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
4889 of CONST_INTs, where we know the old_mode only from the call argument. */
4891 old_mode = GET_MODE (val);
4892 if (old_mode == VOIDmode)
4893 old_mode = TYPE_MODE (TREE_TYPE (exp));
4894 val = convert_modes (mode, old_mode, val, 1);
4895 return val;
4899 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
4900 EXP is the CALL_EXPR. CODE is the rtx code
4901 that corresponds to the arithmetic or logical operation from the name;
4902 an exception here is that NOT actually means NAND. TARGET is an optional
4903 place for us to store the results; AFTER is true if this is the
4904 fetch_and_xxx form. */
4906 static rtx
4907 expand_builtin_sync_operation (machine_mode mode, tree exp,
4908 enum rtx_code code, bool after,
4909 rtx target)
4911 rtx val, mem;
4912 location_t loc = EXPR_LOCATION (exp);
4914 if (code == NOT && warn_sync_nand)
4916 tree fndecl = get_callee_fndecl (exp);
4917 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4919 static bool warned_f_a_n, warned_n_a_f;
4921 switch (fcode)
4923 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
4924 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
4925 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
4926 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
4927 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
4928 if (warned_f_a_n)
4929 break;
4931 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
4932 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
4933 warned_f_a_n = true;
4934 break;
4936 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
4937 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
4938 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
4939 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
4940 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
4941 if (warned_n_a_f)
4942 break;
4944 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
4945 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
4946 warned_n_a_f = true;
4947 break;
4949 default:
4950 gcc_unreachable ();
4954 /* Expand the operands. */
4955 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
4956 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
4958 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
4959 after);
4962 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
4963 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
4964 true if this is the boolean form. TARGET is a place for us to store the
4965 results; this is NOT optional if IS_BOOL is true. */
4967 static rtx
4968 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
4969 bool is_bool, rtx target)
4971 rtx old_val, new_val, mem;
4972 rtx *pbool, *poval;
4974 /* Expand the operands. */
4975 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
4976 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
4977 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
4979 pbool = poval = NULL;
4980 if (target != const0_rtx)
4982 if (is_bool)
4983 pbool = &target;
4984 else
4985 poval = &target;
4987 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
4988 false, MEMMODEL_SYNC_SEQ_CST,
4989 MEMMODEL_SYNC_SEQ_CST))
4990 return NULL_RTX;
4992 return target;
4995 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
4996 general form is actually an atomic exchange, and some targets only
4997 support a reduced form with the second argument being a constant 1.
4998 EXP is the CALL_EXPR; TARGET is an optional place for us to store
4999 the results. */
5001 static rtx
5002 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5003 rtx target)
5005 rtx val, mem;
5007 /* Expand the operands. */
5008 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5009 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5011 return expand_sync_lock_test_and_set (target, mem, val);
5014 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5016 static void
5017 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5019 rtx mem;
5021 /* Expand the operands. */
5022 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5024 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5027 /* Given an integer representing an ``enum memmodel'', verify its
5028 correctness and return the memory model enum. */
5030 static enum memmodel
5031 get_memmodel (tree exp)
5033 rtx op;
5034 unsigned HOST_WIDE_INT val;
5036 /* If the parameter is not a constant, it's a run time value so we'll just
5037 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5038 if (TREE_CODE (exp) != INTEGER_CST)
5039 return MEMMODEL_SEQ_CST;
5041 op = expand_normal (exp);
5043 val = INTVAL (op);
5044 if (targetm.memmodel_check)
5045 val = targetm.memmodel_check (val);
5046 else if (val & ~MEMMODEL_MASK)
5048 warning (OPT_Winvalid_memory_model,
5049 "Unknown architecture specifier in memory model to builtin.");
5050 return MEMMODEL_SEQ_CST;
5053 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5054 if (memmodel_base (val) >= MEMMODEL_LAST)
5056 warning (OPT_Winvalid_memory_model,
5057 "invalid memory model argument to builtin");
5058 return MEMMODEL_SEQ_CST;
5061 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5062 be conservative and promote consume to acquire. */
5063 if (val == MEMMODEL_CONSUME)
5064 val = MEMMODEL_ACQUIRE;
5066 return (enum memmodel) val;
5069 /* Expand the __atomic_exchange intrinsic:
5070 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5071 EXP is the CALL_EXPR.
5072 TARGET is an optional place for us to store the results. */
5074 static rtx
5075 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5077 rtx val, mem;
5078 enum memmodel model;
5080 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5082 if (!flag_inline_atomics)
5083 return NULL_RTX;
5085 /* Expand the operands. */
5086 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5087 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5089 return expand_atomic_exchange (target, mem, val, model);
5092 /* Expand the __atomic_compare_exchange intrinsic:
5093 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5094 TYPE desired, BOOL weak,
5095 enum memmodel success,
5096 enum memmodel failure)
5097 EXP is the CALL_EXPR.
5098 TARGET is an optional place for us to store the results. */
5100 static rtx
5101 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5102 rtx target)
5104 rtx expect, desired, mem, oldval;
5105 rtx_code_label *label;
5106 enum memmodel success, failure;
5107 tree weak;
5108 bool is_weak;
5110 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5111 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5113 if (failure > success)
5115 warning (OPT_Winvalid_memory_model,
5116 "failure memory model cannot be stronger than success memory "
5117 "model for %<__atomic_compare_exchange%>");
5118 success = MEMMODEL_SEQ_CST;
5121 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5123 warning (OPT_Winvalid_memory_model,
5124 "invalid failure memory model for "
5125 "%<__atomic_compare_exchange%>");
5126 failure = MEMMODEL_SEQ_CST;
5127 success = MEMMODEL_SEQ_CST;
5131 if (!flag_inline_atomics)
5132 return NULL_RTX;
5134 /* Expand the operands. */
5135 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5137 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5138 expect = convert_memory_address (Pmode, expect);
5139 expect = gen_rtx_MEM (mode, expect);
5140 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5142 weak = CALL_EXPR_ARG (exp, 3);
5143 is_weak = false;
5144 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5145 is_weak = true;
5147 if (target == const0_rtx)
5148 target = NULL;
5150 /* Lest the rtl backend create a race condition with an imporoper store
5151 to memory, always create a new pseudo for OLDVAL. */
5152 oldval = NULL;
5154 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5155 is_weak, success, failure))
5156 return NULL_RTX;
5158 /* Conditionally store back to EXPECT, lest we create a race condition
5159 with an improper store to memory. */
5160 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5161 the normal case where EXPECT is totally private, i.e. a register. At
5162 which point the store can be unconditional. */
5163 label = gen_label_rtx ();
5164 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5165 GET_MODE (target), 1, label);
5166 emit_move_insn (expect, oldval);
5167 emit_label (label);
5169 return target;
5172 /* Expand the __atomic_load intrinsic:
5173 TYPE __atomic_load (TYPE *object, enum memmodel)
5174 EXP is the CALL_EXPR.
5175 TARGET is an optional place for us to store the results. */
5177 static rtx
5178 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5180 rtx mem;
5181 enum memmodel model;
5183 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5184 if (is_mm_release (model) || is_mm_acq_rel (model))
5186 warning (OPT_Winvalid_memory_model,
5187 "invalid memory model for %<__atomic_load%>");
5188 model = MEMMODEL_SEQ_CST;
5191 if (!flag_inline_atomics)
5192 return NULL_RTX;
5194 /* Expand the operand. */
5195 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5197 return expand_atomic_load (target, mem, model);
5201 /* Expand the __atomic_store intrinsic:
5202 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5203 EXP is the CALL_EXPR.
5204 TARGET is an optional place for us to store the results. */
5206 static rtx
5207 expand_builtin_atomic_store (machine_mode mode, tree exp)
5209 rtx mem, val;
5210 enum memmodel model;
5212 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5213 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5214 || is_mm_release (model)))
5216 warning (OPT_Winvalid_memory_model,
5217 "invalid memory model for %<__atomic_store%>");
5218 model = MEMMODEL_SEQ_CST;
5221 if (!flag_inline_atomics)
5222 return NULL_RTX;
5224 /* Expand the operands. */
5225 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5226 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5228 return expand_atomic_store (mem, val, model, false);
5231 /* Expand the __atomic_fetch_XXX intrinsic:
5232 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5233 EXP is the CALL_EXPR.
5234 TARGET is an optional place for us to store the results.
5235 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5236 FETCH_AFTER is true if returning the result of the operation.
5237 FETCH_AFTER is false if returning the value before the operation.
5238 IGNORE is true if the result is not used.
5239 EXT_CALL is the correct builtin for an external call if this cannot be
5240 resolved to an instruction sequence. */
5242 static rtx
5243 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5244 enum rtx_code code, bool fetch_after,
5245 bool ignore, enum built_in_function ext_call)
5247 rtx val, mem, ret;
5248 enum memmodel model;
5249 tree fndecl;
5250 tree addr;
5252 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5254 /* Expand the operands. */
5255 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5256 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5258 /* Only try generating instructions if inlining is turned on. */
5259 if (flag_inline_atomics)
5261 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5262 if (ret)
5263 return ret;
5266 /* Return if a different routine isn't needed for the library call. */
5267 if (ext_call == BUILT_IN_NONE)
5268 return NULL_RTX;
5270 /* Change the call to the specified function. */
5271 fndecl = get_callee_fndecl (exp);
5272 addr = CALL_EXPR_FN (exp);
5273 STRIP_NOPS (addr);
5275 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5276 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5278 /* Expand the call here so we can emit trailing code. */
5279 ret = expand_call (exp, target, ignore);
5281 /* Replace the original function just in case it matters. */
5282 TREE_OPERAND (addr, 0) = fndecl;
5284 /* Then issue the arithmetic correction to return the right result. */
5285 if (!ignore)
5287 if (code == NOT)
5289 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5290 OPTAB_LIB_WIDEN);
5291 ret = expand_simple_unop (mode, NOT, ret, target, true);
5293 else
5294 ret = expand_simple_binop (mode, code, ret, val, target, true,
5295 OPTAB_LIB_WIDEN);
5297 return ret;
5300 /* Expand an atomic clear operation.
5301 void _atomic_clear (BOOL *obj, enum memmodel)
5302 EXP is the call expression. */
5304 static rtx
5305 expand_builtin_atomic_clear (tree exp)
5307 machine_mode mode;
5308 rtx mem, ret;
5309 enum memmodel model;
5311 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5312 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5313 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5315 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
5317 warning (OPT_Winvalid_memory_model,
5318 "invalid memory model for %<__atomic_store%>");
5319 model = MEMMODEL_SEQ_CST;
5322 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5323 Failing that, a store is issued by __atomic_store. The only way this can
5324 fail is if the bool type is larger than a word size. Unlikely, but
5325 handle it anyway for completeness. Assume a single threaded model since
5326 there is no atomic support in this case, and no barriers are required. */
5327 ret = expand_atomic_store (mem, const0_rtx, model, true);
5328 if (!ret)
5329 emit_move_insn (mem, const0_rtx);
5330 return const0_rtx;
5333 /* Expand an atomic test_and_set operation.
5334 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5335 EXP is the call expression. */
5337 static rtx
5338 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5340 rtx mem;
5341 enum memmodel model;
5342 machine_mode mode;
5344 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5345 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5346 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5348 return expand_atomic_test_and_set (target, mem, model);
5352 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5353 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5355 static tree
5356 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5358 int size;
5359 machine_mode mode;
5360 unsigned int mode_align, type_align;
5362 if (TREE_CODE (arg0) != INTEGER_CST)
5363 return NULL_TREE;
5365 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5366 mode = mode_for_size (size, MODE_INT, 0);
5367 mode_align = GET_MODE_ALIGNMENT (mode);
5369 if (TREE_CODE (arg1) == INTEGER_CST)
5371 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
5373 /* Either this argument is null, or it's a fake pointer encoding
5374 the alignment of the object. */
5375 val = val & -val;
5376 val *= BITS_PER_UNIT;
5378 if (val == 0 || mode_align < val)
5379 type_align = mode_align;
5380 else
5381 type_align = val;
5383 else
5385 tree ttype = TREE_TYPE (arg1);
5387 /* This function is usually invoked and folded immediately by the front
5388 end before anything else has a chance to look at it. The pointer
5389 parameter at this point is usually cast to a void *, so check for that
5390 and look past the cast. */
5391 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5392 && VOID_TYPE_P (TREE_TYPE (ttype)))
5393 arg1 = TREE_OPERAND (arg1, 0);
5395 ttype = TREE_TYPE (arg1);
5396 gcc_assert (POINTER_TYPE_P (ttype));
5398 /* Get the underlying type of the object. */
5399 ttype = TREE_TYPE (ttype);
5400 type_align = TYPE_ALIGN (ttype);
5403 /* If the object has smaller alignment, the lock free routines cannot
5404 be used. */
5405 if (type_align < mode_align)
5406 return boolean_false_node;
5408 /* Check if a compare_and_swap pattern exists for the mode which represents
5409 the required size. The pattern is not allowed to fail, so the existence
5410 of the pattern indicates support is present. */
5411 if (can_compare_and_swap_p (mode, true))
5412 return boolean_true_node;
5413 else
5414 return boolean_false_node;
5417 /* Return true if the parameters to call EXP represent an object which will
5418 always generate lock free instructions. The first argument represents the
5419 size of the object, and the second parameter is a pointer to the object
5420 itself. If NULL is passed for the object, then the result is based on
5421 typical alignment for an object of the specified size. Otherwise return
5422 false. */
5424 static rtx
5425 expand_builtin_atomic_always_lock_free (tree exp)
5427 tree size;
5428 tree arg0 = CALL_EXPR_ARG (exp, 0);
5429 tree arg1 = CALL_EXPR_ARG (exp, 1);
5431 if (TREE_CODE (arg0) != INTEGER_CST)
5433 error ("non-constant argument 1 to __atomic_always_lock_free");
5434 return const0_rtx;
5437 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5438 if (size == boolean_true_node)
5439 return const1_rtx;
5440 return const0_rtx;
5443 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5444 is lock free on this architecture. */
5446 static tree
5447 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5449 if (!flag_inline_atomics)
5450 return NULL_TREE;
5452 /* If it isn't always lock free, don't generate a result. */
5453 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5454 return boolean_true_node;
5456 return NULL_TREE;
5459 /* Return true if the parameters to call EXP represent an object which will
5460 always generate lock free instructions. The first argument represents the
5461 size of the object, and the second parameter is a pointer to the object
5462 itself. If NULL is passed for the object, then the result is based on
5463 typical alignment for an object of the specified size. Otherwise return
5464 NULL*/
5466 static rtx
5467 expand_builtin_atomic_is_lock_free (tree exp)
5469 tree size;
5470 tree arg0 = CALL_EXPR_ARG (exp, 0);
5471 tree arg1 = CALL_EXPR_ARG (exp, 1);
5473 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5475 error ("non-integer argument 1 to __atomic_is_lock_free");
5476 return NULL_RTX;
5479 if (!flag_inline_atomics)
5480 return NULL_RTX;
5482 /* If the value is known at compile time, return the RTX for it. */
5483 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5484 if (size == boolean_true_node)
5485 return const1_rtx;
5487 return NULL_RTX;
5490 /* Expand the __atomic_thread_fence intrinsic:
5491 void __atomic_thread_fence (enum memmodel)
5492 EXP is the CALL_EXPR. */
5494 static void
5495 expand_builtin_atomic_thread_fence (tree exp)
5497 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5498 expand_mem_thread_fence (model);
5501 /* Expand the __atomic_signal_fence intrinsic:
5502 void __atomic_signal_fence (enum memmodel)
5503 EXP is the CALL_EXPR. */
5505 static void
5506 expand_builtin_atomic_signal_fence (tree exp)
5508 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5509 expand_mem_signal_fence (model);
5512 /* Expand the __sync_synchronize intrinsic. */
5514 static void
5515 expand_builtin_sync_synchronize (void)
5517 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
5520 static rtx
5521 expand_builtin_thread_pointer (tree exp, rtx target)
5523 enum insn_code icode;
5524 if (!validate_arglist (exp, VOID_TYPE))
5525 return const0_rtx;
5526 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5527 if (icode != CODE_FOR_nothing)
5529 struct expand_operand op;
5530 /* If the target is not sutitable then create a new target. */
5531 if (target == NULL_RTX
5532 || !REG_P (target)
5533 || GET_MODE (target) != Pmode)
5534 target = gen_reg_rtx (Pmode);
5535 create_output_operand (&op, target, Pmode);
5536 expand_insn (icode, 1, &op);
5537 return target;
5539 error ("__builtin_thread_pointer is not supported on this target");
5540 return const0_rtx;
5543 static void
5544 expand_builtin_set_thread_pointer (tree exp)
5546 enum insn_code icode;
5547 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5548 return;
5549 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5550 if (icode != CODE_FOR_nothing)
5552 struct expand_operand op;
5553 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5554 Pmode, EXPAND_NORMAL);
5555 create_input_operand (&op, val, Pmode);
5556 expand_insn (icode, 1, &op);
5557 return;
5559 error ("__builtin_set_thread_pointer is not supported on this target");
5563 /* Emit code to restore the current value of stack. */
5565 static void
5566 expand_stack_restore (tree var)
5568 rtx_insn *prev;
5569 rtx sa = expand_normal (var);
5571 sa = convert_memory_address (Pmode, sa);
5573 prev = get_last_insn ();
5574 emit_stack_restore (SAVE_BLOCK, sa);
5576 record_new_stack_level ();
5578 fixup_args_size_notes (prev, get_last_insn (), 0);
5581 /* Emit code to save the current value of stack. */
5583 static rtx
5584 expand_stack_save (void)
5586 rtx ret = NULL_RTX;
5588 emit_stack_save (SAVE_BLOCK, &ret);
5589 return ret;
5593 /* Expand an expression EXP that calls a built-in function,
5594 with result going to TARGET if that's convenient
5595 (and in mode MODE if that's convenient).
5596 SUBTARGET may be used as the target for computing one of EXP's operands.
5597 IGNORE is nonzero if the value is to be ignored. */
5600 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5601 int ignore)
5603 tree fndecl = get_callee_fndecl (exp);
5604 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5605 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5606 int flags;
5608 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5609 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5611 /* When ASan is enabled, we don't want to expand some memory/string
5612 builtins and rely on libsanitizer's hooks. This allows us to avoid
5613 redundant checks and be sure, that possible overflow will be detected
5614 by ASan. */
5616 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5617 return expand_call (exp, target, ignore);
5619 /* When not optimizing, generate calls to library functions for a certain
5620 set of builtins. */
5621 if (!optimize
5622 && !called_as_built_in (fndecl)
5623 && fcode != BUILT_IN_FORK
5624 && fcode != BUILT_IN_EXECL
5625 && fcode != BUILT_IN_EXECV
5626 && fcode != BUILT_IN_EXECLP
5627 && fcode != BUILT_IN_EXECLE
5628 && fcode != BUILT_IN_EXECVP
5629 && fcode != BUILT_IN_EXECVE
5630 && fcode != BUILT_IN_ALLOCA
5631 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5632 && fcode != BUILT_IN_FREE
5633 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5634 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5635 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5636 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5637 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5638 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5639 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5640 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5641 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5642 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5643 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
5644 && fcode != BUILT_IN_CHKP_BNDRET)
5645 return expand_call (exp, target, ignore);
5647 /* The built-in function expanders test for target == const0_rtx
5648 to determine whether the function's result will be ignored. */
5649 if (ignore)
5650 target = const0_rtx;
5652 /* If the result of a pure or const built-in function is ignored, and
5653 none of its arguments are volatile, we can avoid expanding the
5654 built-in call and just evaluate the arguments for side-effects. */
5655 if (target == const0_rtx
5656 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5657 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5659 bool volatilep = false;
5660 tree arg;
5661 call_expr_arg_iterator iter;
5663 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5664 if (TREE_THIS_VOLATILE (arg))
5666 volatilep = true;
5667 break;
5670 if (! volatilep)
5672 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5673 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5674 return const0_rtx;
5678 /* expand_builtin_with_bounds is supposed to be used for
5679 instrumented builtin calls. */
5680 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
5682 switch (fcode)
5684 CASE_FLT_FN (BUILT_IN_FABS):
5685 case BUILT_IN_FABSD32:
5686 case BUILT_IN_FABSD64:
5687 case BUILT_IN_FABSD128:
5688 target = expand_builtin_fabs (exp, target, subtarget);
5689 if (target)
5690 return target;
5691 break;
5693 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5694 target = expand_builtin_copysign (exp, target, subtarget);
5695 if (target)
5696 return target;
5697 break;
5699 /* Just do a normal library call if we were unable to fold
5700 the values. */
5701 CASE_FLT_FN (BUILT_IN_CABS):
5702 break;
5704 CASE_FLT_FN (BUILT_IN_FMA):
5705 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5706 if (target)
5707 return target;
5708 break;
5710 CASE_FLT_FN (BUILT_IN_ILOGB):
5711 if (! flag_unsafe_math_optimizations)
5712 break;
5713 CASE_FLT_FN (BUILT_IN_ISINF):
5714 CASE_FLT_FN (BUILT_IN_FINITE):
5715 case BUILT_IN_ISFINITE:
5716 case BUILT_IN_ISNORMAL:
5717 target = expand_builtin_interclass_mathfn (exp, target);
5718 if (target)
5719 return target;
5720 break;
5722 CASE_FLT_FN (BUILT_IN_ICEIL):
5723 CASE_FLT_FN (BUILT_IN_LCEIL):
5724 CASE_FLT_FN (BUILT_IN_LLCEIL):
5725 CASE_FLT_FN (BUILT_IN_LFLOOR):
5726 CASE_FLT_FN (BUILT_IN_IFLOOR):
5727 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5728 target = expand_builtin_int_roundingfn (exp, target);
5729 if (target)
5730 return target;
5731 break;
5733 CASE_FLT_FN (BUILT_IN_IRINT):
5734 CASE_FLT_FN (BUILT_IN_LRINT):
5735 CASE_FLT_FN (BUILT_IN_LLRINT):
5736 CASE_FLT_FN (BUILT_IN_IROUND):
5737 CASE_FLT_FN (BUILT_IN_LROUND):
5738 CASE_FLT_FN (BUILT_IN_LLROUND):
5739 target = expand_builtin_int_roundingfn_2 (exp, target);
5740 if (target)
5741 return target;
5742 break;
5744 CASE_FLT_FN (BUILT_IN_POWI):
5745 target = expand_builtin_powi (exp, target);
5746 if (target)
5747 return target;
5748 break;
5750 CASE_FLT_FN (BUILT_IN_CEXPI):
5751 target = expand_builtin_cexpi (exp, target);
5752 gcc_assert (target);
5753 return target;
5755 CASE_FLT_FN (BUILT_IN_SIN):
5756 CASE_FLT_FN (BUILT_IN_COS):
5757 if (! flag_unsafe_math_optimizations)
5758 break;
5759 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5760 if (target)
5761 return target;
5762 break;
5764 CASE_FLT_FN (BUILT_IN_SINCOS):
5765 if (! flag_unsafe_math_optimizations)
5766 break;
5767 target = expand_builtin_sincos (exp);
5768 if (target)
5769 return target;
5770 break;
5772 case BUILT_IN_APPLY_ARGS:
5773 return expand_builtin_apply_args ();
5775 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5776 FUNCTION with a copy of the parameters described by
5777 ARGUMENTS, and ARGSIZE. It returns a block of memory
5778 allocated on the stack into which is stored all the registers
5779 that might possibly be used for returning the result of a
5780 function. ARGUMENTS is the value returned by
5781 __builtin_apply_args. ARGSIZE is the number of bytes of
5782 arguments that must be copied. ??? How should this value be
5783 computed? We'll also need a safe worst case value for varargs
5784 functions. */
5785 case BUILT_IN_APPLY:
5786 if (!validate_arglist (exp, POINTER_TYPE,
5787 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5788 && !validate_arglist (exp, REFERENCE_TYPE,
5789 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5790 return const0_rtx;
5791 else
5793 rtx ops[3];
5795 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5796 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5797 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5799 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5802 /* __builtin_return (RESULT) causes the function to return the
5803 value described by RESULT. RESULT is address of the block of
5804 memory returned by __builtin_apply. */
5805 case BUILT_IN_RETURN:
5806 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5807 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5808 return const0_rtx;
5810 case BUILT_IN_SAVEREGS:
5811 return expand_builtin_saveregs ();
5813 case BUILT_IN_VA_ARG_PACK:
5814 /* All valid uses of __builtin_va_arg_pack () are removed during
5815 inlining. */
5816 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5817 return const0_rtx;
5819 case BUILT_IN_VA_ARG_PACK_LEN:
5820 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5821 inlining. */
5822 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5823 return const0_rtx;
5825 /* Return the address of the first anonymous stack arg. */
5826 case BUILT_IN_NEXT_ARG:
5827 if (fold_builtin_next_arg (exp, false))
5828 return const0_rtx;
5829 return expand_builtin_next_arg ();
5831 case BUILT_IN_CLEAR_CACHE:
5832 target = expand_builtin___clear_cache (exp);
5833 if (target)
5834 return target;
5835 break;
5837 case BUILT_IN_CLASSIFY_TYPE:
5838 return expand_builtin_classify_type (exp);
5840 case BUILT_IN_CONSTANT_P:
5841 return const0_rtx;
5843 case BUILT_IN_FRAME_ADDRESS:
5844 case BUILT_IN_RETURN_ADDRESS:
5845 return expand_builtin_frame_address (fndecl, exp);
5847 /* Returns the address of the area where the structure is returned.
5848 0 otherwise. */
5849 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5850 if (call_expr_nargs (exp) != 0
5851 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5852 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5853 return const0_rtx;
5854 else
5855 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5857 case BUILT_IN_ALLOCA:
5858 case BUILT_IN_ALLOCA_WITH_ALIGN:
5859 /* If the allocation stems from the declaration of a variable-sized
5860 object, it cannot accumulate. */
5861 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
5862 if (target)
5863 return target;
5864 break;
5866 case BUILT_IN_STACK_SAVE:
5867 return expand_stack_save ();
5869 case BUILT_IN_STACK_RESTORE:
5870 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
5871 return const0_rtx;
5873 case BUILT_IN_BSWAP16:
5874 case BUILT_IN_BSWAP32:
5875 case BUILT_IN_BSWAP64:
5876 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
5877 if (target)
5878 return target;
5879 break;
5881 CASE_INT_FN (BUILT_IN_FFS):
5882 target = expand_builtin_unop (target_mode, exp, target,
5883 subtarget, ffs_optab);
5884 if (target)
5885 return target;
5886 break;
5888 CASE_INT_FN (BUILT_IN_CLZ):
5889 target = expand_builtin_unop (target_mode, exp, target,
5890 subtarget, clz_optab);
5891 if (target)
5892 return target;
5893 break;
5895 CASE_INT_FN (BUILT_IN_CTZ):
5896 target = expand_builtin_unop (target_mode, exp, target,
5897 subtarget, ctz_optab);
5898 if (target)
5899 return target;
5900 break;
5902 CASE_INT_FN (BUILT_IN_CLRSB):
5903 target = expand_builtin_unop (target_mode, exp, target,
5904 subtarget, clrsb_optab);
5905 if (target)
5906 return target;
5907 break;
5909 CASE_INT_FN (BUILT_IN_POPCOUNT):
5910 target = expand_builtin_unop (target_mode, exp, target,
5911 subtarget, popcount_optab);
5912 if (target)
5913 return target;
5914 break;
5916 CASE_INT_FN (BUILT_IN_PARITY):
5917 target = expand_builtin_unop (target_mode, exp, target,
5918 subtarget, parity_optab);
5919 if (target)
5920 return target;
5921 break;
5923 case BUILT_IN_STRLEN:
5924 target = expand_builtin_strlen (exp, target, target_mode);
5925 if (target)
5926 return target;
5927 break;
5929 case BUILT_IN_STRCPY:
5930 target = expand_builtin_strcpy (exp, target);
5931 if (target)
5932 return target;
5933 break;
5935 case BUILT_IN_STRNCPY:
5936 target = expand_builtin_strncpy (exp, target);
5937 if (target)
5938 return target;
5939 break;
5941 case BUILT_IN_STPCPY:
5942 target = expand_builtin_stpcpy (exp, target, mode);
5943 if (target)
5944 return target;
5945 break;
5947 case BUILT_IN_MEMCPY:
5948 target = expand_builtin_memcpy (exp, target);
5949 if (target)
5950 return target;
5951 break;
5953 case BUILT_IN_MEMPCPY:
5954 target = expand_builtin_mempcpy (exp, target, mode);
5955 if (target)
5956 return target;
5957 break;
5959 case BUILT_IN_MEMSET:
5960 target = expand_builtin_memset (exp, target, mode);
5961 if (target)
5962 return target;
5963 break;
5965 case BUILT_IN_BZERO:
5966 target = expand_builtin_bzero (exp);
5967 if (target)
5968 return target;
5969 break;
5971 case BUILT_IN_STRCMP:
5972 target = expand_builtin_strcmp (exp, target);
5973 if (target)
5974 return target;
5975 break;
5977 case BUILT_IN_STRNCMP:
5978 target = expand_builtin_strncmp (exp, target, mode);
5979 if (target)
5980 return target;
5981 break;
5983 case BUILT_IN_BCMP:
5984 case BUILT_IN_MEMCMP:
5985 target = expand_builtin_memcmp (exp, target);
5986 if (target)
5987 return target;
5988 break;
5990 case BUILT_IN_SETJMP:
5991 /* This should have been lowered to the builtins below. */
5992 gcc_unreachable ();
5994 case BUILT_IN_SETJMP_SETUP:
5995 /* __builtin_setjmp_setup is passed a pointer to an array of five words
5996 and the receiver label. */
5997 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5999 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6000 VOIDmode, EXPAND_NORMAL);
6001 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6002 rtx_insn *label_r = label_rtx (label);
6004 /* This is copied from the handling of non-local gotos. */
6005 expand_builtin_setjmp_setup (buf_addr, label_r);
6006 nonlocal_goto_handler_labels
6007 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6008 nonlocal_goto_handler_labels);
6009 /* ??? Do not let expand_label treat us as such since we would
6010 not want to be both on the list of non-local labels and on
6011 the list of forced labels. */
6012 FORCED_LABEL (label) = 0;
6013 return const0_rtx;
6015 break;
6017 case BUILT_IN_SETJMP_RECEIVER:
6018 /* __builtin_setjmp_receiver is passed the receiver label. */
6019 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6021 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6022 rtx_insn *label_r = label_rtx (label);
6024 expand_builtin_setjmp_receiver (label_r);
6025 return const0_rtx;
6027 break;
6029 /* __builtin_longjmp is passed a pointer to an array of five words.
6030 It's similar to the C library longjmp function but works with
6031 __builtin_setjmp above. */
6032 case BUILT_IN_LONGJMP:
6033 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6035 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6036 VOIDmode, EXPAND_NORMAL);
6037 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6039 if (value != const1_rtx)
6041 error ("%<__builtin_longjmp%> second argument must be 1");
6042 return const0_rtx;
6045 expand_builtin_longjmp (buf_addr, value);
6046 return const0_rtx;
6048 break;
6050 case BUILT_IN_NONLOCAL_GOTO:
6051 target = expand_builtin_nonlocal_goto (exp);
6052 if (target)
6053 return target;
6054 break;
6056 /* This updates the setjmp buffer that is its argument with the value
6057 of the current stack pointer. */
6058 case BUILT_IN_UPDATE_SETJMP_BUF:
6059 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6061 rtx buf_addr
6062 = expand_normal (CALL_EXPR_ARG (exp, 0));
6064 expand_builtin_update_setjmp_buf (buf_addr);
6065 return const0_rtx;
6067 break;
6069 case BUILT_IN_TRAP:
6070 expand_builtin_trap ();
6071 return const0_rtx;
6073 case BUILT_IN_UNREACHABLE:
6074 expand_builtin_unreachable ();
6075 return const0_rtx;
6077 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6078 case BUILT_IN_SIGNBITD32:
6079 case BUILT_IN_SIGNBITD64:
6080 case BUILT_IN_SIGNBITD128:
6081 target = expand_builtin_signbit (exp, target);
6082 if (target)
6083 return target;
6084 break;
6086 /* Various hooks for the DWARF 2 __throw routine. */
6087 case BUILT_IN_UNWIND_INIT:
6088 expand_builtin_unwind_init ();
6089 return const0_rtx;
6090 case BUILT_IN_DWARF_CFA:
6091 return virtual_cfa_rtx;
6092 #ifdef DWARF2_UNWIND_INFO
6093 case BUILT_IN_DWARF_SP_COLUMN:
6094 return expand_builtin_dwarf_sp_column ();
6095 case BUILT_IN_INIT_DWARF_REG_SIZES:
6096 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6097 return const0_rtx;
6098 #endif
6099 case BUILT_IN_FROB_RETURN_ADDR:
6100 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6101 case BUILT_IN_EXTRACT_RETURN_ADDR:
6102 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6103 case BUILT_IN_EH_RETURN:
6104 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6105 CALL_EXPR_ARG (exp, 1));
6106 return const0_rtx;
6107 case BUILT_IN_EH_RETURN_DATA_REGNO:
6108 return expand_builtin_eh_return_data_regno (exp);
6109 case BUILT_IN_EXTEND_POINTER:
6110 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6111 case BUILT_IN_EH_POINTER:
6112 return expand_builtin_eh_pointer (exp);
6113 case BUILT_IN_EH_FILTER:
6114 return expand_builtin_eh_filter (exp);
6115 case BUILT_IN_EH_COPY_VALUES:
6116 return expand_builtin_eh_copy_values (exp);
6118 case BUILT_IN_VA_START:
6119 return expand_builtin_va_start (exp);
6120 case BUILT_IN_VA_END:
6121 return expand_builtin_va_end (exp);
6122 case BUILT_IN_VA_COPY:
6123 return expand_builtin_va_copy (exp);
6124 case BUILT_IN_EXPECT:
6125 return expand_builtin_expect (exp, target);
6126 case BUILT_IN_ASSUME_ALIGNED:
6127 return expand_builtin_assume_aligned (exp, target);
6128 case BUILT_IN_PREFETCH:
6129 expand_builtin_prefetch (exp);
6130 return const0_rtx;
6132 case BUILT_IN_INIT_TRAMPOLINE:
6133 return expand_builtin_init_trampoline (exp, true);
6134 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6135 return expand_builtin_init_trampoline (exp, false);
6136 case BUILT_IN_ADJUST_TRAMPOLINE:
6137 return expand_builtin_adjust_trampoline (exp);
6139 case BUILT_IN_FORK:
6140 case BUILT_IN_EXECL:
6141 case BUILT_IN_EXECV:
6142 case BUILT_IN_EXECLP:
6143 case BUILT_IN_EXECLE:
6144 case BUILT_IN_EXECVP:
6145 case BUILT_IN_EXECVE:
6146 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6147 if (target)
6148 return target;
6149 break;
6151 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6152 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6153 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6154 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6155 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6156 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6157 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6158 if (target)
6159 return target;
6160 break;
6162 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6163 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6164 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6165 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6166 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6167 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6168 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6169 if (target)
6170 return target;
6171 break;
6173 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6174 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6175 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6176 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6177 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6178 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6179 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6180 if (target)
6181 return target;
6182 break;
6184 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6185 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6186 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6187 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6188 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6189 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6190 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6191 if (target)
6192 return target;
6193 break;
6195 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6196 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6197 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6198 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6199 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6200 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6201 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6202 if (target)
6203 return target;
6204 break;
6206 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6207 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6208 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6209 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6210 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6211 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6212 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6213 if (target)
6214 return target;
6215 break;
6217 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6218 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6219 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6220 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6221 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6222 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6223 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6224 if (target)
6225 return target;
6226 break;
6228 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6229 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6230 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6231 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6232 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6233 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6234 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6235 if (target)
6236 return target;
6237 break;
6239 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6240 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6241 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6242 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6243 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6244 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6245 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6246 if (target)
6247 return target;
6248 break;
6250 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6251 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6252 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6253 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6254 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6255 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6256 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6257 if (target)
6258 return target;
6259 break;
6261 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6262 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6263 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6264 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6265 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6266 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6267 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6268 if (target)
6269 return target;
6270 break;
6272 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6273 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6274 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6275 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6276 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6277 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6278 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6279 if (target)
6280 return target;
6281 break;
6283 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6284 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6285 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6286 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6287 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6288 if (mode == VOIDmode)
6289 mode = TYPE_MODE (boolean_type_node);
6290 if (!target || !register_operand (target, mode))
6291 target = gen_reg_rtx (mode);
6293 mode = get_builtin_sync_mode
6294 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6295 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6296 if (target)
6297 return target;
6298 break;
6300 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6301 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6302 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6303 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6304 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6305 mode = get_builtin_sync_mode
6306 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6307 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6308 if (target)
6309 return target;
6310 break;
6312 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6313 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6314 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6315 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6316 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6317 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6318 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6319 if (target)
6320 return target;
6321 break;
6323 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6324 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6325 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6326 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6327 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6328 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6329 expand_builtin_sync_lock_release (mode, exp);
6330 return const0_rtx;
6332 case BUILT_IN_SYNC_SYNCHRONIZE:
6333 expand_builtin_sync_synchronize ();
6334 return const0_rtx;
6336 case BUILT_IN_ATOMIC_EXCHANGE_1:
6337 case BUILT_IN_ATOMIC_EXCHANGE_2:
6338 case BUILT_IN_ATOMIC_EXCHANGE_4:
6339 case BUILT_IN_ATOMIC_EXCHANGE_8:
6340 case BUILT_IN_ATOMIC_EXCHANGE_16:
6341 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6342 target = expand_builtin_atomic_exchange (mode, exp, target);
6343 if (target)
6344 return target;
6345 break;
6347 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6348 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6349 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6350 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6351 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6353 unsigned int nargs, z;
6354 vec<tree, va_gc> *vec;
6356 mode =
6357 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6358 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6359 if (target)
6360 return target;
6362 /* If this is turned into an external library call, the weak parameter
6363 must be dropped to match the expected parameter list. */
6364 nargs = call_expr_nargs (exp);
6365 vec_alloc (vec, nargs - 1);
6366 for (z = 0; z < 3; z++)
6367 vec->quick_push (CALL_EXPR_ARG (exp, z));
6368 /* Skip the boolean weak parameter. */
6369 for (z = 4; z < 6; z++)
6370 vec->quick_push (CALL_EXPR_ARG (exp, z));
6371 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6372 break;
6375 case BUILT_IN_ATOMIC_LOAD_1:
6376 case BUILT_IN_ATOMIC_LOAD_2:
6377 case BUILT_IN_ATOMIC_LOAD_4:
6378 case BUILT_IN_ATOMIC_LOAD_8:
6379 case BUILT_IN_ATOMIC_LOAD_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6381 target = expand_builtin_atomic_load (mode, exp, target);
6382 if (target)
6383 return target;
6384 break;
6386 case BUILT_IN_ATOMIC_STORE_1:
6387 case BUILT_IN_ATOMIC_STORE_2:
6388 case BUILT_IN_ATOMIC_STORE_4:
6389 case BUILT_IN_ATOMIC_STORE_8:
6390 case BUILT_IN_ATOMIC_STORE_16:
6391 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6392 target = expand_builtin_atomic_store (mode, exp);
6393 if (target)
6394 return const0_rtx;
6395 break;
6397 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6398 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6399 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6400 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6401 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6403 enum built_in_function lib;
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6405 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6406 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6407 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6408 ignore, lib);
6409 if (target)
6410 return target;
6411 break;
6413 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6414 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6415 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6416 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6417 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6419 enum built_in_function lib;
6420 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6421 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6422 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6423 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6424 ignore, lib);
6425 if (target)
6426 return target;
6427 break;
6429 case BUILT_IN_ATOMIC_AND_FETCH_1:
6430 case BUILT_IN_ATOMIC_AND_FETCH_2:
6431 case BUILT_IN_ATOMIC_AND_FETCH_4:
6432 case BUILT_IN_ATOMIC_AND_FETCH_8:
6433 case BUILT_IN_ATOMIC_AND_FETCH_16:
6435 enum built_in_function lib;
6436 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6437 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6438 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6439 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6440 ignore, lib);
6441 if (target)
6442 return target;
6443 break;
6445 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6446 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6447 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6448 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6449 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6451 enum built_in_function lib;
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6453 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6454 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6455 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6456 ignore, lib);
6457 if (target)
6458 return target;
6459 break;
6461 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6462 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6463 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6464 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6465 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6467 enum built_in_function lib;
6468 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6469 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6470 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6471 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6472 ignore, lib);
6473 if (target)
6474 return target;
6475 break;
6477 case BUILT_IN_ATOMIC_OR_FETCH_1:
6478 case BUILT_IN_ATOMIC_OR_FETCH_2:
6479 case BUILT_IN_ATOMIC_OR_FETCH_4:
6480 case BUILT_IN_ATOMIC_OR_FETCH_8:
6481 case BUILT_IN_ATOMIC_OR_FETCH_16:
6483 enum built_in_function lib;
6484 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6485 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6486 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6487 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6488 ignore, lib);
6489 if (target)
6490 return target;
6491 break;
6493 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6494 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6495 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6496 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6497 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6498 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6499 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6500 ignore, BUILT_IN_NONE);
6501 if (target)
6502 return target;
6503 break;
6505 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6506 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6507 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6508 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6509 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6510 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6511 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6512 ignore, BUILT_IN_NONE);
6513 if (target)
6514 return target;
6515 break;
6517 case BUILT_IN_ATOMIC_FETCH_AND_1:
6518 case BUILT_IN_ATOMIC_FETCH_AND_2:
6519 case BUILT_IN_ATOMIC_FETCH_AND_4:
6520 case BUILT_IN_ATOMIC_FETCH_AND_8:
6521 case BUILT_IN_ATOMIC_FETCH_AND_16:
6522 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6523 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6524 ignore, BUILT_IN_NONE);
6525 if (target)
6526 return target;
6527 break;
6529 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6530 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6531 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6532 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6533 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6534 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6535 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6536 ignore, BUILT_IN_NONE);
6537 if (target)
6538 return target;
6539 break;
6541 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6542 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6543 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6544 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6545 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6546 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6547 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6548 ignore, BUILT_IN_NONE);
6549 if (target)
6550 return target;
6551 break;
6553 case BUILT_IN_ATOMIC_FETCH_OR_1:
6554 case BUILT_IN_ATOMIC_FETCH_OR_2:
6555 case BUILT_IN_ATOMIC_FETCH_OR_4:
6556 case BUILT_IN_ATOMIC_FETCH_OR_8:
6557 case BUILT_IN_ATOMIC_FETCH_OR_16:
6558 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6559 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6560 ignore, BUILT_IN_NONE);
6561 if (target)
6562 return target;
6563 break;
6565 case BUILT_IN_ATOMIC_TEST_AND_SET:
6566 return expand_builtin_atomic_test_and_set (exp, target);
6568 case BUILT_IN_ATOMIC_CLEAR:
6569 return expand_builtin_atomic_clear (exp);
6571 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6572 return expand_builtin_atomic_always_lock_free (exp);
6574 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6575 target = expand_builtin_atomic_is_lock_free (exp);
6576 if (target)
6577 return target;
6578 break;
6580 case BUILT_IN_ATOMIC_THREAD_FENCE:
6581 expand_builtin_atomic_thread_fence (exp);
6582 return const0_rtx;
6584 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6585 expand_builtin_atomic_signal_fence (exp);
6586 return const0_rtx;
6588 case BUILT_IN_OBJECT_SIZE:
6589 return expand_builtin_object_size (exp);
6591 case BUILT_IN_MEMCPY_CHK:
6592 case BUILT_IN_MEMPCPY_CHK:
6593 case BUILT_IN_MEMMOVE_CHK:
6594 case BUILT_IN_MEMSET_CHK:
6595 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6596 if (target)
6597 return target;
6598 break;
6600 case BUILT_IN_STRCPY_CHK:
6601 case BUILT_IN_STPCPY_CHK:
6602 case BUILT_IN_STRNCPY_CHK:
6603 case BUILT_IN_STPNCPY_CHK:
6604 case BUILT_IN_STRCAT_CHK:
6605 case BUILT_IN_STRNCAT_CHK:
6606 case BUILT_IN_SNPRINTF_CHK:
6607 case BUILT_IN_VSNPRINTF_CHK:
6608 maybe_emit_chk_warning (exp, fcode);
6609 break;
6611 case BUILT_IN_SPRINTF_CHK:
6612 case BUILT_IN_VSPRINTF_CHK:
6613 maybe_emit_sprintf_chk_warning (exp, fcode);
6614 break;
6616 case BUILT_IN_FREE:
6617 if (warn_free_nonheap_object)
6618 maybe_emit_free_warning (exp);
6619 break;
6621 case BUILT_IN_THREAD_POINTER:
6622 return expand_builtin_thread_pointer (exp, target);
6624 case BUILT_IN_SET_THREAD_POINTER:
6625 expand_builtin_set_thread_pointer (exp);
6626 return const0_rtx;
6628 case BUILT_IN_CILK_DETACH:
6629 expand_builtin_cilk_detach (exp);
6630 return const0_rtx;
6632 case BUILT_IN_CILK_POP_FRAME:
6633 expand_builtin_cilk_pop_frame (exp);
6634 return const0_rtx;
6636 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
6637 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
6638 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
6639 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
6640 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
6641 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
6642 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
6643 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
6644 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
6645 case BUILT_IN_CHKP_GET_PTR_LBOUND:
6646 case BUILT_IN_CHKP_GET_PTR_UBOUND:
6647 /* We allow user CHKP builtins if Pointer Bounds
6648 Checker is off. */
6649 if (!chkp_function_instrumented_p (current_function_decl))
6651 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
6652 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6653 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
6654 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
6655 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
6656 return expand_normal (CALL_EXPR_ARG (exp, 0));
6657 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
6658 return expand_normal (size_zero_node);
6659 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
6660 return expand_normal (size_int (-1));
6661 else
6662 return const0_rtx;
6664 /* FALLTHROUGH */
6666 case BUILT_IN_CHKP_BNDMK:
6667 case BUILT_IN_CHKP_BNDSTX:
6668 case BUILT_IN_CHKP_BNDCL:
6669 case BUILT_IN_CHKP_BNDCU:
6670 case BUILT_IN_CHKP_BNDLDX:
6671 case BUILT_IN_CHKP_BNDRET:
6672 case BUILT_IN_CHKP_INTERSECT:
6673 case BUILT_IN_CHKP_NARROW:
6674 case BUILT_IN_CHKP_EXTRACT_LOWER:
6675 case BUILT_IN_CHKP_EXTRACT_UPPER:
6676 /* Software implementation of Pointer Bounds Checker is NYI.
6677 Target support is required. */
6678 error ("Your target platform does not support -fcheck-pointer-bounds");
6679 break;
6681 case BUILT_IN_ACC_ON_DEVICE:
6682 /* Do library call, if we failed to expand the builtin when
6683 folding. */
6684 break;
6686 default: /* just do library call, if unknown builtin */
6687 break;
6690 /* The switch statement above can drop through to cause the function
6691 to be called normally. */
6692 return expand_call (exp, target, ignore);
6695 /* Similar to expand_builtin but is used for instrumented calls. */
6698 expand_builtin_with_bounds (tree exp, rtx target,
6699 rtx subtarget ATTRIBUTE_UNUSED,
6700 machine_mode mode, int ignore)
6702 tree fndecl = get_callee_fndecl (exp);
6703 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6705 gcc_assert (CALL_WITH_BOUNDS_P (exp));
6707 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6708 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6710 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
6711 && fcode < END_CHKP_BUILTINS);
6713 switch (fcode)
6715 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
6716 target = expand_builtin_memcpy_with_bounds (exp, target);
6717 if (target)
6718 return target;
6719 break;
6721 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
6722 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
6723 if (target)
6724 return target;
6725 break;
6727 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
6728 target = expand_builtin_memset_with_bounds (exp, target, mode);
6729 if (target)
6730 return target;
6731 break;
6733 default:
6734 break;
6737 /* The switch statement above can drop through to cause the function
6738 to be called normally. */
6739 return expand_call (exp, target, ignore);
6742 /* Determine whether a tree node represents a call to a built-in
6743 function. If the tree T is a call to a built-in function with
6744 the right number of arguments of the appropriate types, return
6745 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6746 Otherwise the return value is END_BUILTINS. */
6748 enum built_in_function
6749 builtin_mathfn_code (const_tree t)
6751 const_tree fndecl, arg, parmlist;
6752 const_tree argtype, parmtype;
6753 const_call_expr_arg_iterator iter;
6755 if (TREE_CODE (t) != CALL_EXPR
6756 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6757 return END_BUILTINS;
6759 fndecl = get_callee_fndecl (t);
6760 if (fndecl == NULL_TREE
6761 || TREE_CODE (fndecl) != FUNCTION_DECL
6762 || ! DECL_BUILT_IN (fndecl)
6763 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6764 return END_BUILTINS;
6766 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6767 init_const_call_expr_arg_iterator (t, &iter);
6768 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6770 /* If a function doesn't take a variable number of arguments,
6771 the last element in the list will have type `void'. */
6772 parmtype = TREE_VALUE (parmlist);
6773 if (VOID_TYPE_P (parmtype))
6775 if (more_const_call_expr_args_p (&iter))
6776 return END_BUILTINS;
6777 return DECL_FUNCTION_CODE (fndecl);
6780 if (! more_const_call_expr_args_p (&iter))
6781 return END_BUILTINS;
6783 arg = next_const_call_expr_arg (&iter);
6784 argtype = TREE_TYPE (arg);
6786 if (SCALAR_FLOAT_TYPE_P (parmtype))
6788 if (! SCALAR_FLOAT_TYPE_P (argtype))
6789 return END_BUILTINS;
6791 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6793 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6794 return END_BUILTINS;
6796 else if (POINTER_TYPE_P (parmtype))
6798 if (! POINTER_TYPE_P (argtype))
6799 return END_BUILTINS;
6801 else if (INTEGRAL_TYPE_P (parmtype))
6803 if (! INTEGRAL_TYPE_P (argtype))
6804 return END_BUILTINS;
6806 else
6807 return END_BUILTINS;
6810 /* Variable-length argument list. */
6811 return DECL_FUNCTION_CODE (fndecl);
6814 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6815 evaluate to a constant. */
6817 static tree
6818 fold_builtin_constant_p (tree arg)
6820 /* We return 1 for a numeric type that's known to be a constant
6821 value at compile-time or for an aggregate type that's a
6822 literal constant. */
6823 STRIP_NOPS (arg);
6825 /* If we know this is a constant, emit the constant of one. */
6826 if (CONSTANT_CLASS_P (arg)
6827 || (TREE_CODE (arg) == CONSTRUCTOR
6828 && TREE_CONSTANT (arg)))
6829 return integer_one_node;
6830 if (TREE_CODE (arg) == ADDR_EXPR)
6832 tree op = TREE_OPERAND (arg, 0);
6833 if (TREE_CODE (op) == STRING_CST
6834 || (TREE_CODE (op) == ARRAY_REF
6835 && integer_zerop (TREE_OPERAND (op, 1))
6836 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6837 return integer_one_node;
6840 /* If this expression has side effects, show we don't know it to be a
6841 constant. Likewise if it's a pointer or aggregate type since in
6842 those case we only want literals, since those are only optimized
6843 when generating RTL, not later.
6844 And finally, if we are compiling an initializer, not code, we
6845 need to return a definite result now; there's not going to be any
6846 more optimization done. */
6847 if (TREE_SIDE_EFFECTS (arg)
6848 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6849 || POINTER_TYPE_P (TREE_TYPE (arg))
6850 || cfun == 0
6851 || folding_initializer
6852 || force_folding_builtin_constant_p)
6853 return integer_zero_node;
6855 return NULL_TREE;
6858 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6859 return it as a truthvalue. */
6861 static tree
6862 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
6863 tree predictor)
6865 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6867 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6868 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6869 ret_type = TREE_TYPE (TREE_TYPE (fn));
6870 pred_type = TREE_VALUE (arg_types);
6871 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6873 pred = fold_convert_loc (loc, pred_type, pred);
6874 expected = fold_convert_loc (loc, expected_type, expected);
6875 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
6876 predictor);
6878 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6879 build_int_cst (ret_type, 0));
6882 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6883 NULL_TREE if no simplification is possible. */
6885 tree
6886 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
6888 tree inner, fndecl, inner_arg0;
6889 enum tree_code code;
6891 /* Distribute the expected value over short-circuiting operators.
6892 See through the cast from truthvalue_type_node to long. */
6893 inner_arg0 = arg0;
6894 while (CONVERT_EXPR_P (inner_arg0)
6895 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6896 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6897 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6899 /* If this is a builtin_expect within a builtin_expect keep the
6900 inner one. See through a comparison against a constant. It
6901 might have been added to create a thruthvalue. */
6902 inner = inner_arg0;
6904 if (COMPARISON_CLASS_P (inner)
6905 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6906 inner = TREE_OPERAND (inner, 0);
6908 if (TREE_CODE (inner) == CALL_EXPR
6909 && (fndecl = get_callee_fndecl (inner))
6910 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6911 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6912 return arg0;
6914 inner = inner_arg0;
6915 code = TREE_CODE (inner);
6916 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6918 tree op0 = TREE_OPERAND (inner, 0);
6919 tree op1 = TREE_OPERAND (inner, 1);
6921 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
6922 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
6923 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6925 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6928 /* If the argument isn't invariant then there's nothing else we can do. */
6929 if (!TREE_CONSTANT (inner_arg0))
6930 return NULL_TREE;
6932 /* If we expect that a comparison against the argument will fold to
6933 a constant return the constant. In practice, this means a true
6934 constant or the address of a non-weak symbol. */
6935 inner = inner_arg0;
6936 STRIP_NOPS (inner);
6937 if (TREE_CODE (inner) == ADDR_EXPR)
6941 inner = TREE_OPERAND (inner, 0);
6943 while (TREE_CODE (inner) == COMPONENT_REF
6944 || TREE_CODE (inner) == ARRAY_REF);
6945 if ((TREE_CODE (inner) == VAR_DECL
6946 || TREE_CODE (inner) == FUNCTION_DECL)
6947 && DECL_WEAK (inner))
6948 return NULL_TREE;
6951 /* Otherwise, ARG0 already has the proper type for the return value. */
6952 return arg0;
6955 /* Fold a call to __builtin_classify_type with argument ARG. */
6957 static tree
6958 fold_builtin_classify_type (tree arg)
6960 if (arg == 0)
6961 return build_int_cst (integer_type_node, no_type_class);
6963 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
6966 /* Fold a call to __builtin_strlen with argument ARG. */
6968 static tree
6969 fold_builtin_strlen (location_t loc, tree type, tree arg)
6971 if (!validate_arg (arg, POINTER_TYPE))
6972 return NULL_TREE;
6973 else
6975 tree len = c_strlen (arg, 0);
6977 if (len)
6978 return fold_convert_loc (loc, type, len);
6980 return NULL_TREE;
6984 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6986 static tree
6987 fold_builtin_inf (location_t loc, tree type, int warn)
6989 REAL_VALUE_TYPE real;
6991 /* __builtin_inff is intended to be usable to define INFINITY on all
6992 targets. If an infinity is not available, INFINITY expands "to a
6993 positive constant of type float that overflows at translation
6994 time", footnote "In this case, using INFINITY will violate the
6995 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6996 Thus we pedwarn to ensure this constraint violation is
6997 diagnosed. */
6998 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6999 pedwarn (loc, 0, "target format does not support infinity");
7001 real_inf (&real);
7002 return build_real (type, real);
7005 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7006 NULL_TREE if no simplification can be made. */
7008 static tree
7009 fold_builtin_sincos (location_t loc,
7010 tree arg0, tree arg1, tree arg2)
7012 tree type;
7013 tree fndecl, call = NULL_TREE;
7015 if (!validate_arg (arg0, REAL_TYPE)
7016 || !validate_arg (arg1, POINTER_TYPE)
7017 || !validate_arg (arg2, POINTER_TYPE))
7018 return NULL_TREE;
7020 type = TREE_TYPE (arg0);
7022 /* Calculate the result when the argument is a constant. */
7023 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
7024 if (fn == END_BUILTINS)
7025 return NULL_TREE;
7027 /* Canonicalize sincos to cexpi. */
7028 if (TREE_CODE (arg0) == REAL_CST)
7030 tree complex_type = build_complex_type (type);
7031 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
7033 if (!call)
7035 if (!targetm.libc_has_function (function_c99_math_complex)
7036 || !builtin_decl_implicit_p (fn))
7037 return NULL_TREE;
7038 fndecl = builtin_decl_explicit (fn);
7039 call = build_call_expr_loc (loc, fndecl, 1, arg0);
7040 call = builtin_save_expr (call);
7043 return build2 (COMPOUND_EXPR, void_type_node,
7044 build2 (MODIFY_EXPR, void_type_node,
7045 build_fold_indirect_ref_loc (loc, arg1),
7046 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
7047 build2 (MODIFY_EXPR, void_type_node,
7048 build_fold_indirect_ref_loc (loc, arg2),
7049 fold_build1_loc (loc, REALPART_EXPR, type, call)));
7052 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
7053 arguments to the call, and TYPE is its return type.
7054 Return NULL_TREE if no simplification can be made. */
7056 static tree
7057 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
7059 if (!validate_arg (arg1, POINTER_TYPE)
7060 || !validate_arg (arg2, INTEGER_TYPE)
7061 || !validate_arg (len, INTEGER_TYPE))
7062 return NULL_TREE;
7063 else
7065 const char *p1;
7067 if (TREE_CODE (arg2) != INTEGER_CST
7068 || !tree_fits_uhwi_p (len))
7069 return NULL_TREE;
7071 p1 = c_getstr (arg1);
7072 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
7074 char c;
7075 const char *r;
7076 tree tem;
7078 if (target_char_cast (arg2, &c))
7079 return NULL_TREE;
7081 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
7083 if (r == NULL)
7084 return build_int_cst (TREE_TYPE (arg1), 0);
7086 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
7087 return fold_convert_loc (loc, type, tem);
7089 return NULL_TREE;
7093 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
7094 Return NULL_TREE if no simplification can be made. */
7096 static tree
7097 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
7099 if (!validate_arg (arg1, POINTER_TYPE)
7100 || !validate_arg (arg2, POINTER_TYPE)
7101 || !validate_arg (len, INTEGER_TYPE))
7102 return NULL_TREE;
7104 /* If the LEN parameter is zero, return zero. */
7105 if (integer_zerop (len))
7106 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
7107 arg1, arg2);
7109 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7110 if (operand_equal_p (arg1, arg2, 0))
7111 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
7113 /* If len parameter is one, return an expression corresponding to
7114 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7115 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
7117 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7118 tree cst_uchar_ptr_node
7119 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7121 tree ind1
7122 = fold_convert_loc (loc, integer_type_node,
7123 build1 (INDIRECT_REF, cst_uchar_node,
7124 fold_convert_loc (loc,
7125 cst_uchar_ptr_node,
7126 arg1)));
7127 tree ind2
7128 = fold_convert_loc (loc, integer_type_node,
7129 build1 (INDIRECT_REF, cst_uchar_node,
7130 fold_convert_loc (loc,
7131 cst_uchar_ptr_node,
7132 arg2)));
7133 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
7136 return NULL_TREE;
7139 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
7140 Return NULL_TREE if no simplification can be made. */
7142 static tree
7143 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
7145 if (!validate_arg (arg1, POINTER_TYPE)
7146 || !validate_arg (arg2, POINTER_TYPE))
7147 return NULL_TREE;
7149 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7150 if (operand_equal_p (arg1, arg2, 0))
7151 return integer_zero_node;
7153 /* If the second arg is "", return *(const unsigned char*)arg1. */
7154 const char *p2 = c_getstr (arg2);
7155 if (p2 && *p2 == '\0')
7157 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7158 tree cst_uchar_ptr_node
7159 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7161 return fold_convert_loc (loc, integer_type_node,
7162 build1 (INDIRECT_REF, cst_uchar_node,
7163 fold_convert_loc (loc,
7164 cst_uchar_ptr_node,
7165 arg1)));
7168 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7169 const char *p1 = c_getstr (arg1);
7170 if (p1 && *p1 == '\0')
7172 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7173 tree cst_uchar_ptr_node
7174 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7176 tree temp
7177 = fold_convert_loc (loc, integer_type_node,
7178 build1 (INDIRECT_REF, cst_uchar_node,
7179 fold_convert_loc (loc,
7180 cst_uchar_ptr_node,
7181 arg2)));
7182 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
7185 return NULL_TREE;
7188 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
7189 Return NULL_TREE if no simplification can be made. */
7191 static tree
7192 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
7194 if (!validate_arg (arg1, POINTER_TYPE)
7195 || !validate_arg (arg2, POINTER_TYPE)
7196 || !validate_arg (len, INTEGER_TYPE))
7197 return NULL_TREE;
7199 /* If the LEN parameter is zero, return zero. */
7200 if (integer_zerop (len))
7201 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
7202 arg1, arg2);
7204 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7205 if (operand_equal_p (arg1, arg2, 0))
7206 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
7208 /* If the second arg is "", and the length is greater than zero,
7209 return *(const unsigned char*)arg1. */
7210 const char *p2 = c_getstr (arg2);
7211 if (p2 && *p2 == '\0'
7212 && TREE_CODE (len) == INTEGER_CST
7213 && tree_int_cst_sgn (len) == 1)
7215 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7216 tree cst_uchar_ptr_node
7217 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7219 return fold_convert_loc (loc, integer_type_node,
7220 build1 (INDIRECT_REF, cst_uchar_node,
7221 fold_convert_loc (loc,
7222 cst_uchar_ptr_node,
7223 arg1)));
7226 /* If the first arg is "", and the length is greater than zero,
7227 return -*(const unsigned char*)arg2. */
7228 const char *p1 = c_getstr (arg1);
7229 if (p1 && *p1 == '\0'
7230 && TREE_CODE (len) == INTEGER_CST
7231 && tree_int_cst_sgn (len) == 1)
7233 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7234 tree cst_uchar_ptr_node
7235 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7237 tree temp = fold_convert_loc (loc, integer_type_node,
7238 build1 (INDIRECT_REF, cst_uchar_node,
7239 fold_convert_loc (loc,
7240 cst_uchar_ptr_node,
7241 arg2)));
7242 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
7245 /* If len parameter is one, return an expression corresponding to
7246 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7247 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
7249 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7250 tree cst_uchar_ptr_node
7251 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7253 tree ind1 = fold_convert_loc (loc, integer_type_node,
7254 build1 (INDIRECT_REF, cst_uchar_node,
7255 fold_convert_loc (loc,
7256 cst_uchar_ptr_node,
7257 arg1)));
7258 tree ind2 = fold_convert_loc (loc, integer_type_node,
7259 build1 (INDIRECT_REF, cst_uchar_node,
7260 fold_convert_loc (loc,
7261 cst_uchar_ptr_node,
7262 arg2)));
7263 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
7266 return NULL_TREE;
7269 /* Fold a call to builtin isascii with argument ARG. */
7271 static tree
7272 fold_builtin_isascii (location_t loc, tree arg)
7274 if (!validate_arg (arg, INTEGER_TYPE))
7275 return NULL_TREE;
7276 else
7278 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7279 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
7280 build_int_cst (integer_type_node,
7281 ~ (unsigned HOST_WIDE_INT) 0x7f));
7282 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
7283 arg, integer_zero_node);
7287 /* Fold a call to builtin toascii with argument ARG. */
7289 static tree
7290 fold_builtin_toascii (location_t loc, tree arg)
7292 if (!validate_arg (arg, INTEGER_TYPE))
7293 return NULL_TREE;
7295 /* Transform toascii(c) -> (c & 0x7f). */
7296 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
7297 build_int_cst (integer_type_node, 0x7f));
7300 /* Fold a call to builtin isdigit with argument ARG. */
7302 static tree
7303 fold_builtin_isdigit (location_t loc, tree arg)
7305 if (!validate_arg (arg, INTEGER_TYPE))
7306 return NULL_TREE;
7307 else
7309 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7310 /* According to the C standard, isdigit is unaffected by locale.
7311 However, it definitely is affected by the target character set. */
7312 unsigned HOST_WIDE_INT target_digit0
7313 = lang_hooks.to_target_charset ('0');
7315 if (target_digit0 == 0)
7316 return NULL_TREE;
7318 arg = fold_convert_loc (loc, unsigned_type_node, arg);
7319 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
7320 build_int_cst (unsigned_type_node, target_digit0));
7321 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
7322 build_int_cst (unsigned_type_node, 9));
7326 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
7328 static tree
7329 fold_builtin_fabs (location_t loc, tree arg, tree type)
7331 if (!validate_arg (arg, REAL_TYPE))
7332 return NULL_TREE;
7334 arg = fold_convert_loc (loc, type, arg);
7335 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7338 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
7340 static tree
7341 fold_builtin_abs (location_t loc, tree arg, tree type)
7343 if (!validate_arg (arg, INTEGER_TYPE))
7344 return NULL_TREE;
7346 arg = fold_convert_loc (loc, type, arg);
7347 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7350 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
7352 static tree
7353 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
7355 /* ??? Only expand to FMA_EXPR if it's directly supported. */
7356 if (validate_arg (arg0, REAL_TYPE)
7357 && validate_arg (arg1, REAL_TYPE)
7358 && validate_arg (arg2, REAL_TYPE)
7359 && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
7360 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
7362 return NULL_TREE;
7365 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
7367 static tree
7368 fold_builtin_carg (location_t loc, tree arg, tree type)
7370 if (validate_arg (arg, COMPLEX_TYPE)
7371 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7373 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
7375 if (atan2_fn)
7377 tree new_arg = builtin_save_expr (arg);
7378 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
7379 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
7380 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
7384 return NULL_TREE;
7387 /* Fold a call to builtin frexp, we can assume the base is 2. */
7389 static tree
7390 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
7392 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
7393 return NULL_TREE;
7395 STRIP_NOPS (arg0);
7397 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
7398 return NULL_TREE;
7400 arg1 = build_fold_indirect_ref_loc (loc, arg1);
7402 /* Proceed if a valid pointer type was passed in. */
7403 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
7405 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
7406 tree frac, exp;
7408 switch (value->cl)
7410 case rvc_zero:
7411 /* For +-0, return (*exp = 0, +-0). */
7412 exp = integer_zero_node;
7413 frac = arg0;
7414 break;
7415 case rvc_nan:
7416 case rvc_inf:
7417 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
7418 return omit_one_operand_loc (loc, rettype, arg0, arg1);
7419 case rvc_normal:
7421 /* Since the frexp function always expects base 2, and in
7422 GCC normalized significands are already in the range
7423 [0.5, 1.0), we have exactly what frexp wants. */
7424 REAL_VALUE_TYPE frac_rvt = *value;
7425 SET_REAL_EXP (&frac_rvt, 0);
7426 frac = build_real (rettype, frac_rvt);
7427 exp = build_int_cst (integer_type_node, REAL_EXP (value));
7429 break;
7430 default:
7431 gcc_unreachable ();
7434 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
7435 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
7436 TREE_SIDE_EFFECTS (arg1) = 1;
7437 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
7440 return NULL_TREE;
7443 /* Fold a call to builtin modf. */
7445 static tree
7446 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
7448 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
7449 return NULL_TREE;
7451 STRIP_NOPS (arg0);
7453 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
7454 return NULL_TREE;
7456 arg1 = build_fold_indirect_ref_loc (loc, arg1);
7458 /* Proceed if a valid pointer type was passed in. */
7459 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
7461 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
7462 REAL_VALUE_TYPE trunc, frac;
7464 switch (value->cl)
7466 case rvc_nan:
7467 case rvc_zero:
7468 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
7469 trunc = frac = *value;
7470 break;
7471 case rvc_inf:
7472 /* For +-Inf, return (*arg1 = arg0, +-0). */
7473 frac = dconst0;
7474 frac.sign = value->sign;
7475 trunc = *value;
7476 break;
7477 case rvc_normal:
7478 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
7479 real_trunc (&trunc, VOIDmode, value);
7480 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
7481 /* If the original number was negative and already
7482 integral, then the fractional part is -0.0. */
7483 if (value->sign && frac.cl == rvc_zero)
7484 frac.sign = value->sign;
7485 break;
7488 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
7489 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
7490 build_real (rettype, trunc));
7491 TREE_SIDE_EFFECTS (arg1) = 1;
7492 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
7493 build_real (rettype, frac));
7496 return NULL_TREE;
7499 /* Given a location LOC, an interclass builtin function decl FNDECL
7500 and its single argument ARG, return an folded expression computing
7501 the same, or NULL_TREE if we either couldn't or didn't want to fold
7502 (the latter happen if there's an RTL instruction available). */
7504 static tree
7505 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
7507 machine_mode mode;
7509 if (!validate_arg (arg, REAL_TYPE))
7510 return NULL_TREE;
7512 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
7513 return NULL_TREE;
7515 mode = TYPE_MODE (TREE_TYPE (arg));
7517 /* If there is no optab, try generic code. */
7518 switch (DECL_FUNCTION_CODE (fndecl))
7520 tree result;
7522 CASE_FLT_FN (BUILT_IN_ISINF):
7524 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
7525 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
7526 tree const type = TREE_TYPE (arg);
7527 REAL_VALUE_TYPE r;
7528 char buf[128];
7530 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
7531 real_from_string (&r, buf);
7532 result = build_call_expr (isgr_fn, 2,
7533 fold_build1_loc (loc, ABS_EXPR, type, arg),
7534 build_real (type, r));
7535 return result;
7537 CASE_FLT_FN (BUILT_IN_FINITE):
7538 case BUILT_IN_ISFINITE:
7540 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
7541 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
7542 tree const type = TREE_TYPE (arg);
7543 REAL_VALUE_TYPE r;
7544 char buf[128];
7546 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
7547 real_from_string (&r, buf);
7548 result = build_call_expr (isle_fn, 2,
7549 fold_build1_loc (loc, ABS_EXPR, type, arg),
7550 build_real (type, r));
7551 /*result = fold_build2_loc (loc, UNGT_EXPR,
7552 TREE_TYPE (TREE_TYPE (fndecl)),
7553 fold_build1_loc (loc, ABS_EXPR, type, arg),
7554 build_real (type, r));
7555 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
7556 TREE_TYPE (TREE_TYPE (fndecl)),
7557 result);*/
7558 return result;
7560 case BUILT_IN_ISNORMAL:
7562 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
7563 islessequal(fabs(x),DBL_MAX). */
7564 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
7565 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
7566 tree const type = TREE_TYPE (arg);
7567 REAL_VALUE_TYPE rmax, rmin;
7568 char buf[128];
7570 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
7571 real_from_string (&rmax, buf);
7572 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
7573 real_from_string (&rmin, buf);
7574 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
7575 result = build_call_expr (isle_fn, 2, arg,
7576 build_real (type, rmax));
7577 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
7578 build_call_expr (isge_fn, 2, arg,
7579 build_real (type, rmin)));
7580 return result;
7582 default:
7583 break;
7586 return NULL_TREE;
7589 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7590 ARG is the argument for the call. */
7592 static tree
7593 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
7595 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7597 if (!validate_arg (arg, REAL_TYPE))
7598 return NULL_TREE;
7600 switch (builtin_index)
7602 case BUILT_IN_ISINF:
7603 if (!HONOR_INFINITIES (arg))
7604 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
7606 return NULL_TREE;
7608 case BUILT_IN_ISINF_SIGN:
7610 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
7611 /* In a boolean context, GCC will fold the inner COND_EXPR to
7612 1. So e.g. "if (isinf_sign(x))" would be folded to just
7613 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
7614 tree signbit_fn = mathfn_built_in_1
7615 (TREE_TYPE (arg), CFN_BUILT_IN_SIGNBIT, 0);
7616 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
7617 tree tmp = NULL_TREE;
7619 arg = builtin_save_expr (arg);
7621 if (signbit_fn && isinf_fn)
7623 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
7624 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
7626 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
7627 signbit_call, integer_zero_node);
7628 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
7629 isinf_call, integer_zero_node);
7631 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
7632 integer_minus_one_node, integer_one_node);
7633 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
7634 isinf_call, tmp,
7635 integer_zero_node);
7638 return tmp;
7641 case BUILT_IN_ISFINITE:
7642 if (!HONOR_NANS (arg)
7643 && !HONOR_INFINITIES (arg))
7644 return omit_one_operand_loc (loc, type, integer_one_node, arg);
7646 return NULL_TREE;
7648 case BUILT_IN_ISNAN:
7649 if (!HONOR_NANS (arg))
7650 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
7652 arg = builtin_save_expr (arg);
7653 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
7655 default:
7656 gcc_unreachable ();
7660 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
7661 This builtin will generate code to return the appropriate floating
7662 point classification depending on the value of the floating point
7663 number passed in. The possible return values must be supplied as
7664 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
7665 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
7666 one floating point argument which is "type generic". */
7668 static tree
7669 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
7671 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
7672 arg, type, res, tmp;
7673 machine_mode mode;
7674 REAL_VALUE_TYPE r;
7675 char buf[128];
7677 /* Verify the required arguments in the original call. */
7678 if (nargs != 6
7679 || !validate_arg (args[0], INTEGER_TYPE)
7680 || !validate_arg (args[1], INTEGER_TYPE)
7681 || !validate_arg (args[2], INTEGER_TYPE)
7682 || !validate_arg (args[3], INTEGER_TYPE)
7683 || !validate_arg (args[4], INTEGER_TYPE)
7684 || !validate_arg (args[5], REAL_TYPE))
7685 return NULL_TREE;
7687 fp_nan = args[0];
7688 fp_infinite = args[1];
7689 fp_normal = args[2];
7690 fp_subnormal = args[3];
7691 fp_zero = args[4];
7692 arg = args[5];
7693 type = TREE_TYPE (arg);
7694 mode = TYPE_MODE (type);
7695 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
7697 /* fpclassify(x) ->
7698 isnan(x) ? FP_NAN :
7699 (fabs(x) == Inf ? FP_INFINITE :
7700 (fabs(x) >= DBL_MIN ? FP_NORMAL :
7701 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
7703 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
7704 build_real (type, dconst0));
7705 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
7706 tmp, fp_zero, fp_subnormal);
7708 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
7709 real_from_string (&r, buf);
7710 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
7711 arg, build_real (type, r));
7712 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
7714 if (HONOR_INFINITIES (mode))
7716 real_inf (&r);
7717 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
7718 build_real (type, r));
7719 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
7720 fp_infinite, res);
7723 if (HONOR_NANS (mode))
7725 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
7726 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
7729 return res;
7732 /* Fold a call to an unordered comparison function such as
7733 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
7734 being called and ARG0 and ARG1 are the arguments for the call.
7735 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7736 the opposite of the desired result. UNORDERED_CODE is used
7737 for modes that can hold NaNs and ORDERED_CODE is used for
7738 the rest. */
7740 static tree
7741 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
7742 enum tree_code unordered_code,
7743 enum tree_code ordered_code)
7745 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7746 enum tree_code code;
7747 tree type0, type1;
7748 enum tree_code code0, code1;
7749 tree cmp_type = NULL_TREE;
7751 type0 = TREE_TYPE (arg0);
7752 type1 = TREE_TYPE (arg1);
7754 code0 = TREE_CODE (type0);
7755 code1 = TREE_CODE (type1);
7757 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7758 /* Choose the wider of two real types. */
7759 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7760 ? type0 : type1;
7761 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7762 cmp_type = type0;
7763 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7764 cmp_type = type1;
7766 arg0 = fold_convert_loc (loc, cmp_type, arg0);
7767 arg1 = fold_convert_loc (loc, cmp_type, arg1);
7769 if (unordered_code == UNORDERED_EXPR)
7771 if (!HONOR_NANS (arg0))
7772 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
7773 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
7776 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
7777 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
7778 fold_build2_loc (loc, code, type, arg0, arg1));
7781 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
7782 arithmetics if it can never overflow, or into internal functions that
7783 return both result of arithmetics and overflowed boolean flag in
7784 a complex integer result, or some other check for overflow. */
7786 static tree
7787 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
7788 tree arg0, tree arg1, tree arg2)
7790 enum internal_fn ifn = IFN_LAST;
7791 tree type = TREE_TYPE (TREE_TYPE (arg2));
7792 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
7793 switch (fcode)
7795 case BUILT_IN_ADD_OVERFLOW:
7796 case BUILT_IN_SADD_OVERFLOW:
7797 case BUILT_IN_SADDL_OVERFLOW:
7798 case BUILT_IN_SADDLL_OVERFLOW:
7799 case BUILT_IN_UADD_OVERFLOW:
7800 case BUILT_IN_UADDL_OVERFLOW:
7801 case BUILT_IN_UADDLL_OVERFLOW:
7802 ifn = IFN_ADD_OVERFLOW;
7803 break;
7804 case BUILT_IN_SUB_OVERFLOW:
7805 case BUILT_IN_SSUB_OVERFLOW:
7806 case BUILT_IN_SSUBL_OVERFLOW:
7807 case BUILT_IN_SSUBLL_OVERFLOW:
7808 case BUILT_IN_USUB_OVERFLOW:
7809 case BUILT_IN_USUBL_OVERFLOW:
7810 case BUILT_IN_USUBLL_OVERFLOW:
7811 ifn = IFN_SUB_OVERFLOW;
7812 break;
7813 case BUILT_IN_MUL_OVERFLOW:
7814 case BUILT_IN_SMUL_OVERFLOW:
7815 case BUILT_IN_SMULL_OVERFLOW:
7816 case BUILT_IN_SMULLL_OVERFLOW:
7817 case BUILT_IN_UMUL_OVERFLOW:
7818 case BUILT_IN_UMULL_OVERFLOW:
7819 case BUILT_IN_UMULLL_OVERFLOW:
7820 ifn = IFN_MUL_OVERFLOW;
7821 break;
7822 default:
7823 gcc_unreachable ();
7825 tree ctype = build_complex_type (type);
7826 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
7827 2, arg0, arg1);
7828 tree tgt = save_expr (call);
7829 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
7830 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
7831 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
7832 tree store
7833 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
7834 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
7837 /* Fold a call to built-in function FNDECL with 0 arguments.
7838 This function returns NULL_TREE if no simplification was possible. */
7840 static tree
7841 fold_builtin_0 (location_t loc, tree fndecl)
7843 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7844 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7845 switch (fcode)
7847 CASE_FLT_FN (BUILT_IN_INF):
7848 case BUILT_IN_INFD32:
7849 case BUILT_IN_INFD64:
7850 case BUILT_IN_INFD128:
7851 return fold_builtin_inf (loc, type, true);
7853 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
7854 return fold_builtin_inf (loc, type, false);
7856 case BUILT_IN_CLASSIFY_TYPE:
7857 return fold_builtin_classify_type (NULL_TREE);
7859 default:
7860 break;
7862 return NULL_TREE;
7865 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
7866 This function returns NULL_TREE if no simplification was possible. */
7868 static tree
7869 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
7871 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7872 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7874 if (TREE_CODE (arg0) == ERROR_MARK)
7875 return NULL_TREE;
7877 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
7878 return ret;
7880 switch (fcode)
7882 case BUILT_IN_CONSTANT_P:
7884 tree val = fold_builtin_constant_p (arg0);
7886 /* Gimplification will pull the CALL_EXPR for the builtin out of
7887 an if condition. When not optimizing, we'll not CSE it back.
7888 To avoid link error types of regressions, return false now. */
7889 if (!val && !optimize)
7890 val = integer_zero_node;
7892 return val;
7895 case BUILT_IN_CLASSIFY_TYPE:
7896 return fold_builtin_classify_type (arg0);
7898 case BUILT_IN_STRLEN:
7899 return fold_builtin_strlen (loc, type, arg0);
7901 CASE_FLT_FN (BUILT_IN_FABS):
7902 case BUILT_IN_FABSD32:
7903 case BUILT_IN_FABSD64:
7904 case BUILT_IN_FABSD128:
7905 return fold_builtin_fabs (loc, arg0, type);
7907 case BUILT_IN_ABS:
7908 case BUILT_IN_LABS:
7909 case BUILT_IN_LLABS:
7910 case BUILT_IN_IMAXABS:
7911 return fold_builtin_abs (loc, arg0, type);
7913 CASE_FLT_FN (BUILT_IN_CONJ):
7914 if (validate_arg (arg0, COMPLEX_TYPE)
7915 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
7916 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
7917 break;
7919 CASE_FLT_FN (BUILT_IN_CREAL):
7920 if (validate_arg (arg0, COMPLEX_TYPE)
7921 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
7922 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
7923 break;
7925 CASE_FLT_FN (BUILT_IN_CIMAG):
7926 if (validate_arg (arg0, COMPLEX_TYPE)
7927 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
7928 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
7929 break;
7931 CASE_FLT_FN (BUILT_IN_CARG):
7932 return fold_builtin_carg (loc, arg0, type);
7934 case BUILT_IN_ISASCII:
7935 return fold_builtin_isascii (loc, arg0);
7937 case BUILT_IN_TOASCII:
7938 return fold_builtin_toascii (loc, arg0);
7940 case BUILT_IN_ISDIGIT:
7941 return fold_builtin_isdigit (loc, arg0);
7943 CASE_FLT_FN (BUILT_IN_FINITE):
7944 case BUILT_IN_FINITED32:
7945 case BUILT_IN_FINITED64:
7946 case BUILT_IN_FINITED128:
7947 case BUILT_IN_ISFINITE:
7949 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
7950 if (ret)
7951 return ret;
7952 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
7955 CASE_FLT_FN (BUILT_IN_ISINF):
7956 case BUILT_IN_ISINFD32:
7957 case BUILT_IN_ISINFD64:
7958 case BUILT_IN_ISINFD128:
7960 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
7961 if (ret)
7962 return ret;
7963 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
7966 case BUILT_IN_ISNORMAL:
7967 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
7969 case BUILT_IN_ISINF_SIGN:
7970 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
7972 CASE_FLT_FN (BUILT_IN_ISNAN):
7973 case BUILT_IN_ISNAND32:
7974 case BUILT_IN_ISNAND64:
7975 case BUILT_IN_ISNAND128:
7976 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
7978 case BUILT_IN_FREE:
7979 if (integer_zerop (arg0))
7980 return build_empty_stmt (loc);
7981 break;
7983 default:
7984 break;
7987 return NULL_TREE;
7991 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
7992 This function returns NULL_TREE if no simplification was possible. */
7994 static tree
7995 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
7997 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7998 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8000 if (TREE_CODE (arg0) == ERROR_MARK
8001 || TREE_CODE (arg1) == ERROR_MARK)
8002 return NULL_TREE;
8004 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
8005 return ret;
8007 switch (fcode)
8009 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
8010 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
8011 if (validate_arg (arg0, REAL_TYPE)
8012 && validate_arg (arg1, POINTER_TYPE))
8013 return do_mpfr_lgamma_r (arg0, arg1, type);
8014 break;
8016 CASE_FLT_FN (BUILT_IN_FREXP):
8017 return fold_builtin_frexp (loc, arg0, arg1, type);
8019 CASE_FLT_FN (BUILT_IN_MODF):
8020 return fold_builtin_modf (loc, arg0, arg1, type);
8022 case BUILT_IN_STRSTR:
8023 return fold_builtin_strstr (loc, arg0, arg1, type);
8025 case BUILT_IN_STRSPN:
8026 return fold_builtin_strspn (loc, arg0, arg1);
8028 case BUILT_IN_STRCSPN:
8029 return fold_builtin_strcspn (loc, arg0, arg1);
8031 case BUILT_IN_STRCHR:
8032 case BUILT_IN_INDEX:
8033 return fold_builtin_strchr (loc, arg0, arg1, type);
8035 case BUILT_IN_STRRCHR:
8036 case BUILT_IN_RINDEX:
8037 return fold_builtin_strrchr (loc, arg0, arg1, type);
8039 case BUILT_IN_STRCMP:
8040 return fold_builtin_strcmp (loc, arg0, arg1);
8042 case BUILT_IN_STRPBRK:
8043 return fold_builtin_strpbrk (loc, arg0, arg1, type);
8045 case BUILT_IN_EXPECT:
8046 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
8048 case BUILT_IN_ISGREATER:
8049 return fold_builtin_unordered_cmp (loc, fndecl,
8050 arg0, arg1, UNLE_EXPR, LE_EXPR);
8051 case BUILT_IN_ISGREATEREQUAL:
8052 return fold_builtin_unordered_cmp (loc, fndecl,
8053 arg0, arg1, UNLT_EXPR, LT_EXPR);
8054 case BUILT_IN_ISLESS:
8055 return fold_builtin_unordered_cmp (loc, fndecl,
8056 arg0, arg1, UNGE_EXPR, GE_EXPR);
8057 case BUILT_IN_ISLESSEQUAL:
8058 return fold_builtin_unordered_cmp (loc, fndecl,
8059 arg0, arg1, UNGT_EXPR, GT_EXPR);
8060 case BUILT_IN_ISLESSGREATER:
8061 return fold_builtin_unordered_cmp (loc, fndecl,
8062 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
8063 case BUILT_IN_ISUNORDERED:
8064 return fold_builtin_unordered_cmp (loc, fndecl,
8065 arg0, arg1, UNORDERED_EXPR,
8066 NOP_EXPR);
8068 /* We do the folding for va_start in the expander. */
8069 case BUILT_IN_VA_START:
8070 break;
8072 case BUILT_IN_OBJECT_SIZE:
8073 return fold_builtin_object_size (arg0, arg1);
8075 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8076 return fold_builtin_atomic_always_lock_free (arg0, arg1);
8078 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8079 return fold_builtin_atomic_is_lock_free (arg0, arg1);
8081 default:
8082 break;
8084 return NULL_TREE;
8087 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
8088 and ARG2.
8089 This function returns NULL_TREE if no simplification was possible. */
8091 static tree
8092 fold_builtin_3 (location_t loc, tree fndecl,
8093 tree arg0, tree arg1, tree arg2)
8095 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8096 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8098 if (TREE_CODE (arg0) == ERROR_MARK
8099 || TREE_CODE (arg1) == ERROR_MARK
8100 || TREE_CODE (arg2) == ERROR_MARK)
8101 return NULL_TREE;
8103 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
8104 arg0, arg1, arg2))
8105 return ret;
8107 switch (fcode)
8110 CASE_FLT_FN (BUILT_IN_SINCOS):
8111 return fold_builtin_sincos (loc, arg0, arg1, arg2);
8113 CASE_FLT_FN (BUILT_IN_FMA):
8114 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
8116 CASE_FLT_FN (BUILT_IN_REMQUO):
8117 if (validate_arg (arg0, REAL_TYPE)
8118 && validate_arg (arg1, REAL_TYPE)
8119 && validate_arg (arg2, POINTER_TYPE))
8120 return do_mpfr_remquo (arg0, arg1, arg2);
8121 break;
8123 case BUILT_IN_STRNCMP:
8124 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
8126 case BUILT_IN_MEMCHR:
8127 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
8129 case BUILT_IN_BCMP:
8130 case BUILT_IN_MEMCMP:
8131 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
8133 case BUILT_IN_EXPECT:
8134 return fold_builtin_expect (loc, arg0, arg1, arg2);
8136 case BUILT_IN_ADD_OVERFLOW:
8137 case BUILT_IN_SUB_OVERFLOW:
8138 case BUILT_IN_MUL_OVERFLOW:
8139 case BUILT_IN_SADD_OVERFLOW:
8140 case BUILT_IN_SADDL_OVERFLOW:
8141 case BUILT_IN_SADDLL_OVERFLOW:
8142 case BUILT_IN_SSUB_OVERFLOW:
8143 case BUILT_IN_SSUBL_OVERFLOW:
8144 case BUILT_IN_SSUBLL_OVERFLOW:
8145 case BUILT_IN_SMUL_OVERFLOW:
8146 case BUILT_IN_SMULL_OVERFLOW:
8147 case BUILT_IN_SMULLL_OVERFLOW:
8148 case BUILT_IN_UADD_OVERFLOW:
8149 case BUILT_IN_UADDL_OVERFLOW:
8150 case BUILT_IN_UADDLL_OVERFLOW:
8151 case BUILT_IN_USUB_OVERFLOW:
8152 case BUILT_IN_USUBL_OVERFLOW:
8153 case BUILT_IN_USUBLL_OVERFLOW:
8154 case BUILT_IN_UMUL_OVERFLOW:
8155 case BUILT_IN_UMULL_OVERFLOW:
8156 case BUILT_IN_UMULLL_OVERFLOW:
8157 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
8159 default:
8160 break;
8162 return NULL_TREE;
8165 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
8166 arguments. IGNORE is true if the result of the
8167 function call is ignored. This function returns NULL_TREE if no
8168 simplification was possible. */
8170 tree
8171 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
8173 tree ret = NULL_TREE;
8175 switch (nargs)
8177 case 0:
8178 ret = fold_builtin_0 (loc, fndecl);
8179 break;
8180 case 1:
8181 ret = fold_builtin_1 (loc, fndecl, args[0]);
8182 break;
8183 case 2:
8184 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
8185 break;
8186 case 3:
8187 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
8188 break;
8189 default:
8190 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
8191 break;
8193 if (ret)
8195 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
8196 SET_EXPR_LOCATION (ret, loc);
8197 TREE_NO_WARNING (ret) = 1;
8198 return ret;
8200 return NULL_TREE;
8203 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
8204 list ARGS along with N new arguments in NEWARGS. SKIP is the number
8205 of arguments in ARGS to be omitted. OLDNARGS is the number of
8206 elements in ARGS. */
8208 static tree
8209 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
8210 int skip, tree fndecl, int n, va_list newargs)
8212 int nargs = oldnargs - skip + n;
8213 tree *buffer;
8215 if (n > 0)
8217 int i, j;
8219 buffer = XALLOCAVEC (tree, nargs);
8220 for (i = 0; i < n; i++)
8221 buffer[i] = va_arg (newargs, tree);
8222 for (j = skip; j < oldnargs; j++, i++)
8223 buffer[i] = args[j];
8225 else
8226 buffer = args + skip;
8228 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
8231 /* Return true if FNDECL shouldn't be folded right now.
8232 If a built-in function has an inline attribute always_inline
8233 wrapper, defer folding it after always_inline functions have
8234 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
8235 might not be performed. */
8237 bool
8238 avoid_folding_inline_builtin (tree fndecl)
8240 return (DECL_DECLARED_INLINE_P (fndecl)
8241 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
8242 && cfun
8243 && !cfun->always_inline_functions_inlined
8244 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
8247 /* A wrapper function for builtin folding that prevents warnings for
8248 "statement without effect" and the like, caused by removing the
8249 call node earlier than the warning is generated. */
8251 tree
8252 fold_call_expr (location_t loc, tree exp, bool ignore)
8254 tree ret = NULL_TREE;
8255 tree fndecl = get_callee_fndecl (exp);
8256 if (fndecl
8257 && TREE_CODE (fndecl) == FUNCTION_DECL
8258 && DECL_BUILT_IN (fndecl)
8259 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
8260 yet. Defer folding until we see all the arguments
8261 (after inlining). */
8262 && !CALL_EXPR_VA_ARG_PACK (exp))
8264 int nargs = call_expr_nargs (exp);
8266 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
8267 instead last argument is __builtin_va_arg_pack (). Defer folding
8268 even in that case, until arguments are finalized. */
8269 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
8271 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
8272 if (fndecl2
8273 && TREE_CODE (fndecl2) == FUNCTION_DECL
8274 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
8275 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
8276 return NULL_TREE;
8279 if (avoid_folding_inline_builtin (fndecl))
8280 return NULL_TREE;
8282 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8283 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
8284 CALL_EXPR_ARGP (exp), ignore);
8285 else
8287 tree *args = CALL_EXPR_ARGP (exp);
8288 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
8289 if (ret)
8290 return ret;
8293 return NULL_TREE;
8296 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
8297 N arguments are passed in the array ARGARRAY. Return a folded
8298 expression or NULL_TREE if no simplification was possible. */
8300 tree
8301 fold_builtin_call_array (location_t loc, tree,
8302 tree fn,
8303 int n,
8304 tree *argarray)
8306 if (TREE_CODE (fn) != ADDR_EXPR)
8307 return NULL_TREE;
8309 tree fndecl = TREE_OPERAND (fn, 0);
8310 if (TREE_CODE (fndecl) == FUNCTION_DECL
8311 && DECL_BUILT_IN (fndecl))
8313 /* If last argument is __builtin_va_arg_pack (), arguments to this
8314 function are not finalized yet. Defer folding until they are. */
8315 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
8317 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
8318 if (fndecl2
8319 && TREE_CODE (fndecl2) == FUNCTION_DECL
8320 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
8321 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
8322 return NULL_TREE;
8324 if (avoid_folding_inline_builtin (fndecl))
8325 return NULL_TREE;
8326 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8327 return targetm.fold_builtin (fndecl, n, argarray, false);
8328 else
8329 return fold_builtin_n (loc, fndecl, argarray, n, false);
8332 return NULL_TREE;
8335 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
8336 along with N new arguments specified as the "..." parameters. SKIP
8337 is the number of arguments in EXP to be omitted. This function is used
8338 to do varargs-to-varargs transformations. */
8340 static tree
8341 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
8343 va_list ap;
8344 tree t;
8346 va_start (ap, n);
8347 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
8348 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
8349 va_end (ap);
8351 return t;
8354 /* Validate a single argument ARG against a tree code CODE representing
8355 a type. */
8357 static bool
8358 validate_arg (const_tree arg, enum tree_code code)
8360 if (!arg)
8361 return false;
8362 else if (code == POINTER_TYPE)
8363 return POINTER_TYPE_P (TREE_TYPE (arg));
8364 else if (code == INTEGER_TYPE)
8365 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
8366 return code == TREE_CODE (TREE_TYPE (arg));
8369 /* This function validates the types of a function call argument list
8370 against a specified list of tree_codes. If the last specifier is a 0,
8371 that represents an ellipses, otherwise the last specifier must be a
8372 VOID_TYPE.
8374 This is the GIMPLE version of validate_arglist. Eventually we want to
8375 completely convert builtins.c to work from GIMPLEs and the tree based
8376 validate_arglist will then be removed. */
8378 bool
8379 validate_gimple_arglist (const gcall *call, ...)
8381 enum tree_code code;
8382 bool res = 0;
8383 va_list ap;
8384 const_tree arg;
8385 size_t i;
8387 va_start (ap, call);
8388 i = 0;
8392 code = (enum tree_code) va_arg (ap, int);
8393 switch (code)
8395 case 0:
8396 /* This signifies an ellipses, any further arguments are all ok. */
8397 res = true;
8398 goto end;
8399 case VOID_TYPE:
8400 /* This signifies an endlink, if no arguments remain, return
8401 true, otherwise return false. */
8402 res = (i == gimple_call_num_args (call));
8403 goto end;
8404 default:
8405 /* If no parameters remain or the parameter's code does not
8406 match the specified code, return false. Otherwise continue
8407 checking any remaining arguments. */
8408 arg = gimple_call_arg (call, i++);
8409 if (!validate_arg (arg, code))
8410 goto end;
8411 break;
8414 while (1);
8416 /* We need gotos here since we can only have one VA_CLOSE in a
8417 function. */
8418 end: ;
8419 va_end (ap);
8421 return res;
8424 /* Default target-specific builtin expander that does nothing. */
8427 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8428 rtx target ATTRIBUTE_UNUSED,
8429 rtx subtarget ATTRIBUTE_UNUSED,
8430 machine_mode mode ATTRIBUTE_UNUSED,
8431 int ignore ATTRIBUTE_UNUSED)
8433 return NULL_RTX;
8436 /* Returns true is EXP represents data that would potentially reside
8437 in a readonly section. */
8439 bool
8440 readonly_data_expr (tree exp)
8442 STRIP_NOPS (exp);
8444 if (TREE_CODE (exp) != ADDR_EXPR)
8445 return false;
8447 exp = get_base_address (TREE_OPERAND (exp, 0));
8448 if (!exp)
8449 return false;
8451 /* Make sure we call decl_readonly_section only for trees it
8452 can handle (since it returns true for everything it doesn't
8453 understand). */
8454 if (TREE_CODE (exp) == STRING_CST
8455 || TREE_CODE (exp) == CONSTRUCTOR
8456 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8457 return decl_readonly_section (exp, 0);
8458 else
8459 return false;
8462 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
8463 to the call, and TYPE is its return type.
8465 Return NULL_TREE if no simplification was possible, otherwise return the
8466 simplified form of the call as a tree.
8468 The simplified form may be a constant or other expression which
8469 computes the same value, but in a more efficient manner (including
8470 calls to other builtin functions).
8472 The call may contain arguments which need to be evaluated, but
8473 which are not useful to determine the result of the call. In
8474 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8475 COMPOUND_EXPR will be an argument which must be evaluated.
8476 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8477 COMPOUND_EXPR in the chain will contain the tree for the simplified
8478 form of the builtin function call. */
8480 static tree
8481 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
8483 if (!validate_arg (s1, POINTER_TYPE)
8484 || !validate_arg (s2, POINTER_TYPE))
8485 return NULL_TREE;
8486 else
8488 tree fn;
8489 const char *p1, *p2;
8491 p2 = c_getstr (s2);
8492 if (p2 == NULL)
8493 return NULL_TREE;
8495 p1 = c_getstr (s1);
8496 if (p1 != NULL)
8498 const char *r = strstr (p1, p2);
8499 tree tem;
8501 if (r == NULL)
8502 return build_int_cst (TREE_TYPE (s1), 0);
8504 /* Return an offset into the constant string argument. */
8505 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
8506 return fold_convert_loc (loc, type, tem);
8509 /* The argument is const char *, and the result is char *, so we need
8510 a type conversion here to avoid a warning. */
8511 if (p2[0] == '\0')
8512 return fold_convert_loc (loc, type, s1);
8514 if (p2[1] != '\0')
8515 return NULL_TREE;
8517 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
8518 if (!fn)
8519 return NULL_TREE;
8521 /* New argument list transforming strstr(s1, s2) to
8522 strchr(s1, s2[0]). */
8523 return build_call_expr_loc (loc, fn, 2, s1,
8524 build_int_cst (integer_type_node, p2[0]));
8528 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
8529 the call, and TYPE is its return type.
8531 Return NULL_TREE if no simplification was possible, otherwise return the
8532 simplified form of the call as a tree.
8534 The simplified form may be a constant or other expression which
8535 computes the same value, but in a more efficient manner (including
8536 calls to other builtin functions).
8538 The call may contain arguments which need to be evaluated, but
8539 which are not useful to determine the result of the call. In
8540 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8541 COMPOUND_EXPR will be an argument which must be evaluated.
8542 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8543 COMPOUND_EXPR in the chain will contain the tree for the simplified
8544 form of the builtin function call. */
8546 static tree
8547 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
8549 if (!validate_arg (s1, POINTER_TYPE)
8550 || !validate_arg (s2, INTEGER_TYPE))
8551 return NULL_TREE;
8552 else
8554 const char *p1;
8556 if (TREE_CODE (s2) != INTEGER_CST)
8557 return NULL_TREE;
8559 p1 = c_getstr (s1);
8560 if (p1 != NULL)
8562 char c;
8563 const char *r;
8564 tree tem;
8566 if (target_char_cast (s2, &c))
8567 return NULL_TREE;
8569 r = strchr (p1, c);
8571 if (r == NULL)
8572 return build_int_cst (TREE_TYPE (s1), 0);
8574 /* Return an offset into the constant string argument. */
8575 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
8576 return fold_convert_loc (loc, type, tem);
8578 return NULL_TREE;
8582 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
8583 the call, and TYPE is its return type.
8585 Return NULL_TREE if no simplification was possible, otherwise return the
8586 simplified form of the call as a tree.
8588 The simplified form may be a constant or other expression which
8589 computes the same value, but in a more efficient manner (including
8590 calls to other builtin functions).
8592 The call may contain arguments which need to be evaluated, but
8593 which are not useful to determine the result of the call. In
8594 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8595 COMPOUND_EXPR will be an argument which must be evaluated.
8596 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8597 COMPOUND_EXPR in the chain will contain the tree for the simplified
8598 form of the builtin function call. */
8600 static tree
8601 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
8603 if (!validate_arg (s1, POINTER_TYPE)
8604 || !validate_arg (s2, INTEGER_TYPE))
8605 return NULL_TREE;
8606 else
8608 tree fn;
8609 const char *p1;
8611 if (TREE_CODE (s2) != INTEGER_CST)
8612 return NULL_TREE;
8614 p1 = c_getstr (s1);
8615 if (p1 != NULL)
8617 char c;
8618 const char *r;
8619 tree tem;
8621 if (target_char_cast (s2, &c))
8622 return NULL_TREE;
8624 r = strrchr (p1, c);
8626 if (r == NULL)
8627 return build_int_cst (TREE_TYPE (s1), 0);
8629 /* Return an offset into the constant string argument. */
8630 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
8631 return fold_convert_loc (loc, type, tem);
8634 if (! integer_zerop (s2))
8635 return NULL_TREE;
8637 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
8638 if (!fn)
8639 return NULL_TREE;
8641 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8642 return build_call_expr_loc (loc, fn, 2, s1, s2);
8646 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
8647 to the call, and TYPE is its return type.
8649 Return NULL_TREE if no simplification was possible, otherwise return the
8650 simplified form of the call as a tree.
8652 The simplified form may be a constant or other expression which
8653 computes the same value, but in a more efficient manner (including
8654 calls to other builtin functions).
8656 The call may contain arguments which need to be evaluated, but
8657 which are not useful to determine the result of the call. In
8658 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8659 COMPOUND_EXPR will be an argument which must be evaluated.
8660 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8661 COMPOUND_EXPR in the chain will contain the tree for the simplified
8662 form of the builtin function call. */
8664 static tree
8665 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
8667 if (!validate_arg (s1, POINTER_TYPE)
8668 || !validate_arg (s2, POINTER_TYPE))
8669 return NULL_TREE;
8670 else
8672 tree fn;
8673 const char *p1, *p2;
8675 p2 = c_getstr (s2);
8676 if (p2 == NULL)
8677 return NULL_TREE;
8679 p1 = c_getstr (s1);
8680 if (p1 != NULL)
8682 const char *r = strpbrk (p1, p2);
8683 tree tem;
8685 if (r == NULL)
8686 return build_int_cst (TREE_TYPE (s1), 0);
8688 /* Return an offset into the constant string argument. */
8689 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
8690 return fold_convert_loc (loc, type, tem);
8693 if (p2[0] == '\0')
8694 /* strpbrk(x, "") == NULL.
8695 Evaluate and ignore s1 in case it had side-effects. */
8696 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
8698 if (p2[1] != '\0')
8699 return NULL_TREE; /* Really call strpbrk. */
8701 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
8702 if (!fn)
8703 return NULL_TREE;
8705 /* New argument list transforming strpbrk(s1, s2) to
8706 strchr(s1, s2[0]). */
8707 return build_call_expr_loc (loc, fn, 2, s1,
8708 build_int_cst (integer_type_node, p2[0]));
8712 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
8713 to the call.
8715 Return NULL_TREE if no simplification was possible, otherwise return the
8716 simplified form of the call as a tree.
8718 The simplified form may be a constant or other expression which
8719 computes the same value, but in a more efficient manner (including
8720 calls to other builtin functions).
8722 The call may contain arguments which need to be evaluated, but
8723 which are not useful to determine the result of the call. In
8724 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8725 COMPOUND_EXPR will be an argument which must be evaluated.
8726 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8727 COMPOUND_EXPR in the chain will contain the tree for the simplified
8728 form of the builtin function call. */
8730 static tree
8731 fold_builtin_strspn (location_t loc, tree s1, tree s2)
8733 if (!validate_arg (s1, POINTER_TYPE)
8734 || !validate_arg (s2, POINTER_TYPE))
8735 return NULL_TREE;
8736 else
8738 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8740 /* If either argument is "", return NULL_TREE. */
8741 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8742 /* Evaluate and ignore both arguments in case either one has
8743 side-effects. */
8744 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
8745 s1, s2);
8746 return NULL_TREE;
8750 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
8751 to the call.
8753 Return NULL_TREE if no simplification was possible, otherwise return the
8754 simplified form of the call as a tree.
8756 The simplified form may be a constant or other expression which
8757 computes the same value, but in a more efficient manner (including
8758 calls to other builtin functions).
8760 The call may contain arguments which need to be evaluated, but
8761 which are not useful to determine the result of the call. In
8762 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8763 COMPOUND_EXPR will be an argument which must be evaluated.
8764 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8765 COMPOUND_EXPR in the chain will contain the tree for the simplified
8766 form of the builtin function call. */
8768 static tree
8769 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
8771 if (!validate_arg (s1, POINTER_TYPE)
8772 || !validate_arg (s2, POINTER_TYPE))
8773 return NULL_TREE;
8774 else
8776 /* If the first argument is "", return NULL_TREE. */
8777 const char *p1 = c_getstr (s1);
8778 if (p1 && *p1 == '\0')
8780 /* Evaluate and ignore argument s2 in case it has
8781 side-effects. */
8782 return omit_one_operand_loc (loc, size_type_node,
8783 size_zero_node, s2);
8786 /* If the second argument is "", return __builtin_strlen(s1). */
8787 const char *p2 = c_getstr (s2);
8788 if (p2 && *p2 == '\0')
8790 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
8792 /* If the replacement _DECL isn't initialized, don't do the
8793 transformation. */
8794 if (!fn)
8795 return NULL_TREE;
8797 return build_call_expr_loc (loc, fn, 1, s1);
8799 return NULL_TREE;
8803 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
8804 produced. False otherwise. This is done so that we don't output the error
8805 or warning twice or three times. */
8807 bool
8808 fold_builtin_next_arg (tree exp, bool va_start_p)
8810 tree fntype = TREE_TYPE (current_function_decl);
8811 int nargs = call_expr_nargs (exp);
8812 tree arg;
8813 /* There is good chance the current input_location points inside the
8814 definition of the va_start macro (perhaps on the token for
8815 builtin) in a system header, so warnings will not be emitted.
8816 Use the location in real source code. */
8817 source_location current_location =
8818 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
8819 NULL);
8821 if (!stdarg_p (fntype))
8823 error ("%<va_start%> used in function with fixed args");
8824 return true;
8827 if (va_start_p)
8829 if (va_start_p && (nargs != 2))
8831 error ("wrong number of arguments to function %<va_start%>");
8832 return true;
8834 arg = CALL_EXPR_ARG (exp, 1);
8836 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8837 when we checked the arguments and if needed issued a warning. */
8838 else
8840 if (nargs == 0)
8842 /* Evidently an out of date version of <stdarg.h>; can't validate
8843 va_start's second argument, but can still work as intended. */
8844 warning_at (current_location,
8845 OPT_Wvarargs,
8846 "%<__builtin_next_arg%> called without an argument");
8847 return true;
8849 else if (nargs > 1)
8851 error ("wrong number of arguments to function %<__builtin_next_arg%>");
8852 return true;
8854 arg = CALL_EXPR_ARG (exp, 0);
8857 if (TREE_CODE (arg) == SSA_NAME)
8858 arg = SSA_NAME_VAR (arg);
8860 /* We destructively modify the call to be __builtin_va_start (ap, 0)
8861 or __builtin_next_arg (0) the first time we see it, after checking
8862 the arguments and if needed issuing a warning. */
8863 if (!integer_zerop (arg))
8865 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8867 /* Strip off all nops for the sake of the comparison. This
8868 is not quite the same as STRIP_NOPS. It does more.
8869 We must also strip off INDIRECT_EXPR for C++ reference
8870 parameters. */
8871 while (CONVERT_EXPR_P (arg)
8872 || TREE_CODE (arg) == INDIRECT_REF)
8873 arg = TREE_OPERAND (arg, 0);
8874 if (arg != last_parm)
8876 /* FIXME: Sometimes with the tree optimizers we can get the
8877 not the last argument even though the user used the last
8878 argument. We just warn and set the arg to be the last
8879 argument so that we will get wrong-code because of
8880 it. */
8881 warning_at (current_location,
8882 OPT_Wvarargs,
8883 "second parameter of %<va_start%> not last named argument");
8886 /* Undefined by C99 7.15.1.4p4 (va_start):
8887 "If the parameter parmN is declared with the register storage
8888 class, with a function or array type, or with a type that is
8889 not compatible with the type that results after application of
8890 the default argument promotions, the behavior is undefined."
8892 else if (DECL_REGISTER (arg))
8894 warning_at (current_location,
8895 OPT_Wvarargs,
8896 "undefined behaviour when second parameter of "
8897 "%<va_start%> is declared with %<register%> storage");
8900 /* We want to verify the second parameter just once before the tree
8901 optimizers are run and then avoid keeping it in the tree,
8902 as otherwise we could warn even for correct code like:
8903 void foo (int i, ...)
8904 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
8905 if (va_start_p)
8906 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
8907 else
8908 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
8910 return false;
8914 /* Expand a call EXP to __builtin_object_size. */
8916 static rtx
8917 expand_builtin_object_size (tree exp)
8919 tree ost;
8920 int object_size_type;
8921 tree fndecl = get_callee_fndecl (exp);
8923 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8925 error ("%Kfirst argument of %D must be a pointer, second integer constant",
8926 exp, fndecl);
8927 expand_builtin_trap ();
8928 return const0_rtx;
8931 ost = CALL_EXPR_ARG (exp, 1);
8932 STRIP_NOPS (ost);
8934 if (TREE_CODE (ost) != INTEGER_CST
8935 || tree_int_cst_sgn (ost) < 0
8936 || compare_tree_int (ost, 3) > 0)
8938 error ("%Klast argument of %D is not integer constant between 0 and 3",
8939 exp, fndecl);
8940 expand_builtin_trap ();
8941 return const0_rtx;
8944 object_size_type = tree_to_shwi (ost);
8946 return object_size_type < 2 ? constm1_rtx : const0_rtx;
8949 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
8950 FCODE is the BUILT_IN_* to use.
8951 Return NULL_RTX if we failed; the caller should emit a normal call,
8952 otherwise try to get the result in TARGET, if convenient (and in
8953 mode MODE if that's convenient). */
8955 static rtx
8956 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
8957 enum built_in_function fcode)
8959 tree dest, src, len, size;
8961 if (!validate_arglist (exp,
8962 POINTER_TYPE,
8963 fcode == BUILT_IN_MEMSET_CHK
8964 ? INTEGER_TYPE : POINTER_TYPE,
8965 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8966 return NULL_RTX;
8968 dest = CALL_EXPR_ARG (exp, 0);
8969 src = CALL_EXPR_ARG (exp, 1);
8970 len = CALL_EXPR_ARG (exp, 2);
8971 size = CALL_EXPR_ARG (exp, 3);
8973 if (! tree_fits_uhwi_p (size))
8974 return NULL_RTX;
8976 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
8978 tree fn;
8980 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
8982 warning_at (tree_nonartificial_location (exp),
8983 0, "%Kcall to %D will always overflow destination buffer",
8984 exp, get_callee_fndecl (exp));
8985 return NULL_RTX;
8988 fn = NULL_TREE;
8989 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
8990 mem{cpy,pcpy,move,set} is available. */
8991 switch (fcode)
8993 case BUILT_IN_MEMCPY_CHK:
8994 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
8995 break;
8996 case BUILT_IN_MEMPCPY_CHK:
8997 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
8998 break;
8999 case BUILT_IN_MEMMOVE_CHK:
9000 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9001 break;
9002 case BUILT_IN_MEMSET_CHK:
9003 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9004 break;
9005 default:
9006 break;
9009 if (! fn)
9010 return NULL_RTX;
9012 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9013 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9014 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9015 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9017 else if (fcode == BUILT_IN_MEMSET_CHK)
9018 return NULL_RTX;
9019 else
9021 unsigned int dest_align = get_pointer_alignment (dest);
9023 /* If DEST is not a pointer type, call the normal function. */
9024 if (dest_align == 0)
9025 return NULL_RTX;
9027 /* If SRC and DEST are the same (and not volatile), do nothing. */
9028 if (operand_equal_p (src, dest, 0))
9030 tree expr;
9032 if (fcode != BUILT_IN_MEMPCPY_CHK)
9034 /* Evaluate and ignore LEN in case it has side-effects. */
9035 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9036 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9039 expr = fold_build_pointer_plus (dest, len);
9040 return expand_expr (expr, target, mode, EXPAND_NORMAL);
9043 /* __memmove_chk special case. */
9044 if (fcode == BUILT_IN_MEMMOVE_CHK)
9046 unsigned int src_align = get_pointer_alignment (src);
9048 if (src_align == 0)
9049 return NULL_RTX;
9051 /* If src is categorized for a readonly section we can use
9052 normal __memcpy_chk. */
9053 if (readonly_data_expr (src))
9055 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
9056 if (!fn)
9057 return NULL_RTX;
9058 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
9059 dest, src, len, size);
9060 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9061 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9062 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9065 return NULL_RTX;
9069 /* Emit warning if a buffer overflow is detected at compile time. */
9071 static void
9072 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
9074 int is_strlen = 0;
9075 tree len, size;
9076 location_t loc = tree_nonartificial_location (exp);
9078 switch (fcode)
9080 case BUILT_IN_STRCPY_CHK:
9081 case BUILT_IN_STPCPY_CHK:
9082 /* For __strcat_chk the warning will be emitted only if overflowing
9083 by at least strlen (dest) + 1 bytes. */
9084 case BUILT_IN_STRCAT_CHK:
9085 len = CALL_EXPR_ARG (exp, 1);
9086 size = CALL_EXPR_ARG (exp, 2);
9087 is_strlen = 1;
9088 break;
9089 case BUILT_IN_STRNCAT_CHK:
9090 case BUILT_IN_STRNCPY_CHK:
9091 case BUILT_IN_STPNCPY_CHK:
9092 len = CALL_EXPR_ARG (exp, 2);
9093 size = CALL_EXPR_ARG (exp, 3);
9094 break;
9095 case BUILT_IN_SNPRINTF_CHK:
9096 case BUILT_IN_VSNPRINTF_CHK:
9097 len = CALL_EXPR_ARG (exp, 1);
9098 size = CALL_EXPR_ARG (exp, 3);
9099 break;
9100 default:
9101 gcc_unreachable ();
9104 if (!len || !size)
9105 return;
9107 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
9108 return;
9110 if (is_strlen)
9112 len = c_strlen (len, 1);
9113 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
9114 return;
9116 else if (fcode == BUILT_IN_STRNCAT_CHK)
9118 tree src = CALL_EXPR_ARG (exp, 1);
9119 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
9120 return;
9121 src = c_strlen (src, 1);
9122 if (! src || ! tree_fits_uhwi_p (src))
9124 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
9125 exp, get_callee_fndecl (exp));
9126 return;
9128 else if (tree_int_cst_lt (src, size))
9129 return;
9131 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
9132 return;
9134 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
9135 exp, get_callee_fndecl (exp));
9138 /* Emit warning if a buffer overflow is detected at compile time
9139 in __sprintf_chk/__vsprintf_chk calls. */
9141 static void
9142 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
9144 tree size, len, fmt;
9145 const char *fmt_str;
9146 int nargs = call_expr_nargs (exp);
9148 /* Verify the required arguments in the original call. */
9150 if (nargs < 4)
9151 return;
9152 size = CALL_EXPR_ARG (exp, 2);
9153 fmt = CALL_EXPR_ARG (exp, 3);
9155 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
9156 return;
9158 /* Check whether the format is a literal string constant. */
9159 fmt_str = c_getstr (fmt);
9160 if (fmt_str == NULL)
9161 return;
9163 if (!init_target_chars ())
9164 return;
9166 /* If the format doesn't contain % args or %%, we know its size. */
9167 if (strchr (fmt_str, target_percent) == 0)
9168 len = build_int_cstu (size_type_node, strlen (fmt_str));
9169 /* If the format is "%s" and first ... argument is a string literal,
9170 we know it too. */
9171 else if (fcode == BUILT_IN_SPRINTF_CHK
9172 && strcmp (fmt_str, target_percent_s) == 0)
9174 tree arg;
9176 if (nargs < 5)
9177 return;
9178 arg = CALL_EXPR_ARG (exp, 4);
9179 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
9180 return;
9182 len = c_strlen (arg, 1);
9183 if (!len || ! tree_fits_uhwi_p (len))
9184 return;
9186 else
9187 return;
9189 if (! tree_int_cst_lt (len, size))
9190 warning_at (tree_nonartificial_location (exp),
9191 0, "%Kcall to %D will always overflow destination buffer",
9192 exp, get_callee_fndecl (exp));
9195 /* Emit warning if a free is called with address of a variable. */
9197 static void
9198 maybe_emit_free_warning (tree exp)
9200 tree arg = CALL_EXPR_ARG (exp, 0);
9202 STRIP_NOPS (arg);
9203 if (TREE_CODE (arg) != ADDR_EXPR)
9204 return;
9206 arg = get_base_address (TREE_OPERAND (arg, 0));
9207 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
9208 return;
9210 if (SSA_VAR_P (arg))
9211 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9212 "%Kattempt to free a non-heap object %qD", exp, arg);
9213 else
9214 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9215 "%Kattempt to free a non-heap object", exp);
9218 /* Fold a call to __builtin_object_size with arguments PTR and OST,
9219 if possible. */
9221 static tree
9222 fold_builtin_object_size (tree ptr, tree ost)
9224 unsigned HOST_WIDE_INT bytes;
9225 int object_size_type;
9227 if (!validate_arg (ptr, POINTER_TYPE)
9228 || !validate_arg (ost, INTEGER_TYPE))
9229 return NULL_TREE;
9231 STRIP_NOPS (ost);
9233 if (TREE_CODE (ost) != INTEGER_CST
9234 || tree_int_cst_sgn (ost) < 0
9235 || compare_tree_int (ost, 3) > 0)
9236 return NULL_TREE;
9238 object_size_type = tree_to_shwi (ost);
9240 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
9241 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
9242 and (size_t) 0 for types 2 and 3. */
9243 if (TREE_SIDE_EFFECTS (ptr))
9244 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
9246 if (TREE_CODE (ptr) == ADDR_EXPR)
9248 bytes = compute_builtin_object_size (ptr, object_size_type);
9249 if (wi::fits_to_tree_p (bytes, size_type_node))
9250 return build_int_cstu (size_type_node, bytes);
9252 else if (TREE_CODE (ptr) == SSA_NAME)
9254 /* If object size is not known yet, delay folding until
9255 later. Maybe subsequent passes will help determining
9256 it. */
9257 bytes = compute_builtin_object_size (ptr, object_size_type);
9258 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
9259 && wi::fits_to_tree_p (bytes, size_type_node))
9260 return build_int_cstu (size_type_node, bytes);
9263 return NULL_TREE;
9266 /* Builtins with folding operations that operate on "..." arguments
9267 need special handling; we need to store the arguments in a convenient
9268 data structure before attempting any folding. Fortunately there are
9269 only a few builtins that fall into this category. FNDECL is the
9270 function, EXP is the CALL_EXPR for the call. */
9272 static tree
9273 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
9275 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9276 tree ret = NULL_TREE;
9278 switch (fcode)
9280 case BUILT_IN_FPCLASSIFY:
9281 ret = fold_builtin_fpclassify (loc, args, nargs);
9282 break;
9284 default:
9285 break;
9287 if (ret)
9289 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9290 SET_EXPR_LOCATION (ret, loc);
9291 TREE_NO_WARNING (ret) = 1;
9292 return ret;
9294 return NULL_TREE;
9297 /* Initialize format string characters in the target charset. */
9299 bool
9300 init_target_chars (void)
9302 static bool init;
9303 if (!init)
9305 target_newline = lang_hooks.to_target_charset ('\n');
9306 target_percent = lang_hooks.to_target_charset ('%');
9307 target_c = lang_hooks.to_target_charset ('c');
9308 target_s = lang_hooks.to_target_charset ('s');
9309 if (target_newline == 0 || target_percent == 0 || target_c == 0
9310 || target_s == 0)
9311 return false;
9313 target_percent_c[0] = target_percent;
9314 target_percent_c[1] = target_c;
9315 target_percent_c[2] = '\0';
9317 target_percent_s[0] = target_percent;
9318 target_percent_s[1] = target_s;
9319 target_percent_s[2] = '\0';
9321 target_percent_s_newline[0] = target_percent;
9322 target_percent_s_newline[1] = target_s;
9323 target_percent_s_newline[2] = target_newline;
9324 target_percent_s_newline[3] = '\0';
9326 init = true;
9328 return true;
9331 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
9332 and no overflow/underflow occurred. INEXACT is true if M was not
9333 exactly calculated. TYPE is the tree type for the result. This
9334 function assumes that you cleared the MPFR flags and then
9335 calculated M to see if anything subsequently set a flag prior to
9336 entering this function. Return NULL_TREE if any checks fail. */
9338 static tree
9339 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
9341 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9342 overflow/underflow occurred. If -frounding-math, proceed iff the
9343 result of calling FUNC was exact. */
9344 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
9345 && (!flag_rounding_math || !inexact))
9347 REAL_VALUE_TYPE rr;
9349 real_from_mpfr (&rr, m, type, GMP_RNDN);
9350 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
9351 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9352 but the mpft_t is not, then we underflowed in the
9353 conversion. */
9354 if (real_isfinite (&rr)
9355 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
9357 REAL_VALUE_TYPE rmode;
9359 real_convert (&rmode, TYPE_MODE (type), &rr);
9360 /* Proceed iff the specified mode can hold the value. */
9361 if (real_identical (&rmode, &rr))
9362 return build_real (type, rmode);
9365 return NULL_TREE;
9368 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
9369 number and no overflow/underflow occurred. INEXACT is true if M
9370 was not exactly calculated. TYPE is the tree type for the result.
9371 This function assumes that you cleared the MPFR flags and then
9372 calculated M to see if anything subsequently set a flag prior to
9373 entering this function. Return NULL_TREE if any checks fail, if
9374 FORCE_CONVERT is true, then bypass the checks. */
9376 static tree
9377 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
9379 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9380 overflow/underflow occurred. If -frounding-math, proceed iff the
9381 result of calling FUNC was exact. */
9382 if (force_convert
9383 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
9384 && !mpfr_overflow_p () && !mpfr_underflow_p ()
9385 && (!flag_rounding_math || !inexact)))
9387 REAL_VALUE_TYPE re, im;
9389 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
9390 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
9391 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
9392 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9393 but the mpft_t is not, then we underflowed in the
9394 conversion. */
9395 if (force_convert
9396 || (real_isfinite (&re) && real_isfinite (&im)
9397 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
9398 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
9400 REAL_VALUE_TYPE re_mode, im_mode;
9402 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
9403 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
9404 /* Proceed iff the specified mode can hold the value. */
9405 if (force_convert
9406 || (real_identical (&re_mode, &re)
9407 && real_identical (&im_mode, &im)))
9408 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
9409 build_real (TREE_TYPE (type), im_mode));
9412 return NULL_TREE;
9415 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
9416 the pointer *(ARG_QUO) and return the result. The type is taken
9417 from the type of ARG0 and is used for setting the precision of the
9418 calculation and results. */
9420 static tree
9421 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
9423 tree const type = TREE_TYPE (arg0);
9424 tree result = NULL_TREE;
9426 STRIP_NOPS (arg0);
9427 STRIP_NOPS (arg1);
9429 /* To proceed, MPFR must exactly represent the target floating point
9430 format, which only happens when the target base equals two. */
9431 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
9432 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9433 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
9435 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
9436 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
9438 if (real_isfinite (ra0) && real_isfinite (ra1))
9440 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
9441 const int prec = fmt->p;
9442 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
9443 tree result_rem;
9444 long integer_quo;
9445 mpfr_t m0, m1;
9447 mpfr_inits2 (prec, m0, m1, NULL);
9448 mpfr_from_real (m0, ra0, GMP_RNDN);
9449 mpfr_from_real (m1, ra1, GMP_RNDN);
9450 mpfr_clear_flags ();
9451 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
9452 /* Remquo is independent of the rounding mode, so pass
9453 inexact=0 to do_mpfr_ckconv(). */
9454 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
9455 mpfr_clears (m0, m1, NULL);
9456 if (result_rem)
9458 /* MPFR calculates quo in the host's long so it may
9459 return more bits in quo than the target int can hold
9460 if sizeof(host long) > sizeof(target int). This can
9461 happen even for native compilers in LP64 mode. In
9462 these cases, modulo the quo value with the largest
9463 number that the target int can hold while leaving one
9464 bit for the sign. */
9465 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
9466 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
9468 /* Dereference the quo pointer argument. */
9469 arg_quo = build_fold_indirect_ref (arg_quo);
9470 /* Proceed iff a valid pointer type was passed in. */
9471 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
9473 /* Set the value. */
9474 tree result_quo
9475 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
9476 build_int_cst (TREE_TYPE (arg_quo),
9477 integer_quo));
9478 TREE_SIDE_EFFECTS (result_quo) = 1;
9479 /* Combine the quo assignment with the rem. */
9480 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
9481 result_quo, result_rem));
9486 return result;
9489 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
9490 resulting value as a tree with type TYPE. The mpfr precision is
9491 set to the precision of TYPE. We assume that this mpfr function
9492 returns zero if the result could be calculated exactly within the
9493 requested precision. In addition, the integer pointer represented
9494 by ARG_SG will be dereferenced and set to the appropriate signgam
9495 (-1,1) value. */
9497 static tree
9498 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
9500 tree result = NULL_TREE;
9502 STRIP_NOPS (arg);
9504 /* To proceed, MPFR must exactly represent the target floating point
9505 format, which only happens when the target base equals two. Also
9506 verify ARG is a constant and that ARG_SG is an int pointer. */
9507 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
9508 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
9509 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
9510 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
9512 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
9514 /* In addition to NaN and Inf, the argument cannot be zero or a
9515 negative integer. */
9516 if (real_isfinite (ra)
9517 && ra->cl != rvc_zero
9518 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
9520 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
9521 const int prec = fmt->p;
9522 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
9523 int inexact, sg;
9524 mpfr_t m;
9525 tree result_lg;
9527 mpfr_init2 (m, prec);
9528 mpfr_from_real (m, ra, GMP_RNDN);
9529 mpfr_clear_flags ();
9530 inexact = mpfr_lgamma (m, &sg, m, rnd);
9531 result_lg = do_mpfr_ckconv (m, type, inexact);
9532 mpfr_clear (m);
9533 if (result_lg)
9535 tree result_sg;
9537 /* Dereference the arg_sg pointer argument. */
9538 arg_sg = build_fold_indirect_ref (arg_sg);
9539 /* Assign the signgam value into *arg_sg. */
9540 result_sg = fold_build2 (MODIFY_EXPR,
9541 TREE_TYPE (arg_sg), arg_sg,
9542 build_int_cst (TREE_TYPE (arg_sg), sg));
9543 TREE_SIDE_EFFECTS (result_sg) = 1;
9544 /* Combine the signgam assignment with the lgamma result. */
9545 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
9546 result_sg, result_lg));
9551 return result;
9554 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
9555 mpc function FUNC on it and return the resulting value as a tree
9556 with type TYPE. The mpfr precision is set to the precision of
9557 TYPE. We assume that function FUNC returns zero if the result
9558 could be calculated exactly within the requested precision. If
9559 DO_NONFINITE is true, then fold expressions containing Inf or NaN
9560 in the arguments and/or results. */
9562 tree
9563 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
9564 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
9566 tree result = NULL_TREE;
9568 STRIP_NOPS (arg0);
9569 STRIP_NOPS (arg1);
9571 /* To proceed, MPFR must exactly represent the target floating point
9572 format, which only happens when the target base equals two. */
9573 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
9574 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
9575 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
9576 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
9577 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
9579 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
9580 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
9581 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
9582 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
9584 if (do_nonfinite
9585 || (real_isfinite (re0) && real_isfinite (im0)
9586 && real_isfinite (re1) && real_isfinite (im1)))
9588 const struct real_format *const fmt =
9589 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
9590 const int prec = fmt->p;
9591 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
9592 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
9593 int inexact;
9594 mpc_t m0, m1;
9596 mpc_init2 (m0, prec);
9597 mpc_init2 (m1, prec);
9598 mpfr_from_real (mpc_realref (m0), re0, rnd);
9599 mpfr_from_real (mpc_imagref (m0), im0, rnd);
9600 mpfr_from_real (mpc_realref (m1), re1, rnd);
9601 mpfr_from_real (mpc_imagref (m1), im1, rnd);
9602 mpfr_clear_flags ();
9603 inexact = func (m0, m0, m1, crnd);
9604 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
9605 mpc_clear (m0);
9606 mpc_clear (m1);
9610 return result;
9613 /* A wrapper function for builtin folding that prevents warnings for
9614 "statement without effect" and the like, caused by removing the
9615 call node earlier than the warning is generated. */
9617 tree
9618 fold_call_stmt (gcall *stmt, bool ignore)
9620 tree ret = NULL_TREE;
9621 tree fndecl = gimple_call_fndecl (stmt);
9622 location_t loc = gimple_location (stmt);
9623 if (fndecl
9624 && TREE_CODE (fndecl) == FUNCTION_DECL
9625 && DECL_BUILT_IN (fndecl)
9626 && !gimple_call_va_arg_pack_p (stmt))
9628 int nargs = gimple_call_num_args (stmt);
9629 tree *args = (nargs > 0
9630 ? gimple_call_arg_ptr (stmt, 0)
9631 : &error_mark_node);
9633 if (avoid_folding_inline_builtin (fndecl))
9634 return NULL_TREE;
9635 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9637 return targetm.fold_builtin (fndecl, nargs, args, ignore);
9639 else
9641 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9642 if (ret)
9644 /* Propagate location information from original call to
9645 expansion of builtin. Otherwise things like
9646 maybe_emit_chk_warning, that operate on the expansion
9647 of a builtin, will use the wrong location information. */
9648 if (gimple_has_location (stmt))
9650 tree realret = ret;
9651 if (TREE_CODE (ret) == NOP_EXPR)
9652 realret = TREE_OPERAND (ret, 0);
9653 if (CAN_HAVE_LOCATION_P (realret)
9654 && !EXPR_HAS_LOCATION (realret))
9655 SET_EXPR_LOCATION (realret, loc);
9656 return realret;
9658 return ret;
9662 return NULL_TREE;
9665 /* Look up the function in builtin_decl that corresponds to DECL
9666 and set ASMSPEC as its user assembler name. DECL must be a
9667 function decl that declares a builtin. */
9669 void
9670 set_builtin_user_assembler_name (tree decl, const char *asmspec)
9672 tree builtin;
9673 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
9674 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
9675 && asmspec != 0);
9677 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
9678 set_user_assembler_name (builtin, asmspec);
9679 switch (DECL_FUNCTION_CODE (decl))
9681 case BUILT_IN_MEMCPY:
9682 init_block_move_fn (asmspec);
9683 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
9684 break;
9685 case BUILT_IN_MEMSET:
9686 init_block_clear_fn (asmspec);
9687 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
9688 break;
9689 case BUILT_IN_MEMMOVE:
9690 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
9691 break;
9692 case BUILT_IN_MEMCMP:
9693 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
9694 break;
9695 case BUILT_IN_ABORT:
9696 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
9697 break;
9698 case BUILT_IN_FFS:
9699 if (INT_TYPE_SIZE < BITS_PER_WORD)
9701 set_user_assembler_libfunc ("ffs", asmspec);
9702 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
9703 MODE_INT, 0), "ffs");
9705 break;
9706 default:
9707 break;
9711 /* Return true if DECL is a builtin that expands to a constant or similarly
9712 simple code. */
9713 bool
9714 is_simple_builtin (tree decl)
9716 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
9717 switch (DECL_FUNCTION_CODE (decl))
9719 /* Builtins that expand to constants. */
9720 case BUILT_IN_CONSTANT_P:
9721 case BUILT_IN_EXPECT:
9722 case BUILT_IN_OBJECT_SIZE:
9723 case BUILT_IN_UNREACHABLE:
9724 /* Simple register moves or loads from stack. */
9725 case BUILT_IN_ASSUME_ALIGNED:
9726 case BUILT_IN_RETURN_ADDRESS:
9727 case BUILT_IN_EXTRACT_RETURN_ADDR:
9728 case BUILT_IN_FROB_RETURN_ADDR:
9729 case BUILT_IN_RETURN:
9730 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
9731 case BUILT_IN_FRAME_ADDRESS:
9732 case BUILT_IN_VA_END:
9733 case BUILT_IN_STACK_SAVE:
9734 case BUILT_IN_STACK_RESTORE:
9735 /* Exception state returns or moves registers around. */
9736 case BUILT_IN_EH_FILTER:
9737 case BUILT_IN_EH_POINTER:
9738 case BUILT_IN_EH_COPY_VALUES:
9739 return true;
9741 default:
9742 return false;
9745 return false;
9748 /* Return true if DECL is a builtin that is not expensive, i.e., they are
9749 most probably expanded inline into reasonably simple code. This is a
9750 superset of is_simple_builtin. */
9751 bool
9752 is_inexpensive_builtin (tree decl)
9754 if (!decl)
9755 return false;
9756 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
9757 return true;
9758 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
9759 switch (DECL_FUNCTION_CODE (decl))
9761 case BUILT_IN_ABS:
9762 case BUILT_IN_ALLOCA:
9763 case BUILT_IN_ALLOCA_WITH_ALIGN:
9764 case BUILT_IN_BSWAP16:
9765 case BUILT_IN_BSWAP32:
9766 case BUILT_IN_BSWAP64:
9767 case BUILT_IN_CLZ:
9768 case BUILT_IN_CLZIMAX:
9769 case BUILT_IN_CLZL:
9770 case BUILT_IN_CLZLL:
9771 case BUILT_IN_CTZ:
9772 case BUILT_IN_CTZIMAX:
9773 case BUILT_IN_CTZL:
9774 case BUILT_IN_CTZLL:
9775 case BUILT_IN_FFS:
9776 case BUILT_IN_FFSIMAX:
9777 case BUILT_IN_FFSL:
9778 case BUILT_IN_FFSLL:
9779 case BUILT_IN_IMAXABS:
9780 case BUILT_IN_FINITE:
9781 case BUILT_IN_FINITEF:
9782 case BUILT_IN_FINITEL:
9783 case BUILT_IN_FINITED32:
9784 case BUILT_IN_FINITED64:
9785 case BUILT_IN_FINITED128:
9786 case BUILT_IN_FPCLASSIFY:
9787 case BUILT_IN_ISFINITE:
9788 case BUILT_IN_ISINF_SIGN:
9789 case BUILT_IN_ISINF:
9790 case BUILT_IN_ISINFF:
9791 case BUILT_IN_ISINFL:
9792 case BUILT_IN_ISINFD32:
9793 case BUILT_IN_ISINFD64:
9794 case BUILT_IN_ISINFD128:
9795 case BUILT_IN_ISNAN:
9796 case BUILT_IN_ISNANF:
9797 case BUILT_IN_ISNANL:
9798 case BUILT_IN_ISNAND32:
9799 case BUILT_IN_ISNAND64:
9800 case BUILT_IN_ISNAND128:
9801 case BUILT_IN_ISNORMAL:
9802 case BUILT_IN_ISGREATER:
9803 case BUILT_IN_ISGREATEREQUAL:
9804 case BUILT_IN_ISLESS:
9805 case BUILT_IN_ISLESSEQUAL:
9806 case BUILT_IN_ISLESSGREATER:
9807 case BUILT_IN_ISUNORDERED:
9808 case BUILT_IN_VA_ARG_PACK:
9809 case BUILT_IN_VA_ARG_PACK_LEN:
9810 case BUILT_IN_VA_COPY:
9811 case BUILT_IN_TRAP:
9812 case BUILT_IN_SAVEREGS:
9813 case BUILT_IN_POPCOUNTL:
9814 case BUILT_IN_POPCOUNTLL:
9815 case BUILT_IN_POPCOUNTIMAX:
9816 case BUILT_IN_POPCOUNT:
9817 case BUILT_IN_PARITYL:
9818 case BUILT_IN_PARITYLL:
9819 case BUILT_IN_PARITYIMAX:
9820 case BUILT_IN_PARITY:
9821 case BUILT_IN_LABS:
9822 case BUILT_IN_LLABS:
9823 case BUILT_IN_PREFETCH:
9824 case BUILT_IN_ACC_ON_DEVICE:
9825 return true;
9827 default:
9828 return is_simple_builtin (decl);
9831 return false;