2011-12-09 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / builtins.c
blobb00749848a61657e3f81cddd7b700de97a90fb67
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
52 #include "builtins.h"
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 #endif
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
60 struct target_builtins default_target_builtins;
61 #if SWITCHABLE_TARGET
62 struct target_builtins *this_target_builtins = &default_target_builtins;
63 #endif
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
72 #include "builtins.def"
74 #undef DEF_BUILTIN
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 builtin_info_type builtin_info;
80 static const char *c_getstr (tree);
81 static rtx c_readstr (const char *, enum machine_mode);
82 static int target_char_cast (tree, char *);
83 static rtx get_memory_rtx (tree, tree);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector (int, rtx);
88 #endif
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_next_arg (void);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx);
116 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
118 enum machine_mode, int);
119 static rtx expand_builtin_strcpy (tree, rtx);
120 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
121 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, bool);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static tree stabilize_va_list_loc (location_t, tree, int);
132 static rtx expand_builtin_expect (tree, rtx);
133 static tree fold_builtin_constant_p (tree);
134 static tree fold_builtin_expect (location_t, tree, tree);
135 static tree fold_builtin_classify_type (tree);
136 static tree fold_builtin_strlen (location_t, tree, tree);
137 static tree fold_builtin_inf (location_t, tree, int);
138 static tree fold_builtin_nan (tree, tree, int);
139 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
140 static bool validate_arg (const_tree, enum tree_code code);
141 static bool integer_valued_real_p (tree);
142 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
143 static bool readonly_data_expr (tree);
144 static rtx expand_builtin_fabs (tree, rtx, rtx);
145 static rtx expand_builtin_signbit (tree, rtx);
146 static tree fold_builtin_sqrt (location_t, tree, tree);
147 static tree fold_builtin_cbrt (location_t, tree, tree);
148 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_cos (location_t, tree, tree, tree);
151 static tree fold_builtin_cosh (location_t, tree, tree, tree);
152 static tree fold_builtin_tan (tree, tree);
153 static tree fold_builtin_trunc (location_t, tree, tree);
154 static tree fold_builtin_floor (location_t, tree, tree);
155 static tree fold_builtin_ceil (location_t, tree, tree);
156 static tree fold_builtin_round (location_t, tree, tree);
157 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
158 static tree fold_builtin_bitop (tree, tree);
159 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160 static tree fold_builtin_strchr (location_t, tree, tree, tree);
161 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163 static tree fold_builtin_strcmp (location_t, tree, tree);
164 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165 static tree fold_builtin_signbit (location_t, tree, tree);
166 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173 enum tree_code);
174 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175 static tree fold_builtin_0 (location_t, tree, bool);
176 static tree fold_builtin_1 (location_t, tree, tree, bool);
177 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180 static tree fold_builtin_varargs (location_t, tree, tree, bool);
182 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183 static tree fold_builtin_strstr (location_t, tree, tree, tree);
184 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185 static tree fold_builtin_strcat (location_t, tree, tree);
186 static tree fold_builtin_strncat (location_t, tree, tree, tree);
187 static tree fold_builtin_strspn (location_t, tree, tree);
188 static tree fold_builtin_strcspn (location_t, tree, tree);
189 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
190 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
192 static rtx expand_builtin_object_size (tree);
193 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194 enum built_in_function);
195 static void maybe_emit_chk_warning (tree, enum built_in_function);
196 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_free_warning (tree);
198 static tree fold_builtin_object_size (tree, tree);
199 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
204 enum built_in_function);
205 static bool init_target_chars (void);
207 static unsigned HOST_WIDE_INT target_newline;
208 static unsigned HOST_WIDE_INT target_percent;
209 static unsigned HOST_WIDE_INT target_c;
210 static unsigned HOST_WIDE_INT target_s;
211 static char target_percent_c[3];
212 static char target_percent_s[3];
213 static char target_percent_s_newline[4];
214 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
216 static tree do_mpfr_arg2 (tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_arg3 (tree, tree, tree, tree,
219 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
220 static tree do_mpfr_sincos (tree, tree, tree);
221 static tree do_mpfr_bessel_n (tree, tree, tree,
222 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_remquo (tree, tree, tree);
225 static tree do_mpfr_lgamma_r (tree, tree, tree);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
230 static bool
231 is_builtin_name (const char *name)
233 if (strncmp (name, "__builtin_", 10) == 0)
234 return true;
235 if (strncmp (name, "__sync_", 7) == 0)
236 return true;
237 if (strncmp (name, "__atomic_", 9) == 0)
238 return true;
239 return false;
243 /* Return true if DECL is a function symbol representing a built-in. */
245 bool
246 is_builtin_fn (tree decl)
248 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
252 /* Return true if NODE should be considered for inline expansion regardless
253 of the optimization level. This means whenever a function is invoked with
254 its "internal" name, which normally contains the prefix "__builtin". */
256 static bool
257 called_as_built_in (tree node)
259 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260 we want the name used to call the function, not the name it
261 will have. */
262 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
263 return is_builtin_name (name);
266 /* Compute values M and N such that M divides (address of EXP - N) and
267 such that N < M. Store N in *BITPOSP and return M.
269 Note that the address (and thus the alignment) computed here is based
270 on the address to which a symbol resolves, whereas DECL_ALIGN is based
271 on the address at which an object is actually located. These two
272 addresses are not always the same. For example, on ARM targets,
273 the address &foo of a Thumb function foo() has the lowest bit set,
274 whereas foo() itself starts on an even address. */
276 unsigned int
277 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
279 HOST_WIDE_INT bitsize, bitpos;
280 tree offset;
281 enum machine_mode mode;
282 int unsignedp, volatilep;
283 unsigned int align, inner;
285 /* Get the innermost object and the constant (bitpos) and possibly
286 variable (offset) offset of the access. */
287 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
288 &mode, &unsignedp, &volatilep, true);
290 /* Extract alignment information from the innermost object and
291 possibly adjust bitpos and offset. */
292 if (TREE_CODE (exp) == CONST_DECL)
293 exp = DECL_INITIAL (exp);
294 if (DECL_P (exp)
295 && TREE_CODE (exp) != LABEL_DECL)
297 if (TREE_CODE (exp) == FUNCTION_DECL)
299 /* Function addresses can encode extra information besides their
300 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
301 allows the low bit to be used as a virtual bit, we know
302 that the address itself must be 2-byte aligned. */
303 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
304 align = 2 * BITS_PER_UNIT;
305 else
306 align = BITS_PER_UNIT;
308 else
309 align = DECL_ALIGN (exp);
311 else if (CONSTANT_CLASS_P (exp))
313 align = TYPE_ALIGN (TREE_TYPE (exp));
314 #ifdef CONSTANT_ALIGNMENT
315 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
316 #endif
318 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
319 align = TYPE_ALIGN (TREE_TYPE (exp));
320 else if (TREE_CODE (exp) == INDIRECT_REF)
321 align = TYPE_ALIGN (TREE_TYPE (exp));
322 else if (TREE_CODE (exp) == MEM_REF)
324 tree addr = TREE_OPERAND (exp, 0);
325 struct ptr_info_def *pi;
326 if (TREE_CODE (addr) == BIT_AND_EXPR
327 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
329 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
330 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
331 align *= BITS_PER_UNIT;
332 addr = TREE_OPERAND (addr, 0);
334 else
335 align = BITS_PER_UNIT;
336 if (TREE_CODE (addr) == SSA_NAME
337 && (pi = SSA_NAME_PTR_INFO (addr)))
339 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
340 align = MAX (pi->align * BITS_PER_UNIT, align);
342 else if (TREE_CODE (addr) == ADDR_EXPR)
343 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
344 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
346 else if (TREE_CODE (exp) == TARGET_MEM_REF)
348 struct ptr_info_def *pi;
349 tree addr = TMR_BASE (exp);
350 if (TREE_CODE (addr) == BIT_AND_EXPR
351 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
353 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
354 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
355 align *= BITS_PER_UNIT;
356 addr = TREE_OPERAND (addr, 0);
358 else
359 align = BITS_PER_UNIT;
360 if (TREE_CODE (addr) == SSA_NAME
361 && (pi = SSA_NAME_PTR_INFO (addr)))
363 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
364 align = MAX (pi->align * BITS_PER_UNIT, align);
366 else if (TREE_CODE (addr) == ADDR_EXPR)
367 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
368 if (TMR_OFFSET (exp))
369 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
370 if (TMR_INDEX (exp) && TMR_STEP (exp))
372 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
373 align = MIN (align, (step & -step) * BITS_PER_UNIT);
375 else if (TMR_INDEX (exp))
376 align = BITS_PER_UNIT;
377 if (TMR_INDEX2 (exp))
378 align = BITS_PER_UNIT;
380 else
381 align = BITS_PER_UNIT;
383 /* If there is a non-constant offset part extract the maximum
384 alignment that can prevail. */
385 inner = ~0U;
386 while (offset)
388 tree next_offset;
390 if (TREE_CODE (offset) == PLUS_EXPR)
392 next_offset = TREE_OPERAND (offset, 0);
393 offset = TREE_OPERAND (offset, 1);
395 else
396 next_offset = NULL;
397 if (host_integerp (offset, 1))
399 /* Any overflow in calculating offset_bits won't change
400 the alignment. */
401 unsigned offset_bits
402 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
404 if (offset_bits)
405 inner = MIN (inner, (offset_bits & -offset_bits));
407 else if (TREE_CODE (offset) == MULT_EXPR
408 && host_integerp (TREE_OPERAND (offset, 1), 1))
410 /* Any overflow in calculating offset_factor won't change
411 the alignment. */
412 unsigned offset_factor
413 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
414 * BITS_PER_UNIT);
416 if (offset_factor)
417 inner = MIN (inner, (offset_factor & -offset_factor));
419 else
421 inner = MIN (inner, BITS_PER_UNIT);
422 break;
424 offset = next_offset;
427 /* Alignment is innermost object alignment adjusted by the constant
428 and non-constant offset parts. */
429 align = MIN (align, inner);
430 bitpos = bitpos & (align - 1);
432 *bitposp = bitpos;
433 return align;
436 /* Return the alignment in bits of EXP, an object. */
438 unsigned int
439 get_object_alignment (tree exp)
441 unsigned HOST_WIDE_INT bitpos = 0;
442 unsigned int align;
444 align = get_object_alignment_1 (exp, &bitpos);
446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
449 if (bitpos != 0)
450 align = (bitpos & -bitpos);
452 return align;
455 /* Return the alignment of object EXP, also considering its type when we do
456 not know of explicit misalignment. Only handle MEM_REF and TARGET_MEM_REF.
458 ??? Note that, in the general case, the type of an expression is not kept
459 consistent with misalignment information by the front-end, for example when
460 taking the address of a member of a packed structure. However, in most of
461 the cases, expressions have the alignment of their type so we optimistically
462 fall back to this alignment when we cannot compute a misalignment. */
464 unsigned int
465 get_object_or_type_alignment (tree exp)
467 unsigned HOST_WIDE_INT misalign;
468 unsigned int align = get_object_alignment_1 (exp, &misalign);
470 gcc_assert (TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == TARGET_MEM_REF);
472 if (misalign != 0)
473 align = (misalign & -misalign);
474 else
475 align = MAX (TYPE_ALIGN (TREE_TYPE (exp)), align);
477 return align;
480 /* Return the alignment in bits of EXP, a pointer valued expression.
481 The alignment returned is, by default, the alignment of the thing that
482 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
484 Otherwise, look at the expression to see if we can do better, i.e., if the
485 expression is actually pointing at an object whose alignment is tighter. */
487 unsigned int
488 get_pointer_alignment (tree exp)
490 STRIP_NOPS (exp);
492 if (TREE_CODE (exp) == ADDR_EXPR)
493 return get_object_alignment (TREE_OPERAND (exp, 0));
494 else if (TREE_CODE (exp) == SSA_NAME
495 && POINTER_TYPE_P (TREE_TYPE (exp)))
497 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
498 unsigned align;
499 if (!pi)
500 return BITS_PER_UNIT;
501 if (pi->misalign != 0)
502 align = (pi->misalign & -pi->misalign);
503 else
504 align = pi->align;
505 return align * BITS_PER_UNIT;
508 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
511 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
512 way, because it could contain a zero byte in the middle.
513 TREE_STRING_LENGTH is the size of the character array, not the string.
515 ONLY_VALUE should be nonzero if the result is not going to be emitted
516 into the instruction stream and zero if it is going to be expanded.
517 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
518 is returned, otherwise NULL, since
519 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
520 evaluate the side-effects.
522 The value returned is of type `ssizetype'.
524 Unfortunately, string_constant can't access the values of const char
525 arrays with initializers, so neither can we do so here. */
527 tree
528 c_strlen (tree src, int only_value)
530 tree offset_node;
531 HOST_WIDE_INT offset;
532 int max;
533 const char *ptr;
534 location_t loc;
536 STRIP_NOPS (src);
537 if (TREE_CODE (src) == COND_EXPR
538 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
540 tree len1, len2;
542 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
543 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
544 if (tree_int_cst_equal (len1, len2))
545 return len1;
548 if (TREE_CODE (src) == COMPOUND_EXPR
549 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
550 return c_strlen (TREE_OPERAND (src, 1), only_value);
552 loc = EXPR_LOC_OR_HERE (src);
554 src = string_constant (src, &offset_node);
555 if (src == 0)
556 return NULL_TREE;
558 max = TREE_STRING_LENGTH (src) - 1;
559 ptr = TREE_STRING_POINTER (src);
561 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
563 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
564 compute the offset to the following null if we don't know where to
565 start searching for it. */
566 int i;
568 for (i = 0; i < max; i++)
569 if (ptr[i] == 0)
570 return NULL_TREE;
572 /* We don't know the starting offset, but we do know that the string
573 has no internal zero bytes. We can assume that the offset falls
574 within the bounds of the string; otherwise, the programmer deserves
575 what he gets. Subtract the offset from the length of the string,
576 and return that. This would perhaps not be valid if we were dealing
577 with named arrays in addition to literal string constants. */
579 return size_diffop_loc (loc, size_int (max), offset_node);
582 /* We have a known offset into the string. Start searching there for
583 a null character if we can represent it as a single HOST_WIDE_INT. */
584 if (offset_node == 0)
585 offset = 0;
586 else if (! host_integerp (offset_node, 0))
587 offset = -1;
588 else
589 offset = tree_low_cst (offset_node, 0);
591 /* If the offset is known to be out of bounds, warn, and call strlen at
592 runtime. */
593 if (offset < 0 || offset > max)
595 /* Suppress multiple warnings for propagated constant strings. */
596 if (! TREE_NO_WARNING (src))
598 warning_at (loc, 0, "offset outside bounds of constant string");
599 TREE_NO_WARNING (src) = 1;
601 return NULL_TREE;
604 /* Use strlen to search for the first zero byte. Since any strings
605 constructed with build_string will have nulls appended, we win even
606 if we get handed something like (char[4])"abcd".
608 Since OFFSET is our starting index into the string, no further
609 calculation is needed. */
610 return ssize_int (strlen (ptr + offset));
613 /* Return a char pointer for a C string if it is a string constant
614 or sum of string constant and integer constant. */
616 static const char *
617 c_getstr (tree src)
619 tree offset_node;
621 src = string_constant (src, &offset_node);
622 if (src == 0)
623 return 0;
625 if (offset_node == 0)
626 return TREE_STRING_POINTER (src);
627 else if (!host_integerp (offset_node, 1)
628 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
629 return 0;
631 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
634 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
635 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
637 static rtx
638 c_readstr (const char *str, enum machine_mode mode)
640 HOST_WIDE_INT c[2];
641 HOST_WIDE_INT ch;
642 unsigned int i, j;
644 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
646 c[0] = 0;
647 c[1] = 0;
648 ch = 1;
649 for (i = 0; i < GET_MODE_SIZE (mode); i++)
651 j = i;
652 if (WORDS_BIG_ENDIAN)
653 j = GET_MODE_SIZE (mode) - i - 1;
654 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
655 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
656 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
657 j *= BITS_PER_UNIT;
658 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
660 if (ch)
661 ch = (unsigned char) str[i];
662 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
664 return immed_double_const (c[0], c[1], mode);
667 /* Cast a target constant CST to target CHAR and if that value fits into
668 host char type, return zero and put that value into variable pointed to by
669 P. */
671 static int
672 target_char_cast (tree cst, char *p)
674 unsigned HOST_WIDE_INT val, hostval;
676 if (TREE_CODE (cst) != INTEGER_CST
677 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
678 return 1;
680 val = TREE_INT_CST_LOW (cst);
681 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
682 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
684 hostval = val;
685 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
686 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
688 if (val != hostval)
689 return 1;
691 *p = hostval;
692 return 0;
695 /* Similar to save_expr, but assumes that arbitrary code is not executed
696 in between the multiple evaluations. In particular, we assume that a
697 non-addressable local variable will not be modified. */
699 static tree
700 builtin_save_expr (tree exp)
702 if (TREE_CODE (exp) == SSA_NAME
703 || (TREE_ADDRESSABLE (exp) == 0
704 && (TREE_CODE (exp) == PARM_DECL
705 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
706 return exp;
708 return save_expr (exp);
711 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
712 times to get the address of either a higher stack frame, or a return
713 address located within it (depending on FNDECL_CODE). */
715 static rtx
716 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
718 int i;
720 #ifdef INITIAL_FRAME_ADDRESS_RTX
721 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
722 #else
723 rtx tem;
725 /* For a zero count with __builtin_return_address, we don't care what
726 frame address we return, because target-specific definitions will
727 override us. Therefore frame pointer elimination is OK, and using
728 the soft frame pointer is OK.
730 For a nonzero count, or a zero count with __builtin_frame_address,
731 we require a stable offset from the current frame pointer to the
732 previous one, so we must use the hard frame pointer, and
733 we must disable frame pointer elimination. */
734 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
735 tem = frame_pointer_rtx;
736 else
738 tem = hard_frame_pointer_rtx;
740 /* Tell reload not to eliminate the frame pointer. */
741 crtl->accesses_prior_frames = 1;
743 #endif
745 /* Some machines need special handling before we can access
746 arbitrary frames. For example, on the SPARC, we must first flush
747 all register windows to the stack. */
748 #ifdef SETUP_FRAME_ADDRESSES
749 if (count > 0)
750 SETUP_FRAME_ADDRESSES ();
751 #endif
753 /* On the SPARC, the return address is not in the frame, it is in a
754 register. There is no way to access it off of the current frame
755 pointer, but it can be accessed off the previous frame pointer by
756 reading the value from the register window save area. */
757 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
758 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
759 count--;
760 #endif
762 /* Scan back COUNT frames to the specified frame. */
763 for (i = 0; i < count; i++)
765 /* Assume the dynamic chain pointer is in the word that the
766 frame address points to, unless otherwise specified. */
767 #ifdef DYNAMIC_CHAIN_ADDRESS
768 tem = DYNAMIC_CHAIN_ADDRESS (tem);
769 #endif
770 tem = memory_address (Pmode, tem);
771 tem = gen_frame_mem (Pmode, tem);
772 tem = copy_to_reg (tem);
775 /* For __builtin_frame_address, return what we've got. But, on
776 the SPARC for example, we may have to add a bias. */
777 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
778 #ifdef FRAME_ADDR_RTX
779 return FRAME_ADDR_RTX (tem);
780 #else
781 return tem;
782 #endif
784 /* For __builtin_return_address, get the return address from that frame. */
785 #ifdef RETURN_ADDR_RTX
786 tem = RETURN_ADDR_RTX (count, tem);
787 #else
788 tem = memory_address (Pmode,
789 plus_constant (tem, GET_MODE_SIZE (Pmode)));
790 tem = gen_frame_mem (Pmode, tem);
791 #endif
792 return tem;
795 /* Alias set used for setjmp buffer. */
796 static alias_set_type setjmp_alias_set = -1;
798 /* Construct the leading half of a __builtin_setjmp call. Control will
799 return to RECEIVER_LABEL. This is also called directly by the SJLJ
800 exception handling code. */
802 void
803 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
805 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
806 rtx stack_save;
807 rtx mem;
809 if (setjmp_alias_set == -1)
810 setjmp_alias_set = new_alias_set ();
812 buf_addr = convert_memory_address (Pmode, buf_addr);
814 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
816 /* We store the frame pointer and the address of receiver_label in
817 the buffer and use the rest of it for the stack save area, which
818 is machine-dependent. */
820 mem = gen_rtx_MEM (Pmode, buf_addr);
821 set_mem_alias_set (mem, setjmp_alias_set);
822 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
824 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
825 set_mem_alias_set (mem, setjmp_alias_set);
827 emit_move_insn (validize_mem (mem),
828 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
830 stack_save = gen_rtx_MEM (sa_mode,
831 plus_constant (buf_addr,
832 2 * GET_MODE_SIZE (Pmode)));
833 set_mem_alias_set (stack_save, setjmp_alias_set);
834 emit_stack_save (SAVE_NONLOCAL, &stack_save);
836 /* If there is further processing to do, do it. */
837 #ifdef HAVE_builtin_setjmp_setup
838 if (HAVE_builtin_setjmp_setup)
839 emit_insn (gen_builtin_setjmp_setup (buf_addr));
840 #endif
842 /* We have a nonlocal label. */
843 cfun->has_nonlocal_label = 1;
846 /* Construct the trailing part of a __builtin_setjmp call. This is
847 also called directly by the SJLJ exception handling code. */
849 void
850 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
852 rtx chain;
854 /* Clobber the FP when we get here, so we have to make sure it's
855 marked as used by this function. */
856 emit_use (hard_frame_pointer_rtx);
858 /* Mark the static chain as clobbered here so life information
859 doesn't get messed up for it. */
860 chain = targetm.calls.static_chain (current_function_decl, true);
861 if (chain && REG_P (chain))
862 emit_clobber (chain);
864 /* Now put in the code to restore the frame pointer, and argument
865 pointer, if needed. */
866 #ifdef HAVE_nonlocal_goto
867 if (! HAVE_nonlocal_goto)
868 #endif
870 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
871 /* This might change the hard frame pointer in ways that aren't
872 apparent to early optimization passes, so force a clobber. */
873 emit_clobber (hard_frame_pointer_rtx);
876 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
877 if (fixed_regs[ARG_POINTER_REGNUM])
879 #ifdef ELIMINABLE_REGS
880 size_t i;
881 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
883 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
884 if (elim_regs[i].from == ARG_POINTER_REGNUM
885 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
886 break;
888 if (i == ARRAY_SIZE (elim_regs))
889 #endif
891 /* Now restore our arg pointer from the address at which it
892 was saved in our stack frame. */
893 emit_move_insn (crtl->args.internal_arg_pointer,
894 copy_to_reg (get_arg_pointer_save_area ()));
897 #endif
899 #ifdef HAVE_builtin_setjmp_receiver
900 if (HAVE_builtin_setjmp_receiver)
901 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
902 else
903 #endif
904 #ifdef HAVE_nonlocal_goto_receiver
905 if (HAVE_nonlocal_goto_receiver)
906 emit_insn (gen_nonlocal_goto_receiver ());
907 else
908 #endif
909 { /* Nothing */ }
911 /* We must not allow the code we just generated to be reordered by
912 scheduling. Specifically, the update of the frame pointer must
913 happen immediately, not later. */
914 emit_insn (gen_blockage ());
917 /* __builtin_longjmp is passed a pointer to an array of five words (not
918 all will be used on all machines). It operates similarly to the C
919 library function of the same name, but is more efficient. Much of
920 the code below is copied from the handling of non-local gotos. */
922 static void
923 expand_builtin_longjmp (rtx buf_addr, rtx value)
925 rtx fp, lab, stack, insn, last;
926 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
928 /* DRAP is needed for stack realign if longjmp is expanded to current
929 function */
930 if (SUPPORTS_STACK_ALIGNMENT)
931 crtl->need_drap = true;
933 if (setjmp_alias_set == -1)
934 setjmp_alias_set = new_alias_set ();
936 buf_addr = convert_memory_address (Pmode, buf_addr);
938 buf_addr = force_reg (Pmode, buf_addr);
940 /* We require that the user must pass a second argument of 1, because
941 that is what builtin_setjmp will return. */
942 gcc_assert (value == const1_rtx);
944 last = get_last_insn ();
945 #ifdef HAVE_builtin_longjmp
946 if (HAVE_builtin_longjmp)
947 emit_insn (gen_builtin_longjmp (buf_addr));
948 else
949 #endif
951 fp = gen_rtx_MEM (Pmode, buf_addr);
952 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
953 GET_MODE_SIZE (Pmode)));
955 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
956 2 * GET_MODE_SIZE (Pmode)));
957 set_mem_alias_set (fp, setjmp_alias_set);
958 set_mem_alias_set (lab, setjmp_alias_set);
959 set_mem_alias_set (stack, setjmp_alias_set);
961 /* Pick up FP, label, and SP from the block and jump. This code is
962 from expand_goto in stmt.c; see there for detailed comments. */
963 #ifdef HAVE_nonlocal_goto
964 if (HAVE_nonlocal_goto)
965 /* We have to pass a value to the nonlocal_goto pattern that will
966 get copied into the static_chain pointer, but it does not matter
967 what that value is, because builtin_setjmp does not use it. */
968 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
969 else
970 #endif
972 lab = copy_to_reg (lab);
974 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
975 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
977 emit_move_insn (hard_frame_pointer_rtx, fp);
978 emit_stack_restore (SAVE_NONLOCAL, stack);
980 emit_use (hard_frame_pointer_rtx);
981 emit_use (stack_pointer_rtx);
982 emit_indirect_jump (lab);
986 /* Search backwards and mark the jump insn as a non-local goto.
987 Note that this precludes the use of __builtin_longjmp to a
988 __builtin_setjmp target in the same function. However, we've
989 already cautioned the user that these functions are for
990 internal exception handling use only. */
991 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
993 gcc_assert (insn != last);
995 if (JUMP_P (insn))
997 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
998 break;
1000 else if (CALL_P (insn))
1001 break;
1005 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1006 and the address of the save area. */
1008 static rtx
1009 expand_builtin_nonlocal_goto (tree exp)
1011 tree t_label, t_save_area;
1012 rtx r_label, r_save_area, r_fp, r_sp, insn;
1014 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1015 return NULL_RTX;
1017 t_label = CALL_EXPR_ARG (exp, 0);
1018 t_save_area = CALL_EXPR_ARG (exp, 1);
1020 r_label = expand_normal (t_label);
1021 r_label = convert_memory_address (Pmode, r_label);
1022 r_save_area = expand_normal (t_save_area);
1023 r_save_area = convert_memory_address (Pmode, r_save_area);
1024 /* Copy the address of the save location to a register just in case it was
1025 based on the frame pointer. */
1026 r_save_area = copy_to_reg (r_save_area);
1027 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1028 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1029 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1031 crtl->has_nonlocal_goto = 1;
1033 #ifdef HAVE_nonlocal_goto
1034 /* ??? We no longer need to pass the static chain value, afaik. */
1035 if (HAVE_nonlocal_goto)
1036 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1037 else
1038 #endif
1040 r_label = copy_to_reg (r_label);
1042 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1043 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1045 /* Restore frame pointer for containing function. */
1046 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1047 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1049 /* USE of hard_frame_pointer_rtx added for consistency;
1050 not clear if really needed. */
1051 emit_use (hard_frame_pointer_rtx);
1052 emit_use (stack_pointer_rtx);
1054 /* If the architecture is using a GP register, we must
1055 conservatively assume that the target function makes use of it.
1056 The prologue of functions with nonlocal gotos must therefore
1057 initialize the GP register to the appropriate value, and we
1058 must then make sure that this value is live at the point
1059 of the jump. (Note that this doesn't necessarily apply
1060 to targets with a nonlocal_goto pattern; they are free
1061 to implement it in their own way. Note also that this is
1062 a no-op if the GP register is a global invariant.) */
1063 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1064 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1065 emit_use (pic_offset_table_rtx);
1067 emit_indirect_jump (r_label);
1070 /* Search backwards to the jump insn and mark it as a
1071 non-local goto. */
1072 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1074 if (JUMP_P (insn))
1076 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1077 break;
1079 else if (CALL_P (insn))
1080 break;
1083 return const0_rtx;
1086 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1087 (not all will be used on all machines) that was passed to __builtin_setjmp.
1088 It updates the stack pointer in that block to correspond to the current
1089 stack pointer. */
1091 static void
1092 expand_builtin_update_setjmp_buf (rtx buf_addr)
1094 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1095 rtx stack_save
1096 = gen_rtx_MEM (sa_mode,
1097 memory_address
1098 (sa_mode,
1099 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1101 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1104 /* Expand a call to __builtin_prefetch. For a target that does not support
1105 data prefetch, evaluate the memory address argument in case it has side
1106 effects. */
1108 static void
1109 expand_builtin_prefetch (tree exp)
1111 tree arg0, arg1, arg2;
1112 int nargs;
1113 rtx op0, op1, op2;
1115 if (!validate_arglist (exp, POINTER_TYPE, 0))
1116 return;
1118 arg0 = CALL_EXPR_ARG (exp, 0);
1120 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1121 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1122 locality). */
1123 nargs = call_expr_nargs (exp);
1124 if (nargs > 1)
1125 arg1 = CALL_EXPR_ARG (exp, 1);
1126 else
1127 arg1 = integer_zero_node;
1128 if (nargs > 2)
1129 arg2 = CALL_EXPR_ARG (exp, 2);
1130 else
1131 arg2 = integer_three_node;
1133 /* Argument 0 is an address. */
1134 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1136 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1137 if (TREE_CODE (arg1) != INTEGER_CST)
1139 error ("second argument to %<__builtin_prefetch%> must be a constant");
1140 arg1 = integer_zero_node;
1142 op1 = expand_normal (arg1);
1143 /* Argument 1 must be either zero or one. */
1144 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1146 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1147 " using zero");
1148 op1 = const0_rtx;
1151 /* Argument 2 (locality) must be a compile-time constant int. */
1152 if (TREE_CODE (arg2) != INTEGER_CST)
1154 error ("third argument to %<__builtin_prefetch%> must be a constant");
1155 arg2 = integer_zero_node;
1157 op2 = expand_normal (arg2);
1158 /* Argument 2 must be 0, 1, 2, or 3. */
1159 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1161 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1162 op2 = const0_rtx;
1165 #ifdef HAVE_prefetch
1166 if (HAVE_prefetch)
1168 struct expand_operand ops[3];
1170 create_address_operand (&ops[0], op0);
1171 create_integer_operand (&ops[1], INTVAL (op1));
1172 create_integer_operand (&ops[2], INTVAL (op2));
1173 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1174 return;
1176 #endif
1178 /* Don't do anything with direct references to volatile memory, but
1179 generate code to handle other side effects. */
1180 if (!MEM_P (op0) && side_effects_p (op0))
1181 emit_insn (op0);
1184 /* Get a MEM rtx for expression EXP which is the address of an operand
1185 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1186 the maximum length of the block of memory that might be accessed or
1187 NULL if unknown. */
1189 static rtx
1190 get_memory_rtx (tree exp, tree len)
1192 tree orig_exp = exp;
1193 rtx addr, mem;
1194 HOST_WIDE_INT off;
1196 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1197 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1198 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1199 exp = TREE_OPERAND (exp, 0);
1201 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1202 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1204 /* Get an expression we can use to find the attributes to assign to MEM.
1205 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1206 we can. First remove any nops. */
1207 while (CONVERT_EXPR_P (exp)
1208 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1209 exp = TREE_OPERAND (exp, 0);
1211 off = 0;
1212 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1213 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1214 && host_integerp (TREE_OPERAND (exp, 1), 0)
1215 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1216 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1217 else if (TREE_CODE (exp) == ADDR_EXPR)
1218 exp = TREE_OPERAND (exp, 0);
1219 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1220 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1221 else
1222 exp = NULL;
1224 /* Honor attributes derived from exp, except for the alias set
1225 (as builtin stringops may alias with anything) and the size
1226 (as stringops may access multiple array elements). */
1227 if (exp)
1229 set_mem_attributes (mem, exp, 0);
1231 if (off)
1232 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1234 /* Allow the string and memory builtins to overflow from one
1235 field into another, see http://gcc.gnu.org/PR23561.
1236 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1237 memory accessed by the string or memory builtin will fit
1238 within the field. */
1239 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1241 tree mem_expr = MEM_EXPR (mem);
1242 HOST_WIDE_INT offset = -1, length = -1;
1243 tree inner = exp;
1245 while (TREE_CODE (inner) == ARRAY_REF
1246 || CONVERT_EXPR_P (inner)
1247 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1248 || TREE_CODE (inner) == SAVE_EXPR)
1249 inner = TREE_OPERAND (inner, 0);
1251 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1253 if (MEM_OFFSET_KNOWN_P (mem))
1254 offset = MEM_OFFSET (mem);
1256 if (offset >= 0 && len && host_integerp (len, 0))
1257 length = tree_low_cst (len, 0);
1259 while (TREE_CODE (inner) == COMPONENT_REF)
1261 tree field = TREE_OPERAND (inner, 1);
1262 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1263 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1265 /* Bitfields are generally not byte-addressable. */
1266 gcc_assert (!DECL_BIT_FIELD (field)
1267 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1268 % BITS_PER_UNIT) == 0
1269 && host_integerp (DECL_SIZE (field), 0)
1270 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1271 % BITS_PER_UNIT) == 0));
1273 /* If we can prove that the memory starting at XEXP (mem, 0) and
1274 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1275 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1276 fields without DECL_SIZE_UNIT like flexible array members. */
1277 if (length >= 0
1278 && DECL_SIZE_UNIT (field)
1279 && host_integerp (DECL_SIZE_UNIT (field), 0))
1281 HOST_WIDE_INT size
1282 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1283 if (offset <= size
1284 && length <= size
1285 && offset + length <= size)
1286 break;
1289 if (offset >= 0
1290 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1291 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1292 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1293 / BITS_PER_UNIT;
1294 else
1296 offset = -1;
1297 length = -1;
1300 mem_expr = TREE_OPERAND (mem_expr, 0);
1301 inner = TREE_OPERAND (inner, 0);
1304 if (mem_expr == NULL)
1305 offset = -1;
1306 if (mem_expr != MEM_EXPR (mem))
1308 set_mem_expr (mem, mem_expr);
1309 if (offset >= 0)
1310 set_mem_offset (mem, offset);
1311 else
1312 clear_mem_offset (mem);
1315 set_mem_alias_set (mem, 0);
1316 clear_mem_size (mem);
1319 return mem;
1322 /* Built-in functions to perform an untyped call and return. */
1324 #define apply_args_mode \
1325 (this_target_builtins->x_apply_args_mode)
1326 #define apply_result_mode \
1327 (this_target_builtins->x_apply_result_mode)
1329 /* Return the size required for the block returned by __builtin_apply_args,
1330 and initialize apply_args_mode. */
1332 static int
1333 apply_args_size (void)
1335 static int size = -1;
1336 int align;
1337 unsigned int regno;
1338 enum machine_mode mode;
1340 /* The values computed by this function never change. */
1341 if (size < 0)
1343 /* The first value is the incoming arg-pointer. */
1344 size = GET_MODE_SIZE (Pmode);
1346 /* The second value is the structure value address unless this is
1347 passed as an "invisible" first argument. */
1348 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1349 size += GET_MODE_SIZE (Pmode);
1351 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1352 if (FUNCTION_ARG_REGNO_P (regno))
1354 mode = targetm.calls.get_raw_arg_mode (regno);
1356 gcc_assert (mode != VOIDmode);
1358 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1359 if (size % align != 0)
1360 size = CEIL (size, align) * align;
1361 size += GET_MODE_SIZE (mode);
1362 apply_args_mode[regno] = mode;
1364 else
1366 apply_args_mode[regno] = VOIDmode;
1369 return size;
1372 /* Return the size required for the block returned by __builtin_apply,
1373 and initialize apply_result_mode. */
1375 static int
1376 apply_result_size (void)
1378 static int size = -1;
1379 int align, regno;
1380 enum machine_mode mode;
1382 /* The values computed by this function never change. */
1383 if (size < 0)
1385 size = 0;
1387 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1388 if (targetm.calls.function_value_regno_p (regno))
1390 mode = targetm.calls.get_raw_result_mode (regno);
1392 gcc_assert (mode != VOIDmode);
1394 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1395 if (size % align != 0)
1396 size = CEIL (size, align) * align;
1397 size += GET_MODE_SIZE (mode);
1398 apply_result_mode[regno] = mode;
1400 else
1401 apply_result_mode[regno] = VOIDmode;
1403 /* Allow targets that use untyped_call and untyped_return to override
1404 the size so that machine-specific information can be stored here. */
1405 #ifdef APPLY_RESULT_SIZE
1406 size = APPLY_RESULT_SIZE;
1407 #endif
1409 return size;
1412 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1413 /* Create a vector describing the result block RESULT. If SAVEP is true,
1414 the result block is used to save the values; otherwise it is used to
1415 restore the values. */
1417 static rtx
1418 result_vector (int savep, rtx result)
1420 int regno, size, align, nelts;
1421 enum machine_mode mode;
1422 rtx reg, mem;
1423 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1425 size = nelts = 0;
1426 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1427 if ((mode = apply_result_mode[regno]) != VOIDmode)
1429 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1430 if (size % align != 0)
1431 size = CEIL (size, align) * align;
1432 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1433 mem = adjust_address (result, mode, size);
1434 savevec[nelts++] = (savep
1435 ? gen_rtx_SET (VOIDmode, mem, reg)
1436 : gen_rtx_SET (VOIDmode, reg, mem));
1437 size += GET_MODE_SIZE (mode);
1439 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1441 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1443 /* Save the state required to perform an untyped call with the same
1444 arguments as were passed to the current function. */
1446 static rtx
1447 expand_builtin_apply_args_1 (void)
1449 rtx registers, tem;
1450 int size, align, regno;
1451 enum machine_mode mode;
1452 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1454 /* Create a block where the arg-pointer, structure value address,
1455 and argument registers can be saved. */
1456 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1458 /* Walk past the arg-pointer and structure value address. */
1459 size = GET_MODE_SIZE (Pmode);
1460 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1461 size += GET_MODE_SIZE (Pmode);
1463 /* Save each register used in calling a function to the block. */
1464 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1465 if ((mode = apply_args_mode[regno]) != VOIDmode)
1467 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1468 if (size % align != 0)
1469 size = CEIL (size, align) * align;
1471 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1473 emit_move_insn (adjust_address (registers, mode, size), tem);
1474 size += GET_MODE_SIZE (mode);
1477 /* Save the arg pointer to the block. */
1478 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1479 #ifdef STACK_GROWS_DOWNWARD
1480 /* We need the pointer as the caller actually passed them to us, not
1481 as we might have pretended they were passed. Make sure it's a valid
1482 operand, as emit_move_insn isn't expected to handle a PLUS. */
1484 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1485 NULL_RTX);
1486 #endif
1487 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1489 size = GET_MODE_SIZE (Pmode);
1491 /* Save the structure value address unless this is passed as an
1492 "invisible" first argument. */
1493 if (struct_incoming_value)
1495 emit_move_insn (adjust_address (registers, Pmode, size),
1496 copy_to_reg (struct_incoming_value));
1497 size += GET_MODE_SIZE (Pmode);
1500 /* Return the address of the block. */
1501 return copy_addr_to_reg (XEXP (registers, 0));
1504 /* __builtin_apply_args returns block of memory allocated on
1505 the stack into which is stored the arg pointer, structure
1506 value address, static chain, and all the registers that might
1507 possibly be used in performing a function call. The code is
1508 moved to the start of the function so the incoming values are
1509 saved. */
1511 static rtx
1512 expand_builtin_apply_args (void)
1514 /* Don't do __builtin_apply_args more than once in a function.
1515 Save the result of the first call and reuse it. */
1516 if (apply_args_value != 0)
1517 return apply_args_value;
1519 /* When this function is called, it means that registers must be
1520 saved on entry to this function. So we migrate the
1521 call to the first insn of this function. */
1522 rtx temp;
1523 rtx seq;
1525 start_sequence ();
1526 temp = expand_builtin_apply_args_1 ();
1527 seq = get_insns ();
1528 end_sequence ();
1530 apply_args_value = temp;
1532 /* Put the insns after the NOTE that starts the function.
1533 If this is inside a start_sequence, make the outer-level insn
1534 chain current, so the code is placed at the start of the
1535 function. If internal_arg_pointer is a non-virtual pseudo,
1536 it needs to be placed after the function that initializes
1537 that pseudo. */
1538 push_topmost_sequence ();
1539 if (REG_P (crtl->args.internal_arg_pointer)
1540 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1541 emit_insn_before (seq, parm_birth_insn);
1542 else
1543 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1544 pop_topmost_sequence ();
1545 return temp;
1549 /* Perform an untyped call and save the state required to perform an
1550 untyped return of whatever value was returned by the given function. */
1552 static rtx
1553 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1555 int size, align, regno;
1556 enum machine_mode mode;
1557 rtx incoming_args, result, reg, dest, src, call_insn;
1558 rtx old_stack_level = 0;
1559 rtx call_fusage = 0;
1560 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1562 arguments = convert_memory_address (Pmode, arguments);
1564 /* Create a block where the return registers can be saved. */
1565 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1567 /* Fetch the arg pointer from the ARGUMENTS block. */
1568 incoming_args = gen_reg_rtx (Pmode);
1569 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1570 #ifndef STACK_GROWS_DOWNWARD
1571 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1572 incoming_args, 0, OPTAB_LIB_WIDEN);
1573 #endif
1575 /* Push a new argument block and copy the arguments. Do not allow
1576 the (potential) memcpy call below to interfere with our stack
1577 manipulations. */
1578 do_pending_stack_adjust ();
1579 NO_DEFER_POP;
1581 /* Save the stack with nonlocal if available. */
1582 #ifdef HAVE_save_stack_nonlocal
1583 if (HAVE_save_stack_nonlocal)
1584 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1585 else
1586 #endif
1587 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1589 /* Allocate a block of memory onto the stack and copy the memory
1590 arguments to the outgoing arguments address. We can pass TRUE
1591 as the 4th argument because we just saved the stack pointer
1592 and will restore it right after the call. */
1593 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1595 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1596 may have already set current_function_calls_alloca to true.
1597 current_function_calls_alloca won't be set if argsize is zero,
1598 so we have to guarantee need_drap is true here. */
1599 if (SUPPORTS_STACK_ALIGNMENT)
1600 crtl->need_drap = true;
1602 dest = virtual_outgoing_args_rtx;
1603 #ifndef STACK_GROWS_DOWNWARD
1604 if (CONST_INT_P (argsize))
1605 dest = plus_constant (dest, -INTVAL (argsize));
1606 else
1607 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1608 #endif
1609 dest = gen_rtx_MEM (BLKmode, dest);
1610 set_mem_align (dest, PARM_BOUNDARY);
1611 src = gen_rtx_MEM (BLKmode, incoming_args);
1612 set_mem_align (src, PARM_BOUNDARY);
1613 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1615 /* Refer to the argument block. */
1616 apply_args_size ();
1617 arguments = gen_rtx_MEM (BLKmode, arguments);
1618 set_mem_align (arguments, PARM_BOUNDARY);
1620 /* Walk past the arg-pointer and structure value address. */
1621 size = GET_MODE_SIZE (Pmode);
1622 if (struct_value)
1623 size += GET_MODE_SIZE (Pmode);
1625 /* Restore each of the registers previously saved. Make USE insns
1626 for each of these registers for use in making the call. */
1627 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1628 if ((mode = apply_args_mode[regno]) != VOIDmode)
1630 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1631 if (size % align != 0)
1632 size = CEIL (size, align) * align;
1633 reg = gen_rtx_REG (mode, regno);
1634 emit_move_insn (reg, adjust_address (arguments, mode, size));
1635 use_reg (&call_fusage, reg);
1636 size += GET_MODE_SIZE (mode);
1639 /* Restore the structure value address unless this is passed as an
1640 "invisible" first argument. */
1641 size = GET_MODE_SIZE (Pmode);
1642 if (struct_value)
1644 rtx value = gen_reg_rtx (Pmode);
1645 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1646 emit_move_insn (struct_value, value);
1647 if (REG_P (struct_value))
1648 use_reg (&call_fusage, struct_value);
1649 size += GET_MODE_SIZE (Pmode);
1652 /* All arguments and registers used for the call are set up by now! */
1653 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1655 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1656 and we don't want to load it into a register as an optimization,
1657 because prepare_call_address already did it if it should be done. */
1658 if (GET_CODE (function) != SYMBOL_REF)
1659 function = memory_address (FUNCTION_MODE, function);
1661 /* Generate the actual call instruction and save the return value. */
1662 #ifdef HAVE_untyped_call
1663 if (HAVE_untyped_call)
1664 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1665 result, result_vector (1, result)));
1666 else
1667 #endif
1668 #ifdef HAVE_call_value
1669 if (HAVE_call_value)
1671 rtx valreg = 0;
1673 /* Locate the unique return register. It is not possible to
1674 express a call that sets more than one return register using
1675 call_value; use untyped_call for that. In fact, untyped_call
1676 only needs to save the return registers in the given block. */
1677 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1678 if ((mode = apply_result_mode[regno]) != VOIDmode)
1680 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1682 valreg = gen_rtx_REG (mode, regno);
1685 emit_call_insn (GEN_CALL_VALUE (valreg,
1686 gen_rtx_MEM (FUNCTION_MODE, function),
1687 const0_rtx, NULL_RTX, const0_rtx));
1689 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1691 else
1692 #endif
1693 gcc_unreachable ();
1695 /* Find the CALL insn we just emitted, and attach the register usage
1696 information. */
1697 call_insn = last_call_insn ();
1698 add_function_usage_to (call_insn, call_fusage);
1700 /* Restore the stack. */
1701 #ifdef HAVE_save_stack_nonlocal
1702 if (HAVE_save_stack_nonlocal)
1703 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1704 else
1705 #endif
1706 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1707 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1709 OK_DEFER_POP;
1711 /* Return the address of the result block. */
1712 result = copy_addr_to_reg (XEXP (result, 0));
1713 return convert_memory_address (ptr_mode, result);
1716 /* Perform an untyped return. */
1718 static void
1719 expand_builtin_return (rtx result)
1721 int size, align, regno;
1722 enum machine_mode mode;
1723 rtx reg;
1724 rtx call_fusage = 0;
1726 result = convert_memory_address (Pmode, result);
1728 apply_result_size ();
1729 result = gen_rtx_MEM (BLKmode, result);
1731 #ifdef HAVE_untyped_return
1732 if (HAVE_untyped_return)
1734 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1735 emit_barrier ();
1736 return;
1738 #endif
1740 /* Restore the return value and note that each value is used. */
1741 size = 0;
1742 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1743 if ((mode = apply_result_mode[regno]) != VOIDmode)
1745 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1746 if (size % align != 0)
1747 size = CEIL (size, align) * align;
1748 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1749 emit_move_insn (reg, adjust_address (result, mode, size));
1751 push_to_sequence (call_fusage);
1752 emit_use (reg);
1753 call_fusage = get_insns ();
1754 end_sequence ();
1755 size += GET_MODE_SIZE (mode);
1758 /* Put the USE insns before the return. */
1759 emit_insn (call_fusage);
1761 /* Return whatever values was restored by jumping directly to the end
1762 of the function. */
1763 expand_naked_return ();
1766 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1768 static enum type_class
1769 type_to_class (tree type)
1771 switch (TREE_CODE (type))
1773 case VOID_TYPE: return void_type_class;
1774 case INTEGER_TYPE: return integer_type_class;
1775 case ENUMERAL_TYPE: return enumeral_type_class;
1776 case BOOLEAN_TYPE: return boolean_type_class;
1777 case POINTER_TYPE: return pointer_type_class;
1778 case REFERENCE_TYPE: return reference_type_class;
1779 case OFFSET_TYPE: return offset_type_class;
1780 case REAL_TYPE: return real_type_class;
1781 case COMPLEX_TYPE: return complex_type_class;
1782 case FUNCTION_TYPE: return function_type_class;
1783 case METHOD_TYPE: return method_type_class;
1784 case RECORD_TYPE: return record_type_class;
1785 case UNION_TYPE:
1786 case QUAL_UNION_TYPE: return union_type_class;
1787 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1788 ? string_type_class : array_type_class);
1789 case LANG_TYPE: return lang_type_class;
1790 default: return no_type_class;
1794 /* Expand a call EXP to __builtin_classify_type. */
1796 static rtx
1797 expand_builtin_classify_type (tree exp)
1799 if (call_expr_nargs (exp))
1800 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1801 return GEN_INT (no_type_class);
1804 /* This helper macro, meant to be used in mathfn_built_in below,
1805 determines which among a set of three builtin math functions is
1806 appropriate for a given type mode. The `F' and `L' cases are
1807 automatically generated from the `double' case. */
1808 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1809 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1810 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1811 fcodel = BUILT_IN_MATHFN##L ; break;
1812 /* Similar to above, but appends _R after any F/L suffix. */
1813 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1814 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1815 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1816 fcodel = BUILT_IN_MATHFN##L_R ; break;
1818 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1819 if available. If IMPLICIT is true use the implicit builtin declaration,
1820 otherwise use the explicit declaration. If we can't do the conversion,
1821 return zero. */
1823 static tree
1824 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1826 enum built_in_function fcode, fcodef, fcodel, fcode2;
1828 switch (fn)
1830 CASE_MATHFN (BUILT_IN_ACOS)
1831 CASE_MATHFN (BUILT_IN_ACOSH)
1832 CASE_MATHFN (BUILT_IN_ASIN)
1833 CASE_MATHFN (BUILT_IN_ASINH)
1834 CASE_MATHFN (BUILT_IN_ATAN)
1835 CASE_MATHFN (BUILT_IN_ATAN2)
1836 CASE_MATHFN (BUILT_IN_ATANH)
1837 CASE_MATHFN (BUILT_IN_CBRT)
1838 CASE_MATHFN (BUILT_IN_CEIL)
1839 CASE_MATHFN (BUILT_IN_CEXPI)
1840 CASE_MATHFN (BUILT_IN_COPYSIGN)
1841 CASE_MATHFN (BUILT_IN_COS)
1842 CASE_MATHFN (BUILT_IN_COSH)
1843 CASE_MATHFN (BUILT_IN_DREM)
1844 CASE_MATHFN (BUILT_IN_ERF)
1845 CASE_MATHFN (BUILT_IN_ERFC)
1846 CASE_MATHFN (BUILT_IN_EXP)
1847 CASE_MATHFN (BUILT_IN_EXP10)
1848 CASE_MATHFN (BUILT_IN_EXP2)
1849 CASE_MATHFN (BUILT_IN_EXPM1)
1850 CASE_MATHFN (BUILT_IN_FABS)
1851 CASE_MATHFN (BUILT_IN_FDIM)
1852 CASE_MATHFN (BUILT_IN_FLOOR)
1853 CASE_MATHFN (BUILT_IN_FMA)
1854 CASE_MATHFN (BUILT_IN_FMAX)
1855 CASE_MATHFN (BUILT_IN_FMIN)
1856 CASE_MATHFN (BUILT_IN_FMOD)
1857 CASE_MATHFN (BUILT_IN_FREXP)
1858 CASE_MATHFN (BUILT_IN_GAMMA)
1859 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1860 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1861 CASE_MATHFN (BUILT_IN_HYPOT)
1862 CASE_MATHFN (BUILT_IN_ILOGB)
1863 CASE_MATHFN (BUILT_IN_ICEIL)
1864 CASE_MATHFN (BUILT_IN_IFLOOR)
1865 CASE_MATHFN (BUILT_IN_INF)
1866 CASE_MATHFN (BUILT_IN_IRINT)
1867 CASE_MATHFN (BUILT_IN_IROUND)
1868 CASE_MATHFN (BUILT_IN_ISINF)
1869 CASE_MATHFN (BUILT_IN_J0)
1870 CASE_MATHFN (BUILT_IN_J1)
1871 CASE_MATHFN (BUILT_IN_JN)
1872 CASE_MATHFN (BUILT_IN_LCEIL)
1873 CASE_MATHFN (BUILT_IN_LDEXP)
1874 CASE_MATHFN (BUILT_IN_LFLOOR)
1875 CASE_MATHFN (BUILT_IN_LGAMMA)
1876 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1877 CASE_MATHFN (BUILT_IN_LLCEIL)
1878 CASE_MATHFN (BUILT_IN_LLFLOOR)
1879 CASE_MATHFN (BUILT_IN_LLRINT)
1880 CASE_MATHFN (BUILT_IN_LLROUND)
1881 CASE_MATHFN (BUILT_IN_LOG)
1882 CASE_MATHFN (BUILT_IN_LOG10)
1883 CASE_MATHFN (BUILT_IN_LOG1P)
1884 CASE_MATHFN (BUILT_IN_LOG2)
1885 CASE_MATHFN (BUILT_IN_LOGB)
1886 CASE_MATHFN (BUILT_IN_LRINT)
1887 CASE_MATHFN (BUILT_IN_LROUND)
1888 CASE_MATHFN (BUILT_IN_MODF)
1889 CASE_MATHFN (BUILT_IN_NAN)
1890 CASE_MATHFN (BUILT_IN_NANS)
1891 CASE_MATHFN (BUILT_IN_NEARBYINT)
1892 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1893 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1894 CASE_MATHFN (BUILT_IN_POW)
1895 CASE_MATHFN (BUILT_IN_POWI)
1896 CASE_MATHFN (BUILT_IN_POW10)
1897 CASE_MATHFN (BUILT_IN_REMAINDER)
1898 CASE_MATHFN (BUILT_IN_REMQUO)
1899 CASE_MATHFN (BUILT_IN_RINT)
1900 CASE_MATHFN (BUILT_IN_ROUND)
1901 CASE_MATHFN (BUILT_IN_SCALB)
1902 CASE_MATHFN (BUILT_IN_SCALBLN)
1903 CASE_MATHFN (BUILT_IN_SCALBN)
1904 CASE_MATHFN (BUILT_IN_SIGNBIT)
1905 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1906 CASE_MATHFN (BUILT_IN_SIN)
1907 CASE_MATHFN (BUILT_IN_SINCOS)
1908 CASE_MATHFN (BUILT_IN_SINH)
1909 CASE_MATHFN (BUILT_IN_SQRT)
1910 CASE_MATHFN (BUILT_IN_TAN)
1911 CASE_MATHFN (BUILT_IN_TANH)
1912 CASE_MATHFN (BUILT_IN_TGAMMA)
1913 CASE_MATHFN (BUILT_IN_TRUNC)
1914 CASE_MATHFN (BUILT_IN_Y0)
1915 CASE_MATHFN (BUILT_IN_Y1)
1916 CASE_MATHFN (BUILT_IN_YN)
1918 default:
1919 return NULL_TREE;
1922 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1923 fcode2 = fcode;
1924 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1925 fcode2 = fcodef;
1926 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1927 fcode2 = fcodel;
1928 else
1929 return NULL_TREE;
1931 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1932 return NULL_TREE;
1934 return builtin_decl_explicit (fcode2);
1937 /* Like mathfn_built_in_1(), but always use the implicit array. */
1939 tree
1940 mathfn_built_in (tree type, enum built_in_function fn)
1942 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1945 /* If errno must be maintained, expand the RTL to check if the result,
1946 TARGET, of a built-in function call, EXP, is NaN, and if so set
1947 errno to EDOM. */
1949 static void
1950 expand_errno_check (tree exp, rtx target)
1952 rtx lab = gen_label_rtx ();
1954 /* Test the result; if it is NaN, set errno=EDOM because
1955 the argument was not in the domain. */
1956 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1957 NULL_RTX, NULL_RTX, lab,
1958 /* The jump is very likely. */
1959 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1961 #ifdef TARGET_EDOM
1962 /* If this built-in doesn't throw an exception, set errno directly. */
1963 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1965 #ifdef GEN_ERRNO_RTX
1966 rtx errno_rtx = GEN_ERRNO_RTX;
1967 #else
1968 rtx errno_rtx
1969 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1970 #endif
1971 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1972 emit_label (lab);
1973 return;
1975 #endif
1977 /* Make sure the library call isn't expanded as a tail call. */
1978 CALL_EXPR_TAILCALL (exp) = 0;
1980 /* We can't set errno=EDOM directly; let the library call do it.
1981 Pop the arguments right away in case the call gets deleted. */
1982 NO_DEFER_POP;
1983 expand_call (exp, target, 0);
1984 OK_DEFER_POP;
1985 emit_label (lab);
1988 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1989 Return NULL_RTX if a normal call should be emitted rather than expanding
1990 the function in-line. EXP is the expression that is a call to the builtin
1991 function; if convenient, the result should be placed in TARGET.
1992 SUBTARGET may be used as the target for computing one of EXP's operands. */
1994 static rtx
1995 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1997 optab builtin_optab;
1998 rtx op0, insns;
1999 tree fndecl = get_callee_fndecl (exp);
2000 enum machine_mode mode;
2001 bool errno_set = false;
2002 tree arg;
2004 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2005 return NULL_RTX;
2007 arg = CALL_EXPR_ARG (exp, 0);
2009 switch (DECL_FUNCTION_CODE (fndecl))
2011 CASE_FLT_FN (BUILT_IN_SQRT):
2012 errno_set = ! tree_expr_nonnegative_p (arg);
2013 builtin_optab = sqrt_optab;
2014 break;
2015 CASE_FLT_FN (BUILT_IN_EXP):
2016 errno_set = true; builtin_optab = exp_optab; break;
2017 CASE_FLT_FN (BUILT_IN_EXP10):
2018 CASE_FLT_FN (BUILT_IN_POW10):
2019 errno_set = true; builtin_optab = exp10_optab; break;
2020 CASE_FLT_FN (BUILT_IN_EXP2):
2021 errno_set = true; builtin_optab = exp2_optab; break;
2022 CASE_FLT_FN (BUILT_IN_EXPM1):
2023 errno_set = true; builtin_optab = expm1_optab; break;
2024 CASE_FLT_FN (BUILT_IN_LOGB):
2025 errno_set = true; builtin_optab = logb_optab; break;
2026 CASE_FLT_FN (BUILT_IN_LOG):
2027 errno_set = true; builtin_optab = log_optab; break;
2028 CASE_FLT_FN (BUILT_IN_LOG10):
2029 errno_set = true; builtin_optab = log10_optab; break;
2030 CASE_FLT_FN (BUILT_IN_LOG2):
2031 errno_set = true; builtin_optab = log2_optab; break;
2032 CASE_FLT_FN (BUILT_IN_LOG1P):
2033 errno_set = true; builtin_optab = log1p_optab; break;
2034 CASE_FLT_FN (BUILT_IN_ASIN):
2035 builtin_optab = asin_optab; break;
2036 CASE_FLT_FN (BUILT_IN_ACOS):
2037 builtin_optab = acos_optab; break;
2038 CASE_FLT_FN (BUILT_IN_TAN):
2039 builtin_optab = tan_optab; break;
2040 CASE_FLT_FN (BUILT_IN_ATAN):
2041 builtin_optab = atan_optab; break;
2042 CASE_FLT_FN (BUILT_IN_FLOOR):
2043 builtin_optab = floor_optab; break;
2044 CASE_FLT_FN (BUILT_IN_CEIL):
2045 builtin_optab = ceil_optab; break;
2046 CASE_FLT_FN (BUILT_IN_TRUNC):
2047 builtin_optab = btrunc_optab; break;
2048 CASE_FLT_FN (BUILT_IN_ROUND):
2049 builtin_optab = round_optab; break;
2050 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2051 builtin_optab = nearbyint_optab;
2052 if (flag_trapping_math)
2053 break;
2054 /* Else fallthrough and expand as rint. */
2055 CASE_FLT_FN (BUILT_IN_RINT):
2056 builtin_optab = rint_optab; break;
2057 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2058 builtin_optab = significand_optab; break;
2059 default:
2060 gcc_unreachable ();
2063 /* Make a suitable register to place result in. */
2064 mode = TYPE_MODE (TREE_TYPE (exp));
2066 if (! flag_errno_math || ! HONOR_NANS (mode))
2067 errno_set = false;
2069 /* Before working hard, check whether the instruction is available. */
2070 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2071 && (!errno_set || !optimize_insn_for_size_p ()))
2073 target = gen_reg_rtx (mode);
2075 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2076 need to expand the argument again. This way, we will not perform
2077 side-effects more the once. */
2078 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2080 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2082 start_sequence ();
2084 /* Compute into TARGET.
2085 Set TARGET to wherever the result comes back. */
2086 target = expand_unop (mode, builtin_optab, op0, target, 0);
2088 if (target != 0)
2090 if (errno_set)
2091 expand_errno_check (exp, target);
2093 /* Output the entire sequence. */
2094 insns = get_insns ();
2095 end_sequence ();
2096 emit_insn (insns);
2097 return target;
2100 /* If we were unable to expand via the builtin, stop the sequence
2101 (without outputting the insns) and call to the library function
2102 with the stabilized argument list. */
2103 end_sequence ();
2106 return expand_call (exp, target, target == const0_rtx);
2109 /* Expand a call to the builtin binary math functions (pow and atan2).
2110 Return NULL_RTX if a normal call should be emitted rather than expanding the
2111 function in-line. EXP is the expression that is a call to the builtin
2112 function; if convenient, the result should be placed in TARGET.
2113 SUBTARGET may be used as the target for computing one of EXP's
2114 operands. */
2116 static rtx
2117 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2119 optab builtin_optab;
2120 rtx op0, op1, insns;
2121 int op1_type = REAL_TYPE;
2122 tree fndecl = get_callee_fndecl (exp);
2123 tree arg0, arg1;
2124 enum machine_mode mode;
2125 bool errno_set = true;
2127 switch (DECL_FUNCTION_CODE (fndecl))
2129 CASE_FLT_FN (BUILT_IN_SCALBN):
2130 CASE_FLT_FN (BUILT_IN_SCALBLN):
2131 CASE_FLT_FN (BUILT_IN_LDEXP):
2132 op1_type = INTEGER_TYPE;
2133 default:
2134 break;
2137 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2138 return NULL_RTX;
2140 arg0 = CALL_EXPR_ARG (exp, 0);
2141 arg1 = CALL_EXPR_ARG (exp, 1);
2143 switch (DECL_FUNCTION_CODE (fndecl))
2145 CASE_FLT_FN (BUILT_IN_POW):
2146 builtin_optab = pow_optab; break;
2147 CASE_FLT_FN (BUILT_IN_ATAN2):
2148 builtin_optab = atan2_optab; break;
2149 CASE_FLT_FN (BUILT_IN_SCALB):
2150 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2151 return 0;
2152 builtin_optab = scalb_optab; break;
2153 CASE_FLT_FN (BUILT_IN_SCALBN):
2154 CASE_FLT_FN (BUILT_IN_SCALBLN):
2155 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2156 return 0;
2157 /* Fall through... */
2158 CASE_FLT_FN (BUILT_IN_LDEXP):
2159 builtin_optab = ldexp_optab; break;
2160 CASE_FLT_FN (BUILT_IN_FMOD):
2161 builtin_optab = fmod_optab; break;
2162 CASE_FLT_FN (BUILT_IN_REMAINDER):
2163 CASE_FLT_FN (BUILT_IN_DREM):
2164 builtin_optab = remainder_optab; break;
2165 default:
2166 gcc_unreachable ();
2169 /* Make a suitable register to place result in. */
2170 mode = TYPE_MODE (TREE_TYPE (exp));
2172 /* Before working hard, check whether the instruction is available. */
2173 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2174 return NULL_RTX;
2176 target = gen_reg_rtx (mode);
2178 if (! flag_errno_math || ! HONOR_NANS (mode))
2179 errno_set = false;
2181 if (errno_set && optimize_insn_for_size_p ())
2182 return 0;
2184 /* Always stabilize the argument list. */
2185 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2186 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2188 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2189 op1 = expand_normal (arg1);
2191 start_sequence ();
2193 /* Compute into TARGET.
2194 Set TARGET to wherever the result comes back. */
2195 target = expand_binop (mode, builtin_optab, op0, op1,
2196 target, 0, OPTAB_DIRECT);
2198 /* If we were unable to expand via the builtin, stop the sequence
2199 (without outputting the insns) and call to the library function
2200 with the stabilized argument list. */
2201 if (target == 0)
2203 end_sequence ();
2204 return expand_call (exp, target, target == const0_rtx);
2207 if (errno_set)
2208 expand_errno_check (exp, target);
2210 /* Output the entire sequence. */
2211 insns = get_insns ();
2212 end_sequence ();
2213 emit_insn (insns);
2215 return target;
2218 /* Expand a call to the builtin trinary math functions (fma).
2219 Return NULL_RTX if a normal call should be emitted rather than expanding the
2220 function in-line. EXP is the expression that is a call to the builtin
2221 function; if convenient, the result should be placed in TARGET.
2222 SUBTARGET may be used as the target for computing one of EXP's
2223 operands. */
2225 static rtx
2226 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2228 optab builtin_optab;
2229 rtx op0, op1, op2, insns;
2230 tree fndecl = get_callee_fndecl (exp);
2231 tree arg0, arg1, arg2;
2232 enum machine_mode mode;
2234 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2235 return NULL_RTX;
2237 arg0 = CALL_EXPR_ARG (exp, 0);
2238 arg1 = CALL_EXPR_ARG (exp, 1);
2239 arg2 = CALL_EXPR_ARG (exp, 2);
2241 switch (DECL_FUNCTION_CODE (fndecl))
2243 CASE_FLT_FN (BUILT_IN_FMA):
2244 builtin_optab = fma_optab; break;
2245 default:
2246 gcc_unreachable ();
2249 /* Make a suitable register to place result in. */
2250 mode = TYPE_MODE (TREE_TYPE (exp));
2252 /* Before working hard, check whether the instruction is available. */
2253 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2254 return NULL_RTX;
2256 target = gen_reg_rtx (mode);
2258 /* Always stabilize the argument list. */
2259 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2260 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2261 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2263 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2264 op1 = expand_normal (arg1);
2265 op2 = expand_normal (arg2);
2267 start_sequence ();
2269 /* Compute into TARGET.
2270 Set TARGET to wherever the result comes back. */
2271 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2272 target, 0);
2274 /* If we were unable to expand via the builtin, stop the sequence
2275 (without outputting the insns) and call to the library function
2276 with the stabilized argument list. */
2277 if (target == 0)
2279 end_sequence ();
2280 return expand_call (exp, target, target == const0_rtx);
2283 /* Output the entire sequence. */
2284 insns = get_insns ();
2285 end_sequence ();
2286 emit_insn (insns);
2288 return target;
2291 /* Expand a call to the builtin sin and cos math functions.
2292 Return NULL_RTX if a normal call should be emitted rather than expanding the
2293 function in-line. EXP is the expression that is a call to the builtin
2294 function; if convenient, the result should be placed in TARGET.
2295 SUBTARGET may be used as the target for computing one of EXP's
2296 operands. */
2298 static rtx
2299 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2301 optab builtin_optab;
2302 rtx op0, insns;
2303 tree fndecl = get_callee_fndecl (exp);
2304 enum machine_mode mode;
2305 tree arg;
2307 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2308 return NULL_RTX;
2310 arg = CALL_EXPR_ARG (exp, 0);
2312 switch (DECL_FUNCTION_CODE (fndecl))
2314 CASE_FLT_FN (BUILT_IN_SIN):
2315 CASE_FLT_FN (BUILT_IN_COS):
2316 builtin_optab = sincos_optab; break;
2317 default:
2318 gcc_unreachable ();
2321 /* Make a suitable register to place result in. */
2322 mode = TYPE_MODE (TREE_TYPE (exp));
2324 /* Check if sincos insn is available, otherwise fallback
2325 to sin or cos insn. */
2326 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2327 switch (DECL_FUNCTION_CODE (fndecl))
2329 CASE_FLT_FN (BUILT_IN_SIN):
2330 builtin_optab = sin_optab; break;
2331 CASE_FLT_FN (BUILT_IN_COS):
2332 builtin_optab = cos_optab; break;
2333 default:
2334 gcc_unreachable ();
2337 /* Before working hard, check whether the instruction is available. */
2338 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2340 target = gen_reg_rtx (mode);
2342 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2343 need to expand the argument again. This way, we will not perform
2344 side-effects more the once. */
2345 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2347 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2349 start_sequence ();
2351 /* Compute into TARGET.
2352 Set TARGET to wherever the result comes back. */
2353 if (builtin_optab == sincos_optab)
2355 int result;
2357 switch (DECL_FUNCTION_CODE (fndecl))
2359 CASE_FLT_FN (BUILT_IN_SIN):
2360 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2361 break;
2362 CASE_FLT_FN (BUILT_IN_COS):
2363 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2364 break;
2365 default:
2366 gcc_unreachable ();
2368 gcc_assert (result);
2370 else
2372 target = expand_unop (mode, builtin_optab, op0, target, 0);
2375 if (target != 0)
2377 /* Output the entire sequence. */
2378 insns = get_insns ();
2379 end_sequence ();
2380 emit_insn (insns);
2381 return target;
2384 /* If we were unable to expand via the builtin, stop the sequence
2385 (without outputting the insns) and call to the library function
2386 with the stabilized argument list. */
2387 end_sequence ();
2390 target = expand_call (exp, target, target == const0_rtx);
2392 return target;
2395 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2396 return an RTL instruction code that implements the functionality.
2397 If that isn't possible or available return CODE_FOR_nothing. */
2399 static enum insn_code
2400 interclass_mathfn_icode (tree arg, tree fndecl)
2402 bool errno_set = false;
2403 optab builtin_optab = 0;
2404 enum machine_mode mode;
2406 switch (DECL_FUNCTION_CODE (fndecl))
2408 CASE_FLT_FN (BUILT_IN_ILOGB):
2409 errno_set = true; builtin_optab = ilogb_optab; break;
2410 CASE_FLT_FN (BUILT_IN_ISINF):
2411 builtin_optab = isinf_optab; break;
2412 case BUILT_IN_ISNORMAL:
2413 case BUILT_IN_ISFINITE:
2414 CASE_FLT_FN (BUILT_IN_FINITE):
2415 case BUILT_IN_FINITED32:
2416 case BUILT_IN_FINITED64:
2417 case BUILT_IN_FINITED128:
2418 case BUILT_IN_ISINFD32:
2419 case BUILT_IN_ISINFD64:
2420 case BUILT_IN_ISINFD128:
2421 /* These builtins have no optabs (yet). */
2422 break;
2423 default:
2424 gcc_unreachable ();
2427 /* There's no easy way to detect the case we need to set EDOM. */
2428 if (flag_errno_math && errno_set)
2429 return CODE_FOR_nothing;
2431 /* Optab mode depends on the mode of the input argument. */
2432 mode = TYPE_MODE (TREE_TYPE (arg));
2434 if (builtin_optab)
2435 return optab_handler (builtin_optab, mode);
2436 return CODE_FOR_nothing;
2439 /* Expand a call to one of the builtin math functions that operate on
2440 floating point argument and output an integer result (ilogb, isinf,
2441 isnan, etc).
2442 Return 0 if a normal call should be emitted rather than expanding the
2443 function in-line. EXP is the expression that is a call to the builtin
2444 function; if convenient, the result should be placed in TARGET. */
2446 static rtx
2447 expand_builtin_interclass_mathfn (tree exp, rtx target)
2449 enum insn_code icode = CODE_FOR_nothing;
2450 rtx op0;
2451 tree fndecl = get_callee_fndecl (exp);
2452 enum machine_mode mode;
2453 tree arg;
2455 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2456 return NULL_RTX;
2458 arg = CALL_EXPR_ARG (exp, 0);
2459 icode = interclass_mathfn_icode (arg, fndecl);
2460 mode = TYPE_MODE (TREE_TYPE (arg));
2462 if (icode != CODE_FOR_nothing)
2464 struct expand_operand ops[1];
2465 rtx last = get_last_insn ();
2466 tree orig_arg = arg;
2468 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2469 need to expand the argument again. This way, we will not perform
2470 side-effects more the once. */
2471 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2473 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2475 if (mode != GET_MODE (op0))
2476 op0 = convert_to_mode (mode, op0, 0);
2478 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2479 if (maybe_legitimize_operands (icode, 0, 1, ops)
2480 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2481 return ops[0].value;
2483 delete_insns_since (last);
2484 CALL_EXPR_ARG (exp, 0) = orig_arg;
2487 return NULL_RTX;
2490 /* Expand a call to the builtin sincos math function.
2491 Return NULL_RTX if a normal call should be emitted rather than expanding the
2492 function in-line. EXP is the expression that is a call to the builtin
2493 function. */
2495 static rtx
2496 expand_builtin_sincos (tree exp)
2498 rtx op0, op1, op2, target1, target2;
2499 enum machine_mode mode;
2500 tree arg, sinp, cosp;
2501 int result;
2502 location_t loc = EXPR_LOCATION (exp);
2503 tree alias_type, alias_off;
2505 if (!validate_arglist (exp, REAL_TYPE,
2506 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2507 return NULL_RTX;
2509 arg = CALL_EXPR_ARG (exp, 0);
2510 sinp = CALL_EXPR_ARG (exp, 1);
2511 cosp = CALL_EXPR_ARG (exp, 2);
2513 /* Make a suitable register to place result in. */
2514 mode = TYPE_MODE (TREE_TYPE (arg));
2516 /* Check if sincos insn is available, otherwise emit the call. */
2517 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2518 return NULL_RTX;
2520 target1 = gen_reg_rtx (mode);
2521 target2 = gen_reg_rtx (mode);
2523 op0 = expand_normal (arg);
2524 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2525 alias_off = build_int_cst (alias_type, 0);
2526 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2527 sinp, alias_off));
2528 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2529 cosp, alias_off));
2531 /* Compute into target1 and target2.
2532 Set TARGET to wherever the result comes back. */
2533 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2534 gcc_assert (result);
2536 /* Move target1 and target2 to the memory locations indicated
2537 by op1 and op2. */
2538 emit_move_insn (op1, target1);
2539 emit_move_insn (op2, target2);
2541 return const0_rtx;
2544 /* Expand a call to the internal cexpi builtin to the sincos math function.
2545 EXP is the expression that is a call to the builtin function; if convenient,
2546 the result should be placed in TARGET. */
2548 static rtx
2549 expand_builtin_cexpi (tree exp, rtx target)
2551 tree fndecl = get_callee_fndecl (exp);
2552 tree arg, type;
2553 enum machine_mode mode;
2554 rtx op0, op1, op2;
2555 location_t loc = EXPR_LOCATION (exp);
2557 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2558 return NULL_RTX;
2560 arg = CALL_EXPR_ARG (exp, 0);
2561 type = TREE_TYPE (arg);
2562 mode = TYPE_MODE (TREE_TYPE (arg));
2564 /* Try expanding via a sincos optab, fall back to emitting a libcall
2565 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2566 is only generated from sincos, cexp or if we have either of them. */
2567 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2569 op1 = gen_reg_rtx (mode);
2570 op2 = gen_reg_rtx (mode);
2572 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2574 /* Compute into op1 and op2. */
2575 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2577 else if (TARGET_HAS_SINCOS)
2579 tree call, fn = NULL_TREE;
2580 tree top1, top2;
2581 rtx op1a, op2a;
2583 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2584 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2585 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2586 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2587 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2588 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2589 else
2590 gcc_unreachable ();
2592 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2593 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2594 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2595 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2596 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2597 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2599 /* Make sure not to fold the sincos call again. */
2600 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2601 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2602 call, 3, arg, top1, top2));
2604 else
2606 tree call, fn = NULL_TREE, narg;
2607 tree ctype = build_complex_type (type);
2609 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2610 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2611 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2612 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2613 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2614 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2615 else
2616 gcc_unreachable ();
2618 /* If we don't have a decl for cexp create one. This is the
2619 friendliest fallback if the user calls __builtin_cexpi
2620 without full target C99 function support. */
2621 if (fn == NULL_TREE)
2623 tree fntype;
2624 const char *name = NULL;
2626 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2627 name = "cexpf";
2628 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2629 name = "cexp";
2630 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2631 name = "cexpl";
2633 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2634 fn = build_fn_decl (name, fntype);
2637 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2638 build_real (type, dconst0), arg);
2640 /* Make sure not to fold the cexp call again. */
2641 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2642 return expand_expr (build_call_nary (ctype, call, 1, narg),
2643 target, VOIDmode, EXPAND_NORMAL);
2646 /* Now build the proper return type. */
2647 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2648 make_tree (TREE_TYPE (arg), op2),
2649 make_tree (TREE_TYPE (arg), op1)),
2650 target, VOIDmode, EXPAND_NORMAL);
2653 /* Conveniently construct a function call expression. FNDECL names the
2654 function to be called, N is the number of arguments, and the "..."
2655 parameters are the argument expressions. Unlike build_call_exr
2656 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2658 static tree
2659 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2661 va_list ap;
2662 tree fntype = TREE_TYPE (fndecl);
2663 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2665 va_start (ap, n);
2666 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2667 va_end (ap);
2668 SET_EXPR_LOCATION (fn, loc);
2669 return fn;
2672 /* Expand a call to one of the builtin rounding functions gcc defines
2673 as an extension (lfloor and lceil). As these are gcc extensions we
2674 do not need to worry about setting errno to EDOM.
2675 If expanding via optab fails, lower expression to (int)(floor(x)).
2676 EXP is the expression that is a call to the builtin function;
2677 if convenient, the result should be placed in TARGET. */
2679 static rtx
2680 expand_builtin_int_roundingfn (tree exp, rtx target)
2682 convert_optab builtin_optab;
2683 rtx op0, insns, tmp;
2684 tree fndecl = get_callee_fndecl (exp);
2685 enum built_in_function fallback_fn;
2686 tree fallback_fndecl;
2687 enum machine_mode mode;
2688 tree arg;
2690 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2691 gcc_unreachable ();
2693 arg = CALL_EXPR_ARG (exp, 0);
2695 switch (DECL_FUNCTION_CODE (fndecl))
2697 CASE_FLT_FN (BUILT_IN_ICEIL):
2698 CASE_FLT_FN (BUILT_IN_LCEIL):
2699 CASE_FLT_FN (BUILT_IN_LLCEIL):
2700 builtin_optab = lceil_optab;
2701 fallback_fn = BUILT_IN_CEIL;
2702 break;
2704 CASE_FLT_FN (BUILT_IN_IFLOOR):
2705 CASE_FLT_FN (BUILT_IN_LFLOOR):
2706 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2707 builtin_optab = lfloor_optab;
2708 fallback_fn = BUILT_IN_FLOOR;
2709 break;
2711 default:
2712 gcc_unreachable ();
2715 /* Make a suitable register to place result in. */
2716 mode = TYPE_MODE (TREE_TYPE (exp));
2718 target = gen_reg_rtx (mode);
2720 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2721 need to expand the argument again. This way, we will not perform
2722 side-effects more the once. */
2723 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2725 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2727 start_sequence ();
2729 /* Compute into TARGET. */
2730 if (expand_sfix_optab (target, op0, builtin_optab))
2732 /* Output the entire sequence. */
2733 insns = get_insns ();
2734 end_sequence ();
2735 emit_insn (insns);
2736 return target;
2739 /* If we were unable to expand via the builtin, stop the sequence
2740 (without outputting the insns). */
2741 end_sequence ();
2743 /* Fall back to floating point rounding optab. */
2744 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2746 /* For non-C99 targets we may end up without a fallback fndecl here
2747 if the user called __builtin_lfloor directly. In this case emit
2748 a call to the floor/ceil variants nevertheless. This should result
2749 in the best user experience for not full C99 targets. */
2750 if (fallback_fndecl == NULL_TREE)
2752 tree fntype;
2753 const char *name = NULL;
2755 switch (DECL_FUNCTION_CODE (fndecl))
2757 case BUILT_IN_ICEIL:
2758 case BUILT_IN_LCEIL:
2759 case BUILT_IN_LLCEIL:
2760 name = "ceil";
2761 break;
2762 case BUILT_IN_ICEILF:
2763 case BUILT_IN_LCEILF:
2764 case BUILT_IN_LLCEILF:
2765 name = "ceilf";
2766 break;
2767 case BUILT_IN_ICEILL:
2768 case BUILT_IN_LCEILL:
2769 case BUILT_IN_LLCEILL:
2770 name = "ceill";
2771 break;
2772 case BUILT_IN_IFLOOR:
2773 case BUILT_IN_LFLOOR:
2774 case BUILT_IN_LLFLOOR:
2775 name = "floor";
2776 break;
2777 case BUILT_IN_IFLOORF:
2778 case BUILT_IN_LFLOORF:
2779 case BUILT_IN_LLFLOORF:
2780 name = "floorf";
2781 break;
2782 case BUILT_IN_IFLOORL:
2783 case BUILT_IN_LFLOORL:
2784 case BUILT_IN_LLFLOORL:
2785 name = "floorl";
2786 break;
2787 default:
2788 gcc_unreachable ();
2791 fntype = build_function_type_list (TREE_TYPE (arg),
2792 TREE_TYPE (arg), NULL_TREE);
2793 fallback_fndecl = build_fn_decl (name, fntype);
2796 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2798 tmp = expand_normal (exp);
2800 /* Truncate the result of floating point optab to integer
2801 via expand_fix (). */
2802 target = gen_reg_rtx (mode);
2803 expand_fix (target, tmp, 0);
2805 return target;
2808 /* Expand a call to one of the builtin math functions doing integer
2809 conversion (lrint).
2810 Return 0 if a normal call should be emitted rather than expanding the
2811 function in-line. EXP is the expression that is a call to the builtin
2812 function; if convenient, the result should be placed in TARGET. */
2814 static rtx
2815 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2817 convert_optab builtin_optab;
2818 rtx op0, insns;
2819 tree fndecl = get_callee_fndecl (exp);
2820 tree arg;
2821 enum machine_mode mode;
2823 /* There's no easy way to detect the case we need to set EDOM. */
2824 if (flag_errno_math)
2825 return NULL_RTX;
2827 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2828 gcc_unreachable ();
2830 arg = CALL_EXPR_ARG (exp, 0);
2832 switch (DECL_FUNCTION_CODE (fndecl))
2834 CASE_FLT_FN (BUILT_IN_IRINT):
2835 CASE_FLT_FN (BUILT_IN_LRINT):
2836 CASE_FLT_FN (BUILT_IN_LLRINT):
2837 builtin_optab = lrint_optab; break;
2839 CASE_FLT_FN (BUILT_IN_IROUND):
2840 CASE_FLT_FN (BUILT_IN_LROUND):
2841 CASE_FLT_FN (BUILT_IN_LLROUND):
2842 builtin_optab = lround_optab; break;
2844 default:
2845 gcc_unreachable ();
2848 /* Make a suitable register to place result in. */
2849 mode = TYPE_MODE (TREE_TYPE (exp));
2851 target = gen_reg_rtx (mode);
2853 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2854 need to expand the argument again. This way, we will not perform
2855 side-effects more the once. */
2856 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2858 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2860 start_sequence ();
2862 if (expand_sfix_optab (target, op0, builtin_optab))
2864 /* Output the entire sequence. */
2865 insns = get_insns ();
2866 end_sequence ();
2867 emit_insn (insns);
2868 return target;
2871 /* If we were unable to expand via the builtin, stop the sequence
2872 (without outputting the insns) and call to the library function
2873 with the stabilized argument list. */
2874 end_sequence ();
2876 target = expand_call (exp, target, target == const0_rtx);
2878 return target;
2881 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2882 a normal call should be emitted rather than expanding the function
2883 in-line. EXP is the expression that is a call to the builtin
2884 function; if convenient, the result should be placed in TARGET. */
2886 static rtx
2887 expand_builtin_powi (tree exp, rtx target)
2889 tree arg0, arg1;
2890 rtx op0, op1;
2891 enum machine_mode mode;
2892 enum machine_mode mode2;
2894 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2895 return NULL_RTX;
2897 arg0 = CALL_EXPR_ARG (exp, 0);
2898 arg1 = CALL_EXPR_ARG (exp, 1);
2899 mode = TYPE_MODE (TREE_TYPE (exp));
2901 /* Emit a libcall to libgcc. */
2903 /* Mode of the 2nd argument must match that of an int. */
2904 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2906 if (target == NULL_RTX)
2907 target = gen_reg_rtx (mode);
2909 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2910 if (GET_MODE (op0) != mode)
2911 op0 = convert_to_mode (mode, op0, 0);
2912 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2913 if (GET_MODE (op1) != mode2)
2914 op1 = convert_to_mode (mode2, op1, 0);
2916 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2917 target, LCT_CONST, mode, 2,
2918 op0, mode, op1, mode2);
2920 return target;
2923 /* Expand expression EXP which is a call to the strlen builtin. Return
2924 NULL_RTX if we failed the caller should emit a normal call, otherwise
2925 try to get the result in TARGET, if convenient. */
2927 static rtx
2928 expand_builtin_strlen (tree exp, rtx target,
2929 enum machine_mode target_mode)
2931 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2932 return NULL_RTX;
2933 else
2935 struct expand_operand ops[4];
2936 rtx pat;
2937 tree len;
2938 tree src = CALL_EXPR_ARG (exp, 0);
2939 rtx src_reg, before_strlen;
2940 enum machine_mode insn_mode = target_mode;
2941 enum insn_code icode = CODE_FOR_nothing;
2942 unsigned int align;
2944 /* If the length can be computed at compile-time, return it. */
2945 len = c_strlen (src, 0);
2946 if (len)
2947 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2949 /* If the length can be computed at compile-time and is constant
2950 integer, but there are side-effects in src, evaluate
2951 src for side-effects, then return len.
2952 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2953 can be optimized into: i++; x = 3; */
2954 len = c_strlen (src, 1);
2955 if (len && TREE_CODE (len) == INTEGER_CST)
2957 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2958 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2961 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2963 /* If SRC is not a pointer type, don't do this operation inline. */
2964 if (align == 0)
2965 return NULL_RTX;
2967 /* Bail out if we can't compute strlen in the right mode. */
2968 while (insn_mode != VOIDmode)
2970 icode = optab_handler (strlen_optab, insn_mode);
2971 if (icode != CODE_FOR_nothing)
2972 break;
2974 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2976 if (insn_mode == VOIDmode)
2977 return NULL_RTX;
2979 /* Make a place to hold the source address. We will not expand
2980 the actual source until we are sure that the expansion will
2981 not fail -- there are trees that cannot be expanded twice. */
2982 src_reg = gen_reg_rtx (Pmode);
2984 /* Mark the beginning of the strlen sequence so we can emit the
2985 source operand later. */
2986 before_strlen = get_last_insn ();
2988 create_output_operand (&ops[0], target, insn_mode);
2989 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2990 create_integer_operand (&ops[2], 0);
2991 create_integer_operand (&ops[3], align);
2992 if (!maybe_expand_insn (icode, 4, ops))
2993 return NULL_RTX;
2995 /* Now that we are assured of success, expand the source. */
2996 start_sequence ();
2997 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2998 if (pat != src_reg)
3000 #ifdef POINTERS_EXTEND_UNSIGNED
3001 if (GET_MODE (pat) != Pmode)
3002 pat = convert_to_mode (Pmode, pat,
3003 POINTERS_EXTEND_UNSIGNED);
3004 #endif
3005 emit_move_insn (src_reg, pat);
3007 pat = get_insns ();
3008 end_sequence ();
3010 if (before_strlen)
3011 emit_insn_after (pat, before_strlen);
3012 else
3013 emit_insn_before (pat, get_insns ());
3015 /* Return the value in the proper mode for this function. */
3016 if (GET_MODE (ops[0].value) == target_mode)
3017 target = ops[0].value;
3018 else if (target != 0)
3019 convert_move (target, ops[0].value, 0);
3020 else
3021 target = convert_to_mode (target_mode, ops[0].value, 0);
3023 return target;
3027 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3028 bytes from constant string DATA + OFFSET and return it as target
3029 constant. */
3031 static rtx
3032 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3033 enum machine_mode mode)
3035 const char *str = (const char *) data;
3037 gcc_assert (offset >= 0
3038 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3039 <= strlen (str) + 1));
3041 return c_readstr (str + offset, mode);
3044 /* Expand a call EXP to the memcpy builtin.
3045 Return NULL_RTX if we failed, the caller should emit a normal call,
3046 otherwise try to get the result in TARGET, if convenient (and in
3047 mode MODE if that's convenient). */
3049 static rtx
3050 expand_builtin_memcpy (tree exp, rtx target)
3052 if (!validate_arglist (exp,
3053 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3054 return NULL_RTX;
3055 else
3057 tree dest = CALL_EXPR_ARG (exp, 0);
3058 tree src = CALL_EXPR_ARG (exp, 1);
3059 tree len = CALL_EXPR_ARG (exp, 2);
3060 const char *src_str;
3061 unsigned int src_align = get_pointer_alignment (src);
3062 unsigned int dest_align = get_pointer_alignment (dest);
3063 rtx dest_mem, src_mem, dest_addr, len_rtx;
3064 HOST_WIDE_INT expected_size = -1;
3065 unsigned int expected_align = 0;
3067 /* If DEST is not a pointer type, call the normal function. */
3068 if (dest_align == 0)
3069 return NULL_RTX;
3071 /* If either SRC is not a pointer type, don't do this
3072 operation in-line. */
3073 if (src_align == 0)
3074 return NULL_RTX;
3076 if (currently_expanding_gimple_stmt)
3077 stringop_block_profile (currently_expanding_gimple_stmt,
3078 &expected_align, &expected_size);
3080 if (expected_align < dest_align)
3081 expected_align = dest_align;
3082 dest_mem = get_memory_rtx (dest, len);
3083 set_mem_align (dest_mem, dest_align);
3084 len_rtx = expand_normal (len);
3085 src_str = c_getstr (src);
3087 /* If SRC is a string constant and block move would be done
3088 by pieces, we can avoid loading the string from memory
3089 and only stored the computed constants. */
3090 if (src_str
3091 && CONST_INT_P (len_rtx)
3092 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3093 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3094 CONST_CAST (char *, src_str),
3095 dest_align, false))
3097 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3098 builtin_memcpy_read_str,
3099 CONST_CAST (char *, src_str),
3100 dest_align, false, 0);
3101 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3102 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3103 return dest_mem;
3106 src_mem = get_memory_rtx (src, len);
3107 set_mem_align (src_mem, src_align);
3109 /* Copy word part most expediently. */
3110 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3111 CALL_EXPR_TAILCALL (exp)
3112 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3113 expected_align, expected_size);
3115 if (dest_addr == 0)
3117 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3118 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3120 return dest_addr;
3124 /* Expand a call EXP to the mempcpy builtin.
3125 Return NULL_RTX if we failed; the caller should emit a normal call,
3126 otherwise try to get the result in TARGET, if convenient (and in
3127 mode MODE if that's convenient). If ENDP is 0 return the
3128 destination pointer, if ENDP is 1 return the end pointer ala
3129 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3130 stpcpy. */
3132 static rtx
3133 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3135 if (!validate_arglist (exp,
3136 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3137 return NULL_RTX;
3138 else
3140 tree dest = CALL_EXPR_ARG (exp, 0);
3141 tree src = CALL_EXPR_ARG (exp, 1);
3142 tree len = CALL_EXPR_ARG (exp, 2);
3143 return expand_builtin_mempcpy_args (dest, src, len,
3144 target, mode, /*endp=*/ 1);
3148 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3149 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3150 so that this can also be called without constructing an actual CALL_EXPR.
3151 The other arguments and return value are the same as for
3152 expand_builtin_mempcpy. */
3154 static rtx
3155 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3156 rtx target, enum machine_mode mode, int endp)
3158 /* If return value is ignored, transform mempcpy into memcpy. */
3159 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3161 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3162 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3163 dest, src, len);
3164 return expand_expr (result, target, mode, EXPAND_NORMAL);
3166 else
3168 const char *src_str;
3169 unsigned int src_align = get_pointer_alignment (src);
3170 unsigned int dest_align = get_pointer_alignment (dest);
3171 rtx dest_mem, src_mem, len_rtx;
3173 /* If either SRC or DEST is not a pointer type, don't do this
3174 operation in-line. */
3175 if (dest_align == 0 || src_align == 0)
3176 return NULL_RTX;
3178 /* If LEN is not constant, call the normal function. */
3179 if (! host_integerp (len, 1))
3180 return NULL_RTX;
3182 len_rtx = expand_normal (len);
3183 src_str = c_getstr (src);
3185 /* If SRC is a string constant and block move would be done
3186 by pieces, we can avoid loading the string from memory
3187 and only stored the computed constants. */
3188 if (src_str
3189 && CONST_INT_P (len_rtx)
3190 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3191 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3192 CONST_CAST (char *, src_str),
3193 dest_align, false))
3195 dest_mem = get_memory_rtx (dest, len);
3196 set_mem_align (dest_mem, dest_align);
3197 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3198 builtin_memcpy_read_str,
3199 CONST_CAST (char *, src_str),
3200 dest_align, false, endp);
3201 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3202 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3203 return dest_mem;
3206 if (CONST_INT_P (len_rtx)
3207 && can_move_by_pieces (INTVAL (len_rtx),
3208 MIN (dest_align, src_align)))
3210 dest_mem = get_memory_rtx (dest, len);
3211 set_mem_align (dest_mem, dest_align);
3212 src_mem = get_memory_rtx (src, len);
3213 set_mem_align (src_mem, src_align);
3214 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3215 MIN (dest_align, src_align), endp);
3216 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3217 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3218 return dest_mem;
3221 return NULL_RTX;
3225 #ifndef HAVE_movstr
3226 # define HAVE_movstr 0
3227 # define CODE_FOR_movstr CODE_FOR_nothing
3228 #endif
3230 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3231 we failed, the caller should emit a normal call, otherwise try to
3232 get the result in TARGET, if convenient. If ENDP is 0 return the
3233 destination pointer, if ENDP is 1 return the end pointer ala
3234 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3235 stpcpy. */
3237 static rtx
3238 expand_movstr (tree dest, tree src, rtx target, int endp)
3240 struct expand_operand ops[3];
3241 rtx dest_mem;
3242 rtx src_mem;
3244 if (!HAVE_movstr)
3245 return NULL_RTX;
3247 dest_mem = get_memory_rtx (dest, NULL);
3248 src_mem = get_memory_rtx (src, NULL);
3249 if (!endp)
3251 target = force_reg (Pmode, XEXP (dest_mem, 0));
3252 dest_mem = replace_equiv_address (dest_mem, target);
3255 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3256 create_fixed_operand (&ops[1], dest_mem);
3257 create_fixed_operand (&ops[2], src_mem);
3258 expand_insn (CODE_FOR_movstr, 3, ops);
3260 if (endp && target != const0_rtx)
3262 target = ops[0].value;
3263 /* movstr is supposed to set end to the address of the NUL
3264 terminator. If the caller requested a mempcpy-like return value,
3265 adjust it. */
3266 if (endp == 1)
3268 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3269 emit_move_insn (target, force_operand (tem, NULL_RTX));
3272 return target;
3275 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3276 NULL_RTX if we failed the caller should emit a normal call, otherwise
3277 try to get the result in TARGET, if convenient (and in mode MODE if that's
3278 convenient). */
3280 static rtx
3281 expand_builtin_strcpy (tree exp, rtx target)
3283 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3285 tree dest = CALL_EXPR_ARG (exp, 0);
3286 tree src = CALL_EXPR_ARG (exp, 1);
3287 return expand_builtin_strcpy_args (dest, src, target);
3289 return NULL_RTX;
3292 /* Helper function to do the actual work for expand_builtin_strcpy. The
3293 arguments to the builtin_strcpy call DEST and SRC are broken out
3294 so that this can also be called without constructing an actual CALL_EXPR.
3295 The other arguments and return value are the same as for
3296 expand_builtin_strcpy. */
3298 static rtx
3299 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3301 return expand_movstr (dest, src, target, /*endp=*/0);
3304 /* Expand a call EXP to the stpcpy builtin.
3305 Return NULL_RTX if we failed the caller should emit a normal call,
3306 otherwise try to get the result in TARGET, if convenient (and in
3307 mode MODE if that's convenient). */
3309 static rtx
3310 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3312 tree dst, src;
3313 location_t loc = EXPR_LOCATION (exp);
3315 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3316 return NULL_RTX;
3318 dst = CALL_EXPR_ARG (exp, 0);
3319 src = CALL_EXPR_ARG (exp, 1);
3321 /* If return value is ignored, transform stpcpy into strcpy. */
3322 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3324 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3325 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3326 return expand_expr (result, target, mode, EXPAND_NORMAL);
3328 else
3330 tree len, lenp1;
3331 rtx ret;
3333 /* Ensure we get an actual string whose length can be evaluated at
3334 compile-time, not an expression containing a string. This is
3335 because the latter will potentially produce pessimized code
3336 when used to produce the return value. */
3337 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3338 return expand_movstr (dst, src, target, /*endp=*/2);
3340 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3341 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3342 target, mode, /*endp=*/2);
3344 if (ret)
3345 return ret;
3347 if (TREE_CODE (len) == INTEGER_CST)
3349 rtx len_rtx = expand_normal (len);
3351 if (CONST_INT_P (len_rtx))
3353 ret = expand_builtin_strcpy_args (dst, src, target);
3355 if (ret)
3357 if (! target)
3359 if (mode != VOIDmode)
3360 target = gen_reg_rtx (mode);
3361 else
3362 target = gen_reg_rtx (GET_MODE (ret));
3364 if (GET_MODE (target) != GET_MODE (ret))
3365 ret = gen_lowpart (GET_MODE (target), ret);
3367 ret = plus_constant (ret, INTVAL (len_rtx));
3368 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3369 gcc_assert (ret);
3371 return target;
3376 return expand_movstr (dst, src, target, /*endp=*/2);
3380 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3381 bytes from constant string DATA + OFFSET and return it as target
3382 constant. */
3385 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3386 enum machine_mode mode)
3388 const char *str = (const char *) data;
3390 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3391 return const0_rtx;
3393 return c_readstr (str + offset, mode);
3396 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3397 NULL_RTX if we failed the caller should emit a normal call. */
3399 static rtx
3400 expand_builtin_strncpy (tree exp, rtx target)
3402 location_t loc = EXPR_LOCATION (exp);
3404 if (validate_arglist (exp,
3405 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3407 tree dest = CALL_EXPR_ARG (exp, 0);
3408 tree src = CALL_EXPR_ARG (exp, 1);
3409 tree len = CALL_EXPR_ARG (exp, 2);
3410 tree slen = c_strlen (src, 1);
3412 /* We must be passed a constant len and src parameter. */
3413 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3414 return NULL_RTX;
3416 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3418 /* We're required to pad with trailing zeros if the requested
3419 len is greater than strlen(s2)+1. In that case try to
3420 use store_by_pieces, if it fails, punt. */
3421 if (tree_int_cst_lt (slen, len))
3423 unsigned int dest_align = get_pointer_alignment (dest);
3424 const char *p = c_getstr (src);
3425 rtx dest_mem;
3427 if (!p || dest_align == 0 || !host_integerp (len, 1)
3428 || !can_store_by_pieces (tree_low_cst (len, 1),
3429 builtin_strncpy_read_str,
3430 CONST_CAST (char *, p),
3431 dest_align, false))
3432 return NULL_RTX;
3434 dest_mem = get_memory_rtx (dest, len);
3435 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3436 builtin_strncpy_read_str,
3437 CONST_CAST (char *, p), dest_align, false, 0);
3438 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3439 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3440 return dest_mem;
3443 return NULL_RTX;
3446 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3447 bytes from constant string DATA + OFFSET and return it as target
3448 constant. */
3451 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3452 enum machine_mode mode)
3454 const char *c = (const char *) data;
3455 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3457 memset (p, *c, GET_MODE_SIZE (mode));
3459 return c_readstr (p, mode);
3462 /* Callback routine for store_by_pieces. Return the RTL of a register
3463 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3464 char value given in the RTL register data. For example, if mode is
3465 4 bytes wide, return the RTL for 0x01010101*data. */
3467 static rtx
3468 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3469 enum machine_mode mode)
3471 rtx target, coeff;
3472 size_t size;
3473 char *p;
3475 size = GET_MODE_SIZE (mode);
3476 if (size == 1)
3477 return (rtx) data;
3479 p = XALLOCAVEC (char, size);
3480 memset (p, 1, size);
3481 coeff = c_readstr (p, mode);
3483 target = convert_to_mode (mode, (rtx) data, 1);
3484 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3485 return force_reg (mode, target);
3488 /* Expand expression EXP, which is a call to the memset builtin. Return
3489 NULL_RTX if we failed the caller should emit a normal call, otherwise
3490 try to get the result in TARGET, if convenient (and in mode MODE if that's
3491 convenient). */
3493 static rtx
3494 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3496 if (!validate_arglist (exp,
3497 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3498 return NULL_RTX;
3499 else
3501 tree dest = CALL_EXPR_ARG (exp, 0);
3502 tree val = CALL_EXPR_ARG (exp, 1);
3503 tree len = CALL_EXPR_ARG (exp, 2);
3504 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3508 /* Helper function to do the actual work for expand_builtin_memset. The
3509 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3510 so that this can also be called without constructing an actual CALL_EXPR.
3511 The other arguments and return value are the same as for
3512 expand_builtin_memset. */
3514 static rtx
3515 expand_builtin_memset_args (tree dest, tree val, tree len,
3516 rtx target, enum machine_mode mode, tree orig_exp)
3518 tree fndecl, fn;
3519 enum built_in_function fcode;
3520 enum machine_mode val_mode;
3521 char c;
3522 unsigned int dest_align;
3523 rtx dest_mem, dest_addr, len_rtx;
3524 HOST_WIDE_INT expected_size = -1;
3525 unsigned int expected_align = 0;
3527 dest_align = get_pointer_alignment (dest);
3529 /* If DEST is not a pointer type, don't do this operation in-line. */
3530 if (dest_align == 0)
3531 return NULL_RTX;
3533 if (currently_expanding_gimple_stmt)
3534 stringop_block_profile (currently_expanding_gimple_stmt,
3535 &expected_align, &expected_size);
3537 if (expected_align < dest_align)
3538 expected_align = dest_align;
3540 /* If the LEN parameter is zero, return DEST. */
3541 if (integer_zerop (len))
3543 /* Evaluate and ignore VAL in case it has side-effects. */
3544 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3545 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3548 /* Stabilize the arguments in case we fail. */
3549 dest = builtin_save_expr (dest);
3550 val = builtin_save_expr (val);
3551 len = builtin_save_expr (len);
3553 len_rtx = expand_normal (len);
3554 dest_mem = get_memory_rtx (dest, len);
3555 val_mode = TYPE_MODE (unsigned_char_type_node);
3557 if (TREE_CODE (val) != INTEGER_CST)
3559 rtx val_rtx;
3561 val_rtx = expand_normal (val);
3562 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3564 /* Assume that we can memset by pieces if we can store
3565 * the coefficients by pieces (in the required modes).
3566 * We can't pass builtin_memset_gen_str as that emits RTL. */
3567 c = 1;
3568 if (host_integerp (len, 1)
3569 && can_store_by_pieces (tree_low_cst (len, 1),
3570 builtin_memset_read_str, &c, dest_align,
3571 true))
3573 val_rtx = force_reg (val_mode, val_rtx);
3574 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3575 builtin_memset_gen_str, val_rtx, dest_align,
3576 true, 0);
3578 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3579 dest_align, expected_align,
3580 expected_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 if (target_char_cast (val, &c))
3589 goto do_libcall;
3591 if (c)
3593 if (host_integerp (len, 1)
3594 && can_store_by_pieces (tree_low_cst (len, 1),
3595 builtin_memset_read_str, &c, dest_align,
3596 true))
3597 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3598 builtin_memset_read_str, &c, dest_align, true, 0);
3599 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3600 gen_int_mode (c, val_mode),
3601 dest_align, expected_align,
3602 expected_size))
3603 goto do_libcall;
3605 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3606 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3607 return dest_mem;
3610 set_mem_align (dest_mem, dest_align);
3611 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3612 CALL_EXPR_TAILCALL (orig_exp)
3613 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3614 expected_align, expected_size);
3616 if (dest_addr == 0)
3618 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3619 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3622 return dest_addr;
3624 do_libcall:
3625 fndecl = get_callee_fndecl (orig_exp);
3626 fcode = DECL_FUNCTION_CODE (fndecl);
3627 if (fcode == BUILT_IN_MEMSET)
3628 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3629 dest, val, len);
3630 else if (fcode == BUILT_IN_BZERO)
3631 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3632 dest, len);
3633 else
3634 gcc_unreachable ();
3635 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3636 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3637 return expand_call (fn, target, target == const0_rtx);
3640 /* Expand expression EXP, which is a call to the bzero builtin. Return
3641 NULL_RTX if we failed the caller should emit a normal call. */
3643 static rtx
3644 expand_builtin_bzero (tree exp)
3646 tree dest, size;
3647 location_t loc = EXPR_LOCATION (exp);
3649 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3650 return NULL_RTX;
3652 dest = CALL_EXPR_ARG (exp, 0);
3653 size = CALL_EXPR_ARG (exp, 1);
3655 /* New argument list transforming bzero(ptr x, int y) to
3656 memset(ptr x, int 0, size_t y). This is done this way
3657 so that if it isn't expanded inline, we fallback to
3658 calling bzero instead of memset. */
3660 return expand_builtin_memset_args (dest, integer_zero_node,
3661 fold_convert_loc (loc,
3662 size_type_node, size),
3663 const0_rtx, VOIDmode, exp);
3666 /* Expand expression EXP, which is a call to the memcmp built-in function.
3667 Return NULL_RTX if we failed and the caller should emit a normal call,
3668 otherwise try to get the result in TARGET, if convenient (and in mode
3669 MODE, if that's convenient). */
3671 static rtx
3672 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3673 ATTRIBUTE_UNUSED enum machine_mode mode)
3675 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3677 if (!validate_arglist (exp,
3678 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3679 return NULL_RTX;
3681 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3682 implementing memcmp because it will stop if it encounters two
3683 zero bytes. */
3684 #if defined HAVE_cmpmemsi
3686 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3687 rtx result;
3688 rtx insn;
3689 tree arg1 = CALL_EXPR_ARG (exp, 0);
3690 tree arg2 = CALL_EXPR_ARG (exp, 1);
3691 tree len = CALL_EXPR_ARG (exp, 2);
3693 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3694 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3695 enum machine_mode insn_mode;
3697 if (HAVE_cmpmemsi)
3698 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3699 else
3700 return NULL_RTX;
3702 /* If we don't have POINTER_TYPE, call the function. */
3703 if (arg1_align == 0 || arg2_align == 0)
3704 return NULL_RTX;
3706 /* Make a place to write the result of the instruction. */
3707 result = target;
3708 if (! (result != 0
3709 && REG_P (result) && GET_MODE (result) == insn_mode
3710 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3711 result = gen_reg_rtx (insn_mode);
3713 arg1_rtx = get_memory_rtx (arg1, len);
3714 arg2_rtx = get_memory_rtx (arg2, len);
3715 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3717 /* Set MEM_SIZE as appropriate. */
3718 if (CONST_INT_P (arg3_rtx))
3720 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3721 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3724 if (HAVE_cmpmemsi)
3725 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3726 GEN_INT (MIN (arg1_align, arg2_align)));
3727 else
3728 gcc_unreachable ();
3730 if (insn)
3731 emit_insn (insn);
3732 else
3733 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3734 TYPE_MODE (integer_type_node), 3,
3735 XEXP (arg1_rtx, 0), Pmode,
3736 XEXP (arg2_rtx, 0), Pmode,
3737 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3738 TYPE_UNSIGNED (sizetype)),
3739 TYPE_MODE (sizetype));
3741 /* Return the value in the proper mode for this function. */
3742 mode = TYPE_MODE (TREE_TYPE (exp));
3743 if (GET_MODE (result) == mode)
3744 return result;
3745 else if (target != 0)
3747 convert_move (target, result, 0);
3748 return target;
3750 else
3751 return convert_to_mode (mode, result, 0);
3753 #endif /* HAVE_cmpmemsi. */
3755 return NULL_RTX;
3758 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3759 if we failed the caller should emit a normal call, otherwise try to get
3760 the result in TARGET, if convenient. */
3762 static rtx
3763 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3765 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3766 return NULL_RTX;
3768 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3769 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3770 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3772 rtx arg1_rtx, arg2_rtx;
3773 rtx result, insn = NULL_RTX;
3774 tree fndecl, fn;
3775 tree arg1 = CALL_EXPR_ARG (exp, 0);
3776 tree arg2 = CALL_EXPR_ARG (exp, 1);
3778 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3779 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3781 /* If we don't have POINTER_TYPE, call the function. */
3782 if (arg1_align == 0 || arg2_align == 0)
3783 return NULL_RTX;
3785 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3786 arg1 = builtin_save_expr (arg1);
3787 arg2 = builtin_save_expr (arg2);
3789 arg1_rtx = get_memory_rtx (arg1, NULL);
3790 arg2_rtx = get_memory_rtx (arg2, NULL);
3792 #ifdef HAVE_cmpstrsi
3793 /* Try to call cmpstrsi. */
3794 if (HAVE_cmpstrsi)
3796 enum machine_mode insn_mode
3797 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3799 /* Make a place to write the result of the instruction. */
3800 result = target;
3801 if (! (result != 0
3802 && REG_P (result) && GET_MODE (result) == insn_mode
3803 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3804 result = gen_reg_rtx (insn_mode);
3806 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3807 GEN_INT (MIN (arg1_align, arg2_align)));
3809 #endif
3810 #ifdef HAVE_cmpstrnsi
3811 /* Try to determine at least one length and call cmpstrnsi. */
3812 if (!insn && HAVE_cmpstrnsi)
3814 tree len;
3815 rtx arg3_rtx;
3817 enum machine_mode insn_mode
3818 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3819 tree len1 = c_strlen (arg1, 1);
3820 tree len2 = c_strlen (arg2, 1);
3822 if (len1)
3823 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3824 if (len2)
3825 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3827 /* If we don't have a constant length for the first, use the length
3828 of the second, if we know it. We don't require a constant for
3829 this case; some cost analysis could be done if both are available
3830 but neither is constant. For now, assume they're equally cheap,
3831 unless one has side effects. If both strings have constant lengths,
3832 use the smaller. */
3834 if (!len1)
3835 len = len2;
3836 else if (!len2)
3837 len = len1;
3838 else if (TREE_SIDE_EFFECTS (len1))
3839 len = len2;
3840 else if (TREE_SIDE_EFFECTS (len2))
3841 len = len1;
3842 else if (TREE_CODE (len1) != INTEGER_CST)
3843 len = len2;
3844 else if (TREE_CODE (len2) != INTEGER_CST)
3845 len = len1;
3846 else if (tree_int_cst_lt (len1, len2))
3847 len = len1;
3848 else
3849 len = len2;
3851 /* If both arguments have side effects, we cannot optimize. */
3852 if (!len || TREE_SIDE_EFFECTS (len))
3853 goto do_libcall;
3855 arg3_rtx = expand_normal (len);
3857 /* Make a place to write the result of the instruction. */
3858 result = target;
3859 if (! (result != 0
3860 && REG_P (result) && GET_MODE (result) == insn_mode
3861 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3862 result = gen_reg_rtx (insn_mode);
3864 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3865 GEN_INT (MIN (arg1_align, arg2_align)));
3867 #endif
3869 if (insn)
3871 enum machine_mode mode;
3872 emit_insn (insn);
3874 /* Return the value in the proper mode for this function. */
3875 mode = TYPE_MODE (TREE_TYPE (exp));
3876 if (GET_MODE (result) == mode)
3877 return result;
3878 if (target == 0)
3879 return convert_to_mode (mode, result, 0);
3880 convert_move (target, result, 0);
3881 return target;
3884 /* Expand the library call ourselves using a stabilized argument
3885 list to avoid re-evaluating the function's arguments twice. */
3886 #ifdef HAVE_cmpstrnsi
3887 do_libcall:
3888 #endif
3889 fndecl = get_callee_fndecl (exp);
3890 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3891 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3892 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3893 return expand_call (fn, target, target == const0_rtx);
3895 #endif
3896 return NULL_RTX;
3899 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3900 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3901 the result in TARGET, if convenient. */
3903 static rtx
3904 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3905 ATTRIBUTE_UNUSED enum machine_mode mode)
3907 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3909 if (!validate_arglist (exp,
3910 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3911 return NULL_RTX;
3913 /* If c_strlen can determine an expression for one of the string
3914 lengths, and it doesn't have side effects, then emit cmpstrnsi
3915 using length MIN(strlen(string)+1, arg3). */
3916 #ifdef HAVE_cmpstrnsi
3917 if (HAVE_cmpstrnsi)
3919 tree len, len1, len2;
3920 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3921 rtx result, insn;
3922 tree fndecl, fn;
3923 tree arg1 = CALL_EXPR_ARG (exp, 0);
3924 tree arg2 = CALL_EXPR_ARG (exp, 1);
3925 tree arg3 = CALL_EXPR_ARG (exp, 2);
3927 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3928 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3929 enum machine_mode insn_mode
3930 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3932 len1 = c_strlen (arg1, 1);
3933 len2 = c_strlen (arg2, 1);
3935 if (len1)
3936 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3937 if (len2)
3938 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3940 /* If we don't have a constant length for the first, use the length
3941 of the second, if we know it. We don't require a constant for
3942 this case; some cost analysis could be done if both are available
3943 but neither is constant. For now, assume they're equally cheap,
3944 unless one has side effects. If both strings have constant lengths,
3945 use the smaller. */
3947 if (!len1)
3948 len = len2;
3949 else if (!len2)
3950 len = len1;
3951 else if (TREE_SIDE_EFFECTS (len1))
3952 len = len2;
3953 else if (TREE_SIDE_EFFECTS (len2))
3954 len = len1;
3955 else if (TREE_CODE (len1) != INTEGER_CST)
3956 len = len2;
3957 else if (TREE_CODE (len2) != INTEGER_CST)
3958 len = len1;
3959 else if (tree_int_cst_lt (len1, len2))
3960 len = len1;
3961 else
3962 len = len2;
3964 /* If both arguments have side effects, we cannot optimize. */
3965 if (!len || TREE_SIDE_EFFECTS (len))
3966 return NULL_RTX;
3968 /* The actual new length parameter is MIN(len,arg3). */
3969 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3970 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3972 /* If we don't have POINTER_TYPE, call the function. */
3973 if (arg1_align == 0 || arg2_align == 0)
3974 return NULL_RTX;
3976 /* Make a place to write the result of the instruction. */
3977 result = target;
3978 if (! (result != 0
3979 && REG_P (result) && GET_MODE (result) == insn_mode
3980 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3981 result = gen_reg_rtx (insn_mode);
3983 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3984 arg1 = builtin_save_expr (arg1);
3985 arg2 = builtin_save_expr (arg2);
3986 len = builtin_save_expr (len);
3988 arg1_rtx = get_memory_rtx (arg1, len);
3989 arg2_rtx = get_memory_rtx (arg2, len);
3990 arg3_rtx = expand_normal (len);
3991 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3992 GEN_INT (MIN (arg1_align, arg2_align)));
3993 if (insn)
3995 emit_insn (insn);
3997 /* Return the value in the proper mode for this function. */
3998 mode = TYPE_MODE (TREE_TYPE (exp));
3999 if (GET_MODE (result) == mode)
4000 return result;
4001 if (target == 0)
4002 return convert_to_mode (mode, result, 0);
4003 convert_move (target, result, 0);
4004 return target;
4007 /* Expand the library call ourselves using a stabilized argument
4008 list to avoid re-evaluating the function's arguments twice. */
4009 fndecl = get_callee_fndecl (exp);
4010 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4011 arg1, arg2, len);
4012 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4013 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4014 return expand_call (fn, target, target == const0_rtx);
4016 #endif
4017 return NULL_RTX;
4020 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4021 if that's convenient. */
4024 expand_builtin_saveregs (void)
4026 rtx val, seq;
4028 /* Don't do __builtin_saveregs more than once in a function.
4029 Save the result of the first call and reuse it. */
4030 if (saveregs_value != 0)
4031 return saveregs_value;
4033 /* When this function is called, it means that registers must be
4034 saved on entry to this function. So we migrate the call to the
4035 first insn of this function. */
4037 start_sequence ();
4039 /* Do whatever the machine needs done in this case. */
4040 val = targetm.calls.expand_builtin_saveregs ();
4042 seq = get_insns ();
4043 end_sequence ();
4045 saveregs_value = val;
4047 /* Put the insns after the NOTE that starts the function. If this
4048 is inside a start_sequence, make the outer-level insn chain current, so
4049 the code is placed at the start of the function. */
4050 push_topmost_sequence ();
4051 emit_insn_after (seq, entry_of_function ());
4052 pop_topmost_sequence ();
4054 return val;
4057 /* Expand a call to __builtin_next_arg. */
4059 static rtx
4060 expand_builtin_next_arg (void)
4062 /* Checking arguments is already done in fold_builtin_next_arg
4063 that must be called before this function. */
4064 return expand_binop (ptr_mode, add_optab,
4065 crtl->args.internal_arg_pointer,
4066 crtl->args.arg_offset_rtx,
4067 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4070 /* Make it easier for the backends by protecting the valist argument
4071 from multiple evaluations. */
4073 static tree
4074 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4076 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4078 /* The current way of determining the type of valist is completely
4079 bogus. We should have the information on the va builtin instead. */
4080 if (!vatype)
4081 vatype = targetm.fn_abi_va_list (cfun->decl);
4083 if (TREE_CODE (vatype) == ARRAY_TYPE)
4085 if (TREE_SIDE_EFFECTS (valist))
4086 valist = save_expr (valist);
4088 /* For this case, the backends will be expecting a pointer to
4089 vatype, but it's possible we've actually been given an array
4090 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4091 So fix it. */
4092 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4094 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4095 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4098 else
4100 tree pt = build_pointer_type (vatype);
4102 if (! needs_lvalue)
4104 if (! TREE_SIDE_EFFECTS (valist))
4105 return valist;
4107 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4108 TREE_SIDE_EFFECTS (valist) = 1;
4111 if (TREE_SIDE_EFFECTS (valist))
4112 valist = save_expr (valist);
4113 valist = fold_build2_loc (loc, MEM_REF,
4114 vatype, valist, build_int_cst (pt, 0));
4117 return valist;
4120 /* The "standard" definition of va_list is void*. */
4122 tree
4123 std_build_builtin_va_list (void)
4125 return ptr_type_node;
4128 /* The "standard" abi va_list is va_list_type_node. */
4130 tree
4131 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4133 return va_list_type_node;
4136 /* The "standard" type of va_list is va_list_type_node. */
4138 tree
4139 std_canonical_va_list_type (tree type)
4141 tree wtype, htype;
4143 if (INDIRECT_REF_P (type))
4144 type = TREE_TYPE (type);
4145 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4146 type = TREE_TYPE (type);
4147 wtype = va_list_type_node;
4148 htype = type;
4149 /* Treat structure va_list types. */
4150 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4151 htype = TREE_TYPE (htype);
4152 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4154 /* If va_list is an array type, the argument may have decayed
4155 to a pointer type, e.g. by being passed to another function.
4156 In that case, unwrap both types so that we can compare the
4157 underlying records. */
4158 if (TREE_CODE (htype) == ARRAY_TYPE
4159 || POINTER_TYPE_P (htype))
4161 wtype = TREE_TYPE (wtype);
4162 htype = TREE_TYPE (htype);
4165 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4166 return va_list_type_node;
4168 return NULL_TREE;
4171 /* The "standard" implementation of va_start: just assign `nextarg' to
4172 the variable. */
4174 void
4175 std_expand_builtin_va_start (tree valist, rtx nextarg)
4177 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4178 convert_move (va_r, nextarg, 0);
4181 /* Expand EXP, a call to __builtin_va_start. */
4183 static rtx
4184 expand_builtin_va_start (tree exp)
4186 rtx nextarg;
4187 tree valist;
4188 location_t loc = EXPR_LOCATION (exp);
4190 if (call_expr_nargs (exp) < 2)
4192 error_at (loc, "too few arguments to function %<va_start%>");
4193 return const0_rtx;
4196 if (fold_builtin_next_arg (exp, true))
4197 return const0_rtx;
4199 nextarg = expand_builtin_next_arg ();
4200 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4202 if (targetm.expand_builtin_va_start)
4203 targetm.expand_builtin_va_start (valist, nextarg);
4204 else
4205 std_expand_builtin_va_start (valist, nextarg);
4207 return const0_rtx;
4210 /* The "standard" implementation of va_arg: read the value from the
4211 current (padded) address and increment by the (padded) size. */
4213 tree
4214 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4215 gimple_seq *post_p)
4217 tree addr, t, type_size, rounded_size, valist_tmp;
4218 unsigned HOST_WIDE_INT align, boundary;
4219 bool indirect;
4221 #ifdef ARGS_GROW_DOWNWARD
4222 /* All of the alignment and movement below is for args-grow-up machines.
4223 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4224 implement their own specialized gimplify_va_arg_expr routines. */
4225 gcc_unreachable ();
4226 #endif
4228 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4229 if (indirect)
4230 type = build_pointer_type (type);
4232 align = PARM_BOUNDARY / BITS_PER_UNIT;
4233 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4235 /* When we align parameter on stack for caller, if the parameter
4236 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4237 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4238 here with caller. */
4239 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4240 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4242 boundary /= BITS_PER_UNIT;
4244 /* Hoist the valist value into a temporary for the moment. */
4245 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4247 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4248 requires greater alignment, we must perform dynamic alignment. */
4249 if (boundary > align
4250 && !integer_zerop (TYPE_SIZE (type)))
4252 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4253 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4254 gimplify_and_add (t, pre_p);
4256 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4257 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4258 valist_tmp,
4259 build_int_cst (TREE_TYPE (valist), -boundary)));
4260 gimplify_and_add (t, pre_p);
4262 else
4263 boundary = align;
4265 /* If the actual alignment is less than the alignment of the type,
4266 adjust the type accordingly so that we don't assume strict alignment
4267 when dereferencing the pointer. */
4268 boundary *= BITS_PER_UNIT;
4269 if (boundary < TYPE_ALIGN (type))
4271 type = build_variant_type_copy (type);
4272 TYPE_ALIGN (type) = boundary;
4275 /* Compute the rounded size of the type. */
4276 type_size = size_in_bytes (type);
4277 rounded_size = round_up (type_size, align);
4279 /* Reduce rounded_size so it's sharable with the postqueue. */
4280 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4282 /* Get AP. */
4283 addr = valist_tmp;
4284 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4286 /* Small args are padded downward. */
4287 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4288 rounded_size, size_int (align));
4289 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4290 size_binop (MINUS_EXPR, rounded_size, type_size));
4291 addr = fold_build_pointer_plus (addr, t);
4294 /* Compute new value for AP. */
4295 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4296 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4297 gimplify_and_add (t, pre_p);
4299 addr = fold_convert (build_pointer_type (type), addr);
4301 if (indirect)
4302 addr = build_va_arg_indirect_ref (addr);
4304 return build_va_arg_indirect_ref (addr);
4307 /* Build an indirect-ref expression over the given TREE, which represents a
4308 piece of a va_arg() expansion. */
4309 tree
4310 build_va_arg_indirect_ref (tree addr)
4312 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4314 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4315 mf_mark (addr);
4317 return addr;
4320 /* Return a dummy expression of type TYPE in order to keep going after an
4321 error. */
4323 static tree
4324 dummy_object (tree type)
4326 tree t = build_int_cst (build_pointer_type (type), 0);
4327 return build2 (MEM_REF, type, t, t);
4330 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4331 builtin function, but a very special sort of operator. */
4333 enum gimplify_status
4334 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4336 tree promoted_type, have_va_type;
4337 tree valist = TREE_OPERAND (*expr_p, 0);
4338 tree type = TREE_TYPE (*expr_p);
4339 tree t;
4340 location_t loc = EXPR_LOCATION (*expr_p);
4342 /* Verify that valist is of the proper type. */
4343 have_va_type = TREE_TYPE (valist);
4344 if (have_va_type == error_mark_node)
4345 return GS_ERROR;
4346 have_va_type = targetm.canonical_va_list_type (have_va_type);
4348 if (have_va_type == NULL_TREE)
4350 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4351 return GS_ERROR;
4354 /* Generate a diagnostic for requesting data of a type that cannot
4355 be passed through `...' due to type promotion at the call site. */
4356 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4357 != type)
4359 static bool gave_help;
4360 bool warned;
4362 /* Unfortunately, this is merely undefined, rather than a constraint
4363 violation, so we cannot make this an error. If this call is never
4364 executed, the program is still strictly conforming. */
4365 warned = warning_at (loc, 0,
4366 "%qT is promoted to %qT when passed through %<...%>",
4367 type, promoted_type);
4368 if (!gave_help && warned)
4370 gave_help = true;
4371 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4372 promoted_type, type);
4375 /* We can, however, treat "undefined" any way we please.
4376 Call abort to encourage the user to fix the program. */
4377 if (warned)
4378 inform (loc, "if this code is reached, the program will abort");
4379 /* Before the abort, allow the evaluation of the va_list
4380 expression to exit or longjmp. */
4381 gimplify_and_add (valist, pre_p);
4382 t = build_call_expr_loc (loc,
4383 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4384 gimplify_and_add (t, pre_p);
4386 /* This is dead code, but go ahead and finish so that the
4387 mode of the result comes out right. */
4388 *expr_p = dummy_object (type);
4389 return GS_ALL_DONE;
4391 else
4393 /* Make it easier for the backends by protecting the valist argument
4394 from multiple evaluations. */
4395 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4397 /* For this case, the backends will be expecting a pointer to
4398 TREE_TYPE (abi), but it's possible we've
4399 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4400 So fix it. */
4401 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4403 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4404 valist = fold_convert_loc (loc, p1,
4405 build_fold_addr_expr_loc (loc, valist));
4408 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4410 else
4411 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4413 if (!targetm.gimplify_va_arg_expr)
4414 /* FIXME: Once most targets are converted we should merely
4415 assert this is non-null. */
4416 return GS_ALL_DONE;
4418 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4419 return GS_OK;
4423 /* Expand EXP, a call to __builtin_va_end. */
4425 static rtx
4426 expand_builtin_va_end (tree exp)
4428 tree valist = CALL_EXPR_ARG (exp, 0);
4430 /* Evaluate for side effects, if needed. I hate macros that don't
4431 do that. */
4432 if (TREE_SIDE_EFFECTS (valist))
4433 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4435 return const0_rtx;
4438 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4439 builtin rather than just as an assignment in stdarg.h because of the
4440 nastiness of array-type va_list types. */
4442 static rtx
4443 expand_builtin_va_copy (tree exp)
4445 tree dst, src, t;
4446 location_t loc = EXPR_LOCATION (exp);
4448 dst = CALL_EXPR_ARG (exp, 0);
4449 src = CALL_EXPR_ARG (exp, 1);
4451 dst = stabilize_va_list_loc (loc, dst, 1);
4452 src = stabilize_va_list_loc (loc, src, 0);
4454 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4456 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4458 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4459 TREE_SIDE_EFFECTS (t) = 1;
4460 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4462 else
4464 rtx dstb, srcb, size;
4466 /* Evaluate to pointers. */
4467 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4468 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4469 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4470 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4472 dstb = convert_memory_address (Pmode, dstb);
4473 srcb = convert_memory_address (Pmode, srcb);
4475 /* "Dereference" to BLKmode memories. */
4476 dstb = gen_rtx_MEM (BLKmode, dstb);
4477 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4478 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4479 srcb = gen_rtx_MEM (BLKmode, srcb);
4480 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4481 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4483 /* Copy. */
4484 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4487 return const0_rtx;
4490 /* Expand a call to one of the builtin functions __builtin_frame_address or
4491 __builtin_return_address. */
4493 static rtx
4494 expand_builtin_frame_address (tree fndecl, tree exp)
4496 /* The argument must be a nonnegative integer constant.
4497 It counts the number of frames to scan up the stack.
4498 The value is the return address saved in that frame. */
4499 if (call_expr_nargs (exp) == 0)
4500 /* Warning about missing arg was already issued. */
4501 return const0_rtx;
4502 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4504 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4505 error ("invalid argument to %<__builtin_frame_address%>");
4506 else
4507 error ("invalid argument to %<__builtin_return_address%>");
4508 return const0_rtx;
4510 else
4512 rtx tem
4513 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4514 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4516 /* Some ports cannot access arbitrary stack frames. */
4517 if (tem == NULL)
4519 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4520 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4521 else
4522 warning (0, "unsupported argument to %<__builtin_return_address%>");
4523 return const0_rtx;
4526 /* For __builtin_frame_address, return what we've got. */
4527 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4528 return tem;
4530 if (!REG_P (tem)
4531 && ! CONSTANT_P (tem))
4532 tem = copy_to_mode_reg (Pmode, tem);
4533 return tem;
4537 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4538 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4539 is the same as for allocate_dynamic_stack_space. */
4541 static rtx
4542 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4544 rtx op0;
4545 rtx result;
4546 bool valid_arglist;
4547 unsigned int align;
4548 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4549 == BUILT_IN_ALLOCA_WITH_ALIGN);
4551 /* Emit normal call if we use mudflap. */
4552 if (flag_mudflap)
4553 return NULL_RTX;
4555 valid_arglist
4556 = (alloca_with_align
4557 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4558 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4560 if (!valid_arglist)
4561 return NULL_RTX;
4563 /* Compute the argument. */
4564 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4566 /* Compute the alignment. */
4567 align = (alloca_with_align
4568 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4569 : BIGGEST_ALIGNMENT);
4571 /* Allocate the desired space. */
4572 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4573 result = convert_memory_address (ptr_mode, result);
4575 return result;
4578 /* Expand a call to a bswap builtin with argument ARG0. MODE
4579 is the mode to expand with. */
4581 static rtx
4582 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4584 enum machine_mode mode;
4585 tree arg;
4586 rtx op0;
4588 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4589 return NULL_RTX;
4591 arg = CALL_EXPR_ARG (exp, 0);
4592 mode = TYPE_MODE (TREE_TYPE (arg));
4593 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4595 target = expand_unop (mode, bswap_optab, op0, target, 1);
4597 gcc_assert (target);
4599 return convert_to_mode (mode, target, 0);
4602 /* Expand a call to a unary builtin in EXP.
4603 Return NULL_RTX if a normal call should be emitted rather than expanding the
4604 function in-line. If convenient, the result should be placed in TARGET.
4605 SUBTARGET may be used as the target for computing one of EXP's operands. */
4607 static rtx
4608 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4609 rtx subtarget, optab op_optab)
4611 rtx op0;
4613 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4614 return NULL_RTX;
4616 /* Compute the argument. */
4617 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4618 (subtarget
4619 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4620 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4621 VOIDmode, EXPAND_NORMAL);
4622 /* Compute op, into TARGET if possible.
4623 Set TARGET to wherever the result comes back. */
4624 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4625 op_optab, op0, target, op_optab != clrsb_optab);
4626 gcc_assert (target);
4628 return convert_to_mode (target_mode, target, 0);
4631 /* Expand a call to __builtin_expect. We just return our argument
4632 as the builtin_expect semantic should've been already executed by
4633 tree branch prediction pass. */
4635 static rtx
4636 expand_builtin_expect (tree exp, rtx target)
4638 tree arg;
4640 if (call_expr_nargs (exp) < 2)
4641 return const0_rtx;
4642 arg = CALL_EXPR_ARG (exp, 0);
4644 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4645 /* When guessing was done, the hints should be already stripped away. */
4646 gcc_assert (!flag_guess_branch_prob
4647 || optimize == 0 || seen_error ());
4648 return target;
4651 /* Expand a call to __builtin_assume_aligned. We just return our first
4652 argument as the builtin_assume_aligned semantic should've been already
4653 executed by CCP. */
4655 static rtx
4656 expand_builtin_assume_aligned (tree exp, rtx target)
4658 if (call_expr_nargs (exp) < 2)
4659 return const0_rtx;
4660 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4661 EXPAND_NORMAL);
4662 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4663 && (call_expr_nargs (exp) < 3
4664 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4665 return target;
4668 void
4669 expand_builtin_trap (void)
4671 #ifdef HAVE_trap
4672 if (HAVE_trap)
4673 emit_insn (gen_trap ());
4674 else
4675 #endif
4676 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4677 emit_barrier ();
4680 /* Expand a call to __builtin_unreachable. We do nothing except emit
4681 a barrier saying that control flow will not pass here.
4683 It is the responsibility of the program being compiled to ensure
4684 that control flow does never reach __builtin_unreachable. */
4685 static void
4686 expand_builtin_unreachable (void)
4688 emit_barrier ();
4691 /* Expand EXP, a call to fabs, fabsf or fabsl.
4692 Return NULL_RTX if a normal call should be emitted rather than expanding
4693 the function inline. If convenient, the result should be placed
4694 in TARGET. SUBTARGET may be used as the target for computing
4695 the operand. */
4697 static rtx
4698 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4700 enum machine_mode mode;
4701 tree arg;
4702 rtx op0;
4704 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4705 return NULL_RTX;
4707 arg = CALL_EXPR_ARG (exp, 0);
4708 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4709 mode = TYPE_MODE (TREE_TYPE (arg));
4710 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4711 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4714 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4715 Return NULL is a normal call should be emitted rather than expanding the
4716 function inline. If convenient, the result should be placed in TARGET.
4717 SUBTARGET may be used as the target for computing the operand. */
4719 static rtx
4720 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4722 rtx op0, op1;
4723 tree arg;
4725 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4726 return NULL_RTX;
4728 arg = CALL_EXPR_ARG (exp, 0);
4729 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4731 arg = CALL_EXPR_ARG (exp, 1);
4732 op1 = expand_normal (arg);
4734 return expand_copysign (op0, op1, target);
4737 /* Create a new constant string literal and return a char* pointer to it.
4738 The STRING_CST value is the LEN characters at STR. */
4739 tree
4740 build_string_literal (int len, const char *str)
4742 tree t, elem, index, type;
4744 t = build_string (len, str);
4745 elem = build_type_variant (char_type_node, 1, 0);
4746 index = build_index_type (size_int (len - 1));
4747 type = build_array_type (elem, index);
4748 TREE_TYPE (t) = type;
4749 TREE_CONSTANT (t) = 1;
4750 TREE_READONLY (t) = 1;
4751 TREE_STATIC (t) = 1;
4753 type = build_pointer_type (elem);
4754 t = build1 (ADDR_EXPR, type,
4755 build4 (ARRAY_REF, elem,
4756 t, integer_zero_node, NULL_TREE, NULL_TREE));
4757 return t;
4760 /* Expand a call to __builtin___clear_cache. */
4762 static rtx
4763 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4765 #ifndef HAVE_clear_cache
4766 #ifdef CLEAR_INSN_CACHE
4767 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4768 does something. Just do the default expansion to a call to
4769 __clear_cache(). */
4770 return NULL_RTX;
4771 #else
4772 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4773 does nothing. There is no need to call it. Do nothing. */
4774 return const0_rtx;
4775 #endif /* CLEAR_INSN_CACHE */
4776 #else
4777 /* We have a "clear_cache" insn, and it will handle everything. */
4778 tree begin, end;
4779 rtx begin_rtx, end_rtx;
4781 /* We must not expand to a library call. If we did, any
4782 fallback library function in libgcc that might contain a call to
4783 __builtin___clear_cache() would recurse infinitely. */
4784 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4786 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4787 return const0_rtx;
4790 if (HAVE_clear_cache)
4792 struct expand_operand ops[2];
4794 begin = CALL_EXPR_ARG (exp, 0);
4795 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4797 end = CALL_EXPR_ARG (exp, 1);
4798 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4800 create_address_operand (&ops[0], begin_rtx);
4801 create_address_operand (&ops[1], end_rtx);
4802 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4803 return const0_rtx;
4805 return const0_rtx;
4806 #endif /* HAVE_clear_cache */
4809 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4811 static rtx
4812 round_trampoline_addr (rtx tramp)
4814 rtx temp, addend, mask;
4816 /* If we don't need too much alignment, we'll have been guaranteed
4817 proper alignment by get_trampoline_type. */
4818 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4819 return tramp;
4821 /* Round address up to desired boundary. */
4822 temp = gen_reg_rtx (Pmode);
4823 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4824 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4826 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4827 temp, 0, OPTAB_LIB_WIDEN);
4828 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4829 temp, 0, OPTAB_LIB_WIDEN);
4831 return tramp;
4834 static rtx
4835 expand_builtin_init_trampoline (tree exp)
4837 tree t_tramp, t_func, t_chain;
4838 rtx m_tramp, r_tramp, r_chain, tmp;
4840 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4841 POINTER_TYPE, VOID_TYPE))
4842 return NULL_RTX;
4844 t_tramp = CALL_EXPR_ARG (exp, 0);
4845 t_func = CALL_EXPR_ARG (exp, 1);
4846 t_chain = CALL_EXPR_ARG (exp, 2);
4848 r_tramp = expand_normal (t_tramp);
4849 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4850 MEM_NOTRAP_P (m_tramp) = 1;
4852 /* The TRAMP argument should be the address of a field within the
4853 local function's FRAME decl. Let's see if we can fill in the
4854 to fill in the MEM_ATTRs for this memory. */
4855 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4856 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4857 true, 0);
4859 tmp = round_trampoline_addr (r_tramp);
4860 if (tmp != r_tramp)
4862 m_tramp = change_address (m_tramp, BLKmode, tmp);
4863 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4864 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4867 /* The FUNC argument should be the address of the nested function.
4868 Extract the actual function decl to pass to the hook. */
4869 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4870 t_func = TREE_OPERAND (t_func, 0);
4871 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4873 r_chain = expand_normal (t_chain);
4875 /* Generate insns to initialize the trampoline. */
4876 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4878 trampolines_created = 1;
4880 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4881 "trampoline generated for nested function %qD", t_func);
4883 return const0_rtx;
4886 static rtx
4887 expand_builtin_adjust_trampoline (tree exp)
4889 rtx tramp;
4891 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4892 return NULL_RTX;
4894 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4895 tramp = round_trampoline_addr (tramp);
4896 if (targetm.calls.trampoline_adjust_address)
4897 tramp = targetm.calls.trampoline_adjust_address (tramp);
4899 return tramp;
4902 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4903 function. The function first checks whether the back end provides
4904 an insn to implement signbit for the respective mode. If not, it
4905 checks whether the floating point format of the value is such that
4906 the sign bit can be extracted. If that is not the case, the
4907 function returns NULL_RTX to indicate that a normal call should be
4908 emitted rather than expanding the function in-line. EXP is the
4909 expression that is a call to the builtin function; if convenient,
4910 the result should be placed in TARGET. */
4911 static rtx
4912 expand_builtin_signbit (tree exp, rtx target)
4914 const struct real_format *fmt;
4915 enum machine_mode fmode, imode, rmode;
4916 tree arg;
4917 int word, bitpos;
4918 enum insn_code icode;
4919 rtx temp;
4920 location_t loc = EXPR_LOCATION (exp);
4922 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4923 return NULL_RTX;
4925 arg = CALL_EXPR_ARG (exp, 0);
4926 fmode = TYPE_MODE (TREE_TYPE (arg));
4927 rmode = TYPE_MODE (TREE_TYPE (exp));
4928 fmt = REAL_MODE_FORMAT (fmode);
4930 arg = builtin_save_expr (arg);
4932 /* Expand the argument yielding a RTX expression. */
4933 temp = expand_normal (arg);
4935 /* Check if the back end provides an insn that handles signbit for the
4936 argument's mode. */
4937 icode = optab_handler (signbit_optab, fmode);
4938 if (icode != CODE_FOR_nothing)
4940 rtx last = get_last_insn ();
4941 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4942 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4943 return target;
4944 delete_insns_since (last);
4947 /* For floating point formats without a sign bit, implement signbit
4948 as "ARG < 0.0". */
4949 bitpos = fmt->signbit_ro;
4950 if (bitpos < 0)
4952 /* But we can't do this if the format supports signed zero. */
4953 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4954 return NULL_RTX;
4956 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4957 build_real (TREE_TYPE (arg), dconst0));
4958 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4961 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4963 imode = int_mode_for_mode (fmode);
4964 if (imode == BLKmode)
4965 return NULL_RTX;
4966 temp = gen_lowpart (imode, temp);
4968 else
4970 imode = word_mode;
4971 /* Handle targets with different FP word orders. */
4972 if (FLOAT_WORDS_BIG_ENDIAN)
4973 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4974 else
4975 word = bitpos / BITS_PER_WORD;
4976 temp = operand_subword_force (temp, word, fmode);
4977 bitpos = bitpos % BITS_PER_WORD;
4980 /* Force the intermediate word_mode (or narrower) result into a
4981 register. This avoids attempting to create paradoxical SUBREGs
4982 of floating point modes below. */
4983 temp = force_reg (imode, temp);
4985 /* If the bitpos is within the "result mode" lowpart, the operation
4986 can be implement with a single bitwise AND. Otherwise, we need
4987 a right shift and an AND. */
4989 if (bitpos < GET_MODE_BITSIZE (rmode))
4991 double_int mask = double_int_setbit (double_int_zero, bitpos);
4993 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4994 temp = gen_lowpart (rmode, temp);
4995 temp = expand_binop (rmode, and_optab, temp,
4996 immed_double_int_const (mask, rmode),
4997 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4999 else
5001 /* Perform a logical right shift to place the signbit in the least
5002 significant bit, then truncate the result to the desired mode
5003 and mask just this bit. */
5004 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5005 temp = gen_lowpart (rmode, temp);
5006 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5007 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5010 return temp;
5013 /* Expand fork or exec calls. TARGET is the desired target of the
5014 call. EXP is the call. FN is the
5015 identificator of the actual function. IGNORE is nonzero if the
5016 value is to be ignored. */
5018 static rtx
5019 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5021 tree id, decl;
5022 tree call;
5024 /* If we are not profiling, just call the function. */
5025 if (!profile_arc_flag)
5026 return NULL_RTX;
5028 /* Otherwise call the wrapper. This should be equivalent for the rest of
5029 compiler, so the code does not diverge, and the wrapper may run the
5030 code necessary for keeping the profiling sane. */
5032 switch (DECL_FUNCTION_CODE (fn))
5034 case BUILT_IN_FORK:
5035 id = get_identifier ("__gcov_fork");
5036 break;
5038 case BUILT_IN_EXECL:
5039 id = get_identifier ("__gcov_execl");
5040 break;
5042 case BUILT_IN_EXECV:
5043 id = get_identifier ("__gcov_execv");
5044 break;
5046 case BUILT_IN_EXECLP:
5047 id = get_identifier ("__gcov_execlp");
5048 break;
5050 case BUILT_IN_EXECLE:
5051 id = get_identifier ("__gcov_execle");
5052 break;
5054 case BUILT_IN_EXECVP:
5055 id = get_identifier ("__gcov_execvp");
5056 break;
5058 case BUILT_IN_EXECVE:
5059 id = get_identifier ("__gcov_execve");
5060 break;
5062 default:
5063 gcc_unreachable ();
5066 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5067 FUNCTION_DECL, id, TREE_TYPE (fn));
5068 DECL_EXTERNAL (decl) = 1;
5069 TREE_PUBLIC (decl) = 1;
5070 DECL_ARTIFICIAL (decl) = 1;
5071 TREE_NOTHROW (decl) = 1;
5072 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5073 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5074 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5075 return expand_call (call, target, ignore);
5080 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5081 the pointer in these functions is void*, the tree optimizers may remove
5082 casts. The mode computed in expand_builtin isn't reliable either, due
5083 to __sync_bool_compare_and_swap.
5085 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5086 group of builtins. This gives us log2 of the mode size. */
5088 static inline enum machine_mode
5089 get_builtin_sync_mode (int fcode_diff)
5091 /* The size is not negotiable, so ask not to get BLKmode in return
5092 if the target indicates that a smaller size would be better. */
5093 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5096 /* Expand the memory expression LOC and return the appropriate memory operand
5097 for the builtin_sync operations. */
5099 static rtx
5100 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5102 rtx addr, mem;
5104 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5105 addr = convert_memory_address (Pmode, addr);
5107 /* Note that we explicitly do not want any alias information for this
5108 memory, so that we kill all other live memories. Otherwise we don't
5109 satisfy the full barrier semantics of the intrinsic. */
5110 mem = validize_mem (gen_rtx_MEM (mode, addr));
5112 /* The alignment needs to be at least according to that of the mode. */
5113 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5114 get_pointer_alignment (loc)));
5115 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5116 MEM_VOLATILE_P (mem) = 1;
5118 return mem;
5121 /* Make sure an argument is in the right mode.
5122 EXP is the tree argument.
5123 MODE is the mode it should be in. */
5125 static rtx
5126 expand_expr_force_mode (tree exp, enum machine_mode mode)
5128 rtx val;
5129 enum machine_mode old_mode;
5131 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5132 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5133 of CONST_INTs, where we know the old_mode only from the call argument. */
5135 old_mode = GET_MODE (val);
5136 if (old_mode == VOIDmode)
5137 old_mode = TYPE_MODE (TREE_TYPE (exp));
5138 val = convert_modes (mode, old_mode, val, 1);
5139 return val;
5143 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5144 EXP is the CALL_EXPR. CODE is the rtx code
5145 that corresponds to the arithmetic or logical operation from the name;
5146 an exception here is that NOT actually means NAND. TARGET is an optional
5147 place for us to store the results; AFTER is true if this is the
5148 fetch_and_xxx form. */
5150 static rtx
5151 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5152 enum rtx_code code, bool after,
5153 rtx target)
5155 rtx val, mem;
5156 location_t loc = EXPR_LOCATION (exp);
5158 if (code == NOT && warn_sync_nand)
5160 tree fndecl = get_callee_fndecl (exp);
5161 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5163 static bool warned_f_a_n, warned_n_a_f;
5165 switch (fcode)
5167 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5168 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5169 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5170 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5171 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5172 if (warned_f_a_n)
5173 break;
5175 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5176 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5177 warned_f_a_n = true;
5178 break;
5180 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5181 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5182 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5183 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5184 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5185 if (warned_n_a_f)
5186 break;
5188 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5189 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5190 warned_n_a_f = true;
5191 break;
5193 default:
5194 gcc_unreachable ();
5198 /* Expand the operands. */
5199 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5200 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5202 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5203 after);
5206 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5207 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5208 true if this is the boolean form. TARGET is a place for us to store the
5209 results; this is NOT optional if IS_BOOL is true. */
5211 static rtx
5212 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5213 bool is_bool, rtx target)
5215 rtx old_val, new_val, mem;
5216 rtx *pbool, *poval;
5218 /* Expand the operands. */
5219 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5220 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5221 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5223 pbool = poval = NULL;
5224 if (target != const0_rtx)
5226 if (is_bool)
5227 pbool = &target;
5228 else
5229 poval = &target;
5231 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5232 false, MEMMODEL_SEQ_CST,
5233 MEMMODEL_SEQ_CST))
5234 return NULL_RTX;
5236 return target;
5239 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5240 general form is actually an atomic exchange, and some targets only
5241 support a reduced form with the second argument being a constant 1.
5242 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5243 the results. */
5245 static rtx
5246 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5247 rtx target)
5249 rtx val, mem;
5251 /* Expand the operands. */
5252 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5253 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5255 return expand_sync_lock_test_and_set (target, mem, val);
5258 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5260 static void
5261 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5263 rtx mem;
5265 /* Expand the operands. */
5266 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5268 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5271 /* Given an integer representing an ``enum memmodel'', verify its
5272 correctness and return the memory model enum. */
5274 static enum memmodel
5275 get_memmodel (tree exp)
5277 rtx op;
5279 /* If the parameter is not a constant, it's a run time value so we'll just
5280 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5281 if (TREE_CODE (exp) != INTEGER_CST)
5282 return MEMMODEL_SEQ_CST;
5284 op = expand_normal (exp);
5285 if (INTVAL (op) < 0 || INTVAL (op) >= MEMMODEL_LAST)
5287 warning (OPT_Winvalid_memory_model,
5288 "invalid memory model argument to builtin");
5289 return MEMMODEL_SEQ_CST;
5291 return (enum memmodel) INTVAL (op);
5294 /* Expand the __atomic_exchange intrinsic:
5295 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5296 EXP is the CALL_EXPR.
5297 TARGET is an optional place for us to store the results. */
5299 static rtx
5300 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5302 rtx val, mem;
5303 enum memmodel model;
5305 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5306 if (model == MEMMODEL_CONSUME)
5308 error ("invalid memory model for %<__atomic_exchange%>");
5309 return NULL_RTX;
5312 if (!flag_inline_atomics)
5313 return NULL_RTX;
5315 /* Expand the operands. */
5316 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5317 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5319 return expand_atomic_exchange (target, mem, val, model);
5322 /* Expand the __atomic_compare_exchange intrinsic:
5323 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5324 TYPE desired, BOOL weak,
5325 enum memmodel success,
5326 enum memmodel failure)
5327 EXP is the CALL_EXPR.
5328 TARGET is an optional place for us to store the results. */
5330 static rtx
5331 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5332 rtx target)
5334 rtx expect, desired, mem, oldval;
5335 enum memmodel success, failure;
5336 tree weak;
5337 bool is_weak;
5339 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5340 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5342 if (failure == MEMMODEL_RELEASE || failure == MEMMODEL_ACQ_REL)
5344 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5345 return NULL_RTX;
5348 if (failure > success)
5350 error ("failure memory model cannot be stronger than success "
5351 "memory model for %<__atomic_compare_exchange%>");
5352 return NULL_RTX;
5355 if (!flag_inline_atomics)
5356 return NULL_RTX;
5358 /* Expand the operands. */
5359 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5361 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5362 expect = convert_memory_address (Pmode, expect);
5363 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5365 weak = CALL_EXPR_ARG (exp, 3);
5366 is_weak = false;
5367 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5368 is_weak = true;
5370 oldval = copy_to_reg (gen_rtx_MEM (mode, expect));
5372 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5373 &oldval, mem, oldval, desired,
5374 is_weak, success, failure))
5375 return NULL_RTX;
5377 emit_move_insn (gen_rtx_MEM (mode, expect), oldval);
5378 return target;
5381 /* Expand the __atomic_load intrinsic:
5382 TYPE __atomic_load (TYPE *object, enum memmodel)
5383 EXP is the CALL_EXPR.
5384 TARGET is an optional place for us to store the results. */
5386 static rtx
5387 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5389 rtx mem;
5390 enum memmodel model;
5392 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5393 if (model == MEMMODEL_RELEASE
5394 || model == MEMMODEL_ACQ_REL)
5396 error ("invalid memory model for %<__atomic_load%>");
5397 return NULL_RTX;
5400 if (!flag_inline_atomics)
5401 return NULL_RTX;
5403 /* Expand the operand. */
5404 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5406 return expand_atomic_load (target, mem, model);
5410 /* Expand the __atomic_store intrinsic:
5411 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5412 EXP is the CALL_EXPR.
5413 TARGET is an optional place for us to store the results. */
5415 static rtx
5416 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5418 rtx mem, val;
5419 enum memmodel model;
5421 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5422 if (model != MEMMODEL_RELAXED
5423 && model != MEMMODEL_SEQ_CST
5424 && model != MEMMODEL_RELEASE)
5426 error ("invalid memory model for %<__atomic_store%>");
5427 return NULL_RTX;
5430 if (!flag_inline_atomics)
5431 return NULL_RTX;
5433 /* Expand the operands. */
5434 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5435 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5437 return expand_atomic_store (mem, val, model, false);
5440 /* Expand the __atomic_fetch_XXX intrinsic:
5441 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5442 EXP is the CALL_EXPR.
5443 TARGET is an optional place for us to store the results.
5444 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5445 FETCH_AFTER is true if returning the result of the operation.
5446 FETCH_AFTER is false if returning the value before the operation.
5447 IGNORE is true if the result is not used.
5448 EXT_CALL is the correct builtin for an external call if this cannot be
5449 resolved to an instruction sequence. */
5451 static rtx
5452 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5453 enum rtx_code code, bool fetch_after,
5454 bool ignore, enum built_in_function ext_call)
5456 rtx val, mem, ret;
5457 enum memmodel model;
5458 tree fndecl;
5459 tree addr;
5461 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5463 /* Expand the operands. */
5464 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5465 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5467 /* Only try generating instructions if inlining is turned on. */
5468 if (flag_inline_atomics)
5470 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5471 if (ret)
5472 return ret;
5475 /* Return if a different routine isn't needed for the library call. */
5476 if (ext_call == BUILT_IN_NONE)
5477 return NULL_RTX;
5479 /* Change the call to the specified function. */
5480 fndecl = get_callee_fndecl (exp);
5481 addr = CALL_EXPR_FN (exp);
5482 STRIP_NOPS (addr);
5484 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5485 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5487 /* Expand the call here so we can emit trailing code. */
5488 ret = expand_call (exp, target, ignore);
5490 /* Replace the original function just in case it matters. */
5491 TREE_OPERAND (addr, 0) = fndecl;
5493 /* Then issue the arithmetic correction to return the right result. */
5494 if (!ignore)
5496 if (code == NOT)
5498 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5499 OPTAB_LIB_WIDEN);
5500 ret = expand_simple_unop (mode, NOT, ret, target, true);
5502 else
5503 ret = expand_simple_binop (mode, code, ret, val, target, true,
5504 OPTAB_LIB_WIDEN);
5506 return ret;
5510 #ifndef HAVE_atomic_clear
5511 # define HAVE_atomic_clear 0
5512 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5513 #endif
5515 /* Expand an atomic clear operation.
5516 void _atomic_clear (BOOL *obj, enum memmodel)
5517 EXP is the call expression. */
5519 static rtx
5520 expand_builtin_atomic_clear (tree exp)
5522 enum machine_mode mode;
5523 rtx mem, ret;
5524 enum memmodel model;
5526 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5527 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5528 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5530 if (model == MEMMODEL_ACQUIRE || model == MEMMODEL_ACQ_REL)
5532 error ("invalid memory model for %<__atomic_store%>");
5533 return const0_rtx;
5536 if (HAVE_atomic_clear)
5538 emit_insn (gen_atomic_clear (mem, model));
5539 return const0_rtx;
5542 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5543 Failing that, a store is issued by __atomic_store. The only way this can
5544 fail is if the bool type is larger than a word size. Unlikely, but
5545 handle it anyway for completeness. Assume a single threaded model since
5546 there is no atomic support in this case, and no barriers are required. */
5547 ret = expand_atomic_store (mem, const0_rtx, model, true);
5548 if (!ret)
5549 emit_move_insn (mem, const0_rtx);
5550 return const0_rtx;
5553 /* Expand an atomic test_and_set operation.
5554 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5555 EXP is the call expression. */
5557 static rtx
5558 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5560 rtx mem;
5561 enum memmodel model;
5562 enum machine_mode mode;
5564 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5565 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5566 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5568 return expand_atomic_test_and_set (target, mem, model);
5572 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5573 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5575 static tree
5576 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5578 int size;
5579 enum machine_mode mode;
5580 unsigned int mode_align, type_align;
5582 if (TREE_CODE (arg0) != INTEGER_CST)
5583 return NULL_TREE;
5585 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5586 mode = mode_for_size (size, MODE_INT, 0);
5587 mode_align = GET_MODE_ALIGNMENT (mode);
5589 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5590 type_align = mode_align;
5591 else
5593 tree ttype = TREE_TYPE (arg1);
5595 /* This function is usually invoked and folded immediately by the front
5596 end before anything else has a chance to look at it. The pointer
5597 parameter at this point is usually cast to a void *, so check for that
5598 and look past the cast. */
5599 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5600 && VOID_TYPE_P (TREE_TYPE (ttype)))
5601 arg1 = TREE_OPERAND (arg1, 0);
5603 ttype = TREE_TYPE (arg1);
5604 gcc_assert (POINTER_TYPE_P (ttype));
5606 /* Get the underlying type of the object. */
5607 ttype = TREE_TYPE (ttype);
5608 type_align = TYPE_ALIGN (ttype);
5611 /* If the object has smaller alignment, the the lock free routines cannot
5612 be used. */
5613 if (type_align < mode_align)
5614 return integer_zero_node;
5616 /* Check if a compare_and_swap pattern exists for the mode which represents
5617 the required size. The pattern is not allowed to fail, so the existence
5618 of the pattern indicates support is present. */
5619 if (can_compare_and_swap_p (mode, true))
5620 return integer_one_node;
5621 else
5622 return integer_zero_node;
5625 /* Return true if the parameters to call EXP represent an object which will
5626 always generate lock free instructions. The first argument represents the
5627 size of the object, and the second parameter is a pointer to the object
5628 itself. If NULL is passed for the object, then the result is based on
5629 typical alignment for an object of the specified size. Otherwise return
5630 false. */
5632 static rtx
5633 expand_builtin_atomic_always_lock_free (tree exp)
5635 tree size;
5636 tree arg0 = CALL_EXPR_ARG (exp, 0);
5637 tree arg1 = CALL_EXPR_ARG (exp, 1);
5639 if (TREE_CODE (arg0) != INTEGER_CST)
5641 error ("non-constant argument 1 to __atomic_always_lock_free");
5642 return const0_rtx;
5645 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5646 if (size == integer_one_node)
5647 return const1_rtx;
5648 return const0_rtx;
5651 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5652 is lock free on this architecture. */
5654 static tree
5655 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5657 if (!flag_inline_atomics)
5658 return NULL_TREE;
5660 /* If it isn't always lock free, don't generate a result. */
5661 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == integer_one_node)
5662 return integer_one_node;
5664 return NULL_TREE;
5667 /* Return true if the parameters to call EXP represent an object which will
5668 always generate lock free instructions. The first argument represents the
5669 size of the object, and the second parameter is a pointer to the object
5670 itself. If NULL is passed for the object, then the result is based on
5671 typical alignment for an object of the specified size. Otherwise return
5672 NULL*/
5674 static rtx
5675 expand_builtin_atomic_is_lock_free (tree exp)
5677 tree size;
5678 tree arg0 = CALL_EXPR_ARG (exp, 0);
5679 tree arg1 = CALL_EXPR_ARG (exp, 1);
5681 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5683 error ("non-integer argument 1 to __atomic_is_lock_free");
5684 return NULL_RTX;
5687 if (!flag_inline_atomics)
5688 return NULL_RTX;
5690 /* If the value is known at compile time, return the RTX for it. */
5691 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5692 if (size == integer_one_node)
5693 return const1_rtx;
5695 return NULL_RTX;
5698 /* Expand the __atomic_thread_fence intrinsic:
5699 void __atomic_thread_fence (enum memmodel)
5700 EXP is the CALL_EXPR. */
5702 static void
5703 expand_builtin_atomic_thread_fence (tree exp)
5705 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5706 expand_mem_thread_fence (model);
5709 /* Expand the __atomic_signal_fence intrinsic:
5710 void __atomic_signal_fence (enum memmodel)
5711 EXP is the CALL_EXPR. */
5713 static void
5714 expand_builtin_atomic_signal_fence (tree exp)
5716 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5717 expand_mem_signal_fence (model);
5720 /* Expand the __sync_synchronize intrinsic. */
5722 static void
5723 expand_builtin_sync_synchronize (void)
5725 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5729 /* Expand an expression EXP that calls a built-in function,
5730 with result going to TARGET if that's convenient
5731 (and in mode MODE if that's convenient).
5732 SUBTARGET may be used as the target for computing one of EXP's operands.
5733 IGNORE is nonzero if the value is to be ignored. */
5736 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5737 int ignore)
5739 tree fndecl = get_callee_fndecl (exp);
5740 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5741 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5742 int flags;
5744 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5745 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5747 /* When not optimizing, generate calls to library functions for a certain
5748 set of builtins. */
5749 if (!optimize
5750 && !called_as_built_in (fndecl)
5751 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5752 && fcode != BUILT_IN_ALLOCA
5753 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5754 && fcode != BUILT_IN_FREE)
5755 return expand_call (exp, target, ignore);
5757 /* The built-in function expanders test for target == const0_rtx
5758 to determine whether the function's result will be ignored. */
5759 if (ignore)
5760 target = const0_rtx;
5762 /* If the result of a pure or const built-in function is ignored, and
5763 none of its arguments are volatile, we can avoid expanding the
5764 built-in call and just evaluate the arguments for side-effects. */
5765 if (target == const0_rtx
5766 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5767 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5769 bool volatilep = false;
5770 tree arg;
5771 call_expr_arg_iterator iter;
5773 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5774 if (TREE_THIS_VOLATILE (arg))
5776 volatilep = true;
5777 break;
5780 if (! volatilep)
5782 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5783 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5784 return const0_rtx;
5788 switch (fcode)
5790 CASE_FLT_FN (BUILT_IN_FABS):
5791 target = expand_builtin_fabs (exp, target, subtarget);
5792 if (target)
5793 return target;
5794 break;
5796 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5797 target = expand_builtin_copysign (exp, target, subtarget);
5798 if (target)
5799 return target;
5800 break;
5802 /* Just do a normal library call if we were unable to fold
5803 the values. */
5804 CASE_FLT_FN (BUILT_IN_CABS):
5805 break;
5807 CASE_FLT_FN (BUILT_IN_EXP):
5808 CASE_FLT_FN (BUILT_IN_EXP10):
5809 CASE_FLT_FN (BUILT_IN_POW10):
5810 CASE_FLT_FN (BUILT_IN_EXP2):
5811 CASE_FLT_FN (BUILT_IN_EXPM1):
5812 CASE_FLT_FN (BUILT_IN_LOGB):
5813 CASE_FLT_FN (BUILT_IN_LOG):
5814 CASE_FLT_FN (BUILT_IN_LOG10):
5815 CASE_FLT_FN (BUILT_IN_LOG2):
5816 CASE_FLT_FN (BUILT_IN_LOG1P):
5817 CASE_FLT_FN (BUILT_IN_TAN):
5818 CASE_FLT_FN (BUILT_IN_ASIN):
5819 CASE_FLT_FN (BUILT_IN_ACOS):
5820 CASE_FLT_FN (BUILT_IN_ATAN):
5821 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5822 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5823 because of possible accuracy problems. */
5824 if (! flag_unsafe_math_optimizations)
5825 break;
5826 CASE_FLT_FN (BUILT_IN_SQRT):
5827 CASE_FLT_FN (BUILT_IN_FLOOR):
5828 CASE_FLT_FN (BUILT_IN_CEIL):
5829 CASE_FLT_FN (BUILT_IN_TRUNC):
5830 CASE_FLT_FN (BUILT_IN_ROUND):
5831 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5832 CASE_FLT_FN (BUILT_IN_RINT):
5833 target = expand_builtin_mathfn (exp, target, subtarget);
5834 if (target)
5835 return target;
5836 break;
5838 CASE_FLT_FN (BUILT_IN_FMA):
5839 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5840 if (target)
5841 return target;
5842 break;
5844 CASE_FLT_FN (BUILT_IN_ILOGB):
5845 if (! flag_unsafe_math_optimizations)
5846 break;
5847 CASE_FLT_FN (BUILT_IN_ISINF):
5848 CASE_FLT_FN (BUILT_IN_FINITE):
5849 case BUILT_IN_ISFINITE:
5850 case BUILT_IN_ISNORMAL:
5851 target = expand_builtin_interclass_mathfn (exp, target);
5852 if (target)
5853 return target;
5854 break;
5856 CASE_FLT_FN (BUILT_IN_ICEIL):
5857 CASE_FLT_FN (BUILT_IN_LCEIL):
5858 CASE_FLT_FN (BUILT_IN_LLCEIL):
5859 CASE_FLT_FN (BUILT_IN_LFLOOR):
5860 CASE_FLT_FN (BUILT_IN_IFLOOR):
5861 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5862 target = expand_builtin_int_roundingfn (exp, target);
5863 if (target)
5864 return target;
5865 break;
5867 CASE_FLT_FN (BUILT_IN_IRINT):
5868 CASE_FLT_FN (BUILT_IN_LRINT):
5869 CASE_FLT_FN (BUILT_IN_LLRINT):
5870 CASE_FLT_FN (BUILT_IN_IROUND):
5871 CASE_FLT_FN (BUILT_IN_LROUND):
5872 CASE_FLT_FN (BUILT_IN_LLROUND):
5873 target = expand_builtin_int_roundingfn_2 (exp, target);
5874 if (target)
5875 return target;
5876 break;
5878 CASE_FLT_FN (BUILT_IN_POWI):
5879 target = expand_builtin_powi (exp, target);
5880 if (target)
5881 return target;
5882 break;
5884 CASE_FLT_FN (BUILT_IN_ATAN2):
5885 CASE_FLT_FN (BUILT_IN_LDEXP):
5886 CASE_FLT_FN (BUILT_IN_SCALB):
5887 CASE_FLT_FN (BUILT_IN_SCALBN):
5888 CASE_FLT_FN (BUILT_IN_SCALBLN):
5889 if (! flag_unsafe_math_optimizations)
5890 break;
5892 CASE_FLT_FN (BUILT_IN_FMOD):
5893 CASE_FLT_FN (BUILT_IN_REMAINDER):
5894 CASE_FLT_FN (BUILT_IN_DREM):
5895 CASE_FLT_FN (BUILT_IN_POW):
5896 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5897 if (target)
5898 return target;
5899 break;
5901 CASE_FLT_FN (BUILT_IN_CEXPI):
5902 target = expand_builtin_cexpi (exp, target);
5903 gcc_assert (target);
5904 return target;
5906 CASE_FLT_FN (BUILT_IN_SIN):
5907 CASE_FLT_FN (BUILT_IN_COS):
5908 if (! flag_unsafe_math_optimizations)
5909 break;
5910 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5911 if (target)
5912 return target;
5913 break;
5915 CASE_FLT_FN (BUILT_IN_SINCOS):
5916 if (! flag_unsafe_math_optimizations)
5917 break;
5918 target = expand_builtin_sincos (exp);
5919 if (target)
5920 return target;
5921 break;
5923 case BUILT_IN_APPLY_ARGS:
5924 return expand_builtin_apply_args ();
5926 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5927 FUNCTION with a copy of the parameters described by
5928 ARGUMENTS, and ARGSIZE. It returns a block of memory
5929 allocated on the stack into which is stored all the registers
5930 that might possibly be used for returning the result of a
5931 function. ARGUMENTS is the value returned by
5932 __builtin_apply_args. ARGSIZE is the number of bytes of
5933 arguments that must be copied. ??? How should this value be
5934 computed? We'll also need a safe worst case value for varargs
5935 functions. */
5936 case BUILT_IN_APPLY:
5937 if (!validate_arglist (exp, POINTER_TYPE,
5938 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5939 && !validate_arglist (exp, REFERENCE_TYPE,
5940 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5941 return const0_rtx;
5942 else
5944 rtx ops[3];
5946 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5947 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5948 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5950 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5953 /* __builtin_return (RESULT) causes the function to return the
5954 value described by RESULT. RESULT is address of the block of
5955 memory returned by __builtin_apply. */
5956 case BUILT_IN_RETURN:
5957 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5958 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5959 return const0_rtx;
5961 case BUILT_IN_SAVEREGS:
5962 return expand_builtin_saveregs ();
5964 case BUILT_IN_VA_ARG_PACK:
5965 /* All valid uses of __builtin_va_arg_pack () are removed during
5966 inlining. */
5967 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5968 return const0_rtx;
5970 case BUILT_IN_VA_ARG_PACK_LEN:
5971 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5972 inlining. */
5973 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5974 return const0_rtx;
5976 /* Return the address of the first anonymous stack arg. */
5977 case BUILT_IN_NEXT_ARG:
5978 if (fold_builtin_next_arg (exp, false))
5979 return const0_rtx;
5980 return expand_builtin_next_arg ();
5982 case BUILT_IN_CLEAR_CACHE:
5983 target = expand_builtin___clear_cache (exp);
5984 if (target)
5985 return target;
5986 break;
5988 case BUILT_IN_CLASSIFY_TYPE:
5989 return expand_builtin_classify_type (exp);
5991 case BUILT_IN_CONSTANT_P:
5992 return const0_rtx;
5994 case BUILT_IN_FRAME_ADDRESS:
5995 case BUILT_IN_RETURN_ADDRESS:
5996 return expand_builtin_frame_address (fndecl, exp);
5998 /* Returns the address of the area where the structure is returned.
5999 0 otherwise. */
6000 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6001 if (call_expr_nargs (exp) != 0
6002 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6003 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6004 return const0_rtx;
6005 else
6006 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6008 case BUILT_IN_ALLOCA:
6009 case BUILT_IN_ALLOCA_WITH_ALIGN:
6010 /* If the allocation stems from the declaration of a variable-sized
6011 object, it cannot accumulate. */
6012 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6013 if (target)
6014 return target;
6015 break;
6017 case BUILT_IN_STACK_SAVE:
6018 return expand_stack_save ();
6020 case BUILT_IN_STACK_RESTORE:
6021 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6022 return const0_rtx;
6024 case BUILT_IN_BSWAP32:
6025 case BUILT_IN_BSWAP64:
6026 target = expand_builtin_bswap (exp, target, subtarget);
6028 if (target)
6029 return target;
6030 break;
6032 CASE_INT_FN (BUILT_IN_FFS):
6033 case BUILT_IN_FFSIMAX:
6034 target = expand_builtin_unop (target_mode, exp, target,
6035 subtarget, ffs_optab);
6036 if (target)
6037 return target;
6038 break;
6040 CASE_INT_FN (BUILT_IN_CLZ):
6041 case BUILT_IN_CLZIMAX:
6042 target = expand_builtin_unop (target_mode, exp, target,
6043 subtarget, clz_optab);
6044 if (target)
6045 return target;
6046 break;
6048 CASE_INT_FN (BUILT_IN_CTZ):
6049 case BUILT_IN_CTZIMAX:
6050 target = expand_builtin_unop (target_mode, exp, target,
6051 subtarget, ctz_optab);
6052 if (target)
6053 return target;
6054 break;
6056 CASE_INT_FN (BUILT_IN_CLRSB):
6057 case BUILT_IN_CLRSBIMAX:
6058 target = expand_builtin_unop (target_mode, exp, target,
6059 subtarget, clrsb_optab);
6060 if (target)
6061 return target;
6062 break;
6064 CASE_INT_FN (BUILT_IN_POPCOUNT):
6065 case BUILT_IN_POPCOUNTIMAX:
6066 target = expand_builtin_unop (target_mode, exp, target,
6067 subtarget, popcount_optab);
6068 if (target)
6069 return target;
6070 break;
6072 CASE_INT_FN (BUILT_IN_PARITY):
6073 case BUILT_IN_PARITYIMAX:
6074 target = expand_builtin_unop (target_mode, exp, target,
6075 subtarget, parity_optab);
6076 if (target)
6077 return target;
6078 break;
6080 case BUILT_IN_STRLEN:
6081 target = expand_builtin_strlen (exp, target, target_mode);
6082 if (target)
6083 return target;
6084 break;
6086 case BUILT_IN_STRCPY:
6087 target = expand_builtin_strcpy (exp, target);
6088 if (target)
6089 return target;
6090 break;
6092 case BUILT_IN_STRNCPY:
6093 target = expand_builtin_strncpy (exp, target);
6094 if (target)
6095 return target;
6096 break;
6098 case BUILT_IN_STPCPY:
6099 target = expand_builtin_stpcpy (exp, target, mode);
6100 if (target)
6101 return target;
6102 break;
6104 case BUILT_IN_MEMCPY:
6105 target = expand_builtin_memcpy (exp, target);
6106 if (target)
6107 return target;
6108 break;
6110 case BUILT_IN_MEMPCPY:
6111 target = expand_builtin_mempcpy (exp, target, mode);
6112 if (target)
6113 return target;
6114 break;
6116 case BUILT_IN_MEMSET:
6117 target = expand_builtin_memset (exp, target, mode);
6118 if (target)
6119 return target;
6120 break;
6122 case BUILT_IN_BZERO:
6123 target = expand_builtin_bzero (exp);
6124 if (target)
6125 return target;
6126 break;
6128 case BUILT_IN_STRCMP:
6129 target = expand_builtin_strcmp (exp, target);
6130 if (target)
6131 return target;
6132 break;
6134 case BUILT_IN_STRNCMP:
6135 target = expand_builtin_strncmp (exp, target, mode);
6136 if (target)
6137 return target;
6138 break;
6140 case BUILT_IN_BCMP:
6141 case BUILT_IN_MEMCMP:
6142 target = expand_builtin_memcmp (exp, target, mode);
6143 if (target)
6144 return target;
6145 break;
6147 case BUILT_IN_SETJMP:
6148 /* This should have been lowered to the builtins below. */
6149 gcc_unreachable ();
6151 case BUILT_IN_SETJMP_SETUP:
6152 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6153 and the receiver label. */
6154 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6156 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6157 VOIDmode, EXPAND_NORMAL);
6158 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6159 rtx label_r = label_rtx (label);
6161 /* This is copied from the handling of non-local gotos. */
6162 expand_builtin_setjmp_setup (buf_addr, label_r);
6163 nonlocal_goto_handler_labels
6164 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6165 nonlocal_goto_handler_labels);
6166 /* ??? Do not let expand_label treat us as such since we would
6167 not want to be both on the list of non-local labels and on
6168 the list of forced labels. */
6169 FORCED_LABEL (label) = 0;
6170 return const0_rtx;
6172 break;
6174 case BUILT_IN_SETJMP_DISPATCHER:
6175 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6176 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6178 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6179 rtx label_r = label_rtx (label);
6181 /* Remove the dispatcher label from the list of non-local labels
6182 since the receiver labels have been added to it above. */
6183 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6184 return const0_rtx;
6186 break;
6188 case BUILT_IN_SETJMP_RECEIVER:
6189 /* __builtin_setjmp_receiver is passed the receiver label. */
6190 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6192 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6193 rtx label_r = label_rtx (label);
6195 expand_builtin_setjmp_receiver (label_r);
6196 return const0_rtx;
6198 break;
6200 /* __builtin_longjmp is passed a pointer to an array of five words.
6201 It's similar to the C library longjmp function but works with
6202 __builtin_setjmp above. */
6203 case BUILT_IN_LONGJMP:
6204 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6206 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6207 VOIDmode, EXPAND_NORMAL);
6208 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6210 if (value != const1_rtx)
6212 error ("%<__builtin_longjmp%> second argument must be 1");
6213 return const0_rtx;
6216 expand_builtin_longjmp (buf_addr, value);
6217 return const0_rtx;
6219 break;
6221 case BUILT_IN_NONLOCAL_GOTO:
6222 target = expand_builtin_nonlocal_goto (exp);
6223 if (target)
6224 return target;
6225 break;
6227 /* This updates the setjmp buffer that is its argument with the value
6228 of the current stack pointer. */
6229 case BUILT_IN_UPDATE_SETJMP_BUF:
6230 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6232 rtx buf_addr
6233 = expand_normal (CALL_EXPR_ARG (exp, 0));
6235 expand_builtin_update_setjmp_buf (buf_addr);
6236 return const0_rtx;
6238 break;
6240 case BUILT_IN_TRAP:
6241 expand_builtin_trap ();
6242 return const0_rtx;
6244 case BUILT_IN_UNREACHABLE:
6245 expand_builtin_unreachable ();
6246 return const0_rtx;
6248 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6249 case BUILT_IN_SIGNBITD32:
6250 case BUILT_IN_SIGNBITD64:
6251 case BUILT_IN_SIGNBITD128:
6252 target = expand_builtin_signbit (exp, target);
6253 if (target)
6254 return target;
6255 break;
6257 /* Various hooks for the DWARF 2 __throw routine. */
6258 case BUILT_IN_UNWIND_INIT:
6259 expand_builtin_unwind_init ();
6260 return const0_rtx;
6261 case BUILT_IN_DWARF_CFA:
6262 return virtual_cfa_rtx;
6263 #ifdef DWARF2_UNWIND_INFO
6264 case BUILT_IN_DWARF_SP_COLUMN:
6265 return expand_builtin_dwarf_sp_column ();
6266 case BUILT_IN_INIT_DWARF_REG_SIZES:
6267 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6268 return const0_rtx;
6269 #endif
6270 case BUILT_IN_FROB_RETURN_ADDR:
6271 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6272 case BUILT_IN_EXTRACT_RETURN_ADDR:
6273 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6274 case BUILT_IN_EH_RETURN:
6275 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6276 CALL_EXPR_ARG (exp, 1));
6277 return const0_rtx;
6278 #ifdef EH_RETURN_DATA_REGNO
6279 case BUILT_IN_EH_RETURN_DATA_REGNO:
6280 return expand_builtin_eh_return_data_regno (exp);
6281 #endif
6282 case BUILT_IN_EXTEND_POINTER:
6283 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6284 case BUILT_IN_EH_POINTER:
6285 return expand_builtin_eh_pointer (exp);
6286 case BUILT_IN_EH_FILTER:
6287 return expand_builtin_eh_filter (exp);
6288 case BUILT_IN_EH_COPY_VALUES:
6289 return expand_builtin_eh_copy_values (exp);
6291 case BUILT_IN_VA_START:
6292 return expand_builtin_va_start (exp);
6293 case BUILT_IN_VA_END:
6294 return expand_builtin_va_end (exp);
6295 case BUILT_IN_VA_COPY:
6296 return expand_builtin_va_copy (exp);
6297 case BUILT_IN_EXPECT:
6298 return expand_builtin_expect (exp, target);
6299 case BUILT_IN_ASSUME_ALIGNED:
6300 return expand_builtin_assume_aligned (exp, target);
6301 case BUILT_IN_PREFETCH:
6302 expand_builtin_prefetch (exp);
6303 return const0_rtx;
6305 case BUILT_IN_INIT_TRAMPOLINE:
6306 return expand_builtin_init_trampoline (exp);
6307 case BUILT_IN_ADJUST_TRAMPOLINE:
6308 return expand_builtin_adjust_trampoline (exp);
6310 case BUILT_IN_FORK:
6311 case BUILT_IN_EXECL:
6312 case BUILT_IN_EXECV:
6313 case BUILT_IN_EXECLP:
6314 case BUILT_IN_EXECLE:
6315 case BUILT_IN_EXECVP:
6316 case BUILT_IN_EXECVE:
6317 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6318 if (target)
6319 return target;
6320 break;
6322 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6323 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6324 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6325 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6326 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6327 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6328 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6329 if (target)
6330 return target;
6331 break;
6333 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6334 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6335 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6336 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6337 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6338 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6339 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6340 if (target)
6341 return target;
6342 break;
6344 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6345 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6346 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6347 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6348 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6349 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6350 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6351 if (target)
6352 return target;
6353 break;
6355 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6356 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6357 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6358 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6359 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6360 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6361 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6362 if (target)
6363 return target;
6364 break;
6366 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6367 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6368 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6369 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6370 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6371 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6372 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6373 if (target)
6374 return target;
6375 break;
6377 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6378 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6379 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6380 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6381 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6382 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6383 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6384 if (target)
6385 return target;
6386 break;
6388 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6389 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6390 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6391 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6392 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6393 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6394 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6395 if (target)
6396 return target;
6397 break;
6399 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6400 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6401 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6402 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6403 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6405 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6406 if (target)
6407 return target;
6408 break;
6410 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6411 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6412 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6413 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6414 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6415 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6416 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6417 if (target)
6418 return target;
6419 break;
6421 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6422 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6423 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6424 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6425 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6426 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6427 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6428 if (target)
6429 return target;
6430 break;
6432 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6433 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6434 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6435 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6436 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6437 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6438 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6439 if (target)
6440 return target;
6441 break;
6443 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6444 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6445 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6446 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6447 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6448 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6449 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6450 if (target)
6451 return target;
6452 break;
6454 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6455 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6456 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6458 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6459 if (mode == VOIDmode)
6460 mode = TYPE_MODE (boolean_type_node);
6461 if (!target || !register_operand (target, mode))
6462 target = gen_reg_rtx (mode);
6464 mode = get_builtin_sync_mode
6465 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6466 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6467 if (target)
6468 return target;
6469 break;
6471 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6472 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6473 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6474 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6475 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6476 mode = get_builtin_sync_mode
6477 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6478 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6479 if (target)
6480 return target;
6481 break;
6483 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6484 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6485 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6486 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6487 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6488 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6489 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6490 if (target)
6491 return target;
6492 break;
6494 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6495 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6496 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6497 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6498 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6499 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6500 expand_builtin_sync_lock_release (mode, exp);
6501 return const0_rtx;
6503 case BUILT_IN_SYNC_SYNCHRONIZE:
6504 expand_builtin_sync_synchronize ();
6505 return const0_rtx;
6507 case BUILT_IN_ATOMIC_EXCHANGE_1:
6508 case BUILT_IN_ATOMIC_EXCHANGE_2:
6509 case BUILT_IN_ATOMIC_EXCHANGE_4:
6510 case BUILT_IN_ATOMIC_EXCHANGE_8:
6511 case BUILT_IN_ATOMIC_EXCHANGE_16:
6512 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6513 target = expand_builtin_atomic_exchange (mode, exp, target);
6514 if (target)
6515 return target;
6516 break;
6518 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6519 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6522 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6524 unsigned int nargs, z;
6525 VEC(tree,gc) *vec;
6527 mode =
6528 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6529 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6530 if (target)
6531 return target;
6533 /* If this is turned into an external library call, the weak parameter
6534 must be dropped to match the expected parameter list. */
6535 nargs = call_expr_nargs (exp);
6536 vec = VEC_alloc (tree, gc, nargs - 1);
6537 for (z = 0; z < 3; z++)
6538 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6539 /* Skip the boolean weak parameter. */
6540 for (z = 4; z < 6; z++)
6541 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6542 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6543 break;
6546 case BUILT_IN_ATOMIC_LOAD_1:
6547 case BUILT_IN_ATOMIC_LOAD_2:
6548 case BUILT_IN_ATOMIC_LOAD_4:
6549 case BUILT_IN_ATOMIC_LOAD_8:
6550 case BUILT_IN_ATOMIC_LOAD_16:
6551 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6552 target = expand_builtin_atomic_load (mode, exp, target);
6553 if (target)
6554 return target;
6555 break;
6557 case BUILT_IN_ATOMIC_STORE_1:
6558 case BUILT_IN_ATOMIC_STORE_2:
6559 case BUILT_IN_ATOMIC_STORE_4:
6560 case BUILT_IN_ATOMIC_STORE_8:
6561 case BUILT_IN_ATOMIC_STORE_16:
6562 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6563 target = expand_builtin_atomic_store (mode, exp);
6564 if (target)
6565 return const0_rtx;
6566 break;
6568 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6569 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6570 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6571 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6572 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6574 enum built_in_function lib;
6575 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6576 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6577 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6578 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6579 ignore, lib);
6580 if (target)
6581 return target;
6582 break;
6584 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6585 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6586 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6587 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6588 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6590 enum built_in_function lib;
6591 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6592 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6593 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6594 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6595 ignore, lib);
6596 if (target)
6597 return target;
6598 break;
6600 case BUILT_IN_ATOMIC_AND_FETCH_1:
6601 case BUILT_IN_ATOMIC_AND_FETCH_2:
6602 case BUILT_IN_ATOMIC_AND_FETCH_4:
6603 case BUILT_IN_ATOMIC_AND_FETCH_8:
6604 case BUILT_IN_ATOMIC_AND_FETCH_16:
6606 enum built_in_function lib;
6607 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6608 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6609 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6610 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6611 ignore, lib);
6612 if (target)
6613 return target;
6614 break;
6616 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6617 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6618 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6619 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6620 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6622 enum built_in_function lib;
6623 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6624 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6625 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6626 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6627 ignore, lib);
6628 if (target)
6629 return target;
6630 break;
6632 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6633 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6634 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6635 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6636 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6638 enum built_in_function lib;
6639 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6640 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6641 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6642 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6643 ignore, lib);
6644 if (target)
6645 return target;
6646 break;
6648 case BUILT_IN_ATOMIC_OR_FETCH_1:
6649 case BUILT_IN_ATOMIC_OR_FETCH_2:
6650 case BUILT_IN_ATOMIC_OR_FETCH_4:
6651 case BUILT_IN_ATOMIC_OR_FETCH_8:
6652 case BUILT_IN_ATOMIC_OR_FETCH_16:
6654 enum built_in_function lib;
6655 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6656 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6657 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6658 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6659 ignore, lib);
6660 if (target)
6661 return target;
6662 break;
6664 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6665 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6666 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6667 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6668 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6669 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6670 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6671 ignore, BUILT_IN_NONE);
6672 if (target)
6673 return target;
6674 break;
6676 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6677 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6678 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6679 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6680 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6681 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6682 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6683 ignore, BUILT_IN_NONE);
6684 if (target)
6685 return target;
6686 break;
6688 case BUILT_IN_ATOMIC_FETCH_AND_1:
6689 case BUILT_IN_ATOMIC_FETCH_AND_2:
6690 case BUILT_IN_ATOMIC_FETCH_AND_4:
6691 case BUILT_IN_ATOMIC_FETCH_AND_8:
6692 case BUILT_IN_ATOMIC_FETCH_AND_16:
6693 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6694 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6695 ignore, BUILT_IN_NONE);
6696 if (target)
6697 return target;
6698 break;
6700 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6701 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6702 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6703 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6704 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6705 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6706 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6707 ignore, BUILT_IN_NONE);
6708 if (target)
6709 return target;
6710 break;
6712 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6713 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6714 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6715 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6716 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6717 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6718 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6719 ignore, BUILT_IN_NONE);
6720 if (target)
6721 return target;
6722 break;
6724 case BUILT_IN_ATOMIC_FETCH_OR_1:
6725 case BUILT_IN_ATOMIC_FETCH_OR_2:
6726 case BUILT_IN_ATOMIC_FETCH_OR_4:
6727 case BUILT_IN_ATOMIC_FETCH_OR_8:
6728 case BUILT_IN_ATOMIC_FETCH_OR_16:
6729 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6730 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6731 ignore, BUILT_IN_NONE);
6732 if (target)
6733 return target;
6734 break;
6736 case BUILT_IN_ATOMIC_TEST_AND_SET:
6737 return expand_builtin_atomic_test_and_set (exp, target);
6739 case BUILT_IN_ATOMIC_CLEAR:
6740 return expand_builtin_atomic_clear (exp);
6742 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6743 return expand_builtin_atomic_always_lock_free (exp);
6745 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6746 target = expand_builtin_atomic_is_lock_free (exp);
6747 if (target)
6748 return target;
6749 break;
6751 case BUILT_IN_ATOMIC_THREAD_FENCE:
6752 expand_builtin_atomic_thread_fence (exp);
6753 return const0_rtx;
6755 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6756 expand_builtin_atomic_signal_fence (exp);
6757 return const0_rtx;
6759 case BUILT_IN_OBJECT_SIZE:
6760 return expand_builtin_object_size (exp);
6762 case BUILT_IN_MEMCPY_CHK:
6763 case BUILT_IN_MEMPCPY_CHK:
6764 case BUILT_IN_MEMMOVE_CHK:
6765 case BUILT_IN_MEMSET_CHK:
6766 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6767 if (target)
6768 return target;
6769 break;
6771 case BUILT_IN_STRCPY_CHK:
6772 case BUILT_IN_STPCPY_CHK:
6773 case BUILT_IN_STRNCPY_CHK:
6774 case BUILT_IN_STRCAT_CHK:
6775 case BUILT_IN_STRNCAT_CHK:
6776 case BUILT_IN_SNPRINTF_CHK:
6777 case BUILT_IN_VSNPRINTF_CHK:
6778 maybe_emit_chk_warning (exp, fcode);
6779 break;
6781 case BUILT_IN_SPRINTF_CHK:
6782 case BUILT_IN_VSPRINTF_CHK:
6783 maybe_emit_sprintf_chk_warning (exp, fcode);
6784 break;
6786 case BUILT_IN_FREE:
6787 if (warn_free_nonheap_object)
6788 maybe_emit_free_warning (exp);
6789 break;
6791 default: /* just do library call, if unknown builtin */
6792 break;
6795 /* The switch statement above can drop through to cause the function
6796 to be called normally. */
6797 return expand_call (exp, target, ignore);
6800 /* Determine whether a tree node represents a call to a built-in
6801 function. If the tree T is a call to a built-in function with
6802 the right number of arguments of the appropriate types, return
6803 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6804 Otherwise the return value is END_BUILTINS. */
6806 enum built_in_function
6807 builtin_mathfn_code (const_tree t)
6809 const_tree fndecl, arg, parmlist;
6810 const_tree argtype, parmtype;
6811 const_call_expr_arg_iterator iter;
6813 if (TREE_CODE (t) != CALL_EXPR
6814 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6815 return END_BUILTINS;
6817 fndecl = get_callee_fndecl (t);
6818 if (fndecl == NULL_TREE
6819 || TREE_CODE (fndecl) != FUNCTION_DECL
6820 || ! DECL_BUILT_IN (fndecl)
6821 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6822 return END_BUILTINS;
6824 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6825 init_const_call_expr_arg_iterator (t, &iter);
6826 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6828 /* If a function doesn't take a variable number of arguments,
6829 the last element in the list will have type `void'. */
6830 parmtype = TREE_VALUE (parmlist);
6831 if (VOID_TYPE_P (parmtype))
6833 if (more_const_call_expr_args_p (&iter))
6834 return END_BUILTINS;
6835 return DECL_FUNCTION_CODE (fndecl);
6838 if (! more_const_call_expr_args_p (&iter))
6839 return END_BUILTINS;
6841 arg = next_const_call_expr_arg (&iter);
6842 argtype = TREE_TYPE (arg);
6844 if (SCALAR_FLOAT_TYPE_P (parmtype))
6846 if (! SCALAR_FLOAT_TYPE_P (argtype))
6847 return END_BUILTINS;
6849 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6851 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6852 return END_BUILTINS;
6854 else if (POINTER_TYPE_P (parmtype))
6856 if (! POINTER_TYPE_P (argtype))
6857 return END_BUILTINS;
6859 else if (INTEGRAL_TYPE_P (parmtype))
6861 if (! INTEGRAL_TYPE_P (argtype))
6862 return END_BUILTINS;
6864 else
6865 return END_BUILTINS;
6868 /* Variable-length argument list. */
6869 return DECL_FUNCTION_CODE (fndecl);
6872 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6873 evaluate to a constant. */
6875 static tree
6876 fold_builtin_constant_p (tree arg)
6878 /* We return 1 for a numeric type that's known to be a constant
6879 value at compile-time or for an aggregate type that's a
6880 literal constant. */
6881 STRIP_NOPS (arg);
6883 /* If we know this is a constant, emit the constant of one. */
6884 if (CONSTANT_CLASS_P (arg)
6885 || (TREE_CODE (arg) == CONSTRUCTOR
6886 && TREE_CONSTANT (arg)))
6887 return integer_one_node;
6888 if (TREE_CODE (arg) == ADDR_EXPR)
6890 tree op = TREE_OPERAND (arg, 0);
6891 if (TREE_CODE (op) == STRING_CST
6892 || (TREE_CODE (op) == ARRAY_REF
6893 && integer_zerop (TREE_OPERAND (op, 1))
6894 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6895 return integer_one_node;
6898 /* If this expression has side effects, show we don't know it to be a
6899 constant. Likewise if it's a pointer or aggregate type since in
6900 those case we only want literals, since those are only optimized
6901 when generating RTL, not later.
6902 And finally, if we are compiling an initializer, not code, we
6903 need to return a definite result now; there's not going to be any
6904 more optimization done. */
6905 if (TREE_SIDE_EFFECTS (arg)
6906 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6907 || POINTER_TYPE_P (TREE_TYPE (arg))
6908 || cfun == 0
6909 || folding_initializer)
6910 return integer_zero_node;
6912 return NULL_TREE;
6915 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6916 return it as a truthvalue. */
6918 static tree
6919 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6921 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6923 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6924 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6925 ret_type = TREE_TYPE (TREE_TYPE (fn));
6926 pred_type = TREE_VALUE (arg_types);
6927 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6929 pred = fold_convert_loc (loc, pred_type, pred);
6930 expected = fold_convert_loc (loc, expected_type, expected);
6931 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6933 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6934 build_int_cst (ret_type, 0));
6937 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6938 NULL_TREE if no simplification is possible. */
6940 static tree
6941 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6943 tree inner, fndecl, inner_arg0;
6944 enum tree_code code;
6946 /* Distribute the expected value over short-circuiting operators.
6947 See through the cast from truthvalue_type_node to long. */
6948 inner_arg0 = arg0;
6949 while (TREE_CODE (inner_arg0) == NOP_EXPR
6950 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6951 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6952 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6954 /* If this is a builtin_expect within a builtin_expect keep the
6955 inner one. See through a comparison against a constant. It
6956 might have been added to create a thruthvalue. */
6957 inner = inner_arg0;
6959 if (COMPARISON_CLASS_P (inner)
6960 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6961 inner = TREE_OPERAND (inner, 0);
6963 if (TREE_CODE (inner) == CALL_EXPR
6964 && (fndecl = get_callee_fndecl (inner))
6965 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6966 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6967 return arg0;
6969 inner = inner_arg0;
6970 code = TREE_CODE (inner);
6971 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6973 tree op0 = TREE_OPERAND (inner, 0);
6974 tree op1 = TREE_OPERAND (inner, 1);
6976 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6977 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6978 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6980 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6983 /* If the argument isn't invariant then there's nothing else we can do. */
6984 if (!TREE_CONSTANT (inner_arg0))
6985 return NULL_TREE;
6987 /* If we expect that a comparison against the argument will fold to
6988 a constant return the constant. In practice, this means a true
6989 constant or the address of a non-weak symbol. */
6990 inner = inner_arg0;
6991 STRIP_NOPS (inner);
6992 if (TREE_CODE (inner) == ADDR_EXPR)
6996 inner = TREE_OPERAND (inner, 0);
6998 while (TREE_CODE (inner) == COMPONENT_REF
6999 || TREE_CODE (inner) == ARRAY_REF);
7000 if ((TREE_CODE (inner) == VAR_DECL
7001 || TREE_CODE (inner) == FUNCTION_DECL)
7002 && DECL_WEAK (inner))
7003 return NULL_TREE;
7006 /* Otherwise, ARG0 already has the proper type for the return value. */
7007 return arg0;
7010 /* Fold a call to __builtin_classify_type with argument ARG. */
7012 static tree
7013 fold_builtin_classify_type (tree arg)
7015 if (arg == 0)
7016 return build_int_cst (integer_type_node, no_type_class);
7018 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7021 /* Fold a call to __builtin_strlen with argument ARG. */
7023 static tree
7024 fold_builtin_strlen (location_t loc, tree type, tree arg)
7026 if (!validate_arg (arg, POINTER_TYPE))
7027 return NULL_TREE;
7028 else
7030 tree len = c_strlen (arg, 0);
7032 if (len)
7033 return fold_convert_loc (loc, type, len);
7035 return NULL_TREE;
7039 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7041 static tree
7042 fold_builtin_inf (location_t loc, tree type, int warn)
7044 REAL_VALUE_TYPE real;
7046 /* __builtin_inff is intended to be usable to define INFINITY on all
7047 targets. If an infinity is not available, INFINITY expands "to a
7048 positive constant of type float that overflows at translation
7049 time", footnote "In this case, using INFINITY will violate the
7050 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7051 Thus we pedwarn to ensure this constraint violation is
7052 diagnosed. */
7053 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7054 pedwarn (loc, 0, "target format does not support infinity");
7056 real_inf (&real);
7057 return build_real (type, real);
7060 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7062 static tree
7063 fold_builtin_nan (tree arg, tree type, int quiet)
7065 REAL_VALUE_TYPE real;
7066 const char *str;
7068 if (!validate_arg (arg, POINTER_TYPE))
7069 return NULL_TREE;
7070 str = c_getstr (arg);
7071 if (!str)
7072 return NULL_TREE;
7074 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7075 return NULL_TREE;
7077 return build_real (type, real);
7080 /* Return true if the floating point expression T has an integer value.
7081 We also allow +Inf, -Inf and NaN to be considered integer values. */
7083 static bool
7084 integer_valued_real_p (tree t)
7086 switch (TREE_CODE (t))
7088 case FLOAT_EXPR:
7089 return true;
7091 case ABS_EXPR:
7092 case SAVE_EXPR:
7093 return integer_valued_real_p (TREE_OPERAND (t, 0));
7095 case COMPOUND_EXPR:
7096 case MODIFY_EXPR:
7097 case BIND_EXPR:
7098 return integer_valued_real_p (TREE_OPERAND (t, 1));
7100 case PLUS_EXPR:
7101 case MINUS_EXPR:
7102 case MULT_EXPR:
7103 case MIN_EXPR:
7104 case MAX_EXPR:
7105 return integer_valued_real_p (TREE_OPERAND (t, 0))
7106 && integer_valued_real_p (TREE_OPERAND (t, 1));
7108 case COND_EXPR:
7109 return integer_valued_real_p (TREE_OPERAND (t, 1))
7110 && integer_valued_real_p (TREE_OPERAND (t, 2));
7112 case REAL_CST:
7113 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7115 case NOP_EXPR:
7117 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7118 if (TREE_CODE (type) == INTEGER_TYPE)
7119 return true;
7120 if (TREE_CODE (type) == REAL_TYPE)
7121 return integer_valued_real_p (TREE_OPERAND (t, 0));
7122 break;
7125 case CALL_EXPR:
7126 switch (builtin_mathfn_code (t))
7128 CASE_FLT_FN (BUILT_IN_CEIL):
7129 CASE_FLT_FN (BUILT_IN_FLOOR):
7130 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7131 CASE_FLT_FN (BUILT_IN_RINT):
7132 CASE_FLT_FN (BUILT_IN_ROUND):
7133 CASE_FLT_FN (BUILT_IN_TRUNC):
7134 return true;
7136 CASE_FLT_FN (BUILT_IN_FMIN):
7137 CASE_FLT_FN (BUILT_IN_FMAX):
7138 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7139 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7141 default:
7142 break;
7144 break;
7146 default:
7147 break;
7149 return false;
7152 /* FNDECL is assumed to be a builtin where truncation can be propagated
7153 across (for instance floor((double)f) == (double)floorf (f).
7154 Do the transformation for a call with argument ARG. */
7156 static tree
7157 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7159 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7161 if (!validate_arg (arg, REAL_TYPE))
7162 return NULL_TREE;
7164 /* Integer rounding functions are idempotent. */
7165 if (fcode == builtin_mathfn_code (arg))
7166 return arg;
7168 /* If argument is already integer valued, and we don't need to worry
7169 about setting errno, there's no need to perform rounding. */
7170 if (! flag_errno_math && integer_valued_real_p (arg))
7171 return arg;
7173 if (optimize)
7175 tree arg0 = strip_float_extensions (arg);
7176 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7177 tree newtype = TREE_TYPE (arg0);
7178 tree decl;
7180 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7181 && (decl = mathfn_built_in (newtype, fcode)))
7182 return fold_convert_loc (loc, ftype,
7183 build_call_expr_loc (loc, decl, 1,
7184 fold_convert_loc (loc,
7185 newtype,
7186 arg0)));
7188 return NULL_TREE;
7191 /* FNDECL is assumed to be builtin which can narrow the FP type of
7192 the argument, for instance lround((double)f) -> lroundf (f).
7193 Do the transformation for a call with argument ARG. */
7195 static tree
7196 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7198 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7200 if (!validate_arg (arg, REAL_TYPE))
7201 return NULL_TREE;
7203 /* If argument is already integer valued, and we don't need to worry
7204 about setting errno, there's no need to perform rounding. */
7205 if (! flag_errno_math && integer_valued_real_p (arg))
7206 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7207 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7209 if (optimize)
7211 tree ftype = TREE_TYPE (arg);
7212 tree arg0 = strip_float_extensions (arg);
7213 tree newtype = TREE_TYPE (arg0);
7214 tree decl;
7216 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7217 && (decl = mathfn_built_in (newtype, fcode)))
7218 return build_call_expr_loc (loc, decl, 1,
7219 fold_convert_loc (loc, newtype, arg0));
7222 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7223 sizeof (int) == sizeof (long). */
7224 if (TYPE_PRECISION (integer_type_node)
7225 == TYPE_PRECISION (long_integer_type_node))
7227 tree newfn = NULL_TREE;
7228 switch (fcode)
7230 CASE_FLT_FN (BUILT_IN_ICEIL):
7231 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7232 break;
7234 CASE_FLT_FN (BUILT_IN_IFLOOR):
7235 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7236 break;
7238 CASE_FLT_FN (BUILT_IN_IROUND):
7239 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7240 break;
7242 CASE_FLT_FN (BUILT_IN_IRINT):
7243 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7244 break;
7246 default:
7247 break;
7250 if (newfn)
7252 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7253 return fold_convert_loc (loc,
7254 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7258 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7259 sizeof (long long) == sizeof (long). */
7260 if (TYPE_PRECISION (long_long_integer_type_node)
7261 == TYPE_PRECISION (long_integer_type_node))
7263 tree newfn = NULL_TREE;
7264 switch (fcode)
7266 CASE_FLT_FN (BUILT_IN_LLCEIL):
7267 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7268 break;
7270 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7271 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7272 break;
7274 CASE_FLT_FN (BUILT_IN_LLROUND):
7275 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7276 break;
7278 CASE_FLT_FN (BUILT_IN_LLRINT):
7279 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7280 break;
7282 default:
7283 break;
7286 if (newfn)
7288 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7289 return fold_convert_loc (loc,
7290 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7294 return NULL_TREE;
7297 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7298 return type. Return NULL_TREE if no simplification can be made. */
7300 static tree
7301 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7303 tree res;
7305 if (!validate_arg (arg, COMPLEX_TYPE)
7306 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7307 return NULL_TREE;
7309 /* Calculate the result when the argument is a constant. */
7310 if (TREE_CODE (arg) == COMPLEX_CST
7311 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7312 type, mpfr_hypot)))
7313 return res;
7315 if (TREE_CODE (arg) == COMPLEX_EXPR)
7317 tree real = TREE_OPERAND (arg, 0);
7318 tree imag = TREE_OPERAND (arg, 1);
7320 /* If either part is zero, cabs is fabs of the other. */
7321 if (real_zerop (real))
7322 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7323 if (real_zerop (imag))
7324 return fold_build1_loc (loc, ABS_EXPR, type, real);
7326 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7327 if (flag_unsafe_math_optimizations
7328 && operand_equal_p (real, imag, OEP_PURE_SAME))
7330 const REAL_VALUE_TYPE sqrt2_trunc
7331 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7332 STRIP_NOPS (real);
7333 return fold_build2_loc (loc, MULT_EXPR, type,
7334 fold_build1_loc (loc, ABS_EXPR, type, real),
7335 build_real (type, sqrt2_trunc));
7339 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7340 if (TREE_CODE (arg) == NEGATE_EXPR
7341 || TREE_CODE (arg) == CONJ_EXPR)
7342 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7344 /* Don't do this when optimizing for size. */
7345 if (flag_unsafe_math_optimizations
7346 && optimize && optimize_function_for_speed_p (cfun))
7348 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7350 if (sqrtfn != NULL_TREE)
7352 tree rpart, ipart, result;
7354 arg = builtin_save_expr (arg);
7356 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7357 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7359 rpart = builtin_save_expr (rpart);
7360 ipart = builtin_save_expr (ipart);
7362 result = fold_build2_loc (loc, PLUS_EXPR, type,
7363 fold_build2_loc (loc, MULT_EXPR, type,
7364 rpart, rpart),
7365 fold_build2_loc (loc, MULT_EXPR, type,
7366 ipart, ipart));
7368 return build_call_expr_loc (loc, sqrtfn, 1, result);
7372 return NULL_TREE;
7375 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7376 complex tree type of the result. If NEG is true, the imaginary
7377 zero is negative. */
7379 static tree
7380 build_complex_cproj (tree type, bool neg)
7382 REAL_VALUE_TYPE rinf, rzero = dconst0;
7384 real_inf (&rinf);
7385 rzero.sign = neg;
7386 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7387 build_real (TREE_TYPE (type), rzero));
7390 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7391 return type. Return NULL_TREE if no simplification can be made. */
7393 static tree
7394 fold_builtin_cproj (location_t loc, tree arg, tree type)
7396 if (!validate_arg (arg, COMPLEX_TYPE)
7397 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7398 return NULL_TREE;
7400 /* If there are no infinities, return arg. */
7401 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7402 return non_lvalue_loc (loc, arg);
7404 /* Calculate the result when the argument is a constant. */
7405 if (TREE_CODE (arg) == COMPLEX_CST)
7407 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7408 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7410 if (real_isinf (real) || real_isinf (imag))
7411 return build_complex_cproj (type, imag->sign);
7412 else
7413 return arg;
7415 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7417 tree real = TREE_OPERAND (arg, 0);
7418 tree imag = TREE_OPERAND (arg, 1);
7420 STRIP_NOPS (real);
7421 STRIP_NOPS (imag);
7423 /* If the real part is inf and the imag part is known to be
7424 nonnegative, return (inf + 0i). Remember side-effects are
7425 possible in the imag part. */
7426 if (TREE_CODE (real) == REAL_CST
7427 && real_isinf (TREE_REAL_CST_PTR (real))
7428 && tree_expr_nonnegative_p (imag))
7429 return omit_one_operand_loc (loc, type,
7430 build_complex_cproj (type, false),
7431 arg);
7433 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7434 Remember side-effects are possible in the real part. */
7435 if (TREE_CODE (imag) == REAL_CST
7436 && real_isinf (TREE_REAL_CST_PTR (imag)))
7437 return
7438 omit_one_operand_loc (loc, type,
7439 build_complex_cproj (type, TREE_REAL_CST_PTR
7440 (imag)->sign), arg);
7443 return NULL_TREE;
7446 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7447 Return NULL_TREE if no simplification can be made. */
7449 static tree
7450 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7453 enum built_in_function fcode;
7454 tree res;
7456 if (!validate_arg (arg, REAL_TYPE))
7457 return NULL_TREE;
7459 /* Calculate the result when the argument is a constant. */
7460 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7461 return res;
7463 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7464 fcode = builtin_mathfn_code (arg);
7465 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7467 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7468 arg = fold_build2_loc (loc, MULT_EXPR, type,
7469 CALL_EXPR_ARG (arg, 0),
7470 build_real (type, dconsthalf));
7471 return build_call_expr_loc (loc, expfn, 1, arg);
7474 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7475 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7477 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7479 if (powfn)
7481 tree arg0 = CALL_EXPR_ARG (arg, 0);
7482 tree tree_root;
7483 /* The inner root was either sqrt or cbrt. */
7484 /* This was a conditional expression but it triggered a bug
7485 in Sun C 5.5. */
7486 REAL_VALUE_TYPE dconstroot;
7487 if (BUILTIN_SQRT_P (fcode))
7488 dconstroot = dconsthalf;
7489 else
7490 dconstroot = dconst_third ();
7492 /* Adjust for the outer root. */
7493 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7494 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7495 tree_root = build_real (type, dconstroot);
7496 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7500 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7501 if (flag_unsafe_math_optimizations
7502 && (fcode == BUILT_IN_POW
7503 || fcode == BUILT_IN_POWF
7504 || fcode == BUILT_IN_POWL))
7506 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7507 tree arg0 = CALL_EXPR_ARG (arg, 0);
7508 tree arg1 = CALL_EXPR_ARG (arg, 1);
7509 tree narg1;
7510 if (!tree_expr_nonnegative_p (arg0))
7511 arg0 = build1 (ABS_EXPR, type, arg0);
7512 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7513 build_real (type, dconsthalf));
7514 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7517 return NULL_TREE;
7520 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7521 Return NULL_TREE if no simplification can be made. */
7523 static tree
7524 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7526 const enum built_in_function fcode = builtin_mathfn_code (arg);
7527 tree res;
7529 if (!validate_arg (arg, REAL_TYPE))
7530 return NULL_TREE;
7532 /* Calculate the result when the argument is a constant. */
7533 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7534 return res;
7536 if (flag_unsafe_math_optimizations)
7538 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7539 if (BUILTIN_EXPONENT_P (fcode))
7541 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7542 const REAL_VALUE_TYPE third_trunc =
7543 real_value_truncate (TYPE_MODE (type), dconst_third ());
7544 arg = fold_build2_loc (loc, MULT_EXPR, type,
7545 CALL_EXPR_ARG (arg, 0),
7546 build_real (type, third_trunc));
7547 return build_call_expr_loc (loc, expfn, 1, arg);
7550 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7551 if (BUILTIN_SQRT_P (fcode))
7553 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7555 if (powfn)
7557 tree arg0 = CALL_EXPR_ARG (arg, 0);
7558 tree tree_root;
7559 REAL_VALUE_TYPE dconstroot = dconst_third ();
7561 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7562 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7563 tree_root = build_real (type, dconstroot);
7564 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7568 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7569 if (BUILTIN_CBRT_P (fcode))
7571 tree arg0 = CALL_EXPR_ARG (arg, 0);
7572 if (tree_expr_nonnegative_p (arg0))
7574 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7576 if (powfn)
7578 tree tree_root;
7579 REAL_VALUE_TYPE dconstroot;
7581 real_arithmetic (&dconstroot, MULT_EXPR,
7582 dconst_third_ptr (), dconst_third_ptr ());
7583 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7584 tree_root = build_real (type, dconstroot);
7585 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7590 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7591 if (fcode == BUILT_IN_POW
7592 || fcode == BUILT_IN_POWF
7593 || fcode == BUILT_IN_POWL)
7595 tree arg00 = CALL_EXPR_ARG (arg, 0);
7596 tree arg01 = CALL_EXPR_ARG (arg, 1);
7597 if (tree_expr_nonnegative_p (arg00))
7599 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7600 const REAL_VALUE_TYPE dconstroot
7601 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7602 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7603 build_real (type, dconstroot));
7604 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7608 return NULL_TREE;
7611 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7612 TYPE is the type of the return value. Return NULL_TREE if no
7613 simplification can be made. */
7615 static tree
7616 fold_builtin_cos (location_t loc,
7617 tree arg, tree type, tree fndecl)
7619 tree res, narg;
7621 if (!validate_arg (arg, REAL_TYPE))
7622 return NULL_TREE;
7624 /* Calculate the result when the argument is a constant. */
7625 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7626 return res;
7628 /* Optimize cos(-x) into cos (x). */
7629 if ((narg = fold_strip_sign_ops (arg)))
7630 return build_call_expr_loc (loc, fndecl, 1, narg);
7632 return NULL_TREE;
7635 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7636 Return NULL_TREE if no simplification can be made. */
7638 static tree
7639 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7641 if (validate_arg (arg, REAL_TYPE))
7643 tree res, narg;
7645 /* Calculate the result when the argument is a constant. */
7646 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7647 return res;
7649 /* Optimize cosh(-x) into cosh (x). */
7650 if ((narg = fold_strip_sign_ops (arg)))
7651 return build_call_expr_loc (loc, fndecl, 1, narg);
7654 return NULL_TREE;
7657 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7658 argument ARG. TYPE is the type of the return value. Return
7659 NULL_TREE if no simplification can be made. */
7661 static tree
7662 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7663 bool hyper)
7665 if (validate_arg (arg, COMPLEX_TYPE)
7666 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7668 tree tmp;
7670 /* Calculate the result when the argument is a constant. */
7671 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7672 return tmp;
7674 /* Optimize fn(-x) into fn(x). */
7675 if ((tmp = fold_strip_sign_ops (arg)))
7676 return build_call_expr_loc (loc, fndecl, 1, tmp);
7679 return NULL_TREE;
7682 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7683 Return NULL_TREE if no simplification can be made. */
7685 static tree
7686 fold_builtin_tan (tree arg, tree type)
7688 enum built_in_function fcode;
7689 tree res;
7691 if (!validate_arg (arg, REAL_TYPE))
7692 return NULL_TREE;
7694 /* Calculate the result when the argument is a constant. */
7695 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7696 return res;
7698 /* Optimize tan(atan(x)) = x. */
7699 fcode = builtin_mathfn_code (arg);
7700 if (flag_unsafe_math_optimizations
7701 && (fcode == BUILT_IN_ATAN
7702 || fcode == BUILT_IN_ATANF
7703 || fcode == BUILT_IN_ATANL))
7704 return CALL_EXPR_ARG (arg, 0);
7706 return NULL_TREE;
7709 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7710 NULL_TREE if no simplification can be made. */
7712 static tree
7713 fold_builtin_sincos (location_t loc,
7714 tree arg0, tree arg1, tree arg2)
7716 tree type;
7717 tree res, fn, call;
7719 if (!validate_arg (arg0, REAL_TYPE)
7720 || !validate_arg (arg1, POINTER_TYPE)
7721 || !validate_arg (arg2, POINTER_TYPE))
7722 return NULL_TREE;
7724 type = TREE_TYPE (arg0);
7726 /* Calculate the result when the argument is a constant. */
7727 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7728 return res;
7730 /* Canonicalize sincos to cexpi. */
7731 if (!TARGET_C99_FUNCTIONS)
7732 return NULL_TREE;
7733 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7734 if (!fn)
7735 return NULL_TREE;
7737 call = build_call_expr_loc (loc, fn, 1, arg0);
7738 call = builtin_save_expr (call);
7740 return build2 (COMPOUND_EXPR, void_type_node,
7741 build2 (MODIFY_EXPR, void_type_node,
7742 build_fold_indirect_ref_loc (loc, arg1),
7743 build1 (IMAGPART_EXPR, type, call)),
7744 build2 (MODIFY_EXPR, void_type_node,
7745 build_fold_indirect_ref_loc (loc, arg2),
7746 build1 (REALPART_EXPR, type, call)));
7749 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7750 NULL_TREE if no simplification can be made. */
7752 static tree
7753 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7755 tree rtype;
7756 tree realp, imagp, ifn;
7757 tree res;
7759 if (!validate_arg (arg0, COMPLEX_TYPE)
7760 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7761 return NULL_TREE;
7763 /* Calculate the result when the argument is a constant. */
7764 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7765 return res;
7767 rtype = TREE_TYPE (TREE_TYPE (arg0));
7769 /* In case we can figure out the real part of arg0 and it is constant zero
7770 fold to cexpi. */
7771 if (!TARGET_C99_FUNCTIONS)
7772 return NULL_TREE;
7773 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7774 if (!ifn)
7775 return NULL_TREE;
7777 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7778 && real_zerop (realp))
7780 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7781 return build_call_expr_loc (loc, ifn, 1, narg);
7784 /* In case we can easily decompose real and imaginary parts split cexp
7785 to exp (r) * cexpi (i). */
7786 if (flag_unsafe_math_optimizations
7787 && realp)
7789 tree rfn, rcall, icall;
7791 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7792 if (!rfn)
7793 return NULL_TREE;
7795 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7796 if (!imagp)
7797 return NULL_TREE;
7799 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7800 icall = builtin_save_expr (icall);
7801 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7802 rcall = builtin_save_expr (rcall);
7803 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7804 fold_build2_loc (loc, MULT_EXPR, rtype,
7805 rcall,
7806 fold_build1_loc (loc, REALPART_EXPR,
7807 rtype, icall)),
7808 fold_build2_loc (loc, MULT_EXPR, rtype,
7809 rcall,
7810 fold_build1_loc (loc, IMAGPART_EXPR,
7811 rtype, icall)));
7814 return NULL_TREE;
7817 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7818 Return NULL_TREE if no simplification can be made. */
7820 static tree
7821 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7823 if (!validate_arg (arg, REAL_TYPE))
7824 return NULL_TREE;
7826 /* Optimize trunc of constant value. */
7827 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7829 REAL_VALUE_TYPE r, x;
7830 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7832 x = TREE_REAL_CST (arg);
7833 real_trunc (&r, TYPE_MODE (type), &x);
7834 return build_real (type, r);
7837 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7840 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7841 Return NULL_TREE if no simplification can be made. */
7843 static tree
7844 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7846 if (!validate_arg (arg, REAL_TYPE))
7847 return NULL_TREE;
7849 /* Optimize floor of constant value. */
7850 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7852 REAL_VALUE_TYPE x;
7854 x = TREE_REAL_CST (arg);
7855 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7857 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7858 REAL_VALUE_TYPE r;
7860 real_floor (&r, TYPE_MODE (type), &x);
7861 return build_real (type, r);
7865 /* Fold floor (x) where x is nonnegative to trunc (x). */
7866 if (tree_expr_nonnegative_p (arg))
7868 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7869 if (truncfn)
7870 return build_call_expr_loc (loc, truncfn, 1, arg);
7873 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7876 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7877 Return NULL_TREE if no simplification can be made. */
7879 static tree
7880 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7882 if (!validate_arg (arg, REAL_TYPE))
7883 return NULL_TREE;
7885 /* Optimize ceil of constant value. */
7886 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7888 REAL_VALUE_TYPE x;
7890 x = TREE_REAL_CST (arg);
7891 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7893 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7894 REAL_VALUE_TYPE r;
7896 real_ceil (&r, TYPE_MODE (type), &x);
7897 return build_real (type, r);
7901 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7904 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7905 Return NULL_TREE if no simplification can be made. */
7907 static tree
7908 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7910 if (!validate_arg (arg, REAL_TYPE))
7911 return NULL_TREE;
7913 /* Optimize round of constant value. */
7914 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7916 REAL_VALUE_TYPE x;
7918 x = TREE_REAL_CST (arg);
7919 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7921 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7922 REAL_VALUE_TYPE r;
7924 real_round (&r, TYPE_MODE (type), &x);
7925 return build_real (type, r);
7929 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7932 /* Fold function call to builtin lround, lroundf or lroundl (or the
7933 corresponding long long versions) and other rounding functions. ARG
7934 is the argument to the call. Return NULL_TREE if no simplification
7935 can be made. */
7937 static tree
7938 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7940 if (!validate_arg (arg, REAL_TYPE))
7941 return NULL_TREE;
7943 /* Optimize lround of constant value. */
7944 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7946 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7948 if (real_isfinite (&x))
7950 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7951 tree ftype = TREE_TYPE (arg);
7952 double_int val;
7953 REAL_VALUE_TYPE r;
7955 switch (DECL_FUNCTION_CODE (fndecl))
7957 CASE_FLT_FN (BUILT_IN_IFLOOR):
7958 CASE_FLT_FN (BUILT_IN_LFLOOR):
7959 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7960 real_floor (&r, TYPE_MODE (ftype), &x);
7961 break;
7963 CASE_FLT_FN (BUILT_IN_ICEIL):
7964 CASE_FLT_FN (BUILT_IN_LCEIL):
7965 CASE_FLT_FN (BUILT_IN_LLCEIL):
7966 real_ceil (&r, TYPE_MODE (ftype), &x);
7967 break;
7969 CASE_FLT_FN (BUILT_IN_IROUND):
7970 CASE_FLT_FN (BUILT_IN_LROUND):
7971 CASE_FLT_FN (BUILT_IN_LLROUND):
7972 real_round (&r, TYPE_MODE (ftype), &x);
7973 break;
7975 default:
7976 gcc_unreachable ();
7979 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7980 if (double_int_fits_to_tree_p (itype, val))
7981 return double_int_to_tree (itype, val);
7985 switch (DECL_FUNCTION_CODE (fndecl))
7987 CASE_FLT_FN (BUILT_IN_LFLOOR):
7988 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7989 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7990 if (tree_expr_nonnegative_p (arg))
7991 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7992 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7993 break;
7994 default:;
7997 return fold_fixed_mathfn (loc, fndecl, arg);
8000 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8001 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8002 the argument to the call. Return NULL_TREE if no simplification can
8003 be made. */
8005 static tree
8006 fold_builtin_bitop (tree fndecl, tree arg)
8008 if (!validate_arg (arg, INTEGER_TYPE))
8009 return NULL_TREE;
8011 /* Optimize for constant argument. */
8012 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8014 HOST_WIDE_INT hi, width, result;
8015 unsigned HOST_WIDE_INT lo;
8016 tree type;
8018 type = TREE_TYPE (arg);
8019 width = TYPE_PRECISION (type);
8020 lo = TREE_INT_CST_LOW (arg);
8022 /* Clear all the bits that are beyond the type's precision. */
8023 if (width > HOST_BITS_PER_WIDE_INT)
8025 hi = TREE_INT_CST_HIGH (arg);
8026 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8027 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8028 << (width - HOST_BITS_PER_WIDE_INT));
8030 else
8032 hi = 0;
8033 if (width < HOST_BITS_PER_WIDE_INT)
8034 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8037 switch (DECL_FUNCTION_CODE (fndecl))
8039 CASE_INT_FN (BUILT_IN_FFS):
8040 if (lo != 0)
8041 result = ffs_hwi (lo);
8042 else if (hi != 0)
8043 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8044 else
8045 result = 0;
8046 break;
8048 CASE_INT_FN (BUILT_IN_CLZ):
8049 if (hi != 0)
8050 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8051 else if (lo != 0)
8052 result = width - floor_log2 (lo) - 1;
8053 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8054 result = width;
8055 break;
8057 CASE_INT_FN (BUILT_IN_CTZ):
8058 if (lo != 0)
8059 result = ctz_hwi (lo);
8060 else if (hi != 0)
8061 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8062 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8063 result = width;
8064 break;
8066 CASE_INT_FN (BUILT_IN_CLRSB):
8067 if (width > HOST_BITS_PER_WIDE_INT
8068 && (hi & ((unsigned HOST_WIDE_INT) 1
8069 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8071 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8072 << (width - HOST_BITS_PER_WIDE_INT - 1));
8073 lo = ~lo;
8075 else if (width <= HOST_BITS_PER_WIDE_INT
8076 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8077 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8078 if (hi != 0)
8079 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8080 else if (lo != 0)
8081 result = width - floor_log2 (lo) - 2;
8082 else
8083 result = width - 1;
8084 break;
8086 CASE_INT_FN (BUILT_IN_POPCOUNT):
8087 result = 0;
8088 while (lo)
8089 result++, lo &= lo - 1;
8090 while (hi)
8091 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8092 break;
8094 CASE_INT_FN (BUILT_IN_PARITY):
8095 result = 0;
8096 while (lo)
8097 result++, lo &= lo - 1;
8098 while (hi)
8099 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8100 result &= 1;
8101 break;
8103 default:
8104 gcc_unreachable ();
8107 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8110 return NULL_TREE;
8113 /* Fold function call to builtin_bswap and the long and long long
8114 variants. Return NULL_TREE if no simplification can be made. */
8115 static tree
8116 fold_builtin_bswap (tree fndecl, tree arg)
8118 if (! validate_arg (arg, INTEGER_TYPE))
8119 return NULL_TREE;
8121 /* Optimize constant value. */
8122 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8124 HOST_WIDE_INT hi, width, r_hi = 0;
8125 unsigned HOST_WIDE_INT lo, r_lo = 0;
8126 tree type;
8128 type = TREE_TYPE (arg);
8129 width = TYPE_PRECISION (type);
8130 lo = TREE_INT_CST_LOW (arg);
8131 hi = TREE_INT_CST_HIGH (arg);
8133 switch (DECL_FUNCTION_CODE (fndecl))
8135 case BUILT_IN_BSWAP32:
8136 case BUILT_IN_BSWAP64:
8138 int s;
8140 for (s = 0; s < width; s += 8)
8142 int d = width - s - 8;
8143 unsigned HOST_WIDE_INT byte;
8145 if (s < HOST_BITS_PER_WIDE_INT)
8146 byte = (lo >> s) & 0xff;
8147 else
8148 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8150 if (d < HOST_BITS_PER_WIDE_INT)
8151 r_lo |= byte << d;
8152 else
8153 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8157 break;
8159 default:
8160 gcc_unreachable ();
8163 if (width < HOST_BITS_PER_WIDE_INT)
8164 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8165 else
8166 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8169 return NULL_TREE;
8172 /* A subroutine of fold_builtin to fold the various logarithmic
8173 functions. Return NULL_TREE if no simplification can me made.
8174 FUNC is the corresponding MPFR logarithm function. */
8176 static tree
8177 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8178 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8180 if (validate_arg (arg, REAL_TYPE))
8182 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8183 tree res;
8184 const enum built_in_function fcode = builtin_mathfn_code (arg);
8186 /* Calculate the result when the argument is a constant. */
8187 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8188 return res;
8190 /* Special case, optimize logN(expN(x)) = x. */
8191 if (flag_unsafe_math_optimizations
8192 && ((func == mpfr_log
8193 && (fcode == BUILT_IN_EXP
8194 || fcode == BUILT_IN_EXPF
8195 || fcode == BUILT_IN_EXPL))
8196 || (func == mpfr_log2
8197 && (fcode == BUILT_IN_EXP2
8198 || fcode == BUILT_IN_EXP2F
8199 || fcode == BUILT_IN_EXP2L))
8200 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8201 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8203 /* Optimize logN(func()) for various exponential functions. We
8204 want to determine the value "x" and the power "exponent" in
8205 order to transform logN(x**exponent) into exponent*logN(x). */
8206 if (flag_unsafe_math_optimizations)
8208 tree exponent = 0, x = 0;
8210 switch (fcode)
8212 CASE_FLT_FN (BUILT_IN_EXP):
8213 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8214 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8215 dconst_e ()));
8216 exponent = CALL_EXPR_ARG (arg, 0);
8217 break;
8218 CASE_FLT_FN (BUILT_IN_EXP2):
8219 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8220 x = build_real (type, dconst2);
8221 exponent = CALL_EXPR_ARG (arg, 0);
8222 break;
8223 CASE_FLT_FN (BUILT_IN_EXP10):
8224 CASE_FLT_FN (BUILT_IN_POW10):
8225 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8227 REAL_VALUE_TYPE dconst10;
8228 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8229 x = build_real (type, dconst10);
8231 exponent = CALL_EXPR_ARG (arg, 0);
8232 break;
8233 CASE_FLT_FN (BUILT_IN_SQRT):
8234 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8235 x = CALL_EXPR_ARG (arg, 0);
8236 exponent = build_real (type, dconsthalf);
8237 break;
8238 CASE_FLT_FN (BUILT_IN_CBRT):
8239 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8240 x = CALL_EXPR_ARG (arg, 0);
8241 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8242 dconst_third ()));
8243 break;
8244 CASE_FLT_FN (BUILT_IN_POW):
8245 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8246 x = CALL_EXPR_ARG (arg, 0);
8247 exponent = CALL_EXPR_ARG (arg, 1);
8248 break;
8249 default:
8250 break;
8253 /* Now perform the optimization. */
8254 if (x && exponent)
8256 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8257 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8262 return NULL_TREE;
8265 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8266 NULL_TREE if no simplification can be made. */
8268 static tree
8269 fold_builtin_hypot (location_t loc, tree fndecl,
8270 tree arg0, tree arg1, tree type)
8272 tree res, narg0, narg1;
8274 if (!validate_arg (arg0, REAL_TYPE)
8275 || !validate_arg (arg1, REAL_TYPE))
8276 return NULL_TREE;
8278 /* Calculate the result when the argument is a constant. */
8279 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8280 return res;
8282 /* If either argument to hypot has a negate or abs, strip that off.
8283 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8284 narg0 = fold_strip_sign_ops (arg0);
8285 narg1 = fold_strip_sign_ops (arg1);
8286 if (narg0 || narg1)
8288 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8289 narg1 ? narg1 : arg1);
8292 /* If either argument is zero, hypot is fabs of the other. */
8293 if (real_zerop (arg0))
8294 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8295 else if (real_zerop (arg1))
8296 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8298 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8299 if (flag_unsafe_math_optimizations
8300 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8302 const REAL_VALUE_TYPE sqrt2_trunc
8303 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8304 return fold_build2_loc (loc, MULT_EXPR, type,
8305 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8306 build_real (type, sqrt2_trunc));
8309 return NULL_TREE;
8313 /* Fold a builtin function call to pow, powf, or powl. Return
8314 NULL_TREE if no simplification can be made. */
8315 static tree
8316 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8318 tree res;
8320 if (!validate_arg (arg0, REAL_TYPE)
8321 || !validate_arg (arg1, REAL_TYPE))
8322 return NULL_TREE;
8324 /* Calculate the result when the argument is a constant. */
8325 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8326 return res;
8328 /* Optimize pow(1.0,y) = 1.0. */
8329 if (real_onep (arg0))
8330 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8332 if (TREE_CODE (arg1) == REAL_CST
8333 && !TREE_OVERFLOW (arg1))
8335 REAL_VALUE_TYPE cint;
8336 REAL_VALUE_TYPE c;
8337 HOST_WIDE_INT n;
8339 c = TREE_REAL_CST (arg1);
8341 /* Optimize pow(x,0.0) = 1.0. */
8342 if (REAL_VALUES_EQUAL (c, dconst0))
8343 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8344 arg0);
8346 /* Optimize pow(x,1.0) = x. */
8347 if (REAL_VALUES_EQUAL (c, dconst1))
8348 return arg0;
8350 /* Optimize pow(x,-1.0) = 1.0/x. */
8351 if (REAL_VALUES_EQUAL (c, dconstm1))
8352 return fold_build2_loc (loc, RDIV_EXPR, type,
8353 build_real (type, dconst1), arg0);
8355 /* Optimize pow(x,0.5) = sqrt(x). */
8356 if (flag_unsafe_math_optimizations
8357 && REAL_VALUES_EQUAL (c, dconsthalf))
8359 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8361 if (sqrtfn != NULL_TREE)
8362 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8365 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8366 if (flag_unsafe_math_optimizations)
8368 const REAL_VALUE_TYPE dconstroot
8369 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8371 if (REAL_VALUES_EQUAL (c, dconstroot))
8373 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8374 if (cbrtfn != NULL_TREE)
8375 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8379 /* Check for an integer exponent. */
8380 n = real_to_integer (&c);
8381 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8382 if (real_identical (&c, &cint))
8384 /* Attempt to evaluate pow at compile-time, unless this should
8385 raise an exception. */
8386 if (TREE_CODE (arg0) == REAL_CST
8387 && !TREE_OVERFLOW (arg0)
8388 && (n > 0
8389 || (!flag_trapping_math && !flag_errno_math)
8390 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8392 REAL_VALUE_TYPE x;
8393 bool inexact;
8395 x = TREE_REAL_CST (arg0);
8396 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8397 if (flag_unsafe_math_optimizations || !inexact)
8398 return build_real (type, x);
8401 /* Strip sign ops from even integer powers. */
8402 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8404 tree narg0 = fold_strip_sign_ops (arg0);
8405 if (narg0)
8406 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8411 if (flag_unsafe_math_optimizations)
8413 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8415 /* Optimize pow(expN(x),y) = expN(x*y). */
8416 if (BUILTIN_EXPONENT_P (fcode))
8418 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8419 tree arg = CALL_EXPR_ARG (arg0, 0);
8420 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8421 return build_call_expr_loc (loc, expfn, 1, arg);
8424 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8425 if (BUILTIN_SQRT_P (fcode))
8427 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8428 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8429 build_real (type, dconsthalf));
8430 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8433 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8434 if (BUILTIN_CBRT_P (fcode))
8436 tree arg = CALL_EXPR_ARG (arg0, 0);
8437 if (tree_expr_nonnegative_p (arg))
8439 const REAL_VALUE_TYPE dconstroot
8440 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8441 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8442 build_real (type, dconstroot));
8443 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8447 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8448 if (fcode == BUILT_IN_POW
8449 || fcode == BUILT_IN_POWF
8450 || fcode == BUILT_IN_POWL)
8452 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8453 if (tree_expr_nonnegative_p (arg00))
8455 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8456 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8457 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8462 return NULL_TREE;
8465 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8466 Return NULL_TREE if no simplification can be made. */
8467 static tree
8468 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8469 tree arg0, tree arg1, tree type)
8471 if (!validate_arg (arg0, REAL_TYPE)
8472 || !validate_arg (arg1, INTEGER_TYPE))
8473 return NULL_TREE;
8475 /* Optimize pow(1.0,y) = 1.0. */
8476 if (real_onep (arg0))
8477 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8479 if (host_integerp (arg1, 0))
8481 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8483 /* Evaluate powi at compile-time. */
8484 if (TREE_CODE (arg0) == REAL_CST
8485 && !TREE_OVERFLOW (arg0))
8487 REAL_VALUE_TYPE x;
8488 x = TREE_REAL_CST (arg0);
8489 real_powi (&x, TYPE_MODE (type), &x, c);
8490 return build_real (type, x);
8493 /* Optimize pow(x,0) = 1.0. */
8494 if (c == 0)
8495 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8496 arg0);
8498 /* Optimize pow(x,1) = x. */
8499 if (c == 1)
8500 return arg0;
8502 /* Optimize pow(x,-1) = 1.0/x. */
8503 if (c == -1)
8504 return fold_build2_loc (loc, RDIV_EXPR, type,
8505 build_real (type, dconst1), arg0);
8508 return NULL_TREE;
8511 /* A subroutine of fold_builtin to fold the various exponent
8512 functions. Return NULL_TREE if no simplification can be made.
8513 FUNC is the corresponding MPFR exponent function. */
8515 static tree
8516 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8517 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8519 if (validate_arg (arg, REAL_TYPE))
8521 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8522 tree res;
8524 /* Calculate the result when the argument is a constant. */
8525 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8526 return res;
8528 /* Optimize expN(logN(x)) = x. */
8529 if (flag_unsafe_math_optimizations)
8531 const enum built_in_function fcode = builtin_mathfn_code (arg);
8533 if ((func == mpfr_exp
8534 && (fcode == BUILT_IN_LOG
8535 || fcode == BUILT_IN_LOGF
8536 || fcode == BUILT_IN_LOGL))
8537 || (func == mpfr_exp2
8538 && (fcode == BUILT_IN_LOG2
8539 || fcode == BUILT_IN_LOG2F
8540 || fcode == BUILT_IN_LOG2L))
8541 || (func == mpfr_exp10
8542 && (fcode == BUILT_IN_LOG10
8543 || fcode == BUILT_IN_LOG10F
8544 || fcode == BUILT_IN_LOG10L)))
8545 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8549 return NULL_TREE;
8552 /* Return true if VAR is a VAR_DECL or a component thereof. */
8554 static bool
8555 var_decl_component_p (tree var)
8557 tree inner = var;
8558 while (handled_component_p (inner))
8559 inner = TREE_OPERAND (inner, 0);
8560 return SSA_VAR_P (inner);
8563 /* Fold function call to builtin memset. Return
8564 NULL_TREE if no simplification can be made. */
8566 static tree
8567 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8568 tree type, bool ignore)
8570 tree var, ret, etype;
8571 unsigned HOST_WIDE_INT length, cval;
8573 if (! validate_arg (dest, POINTER_TYPE)
8574 || ! validate_arg (c, INTEGER_TYPE)
8575 || ! validate_arg (len, INTEGER_TYPE))
8576 return NULL_TREE;
8578 if (! host_integerp (len, 1))
8579 return NULL_TREE;
8581 /* If the LEN parameter is zero, return DEST. */
8582 if (integer_zerop (len))
8583 return omit_one_operand_loc (loc, type, dest, c);
8585 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8586 return NULL_TREE;
8588 var = dest;
8589 STRIP_NOPS (var);
8590 if (TREE_CODE (var) != ADDR_EXPR)
8591 return NULL_TREE;
8593 var = TREE_OPERAND (var, 0);
8594 if (TREE_THIS_VOLATILE (var))
8595 return NULL_TREE;
8597 etype = TREE_TYPE (var);
8598 if (TREE_CODE (etype) == ARRAY_TYPE)
8599 etype = TREE_TYPE (etype);
8601 if (!INTEGRAL_TYPE_P (etype)
8602 && !POINTER_TYPE_P (etype))
8603 return NULL_TREE;
8605 if (! var_decl_component_p (var))
8606 return NULL_TREE;
8608 length = tree_low_cst (len, 1);
8609 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8610 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8611 return NULL_TREE;
8613 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8614 return NULL_TREE;
8616 if (integer_zerop (c))
8617 cval = 0;
8618 else
8620 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8621 return NULL_TREE;
8623 cval = TREE_INT_CST_LOW (c);
8624 cval &= 0xff;
8625 cval |= cval << 8;
8626 cval |= cval << 16;
8627 cval |= (cval << 31) << 1;
8630 ret = build_int_cst_type (etype, cval);
8631 var = build_fold_indirect_ref_loc (loc,
8632 fold_convert_loc (loc,
8633 build_pointer_type (etype),
8634 dest));
8635 ret = build2 (MODIFY_EXPR, etype, var, ret);
8636 if (ignore)
8637 return ret;
8639 return omit_one_operand_loc (loc, type, dest, ret);
8642 /* Fold function call to builtin memset. Return
8643 NULL_TREE if no simplification can be made. */
8645 static tree
8646 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8648 if (! validate_arg (dest, POINTER_TYPE)
8649 || ! validate_arg (size, INTEGER_TYPE))
8650 return NULL_TREE;
8652 if (!ignore)
8653 return NULL_TREE;
8655 /* New argument list transforming bzero(ptr x, int y) to
8656 memset(ptr x, int 0, size_t y). This is done this way
8657 so that if it isn't expanded inline, we fallback to
8658 calling bzero instead of memset. */
8660 return fold_builtin_memset (loc, dest, integer_zero_node,
8661 fold_convert_loc (loc, size_type_node, size),
8662 void_type_node, ignore);
8665 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8666 NULL_TREE if no simplification can be made.
8667 If ENDP is 0, return DEST (like memcpy).
8668 If ENDP is 1, return DEST+LEN (like mempcpy).
8669 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8670 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8671 (memmove). */
8673 static tree
8674 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8675 tree len, tree type, bool ignore, int endp)
8677 tree destvar, srcvar, expr;
8679 if (! validate_arg (dest, POINTER_TYPE)
8680 || ! validate_arg (src, POINTER_TYPE)
8681 || ! validate_arg (len, INTEGER_TYPE))
8682 return NULL_TREE;
8684 /* If the LEN parameter is zero, return DEST. */
8685 if (integer_zerop (len))
8686 return omit_one_operand_loc (loc, type, dest, src);
8688 /* If SRC and DEST are the same (and not volatile), return
8689 DEST{,+LEN,+LEN-1}. */
8690 if (operand_equal_p (src, dest, 0))
8691 expr = len;
8692 else
8694 tree srctype, desttype;
8695 unsigned int src_align, dest_align;
8696 tree off0;
8698 if (endp == 3)
8700 src_align = get_pointer_alignment (src);
8701 dest_align = get_pointer_alignment (dest);
8703 /* Both DEST and SRC must be pointer types.
8704 ??? This is what old code did. Is the testing for pointer types
8705 really mandatory?
8707 If either SRC is readonly or length is 1, we can use memcpy. */
8708 if (!dest_align || !src_align)
8709 return NULL_TREE;
8710 if (readonly_data_expr (src)
8711 || (host_integerp (len, 1)
8712 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8713 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8715 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8716 if (!fn)
8717 return NULL_TREE;
8718 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8721 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8722 if (TREE_CODE (src) == ADDR_EXPR
8723 && TREE_CODE (dest) == ADDR_EXPR)
8725 tree src_base, dest_base, fn;
8726 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8727 HOST_WIDE_INT size = -1;
8728 HOST_WIDE_INT maxsize = -1;
8730 srcvar = TREE_OPERAND (src, 0);
8731 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8732 &size, &maxsize);
8733 destvar = TREE_OPERAND (dest, 0);
8734 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8735 &size, &maxsize);
8736 if (host_integerp (len, 1))
8737 maxsize = tree_low_cst (len, 1);
8738 else
8739 maxsize = -1;
8740 src_offset /= BITS_PER_UNIT;
8741 dest_offset /= BITS_PER_UNIT;
8742 if (SSA_VAR_P (src_base)
8743 && SSA_VAR_P (dest_base))
8745 if (operand_equal_p (src_base, dest_base, 0)
8746 && ranges_overlap_p (src_offset, maxsize,
8747 dest_offset, maxsize))
8748 return NULL_TREE;
8750 else if (TREE_CODE (src_base) == MEM_REF
8751 && TREE_CODE (dest_base) == MEM_REF)
8753 double_int off;
8754 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8755 TREE_OPERAND (dest_base, 0), 0))
8756 return NULL_TREE;
8757 off = double_int_add (mem_ref_offset (src_base),
8758 shwi_to_double_int (src_offset));
8759 if (!double_int_fits_in_shwi_p (off))
8760 return NULL_TREE;
8761 src_offset = off.low;
8762 off = double_int_add (mem_ref_offset (dest_base),
8763 shwi_to_double_int (dest_offset));
8764 if (!double_int_fits_in_shwi_p (off))
8765 return NULL_TREE;
8766 dest_offset = off.low;
8767 if (ranges_overlap_p (src_offset, maxsize,
8768 dest_offset, maxsize))
8769 return NULL_TREE;
8771 else
8772 return NULL_TREE;
8774 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8775 if (!fn)
8776 return NULL_TREE;
8777 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8780 /* If the destination and source do not alias optimize into
8781 memcpy as well. */
8782 if ((is_gimple_min_invariant (dest)
8783 || TREE_CODE (dest) == SSA_NAME)
8784 && (is_gimple_min_invariant (src)
8785 || TREE_CODE (src) == SSA_NAME))
8787 ao_ref destr, srcr;
8788 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8789 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8790 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8792 tree fn;
8793 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8794 if (!fn)
8795 return NULL_TREE;
8796 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8800 return NULL_TREE;
8803 if (!host_integerp (len, 0))
8804 return NULL_TREE;
8805 /* FIXME:
8806 This logic lose for arguments like (type *)malloc (sizeof (type)),
8807 since we strip the casts of up to VOID return value from malloc.
8808 Perhaps we ought to inherit type from non-VOID argument here? */
8809 STRIP_NOPS (src);
8810 STRIP_NOPS (dest);
8811 if (!POINTER_TYPE_P (TREE_TYPE (src))
8812 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8813 return NULL_TREE;
8814 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8815 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8817 tree tem = TREE_OPERAND (src, 0);
8818 STRIP_NOPS (tem);
8819 if (tem != TREE_OPERAND (src, 0))
8820 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8822 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8824 tree tem = TREE_OPERAND (dest, 0);
8825 STRIP_NOPS (tem);
8826 if (tem != TREE_OPERAND (dest, 0))
8827 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8829 srctype = TREE_TYPE (TREE_TYPE (src));
8830 if (TREE_CODE (srctype) == ARRAY_TYPE
8831 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8833 srctype = TREE_TYPE (srctype);
8834 STRIP_NOPS (src);
8835 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8837 desttype = TREE_TYPE (TREE_TYPE (dest));
8838 if (TREE_CODE (desttype) == ARRAY_TYPE
8839 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8841 desttype = TREE_TYPE (desttype);
8842 STRIP_NOPS (dest);
8843 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8845 if (TREE_ADDRESSABLE (srctype)
8846 || TREE_ADDRESSABLE (desttype))
8847 return NULL_TREE;
8849 src_align = get_pointer_alignment (src);
8850 dest_align = get_pointer_alignment (dest);
8851 if (dest_align < TYPE_ALIGN (desttype)
8852 || src_align < TYPE_ALIGN (srctype))
8853 return NULL_TREE;
8855 if (!ignore)
8856 dest = builtin_save_expr (dest);
8858 /* Build accesses at offset zero with a ref-all character type. */
8859 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8860 ptr_mode, true), 0);
8862 destvar = dest;
8863 STRIP_NOPS (destvar);
8864 if (TREE_CODE (destvar) == ADDR_EXPR
8865 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8866 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8867 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8868 else
8869 destvar = NULL_TREE;
8871 srcvar = src;
8872 STRIP_NOPS (srcvar);
8873 if (TREE_CODE (srcvar) == ADDR_EXPR
8874 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8875 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8877 if (!destvar
8878 || src_align >= TYPE_ALIGN (desttype))
8879 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8880 srcvar, off0);
8881 else if (!STRICT_ALIGNMENT)
8883 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8884 src_align);
8885 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8887 else
8888 srcvar = NULL_TREE;
8890 else
8891 srcvar = NULL_TREE;
8893 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8894 return NULL_TREE;
8896 if (srcvar == NULL_TREE)
8898 STRIP_NOPS (src);
8899 if (src_align >= TYPE_ALIGN (desttype))
8900 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8901 else
8903 if (STRICT_ALIGNMENT)
8904 return NULL_TREE;
8905 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8906 src_align);
8907 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8910 else if (destvar == NULL_TREE)
8912 STRIP_NOPS (dest);
8913 if (dest_align >= TYPE_ALIGN (srctype))
8914 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8915 else
8917 if (STRICT_ALIGNMENT)
8918 return NULL_TREE;
8919 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8920 dest_align);
8921 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8925 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8928 if (ignore)
8929 return expr;
8931 if (endp == 0 || endp == 3)
8932 return omit_one_operand_loc (loc, type, dest, expr);
8934 if (expr == len)
8935 expr = NULL_TREE;
8937 if (endp == 2)
8938 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8939 ssize_int (1));
8941 dest = fold_build_pointer_plus_loc (loc, dest, len);
8942 dest = fold_convert_loc (loc, type, dest);
8943 if (expr)
8944 dest = omit_one_operand_loc (loc, type, dest, expr);
8945 return dest;
8948 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8949 If LEN is not NULL, it represents the length of the string to be
8950 copied. Return NULL_TREE if no simplification can be made. */
8952 tree
8953 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8955 tree fn;
8957 if (!validate_arg (dest, POINTER_TYPE)
8958 || !validate_arg (src, POINTER_TYPE))
8959 return NULL_TREE;
8961 /* If SRC and DEST are the same (and not volatile), return DEST. */
8962 if (operand_equal_p (src, dest, 0))
8963 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8965 if (optimize_function_for_size_p (cfun))
8966 return NULL_TREE;
8968 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8969 if (!fn)
8970 return NULL_TREE;
8972 if (!len)
8974 len = c_strlen (src, 1);
8975 if (! len || TREE_SIDE_EFFECTS (len))
8976 return NULL_TREE;
8979 len = fold_convert_loc (loc, size_type_node, len);
8980 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8981 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8982 build_call_expr_loc (loc, fn, 3, dest, src, len));
8985 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8986 Return NULL_TREE if no simplification can be made. */
8988 static tree
8989 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8991 tree fn, len, lenp1, call, type;
8993 if (!validate_arg (dest, POINTER_TYPE)
8994 || !validate_arg (src, POINTER_TYPE))
8995 return NULL_TREE;
8997 len = c_strlen (src, 1);
8998 if (!len
8999 || TREE_CODE (len) != INTEGER_CST)
9000 return NULL_TREE;
9002 if (optimize_function_for_size_p (cfun)
9003 /* If length is zero it's small enough. */
9004 && !integer_zerop (len))
9005 return NULL_TREE;
9007 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9008 if (!fn)
9009 return NULL_TREE;
9011 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9012 fold_convert_loc (loc, size_type_node, len),
9013 build_int_cst (size_type_node, 1));
9014 /* We use dest twice in building our expression. Save it from
9015 multiple expansions. */
9016 dest = builtin_save_expr (dest);
9017 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9019 type = TREE_TYPE (TREE_TYPE (fndecl));
9020 dest = fold_build_pointer_plus_loc (loc, dest, len);
9021 dest = fold_convert_loc (loc, type, dest);
9022 dest = omit_one_operand_loc (loc, type, dest, call);
9023 return dest;
9026 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9027 If SLEN is not NULL, it represents the length of the source string.
9028 Return NULL_TREE if no simplification can be made. */
9030 tree
9031 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9032 tree src, tree len, tree slen)
9034 tree fn;
9036 if (!validate_arg (dest, POINTER_TYPE)
9037 || !validate_arg (src, POINTER_TYPE)
9038 || !validate_arg (len, INTEGER_TYPE))
9039 return NULL_TREE;
9041 /* If the LEN parameter is zero, return DEST. */
9042 if (integer_zerop (len))
9043 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9045 /* We can't compare slen with len as constants below if len is not a
9046 constant. */
9047 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9048 return NULL_TREE;
9050 if (!slen)
9051 slen = c_strlen (src, 1);
9053 /* Now, we must be passed a constant src ptr parameter. */
9054 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9055 return NULL_TREE;
9057 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9059 /* We do not support simplification of this case, though we do
9060 support it when expanding trees into RTL. */
9061 /* FIXME: generate a call to __builtin_memset. */
9062 if (tree_int_cst_lt (slen, len))
9063 return NULL_TREE;
9065 /* OK transform into builtin memcpy. */
9066 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9067 if (!fn)
9068 return NULL_TREE;
9070 len = fold_convert_loc (loc, size_type_node, len);
9071 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9072 build_call_expr_loc (loc, fn, 3, dest, src, len));
9075 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9076 arguments to the call, and TYPE is its return type.
9077 Return NULL_TREE if no simplification can be made. */
9079 static tree
9080 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9082 if (!validate_arg (arg1, POINTER_TYPE)
9083 || !validate_arg (arg2, INTEGER_TYPE)
9084 || !validate_arg (len, INTEGER_TYPE))
9085 return NULL_TREE;
9086 else
9088 const char *p1;
9090 if (TREE_CODE (arg2) != INTEGER_CST
9091 || !host_integerp (len, 1))
9092 return NULL_TREE;
9094 p1 = c_getstr (arg1);
9095 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9097 char c;
9098 const char *r;
9099 tree tem;
9101 if (target_char_cast (arg2, &c))
9102 return NULL_TREE;
9104 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9106 if (r == NULL)
9107 return build_int_cst (TREE_TYPE (arg1), 0);
9109 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9110 return fold_convert_loc (loc, type, tem);
9112 return NULL_TREE;
9116 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9117 Return NULL_TREE if no simplification can be made. */
9119 static tree
9120 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9122 const char *p1, *p2;
9124 if (!validate_arg (arg1, POINTER_TYPE)
9125 || !validate_arg (arg2, POINTER_TYPE)
9126 || !validate_arg (len, INTEGER_TYPE))
9127 return NULL_TREE;
9129 /* If the LEN parameter is zero, return zero. */
9130 if (integer_zerop (len))
9131 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9132 arg1, arg2);
9134 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9135 if (operand_equal_p (arg1, arg2, 0))
9136 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9138 p1 = c_getstr (arg1);
9139 p2 = c_getstr (arg2);
9141 /* If all arguments are constant, and the value of len is not greater
9142 than the lengths of arg1 and arg2, evaluate at compile-time. */
9143 if (host_integerp (len, 1) && p1 && p2
9144 && compare_tree_int (len, strlen (p1) + 1) <= 0
9145 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9147 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9149 if (r > 0)
9150 return integer_one_node;
9151 else if (r < 0)
9152 return integer_minus_one_node;
9153 else
9154 return integer_zero_node;
9157 /* If len parameter is one, return an expression corresponding to
9158 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9159 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9161 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9162 tree cst_uchar_ptr_node
9163 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9165 tree ind1
9166 = fold_convert_loc (loc, integer_type_node,
9167 build1 (INDIRECT_REF, cst_uchar_node,
9168 fold_convert_loc (loc,
9169 cst_uchar_ptr_node,
9170 arg1)));
9171 tree ind2
9172 = fold_convert_loc (loc, integer_type_node,
9173 build1 (INDIRECT_REF, cst_uchar_node,
9174 fold_convert_loc (loc,
9175 cst_uchar_ptr_node,
9176 arg2)));
9177 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9180 return NULL_TREE;
9183 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9184 Return NULL_TREE if no simplification can be made. */
9186 static tree
9187 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9189 const char *p1, *p2;
9191 if (!validate_arg (arg1, POINTER_TYPE)
9192 || !validate_arg (arg2, POINTER_TYPE))
9193 return NULL_TREE;
9195 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9196 if (operand_equal_p (arg1, arg2, 0))
9197 return integer_zero_node;
9199 p1 = c_getstr (arg1);
9200 p2 = c_getstr (arg2);
9202 if (p1 && p2)
9204 const int i = strcmp (p1, p2);
9205 if (i < 0)
9206 return integer_minus_one_node;
9207 else if (i > 0)
9208 return integer_one_node;
9209 else
9210 return integer_zero_node;
9213 /* If the second arg is "", return *(const unsigned char*)arg1. */
9214 if (p2 && *p2 == '\0')
9216 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9217 tree cst_uchar_ptr_node
9218 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9220 return fold_convert_loc (loc, integer_type_node,
9221 build1 (INDIRECT_REF, cst_uchar_node,
9222 fold_convert_loc (loc,
9223 cst_uchar_ptr_node,
9224 arg1)));
9227 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9228 if (p1 && *p1 == '\0')
9230 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9231 tree cst_uchar_ptr_node
9232 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9234 tree temp
9235 = fold_convert_loc (loc, integer_type_node,
9236 build1 (INDIRECT_REF, cst_uchar_node,
9237 fold_convert_loc (loc,
9238 cst_uchar_ptr_node,
9239 arg2)));
9240 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9243 return NULL_TREE;
9246 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9247 Return NULL_TREE if no simplification can be made. */
9249 static tree
9250 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9252 const char *p1, *p2;
9254 if (!validate_arg (arg1, POINTER_TYPE)
9255 || !validate_arg (arg2, POINTER_TYPE)
9256 || !validate_arg (len, INTEGER_TYPE))
9257 return NULL_TREE;
9259 /* If the LEN parameter is zero, return zero. */
9260 if (integer_zerop (len))
9261 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9262 arg1, arg2);
9264 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9265 if (operand_equal_p (arg1, arg2, 0))
9266 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9268 p1 = c_getstr (arg1);
9269 p2 = c_getstr (arg2);
9271 if (host_integerp (len, 1) && p1 && p2)
9273 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9274 if (i > 0)
9275 return integer_one_node;
9276 else if (i < 0)
9277 return integer_minus_one_node;
9278 else
9279 return integer_zero_node;
9282 /* If the second arg is "", and the length is greater than zero,
9283 return *(const unsigned char*)arg1. */
9284 if (p2 && *p2 == '\0'
9285 && TREE_CODE (len) == INTEGER_CST
9286 && tree_int_cst_sgn (len) == 1)
9288 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9289 tree cst_uchar_ptr_node
9290 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9292 return fold_convert_loc (loc, integer_type_node,
9293 build1 (INDIRECT_REF, cst_uchar_node,
9294 fold_convert_loc (loc,
9295 cst_uchar_ptr_node,
9296 arg1)));
9299 /* If the first arg is "", and the length is greater than zero,
9300 return -*(const unsigned char*)arg2. */
9301 if (p1 && *p1 == '\0'
9302 && TREE_CODE (len) == INTEGER_CST
9303 && tree_int_cst_sgn (len) == 1)
9305 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9306 tree cst_uchar_ptr_node
9307 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9309 tree temp = fold_convert_loc (loc, integer_type_node,
9310 build1 (INDIRECT_REF, cst_uchar_node,
9311 fold_convert_loc (loc,
9312 cst_uchar_ptr_node,
9313 arg2)));
9314 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9317 /* If len parameter is one, return an expression corresponding to
9318 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9319 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9321 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9322 tree cst_uchar_ptr_node
9323 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9325 tree ind1 = fold_convert_loc (loc, integer_type_node,
9326 build1 (INDIRECT_REF, cst_uchar_node,
9327 fold_convert_loc (loc,
9328 cst_uchar_ptr_node,
9329 arg1)));
9330 tree ind2 = fold_convert_loc (loc, integer_type_node,
9331 build1 (INDIRECT_REF, cst_uchar_node,
9332 fold_convert_loc (loc,
9333 cst_uchar_ptr_node,
9334 arg2)));
9335 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9338 return NULL_TREE;
9341 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9342 ARG. Return NULL_TREE if no simplification can be made. */
9344 static tree
9345 fold_builtin_signbit (location_t loc, tree arg, tree type)
9347 if (!validate_arg (arg, REAL_TYPE))
9348 return NULL_TREE;
9350 /* If ARG is a compile-time constant, determine the result. */
9351 if (TREE_CODE (arg) == REAL_CST
9352 && !TREE_OVERFLOW (arg))
9354 REAL_VALUE_TYPE c;
9356 c = TREE_REAL_CST (arg);
9357 return (REAL_VALUE_NEGATIVE (c)
9358 ? build_one_cst (type)
9359 : build_zero_cst (type));
9362 /* If ARG is non-negative, the result is always zero. */
9363 if (tree_expr_nonnegative_p (arg))
9364 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9366 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9367 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9368 return fold_convert (type,
9369 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9370 build_real (TREE_TYPE (arg), dconst0)));
9372 return NULL_TREE;
9375 /* Fold function call to builtin copysign, copysignf or copysignl with
9376 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9377 be made. */
9379 static tree
9380 fold_builtin_copysign (location_t loc, tree fndecl,
9381 tree arg1, tree arg2, tree type)
9383 tree tem;
9385 if (!validate_arg (arg1, REAL_TYPE)
9386 || !validate_arg (arg2, REAL_TYPE))
9387 return NULL_TREE;
9389 /* copysign(X,X) is X. */
9390 if (operand_equal_p (arg1, arg2, 0))
9391 return fold_convert_loc (loc, type, arg1);
9393 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9394 if (TREE_CODE (arg1) == REAL_CST
9395 && TREE_CODE (arg2) == REAL_CST
9396 && !TREE_OVERFLOW (arg1)
9397 && !TREE_OVERFLOW (arg2))
9399 REAL_VALUE_TYPE c1, c2;
9401 c1 = TREE_REAL_CST (arg1);
9402 c2 = TREE_REAL_CST (arg2);
9403 /* c1.sign := c2.sign. */
9404 real_copysign (&c1, &c2);
9405 return build_real (type, c1);
9408 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9409 Remember to evaluate Y for side-effects. */
9410 if (tree_expr_nonnegative_p (arg2))
9411 return omit_one_operand_loc (loc, type,
9412 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9413 arg2);
9415 /* Strip sign changing operations for the first argument. */
9416 tem = fold_strip_sign_ops (arg1);
9417 if (tem)
9418 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9420 return NULL_TREE;
9423 /* Fold a call to builtin isascii with argument ARG. */
9425 static tree
9426 fold_builtin_isascii (location_t loc, tree arg)
9428 if (!validate_arg (arg, INTEGER_TYPE))
9429 return NULL_TREE;
9430 else
9432 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9433 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9434 build_int_cst (integer_type_node,
9435 ~ (unsigned HOST_WIDE_INT) 0x7f));
9436 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9437 arg, integer_zero_node);
9441 /* Fold a call to builtin toascii with argument ARG. */
9443 static tree
9444 fold_builtin_toascii (location_t loc, tree arg)
9446 if (!validate_arg (arg, INTEGER_TYPE))
9447 return NULL_TREE;
9449 /* Transform toascii(c) -> (c & 0x7f). */
9450 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9451 build_int_cst (integer_type_node, 0x7f));
9454 /* Fold a call to builtin isdigit with argument ARG. */
9456 static tree
9457 fold_builtin_isdigit (location_t loc, tree arg)
9459 if (!validate_arg (arg, INTEGER_TYPE))
9460 return NULL_TREE;
9461 else
9463 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9464 /* According to the C standard, isdigit is unaffected by locale.
9465 However, it definitely is affected by the target character set. */
9466 unsigned HOST_WIDE_INT target_digit0
9467 = lang_hooks.to_target_charset ('0');
9469 if (target_digit0 == 0)
9470 return NULL_TREE;
9472 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9473 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9474 build_int_cst (unsigned_type_node, target_digit0));
9475 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9476 build_int_cst (unsigned_type_node, 9));
9480 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9482 static tree
9483 fold_builtin_fabs (location_t loc, tree arg, tree type)
9485 if (!validate_arg (arg, REAL_TYPE))
9486 return NULL_TREE;
9488 arg = fold_convert_loc (loc, type, arg);
9489 if (TREE_CODE (arg) == REAL_CST)
9490 return fold_abs_const (arg, type);
9491 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9494 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9496 static tree
9497 fold_builtin_abs (location_t loc, tree arg, tree type)
9499 if (!validate_arg (arg, INTEGER_TYPE))
9500 return NULL_TREE;
9502 arg = fold_convert_loc (loc, type, arg);
9503 if (TREE_CODE (arg) == INTEGER_CST)
9504 return fold_abs_const (arg, type);
9505 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9508 /* Fold a fma operation with arguments ARG[012]. */
9510 tree
9511 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9512 tree type, tree arg0, tree arg1, tree arg2)
9514 if (TREE_CODE (arg0) == REAL_CST
9515 && TREE_CODE (arg1) == REAL_CST
9516 && TREE_CODE (arg2) == REAL_CST)
9517 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9519 return NULL_TREE;
9522 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9524 static tree
9525 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9527 if (validate_arg (arg0, REAL_TYPE)
9528 && validate_arg(arg1, REAL_TYPE)
9529 && validate_arg(arg2, REAL_TYPE))
9531 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9532 if (tem)
9533 return tem;
9535 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9536 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9537 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9539 return NULL_TREE;
9542 /* Fold a call to builtin fmin or fmax. */
9544 static tree
9545 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9546 tree type, bool max)
9548 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9550 /* Calculate the result when the argument is a constant. */
9551 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9553 if (res)
9554 return res;
9556 /* If either argument is NaN, return the other one. Avoid the
9557 transformation if we get (and honor) a signalling NaN. Using
9558 omit_one_operand() ensures we create a non-lvalue. */
9559 if (TREE_CODE (arg0) == REAL_CST
9560 && real_isnan (&TREE_REAL_CST (arg0))
9561 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9562 || ! TREE_REAL_CST (arg0).signalling))
9563 return omit_one_operand_loc (loc, type, arg1, arg0);
9564 if (TREE_CODE (arg1) == REAL_CST
9565 && real_isnan (&TREE_REAL_CST (arg1))
9566 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9567 || ! TREE_REAL_CST (arg1).signalling))
9568 return omit_one_operand_loc (loc, type, arg0, arg1);
9570 /* Transform fmin/fmax(x,x) -> x. */
9571 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9572 return omit_one_operand_loc (loc, type, arg0, arg1);
9574 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9575 functions to return the numeric arg if the other one is NaN.
9576 These tree codes don't honor that, so only transform if
9577 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9578 handled, so we don't have to worry about it either. */
9579 if (flag_finite_math_only)
9580 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9581 fold_convert_loc (loc, type, arg0),
9582 fold_convert_loc (loc, type, arg1));
9584 return NULL_TREE;
9587 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9589 static tree
9590 fold_builtin_carg (location_t loc, tree arg, tree type)
9592 if (validate_arg (arg, COMPLEX_TYPE)
9593 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9595 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9597 if (atan2_fn)
9599 tree new_arg = builtin_save_expr (arg);
9600 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9601 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9602 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9606 return NULL_TREE;
9609 /* Fold a call to builtin logb/ilogb. */
9611 static tree
9612 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9614 if (! validate_arg (arg, REAL_TYPE))
9615 return NULL_TREE;
9617 STRIP_NOPS (arg);
9619 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9621 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9623 switch (value->cl)
9625 case rvc_nan:
9626 case rvc_inf:
9627 /* If arg is Inf or NaN and we're logb, return it. */
9628 if (TREE_CODE (rettype) == REAL_TYPE)
9629 return fold_convert_loc (loc, rettype, arg);
9630 /* Fall through... */
9631 case rvc_zero:
9632 /* Zero may set errno and/or raise an exception for logb, also
9633 for ilogb we don't know FP_ILOGB0. */
9634 return NULL_TREE;
9635 case rvc_normal:
9636 /* For normal numbers, proceed iff radix == 2. In GCC,
9637 normalized significands are in the range [0.5, 1.0). We
9638 want the exponent as if they were [1.0, 2.0) so get the
9639 exponent and subtract 1. */
9640 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9641 return fold_convert_loc (loc, rettype,
9642 build_int_cst (integer_type_node,
9643 REAL_EXP (value)-1));
9644 break;
9648 return NULL_TREE;
9651 /* Fold a call to builtin significand, if radix == 2. */
9653 static tree
9654 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9656 if (! validate_arg (arg, REAL_TYPE))
9657 return NULL_TREE;
9659 STRIP_NOPS (arg);
9661 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9663 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9665 switch (value->cl)
9667 case rvc_zero:
9668 case rvc_nan:
9669 case rvc_inf:
9670 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9671 return fold_convert_loc (loc, rettype, arg);
9672 case rvc_normal:
9673 /* For normal numbers, proceed iff radix == 2. */
9674 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9676 REAL_VALUE_TYPE result = *value;
9677 /* In GCC, normalized significands are in the range [0.5,
9678 1.0). We want them to be [1.0, 2.0) so set the
9679 exponent to 1. */
9680 SET_REAL_EXP (&result, 1);
9681 return build_real (rettype, result);
9683 break;
9687 return NULL_TREE;
9690 /* Fold a call to builtin frexp, we can assume the base is 2. */
9692 static tree
9693 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9695 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9696 return NULL_TREE;
9698 STRIP_NOPS (arg0);
9700 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9701 return NULL_TREE;
9703 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9705 /* Proceed if a valid pointer type was passed in. */
9706 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9708 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9709 tree frac, exp;
9711 switch (value->cl)
9713 case rvc_zero:
9714 /* For +-0, return (*exp = 0, +-0). */
9715 exp = integer_zero_node;
9716 frac = arg0;
9717 break;
9718 case rvc_nan:
9719 case rvc_inf:
9720 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9721 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9722 case rvc_normal:
9724 /* Since the frexp function always expects base 2, and in
9725 GCC normalized significands are already in the range
9726 [0.5, 1.0), we have exactly what frexp wants. */
9727 REAL_VALUE_TYPE frac_rvt = *value;
9728 SET_REAL_EXP (&frac_rvt, 0);
9729 frac = build_real (rettype, frac_rvt);
9730 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9732 break;
9733 default:
9734 gcc_unreachable ();
9737 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9738 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9739 TREE_SIDE_EFFECTS (arg1) = 1;
9740 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9743 return NULL_TREE;
9746 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9747 then we can assume the base is two. If it's false, then we have to
9748 check the mode of the TYPE parameter in certain cases. */
9750 static tree
9751 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9752 tree type, bool ldexp)
9754 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9756 STRIP_NOPS (arg0);
9757 STRIP_NOPS (arg1);
9759 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9760 if (real_zerop (arg0) || integer_zerop (arg1)
9761 || (TREE_CODE (arg0) == REAL_CST
9762 && !real_isfinite (&TREE_REAL_CST (arg0))))
9763 return omit_one_operand_loc (loc, type, arg0, arg1);
9765 /* If both arguments are constant, then try to evaluate it. */
9766 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9767 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9768 && host_integerp (arg1, 0))
9770 /* Bound the maximum adjustment to twice the range of the
9771 mode's valid exponents. Use abs to ensure the range is
9772 positive as a sanity check. */
9773 const long max_exp_adj = 2 *
9774 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9775 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9777 /* Get the user-requested adjustment. */
9778 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9780 /* The requested adjustment must be inside this range. This
9781 is a preliminary cap to avoid things like overflow, we
9782 may still fail to compute the result for other reasons. */
9783 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9785 REAL_VALUE_TYPE initial_result;
9787 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9789 /* Ensure we didn't overflow. */
9790 if (! real_isinf (&initial_result))
9792 const REAL_VALUE_TYPE trunc_result
9793 = real_value_truncate (TYPE_MODE (type), initial_result);
9795 /* Only proceed if the target mode can hold the
9796 resulting value. */
9797 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9798 return build_real (type, trunc_result);
9804 return NULL_TREE;
9807 /* Fold a call to builtin modf. */
9809 static tree
9810 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9812 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9813 return NULL_TREE;
9815 STRIP_NOPS (arg0);
9817 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9818 return NULL_TREE;
9820 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9822 /* Proceed if a valid pointer type was passed in. */
9823 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9825 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9826 REAL_VALUE_TYPE trunc, frac;
9828 switch (value->cl)
9830 case rvc_nan:
9831 case rvc_zero:
9832 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9833 trunc = frac = *value;
9834 break;
9835 case rvc_inf:
9836 /* For +-Inf, return (*arg1 = arg0, +-0). */
9837 frac = dconst0;
9838 frac.sign = value->sign;
9839 trunc = *value;
9840 break;
9841 case rvc_normal:
9842 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9843 real_trunc (&trunc, VOIDmode, value);
9844 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9845 /* If the original number was negative and already
9846 integral, then the fractional part is -0.0. */
9847 if (value->sign && frac.cl == rvc_zero)
9848 frac.sign = value->sign;
9849 break;
9852 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9853 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9854 build_real (rettype, trunc));
9855 TREE_SIDE_EFFECTS (arg1) = 1;
9856 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9857 build_real (rettype, frac));
9860 return NULL_TREE;
9863 /* Given a location LOC, an interclass builtin function decl FNDECL
9864 and its single argument ARG, return an folded expression computing
9865 the same, or NULL_TREE if we either couldn't or didn't want to fold
9866 (the latter happen if there's an RTL instruction available). */
9868 static tree
9869 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9871 enum machine_mode mode;
9873 if (!validate_arg (arg, REAL_TYPE))
9874 return NULL_TREE;
9876 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9877 return NULL_TREE;
9879 mode = TYPE_MODE (TREE_TYPE (arg));
9881 /* If there is no optab, try generic code. */
9882 switch (DECL_FUNCTION_CODE (fndecl))
9884 tree result;
9886 CASE_FLT_FN (BUILT_IN_ISINF):
9888 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9889 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9890 tree const type = TREE_TYPE (arg);
9891 REAL_VALUE_TYPE r;
9892 char buf[128];
9894 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9895 real_from_string (&r, buf);
9896 result = build_call_expr (isgr_fn, 2,
9897 fold_build1_loc (loc, ABS_EXPR, type, arg),
9898 build_real (type, r));
9899 return result;
9901 CASE_FLT_FN (BUILT_IN_FINITE):
9902 case BUILT_IN_ISFINITE:
9904 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9905 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9906 tree const type = TREE_TYPE (arg);
9907 REAL_VALUE_TYPE r;
9908 char buf[128];
9910 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9911 real_from_string (&r, buf);
9912 result = build_call_expr (isle_fn, 2,
9913 fold_build1_loc (loc, ABS_EXPR, type, arg),
9914 build_real (type, r));
9915 /*result = fold_build2_loc (loc, UNGT_EXPR,
9916 TREE_TYPE (TREE_TYPE (fndecl)),
9917 fold_build1_loc (loc, ABS_EXPR, type, arg),
9918 build_real (type, r));
9919 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9920 TREE_TYPE (TREE_TYPE (fndecl)),
9921 result);*/
9922 return result;
9924 case BUILT_IN_ISNORMAL:
9926 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9927 islessequal(fabs(x),DBL_MAX). */
9928 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9929 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9930 tree const type = TREE_TYPE (arg);
9931 REAL_VALUE_TYPE rmax, rmin;
9932 char buf[128];
9934 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9935 real_from_string (&rmax, buf);
9936 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9937 real_from_string (&rmin, buf);
9938 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9939 result = build_call_expr (isle_fn, 2, arg,
9940 build_real (type, rmax));
9941 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9942 build_call_expr (isge_fn, 2, arg,
9943 build_real (type, rmin)));
9944 return result;
9946 default:
9947 break;
9950 return NULL_TREE;
9953 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9954 ARG is the argument for the call. */
9956 static tree
9957 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9959 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9960 REAL_VALUE_TYPE r;
9962 if (!validate_arg (arg, REAL_TYPE))
9963 return NULL_TREE;
9965 switch (builtin_index)
9967 case BUILT_IN_ISINF:
9968 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9969 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9971 if (TREE_CODE (arg) == REAL_CST)
9973 r = TREE_REAL_CST (arg);
9974 if (real_isinf (&r))
9975 return real_compare (GT_EXPR, &r, &dconst0)
9976 ? integer_one_node : integer_minus_one_node;
9977 else
9978 return integer_zero_node;
9981 return NULL_TREE;
9983 case BUILT_IN_ISINF_SIGN:
9985 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9986 /* In a boolean context, GCC will fold the inner COND_EXPR to
9987 1. So e.g. "if (isinf_sign(x))" would be folded to just
9988 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9989 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9990 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9991 tree tmp = NULL_TREE;
9993 arg = builtin_save_expr (arg);
9995 if (signbit_fn && isinf_fn)
9997 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9998 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10000 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10001 signbit_call, integer_zero_node);
10002 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10003 isinf_call, integer_zero_node);
10005 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10006 integer_minus_one_node, integer_one_node);
10007 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10008 isinf_call, tmp,
10009 integer_zero_node);
10012 return tmp;
10015 case BUILT_IN_ISFINITE:
10016 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10017 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10018 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10020 if (TREE_CODE (arg) == REAL_CST)
10022 r = TREE_REAL_CST (arg);
10023 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10026 return NULL_TREE;
10028 case BUILT_IN_ISNAN:
10029 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10030 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10032 if (TREE_CODE (arg) == REAL_CST)
10034 r = TREE_REAL_CST (arg);
10035 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10038 arg = builtin_save_expr (arg);
10039 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10041 default:
10042 gcc_unreachable ();
10046 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10047 This builtin will generate code to return the appropriate floating
10048 point classification depending on the value of the floating point
10049 number passed in. The possible return values must be supplied as
10050 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10051 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10052 one floating point argument which is "type generic". */
10054 static tree
10055 fold_builtin_fpclassify (location_t loc, tree exp)
10057 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10058 arg, type, res, tmp;
10059 enum machine_mode mode;
10060 REAL_VALUE_TYPE r;
10061 char buf[128];
10063 /* Verify the required arguments in the original call. */
10064 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10065 INTEGER_TYPE, INTEGER_TYPE,
10066 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10067 return NULL_TREE;
10069 fp_nan = CALL_EXPR_ARG (exp, 0);
10070 fp_infinite = CALL_EXPR_ARG (exp, 1);
10071 fp_normal = CALL_EXPR_ARG (exp, 2);
10072 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10073 fp_zero = CALL_EXPR_ARG (exp, 4);
10074 arg = CALL_EXPR_ARG (exp, 5);
10075 type = TREE_TYPE (arg);
10076 mode = TYPE_MODE (type);
10077 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10079 /* fpclassify(x) ->
10080 isnan(x) ? FP_NAN :
10081 (fabs(x) == Inf ? FP_INFINITE :
10082 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10083 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10085 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10086 build_real (type, dconst0));
10087 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10088 tmp, fp_zero, fp_subnormal);
10090 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10091 real_from_string (&r, buf);
10092 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10093 arg, build_real (type, r));
10094 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10096 if (HONOR_INFINITIES (mode))
10098 real_inf (&r);
10099 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10100 build_real (type, r));
10101 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10102 fp_infinite, res);
10105 if (HONOR_NANS (mode))
10107 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10108 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10111 return res;
10114 /* Fold a call to an unordered comparison function such as
10115 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10116 being called and ARG0 and ARG1 are the arguments for the call.
10117 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10118 the opposite of the desired result. UNORDERED_CODE is used
10119 for modes that can hold NaNs and ORDERED_CODE is used for
10120 the rest. */
10122 static tree
10123 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10124 enum tree_code unordered_code,
10125 enum tree_code ordered_code)
10127 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10128 enum tree_code code;
10129 tree type0, type1;
10130 enum tree_code code0, code1;
10131 tree cmp_type = NULL_TREE;
10133 type0 = TREE_TYPE (arg0);
10134 type1 = TREE_TYPE (arg1);
10136 code0 = TREE_CODE (type0);
10137 code1 = TREE_CODE (type1);
10139 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10140 /* Choose the wider of two real types. */
10141 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10142 ? type0 : type1;
10143 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10144 cmp_type = type0;
10145 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10146 cmp_type = type1;
10148 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10149 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10151 if (unordered_code == UNORDERED_EXPR)
10153 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10154 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10155 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10158 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10159 : ordered_code;
10160 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10161 fold_build2_loc (loc, code, type, arg0, arg1));
10164 /* Fold a call to built-in function FNDECL with 0 arguments.
10165 IGNORE is true if the result of the function call is ignored. This
10166 function returns NULL_TREE if no simplification was possible. */
10168 static tree
10169 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10171 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10172 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10173 switch (fcode)
10175 CASE_FLT_FN (BUILT_IN_INF):
10176 case BUILT_IN_INFD32:
10177 case BUILT_IN_INFD64:
10178 case BUILT_IN_INFD128:
10179 return fold_builtin_inf (loc, type, true);
10181 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10182 return fold_builtin_inf (loc, type, false);
10184 case BUILT_IN_CLASSIFY_TYPE:
10185 return fold_builtin_classify_type (NULL_TREE);
10187 default:
10188 break;
10190 return NULL_TREE;
10193 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10194 IGNORE is true if the result of the function call is ignored. This
10195 function returns NULL_TREE if no simplification was possible. */
10197 static tree
10198 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10200 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10201 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10202 switch (fcode)
10204 case BUILT_IN_CONSTANT_P:
10206 tree val = fold_builtin_constant_p (arg0);
10208 /* Gimplification will pull the CALL_EXPR for the builtin out of
10209 an if condition. When not optimizing, we'll not CSE it back.
10210 To avoid link error types of regressions, return false now. */
10211 if (!val && !optimize)
10212 val = integer_zero_node;
10214 return val;
10217 case BUILT_IN_CLASSIFY_TYPE:
10218 return fold_builtin_classify_type (arg0);
10220 case BUILT_IN_STRLEN:
10221 return fold_builtin_strlen (loc, type, arg0);
10223 CASE_FLT_FN (BUILT_IN_FABS):
10224 return fold_builtin_fabs (loc, arg0, type);
10226 case BUILT_IN_ABS:
10227 case BUILT_IN_LABS:
10228 case BUILT_IN_LLABS:
10229 case BUILT_IN_IMAXABS:
10230 return fold_builtin_abs (loc, arg0, type);
10232 CASE_FLT_FN (BUILT_IN_CONJ):
10233 if (validate_arg (arg0, COMPLEX_TYPE)
10234 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10235 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10236 break;
10238 CASE_FLT_FN (BUILT_IN_CREAL):
10239 if (validate_arg (arg0, COMPLEX_TYPE)
10240 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10241 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10242 break;
10244 CASE_FLT_FN (BUILT_IN_CIMAG):
10245 if (validate_arg (arg0, COMPLEX_TYPE)
10246 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10247 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10248 break;
10250 CASE_FLT_FN (BUILT_IN_CCOS):
10251 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10253 CASE_FLT_FN (BUILT_IN_CCOSH):
10254 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10256 CASE_FLT_FN (BUILT_IN_CPROJ):
10257 return fold_builtin_cproj(loc, arg0, type);
10259 CASE_FLT_FN (BUILT_IN_CSIN):
10260 if (validate_arg (arg0, COMPLEX_TYPE)
10261 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10262 return do_mpc_arg1 (arg0, type, mpc_sin);
10263 break;
10265 CASE_FLT_FN (BUILT_IN_CSINH):
10266 if (validate_arg (arg0, COMPLEX_TYPE)
10267 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10268 return do_mpc_arg1 (arg0, type, mpc_sinh);
10269 break;
10271 CASE_FLT_FN (BUILT_IN_CTAN):
10272 if (validate_arg (arg0, COMPLEX_TYPE)
10273 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10274 return do_mpc_arg1 (arg0, type, mpc_tan);
10275 break;
10277 CASE_FLT_FN (BUILT_IN_CTANH):
10278 if (validate_arg (arg0, COMPLEX_TYPE)
10279 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10280 return do_mpc_arg1 (arg0, type, mpc_tanh);
10281 break;
10283 CASE_FLT_FN (BUILT_IN_CLOG):
10284 if (validate_arg (arg0, COMPLEX_TYPE)
10285 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10286 return do_mpc_arg1 (arg0, type, mpc_log);
10287 break;
10289 CASE_FLT_FN (BUILT_IN_CSQRT):
10290 if (validate_arg (arg0, COMPLEX_TYPE)
10291 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10292 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10293 break;
10295 CASE_FLT_FN (BUILT_IN_CASIN):
10296 if (validate_arg (arg0, COMPLEX_TYPE)
10297 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10298 return do_mpc_arg1 (arg0, type, mpc_asin);
10299 break;
10301 CASE_FLT_FN (BUILT_IN_CACOS):
10302 if (validate_arg (arg0, COMPLEX_TYPE)
10303 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10304 return do_mpc_arg1 (arg0, type, mpc_acos);
10305 break;
10307 CASE_FLT_FN (BUILT_IN_CATAN):
10308 if (validate_arg (arg0, COMPLEX_TYPE)
10309 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10310 return do_mpc_arg1 (arg0, type, mpc_atan);
10311 break;
10313 CASE_FLT_FN (BUILT_IN_CASINH):
10314 if (validate_arg (arg0, COMPLEX_TYPE)
10315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10316 return do_mpc_arg1 (arg0, type, mpc_asinh);
10317 break;
10319 CASE_FLT_FN (BUILT_IN_CACOSH):
10320 if (validate_arg (arg0, COMPLEX_TYPE)
10321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10322 return do_mpc_arg1 (arg0, type, mpc_acosh);
10323 break;
10325 CASE_FLT_FN (BUILT_IN_CATANH):
10326 if (validate_arg (arg0, COMPLEX_TYPE)
10327 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10328 return do_mpc_arg1 (arg0, type, mpc_atanh);
10329 break;
10331 CASE_FLT_FN (BUILT_IN_CABS):
10332 return fold_builtin_cabs (loc, arg0, type, fndecl);
10334 CASE_FLT_FN (BUILT_IN_CARG):
10335 return fold_builtin_carg (loc, arg0, type);
10337 CASE_FLT_FN (BUILT_IN_SQRT):
10338 return fold_builtin_sqrt (loc, arg0, type);
10340 CASE_FLT_FN (BUILT_IN_CBRT):
10341 return fold_builtin_cbrt (loc, arg0, type);
10343 CASE_FLT_FN (BUILT_IN_ASIN):
10344 if (validate_arg (arg0, REAL_TYPE))
10345 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10346 &dconstm1, &dconst1, true);
10347 break;
10349 CASE_FLT_FN (BUILT_IN_ACOS):
10350 if (validate_arg (arg0, REAL_TYPE))
10351 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10352 &dconstm1, &dconst1, true);
10353 break;
10355 CASE_FLT_FN (BUILT_IN_ATAN):
10356 if (validate_arg (arg0, REAL_TYPE))
10357 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10358 break;
10360 CASE_FLT_FN (BUILT_IN_ASINH):
10361 if (validate_arg (arg0, REAL_TYPE))
10362 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10363 break;
10365 CASE_FLT_FN (BUILT_IN_ACOSH):
10366 if (validate_arg (arg0, REAL_TYPE))
10367 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10368 &dconst1, NULL, true);
10369 break;
10371 CASE_FLT_FN (BUILT_IN_ATANH):
10372 if (validate_arg (arg0, REAL_TYPE))
10373 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10374 &dconstm1, &dconst1, false);
10375 break;
10377 CASE_FLT_FN (BUILT_IN_SIN):
10378 if (validate_arg (arg0, REAL_TYPE))
10379 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10380 break;
10382 CASE_FLT_FN (BUILT_IN_COS):
10383 return fold_builtin_cos (loc, arg0, type, fndecl);
10385 CASE_FLT_FN (BUILT_IN_TAN):
10386 return fold_builtin_tan (arg0, type);
10388 CASE_FLT_FN (BUILT_IN_CEXP):
10389 return fold_builtin_cexp (loc, arg0, type);
10391 CASE_FLT_FN (BUILT_IN_CEXPI):
10392 if (validate_arg (arg0, REAL_TYPE))
10393 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10394 break;
10396 CASE_FLT_FN (BUILT_IN_SINH):
10397 if (validate_arg (arg0, REAL_TYPE))
10398 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10399 break;
10401 CASE_FLT_FN (BUILT_IN_COSH):
10402 return fold_builtin_cosh (loc, arg0, type, fndecl);
10404 CASE_FLT_FN (BUILT_IN_TANH):
10405 if (validate_arg (arg0, REAL_TYPE))
10406 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10407 break;
10409 CASE_FLT_FN (BUILT_IN_ERF):
10410 if (validate_arg (arg0, REAL_TYPE))
10411 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10412 break;
10414 CASE_FLT_FN (BUILT_IN_ERFC):
10415 if (validate_arg (arg0, REAL_TYPE))
10416 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10417 break;
10419 CASE_FLT_FN (BUILT_IN_TGAMMA):
10420 if (validate_arg (arg0, REAL_TYPE))
10421 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10422 break;
10424 CASE_FLT_FN (BUILT_IN_EXP):
10425 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10427 CASE_FLT_FN (BUILT_IN_EXP2):
10428 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10430 CASE_FLT_FN (BUILT_IN_EXP10):
10431 CASE_FLT_FN (BUILT_IN_POW10):
10432 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10434 CASE_FLT_FN (BUILT_IN_EXPM1):
10435 if (validate_arg (arg0, REAL_TYPE))
10436 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10437 break;
10439 CASE_FLT_FN (BUILT_IN_LOG):
10440 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10442 CASE_FLT_FN (BUILT_IN_LOG2):
10443 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10445 CASE_FLT_FN (BUILT_IN_LOG10):
10446 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10448 CASE_FLT_FN (BUILT_IN_LOG1P):
10449 if (validate_arg (arg0, REAL_TYPE))
10450 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10451 &dconstm1, NULL, false);
10452 break;
10454 CASE_FLT_FN (BUILT_IN_J0):
10455 if (validate_arg (arg0, REAL_TYPE))
10456 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10457 NULL, NULL, 0);
10458 break;
10460 CASE_FLT_FN (BUILT_IN_J1):
10461 if (validate_arg (arg0, REAL_TYPE))
10462 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10463 NULL, NULL, 0);
10464 break;
10466 CASE_FLT_FN (BUILT_IN_Y0):
10467 if (validate_arg (arg0, REAL_TYPE))
10468 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10469 &dconst0, NULL, false);
10470 break;
10472 CASE_FLT_FN (BUILT_IN_Y1):
10473 if (validate_arg (arg0, REAL_TYPE))
10474 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10475 &dconst0, NULL, false);
10476 break;
10478 CASE_FLT_FN (BUILT_IN_NAN):
10479 case BUILT_IN_NAND32:
10480 case BUILT_IN_NAND64:
10481 case BUILT_IN_NAND128:
10482 return fold_builtin_nan (arg0, type, true);
10484 CASE_FLT_FN (BUILT_IN_NANS):
10485 return fold_builtin_nan (arg0, type, false);
10487 CASE_FLT_FN (BUILT_IN_FLOOR):
10488 return fold_builtin_floor (loc, fndecl, arg0);
10490 CASE_FLT_FN (BUILT_IN_CEIL):
10491 return fold_builtin_ceil (loc, fndecl, arg0);
10493 CASE_FLT_FN (BUILT_IN_TRUNC):
10494 return fold_builtin_trunc (loc, fndecl, arg0);
10496 CASE_FLT_FN (BUILT_IN_ROUND):
10497 return fold_builtin_round (loc, fndecl, arg0);
10499 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10500 CASE_FLT_FN (BUILT_IN_RINT):
10501 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10503 CASE_FLT_FN (BUILT_IN_ICEIL):
10504 CASE_FLT_FN (BUILT_IN_LCEIL):
10505 CASE_FLT_FN (BUILT_IN_LLCEIL):
10506 CASE_FLT_FN (BUILT_IN_LFLOOR):
10507 CASE_FLT_FN (BUILT_IN_IFLOOR):
10508 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10509 CASE_FLT_FN (BUILT_IN_IROUND):
10510 CASE_FLT_FN (BUILT_IN_LROUND):
10511 CASE_FLT_FN (BUILT_IN_LLROUND):
10512 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10514 CASE_FLT_FN (BUILT_IN_IRINT):
10515 CASE_FLT_FN (BUILT_IN_LRINT):
10516 CASE_FLT_FN (BUILT_IN_LLRINT):
10517 return fold_fixed_mathfn (loc, fndecl, arg0);
10519 case BUILT_IN_BSWAP32:
10520 case BUILT_IN_BSWAP64:
10521 return fold_builtin_bswap (fndecl, arg0);
10523 CASE_INT_FN (BUILT_IN_FFS):
10524 CASE_INT_FN (BUILT_IN_CLZ):
10525 CASE_INT_FN (BUILT_IN_CTZ):
10526 CASE_INT_FN (BUILT_IN_CLRSB):
10527 CASE_INT_FN (BUILT_IN_POPCOUNT):
10528 CASE_INT_FN (BUILT_IN_PARITY):
10529 return fold_builtin_bitop (fndecl, arg0);
10531 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10532 return fold_builtin_signbit (loc, arg0, type);
10534 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10535 return fold_builtin_significand (loc, arg0, type);
10537 CASE_FLT_FN (BUILT_IN_ILOGB):
10538 CASE_FLT_FN (BUILT_IN_LOGB):
10539 return fold_builtin_logb (loc, arg0, type);
10541 case BUILT_IN_ISASCII:
10542 return fold_builtin_isascii (loc, arg0);
10544 case BUILT_IN_TOASCII:
10545 return fold_builtin_toascii (loc, arg0);
10547 case BUILT_IN_ISDIGIT:
10548 return fold_builtin_isdigit (loc, arg0);
10550 CASE_FLT_FN (BUILT_IN_FINITE):
10551 case BUILT_IN_FINITED32:
10552 case BUILT_IN_FINITED64:
10553 case BUILT_IN_FINITED128:
10554 case BUILT_IN_ISFINITE:
10556 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10557 if (ret)
10558 return ret;
10559 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10562 CASE_FLT_FN (BUILT_IN_ISINF):
10563 case BUILT_IN_ISINFD32:
10564 case BUILT_IN_ISINFD64:
10565 case BUILT_IN_ISINFD128:
10567 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10568 if (ret)
10569 return ret;
10570 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10573 case BUILT_IN_ISNORMAL:
10574 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10576 case BUILT_IN_ISINF_SIGN:
10577 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10579 CASE_FLT_FN (BUILT_IN_ISNAN):
10580 case BUILT_IN_ISNAND32:
10581 case BUILT_IN_ISNAND64:
10582 case BUILT_IN_ISNAND128:
10583 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10585 case BUILT_IN_PRINTF:
10586 case BUILT_IN_PRINTF_UNLOCKED:
10587 case BUILT_IN_VPRINTF:
10588 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10590 case BUILT_IN_FREE:
10591 if (integer_zerop (arg0))
10592 return build_empty_stmt (loc);
10593 break;
10595 default:
10596 break;
10599 return NULL_TREE;
10603 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10604 IGNORE is true if the result of the function call is ignored. This
10605 function returns NULL_TREE if no simplification was possible. */
10607 static tree
10608 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10610 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10611 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10613 switch (fcode)
10615 CASE_FLT_FN (BUILT_IN_JN):
10616 if (validate_arg (arg0, INTEGER_TYPE)
10617 && validate_arg (arg1, REAL_TYPE))
10618 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10619 break;
10621 CASE_FLT_FN (BUILT_IN_YN):
10622 if (validate_arg (arg0, INTEGER_TYPE)
10623 && validate_arg (arg1, REAL_TYPE))
10624 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10625 &dconst0, false);
10626 break;
10628 CASE_FLT_FN (BUILT_IN_DREM):
10629 CASE_FLT_FN (BUILT_IN_REMAINDER):
10630 if (validate_arg (arg0, REAL_TYPE)
10631 && validate_arg(arg1, REAL_TYPE))
10632 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10633 break;
10635 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10636 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10637 if (validate_arg (arg0, REAL_TYPE)
10638 && validate_arg(arg1, POINTER_TYPE))
10639 return do_mpfr_lgamma_r (arg0, arg1, type);
10640 break;
10642 CASE_FLT_FN (BUILT_IN_ATAN2):
10643 if (validate_arg (arg0, REAL_TYPE)
10644 && validate_arg(arg1, REAL_TYPE))
10645 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10646 break;
10648 CASE_FLT_FN (BUILT_IN_FDIM):
10649 if (validate_arg (arg0, REAL_TYPE)
10650 && validate_arg(arg1, REAL_TYPE))
10651 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10652 break;
10654 CASE_FLT_FN (BUILT_IN_HYPOT):
10655 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10657 CASE_FLT_FN (BUILT_IN_CPOW):
10658 if (validate_arg (arg0, COMPLEX_TYPE)
10659 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10660 && validate_arg (arg1, COMPLEX_TYPE)
10661 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10662 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10663 break;
10665 CASE_FLT_FN (BUILT_IN_LDEXP):
10666 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10667 CASE_FLT_FN (BUILT_IN_SCALBN):
10668 CASE_FLT_FN (BUILT_IN_SCALBLN):
10669 return fold_builtin_load_exponent (loc, arg0, arg1,
10670 type, /*ldexp=*/false);
10672 CASE_FLT_FN (BUILT_IN_FREXP):
10673 return fold_builtin_frexp (loc, arg0, arg1, type);
10675 CASE_FLT_FN (BUILT_IN_MODF):
10676 return fold_builtin_modf (loc, arg0, arg1, type);
10678 case BUILT_IN_BZERO:
10679 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10681 case BUILT_IN_FPUTS:
10682 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10684 case BUILT_IN_FPUTS_UNLOCKED:
10685 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10687 case BUILT_IN_STRSTR:
10688 return fold_builtin_strstr (loc, arg0, arg1, type);
10690 case BUILT_IN_STRCAT:
10691 return fold_builtin_strcat (loc, arg0, arg1);
10693 case BUILT_IN_STRSPN:
10694 return fold_builtin_strspn (loc, arg0, arg1);
10696 case BUILT_IN_STRCSPN:
10697 return fold_builtin_strcspn (loc, arg0, arg1);
10699 case BUILT_IN_STRCHR:
10700 case BUILT_IN_INDEX:
10701 return fold_builtin_strchr (loc, arg0, arg1, type);
10703 case BUILT_IN_STRRCHR:
10704 case BUILT_IN_RINDEX:
10705 return fold_builtin_strrchr (loc, arg0, arg1, type);
10707 case BUILT_IN_STRCPY:
10708 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10710 case BUILT_IN_STPCPY:
10711 if (ignore)
10713 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10714 if (!fn)
10715 break;
10717 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10719 else
10720 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10721 break;
10723 case BUILT_IN_STRCMP:
10724 return fold_builtin_strcmp (loc, arg0, arg1);
10726 case BUILT_IN_STRPBRK:
10727 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10729 case BUILT_IN_EXPECT:
10730 return fold_builtin_expect (loc, arg0, arg1);
10732 CASE_FLT_FN (BUILT_IN_POW):
10733 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10735 CASE_FLT_FN (BUILT_IN_POWI):
10736 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10738 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10739 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10741 CASE_FLT_FN (BUILT_IN_FMIN):
10742 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10744 CASE_FLT_FN (BUILT_IN_FMAX):
10745 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10747 case BUILT_IN_ISGREATER:
10748 return fold_builtin_unordered_cmp (loc, fndecl,
10749 arg0, arg1, UNLE_EXPR, LE_EXPR);
10750 case BUILT_IN_ISGREATEREQUAL:
10751 return fold_builtin_unordered_cmp (loc, fndecl,
10752 arg0, arg1, UNLT_EXPR, LT_EXPR);
10753 case BUILT_IN_ISLESS:
10754 return fold_builtin_unordered_cmp (loc, fndecl,
10755 arg0, arg1, UNGE_EXPR, GE_EXPR);
10756 case BUILT_IN_ISLESSEQUAL:
10757 return fold_builtin_unordered_cmp (loc, fndecl,
10758 arg0, arg1, UNGT_EXPR, GT_EXPR);
10759 case BUILT_IN_ISLESSGREATER:
10760 return fold_builtin_unordered_cmp (loc, fndecl,
10761 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10762 case BUILT_IN_ISUNORDERED:
10763 return fold_builtin_unordered_cmp (loc, fndecl,
10764 arg0, arg1, UNORDERED_EXPR,
10765 NOP_EXPR);
10767 /* We do the folding for va_start in the expander. */
10768 case BUILT_IN_VA_START:
10769 break;
10771 case BUILT_IN_SPRINTF:
10772 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10774 case BUILT_IN_OBJECT_SIZE:
10775 return fold_builtin_object_size (arg0, arg1);
10777 case BUILT_IN_PRINTF:
10778 case BUILT_IN_PRINTF_UNLOCKED:
10779 case BUILT_IN_VPRINTF:
10780 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10782 case BUILT_IN_PRINTF_CHK:
10783 case BUILT_IN_VPRINTF_CHK:
10784 if (!validate_arg (arg0, INTEGER_TYPE)
10785 || TREE_SIDE_EFFECTS (arg0))
10786 return NULL_TREE;
10787 else
10788 return fold_builtin_printf (loc, fndecl,
10789 arg1, NULL_TREE, ignore, fcode);
10790 break;
10792 case BUILT_IN_FPRINTF:
10793 case BUILT_IN_FPRINTF_UNLOCKED:
10794 case BUILT_IN_VFPRINTF:
10795 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10796 ignore, fcode);
10798 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10799 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10801 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10802 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10804 default:
10805 break;
10807 return NULL_TREE;
10810 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10811 and ARG2. IGNORE is true if the result of the function call is ignored.
10812 This function returns NULL_TREE if no simplification was possible. */
10814 static tree
10815 fold_builtin_3 (location_t loc, tree fndecl,
10816 tree arg0, tree arg1, tree arg2, bool ignore)
10818 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10819 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10820 switch (fcode)
10823 CASE_FLT_FN (BUILT_IN_SINCOS):
10824 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10826 CASE_FLT_FN (BUILT_IN_FMA):
10827 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10828 break;
10830 CASE_FLT_FN (BUILT_IN_REMQUO):
10831 if (validate_arg (arg0, REAL_TYPE)
10832 && validate_arg(arg1, REAL_TYPE)
10833 && validate_arg(arg2, POINTER_TYPE))
10834 return do_mpfr_remquo (arg0, arg1, arg2);
10835 break;
10837 case BUILT_IN_MEMSET:
10838 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10840 case BUILT_IN_BCOPY:
10841 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10842 void_type_node, true, /*endp=*/3);
10844 case BUILT_IN_MEMCPY:
10845 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10846 type, ignore, /*endp=*/0);
10848 case BUILT_IN_MEMPCPY:
10849 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10850 type, ignore, /*endp=*/1);
10852 case BUILT_IN_MEMMOVE:
10853 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10854 type, ignore, /*endp=*/3);
10856 case BUILT_IN_STRNCAT:
10857 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10859 case BUILT_IN_STRNCPY:
10860 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10862 case BUILT_IN_STRNCMP:
10863 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10865 case BUILT_IN_MEMCHR:
10866 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10868 case BUILT_IN_BCMP:
10869 case BUILT_IN_MEMCMP:
10870 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10872 case BUILT_IN_SPRINTF:
10873 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10875 case BUILT_IN_SNPRINTF:
10876 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10878 case BUILT_IN_STRCPY_CHK:
10879 case BUILT_IN_STPCPY_CHK:
10880 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10881 ignore, fcode);
10883 case BUILT_IN_STRCAT_CHK:
10884 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10886 case BUILT_IN_PRINTF_CHK:
10887 case BUILT_IN_VPRINTF_CHK:
10888 if (!validate_arg (arg0, INTEGER_TYPE)
10889 || TREE_SIDE_EFFECTS (arg0))
10890 return NULL_TREE;
10891 else
10892 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10893 break;
10895 case BUILT_IN_FPRINTF:
10896 case BUILT_IN_FPRINTF_UNLOCKED:
10897 case BUILT_IN_VFPRINTF:
10898 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10899 ignore, fcode);
10901 case BUILT_IN_FPRINTF_CHK:
10902 case BUILT_IN_VFPRINTF_CHK:
10903 if (!validate_arg (arg1, INTEGER_TYPE)
10904 || TREE_SIDE_EFFECTS (arg1))
10905 return NULL_TREE;
10906 else
10907 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10908 ignore, fcode);
10910 default:
10911 break;
10913 return NULL_TREE;
10916 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10917 ARG2, and ARG3. IGNORE is true if the result of the function call is
10918 ignored. This function returns NULL_TREE if no simplification was
10919 possible. */
10921 static tree
10922 fold_builtin_4 (location_t loc, tree fndecl,
10923 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10925 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10927 switch (fcode)
10929 case BUILT_IN_MEMCPY_CHK:
10930 case BUILT_IN_MEMPCPY_CHK:
10931 case BUILT_IN_MEMMOVE_CHK:
10932 case BUILT_IN_MEMSET_CHK:
10933 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10934 NULL_TREE, ignore,
10935 DECL_FUNCTION_CODE (fndecl));
10937 case BUILT_IN_STRNCPY_CHK:
10938 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10940 case BUILT_IN_STRNCAT_CHK:
10941 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10943 case BUILT_IN_SNPRINTF:
10944 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10946 case BUILT_IN_FPRINTF_CHK:
10947 case BUILT_IN_VFPRINTF_CHK:
10948 if (!validate_arg (arg1, INTEGER_TYPE)
10949 || TREE_SIDE_EFFECTS (arg1))
10950 return NULL_TREE;
10951 else
10952 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10953 ignore, fcode);
10954 break;
10956 default:
10957 break;
10959 return NULL_TREE;
10962 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10963 arguments, where NARGS <= 4. IGNORE is true if the result of the
10964 function call is ignored. This function returns NULL_TREE if no
10965 simplification was possible. Note that this only folds builtins with
10966 fixed argument patterns. Foldings that do varargs-to-varargs
10967 transformations, or that match calls with more than 4 arguments,
10968 need to be handled with fold_builtin_varargs instead. */
10970 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10972 static tree
10973 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10975 tree ret = NULL_TREE;
10977 switch (nargs)
10979 case 0:
10980 ret = fold_builtin_0 (loc, fndecl, ignore);
10981 break;
10982 case 1:
10983 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10984 break;
10985 case 2:
10986 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10987 break;
10988 case 3:
10989 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10990 break;
10991 case 4:
10992 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10993 ignore);
10994 break;
10995 default:
10996 break;
10998 if (ret)
11000 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11001 SET_EXPR_LOCATION (ret, loc);
11002 TREE_NO_WARNING (ret) = 1;
11003 return ret;
11005 return NULL_TREE;
11008 /* Builtins with folding operations that operate on "..." arguments
11009 need special handling; we need to store the arguments in a convenient
11010 data structure before attempting any folding. Fortunately there are
11011 only a few builtins that fall into this category. FNDECL is the
11012 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11013 result of the function call is ignored. */
11015 static tree
11016 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11017 bool ignore ATTRIBUTE_UNUSED)
11019 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11020 tree ret = NULL_TREE;
11022 switch (fcode)
11024 case BUILT_IN_SPRINTF_CHK:
11025 case BUILT_IN_VSPRINTF_CHK:
11026 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11027 break;
11029 case BUILT_IN_SNPRINTF_CHK:
11030 case BUILT_IN_VSNPRINTF_CHK:
11031 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11032 break;
11034 case BUILT_IN_FPCLASSIFY:
11035 ret = fold_builtin_fpclassify (loc, exp);
11036 break;
11038 default:
11039 break;
11041 if (ret)
11043 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11044 SET_EXPR_LOCATION (ret, loc);
11045 TREE_NO_WARNING (ret) = 1;
11046 return ret;
11048 return NULL_TREE;
11051 /* Return true if FNDECL shouldn't be folded right now.
11052 If a built-in function has an inline attribute always_inline
11053 wrapper, defer folding it after always_inline functions have
11054 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11055 might not be performed. */
11057 bool
11058 avoid_folding_inline_builtin (tree fndecl)
11060 return (DECL_DECLARED_INLINE_P (fndecl)
11061 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11062 && cfun
11063 && !cfun->always_inline_functions_inlined
11064 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11067 /* A wrapper function for builtin folding that prevents warnings for
11068 "statement without effect" and the like, caused by removing the
11069 call node earlier than the warning is generated. */
11071 tree
11072 fold_call_expr (location_t loc, tree exp, bool ignore)
11074 tree ret = NULL_TREE;
11075 tree fndecl = get_callee_fndecl (exp);
11076 if (fndecl
11077 && TREE_CODE (fndecl) == FUNCTION_DECL
11078 && DECL_BUILT_IN (fndecl)
11079 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11080 yet. Defer folding until we see all the arguments
11081 (after inlining). */
11082 && !CALL_EXPR_VA_ARG_PACK (exp))
11084 int nargs = call_expr_nargs (exp);
11086 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11087 instead last argument is __builtin_va_arg_pack (). Defer folding
11088 even in that case, until arguments are finalized. */
11089 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11091 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11092 if (fndecl2
11093 && TREE_CODE (fndecl2) == FUNCTION_DECL
11094 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11095 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11096 return NULL_TREE;
11099 if (avoid_folding_inline_builtin (fndecl))
11100 return NULL_TREE;
11102 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11103 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11104 CALL_EXPR_ARGP (exp), ignore);
11105 else
11107 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11109 tree *args = CALL_EXPR_ARGP (exp);
11110 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11112 if (!ret)
11113 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11114 if (ret)
11115 return ret;
11118 return NULL_TREE;
11121 /* Conveniently construct a function call expression. FNDECL names the
11122 function to be called and N arguments are passed in the array
11123 ARGARRAY. */
11125 tree
11126 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11128 tree fntype = TREE_TYPE (fndecl);
11129 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11131 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11134 /* Conveniently construct a function call expression. FNDECL names the
11135 function to be called and the arguments are passed in the vector
11136 VEC. */
11138 tree
11139 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11141 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11142 VEC_address (tree, vec));
11146 /* Conveniently construct a function call expression. FNDECL names the
11147 function to be called, N is the number of arguments, and the "..."
11148 parameters are the argument expressions. */
11150 tree
11151 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11153 va_list ap;
11154 tree *argarray = XALLOCAVEC (tree, n);
11155 int i;
11157 va_start (ap, n);
11158 for (i = 0; i < n; i++)
11159 argarray[i] = va_arg (ap, tree);
11160 va_end (ap);
11161 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11164 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11165 varargs macros aren't supported by all bootstrap compilers. */
11167 tree
11168 build_call_expr (tree fndecl, int n, ...)
11170 va_list ap;
11171 tree *argarray = XALLOCAVEC (tree, n);
11172 int i;
11174 va_start (ap, n);
11175 for (i = 0; i < n; i++)
11176 argarray[i] = va_arg (ap, tree);
11177 va_end (ap);
11178 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11181 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11182 N arguments are passed in the array ARGARRAY. */
11184 tree
11185 fold_builtin_call_array (location_t loc, tree type,
11186 tree fn,
11187 int n,
11188 tree *argarray)
11190 tree ret = NULL_TREE;
11191 tree exp;
11193 if (TREE_CODE (fn) == ADDR_EXPR)
11195 tree fndecl = TREE_OPERAND (fn, 0);
11196 if (TREE_CODE (fndecl) == FUNCTION_DECL
11197 && DECL_BUILT_IN (fndecl))
11199 /* If last argument is __builtin_va_arg_pack (), arguments to this
11200 function are not finalized yet. Defer folding until they are. */
11201 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11203 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11204 if (fndecl2
11205 && TREE_CODE (fndecl2) == FUNCTION_DECL
11206 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11207 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11208 return build_call_array_loc (loc, type, fn, n, argarray);
11210 if (avoid_folding_inline_builtin (fndecl))
11211 return build_call_array_loc (loc, type, fn, n, argarray);
11212 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11214 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11215 if (ret)
11216 return ret;
11218 return build_call_array_loc (loc, type, fn, n, argarray);
11220 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11222 /* First try the transformations that don't require consing up
11223 an exp. */
11224 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11225 if (ret)
11226 return ret;
11229 /* If we got this far, we need to build an exp. */
11230 exp = build_call_array_loc (loc, type, fn, n, argarray);
11231 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11232 return ret ? ret : exp;
11236 return build_call_array_loc (loc, type, fn, n, argarray);
11239 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11240 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11241 of arguments in ARGS to be omitted. OLDNARGS is the number of
11242 elements in ARGS. */
11244 static tree
11245 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11246 int skip, tree fndecl, int n, va_list newargs)
11248 int nargs = oldnargs - skip + n;
11249 tree *buffer;
11251 if (n > 0)
11253 int i, j;
11255 buffer = XALLOCAVEC (tree, nargs);
11256 for (i = 0; i < n; i++)
11257 buffer[i] = va_arg (newargs, tree);
11258 for (j = skip; j < oldnargs; j++, i++)
11259 buffer[i] = args[j];
11261 else
11262 buffer = args + skip;
11264 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11267 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11268 list ARGS along with N new arguments specified as the "..."
11269 parameters. SKIP is the number of arguments in ARGS to be omitted.
11270 OLDNARGS is the number of elements in ARGS. */
11272 static tree
11273 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11274 int skip, tree fndecl, int n, ...)
11276 va_list ap;
11277 tree t;
11279 va_start (ap, n);
11280 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11281 va_end (ap);
11283 return t;
11286 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11287 along with N new arguments specified as the "..." parameters. SKIP
11288 is the number of arguments in EXP to be omitted. This function is used
11289 to do varargs-to-varargs transformations. */
11291 static tree
11292 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11294 va_list ap;
11295 tree t;
11297 va_start (ap, n);
11298 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11299 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11300 va_end (ap);
11302 return t;
11305 /* Validate a single argument ARG against a tree code CODE representing
11306 a type. */
11308 static bool
11309 validate_arg (const_tree arg, enum tree_code code)
11311 if (!arg)
11312 return false;
11313 else if (code == POINTER_TYPE)
11314 return POINTER_TYPE_P (TREE_TYPE (arg));
11315 else if (code == INTEGER_TYPE)
11316 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11317 return code == TREE_CODE (TREE_TYPE (arg));
11320 /* This function validates the types of a function call argument list
11321 against a specified list of tree_codes. If the last specifier is a 0,
11322 that represents an ellipses, otherwise the last specifier must be a
11323 VOID_TYPE.
11325 This is the GIMPLE version of validate_arglist. Eventually we want to
11326 completely convert builtins.c to work from GIMPLEs and the tree based
11327 validate_arglist will then be removed. */
11329 bool
11330 validate_gimple_arglist (const_gimple call, ...)
11332 enum tree_code code;
11333 bool res = 0;
11334 va_list ap;
11335 const_tree arg;
11336 size_t i;
11338 va_start (ap, call);
11339 i = 0;
11343 code = (enum tree_code) va_arg (ap, int);
11344 switch (code)
11346 case 0:
11347 /* This signifies an ellipses, any further arguments are all ok. */
11348 res = true;
11349 goto end;
11350 case VOID_TYPE:
11351 /* This signifies an endlink, if no arguments remain, return
11352 true, otherwise return false. */
11353 res = (i == gimple_call_num_args (call));
11354 goto end;
11355 default:
11356 /* If no parameters remain or the parameter's code does not
11357 match the specified code, return false. Otherwise continue
11358 checking any remaining arguments. */
11359 arg = gimple_call_arg (call, i++);
11360 if (!validate_arg (arg, code))
11361 goto end;
11362 break;
11365 while (1);
11367 /* We need gotos here since we can only have one VA_CLOSE in a
11368 function. */
11369 end: ;
11370 va_end (ap);
11372 return res;
11375 /* This function validates the types of a function call argument list
11376 against a specified list of tree_codes. If the last specifier is a 0,
11377 that represents an ellipses, otherwise the last specifier must be a
11378 VOID_TYPE. */
11380 bool
11381 validate_arglist (const_tree callexpr, ...)
11383 enum tree_code code;
11384 bool res = 0;
11385 va_list ap;
11386 const_call_expr_arg_iterator iter;
11387 const_tree arg;
11389 va_start (ap, callexpr);
11390 init_const_call_expr_arg_iterator (callexpr, &iter);
11394 code = (enum tree_code) va_arg (ap, int);
11395 switch (code)
11397 case 0:
11398 /* This signifies an ellipses, any further arguments are all ok. */
11399 res = true;
11400 goto end;
11401 case VOID_TYPE:
11402 /* This signifies an endlink, if no arguments remain, return
11403 true, otherwise return false. */
11404 res = !more_const_call_expr_args_p (&iter);
11405 goto end;
11406 default:
11407 /* If no parameters remain or the parameter's code does not
11408 match the specified code, return false. Otherwise continue
11409 checking any remaining arguments. */
11410 arg = next_const_call_expr_arg (&iter);
11411 if (!validate_arg (arg, code))
11412 goto end;
11413 break;
11416 while (1);
11418 /* We need gotos here since we can only have one VA_CLOSE in a
11419 function. */
11420 end: ;
11421 va_end (ap);
11423 return res;
11426 /* Default target-specific builtin expander that does nothing. */
11429 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11430 rtx target ATTRIBUTE_UNUSED,
11431 rtx subtarget ATTRIBUTE_UNUSED,
11432 enum machine_mode mode ATTRIBUTE_UNUSED,
11433 int ignore ATTRIBUTE_UNUSED)
11435 return NULL_RTX;
11438 /* Returns true is EXP represents data that would potentially reside
11439 in a readonly section. */
11441 static bool
11442 readonly_data_expr (tree exp)
11444 STRIP_NOPS (exp);
11446 if (TREE_CODE (exp) != ADDR_EXPR)
11447 return false;
11449 exp = get_base_address (TREE_OPERAND (exp, 0));
11450 if (!exp)
11451 return false;
11453 /* Make sure we call decl_readonly_section only for trees it
11454 can handle (since it returns true for everything it doesn't
11455 understand). */
11456 if (TREE_CODE (exp) == STRING_CST
11457 || TREE_CODE (exp) == CONSTRUCTOR
11458 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11459 return decl_readonly_section (exp, 0);
11460 else
11461 return false;
11464 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11465 to the call, and TYPE is its return type.
11467 Return NULL_TREE if no simplification was possible, otherwise return the
11468 simplified form of the call as a tree.
11470 The simplified form may be a constant or other expression which
11471 computes the same value, but in a more efficient manner (including
11472 calls to other builtin functions).
11474 The call may contain arguments which need to be evaluated, but
11475 which are not useful to determine the result of the call. In
11476 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11477 COMPOUND_EXPR will be an argument which must be evaluated.
11478 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11479 COMPOUND_EXPR in the chain will contain the tree for the simplified
11480 form of the builtin function call. */
11482 static tree
11483 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11485 if (!validate_arg (s1, POINTER_TYPE)
11486 || !validate_arg (s2, POINTER_TYPE))
11487 return NULL_TREE;
11488 else
11490 tree fn;
11491 const char *p1, *p2;
11493 p2 = c_getstr (s2);
11494 if (p2 == NULL)
11495 return NULL_TREE;
11497 p1 = c_getstr (s1);
11498 if (p1 != NULL)
11500 const char *r = strstr (p1, p2);
11501 tree tem;
11503 if (r == NULL)
11504 return build_int_cst (TREE_TYPE (s1), 0);
11506 /* Return an offset into the constant string argument. */
11507 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11508 return fold_convert_loc (loc, type, tem);
11511 /* The argument is const char *, and the result is char *, so we need
11512 a type conversion here to avoid a warning. */
11513 if (p2[0] == '\0')
11514 return fold_convert_loc (loc, type, s1);
11516 if (p2[1] != '\0')
11517 return NULL_TREE;
11519 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11520 if (!fn)
11521 return NULL_TREE;
11523 /* New argument list transforming strstr(s1, s2) to
11524 strchr(s1, s2[0]). */
11525 return build_call_expr_loc (loc, fn, 2, s1,
11526 build_int_cst (integer_type_node, p2[0]));
11530 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11531 the call, and TYPE is its return type.
11533 Return NULL_TREE if no simplification was possible, otherwise return the
11534 simplified form of the call as a tree.
11536 The simplified form may be a constant or other expression which
11537 computes the same value, but in a more efficient manner (including
11538 calls to other builtin functions).
11540 The call may contain arguments which need to be evaluated, but
11541 which are not useful to determine the result of the call. In
11542 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11543 COMPOUND_EXPR will be an argument which must be evaluated.
11544 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11545 COMPOUND_EXPR in the chain will contain the tree for the simplified
11546 form of the builtin function call. */
11548 static tree
11549 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11551 if (!validate_arg (s1, POINTER_TYPE)
11552 || !validate_arg (s2, INTEGER_TYPE))
11553 return NULL_TREE;
11554 else
11556 const char *p1;
11558 if (TREE_CODE (s2) != INTEGER_CST)
11559 return NULL_TREE;
11561 p1 = c_getstr (s1);
11562 if (p1 != NULL)
11564 char c;
11565 const char *r;
11566 tree tem;
11568 if (target_char_cast (s2, &c))
11569 return NULL_TREE;
11571 r = strchr (p1, c);
11573 if (r == NULL)
11574 return build_int_cst (TREE_TYPE (s1), 0);
11576 /* Return an offset into the constant string argument. */
11577 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11578 return fold_convert_loc (loc, type, tem);
11580 return NULL_TREE;
11584 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11585 the call, and TYPE is its return type.
11587 Return NULL_TREE if no simplification was possible, otherwise return the
11588 simplified form of the call as a tree.
11590 The simplified form may be a constant or other expression which
11591 computes the same value, but in a more efficient manner (including
11592 calls to other builtin functions).
11594 The call may contain arguments which need to be evaluated, but
11595 which are not useful to determine the result of the call. In
11596 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11597 COMPOUND_EXPR will be an argument which must be evaluated.
11598 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11599 COMPOUND_EXPR in the chain will contain the tree for the simplified
11600 form of the builtin function call. */
11602 static tree
11603 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11605 if (!validate_arg (s1, POINTER_TYPE)
11606 || !validate_arg (s2, INTEGER_TYPE))
11607 return NULL_TREE;
11608 else
11610 tree fn;
11611 const char *p1;
11613 if (TREE_CODE (s2) != INTEGER_CST)
11614 return NULL_TREE;
11616 p1 = c_getstr (s1);
11617 if (p1 != NULL)
11619 char c;
11620 const char *r;
11621 tree tem;
11623 if (target_char_cast (s2, &c))
11624 return NULL_TREE;
11626 r = strrchr (p1, c);
11628 if (r == NULL)
11629 return build_int_cst (TREE_TYPE (s1), 0);
11631 /* Return an offset into the constant string argument. */
11632 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11633 return fold_convert_loc (loc, type, tem);
11636 if (! integer_zerop (s2))
11637 return NULL_TREE;
11639 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11640 if (!fn)
11641 return NULL_TREE;
11643 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11644 return build_call_expr_loc (loc, fn, 2, s1, s2);
11648 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11649 to the call, and TYPE is its return type.
11651 Return NULL_TREE if no simplification was possible, otherwise return the
11652 simplified form of the call as a tree.
11654 The simplified form may be a constant or other expression which
11655 computes the same value, but in a more efficient manner (including
11656 calls to other builtin functions).
11658 The call may contain arguments which need to be evaluated, but
11659 which are not useful to determine the result of the call. In
11660 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11661 COMPOUND_EXPR will be an argument which must be evaluated.
11662 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11663 COMPOUND_EXPR in the chain will contain the tree for the simplified
11664 form of the builtin function call. */
11666 static tree
11667 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11669 if (!validate_arg (s1, POINTER_TYPE)
11670 || !validate_arg (s2, POINTER_TYPE))
11671 return NULL_TREE;
11672 else
11674 tree fn;
11675 const char *p1, *p2;
11677 p2 = c_getstr (s2);
11678 if (p2 == NULL)
11679 return NULL_TREE;
11681 p1 = c_getstr (s1);
11682 if (p1 != NULL)
11684 const char *r = strpbrk (p1, p2);
11685 tree tem;
11687 if (r == NULL)
11688 return build_int_cst (TREE_TYPE (s1), 0);
11690 /* Return an offset into the constant string argument. */
11691 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11692 return fold_convert_loc (loc, type, tem);
11695 if (p2[0] == '\0')
11696 /* strpbrk(x, "") == NULL.
11697 Evaluate and ignore s1 in case it had side-effects. */
11698 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11700 if (p2[1] != '\0')
11701 return NULL_TREE; /* Really call strpbrk. */
11703 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11704 if (!fn)
11705 return NULL_TREE;
11707 /* New argument list transforming strpbrk(s1, s2) to
11708 strchr(s1, s2[0]). */
11709 return build_call_expr_loc (loc, fn, 2, s1,
11710 build_int_cst (integer_type_node, p2[0]));
11714 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11715 to the call.
11717 Return NULL_TREE if no simplification was possible, otherwise return the
11718 simplified form of the call as a tree.
11720 The simplified form may be a constant or other expression which
11721 computes the same value, but in a more efficient manner (including
11722 calls to other builtin functions).
11724 The call may contain arguments which need to be evaluated, but
11725 which are not useful to determine the result of the call. In
11726 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11727 COMPOUND_EXPR will be an argument which must be evaluated.
11728 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11729 COMPOUND_EXPR in the chain will contain the tree for the simplified
11730 form of the builtin function call. */
11732 static tree
11733 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11735 if (!validate_arg (dst, POINTER_TYPE)
11736 || !validate_arg (src, POINTER_TYPE))
11737 return NULL_TREE;
11738 else
11740 const char *p = c_getstr (src);
11742 /* If the string length is zero, return the dst parameter. */
11743 if (p && *p == '\0')
11744 return dst;
11746 if (optimize_insn_for_speed_p ())
11748 /* See if we can store by pieces into (dst + strlen(dst)). */
11749 tree newdst, call;
11750 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11751 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11753 if (!strlen_fn || !strcpy_fn)
11754 return NULL_TREE;
11756 /* If we don't have a movstr we don't want to emit an strcpy
11757 call. We have to do that if the length of the source string
11758 isn't computable (in that case we can use memcpy probably
11759 later expanding to a sequence of mov instructions). If we
11760 have movstr instructions we can emit strcpy calls. */
11761 if (!HAVE_movstr)
11763 tree len = c_strlen (src, 1);
11764 if (! len || TREE_SIDE_EFFECTS (len))
11765 return NULL_TREE;
11768 /* Stabilize the argument list. */
11769 dst = builtin_save_expr (dst);
11771 /* Create strlen (dst). */
11772 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11773 /* Create (dst p+ strlen (dst)). */
11775 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11776 newdst = builtin_save_expr (newdst);
11778 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11779 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11781 return NULL_TREE;
11785 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11786 arguments to the call.
11788 Return NULL_TREE if no simplification was possible, otherwise return the
11789 simplified form of the call as a tree.
11791 The simplified form may be a constant or other expression which
11792 computes the same value, but in a more efficient manner (including
11793 calls to other builtin functions).
11795 The call may contain arguments which need to be evaluated, but
11796 which are not useful to determine the result of the call. In
11797 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11798 COMPOUND_EXPR will be an argument which must be evaluated.
11799 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11800 COMPOUND_EXPR in the chain will contain the tree for the simplified
11801 form of the builtin function call. */
11803 static tree
11804 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11806 if (!validate_arg (dst, POINTER_TYPE)
11807 || !validate_arg (src, POINTER_TYPE)
11808 || !validate_arg (len, INTEGER_TYPE))
11809 return NULL_TREE;
11810 else
11812 const char *p = c_getstr (src);
11814 /* If the requested length is zero, or the src parameter string
11815 length is zero, return the dst parameter. */
11816 if (integer_zerop (len) || (p && *p == '\0'))
11817 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11819 /* If the requested len is greater than or equal to the string
11820 length, call strcat. */
11821 if (TREE_CODE (len) == INTEGER_CST && p
11822 && compare_tree_int (len, strlen (p)) >= 0)
11824 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11826 /* If the replacement _DECL isn't initialized, don't do the
11827 transformation. */
11828 if (!fn)
11829 return NULL_TREE;
11831 return build_call_expr_loc (loc, fn, 2, dst, src);
11833 return NULL_TREE;
11837 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11838 to the call.
11840 Return NULL_TREE if no simplification was possible, otherwise return the
11841 simplified form of the call as a tree.
11843 The simplified form may be a constant or other expression which
11844 computes the same value, but in a more efficient manner (including
11845 calls to other builtin functions).
11847 The call may contain arguments which need to be evaluated, but
11848 which are not useful to determine the result of the call. In
11849 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11850 COMPOUND_EXPR will be an argument which must be evaluated.
11851 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11852 COMPOUND_EXPR in the chain will contain the tree for the simplified
11853 form of the builtin function call. */
11855 static tree
11856 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11858 if (!validate_arg (s1, POINTER_TYPE)
11859 || !validate_arg (s2, POINTER_TYPE))
11860 return NULL_TREE;
11861 else
11863 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11865 /* If both arguments are constants, evaluate at compile-time. */
11866 if (p1 && p2)
11868 const size_t r = strspn (p1, p2);
11869 return size_int (r);
11872 /* If either argument is "", return NULL_TREE. */
11873 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11874 /* Evaluate and ignore both arguments in case either one has
11875 side-effects. */
11876 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11877 s1, s2);
11878 return NULL_TREE;
11882 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11883 to the call.
11885 Return NULL_TREE if no simplification was possible, otherwise return the
11886 simplified form of the call as a tree.
11888 The simplified form may be a constant or other expression which
11889 computes the same value, but in a more efficient manner (including
11890 calls to other builtin functions).
11892 The call may contain arguments which need to be evaluated, but
11893 which are not useful to determine the result of the call. In
11894 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11895 COMPOUND_EXPR will be an argument which must be evaluated.
11896 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11897 COMPOUND_EXPR in the chain will contain the tree for the simplified
11898 form of the builtin function call. */
11900 static tree
11901 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11903 if (!validate_arg (s1, POINTER_TYPE)
11904 || !validate_arg (s2, POINTER_TYPE))
11905 return NULL_TREE;
11906 else
11908 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11910 /* If both arguments are constants, evaluate at compile-time. */
11911 if (p1 && p2)
11913 const size_t r = strcspn (p1, p2);
11914 return size_int (r);
11917 /* If the first argument is "", return NULL_TREE. */
11918 if (p1 && *p1 == '\0')
11920 /* Evaluate and ignore argument s2 in case it has
11921 side-effects. */
11922 return omit_one_operand_loc (loc, size_type_node,
11923 size_zero_node, s2);
11926 /* If the second argument is "", return __builtin_strlen(s1). */
11927 if (p2 && *p2 == '\0')
11929 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11931 /* If the replacement _DECL isn't initialized, don't do the
11932 transformation. */
11933 if (!fn)
11934 return NULL_TREE;
11936 return build_call_expr_loc (loc, fn, 1, s1);
11938 return NULL_TREE;
11942 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11943 to the call. IGNORE is true if the value returned
11944 by the builtin will be ignored. UNLOCKED is true is true if this
11945 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11946 the known length of the string. Return NULL_TREE if no simplification
11947 was possible. */
11949 tree
11950 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11951 bool ignore, bool unlocked, tree len)
11953 /* If we're using an unlocked function, assume the other unlocked
11954 functions exist explicitly. */
11955 tree const fn_fputc = (unlocked
11956 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11957 : builtin_decl_implicit (BUILT_IN_FPUTC));
11958 tree const fn_fwrite = (unlocked
11959 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11960 : builtin_decl_implicit (BUILT_IN_FWRITE));
11962 /* If the return value is used, don't do the transformation. */
11963 if (!ignore)
11964 return NULL_TREE;
11966 /* Verify the arguments in the original call. */
11967 if (!validate_arg (arg0, POINTER_TYPE)
11968 || !validate_arg (arg1, POINTER_TYPE))
11969 return NULL_TREE;
11971 if (! len)
11972 len = c_strlen (arg0, 0);
11974 /* Get the length of the string passed to fputs. If the length
11975 can't be determined, punt. */
11976 if (!len
11977 || TREE_CODE (len) != INTEGER_CST)
11978 return NULL_TREE;
11980 switch (compare_tree_int (len, 1))
11982 case -1: /* length is 0, delete the call entirely . */
11983 return omit_one_operand_loc (loc, integer_type_node,
11984 integer_zero_node, arg1);;
11986 case 0: /* length is 1, call fputc. */
11988 const char *p = c_getstr (arg0);
11990 if (p != NULL)
11992 if (fn_fputc)
11993 return build_call_expr_loc (loc, fn_fputc, 2,
11994 build_int_cst
11995 (integer_type_node, p[0]), arg1);
11996 else
11997 return NULL_TREE;
12000 /* FALLTHROUGH */
12001 case 1: /* length is greater than 1, call fwrite. */
12003 /* If optimizing for size keep fputs. */
12004 if (optimize_function_for_size_p (cfun))
12005 return NULL_TREE;
12006 /* New argument list transforming fputs(string, stream) to
12007 fwrite(string, 1, len, stream). */
12008 if (fn_fwrite)
12009 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12010 size_one_node, len, arg1);
12011 else
12012 return NULL_TREE;
12014 default:
12015 gcc_unreachable ();
12017 return NULL_TREE;
12020 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12021 produced. False otherwise. This is done so that we don't output the error
12022 or warning twice or three times. */
12024 bool
12025 fold_builtin_next_arg (tree exp, bool va_start_p)
12027 tree fntype = TREE_TYPE (current_function_decl);
12028 int nargs = call_expr_nargs (exp);
12029 tree arg;
12031 if (!stdarg_p (fntype))
12033 error ("%<va_start%> used in function with fixed args");
12034 return true;
12037 if (va_start_p)
12039 if (va_start_p && (nargs != 2))
12041 error ("wrong number of arguments to function %<va_start%>");
12042 return true;
12044 arg = CALL_EXPR_ARG (exp, 1);
12046 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12047 when we checked the arguments and if needed issued a warning. */
12048 else
12050 if (nargs == 0)
12052 /* Evidently an out of date version of <stdarg.h>; can't validate
12053 va_start's second argument, but can still work as intended. */
12054 warning (0, "%<__builtin_next_arg%> called without an argument");
12055 return true;
12057 else if (nargs > 1)
12059 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12060 return true;
12062 arg = CALL_EXPR_ARG (exp, 0);
12065 if (TREE_CODE (arg) == SSA_NAME)
12066 arg = SSA_NAME_VAR (arg);
12068 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12069 or __builtin_next_arg (0) the first time we see it, after checking
12070 the arguments and if needed issuing a warning. */
12071 if (!integer_zerop (arg))
12073 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12075 /* Strip off all nops for the sake of the comparison. This
12076 is not quite the same as STRIP_NOPS. It does more.
12077 We must also strip off INDIRECT_EXPR for C++ reference
12078 parameters. */
12079 while (CONVERT_EXPR_P (arg)
12080 || TREE_CODE (arg) == INDIRECT_REF)
12081 arg = TREE_OPERAND (arg, 0);
12082 if (arg != last_parm)
12084 /* FIXME: Sometimes with the tree optimizers we can get the
12085 not the last argument even though the user used the last
12086 argument. We just warn and set the arg to be the last
12087 argument so that we will get wrong-code because of
12088 it. */
12089 warning (0, "second parameter of %<va_start%> not last named argument");
12092 /* Undefined by C99 7.15.1.4p4 (va_start):
12093 "If the parameter parmN is declared with the register storage
12094 class, with a function or array type, or with a type that is
12095 not compatible with the type that results after application of
12096 the default argument promotions, the behavior is undefined."
12098 else if (DECL_REGISTER (arg))
12099 warning (0, "undefined behaviour when second parameter of "
12100 "%<va_start%> is declared with %<register%> storage");
12102 /* We want to verify the second parameter just once before the tree
12103 optimizers are run and then avoid keeping it in the tree,
12104 as otherwise we could warn even for correct code like:
12105 void foo (int i, ...)
12106 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12107 if (va_start_p)
12108 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12109 else
12110 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12112 return false;
12116 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12117 ORIG may be null if this is a 2-argument call. We don't attempt to
12118 simplify calls with more than 3 arguments.
12120 Return NULL_TREE if no simplification was possible, otherwise return the
12121 simplified form of the call as a tree. If IGNORED is true, it means that
12122 the caller does not use the returned value of the function. */
12124 static tree
12125 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12126 tree orig, int ignored)
12128 tree call, retval;
12129 const char *fmt_str = NULL;
12131 /* Verify the required arguments in the original call. We deal with two
12132 types of sprintf() calls: 'sprintf (str, fmt)' and
12133 'sprintf (dest, "%s", orig)'. */
12134 if (!validate_arg (dest, POINTER_TYPE)
12135 || !validate_arg (fmt, POINTER_TYPE))
12136 return NULL_TREE;
12137 if (orig && !validate_arg (orig, POINTER_TYPE))
12138 return NULL_TREE;
12140 /* Check whether the format is a literal string constant. */
12141 fmt_str = c_getstr (fmt);
12142 if (fmt_str == NULL)
12143 return NULL_TREE;
12145 call = NULL_TREE;
12146 retval = NULL_TREE;
12148 if (!init_target_chars ())
12149 return NULL_TREE;
12151 /* If the format doesn't contain % args or %%, use strcpy. */
12152 if (strchr (fmt_str, target_percent) == NULL)
12154 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12156 if (!fn)
12157 return NULL_TREE;
12159 /* Don't optimize sprintf (buf, "abc", ptr++). */
12160 if (orig)
12161 return NULL_TREE;
12163 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12164 'format' is known to contain no % formats. */
12165 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12166 if (!ignored)
12167 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12170 /* If the format is "%s", use strcpy if the result isn't used. */
12171 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12173 tree fn;
12174 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12176 if (!fn)
12177 return NULL_TREE;
12179 /* Don't crash on sprintf (str1, "%s"). */
12180 if (!orig)
12181 return NULL_TREE;
12183 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12184 if (!ignored)
12186 retval = c_strlen (orig, 1);
12187 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12188 return NULL_TREE;
12190 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12193 if (call && retval)
12195 retval = fold_convert_loc
12196 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12197 retval);
12198 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12200 else
12201 return call;
12204 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12205 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12206 attempt to simplify calls with more than 4 arguments.
12208 Return NULL_TREE if no simplification was possible, otherwise return the
12209 simplified form of the call as a tree. If IGNORED is true, it means that
12210 the caller does not use the returned value of the function. */
12212 static tree
12213 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12214 tree orig, int ignored)
12216 tree call, retval;
12217 const char *fmt_str = NULL;
12218 unsigned HOST_WIDE_INT destlen;
12220 /* Verify the required arguments in the original call. We deal with two
12221 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12222 'snprintf (dest, cst, "%s", orig)'. */
12223 if (!validate_arg (dest, POINTER_TYPE)
12224 || !validate_arg (destsize, INTEGER_TYPE)
12225 || !validate_arg (fmt, POINTER_TYPE))
12226 return NULL_TREE;
12227 if (orig && !validate_arg (orig, POINTER_TYPE))
12228 return NULL_TREE;
12230 if (!host_integerp (destsize, 1))
12231 return NULL_TREE;
12233 /* Check whether the format is a literal string constant. */
12234 fmt_str = c_getstr (fmt);
12235 if (fmt_str == NULL)
12236 return NULL_TREE;
12238 call = NULL_TREE;
12239 retval = NULL_TREE;
12241 if (!init_target_chars ())
12242 return NULL_TREE;
12244 destlen = tree_low_cst (destsize, 1);
12246 /* If the format doesn't contain % args or %%, use strcpy. */
12247 if (strchr (fmt_str, target_percent) == NULL)
12249 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12250 size_t len = strlen (fmt_str);
12252 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12253 if (orig)
12254 return NULL_TREE;
12256 /* We could expand this as
12257 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12258 or to
12259 memcpy (str, fmt_with_nul_at_cstm1, cst);
12260 but in the former case that might increase code size
12261 and in the latter case grow .rodata section too much.
12262 So punt for now. */
12263 if (len >= destlen)
12264 return NULL_TREE;
12266 if (!fn)
12267 return NULL_TREE;
12269 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12270 'format' is known to contain no % formats and
12271 strlen (fmt) < cst. */
12272 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12274 if (!ignored)
12275 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12278 /* If the format is "%s", use strcpy if the result isn't used. */
12279 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12281 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12282 unsigned HOST_WIDE_INT origlen;
12284 /* Don't crash on snprintf (str1, cst, "%s"). */
12285 if (!orig)
12286 return NULL_TREE;
12288 retval = c_strlen (orig, 1);
12289 if (!retval || !host_integerp (retval, 1))
12290 return NULL_TREE;
12292 origlen = tree_low_cst (retval, 1);
12293 /* We could expand this as
12294 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12295 or to
12296 memcpy (str1, str2_with_nul_at_cstm1, cst);
12297 but in the former case that might increase code size
12298 and in the latter case grow .rodata section too much.
12299 So punt for now. */
12300 if (origlen >= destlen)
12301 return NULL_TREE;
12303 /* Convert snprintf (str1, cst, "%s", str2) into
12304 strcpy (str1, str2) if strlen (str2) < cst. */
12305 if (!fn)
12306 return NULL_TREE;
12308 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12310 if (ignored)
12311 retval = NULL_TREE;
12314 if (call && retval)
12316 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12317 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12318 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12320 else
12321 return call;
12324 /* Expand a call EXP to __builtin_object_size. */
12327 expand_builtin_object_size (tree exp)
12329 tree ost;
12330 int object_size_type;
12331 tree fndecl = get_callee_fndecl (exp);
12333 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12335 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12336 exp, fndecl);
12337 expand_builtin_trap ();
12338 return const0_rtx;
12341 ost = CALL_EXPR_ARG (exp, 1);
12342 STRIP_NOPS (ost);
12344 if (TREE_CODE (ost) != INTEGER_CST
12345 || tree_int_cst_sgn (ost) < 0
12346 || compare_tree_int (ost, 3) > 0)
12348 error ("%Klast argument of %D is not integer constant between 0 and 3",
12349 exp, fndecl);
12350 expand_builtin_trap ();
12351 return const0_rtx;
12354 object_size_type = tree_low_cst (ost, 0);
12356 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12359 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12360 FCODE is the BUILT_IN_* to use.
12361 Return NULL_RTX if we failed; the caller should emit a normal call,
12362 otherwise try to get the result in TARGET, if convenient (and in
12363 mode MODE if that's convenient). */
12365 static rtx
12366 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12367 enum built_in_function fcode)
12369 tree dest, src, len, size;
12371 if (!validate_arglist (exp,
12372 POINTER_TYPE,
12373 fcode == BUILT_IN_MEMSET_CHK
12374 ? INTEGER_TYPE : POINTER_TYPE,
12375 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12376 return NULL_RTX;
12378 dest = CALL_EXPR_ARG (exp, 0);
12379 src = CALL_EXPR_ARG (exp, 1);
12380 len = CALL_EXPR_ARG (exp, 2);
12381 size = CALL_EXPR_ARG (exp, 3);
12383 if (! host_integerp (size, 1))
12384 return NULL_RTX;
12386 if (host_integerp (len, 1) || integer_all_onesp (size))
12388 tree fn;
12390 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12392 warning_at (tree_nonartificial_location (exp),
12393 0, "%Kcall to %D will always overflow destination buffer",
12394 exp, get_callee_fndecl (exp));
12395 return NULL_RTX;
12398 fn = NULL_TREE;
12399 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12400 mem{cpy,pcpy,move,set} is available. */
12401 switch (fcode)
12403 case BUILT_IN_MEMCPY_CHK:
12404 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12405 break;
12406 case BUILT_IN_MEMPCPY_CHK:
12407 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12408 break;
12409 case BUILT_IN_MEMMOVE_CHK:
12410 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12411 break;
12412 case BUILT_IN_MEMSET_CHK:
12413 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12414 break;
12415 default:
12416 break;
12419 if (! fn)
12420 return NULL_RTX;
12422 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12423 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12424 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12425 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12427 else if (fcode == BUILT_IN_MEMSET_CHK)
12428 return NULL_RTX;
12429 else
12431 unsigned int dest_align = get_pointer_alignment (dest);
12433 /* If DEST is not a pointer type, call the normal function. */
12434 if (dest_align == 0)
12435 return NULL_RTX;
12437 /* If SRC and DEST are the same (and not volatile), do nothing. */
12438 if (operand_equal_p (src, dest, 0))
12440 tree expr;
12442 if (fcode != BUILT_IN_MEMPCPY_CHK)
12444 /* Evaluate and ignore LEN in case it has side-effects. */
12445 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12446 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12449 expr = fold_build_pointer_plus (dest, len);
12450 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12453 /* __memmove_chk special case. */
12454 if (fcode == BUILT_IN_MEMMOVE_CHK)
12456 unsigned int src_align = get_pointer_alignment (src);
12458 if (src_align == 0)
12459 return NULL_RTX;
12461 /* If src is categorized for a readonly section we can use
12462 normal __memcpy_chk. */
12463 if (readonly_data_expr (src))
12465 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12466 if (!fn)
12467 return NULL_RTX;
12468 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12469 dest, src, len, size);
12470 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12471 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12472 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12475 return NULL_RTX;
12479 /* Emit warning if a buffer overflow is detected at compile time. */
12481 static void
12482 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12484 int is_strlen = 0;
12485 tree len, size;
12486 location_t loc = tree_nonartificial_location (exp);
12488 switch (fcode)
12490 case BUILT_IN_STRCPY_CHK:
12491 case BUILT_IN_STPCPY_CHK:
12492 /* For __strcat_chk the warning will be emitted only if overflowing
12493 by at least strlen (dest) + 1 bytes. */
12494 case BUILT_IN_STRCAT_CHK:
12495 len = CALL_EXPR_ARG (exp, 1);
12496 size = CALL_EXPR_ARG (exp, 2);
12497 is_strlen = 1;
12498 break;
12499 case BUILT_IN_STRNCAT_CHK:
12500 case BUILT_IN_STRNCPY_CHK:
12501 len = CALL_EXPR_ARG (exp, 2);
12502 size = CALL_EXPR_ARG (exp, 3);
12503 break;
12504 case BUILT_IN_SNPRINTF_CHK:
12505 case BUILT_IN_VSNPRINTF_CHK:
12506 len = CALL_EXPR_ARG (exp, 1);
12507 size = CALL_EXPR_ARG (exp, 3);
12508 break;
12509 default:
12510 gcc_unreachable ();
12513 if (!len || !size)
12514 return;
12516 if (! host_integerp (size, 1) || integer_all_onesp (size))
12517 return;
12519 if (is_strlen)
12521 len = c_strlen (len, 1);
12522 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12523 return;
12525 else if (fcode == BUILT_IN_STRNCAT_CHK)
12527 tree src = CALL_EXPR_ARG (exp, 1);
12528 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12529 return;
12530 src = c_strlen (src, 1);
12531 if (! src || ! host_integerp (src, 1))
12533 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12534 exp, get_callee_fndecl (exp));
12535 return;
12537 else if (tree_int_cst_lt (src, size))
12538 return;
12540 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12541 return;
12543 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12544 exp, get_callee_fndecl (exp));
12547 /* Emit warning if a buffer overflow is detected at compile time
12548 in __sprintf_chk/__vsprintf_chk calls. */
12550 static void
12551 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12553 tree size, len, fmt;
12554 const char *fmt_str;
12555 int nargs = call_expr_nargs (exp);
12557 /* Verify the required arguments in the original call. */
12559 if (nargs < 4)
12560 return;
12561 size = CALL_EXPR_ARG (exp, 2);
12562 fmt = CALL_EXPR_ARG (exp, 3);
12564 if (! host_integerp (size, 1) || integer_all_onesp (size))
12565 return;
12567 /* Check whether the format is a literal string constant. */
12568 fmt_str = c_getstr (fmt);
12569 if (fmt_str == NULL)
12570 return;
12572 if (!init_target_chars ())
12573 return;
12575 /* If the format doesn't contain % args or %%, we know its size. */
12576 if (strchr (fmt_str, target_percent) == 0)
12577 len = build_int_cstu (size_type_node, strlen (fmt_str));
12578 /* If the format is "%s" and first ... argument is a string literal,
12579 we know it too. */
12580 else if (fcode == BUILT_IN_SPRINTF_CHK
12581 && strcmp (fmt_str, target_percent_s) == 0)
12583 tree arg;
12585 if (nargs < 5)
12586 return;
12587 arg = CALL_EXPR_ARG (exp, 4);
12588 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12589 return;
12591 len = c_strlen (arg, 1);
12592 if (!len || ! host_integerp (len, 1))
12593 return;
12595 else
12596 return;
12598 if (! tree_int_cst_lt (len, size))
12599 warning_at (tree_nonartificial_location (exp),
12600 0, "%Kcall to %D will always overflow destination buffer",
12601 exp, get_callee_fndecl (exp));
12604 /* Emit warning if a free is called with address of a variable. */
12606 static void
12607 maybe_emit_free_warning (tree exp)
12609 tree arg = CALL_EXPR_ARG (exp, 0);
12611 STRIP_NOPS (arg);
12612 if (TREE_CODE (arg) != ADDR_EXPR)
12613 return;
12615 arg = get_base_address (TREE_OPERAND (arg, 0));
12616 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12617 return;
12619 if (SSA_VAR_P (arg))
12620 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12621 "%Kattempt to free a non-heap object %qD", exp, arg);
12622 else
12623 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12624 "%Kattempt to free a non-heap object", exp);
12627 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12628 if possible. */
12630 tree
12631 fold_builtin_object_size (tree ptr, tree ost)
12633 unsigned HOST_WIDE_INT bytes;
12634 int object_size_type;
12636 if (!validate_arg (ptr, POINTER_TYPE)
12637 || !validate_arg (ost, INTEGER_TYPE))
12638 return NULL_TREE;
12640 STRIP_NOPS (ost);
12642 if (TREE_CODE (ost) != INTEGER_CST
12643 || tree_int_cst_sgn (ost) < 0
12644 || compare_tree_int (ost, 3) > 0)
12645 return NULL_TREE;
12647 object_size_type = tree_low_cst (ost, 0);
12649 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12650 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12651 and (size_t) 0 for types 2 and 3. */
12652 if (TREE_SIDE_EFFECTS (ptr))
12653 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12655 if (TREE_CODE (ptr) == ADDR_EXPR)
12657 bytes = compute_builtin_object_size (ptr, object_size_type);
12658 if (double_int_fits_to_tree_p (size_type_node,
12659 uhwi_to_double_int (bytes)))
12660 return build_int_cstu (size_type_node, bytes);
12662 else if (TREE_CODE (ptr) == SSA_NAME)
12664 /* If object size is not known yet, delay folding until
12665 later. Maybe subsequent passes will help determining
12666 it. */
12667 bytes = compute_builtin_object_size (ptr, object_size_type);
12668 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12669 && double_int_fits_to_tree_p (size_type_node,
12670 uhwi_to_double_int (bytes)))
12671 return build_int_cstu (size_type_node, bytes);
12674 return NULL_TREE;
12677 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12678 DEST, SRC, LEN, and SIZE are the arguments to the call.
12679 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12680 code of the builtin. If MAXLEN is not NULL, it is maximum length
12681 passed as third argument. */
12683 tree
12684 fold_builtin_memory_chk (location_t loc, tree fndecl,
12685 tree dest, tree src, tree len, tree size,
12686 tree maxlen, bool ignore,
12687 enum built_in_function fcode)
12689 tree fn;
12691 if (!validate_arg (dest, POINTER_TYPE)
12692 || !validate_arg (src,
12693 (fcode == BUILT_IN_MEMSET_CHK
12694 ? INTEGER_TYPE : POINTER_TYPE))
12695 || !validate_arg (len, INTEGER_TYPE)
12696 || !validate_arg (size, INTEGER_TYPE))
12697 return NULL_TREE;
12699 /* If SRC and DEST are the same (and not volatile), return DEST
12700 (resp. DEST+LEN for __mempcpy_chk). */
12701 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12703 if (fcode != BUILT_IN_MEMPCPY_CHK)
12704 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12705 dest, len);
12706 else
12708 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12709 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12713 if (! host_integerp (size, 1))
12714 return NULL_TREE;
12716 if (! integer_all_onesp (size))
12718 if (! host_integerp (len, 1))
12720 /* If LEN is not constant, try MAXLEN too.
12721 For MAXLEN only allow optimizing into non-_ocs function
12722 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12723 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12725 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12727 /* (void) __mempcpy_chk () can be optimized into
12728 (void) __memcpy_chk (). */
12729 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12730 if (!fn)
12731 return NULL_TREE;
12733 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12735 return NULL_TREE;
12738 else
12739 maxlen = len;
12741 if (tree_int_cst_lt (size, maxlen))
12742 return NULL_TREE;
12745 fn = NULL_TREE;
12746 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12747 mem{cpy,pcpy,move,set} is available. */
12748 switch (fcode)
12750 case BUILT_IN_MEMCPY_CHK:
12751 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12752 break;
12753 case BUILT_IN_MEMPCPY_CHK:
12754 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12755 break;
12756 case BUILT_IN_MEMMOVE_CHK:
12757 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12758 break;
12759 case BUILT_IN_MEMSET_CHK:
12760 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12761 break;
12762 default:
12763 break;
12766 if (!fn)
12767 return NULL_TREE;
12769 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12772 /* Fold a call to the __st[rp]cpy_chk builtin.
12773 DEST, SRC, and SIZE are the arguments to the call.
12774 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12775 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12776 strings passed as second argument. */
12778 tree
12779 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12780 tree src, tree size,
12781 tree maxlen, bool ignore,
12782 enum built_in_function fcode)
12784 tree len, fn;
12786 if (!validate_arg (dest, POINTER_TYPE)
12787 || !validate_arg (src, POINTER_TYPE)
12788 || !validate_arg (size, INTEGER_TYPE))
12789 return NULL_TREE;
12791 /* If SRC and DEST are the same (and not volatile), return DEST. */
12792 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12793 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12795 if (! host_integerp (size, 1))
12796 return NULL_TREE;
12798 if (! integer_all_onesp (size))
12800 len = c_strlen (src, 1);
12801 if (! len || ! host_integerp (len, 1))
12803 /* If LEN is not constant, try MAXLEN too.
12804 For MAXLEN only allow optimizing into non-_ocs function
12805 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12806 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12808 if (fcode == BUILT_IN_STPCPY_CHK)
12810 if (! ignore)
12811 return NULL_TREE;
12813 /* If return value of __stpcpy_chk is ignored,
12814 optimize into __strcpy_chk. */
12815 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12816 if (!fn)
12817 return NULL_TREE;
12819 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12822 if (! len || TREE_SIDE_EFFECTS (len))
12823 return NULL_TREE;
12825 /* If c_strlen returned something, but not a constant,
12826 transform __strcpy_chk into __memcpy_chk. */
12827 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12828 if (!fn)
12829 return NULL_TREE;
12831 len = fold_convert_loc (loc, size_type_node, len);
12832 len = size_binop_loc (loc, PLUS_EXPR, len,
12833 build_int_cst (size_type_node, 1));
12834 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12835 build_call_expr_loc (loc, fn, 4,
12836 dest, src, len, size));
12839 else
12840 maxlen = len;
12842 if (! tree_int_cst_lt (maxlen, size))
12843 return NULL_TREE;
12846 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12847 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12848 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12849 if (!fn)
12850 return NULL_TREE;
12852 return build_call_expr_loc (loc, fn, 2, dest, src);
12855 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12856 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12857 length passed as third argument. */
12859 tree
12860 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12861 tree len, tree size, tree maxlen)
12863 tree fn;
12865 if (!validate_arg (dest, POINTER_TYPE)
12866 || !validate_arg (src, POINTER_TYPE)
12867 || !validate_arg (len, INTEGER_TYPE)
12868 || !validate_arg (size, INTEGER_TYPE))
12869 return NULL_TREE;
12871 if (! host_integerp (size, 1))
12872 return NULL_TREE;
12874 if (! integer_all_onesp (size))
12876 if (! host_integerp (len, 1))
12878 /* If LEN is not constant, try MAXLEN too.
12879 For MAXLEN only allow optimizing into non-_ocs function
12880 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12881 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12882 return NULL_TREE;
12884 else
12885 maxlen = len;
12887 if (tree_int_cst_lt (size, maxlen))
12888 return NULL_TREE;
12891 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12892 fn = builtin_decl_explicit (BUILT_IN_STRNCPY);
12893 if (!fn)
12894 return NULL_TREE;
12896 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12899 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12900 are the arguments to the call. */
12902 static tree
12903 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12904 tree src, tree size)
12906 tree fn;
12907 const char *p;
12909 if (!validate_arg (dest, POINTER_TYPE)
12910 || !validate_arg (src, POINTER_TYPE)
12911 || !validate_arg (size, INTEGER_TYPE))
12912 return NULL_TREE;
12914 p = c_getstr (src);
12915 /* If the SRC parameter is "", return DEST. */
12916 if (p && *p == '\0')
12917 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12919 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12920 return NULL_TREE;
12922 /* If __builtin_strcat_chk is used, assume strcat is available. */
12923 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12924 if (!fn)
12925 return NULL_TREE;
12927 return build_call_expr_loc (loc, fn, 2, dest, src);
12930 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12931 LEN, and SIZE. */
12933 static tree
12934 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12935 tree dest, tree src, tree len, tree size)
12937 tree fn;
12938 const char *p;
12940 if (!validate_arg (dest, POINTER_TYPE)
12941 || !validate_arg (src, POINTER_TYPE)
12942 || !validate_arg (size, INTEGER_TYPE)
12943 || !validate_arg (size, INTEGER_TYPE))
12944 return NULL_TREE;
12946 p = c_getstr (src);
12947 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12948 if (p && *p == '\0')
12949 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12950 else if (integer_zerop (len))
12951 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12953 if (! host_integerp (size, 1))
12954 return NULL_TREE;
12956 if (! integer_all_onesp (size))
12958 tree src_len = c_strlen (src, 1);
12959 if (src_len
12960 && host_integerp (src_len, 1)
12961 && host_integerp (len, 1)
12962 && ! tree_int_cst_lt (len, src_len))
12964 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12965 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12966 if (!fn)
12967 return NULL_TREE;
12969 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12971 return NULL_TREE;
12974 /* If __builtin_strncat_chk is used, assume strncat is available. */
12975 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
12976 if (!fn)
12977 return NULL_TREE;
12979 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12982 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12983 Return NULL_TREE if a normal call should be emitted rather than
12984 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12985 or BUILT_IN_VSPRINTF_CHK. */
12987 static tree
12988 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12989 enum built_in_function fcode)
12991 tree dest, size, len, fn, fmt, flag;
12992 const char *fmt_str;
12994 /* Verify the required arguments in the original call. */
12995 if (nargs < 4)
12996 return NULL_TREE;
12997 dest = args[0];
12998 if (!validate_arg (dest, POINTER_TYPE))
12999 return NULL_TREE;
13000 flag = args[1];
13001 if (!validate_arg (flag, INTEGER_TYPE))
13002 return NULL_TREE;
13003 size = args[2];
13004 if (!validate_arg (size, INTEGER_TYPE))
13005 return NULL_TREE;
13006 fmt = args[3];
13007 if (!validate_arg (fmt, POINTER_TYPE))
13008 return NULL_TREE;
13010 if (! host_integerp (size, 1))
13011 return NULL_TREE;
13013 len = NULL_TREE;
13015 if (!init_target_chars ())
13016 return NULL_TREE;
13018 /* Check whether the format is a literal string constant. */
13019 fmt_str = c_getstr (fmt);
13020 if (fmt_str != NULL)
13022 /* If the format doesn't contain % args or %%, we know the size. */
13023 if (strchr (fmt_str, target_percent) == 0)
13025 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13026 len = build_int_cstu (size_type_node, strlen (fmt_str));
13028 /* If the format is "%s" and first ... argument is a string literal,
13029 we know the size too. */
13030 else if (fcode == BUILT_IN_SPRINTF_CHK
13031 && strcmp (fmt_str, target_percent_s) == 0)
13033 tree arg;
13035 if (nargs == 5)
13037 arg = args[4];
13038 if (validate_arg (arg, POINTER_TYPE))
13040 len = c_strlen (arg, 1);
13041 if (! len || ! host_integerp (len, 1))
13042 len = NULL_TREE;
13048 if (! integer_all_onesp (size))
13050 if (! len || ! tree_int_cst_lt (len, size))
13051 return NULL_TREE;
13054 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13055 or if format doesn't contain % chars or is "%s". */
13056 if (! integer_zerop (flag))
13058 if (fmt_str == NULL)
13059 return NULL_TREE;
13060 if (strchr (fmt_str, target_percent) != NULL
13061 && strcmp (fmt_str, target_percent_s))
13062 return NULL_TREE;
13065 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13066 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13067 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13068 if (!fn)
13069 return NULL_TREE;
13071 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13074 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13075 a normal call should be emitted rather than expanding the function
13076 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13078 static tree
13079 fold_builtin_sprintf_chk (location_t loc, tree exp,
13080 enum built_in_function fcode)
13082 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13083 CALL_EXPR_ARGP (exp), fcode);
13086 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13087 NULL_TREE if a normal call should be emitted rather than expanding
13088 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13089 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13090 passed as second argument. */
13092 static tree
13093 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13094 tree maxlen, enum built_in_function fcode)
13096 tree dest, size, len, fn, fmt, flag;
13097 const char *fmt_str;
13099 /* Verify the required arguments in the original call. */
13100 if (nargs < 5)
13101 return NULL_TREE;
13102 dest = args[0];
13103 if (!validate_arg (dest, POINTER_TYPE))
13104 return NULL_TREE;
13105 len = args[1];
13106 if (!validate_arg (len, INTEGER_TYPE))
13107 return NULL_TREE;
13108 flag = args[2];
13109 if (!validate_arg (flag, INTEGER_TYPE))
13110 return NULL_TREE;
13111 size = args[3];
13112 if (!validate_arg (size, INTEGER_TYPE))
13113 return NULL_TREE;
13114 fmt = args[4];
13115 if (!validate_arg (fmt, POINTER_TYPE))
13116 return NULL_TREE;
13118 if (! host_integerp (size, 1))
13119 return NULL_TREE;
13121 if (! integer_all_onesp (size))
13123 if (! host_integerp (len, 1))
13125 /* If LEN is not constant, try MAXLEN too.
13126 For MAXLEN only allow optimizing into non-_ocs function
13127 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13128 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13129 return NULL_TREE;
13131 else
13132 maxlen = len;
13134 if (tree_int_cst_lt (size, maxlen))
13135 return NULL_TREE;
13138 if (!init_target_chars ())
13139 return NULL_TREE;
13141 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13142 or if format doesn't contain % chars or is "%s". */
13143 if (! integer_zerop (flag))
13145 fmt_str = c_getstr (fmt);
13146 if (fmt_str == NULL)
13147 return NULL_TREE;
13148 if (strchr (fmt_str, target_percent) != NULL
13149 && strcmp (fmt_str, target_percent_s))
13150 return NULL_TREE;
13153 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13154 available. */
13155 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13156 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13157 if (!fn)
13158 return NULL_TREE;
13160 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13163 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13164 a normal call should be emitted rather than expanding the function
13165 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13166 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13167 passed as second argument. */
13169 tree
13170 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13171 enum built_in_function fcode)
13173 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13174 CALL_EXPR_ARGP (exp), maxlen, fcode);
13177 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13178 FMT and ARG are the arguments to the call; we don't fold cases with
13179 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13181 Return NULL_TREE if no simplification was possible, otherwise return the
13182 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13183 code of the function to be simplified. */
13185 static tree
13186 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13187 tree arg, bool ignore,
13188 enum built_in_function fcode)
13190 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13191 const char *fmt_str = NULL;
13193 /* If the return value is used, don't do the transformation. */
13194 if (! ignore)
13195 return NULL_TREE;
13197 /* Verify the required arguments in the original call. */
13198 if (!validate_arg (fmt, POINTER_TYPE))
13199 return NULL_TREE;
13201 /* Check whether the format is a literal string constant. */
13202 fmt_str = c_getstr (fmt);
13203 if (fmt_str == NULL)
13204 return NULL_TREE;
13206 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13208 /* If we're using an unlocked function, assume the other
13209 unlocked functions exist explicitly. */
13210 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13211 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13213 else
13215 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13216 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13219 if (!init_target_chars ())
13220 return NULL_TREE;
13222 if (strcmp (fmt_str, target_percent_s) == 0
13223 || strchr (fmt_str, target_percent) == NULL)
13225 const char *str;
13227 if (strcmp (fmt_str, target_percent_s) == 0)
13229 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13230 return NULL_TREE;
13232 if (!arg || !validate_arg (arg, POINTER_TYPE))
13233 return NULL_TREE;
13235 str = c_getstr (arg);
13236 if (str == NULL)
13237 return NULL_TREE;
13239 else
13241 /* The format specifier doesn't contain any '%' characters. */
13242 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13243 && arg)
13244 return NULL_TREE;
13245 str = fmt_str;
13248 /* If the string was "", printf does nothing. */
13249 if (str[0] == '\0')
13250 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13252 /* If the string has length of 1, call putchar. */
13253 if (str[1] == '\0')
13255 /* Given printf("c"), (where c is any one character,)
13256 convert "c"[0] to an int and pass that to the replacement
13257 function. */
13258 newarg = build_int_cst (integer_type_node, str[0]);
13259 if (fn_putchar)
13260 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13262 else
13264 /* If the string was "string\n", call puts("string"). */
13265 size_t len = strlen (str);
13266 if ((unsigned char)str[len - 1] == target_newline
13267 && (size_t) (int) len == len
13268 && (int) len > 0)
13270 char *newstr;
13271 tree offset_node, string_cst;
13273 /* Create a NUL-terminated string that's one char shorter
13274 than the original, stripping off the trailing '\n'. */
13275 newarg = build_string_literal (len, str);
13276 string_cst = string_constant (newarg, &offset_node);
13277 gcc_checking_assert (string_cst
13278 && (TREE_STRING_LENGTH (string_cst)
13279 == (int) len)
13280 && integer_zerop (offset_node)
13281 && (unsigned char)
13282 TREE_STRING_POINTER (string_cst)[len - 1]
13283 == target_newline);
13284 /* build_string_literal creates a new STRING_CST,
13285 modify it in place to avoid double copying. */
13286 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13287 newstr[len - 1] = '\0';
13288 if (fn_puts)
13289 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13291 else
13292 /* We'd like to arrange to call fputs(string,stdout) here,
13293 but we need stdout and don't have a way to get it yet. */
13294 return NULL_TREE;
13298 /* The other optimizations can be done only on the non-va_list variants. */
13299 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13300 return NULL_TREE;
13302 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13303 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13305 if (!arg || !validate_arg (arg, POINTER_TYPE))
13306 return NULL_TREE;
13307 if (fn_puts)
13308 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13311 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13312 else if (strcmp (fmt_str, target_percent_c) == 0)
13314 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13315 return NULL_TREE;
13316 if (fn_putchar)
13317 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13320 if (!call)
13321 return NULL_TREE;
13323 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13326 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13327 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13328 more than 3 arguments, and ARG may be null in the 2-argument case.
13330 Return NULL_TREE if no simplification was possible, otherwise return the
13331 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13332 code of the function to be simplified. */
13334 static tree
13335 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13336 tree fmt, tree arg, bool ignore,
13337 enum built_in_function fcode)
13339 tree fn_fputc, fn_fputs, call = NULL_TREE;
13340 const char *fmt_str = NULL;
13342 /* If the return value is used, don't do the transformation. */
13343 if (! ignore)
13344 return NULL_TREE;
13346 /* Verify the required arguments in the original call. */
13347 if (!validate_arg (fp, POINTER_TYPE))
13348 return NULL_TREE;
13349 if (!validate_arg (fmt, POINTER_TYPE))
13350 return NULL_TREE;
13352 /* Check whether the format is a literal string constant. */
13353 fmt_str = c_getstr (fmt);
13354 if (fmt_str == NULL)
13355 return NULL_TREE;
13357 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13359 /* If we're using an unlocked function, assume the other
13360 unlocked functions exist explicitly. */
13361 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13362 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13364 else
13366 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13367 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13370 if (!init_target_chars ())
13371 return NULL_TREE;
13373 /* If the format doesn't contain % args or %%, use strcpy. */
13374 if (strchr (fmt_str, target_percent) == NULL)
13376 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13377 && arg)
13378 return NULL_TREE;
13380 /* If the format specifier was "", fprintf does nothing. */
13381 if (fmt_str[0] == '\0')
13383 /* If FP has side-effects, just wait until gimplification is
13384 done. */
13385 if (TREE_SIDE_EFFECTS (fp))
13386 return NULL_TREE;
13388 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13391 /* When "string" doesn't contain %, replace all cases of
13392 fprintf (fp, string) with fputs (string, fp). The fputs
13393 builtin will take care of special cases like length == 1. */
13394 if (fn_fputs)
13395 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13398 /* The other optimizations can be done only on the non-va_list variants. */
13399 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13400 return NULL_TREE;
13402 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13403 else if (strcmp (fmt_str, target_percent_s) == 0)
13405 if (!arg || !validate_arg (arg, POINTER_TYPE))
13406 return NULL_TREE;
13407 if (fn_fputs)
13408 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13411 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13412 else if (strcmp (fmt_str, target_percent_c) == 0)
13414 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13415 return NULL_TREE;
13416 if (fn_fputc)
13417 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13420 if (!call)
13421 return NULL_TREE;
13422 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13425 /* Initialize format string characters in the target charset. */
13427 static bool
13428 init_target_chars (void)
13430 static bool init;
13431 if (!init)
13433 target_newline = lang_hooks.to_target_charset ('\n');
13434 target_percent = lang_hooks.to_target_charset ('%');
13435 target_c = lang_hooks.to_target_charset ('c');
13436 target_s = lang_hooks.to_target_charset ('s');
13437 if (target_newline == 0 || target_percent == 0 || target_c == 0
13438 || target_s == 0)
13439 return false;
13441 target_percent_c[0] = target_percent;
13442 target_percent_c[1] = target_c;
13443 target_percent_c[2] = '\0';
13445 target_percent_s[0] = target_percent;
13446 target_percent_s[1] = target_s;
13447 target_percent_s[2] = '\0';
13449 target_percent_s_newline[0] = target_percent;
13450 target_percent_s_newline[1] = target_s;
13451 target_percent_s_newline[2] = target_newline;
13452 target_percent_s_newline[3] = '\0';
13454 init = true;
13456 return true;
13459 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13460 and no overflow/underflow occurred. INEXACT is true if M was not
13461 exactly calculated. TYPE is the tree type for the result. This
13462 function assumes that you cleared the MPFR flags and then
13463 calculated M to see if anything subsequently set a flag prior to
13464 entering this function. Return NULL_TREE if any checks fail. */
13466 static tree
13467 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13469 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13470 overflow/underflow occurred. If -frounding-math, proceed iff the
13471 result of calling FUNC was exact. */
13472 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13473 && (!flag_rounding_math || !inexact))
13475 REAL_VALUE_TYPE rr;
13477 real_from_mpfr (&rr, m, type, GMP_RNDN);
13478 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13479 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13480 but the mpft_t is not, then we underflowed in the
13481 conversion. */
13482 if (real_isfinite (&rr)
13483 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13485 REAL_VALUE_TYPE rmode;
13487 real_convert (&rmode, TYPE_MODE (type), &rr);
13488 /* Proceed iff the specified mode can hold the value. */
13489 if (real_identical (&rmode, &rr))
13490 return build_real (type, rmode);
13493 return NULL_TREE;
13496 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13497 number and no overflow/underflow occurred. INEXACT is true if M
13498 was not exactly calculated. TYPE is the tree type for the result.
13499 This function assumes that you cleared the MPFR flags and then
13500 calculated M to see if anything subsequently set a flag prior to
13501 entering this function. Return NULL_TREE if any checks fail, if
13502 FORCE_CONVERT is true, then bypass the checks. */
13504 static tree
13505 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13507 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13508 overflow/underflow occurred. If -frounding-math, proceed iff the
13509 result of calling FUNC was exact. */
13510 if (force_convert
13511 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13512 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13513 && (!flag_rounding_math || !inexact)))
13515 REAL_VALUE_TYPE re, im;
13517 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13518 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13519 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13520 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13521 but the mpft_t is not, then we underflowed in the
13522 conversion. */
13523 if (force_convert
13524 || (real_isfinite (&re) && real_isfinite (&im)
13525 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13526 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13528 REAL_VALUE_TYPE re_mode, im_mode;
13530 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13531 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13532 /* Proceed iff the specified mode can hold the value. */
13533 if (force_convert
13534 || (real_identical (&re_mode, &re)
13535 && real_identical (&im_mode, &im)))
13536 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13537 build_real (TREE_TYPE (type), im_mode));
13540 return NULL_TREE;
13543 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13544 FUNC on it and return the resulting value as a tree with type TYPE.
13545 If MIN and/or MAX are not NULL, then the supplied ARG must be
13546 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13547 acceptable values, otherwise they are not. The mpfr precision is
13548 set to the precision of TYPE. We assume that function FUNC returns
13549 zero if the result could be calculated exactly within the requested
13550 precision. */
13552 static tree
13553 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13554 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13555 bool inclusive)
13557 tree result = NULL_TREE;
13559 STRIP_NOPS (arg);
13561 /* To proceed, MPFR must exactly represent the target floating point
13562 format, which only happens when the target base equals two. */
13563 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13564 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13566 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13568 if (real_isfinite (ra)
13569 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13570 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13572 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13573 const int prec = fmt->p;
13574 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13575 int inexact;
13576 mpfr_t m;
13578 mpfr_init2 (m, prec);
13579 mpfr_from_real (m, ra, GMP_RNDN);
13580 mpfr_clear_flags ();
13581 inexact = func (m, m, rnd);
13582 result = do_mpfr_ckconv (m, type, inexact);
13583 mpfr_clear (m);
13587 return result;
13590 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13591 FUNC on it and return the resulting value as a tree with type TYPE.
13592 The mpfr precision is set to the precision of TYPE. We assume that
13593 function FUNC returns zero if the result could be calculated
13594 exactly within the requested precision. */
13596 static tree
13597 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13598 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13600 tree result = NULL_TREE;
13602 STRIP_NOPS (arg1);
13603 STRIP_NOPS (arg2);
13605 /* To proceed, MPFR must exactly represent the target floating point
13606 format, which only happens when the target base equals two. */
13607 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13608 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13609 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13611 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13612 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13614 if (real_isfinite (ra1) && real_isfinite (ra2))
13616 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13617 const int prec = fmt->p;
13618 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13619 int inexact;
13620 mpfr_t m1, m2;
13622 mpfr_inits2 (prec, m1, m2, NULL);
13623 mpfr_from_real (m1, ra1, GMP_RNDN);
13624 mpfr_from_real (m2, ra2, GMP_RNDN);
13625 mpfr_clear_flags ();
13626 inexact = func (m1, m1, m2, rnd);
13627 result = do_mpfr_ckconv (m1, type, inexact);
13628 mpfr_clears (m1, m2, NULL);
13632 return result;
13635 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13636 FUNC on it and return the resulting value as a tree with type TYPE.
13637 The mpfr precision is set to the precision of TYPE. We assume that
13638 function FUNC returns zero if the result could be calculated
13639 exactly within the requested precision. */
13641 static tree
13642 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13643 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13645 tree result = NULL_TREE;
13647 STRIP_NOPS (arg1);
13648 STRIP_NOPS (arg2);
13649 STRIP_NOPS (arg3);
13651 /* To proceed, MPFR must exactly represent the target floating point
13652 format, which only happens when the target base equals two. */
13653 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13654 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13655 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13656 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13658 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13659 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13660 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13662 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13664 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13665 const int prec = fmt->p;
13666 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13667 int inexact;
13668 mpfr_t m1, m2, m3;
13670 mpfr_inits2 (prec, m1, m2, m3, NULL);
13671 mpfr_from_real (m1, ra1, GMP_RNDN);
13672 mpfr_from_real (m2, ra2, GMP_RNDN);
13673 mpfr_from_real (m3, ra3, GMP_RNDN);
13674 mpfr_clear_flags ();
13675 inexact = func (m1, m1, m2, m3, rnd);
13676 result = do_mpfr_ckconv (m1, type, inexact);
13677 mpfr_clears (m1, m2, m3, NULL);
13681 return result;
13684 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13685 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13686 If ARG_SINP and ARG_COSP are NULL then the result is returned
13687 as a complex value.
13688 The type is taken from the type of ARG and is used for setting the
13689 precision of the calculation and results. */
13691 static tree
13692 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13694 tree const type = TREE_TYPE (arg);
13695 tree result = NULL_TREE;
13697 STRIP_NOPS (arg);
13699 /* To proceed, MPFR must exactly represent the target floating point
13700 format, which only happens when the target base equals two. */
13701 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13702 && TREE_CODE (arg) == REAL_CST
13703 && !TREE_OVERFLOW (arg))
13705 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13707 if (real_isfinite (ra))
13709 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13710 const int prec = fmt->p;
13711 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13712 tree result_s, result_c;
13713 int inexact;
13714 mpfr_t m, ms, mc;
13716 mpfr_inits2 (prec, m, ms, mc, NULL);
13717 mpfr_from_real (m, ra, GMP_RNDN);
13718 mpfr_clear_flags ();
13719 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13720 result_s = do_mpfr_ckconv (ms, type, inexact);
13721 result_c = do_mpfr_ckconv (mc, type, inexact);
13722 mpfr_clears (m, ms, mc, NULL);
13723 if (result_s && result_c)
13725 /* If we are to return in a complex value do so. */
13726 if (!arg_sinp && !arg_cosp)
13727 return build_complex (build_complex_type (type),
13728 result_c, result_s);
13730 /* Dereference the sin/cos pointer arguments. */
13731 arg_sinp = build_fold_indirect_ref (arg_sinp);
13732 arg_cosp = build_fold_indirect_ref (arg_cosp);
13733 /* Proceed if valid pointer type were passed in. */
13734 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13735 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13737 /* Set the values. */
13738 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13739 result_s);
13740 TREE_SIDE_EFFECTS (result_s) = 1;
13741 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13742 result_c);
13743 TREE_SIDE_EFFECTS (result_c) = 1;
13744 /* Combine the assignments into a compound expr. */
13745 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13746 result_s, result_c));
13751 return result;
13754 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13755 two-argument mpfr order N Bessel function FUNC on them and return
13756 the resulting value as a tree with type TYPE. The mpfr precision
13757 is set to the precision of TYPE. We assume that function FUNC
13758 returns zero if the result could be calculated exactly within the
13759 requested precision. */
13760 static tree
13761 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13762 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13763 const REAL_VALUE_TYPE *min, bool inclusive)
13765 tree result = NULL_TREE;
13767 STRIP_NOPS (arg1);
13768 STRIP_NOPS (arg2);
13770 /* To proceed, MPFR must exactly represent the target floating point
13771 format, which only happens when the target base equals two. */
13772 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13773 && host_integerp (arg1, 0)
13774 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13776 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13777 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13779 if (n == (long)n
13780 && real_isfinite (ra)
13781 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13783 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13784 const int prec = fmt->p;
13785 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13786 int inexact;
13787 mpfr_t m;
13789 mpfr_init2 (m, prec);
13790 mpfr_from_real (m, ra, GMP_RNDN);
13791 mpfr_clear_flags ();
13792 inexact = func (m, n, m, rnd);
13793 result = do_mpfr_ckconv (m, type, inexact);
13794 mpfr_clear (m);
13798 return result;
13801 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13802 the pointer *(ARG_QUO) and return the result. The type is taken
13803 from the type of ARG0 and is used for setting the precision of the
13804 calculation and results. */
13806 static tree
13807 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13809 tree const type = TREE_TYPE (arg0);
13810 tree result = NULL_TREE;
13812 STRIP_NOPS (arg0);
13813 STRIP_NOPS (arg1);
13815 /* To proceed, MPFR must exactly represent the target floating point
13816 format, which only happens when the target base equals two. */
13817 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13818 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13819 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13821 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13822 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13824 if (real_isfinite (ra0) && real_isfinite (ra1))
13826 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13827 const int prec = fmt->p;
13828 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13829 tree result_rem;
13830 long integer_quo;
13831 mpfr_t m0, m1;
13833 mpfr_inits2 (prec, m0, m1, NULL);
13834 mpfr_from_real (m0, ra0, GMP_RNDN);
13835 mpfr_from_real (m1, ra1, GMP_RNDN);
13836 mpfr_clear_flags ();
13837 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13838 /* Remquo is independent of the rounding mode, so pass
13839 inexact=0 to do_mpfr_ckconv(). */
13840 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13841 mpfr_clears (m0, m1, NULL);
13842 if (result_rem)
13844 /* MPFR calculates quo in the host's long so it may
13845 return more bits in quo than the target int can hold
13846 if sizeof(host long) > sizeof(target int). This can
13847 happen even for native compilers in LP64 mode. In
13848 these cases, modulo the quo value with the largest
13849 number that the target int can hold while leaving one
13850 bit for the sign. */
13851 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13852 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13854 /* Dereference the quo pointer argument. */
13855 arg_quo = build_fold_indirect_ref (arg_quo);
13856 /* Proceed iff a valid pointer type was passed in. */
13857 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13859 /* Set the value. */
13860 tree result_quo
13861 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13862 build_int_cst (TREE_TYPE (arg_quo),
13863 integer_quo));
13864 TREE_SIDE_EFFECTS (result_quo) = 1;
13865 /* Combine the quo assignment with the rem. */
13866 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13867 result_quo, result_rem));
13872 return result;
13875 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13876 resulting value as a tree with type TYPE. The mpfr precision is
13877 set to the precision of TYPE. We assume that this mpfr function
13878 returns zero if the result could be calculated exactly within the
13879 requested precision. In addition, the integer pointer represented
13880 by ARG_SG will be dereferenced and set to the appropriate signgam
13881 (-1,1) value. */
13883 static tree
13884 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13886 tree result = NULL_TREE;
13888 STRIP_NOPS (arg);
13890 /* To proceed, MPFR must exactly represent the target floating point
13891 format, which only happens when the target base equals two. Also
13892 verify ARG is a constant and that ARG_SG is an int pointer. */
13893 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13894 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13895 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13896 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13898 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13900 /* In addition to NaN and Inf, the argument cannot be zero or a
13901 negative integer. */
13902 if (real_isfinite (ra)
13903 && ra->cl != rvc_zero
13904 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13906 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13907 const int prec = fmt->p;
13908 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13909 int inexact, sg;
13910 mpfr_t m;
13911 tree result_lg;
13913 mpfr_init2 (m, prec);
13914 mpfr_from_real (m, ra, GMP_RNDN);
13915 mpfr_clear_flags ();
13916 inexact = mpfr_lgamma (m, &sg, m, rnd);
13917 result_lg = do_mpfr_ckconv (m, type, inexact);
13918 mpfr_clear (m);
13919 if (result_lg)
13921 tree result_sg;
13923 /* Dereference the arg_sg pointer argument. */
13924 arg_sg = build_fold_indirect_ref (arg_sg);
13925 /* Assign the signgam value into *arg_sg. */
13926 result_sg = fold_build2 (MODIFY_EXPR,
13927 TREE_TYPE (arg_sg), arg_sg,
13928 build_int_cst (TREE_TYPE (arg_sg), sg));
13929 TREE_SIDE_EFFECTS (result_sg) = 1;
13930 /* Combine the signgam assignment with the lgamma result. */
13931 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13932 result_sg, result_lg));
13937 return result;
13940 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13941 function FUNC on it and return the resulting value as a tree with
13942 type TYPE. The mpfr precision is set to the precision of TYPE. We
13943 assume that function FUNC returns zero if the result could be
13944 calculated exactly within the requested precision. */
13946 static tree
13947 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13949 tree result = NULL_TREE;
13951 STRIP_NOPS (arg);
13953 /* To proceed, MPFR must exactly represent the target floating point
13954 format, which only happens when the target base equals two. */
13955 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13956 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13957 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13959 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13960 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13962 if (real_isfinite (re) && real_isfinite (im))
13964 const struct real_format *const fmt =
13965 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13966 const int prec = fmt->p;
13967 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13968 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13969 int inexact;
13970 mpc_t m;
13972 mpc_init2 (m, prec);
13973 mpfr_from_real (mpc_realref(m), re, rnd);
13974 mpfr_from_real (mpc_imagref(m), im, rnd);
13975 mpfr_clear_flags ();
13976 inexact = func (m, m, crnd);
13977 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13978 mpc_clear (m);
13982 return result;
13985 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13986 mpc function FUNC on it and return the resulting value as a tree
13987 with type TYPE. The mpfr precision is set to the precision of
13988 TYPE. We assume that function FUNC returns zero if the result
13989 could be calculated exactly within the requested precision. If
13990 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13991 in the arguments and/or results. */
13993 tree
13994 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13995 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13997 tree result = NULL_TREE;
13999 STRIP_NOPS (arg0);
14000 STRIP_NOPS (arg1);
14002 /* To proceed, MPFR must exactly represent the target floating point
14003 format, which only happens when the target base equals two. */
14004 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14005 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14006 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14007 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14008 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14010 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14011 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14012 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14013 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14015 if (do_nonfinite
14016 || (real_isfinite (re0) && real_isfinite (im0)
14017 && real_isfinite (re1) && real_isfinite (im1)))
14019 const struct real_format *const fmt =
14020 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14021 const int prec = fmt->p;
14022 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14023 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14024 int inexact;
14025 mpc_t m0, m1;
14027 mpc_init2 (m0, prec);
14028 mpc_init2 (m1, prec);
14029 mpfr_from_real (mpc_realref(m0), re0, rnd);
14030 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14031 mpfr_from_real (mpc_realref(m1), re1, rnd);
14032 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14033 mpfr_clear_flags ();
14034 inexact = func (m0, m0, m1, crnd);
14035 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14036 mpc_clear (m0);
14037 mpc_clear (m1);
14041 return result;
14044 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14045 a normal call should be emitted rather than expanding the function
14046 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14048 static tree
14049 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14051 int nargs = gimple_call_num_args (stmt);
14053 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14054 (nargs > 0
14055 ? gimple_call_arg_ptr (stmt, 0)
14056 : &error_mark_node), fcode);
14059 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14060 a normal call should be emitted rather than expanding the function
14061 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14062 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14063 passed as second argument. */
14065 tree
14066 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14067 enum built_in_function fcode)
14069 int nargs = gimple_call_num_args (stmt);
14071 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14072 (nargs > 0
14073 ? gimple_call_arg_ptr (stmt, 0)
14074 : &error_mark_node), maxlen, fcode);
14077 /* Builtins with folding operations that operate on "..." arguments
14078 need special handling; we need to store the arguments in a convenient
14079 data structure before attempting any folding. Fortunately there are
14080 only a few builtins that fall into this category. FNDECL is the
14081 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14082 result of the function call is ignored. */
14084 static tree
14085 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14086 bool ignore ATTRIBUTE_UNUSED)
14088 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14089 tree ret = NULL_TREE;
14091 switch (fcode)
14093 case BUILT_IN_SPRINTF_CHK:
14094 case BUILT_IN_VSPRINTF_CHK:
14095 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14096 break;
14098 case BUILT_IN_SNPRINTF_CHK:
14099 case BUILT_IN_VSNPRINTF_CHK:
14100 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14102 default:
14103 break;
14105 if (ret)
14107 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14108 TREE_NO_WARNING (ret) = 1;
14109 return ret;
14111 return NULL_TREE;
14114 /* A wrapper function for builtin folding that prevents warnings for
14115 "statement without effect" and the like, caused by removing the
14116 call node earlier than the warning is generated. */
14118 tree
14119 fold_call_stmt (gimple stmt, bool ignore)
14121 tree ret = NULL_TREE;
14122 tree fndecl = gimple_call_fndecl (stmt);
14123 location_t loc = gimple_location (stmt);
14124 if (fndecl
14125 && TREE_CODE (fndecl) == FUNCTION_DECL
14126 && DECL_BUILT_IN (fndecl)
14127 && !gimple_call_va_arg_pack_p (stmt))
14129 int nargs = gimple_call_num_args (stmt);
14130 tree *args = (nargs > 0
14131 ? gimple_call_arg_ptr (stmt, 0)
14132 : &error_mark_node);
14134 if (avoid_folding_inline_builtin (fndecl))
14135 return NULL_TREE;
14136 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14138 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14140 else
14142 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14143 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14144 if (!ret)
14145 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14146 if (ret)
14148 /* Propagate location information from original call to
14149 expansion of builtin. Otherwise things like
14150 maybe_emit_chk_warning, that operate on the expansion
14151 of a builtin, will use the wrong location information. */
14152 if (gimple_has_location (stmt))
14154 tree realret = ret;
14155 if (TREE_CODE (ret) == NOP_EXPR)
14156 realret = TREE_OPERAND (ret, 0);
14157 if (CAN_HAVE_LOCATION_P (realret)
14158 && !EXPR_HAS_LOCATION (realret))
14159 SET_EXPR_LOCATION (realret, loc);
14160 return realret;
14162 return ret;
14166 return NULL_TREE;
14169 /* Look up the function in builtin_decl that corresponds to DECL
14170 and set ASMSPEC as its user assembler name. DECL must be a
14171 function decl that declares a builtin. */
14173 void
14174 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14176 tree builtin;
14177 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14178 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14179 && asmspec != 0);
14181 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14182 set_user_assembler_name (builtin, asmspec);
14183 switch (DECL_FUNCTION_CODE (decl))
14185 case BUILT_IN_MEMCPY:
14186 init_block_move_fn (asmspec);
14187 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14188 break;
14189 case BUILT_IN_MEMSET:
14190 init_block_clear_fn (asmspec);
14191 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14192 break;
14193 case BUILT_IN_MEMMOVE:
14194 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14195 break;
14196 case BUILT_IN_MEMCMP:
14197 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14198 break;
14199 case BUILT_IN_ABORT:
14200 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14201 break;
14202 case BUILT_IN_FFS:
14203 if (INT_TYPE_SIZE < BITS_PER_WORD)
14205 set_user_assembler_libfunc ("ffs", asmspec);
14206 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14207 MODE_INT, 0), "ffs");
14209 break;
14210 default:
14211 break;
14215 /* Return true if DECL is a builtin that expands to a constant or similarly
14216 simple code. */
14217 bool
14218 is_simple_builtin (tree decl)
14220 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14221 switch (DECL_FUNCTION_CODE (decl))
14223 /* Builtins that expand to constants. */
14224 case BUILT_IN_CONSTANT_P:
14225 case BUILT_IN_EXPECT:
14226 case BUILT_IN_OBJECT_SIZE:
14227 case BUILT_IN_UNREACHABLE:
14228 /* Simple register moves or loads from stack. */
14229 case BUILT_IN_ASSUME_ALIGNED:
14230 case BUILT_IN_RETURN_ADDRESS:
14231 case BUILT_IN_EXTRACT_RETURN_ADDR:
14232 case BUILT_IN_FROB_RETURN_ADDR:
14233 case BUILT_IN_RETURN:
14234 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14235 case BUILT_IN_FRAME_ADDRESS:
14236 case BUILT_IN_VA_END:
14237 case BUILT_IN_STACK_SAVE:
14238 case BUILT_IN_STACK_RESTORE:
14239 /* Exception state returns or moves registers around. */
14240 case BUILT_IN_EH_FILTER:
14241 case BUILT_IN_EH_POINTER:
14242 case BUILT_IN_EH_COPY_VALUES:
14243 return true;
14245 default:
14246 return false;
14249 return false;
14252 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14253 most probably expanded inline into reasonably simple code. This is a
14254 superset of is_simple_builtin. */
14255 bool
14256 is_inexpensive_builtin (tree decl)
14258 if (!decl)
14259 return false;
14260 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14261 return true;
14262 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14263 switch (DECL_FUNCTION_CODE (decl))
14265 case BUILT_IN_ABS:
14266 case BUILT_IN_ALLOCA:
14267 case BUILT_IN_ALLOCA_WITH_ALIGN:
14268 case BUILT_IN_BSWAP32:
14269 case BUILT_IN_BSWAP64:
14270 case BUILT_IN_CLZ:
14271 case BUILT_IN_CLZIMAX:
14272 case BUILT_IN_CLZL:
14273 case BUILT_IN_CLZLL:
14274 case BUILT_IN_CTZ:
14275 case BUILT_IN_CTZIMAX:
14276 case BUILT_IN_CTZL:
14277 case BUILT_IN_CTZLL:
14278 case BUILT_IN_FFS:
14279 case BUILT_IN_FFSIMAX:
14280 case BUILT_IN_FFSL:
14281 case BUILT_IN_FFSLL:
14282 case BUILT_IN_IMAXABS:
14283 case BUILT_IN_FINITE:
14284 case BUILT_IN_FINITEF:
14285 case BUILT_IN_FINITEL:
14286 case BUILT_IN_FINITED32:
14287 case BUILT_IN_FINITED64:
14288 case BUILT_IN_FINITED128:
14289 case BUILT_IN_FPCLASSIFY:
14290 case BUILT_IN_ISFINITE:
14291 case BUILT_IN_ISINF_SIGN:
14292 case BUILT_IN_ISINF:
14293 case BUILT_IN_ISINFF:
14294 case BUILT_IN_ISINFL:
14295 case BUILT_IN_ISINFD32:
14296 case BUILT_IN_ISINFD64:
14297 case BUILT_IN_ISINFD128:
14298 case BUILT_IN_ISNAN:
14299 case BUILT_IN_ISNANF:
14300 case BUILT_IN_ISNANL:
14301 case BUILT_IN_ISNAND32:
14302 case BUILT_IN_ISNAND64:
14303 case BUILT_IN_ISNAND128:
14304 case BUILT_IN_ISNORMAL:
14305 case BUILT_IN_ISGREATER:
14306 case BUILT_IN_ISGREATEREQUAL:
14307 case BUILT_IN_ISLESS:
14308 case BUILT_IN_ISLESSEQUAL:
14309 case BUILT_IN_ISLESSGREATER:
14310 case BUILT_IN_ISUNORDERED:
14311 case BUILT_IN_VA_ARG_PACK:
14312 case BUILT_IN_VA_ARG_PACK_LEN:
14313 case BUILT_IN_VA_COPY:
14314 case BUILT_IN_TRAP:
14315 case BUILT_IN_SAVEREGS:
14316 case BUILT_IN_POPCOUNTL:
14317 case BUILT_IN_POPCOUNTLL:
14318 case BUILT_IN_POPCOUNTIMAX:
14319 case BUILT_IN_POPCOUNT:
14320 case BUILT_IN_PARITYL:
14321 case BUILT_IN_PARITYLL:
14322 case BUILT_IN_PARITYIMAX:
14323 case BUILT_IN_PARITY:
14324 case BUILT_IN_LABS:
14325 case BUILT_IN_LLABS:
14326 case BUILT_IN_PREFETCH:
14327 return true;
14329 default:
14330 return is_simple_builtin (decl);
14333 return false;