PR target/51756
[official-gcc.git] / gcc / builtins.c
blob44b8551b99d96480839a3618adab74f48af7bd19
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_STPNCPY_CHK:
6775 case BUILT_IN_STRCAT_CHK:
6776 case BUILT_IN_STRNCAT_CHK:
6777 case BUILT_IN_SNPRINTF_CHK:
6778 case BUILT_IN_VSNPRINTF_CHK:
6779 maybe_emit_chk_warning (exp, fcode);
6780 break;
6782 case BUILT_IN_SPRINTF_CHK:
6783 case BUILT_IN_VSPRINTF_CHK:
6784 maybe_emit_sprintf_chk_warning (exp, fcode);
6785 break;
6787 case BUILT_IN_FREE:
6788 if (warn_free_nonheap_object)
6789 maybe_emit_free_warning (exp);
6790 break;
6792 default: /* just do library call, if unknown builtin */
6793 break;
6796 /* The switch statement above can drop through to cause the function
6797 to be called normally. */
6798 return expand_call (exp, target, ignore);
6801 /* Determine whether a tree node represents a call to a built-in
6802 function. If the tree T is a call to a built-in function with
6803 the right number of arguments of the appropriate types, return
6804 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6805 Otherwise the return value is END_BUILTINS. */
6807 enum built_in_function
6808 builtin_mathfn_code (const_tree t)
6810 const_tree fndecl, arg, parmlist;
6811 const_tree argtype, parmtype;
6812 const_call_expr_arg_iterator iter;
6814 if (TREE_CODE (t) != CALL_EXPR
6815 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6816 return END_BUILTINS;
6818 fndecl = get_callee_fndecl (t);
6819 if (fndecl == NULL_TREE
6820 || TREE_CODE (fndecl) != FUNCTION_DECL
6821 || ! DECL_BUILT_IN (fndecl)
6822 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6823 return END_BUILTINS;
6825 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6826 init_const_call_expr_arg_iterator (t, &iter);
6827 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6829 /* If a function doesn't take a variable number of arguments,
6830 the last element in the list will have type `void'. */
6831 parmtype = TREE_VALUE (parmlist);
6832 if (VOID_TYPE_P (parmtype))
6834 if (more_const_call_expr_args_p (&iter))
6835 return END_BUILTINS;
6836 return DECL_FUNCTION_CODE (fndecl);
6839 if (! more_const_call_expr_args_p (&iter))
6840 return END_BUILTINS;
6842 arg = next_const_call_expr_arg (&iter);
6843 argtype = TREE_TYPE (arg);
6845 if (SCALAR_FLOAT_TYPE_P (parmtype))
6847 if (! SCALAR_FLOAT_TYPE_P (argtype))
6848 return END_BUILTINS;
6850 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6852 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6853 return END_BUILTINS;
6855 else if (POINTER_TYPE_P (parmtype))
6857 if (! POINTER_TYPE_P (argtype))
6858 return END_BUILTINS;
6860 else if (INTEGRAL_TYPE_P (parmtype))
6862 if (! INTEGRAL_TYPE_P (argtype))
6863 return END_BUILTINS;
6865 else
6866 return END_BUILTINS;
6869 /* Variable-length argument list. */
6870 return DECL_FUNCTION_CODE (fndecl);
6873 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6874 evaluate to a constant. */
6876 static tree
6877 fold_builtin_constant_p (tree arg)
6879 /* We return 1 for a numeric type that's known to be a constant
6880 value at compile-time or for an aggregate type that's a
6881 literal constant. */
6882 STRIP_NOPS (arg);
6884 /* If we know this is a constant, emit the constant of one. */
6885 if (CONSTANT_CLASS_P (arg)
6886 || (TREE_CODE (arg) == CONSTRUCTOR
6887 && TREE_CONSTANT (arg)))
6888 return integer_one_node;
6889 if (TREE_CODE (arg) == ADDR_EXPR)
6891 tree op = TREE_OPERAND (arg, 0);
6892 if (TREE_CODE (op) == STRING_CST
6893 || (TREE_CODE (op) == ARRAY_REF
6894 && integer_zerop (TREE_OPERAND (op, 1))
6895 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6896 return integer_one_node;
6899 /* If this expression has side effects, show we don't know it to be a
6900 constant. Likewise if it's a pointer or aggregate type since in
6901 those case we only want literals, since those are only optimized
6902 when generating RTL, not later.
6903 And finally, if we are compiling an initializer, not code, we
6904 need to return a definite result now; there's not going to be any
6905 more optimization done. */
6906 if (TREE_SIDE_EFFECTS (arg)
6907 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6908 || POINTER_TYPE_P (TREE_TYPE (arg))
6909 || cfun == 0
6910 || folding_initializer)
6911 return integer_zero_node;
6913 return NULL_TREE;
6916 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6917 return it as a truthvalue. */
6919 static tree
6920 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6922 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6924 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6925 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6926 ret_type = TREE_TYPE (TREE_TYPE (fn));
6927 pred_type = TREE_VALUE (arg_types);
6928 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6930 pred = fold_convert_loc (loc, pred_type, pred);
6931 expected = fold_convert_loc (loc, expected_type, expected);
6932 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6934 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6935 build_int_cst (ret_type, 0));
6938 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6939 NULL_TREE if no simplification is possible. */
6941 static tree
6942 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6944 tree inner, fndecl, inner_arg0;
6945 enum tree_code code;
6947 /* Distribute the expected value over short-circuiting operators.
6948 See through the cast from truthvalue_type_node to long. */
6949 inner_arg0 = arg0;
6950 while (TREE_CODE (inner_arg0) == NOP_EXPR
6951 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6952 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6953 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6955 /* If this is a builtin_expect within a builtin_expect keep the
6956 inner one. See through a comparison against a constant. It
6957 might have been added to create a thruthvalue. */
6958 inner = inner_arg0;
6960 if (COMPARISON_CLASS_P (inner)
6961 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6962 inner = TREE_OPERAND (inner, 0);
6964 if (TREE_CODE (inner) == CALL_EXPR
6965 && (fndecl = get_callee_fndecl (inner))
6966 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6967 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6968 return arg0;
6970 inner = inner_arg0;
6971 code = TREE_CODE (inner);
6972 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6974 tree op0 = TREE_OPERAND (inner, 0);
6975 tree op1 = TREE_OPERAND (inner, 1);
6977 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6978 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6979 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6981 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6984 /* If the argument isn't invariant then there's nothing else we can do. */
6985 if (!TREE_CONSTANT (inner_arg0))
6986 return NULL_TREE;
6988 /* If we expect that a comparison against the argument will fold to
6989 a constant return the constant. In practice, this means a true
6990 constant or the address of a non-weak symbol. */
6991 inner = inner_arg0;
6992 STRIP_NOPS (inner);
6993 if (TREE_CODE (inner) == ADDR_EXPR)
6997 inner = TREE_OPERAND (inner, 0);
6999 while (TREE_CODE (inner) == COMPONENT_REF
7000 || TREE_CODE (inner) == ARRAY_REF);
7001 if ((TREE_CODE (inner) == VAR_DECL
7002 || TREE_CODE (inner) == FUNCTION_DECL)
7003 && DECL_WEAK (inner))
7004 return NULL_TREE;
7007 /* Otherwise, ARG0 already has the proper type for the return value. */
7008 return arg0;
7011 /* Fold a call to __builtin_classify_type with argument ARG. */
7013 static tree
7014 fold_builtin_classify_type (tree arg)
7016 if (arg == 0)
7017 return build_int_cst (integer_type_node, no_type_class);
7019 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7022 /* Fold a call to __builtin_strlen with argument ARG. */
7024 static tree
7025 fold_builtin_strlen (location_t loc, tree type, tree arg)
7027 if (!validate_arg (arg, POINTER_TYPE))
7028 return NULL_TREE;
7029 else
7031 tree len = c_strlen (arg, 0);
7033 if (len)
7034 return fold_convert_loc (loc, type, len);
7036 return NULL_TREE;
7040 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7042 static tree
7043 fold_builtin_inf (location_t loc, tree type, int warn)
7045 REAL_VALUE_TYPE real;
7047 /* __builtin_inff is intended to be usable to define INFINITY on all
7048 targets. If an infinity is not available, INFINITY expands "to a
7049 positive constant of type float that overflows at translation
7050 time", footnote "In this case, using INFINITY will violate the
7051 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7052 Thus we pedwarn to ensure this constraint violation is
7053 diagnosed. */
7054 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7055 pedwarn (loc, 0, "target format does not support infinity");
7057 real_inf (&real);
7058 return build_real (type, real);
7061 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7063 static tree
7064 fold_builtin_nan (tree arg, tree type, int quiet)
7066 REAL_VALUE_TYPE real;
7067 const char *str;
7069 if (!validate_arg (arg, POINTER_TYPE))
7070 return NULL_TREE;
7071 str = c_getstr (arg);
7072 if (!str)
7073 return NULL_TREE;
7075 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7076 return NULL_TREE;
7078 return build_real (type, real);
7081 /* Return true if the floating point expression T has an integer value.
7082 We also allow +Inf, -Inf and NaN to be considered integer values. */
7084 static bool
7085 integer_valued_real_p (tree t)
7087 switch (TREE_CODE (t))
7089 case FLOAT_EXPR:
7090 return true;
7092 case ABS_EXPR:
7093 case SAVE_EXPR:
7094 return integer_valued_real_p (TREE_OPERAND (t, 0));
7096 case COMPOUND_EXPR:
7097 case MODIFY_EXPR:
7098 case BIND_EXPR:
7099 return integer_valued_real_p (TREE_OPERAND (t, 1));
7101 case PLUS_EXPR:
7102 case MINUS_EXPR:
7103 case MULT_EXPR:
7104 case MIN_EXPR:
7105 case MAX_EXPR:
7106 return integer_valued_real_p (TREE_OPERAND (t, 0))
7107 && integer_valued_real_p (TREE_OPERAND (t, 1));
7109 case COND_EXPR:
7110 return integer_valued_real_p (TREE_OPERAND (t, 1))
7111 && integer_valued_real_p (TREE_OPERAND (t, 2));
7113 case REAL_CST:
7114 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7116 case NOP_EXPR:
7118 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7119 if (TREE_CODE (type) == INTEGER_TYPE)
7120 return true;
7121 if (TREE_CODE (type) == REAL_TYPE)
7122 return integer_valued_real_p (TREE_OPERAND (t, 0));
7123 break;
7126 case CALL_EXPR:
7127 switch (builtin_mathfn_code (t))
7129 CASE_FLT_FN (BUILT_IN_CEIL):
7130 CASE_FLT_FN (BUILT_IN_FLOOR):
7131 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7132 CASE_FLT_FN (BUILT_IN_RINT):
7133 CASE_FLT_FN (BUILT_IN_ROUND):
7134 CASE_FLT_FN (BUILT_IN_TRUNC):
7135 return true;
7137 CASE_FLT_FN (BUILT_IN_FMIN):
7138 CASE_FLT_FN (BUILT_IN_FMAX):
7139 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7140 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7142 default:
7143 break;
7145 break;
7147 default:
7148 break;
7150 return false;
7153 /* FNDECL is assumed to be a builtin where truncation can be propagated
7154 across (for instance floor((double)f) == (double)floorf (f).
7155 Do the transformation for a call with argument ARG. */
7157 static tree
7158 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7160 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7162 if (!validate_arg (arg, REAL_TYPE))
7163 return NULL_TREE;
7165 /* Integer rounding functions are idempotent. */
7166 if (fcode == builtin_mathfn_code (arg))
7167 return arg;
7169 /* If argument is already integer valued, and we don't need to worry
7170 about setting errno, there's no need to perform rounding. */
7171 if (! flag_errno_math && integer_valued_real_p (arg))
7172 return arg;
7174 if (optimize)
7176 tree arg0 = strip_float_extensions (arg);
7177 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7178 tree newtype = TREE_TYPE (arg0);
7179 tree decl;
7181 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7182 && (decl = mathfn_built_in (newtype, fcode)))
7183 return fold_convert_loc (loc, ftype,
7184 build_call_expr_loc (loc, decl, 1,
7185 fold_convert_loc (loc,
7186 newtype,
7187 arg0)));
7189 return NULL_TREE;
7192 /* FNDECL is assumed to be builtin which can narrow the FP type of
7193 the argument, for instance lround((double)f) -> lroundf (f).
7194 Do the transformation for a call with argument ARG. */
7196 static tree
7197 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7199 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7201 if (!validate_arg (arg, REAL_TYPE))
7202 return NULL_TREE;
7204 /* If argument is already integer valued, and we don't need to worry
7205 about setting errno, there's no need to perform rounding. */
7206 if (! flag_errno_math && integer_valued_real_p (arg))
7207 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7208 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7210 if (optimize)
7212 tree ftype = TREE_TYPE (arg);
7213 tree arg0 = strip_float_extensions (arg);
7214 tree newtype = TREE_TYPE (arg0);
7215 tree decl;
7217 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7218 && (decl = mathfn_built_in (newtype, fcode)))
7219 return build_call_expr_loc (loc, decl, 1,
7220 fold_convert_loc (loc, newtype, arg0));
7223 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7224 sizeof (int) == sizeof (long). */
7225 if (TYPE_PRECISION (integer_type_node)
7226 == TYPE_PRECISION (long_integer_type_node))
7228 tree newfn = NULL_TREE;
7229 switch (fcode)
7231 CASE_FLT_FN (BUILT_IN_ICEIL):
7232 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7233 break;
7235 CASE_FLT_FN (BUILT_IN_IFLOOR):
7236 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7237 break;
7239 CASE_FLT_FN (BUILT_IN_IROUND):
7240 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7241 break;
7243 CASE_FLT_FN (BUILT_IN_IRINT):
7244 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7245 break;
7247 default:
7248 break;
7251 if (newfn)
7253 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7254 return fold_convert_loc (loc,
7255 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7259 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7260 sizeof (long long) == sizeof (long). */
7261 if (TYPE_PRECISION (long_long_integer_type_node)
7262 == TYPE_PRECISION (long_integer_type_node))
7264 tree newfn = NULL_TREE;
7265 switch (fcode)
7267 CASE_FLT_FN (BUILT_IN_LLCEIL):
7268 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7269 break;
7271 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7272 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7273 break;
7275 CASE_FLT_FN (BUILT_IN_LLROUND):
7276 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7277 break;
7279 CASE_FLT_FN (BUILT_IN_LLRINT):
7280 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7281 break;
7283 default:
7284 break;
7287 if (newfn)
7289 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7290 return fold_convert_loc (loc,
7291 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7295 return NULL_TREE;
7298 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7299 return type. Return NULL_TREE if no simplification can be made. */
7301 static tree
7302 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7304 tree res;
7306 if (!validate_arg (arg, COMPLEX_TYPE)
7307 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7308 return NULL_TREE;
7310 /* Calculate the result when the argument is a constant. */
7311 if (TREE_CODE (arg) == COMPLEX_CST
7312 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7313 type, mpfr_hypot)))
7314 return res;
7316 if (TREE_CODE (arg) == COMPLEX_EXPR)
7318 tree real = TREE_OPERAND (arg, 0);
7319 tree imag = TREE_OPERAND (arg, 1);
7321 /* If either part is zero, cabs is fabs of the other. */
7322 if (real_zerop (real))
7323 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7324 if (real_zerop (imag))
7325 return fold_build1_loc (loc, ABS_EXPR, type, real);
7327 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7328 if (flag_unsafe_math_optimizations
7329 && operand_equal_p (real, imag, OEP_PURE_SAME))
7331 const REAL_VALUE_TYPE sqrt2_trunc
7332 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7333 STRIP_NOPS (real);
7334 return fold_build2_loc (loc, MULT_EXPR, type,
7335 fold_build1_loc (loc, ABS_EXPR, type, real),
7336 build_real (type, sqrt2_trunc));
7340 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7341 if (TREE_CODE (arg) == NEGATE_EXPR
7342 || TREE_CODE (arg) == CONJ_EXPR)
7343 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7345 /* Don't do this when optimizing for size. */
7346 if (flag_unsafe_math_optimizations
7347 && optimize && optimize_function_for_speed_p (cfun))
7349 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7351 if (sqrtfn != NULL_TREE)
7353 tree rpart, ipart, result;
7355 arg = builtin_save_expr (arg);
7357 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7358 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7360 rpart = builtin_save_expr (rpart);
7361 ipart = builtin_save_expr (ipart);
7363 result = fold_build2_loc (loc, PLUS_EXPR, type,
7364 fold_build2_loc (loc, MULT_EXPR, type,
7365 rpart, rpart),
7366 fold_build2_loc (loc, MULT_EXPR, type,
7367 ipart, ipart));
7369 return build_call_expr_loc (loc, sqrtfn, 1, result);
7373 return NULL_TREE;
7376 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7377 complex tree type of the result. If NEG is true, the imaginary
7378 zero is negative. */
7380 static tree
7381 build_complex_cproj (tree type, bool neg)
7383 REAL_VALUE_TYPE rinf, rzero = dconst0;
7385 real_inf (&rinf);
7386 rzero.sign = neg;
7387 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7388 build_real (TREE_TYPE (type), rzero));
7391 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7392 return type. Return NULL_TREE if no simplification can be made. */
7394 static tree
7395 fold_builtin_cproj (location_t loc, tree arg, tree type)
7397 if (!validate_arg (arg, COMPLEX_TYPE)
7398 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7399 return NULL_TREE;
7401 /* If there are no infinities, return arg. */
7402 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7403 return non_lvalue_loc (loc, arg);
7405 /* Calculate the result when the argument is a constant. */
7406 if (TREE_CODE (arg) == COMPLEX_CST)
7408 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7409 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7411 if (real_isinf (real) || real_isinf (imag))
7412 return build_complex_cproj (type, imag->sign);
7413 else
7414 return arg;
7416 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7418 tree real = TREE_OPERAND (arg, 0);
7419 tree imag = TREE_OPERAND (arg, 1);
7421 STRIP_NOPS (real);
7422 STRIP_NOPS (imag);
7424 /* If the real part is inf and the imag part is known to be
7425 nonnegative, return (inf + 0i). Remember side-effects are
7426 possible in the imag part. */
7427 if (TREE_CODE (real) == REAL_CST
7428 && real_isinf (TREE_REAL_CST_PTR (real))
7429 && tree_expr_nonnegative_p (imag))
7430 return omit_one_operand_loc (loc, type,
7431 build_complex_cproj (type, false),
7432 arg);
7434 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7435 Remember side-effects are possible in the real part. */
7436 if (TREE_CODE (imag) == REAL_CST
7437 && real_isinf (TREE_REAL_CST_PTR (imag)))
7438 return
7439 omit_one_operand_loc (loc, type,
7440 build_complex_cproj (type, TREE_REAL_CST_PTR
7441 (imag)->sign), arg);
7444 return NULL_TREE;
7447 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7448 Return NULL_TREE if no simplification can be made. */
7450 static tree
7451 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7454 enum built_in_function fcode;
7455 tree res;
7457 if (!validate_arg (arg, REAL_TYPE))
7458 return NULL_TREE;
7460 /* Calculate the result when the argument is a constant. */
7461 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7462 return res;
7464 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7465 fcode = builtin_mathfn_code (arg);
7466 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7468 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7469 arg = fold_build2_loc (loc, MULT_EXPR, type,
7470 CALL_EXPR_ARG (arg, 0),
7471 build_real (type, dconsthalf));
7472 return build_call_expr_loc (loc, expfn, 1, arg);
7475 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7476 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7478 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7480 if (powfn)
7482 tree arg0 = CALL_EXPR_ARG (arg, 0);
7483 tree tree_root;
7484 /* The inner root was either sqrt or cbrt. */
7485 /* This was a conditional expression but it triggered a bug
7486 in Sun C 5.5. */
7487 REAL_VALUE_TYPE dconstroot;
7488 if (BUILTIN_SQRT_P (fcode))
7489 dconstroot = dconsthalf;
7490 else
7491 dconstroot = dconst_third ();
7493 /* Adjust for the outer root. */
7494 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7495 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7496 tree_root = build_real (type, dconstroot);
7497 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7501 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7502 if (flag_unsafe_math_optimizations
7503 && (fcode == BUILT_IN_POW
7504 || fcode == BUILT_IN_POWF
7505 || fcode == BUILT_IN_POWL))
7507 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7508 tree arg0 = CALL_EXPR_ARG (arg, 0);
7509 tree arg1 = CALL_EXPR_ARG (arg, 1);
7510 tree narg1;
7511 if (!tree_expr_nonnegative_p (arg0))
7512 arg0 = build1 (ABS_EXPR, type, arg0);
7513 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7514 build_real (type, dconsthalf));
7515 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7518 return NULL_TREE;
7521 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7522 Return NULL_TREE if no simplification can be made. */
7524 static tree
7525 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7527 const enum built_in_function fcode = builtin_mathfn_code (arg);
7528 tree res;
7530 if (!validate_arg (arg, REAL_TYPE))
7531 return NULL_TREE;
7533 /* Calculate the result when the argument is a constant. */
7534 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7535 return res;
7537 if (flag_unsafe_math_optimizations)
7539 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7540 if (BUILTIN_EXPONENT_P (fcode))
7542 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7543 const REAL_VALUE_TYPE third_trunc =
7544 real_value_truncate (TYPE_MODE (type), dconst_third ());
7545 arg = fold_build2_loc (loc, MULT_EXPR, type,
7546 CALL_EXPR_ARG (arg, 0),
7547 build_real (type, third_trunc));
7548 return build_call_expr_loc (loc, expfn, 1, arg);
7551 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7552 if (BUILTIN_SQRT_P (fcode))
7554 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7556 if (powfn)
7558 tree arg0 = CALL_EXPR_ARG (arg, 0);
7559 tree tree_root;
7560 REAL_VALUE_TYPE dconstroot = dconst_third ();
7562 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7563 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7564 tree_root = build_real (type, dconstroot);
7565 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7569 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7570 if (BUILTIN_CBRT_P (fcode))
7572 tree arg0 = CALL_EXPR_ARG (arg, 0);
7573 if (tree_expr_nonnegative_p (arg0))
7575 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7577 if (powfn)
7579 tree tree_root;
7580 REAL_VALUE_TYPE dconstroot;
7582 real_arithmetic (&dconstroot, MULT_EXPR,
7583 dconst_third_ptr (), dconst_third_ptr ());
7584 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7585 tree_root = build_real (type, dconstroot);
7586 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7591 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7592 if (fcode == BUILT_IN_POW
7593 || fcode == BUILT_IN_POWF
7594 || fcode == BUILT_IN_POWL)
7596 tree arg00 = CALL_EXPR_ARG (arg, 0);
7597 tree arg01 = CALL_EXPR_ARG (arg, 1);
7598 if (tree_expr_nonnegative_p (arg00))
7600 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7601 const REAL_VALUE_TYPE dconstroot
7602 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7603 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7604 build_real (type, dconstroot));
7605 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7609 return NULL_TREE;
7612 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7613 TYPE is the type of the return value. Return NULL_TREE if no
7614 simplification can be made. */
7616 static tree
7617 fold_builtin_cos (location_t loc,
7618 tree arg, tree type, tree fndecl)
7620 tree res, narg;
7622 if (!validate_arg (arg, REAL_TYPE))
7623 return NULL_TREE;
7625 /* Calculate the result when the argument is a constant. */
7626 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7627 return res;
7629 /* Optimize cos(-x) into cos (x). */
7630 if ((narg = fold_strip_sign_ops (arg)))
7631 return build_call_expr_loc (loc, fndecl, 1, narg);
7633 return NULL_TREE;
7636 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7637 Return NULL_TREE if no simplification can be made. */
7639 static tree
7640 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7642 if (validate_arg (arg, REAL_TYPE))
7644 tree res, narg;
7646 /* Calculate the result when the argument is a constant. */
7647 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7648 return res;
7650 /* Optimize cosh(-x) into cosh (x). */
7651 if ((narg = fold_strip_sign_ops (arg)))
7652 return build_call_expr_loc (loc, fndecl, 1, narg);
7655 return NULL_TREE;
7658 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7659 argument ARG. TYPE is the type of the return value. Return
7660 NULL_TREE if no simplification can be made. */
7662 static tree
7663 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7664 bool hyper)
7666 if (validate_arg (arg, COMPLEX_TYPE)
7667 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7669 tree tmp;
7671 /* Calculate the result when the argument is a constant. */
7672 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7673 return tmp;
7675 /* Optimize fn(-x) into fn(x). */
7676 if ((tmp = fold_strip_sign_ops (arg)))
7677 return build_call_expr_loc (loc, fndecl, 1, tmp);
7680 return NULL_TREE;
7683 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7684 Return NULL_TREE if no simplification can be made. */
7686 static tree
7687 fold_builtin_tan (tree arg, tree type)
7689 enum built_in_function fcode;
7690 tree res;
7692 if (!validate_arg (arg, REAL_TYPE))
7693 return NULL_TREE;
7695 /* Calculate the result when the argument is a constant. */
7696 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7697 return res;
7699 /* Optimize tan(atan(x)) = x. */
7700 fcode = builtin_mathfn_code (arg);
7701 if (flag_unsafe_math_optimizations
7702 && (fcode == BUILT_IN_ATAN
7703 || fcode == BUILT_IN_ATANF
7704 || fcode == BUILT_IN_ATANL))
7705 return CALL_EXPR_ARG (arg, 0);
7707 return NULL_TREE;
7710 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7711 NULL_TREE if no simplification can be made. */
7713 static tree
7714 fold_builtin_sincos (location_t loc,
7715 tree arg0, tree arg1, tree arg2)
7717 tree type;
7718 tree res, fn, call;
7720 if (!validate_arg (arg0, REAL_TYPE)
7721 || !validate_arg (arg1, POINTER_TYPE)
7722 || !validate_arg (arg2, POINTER_TYPE))
7723 return NULL_TREE;
7725 type = TREE_TYPE (arg0);
7727 /* Calculate the result when the argument is a constant. */
7728 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7729 return res;
7731 /* Canonicalize sincos to cexpi. */
7732 if (!TARGET_C99_FUNCTIONS)
7733 return NULL_TREE;
7734 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7735 if (!fn)
7736 return NULL_TREE;
7738 call = build_call_expr_loc (loc, fn, 1, arg0);
7739 call = builtin_save_expr (call);
7741 return build2 (COMPOUND_EXPR, void_type_node,
7742 build2 (MODIFY_EXPR, void_type_node,
7743 build_fold_indirect_ref_loc (loc, arg1),
7744 build1 (IMAGPART_EXPR, type, call)),
7745 build2 (MODIFY_EXPR, void_type_node,
7746 build_fold_indirect_ref_loc (loc, arg2),
7747 build1 (REALPART_EXPR, type, call)));
7750 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7751 NULL_TREE if no simplification can be made. */
7753 static tree
7754 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7756 tree rtype;
7757 tree realp, imagp, ifn;
7758 tree res;
7760 if (!validate_arg (arg0, COMPLEX_TYPE)
7761 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7762 return NULL_TREE;
7764 /* Calculate the result when the argument is a constant. */
7765 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7766 return res;
7768 rtype = TREE_TYPE (TREE_TYPE (arg0));
7770 /* In case we can figure out the real part of arg0 and it is constant zero
7771 fold to cexpi. */
7772 if (!TARGET_C99_FUNCTIONS)
7773 return NULL_TREE;
7774 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7775 if (!ifn)
7776 return NULL_TREE;
7778 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7779 && real_zerop (realp))
7781 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7782 return build_call_expr_loc (loc, ifn, 1, narg);
7785 /* In case we can easily decompose real and imaginary parts split cexp
7786 to exp (r) * cexpi (i). */
7787 if (flag_unsafe_math_optimizations
7788 && realp)
7790 tree rfn, rcall, icall;
7792 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7793 if (!rfn)
7794 return NULL_TREE;
7796 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7797 if (!imagp)
7798 return NULL_TREE;
7800 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7801 icall = builtin_save_expr (icall);
7802 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7803 rcall = builtin_save_expr (rcall);
7804 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7805 fold_build2_loc (loc, MULT_EXPR, rtype,
7806 rcall,
7807 fold_build1_loc (loc, REALPART_EXPR,
7808 rtype, icall)),
7809 fold_build2_loc (loc, MULT_EXPR, rtype,
7810 rcall,
7811 fold_build1_loc (loc, IMAGPART_EXPR,
7812 rtype, icall)));
7815 return NULL_TREE;
7818 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7819 Return NULL_TREE if no simplification can be made. */
7821 static tree
7822 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7824 if (!validate_arg (arg, REAL_TYPE))
7825 return NULL_TREE;
7827 /* Optimize trunc of constant value. */
7828 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7830 REAL_VALUE_TYPE r, x;
7831 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7833 x = TREE_REAL_CST (arg);
7834 real_trunc (&r, TYPE_MODE (type), &x);
7835 return build_real (type, r);
7838 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7841 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7842 Return NULL_TREE if no simplification can be made. */
7844 static tree
7845 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7847 if (!validate_arg (arg, REAL_TYPE))
7848 return NULL_TREE;
7850 /* Optimize floor of constant value. */
7851 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7853 REAL_VALUE_TYPE x;
7855 x = TREE_REAL_CST (arg);
7856 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7858 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7859 REAL_VALUE_TYPE r;
7861 real_floor (&r, TYPE_MODE (type), &x);
7862 return build_real (type, r);
7866 /* Fold floor (x) where x is nonnegative to trunc (x). */
7867 if (tree_expr_nonnegative_p (arg))
7869 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7870 if (truncfn)
7871 return build_call_expr_loc (loc, truncfn, 1, arg);
7874 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7877 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7878 Return NULL_TREE if no simplification can be made. */
7880 static tree
7881 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7883 if (!validate_arg (arg, REAL_TYPE))
7884 return NULL_TREE;
7886 /* Optimize ceil of constant value. */
7887 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7889 REAL_VALUE_TYPE x;
7891 x = TREE_REAL_CST (arg);
7892 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7894 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7895 REAL_VALUE_TYPE r;
7897 real_ceil (&r, TYPE_MODE (type), &x);
7898 return build_real (type, r);
7902 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7905 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7906 Return NULL_TREE if no simplification can be made. */
7908 static tree
7909 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7911 if (!validate_arg (arg, REAL_TYPE))
7912 return NULL_TREE;
7914 /* Optimize round of constant value. */
7915 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7917 REAL_VALUE_TYPE x;
7919 x = TREE_REAL_CST (arg);
7920 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7922 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7923 REAL_VALUE_TYPE r;
7925 real_round (&r, TYPE_MODE (type), &x);
7926 return build_real (type, r);
7930 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7933 /* Fold function call to builtin lround, lroundf or lroundl (or the
7934 corresponding long long versions) and other rounding functions. ARG
7935 is the argument to the call. Return NULL_TREE if no simplification
7936 can be made. */
7938 static tree
7939 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7941 if (!validate_arg (arg, REAL_TYPE))
7942 return NULL_TREE;
7944 /* Optimize lround of constant value. */
7945 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7947 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7949 if (real_isfinite (&x))
7951 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7952 tree ftype = TREE_TYPE (arg);
7953 double_int val;
7954 REAL_VALUE_TYPE r;
7956 switch (DECL_FUNCTION_CODE (fndecl))
7958 CASE_FLT_FN (BUILT_IN_IFLOOR):
7959 CASE_FLT_FN (BUILT_IN_LFLOOR):
7960 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7961 real_floor (&r, TYPE_MODE (ftype), &x);
7962 break;
7964 CASE_FLT_FN (BUILT_IN_ICEIL):
7965 CASE_FLT_FN (BUILT_IN_LCEIL):
7966 CASE_FLT_FN (BUILT_IN_LLCEIL):
7967 real_ceil (&r, TYPE_MODE (ftype), &x);
7968 break;
7970 CASE_FLT_FN (BUILT_IN_IROUND):
7971 CASE_FLT_FN (BUILT_IN_LROUND):
7972 CASE_FLT_FN (BUILT_IN_LLROUND):
7973 real_round (&r, TYPE_MODE (ftype), &x);
7974 break;
7976 default:
7977 gcc_unreachable ();
7980 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7981 if (double_int_fits_to_tree_p (itype, val))
7982 return double_int_to_tree (itype, val);
7986 switch (DECL_FUNCTION_CODE (fndecl))
7988 CASE_FLT_FN (BUILT_IN_LFLOOR):
7989 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7990 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7991 if (tree_expr_nonnegative_p (arg))
7992 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7993 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7994 break;
7995 default:;
7998 return fold_fixed_mathfn (loc, fndecl, arg);
8001 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8002 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8003 the argument to the call. Return NULL_TREE if no simplification can
8004 be made. */
8006 static tree
8007 fold_builtin_bitop (tree fndecl, tree arg)
8009 if (!validate_arg (arg, INTEGER_TYPE))
8010 return NULL_TREE;
8012 /* Optimize for constant argument. */
8013 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8015 HOST_WIDE_INT hi, width, result;
8016 unsigned HOST_WIDE_INT lo;
8017 tree type;
8019 type = TREE_TYPE (arg);
8020 width = TYPE_PRECISION (type);
8021 lo = TREE_INT_CST_LOW (arg);
8023 /* Clear all the bits that are beyond the type's precision. */
8024 if (width > HOST_BITS_PER_WIDE_INT)
8026 hi = TREE_INT_CST_HIGH (arg);
8027 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8028 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8029 << (width - HOST_BITS_PER_WIDE_INT));
8031 else
8033 hi = 0;
8034 if (width < HOST_BITS_PER_WIDE_INT)
8035 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8038 switch (DECL_FUNCTION_CODE (fndecl))
8040 CASE_INT_FN (BUILT_IN_FFS):
8041 if (lo != 0)
8042 result = ffs_hwi (lo);
8043 else if (hi != 0)
8044 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8045 else
8046 result = 0;
8047 break;
8049 CASE_INT_FN (BUILT_IN_CLZ):
8050 if (hi != 0)
8051 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8052 else if (lo != 0)
8053 result = width - floor_log2 (lo) - 1;
8054 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8055 result = width;
8056 break;
8058 CASE_INT_FN (BUILT_IN_CTZ):
8059 if (lo != 0)
8060 result = ctz_hwi (lo);
8061 else if (hi != 0)
8062 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8063 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8064 result = width;
8065 break;
8067 CASE_INT_FN (BUILT_IN_CLRSB):
8068 if (width > HOST_BITS_PER_WIDE_INT
8069 && (hi & ((unsigned HOST_WIDE_INT) 1
8070 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8072 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8073 << (width - HOST_BITS_PER_WIDE_INT - 1));
8074 lo = ~lo;
8076 else if (width <= HOST_BITS_PER_WIDE_INT
8077 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8078 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8079 if (hi != 0)
8080 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8081 else if (lo != 0)
8082 result = width - floor_log2 (lo) - 2;
8083 else
8084 result = width - 1;
8085 break;
8087 CASE_INT_FN (BUILT_IN_POPCOUNT):
8088 result = 0;
8089 while (lo)
8090 result++, lo &= lo - 1;
8091 while (hi)
8092 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8093 break;
8095 CASE_INT_FN (BUILT_IN_PARITY):
8096 result = 0;
8097 while (lo)
8098 result++, lo &= lo - 1;
8099 while (hi)
8100 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8101 result &= 1;
8102 break;
8104 default:
8105 gcc_unreachable ();
8108 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8111 return NULL_TREE;
8114 /* Fold function call to builtin_bswap and the long and long long
8115 variants. Return NULL_TREE if no simplification can be made. */
8116 static tree
8117 fold_builtin_bswap (tree fndecl, tree arg)
8119 if (! validate_arg (arg, INTEGER_TYPE))
8120 return NULL_TREE;
8122 /* Optimize constant value. */
8123 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8125 HOST_WIDE_INT hi, width, r_hi = 0;
8126 unsigned HOST_WIDE_INT lo, r_lo = 0;
8127 tree type;
8129 type = TREE_TYPE (arg);
8130 width = TYPE_PRECISION (type);
8131 lo = TREE_INT_CST_LOW (arg);
8132 hi = TREE_INT_CST_HIGH (arg);
8134 switch (DECL_FUNCTION_CODE (fndecl))
8136 case BUILT_IN_BSWAP32:
8137 case BUILT_IN_BSWAP64:
8139 int s;
8141 for (s = 0; s < width; s += 8)
8143 int d = width - s - 8;
8144 unsigned HOST_WIDE_INT byte;
8146 if (s < HOST_BITS_PER_WIDE_INT)
8147 byte = (lo >> s) & 0xff;
8148 else
8149 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8151 if (d < HOST_BITS_PER_WIDE_INT)
8152 r_lo |= byte << d;
8153 else
8154 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8158 break;
8160 default:
8161 gcc_unreachable ();
8164 if (width < HOST_BITS_PER_WIDE_INT)
8165 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8166 else
8167 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8170 return NULL_TREE;
8173 /* A subroutine of fold_builtin to fold the various logarithmic
8174 functions. Return NULL_TREE if no simplification can me made.
8175 FUNC is the corresponding MPFR logarithm function. */
8177 static tree
8178 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8179 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8181 if (validate_arg (arg, REAL_TYPE))
8183 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8184 tree res;
8185 const enum built_in_function fcode = builtin_mathfn_code (arg);
8187 /* Calculate the result when the argument is a constant. */
8188 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8189 return res;
8191 /* Special case, optimize logN(expN(x)) = x. */
8192 if (flag_unsafe_math_optimizations
8193 && ((func == mpfr_log
8194 && (fcode == BUILT_IN_EXP
8195 || fcode == BUILT_IN_EXPF
8196 || fcode == BUILT_IN_EXPL))
8197 || (func == mpfr_log2
8198 && (fcode == BUILT_IN_EXP2
8199 || fcode == BUILT_IN_EXP2F
8200 || fcode == BUILT_IN_EXP2L))
8201 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8202 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8204 /* Optimize logN(func()) for various exponential functions. We
8205 want to determine the value "x" and the power "exponent" in
8206 order to transform logN(x**exponent) into exponent*logN(x). */
8207 if (flag_unsafe_math_optimizations)
8209 tree exponent = 0, x = 0;
8211 switch (fcode)
8213 CASE_FLT_FN (BUILT_IN_EXP):
8214 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8215 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8216 dconst_e ()));
8217 exponent = CALL_EXPR_ARG (arg, 0);
8218 break;
8219 CASE_FLT_FN (BUILT_IN_EXP2):
8220 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8221 x = build_real (type, dconst2);
8222 exponent = CALL_EXPR_ARG (arg, 0);
8223 break;
8224 CASE_FLT_FN (BUILT_IN_EXP10):
8225 CASE_FLT_FN (BUILT_IN_POW10):
8226 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8228 REAL_VALUE_TYPE dconst10;
8229 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8230 x = build_real (type, dconst10);
8232 exponent = CALL_EXPR_ARG (arg, 0);
8233 break;
8234 CASE_FLT_FN (BUILT_IN_SQRT):
8235 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8236 x = CALL_EXPR_ARG (arg, 0);
8237 exponent = build_real (type, dconsthalf);
8238 break;
8239 CASE_FLT_FN (BUILT_IN_CBRT):
8240 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8241 x = CALL_EXPR_ARG (arg, 0);
8242 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8243 dconst_third ()));
8244 break;
8245 CASE_FLT_FN (BUILT_IN_POW):
8246 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8247 x = CALL_EXPR_ARG (arg, 0);
8248 exponent = CALL_EXPR_ARG (arg, 1);
8249 break;
8250 default:
8251 break;
8254 /* Now perform the optimization. */
8255 if (x && exponent)
8257 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8258 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8263 return NULL_TREE;
8266 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8267 NULL_TREE if no simplification can be made. */
8269 static tree
8270 fold_builtin_hypot (location_t loc, tree fndecl,
8271 tree arg0, tree arg1, tree type)
8273 tree res, narg0, narg1;
8275 if (!validate_arg (arg0, REAL_TYPE)
8276 || !validate_arg (arg1, REAL_TYPE))
8277 return NULL_TREE;
8279 /* Calculate the result when the argument is a constant. */
8280 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8281 return res;
8283 /* If either argument to hypot has a negate or abs, strip that off.
8284 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8285 narg0 = fold_strip_sign_ops (arg0);
8286 narg1 = fold_strip_sign_ops (arg1);
8287 if (narg0 || narg1)
8289 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8290 narg1 ? narg1 : arg1);
8293 /* If either argument is zero, hypot is fabs of the other. */
8294 if (real_zerop (arg0))
8295 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8296 else if (real_zerop (arg1))
8297 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8299 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8300 if (flag_unsafe_math_optimizations
8301 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8303 const REAL_VALUE_TYPE sqrt2_trunc
8304 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8305 return fold_build2_loc (loc, MULT_EXPR, type,
8306 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8307 build_real (type, sqrt2_trunc));
8310 return NULL_TREE;
8314 /* Fold a builtin function call to pow, powf, or powl. Return
8315 NULL_TREE if no simplification can be made. */
8316 static tree
8317 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8319 tree res;
8321 if (!validate_arg (arg0, REAL_TYPE)
8322 || !validate_arg (arg1, REAL_TYPE))
8323 return NULL_TREE;
8325 /* Calculate the result when the argument is a constant. */
8326 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8327 return res;
8329 /* Optimize pow(1.0,y) = 1.0. */
8330 if (real_onep (arg0))
8331 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8333 if (TREE_CODE (arg1) == REAL_CST
8334 && !TREE_OVERFLOW (arg1))
8336 REAL_VALUE_TYPE cint;
8337 REAL_VALUE_TYPE c;
8338 HOST_WIDE_INT n;
8340 c = TREE_REAL_CST (arg1);
8342 /* Optimize pow(x,0.0) = 1.0. */
8343 if (REAL_VALUES_EQUAL (c, dconst0))
8344 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8345 arg0);
8347 /* Optimize pow(x,1.0) = x. */
8348 if (REAL_VALUES_EQUAL (c, dconst1))
8349 return arg0;
8351 /* Optimize pow(x,-1.0) = 1.0/x. */
8352 if (REAL_VALUES_EQUAL (c, dconstm1))
8353 return fold_build2_loc (loc, RDIV_EXPR, type,
8354 build_real (type, dconst1), arg0);
8356 /* Optimize pow(x,0.5) = sqrt(x). */
8357 if (flag_unsafe_math_optimizations
8358 && REAL_VALUES_EQUAL (c, dconsthalf))
8360 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8362 if (sqrtfn != NULL_TREE)
8363 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8366 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8367 if (flag_unsafe_math_optimizations)
8369 const REAL_VALUE_TYPE dconstroot
8370 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8372 if (REAL_VALUES_EQUAL (c, dconstroot))
8374 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8375 if (cbrtfn != NULL_TREE)
8376 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8380 /* Check for an integer exponent. */
8381 n = real_to_integer (&c);
8382 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8383 if (real_identical (&c, &cint))
8385 /* Attempt to evaluate pow at compile-time, unless this should
8386 raise an exception. */
8387 if (TREE_CODE (arg0) == REAL_CST
8388 && !TREE_OVERFLOW (arg0)
8389 && (n > 0
8390 || (!flag_trapping_math && !flag_errno_math)
8391 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8393 REAL_VALUE_TYPE x;
8394 bool inexact;
8396 x = TREE_REAL_CST (arg0);
8397 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8398 if (flag_unsafe_math_optimizations || !inexact)
8399 return build_real (type, x);
8402 /* Strip sign ops from even integer powers. */
8403 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8405 tree narg0 = fold_strip_sign_ops (arg0);
8406 if (narg0)
8407 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8412 if (flag_unsafe_math_optimizations)
8414 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8416 /* Optimize pow(expN(x),y) = expN(x*y). */
8417 if (BUILTIN_EXPONENT_P (fcode))
8419 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8420 tree arg = CALL_EXPR_ARG (arg0, 0);
8421 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8422 return build_call_expr_loc (loc, expfn, 1, arg);
8425 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8426 if (BUILTIN_SQRT_P (fcode))
8428 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8429 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8430 build_real (type, dconsthalf));
8431 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8434 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8435 if (BUILTIN_CBRT_P (fcode))
8437 tree arg = CALL_EXPR_ARG (arg0, 0);
8438 if (tree_expr_nonnegative_p (arg))
8440 const REAL_VALUE_TYPE dconstroot
8441 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8442 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8443 build_real (type, dconstroot));
8444 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8448 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8449 if (fcode == BUILT_IN_POW
8450 || fcode == BUILT_IN_POWF
8451 || fcode == BUILT_IN_POWL)
8453 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8454 if (tree_expr_nonnegative_p (arg00))
8456 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8457 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8458 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8463 return NULL_TREE;
8466 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8467 Return NULL_TREE if no simplification can be made. */
8468 static tree
8469 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8470 tree arg0, tree arg1, tree type)
8472 if (!validate_arg (arg0, REAL_TYPE)
8473 || !validate_arg (arg1, INTEGER_TYPE))
8474 return NULL_TREE;
8476 /* Optimize pow(1.0,y) = 1.0. */
8477 if (real_onep (arg0))
8478 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8480 if (host_integerp (arg1, 0))
8482 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8484 /* Evaluate powi at compile-time. */
8485 if (TREE_CODE (arg0) == REAL_CST
8486 && !TREE_OVERFLOW (arg0))
8488 REAL_VALUE_TYPE x;
8489 x = TREE_REAL_CST (arg0);
8490 real_powi (&x, TYPE_MODE (type), &x, c);
8491 return build_real (type, x);
8494 /* Optimize pow(x,0) = 1.0. */
8495 if (c == 0)
8496 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8497 arg0);
8499 /* Optimize pow(x,1) = x. */
8500 if (c == 1)
8501 return arg0;
8503 /* Optimize pow(x,-1) = 1.0/x. */
8504 if (c == -1)
8505 return fold_build2_loc (loc, RDIV_EXPR, type,
8506 build_real (type, dconst1), arg0);
8509 return NULL_TREE;
8512 /* A subroutine of fold_builtin to fold the various exponent
8513 functions. Return NULL_TREE if no simplification can be made.
8514 FUNC is the corresponding MPFR exponent function. */
8516 static tree
8517 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8518 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8520 if (validate_arg (arg, REAL_TYPE))
8522 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8523 tree res;
8525 /* Calculate the result when the argument is a constant. */
8526 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8527 return res;
8529 /* Optimize expN(logN(x)) = x. */
8530 if (flag_unsafe_math_optimizations)
8532 const enum built_in_function fcode = builtin_mathfn_code (arg);
8534 if ((func == mpfr_exp
8535 && (fcode == BUILT_IN_LOG
8536 || fcode == BUILT_IN_LOGF
8537 || fcode == BUILT_IN_LOGL))
8538 || (func == mpfr_exp2
8539 && (fcode == BUILT_IN_LOG2
8540 || fcode == BUILT_IN_LOG2F
8541 || fcode == BUILT_IN_LOG2L))
8542 || (func == mpfr_exp10
8543 && (fcode == BUILT_IN_LOG10
8544 || fcode == BUILT_IN_LOG10F
8545 || fcode == BUILT_IN_LOG10L)))
8546 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8550 return NULL_TREE;
8553 /* Return true if VAR is a VAR_DECL or a component thereof. */
8555 static bool
8556 var_decl_component_p (tree var)
8558 tree inner = var;
8559 while (handled_component_p (inner))
8560 inner = TREE_OPERAND (inner, 0);
8561 return SSA_VAR_P (inner);
8564 /* Fold function call to builtin memset. Return
8565 NULL_TREE if no simplification can be made. */
8567 static tree
8568 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8569 tree type, bool ignore)
8571 tree var, ret, etype;
8572 unsigned HOST_WIDE_INT length, cval;
8574 if (! validate_arg (dest, POINTER_TYPE)
8575 || ! validate_arg (c, INTEGER_TYPE)
8576 || ! validate_arg (len, INTEGER_TYPE))
8577 return NULL_TREE;
8579 if (! host_integerp (len, 1))
8580 return NULL_TREE;
8582 /* If the LEN parameter is zero, return DEST. */
8583 if (integer_zerop (len))
8584 return omit_one_operand_loc (loc, type, dest, c);
8586 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8587 return NULL_TREE;
8589 var = dest;
8590 STRIP_NOPS (var);
8591 if (TREE_CODE (var) != ADDR_EXPR)
8592 return NULL_TREE;
8594 var = TREE_OPERAND (var, 0);
8595 if (TREE_THIS_VOLATILE (var))
8596 return NULL_TREE;
8598 etype = TREE_TYPE (var);
8599 if (TREE_CODE (etype) == ARRAY_TYPE)
8600 etype = TREE_TYPE (etype);
8602 if (!INTEGRAL_TYPE_P (etype)
8603 && !POINTER_TYPE_P (etype))
8604 return NULL_TREE;
8606 if (! var_decl_component_p (var))
8607 return NULL_TREE;
8609 length = tree_low_cst (len, 1);
8610 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8611 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8612 return NULL_TREE;
8614 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8615 return NULL_TREE;
8617 if (integer_zerop (c))
8618 cval = 0;
8619 else
8621 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8622 return NULL_TREE;
8624 cval = TREE_INT_CST_LOW (c);
8625 cval &= 0xff;
8626 cval |= cval << 8;
8627 cval |= cval << 16;
8628 cval |= (cval << 31) << 1;
8631 ret = build_int_cst_type (etype, cval);
8632 var = build_fold_indirect_ref_loc (loc,
8633 fold_convert_loc (loc,
8634 build_pointer_type (etype),
8635 dest));
8636 ret = build2 (MODIFY_EXPR, etype, var, ret);
8637 if (ignore)
8638 return ret;
8640 return omit_one_operand_loc (loc, type, dest, ret);
8643 /* Fold function call to builtin memset. Return
8644 NULL_TREE if no simplification can be made. */
8646 static tree
8647 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8649 if (! validate_arg (dest, POINTER_TYPE)
8650 || ! validate_arg (size, INTEGER_TYPE))
8651 return NULL_TREE;
8653 if (!ignore)
8654 return NULL_TREE;
8656 /* New argument list transforming bzero(ptr x, int y) to
8657 memset(ptr x, int 0, size_t y). This is done this way
8658 so that if it isn't expanded inline, we fallback to
8659 calling bzero instead of memset. */
8661 return fold_builtin_memset (loc, dest, integer_zero_node,
8662 fold_convert_loc (loc, size_type_node, size),
8663 void_type_node, ignore);
8666 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8667 NULL_TREE if no simplification can be made.
8668 If ENDP is 0, return DEST (like memcpy).
8669 If ENDP is 1, return DEST+LEN (like mempcpy).
8670 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8671 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8672 (memmove). */
8674 static tree
8675 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8676 tree len, tree type, bool ignore, int endp)
8678 tree destvar, srcvar, expr;
8680 if (! validate_arg (dest, POINTER_TYPE)
8681 || ! validate_arg (src, POINTER_TYPE)
8682 || ! validate_arg (len, INTEGER_TYPE))
8683 return NULL_TREE;
8685 /* If the LEN parameter is zero, return DEST. */
8686 if (integer_zerop (len))
8687 return omit_one_operand_loc (loc, type, dest, src);
8689 /* If SRC and DEST are the same (and not volatile), return
8690 DEST{,+LEN,+LEN-1}. */
8691 if (operand_equal_p (src, dest, 0))
8692 expr = len;
8693 else
8695 tree srctype, desttype;
8696 unsigned int src_align, dest_align;
8697 tree off0;
8699 if (endp == 3)
8701 src_align = get_pointer_alignment (src);
8702 dest_align = get_pointer_alignment (dest);
8704 /* Both DEST and SRC must be pointer types.
8705 ??? This is what old code did. Is the testing for pointer types
8706 really mandatory?
8708 If either SRC is readonly or length is 1, we can use memcpy. */
8709 if (!dest_align || !src_align)
8710 return NULL_TREE;
8711 if (readonly_data_expr (src)
8712 || (host_integerp (len, 1)
8713 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8714 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8716 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8717 if (!fn)
8718 return NULL_TREE;
8719 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8722 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8723 if (TREE_CODE (src) == ADDR_EXPR
8724 && TREE_CODE (dest) == ADDR_EXPR)
8726 tree src_base, dest_base, fn;
8727 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8728 HOST_WIDE_INT size = -1;
8729 HOST_WIDE_INT maxsize = -1;
8731 srcvar = TREE_OPERAND (src, 0);
8732 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8733 &size, &maxsize);
8734 destvar = TREE_OPERAND (dest, 0);
8735 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8736 &size, &maxsize);
8737 if (host_integerp (len, 1))
8738 maxsize = tree_low_cst (len, 1);
8739 else
8740 maxsize = -1;
8741 src_offset /= BITS_PER_UNIT;
8742 dest_offset /= BITS_PER_UNIT;
8743 if (SSA_VAR_P (src_base)
8744 && SSA_VAR_P (dest_base))
8746 if (operand_equal_p (src_base, dest_base, 0)
8747 && ranges_overlap_p (src_offset, maxsize,
8748 dest_offset, maxsize))
8749 return NULL_TREE;
8751 else if (TREE_CODE (src_base) == MEM_REF
8752 && TREE_CODE (dest_base) == MEM_REF)
8754 double_int off;
8755 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8756 TREE_OPERAND (dest_base, 0), 0))
8757 return NULL_TREE;
8758 off = double_int_add (mem_ref_offset (src_base),
8759 shwi_to_double_int (src_offset));
8760 if (!double_int_fits_in_shwi_p (off))
8761 return NULL_TREE;
8762 src_offset = off.low;
8763 off = double_int_add (mem_ref_offset (dest_base),
8764 shwi_to_double_int (dest_offset));
8765 if (!double_int_fits_in_shwi_p (off))
8766 return NULL_TREE;
8767 dest_offset = off.low;
8768 if (ranges_overlap_p (src_offset, maxsize,
8769 dest_offset, maxsize))
8770 return NULL_TREE;
8772 else
8773 return NULL_TREE;
8775 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8776 if (!fn)
8777 return NULL_TREE;
8778 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8781 /* If the destination and source do not alias optimize into
8782 memcpy as well. */
8783 if ((is_gimple_min_invariant (dest)
8784 || TREE_CODE (dest) == SSA_NAME)
8785 && (is_gimple_min_invariant (src)
8786 || TREE_CODE (src) == SSA_NAME))
8788 ao_ref destr, srcr;
8789 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8790 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8791 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8793 tree fn;
8794 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8795 if (!fn)
8796 return NULL_TREE;
8797 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8801 return NULL_TREE;
8804 if (!host_integerp (len, 0))
8805 return NULL_TREE;
8806 /* FIXME:
8807 This logic lose for arguments like (type *)malloc (sizeof (type)),
8808 since we strip the casts of up to VOID return value from malloc.
8809 Perhaps we ought to inherit type from non-VOID argument here? */
8810 STRIP_NOPS (src);
8811 STRIP_NOPS (dest);
8812 if (!POINTER_TYPE_P (TREE_TYPE (src))
8813 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8814 return NULL_TREE;
8815 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8816 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8818 tree tem = TREE_OPERAND (src, 0);
8819 STRIP_NOPS (tem);
8820 if (tem != TREE_OPERAND (src, 0))
8821 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8823 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8825 tree tem = TREE_OPERAND (dest, 0);
8826 STRIP_NOPS (tem);
8827 if (tem != TREE_OPERAND (dest, 0))
8828 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8830 srctype = TREE_TYPE (TREE_TYPE (src));
8831 if (TREE_CODE (srctype) == ARRAY_TYPE
8832 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8834 srctype = TREE_TYPE (srctype);
8835 STRIP_NOPS (src);
8836 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8838 desttype = TREE_TYPE (TREE_TYPE (dest));
8839 if (TREE_CODE (desttype) == ARRAY_TYPE
8840 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8842 desttype = TREE_TYPE (desttype);
8843 STRIP_NOPS (dest);
8844 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8846 if (TREE_ADDRESSABLE (srctype)
8847 || TREE_ADDRESSABLE (desttype))
8848 return NULL_TREE;
8850 src_align = get_pointer_alignment (src);
8851 dest_align = get_pointer_alignment (dest);
8852 if (dest_align < TYPE_ALIGN (desttype)
8853 || src_align < TYPE_ALIGN (srctype))
8854 return NULL_TREE;
8856 if (!ignore)
8857 dest = builtin_save_expr (dest);
8859 /* Build accesses at offset zero with a ref-all character type. */
8860 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8861 ptr_mode, true), 0);
8863 destvar = dest;
8864 STRIP_NOPS (destvar);
8865 if (TREE_CODE (destvar) == ADDR_EXPR
8866 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8867 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8868 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8869 else
8870 destvar = NULL_TREE;
8872 srcvar = src;
8873 STRIP_NOPS (srcvar);
8874 if (TREE_CODE (srcvar) == ADDR_EXPR
8875 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8876 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8878 if (!destvar
8879 || src_align >= TYPE_ALIGN (desttype))
8880 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8881 srcvar, off0);
8882 else if (!STRICT_ALIGNMENT)
8884 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8885 src_align);
8886 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8888 else
8889 srcvar = NULL_TREE;
8891 else
8892 srcvar = NULL_TREE;
8894 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8895 return NULL_TREE;
8897 if (srcvar == NULL_TREE)
8899 STRIP_NOPS (src);
8900 if (src_align >= TYPE_ALIGN (desttype))
8901 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8902 else
8904 if (STRICT_ALIGNMENT)
8905 return NULL_TREE;
8906 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8907 src_align);
8908 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8911 else if (destvar == NULL_TREE)
8913 STRIP_NOPS (dest);
8914 if (dest_align >= TYPE_ALIGN (srctype))
8915 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8916 else
8918 if (STRICT_ALIGNMENT)
8919 return NULL_TREE;
8920 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8921 dest_align);
8922 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8926 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8929 if (ignore)
8930 return expr;
8932 if (endp == 0 || endp == 3)
8933 return omit_one_operand_loc (loc, type, dest, expr);
8935 if (expr == len)
8936 expr = NULL_TREE;
8938 if (endp == 2)
8939 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8940 ssize_int (1));
8942 dest = fold_build_pointer_plus_loc (loc, dest, len);
8943 dest = fold_convert_loc (loc, type, dest);
8944 if (expr)
8945 dest = omit_one_operand_loc (loc, type, dest, expr);
8946 return dest;
8949 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8950 If LEN is not NULL, it represents the length of the string to be
8951 copied. Return NULL_TREE if no simplification can be made. */
8953 tree
8954 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8956 tree fn;
8958 if (!validate_arg (dest, POINTER_TYPE)
8959 || !validate_arg (src, POINTER_TYPE))
8960 return NULL_TREE;
8962 /* If SRC and DEST are the same (and not volatile), return DEST. */
8963 if (operand_equal_p (src, dest, 0))
8964 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8966 if (optimize_function_for_size_p (cfun))
8967 return NULL_TREE;
8969 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8970 if (!fn)
8971 return NULL_TREE;
8973 if (!len)
8975 len = c_strlen (src, 1);
8976 if (! len || TREE_SIDE_EFFECTS (len))
8977 return NULL_TREE;
8980 len = fold_convert_loc (loc, size_type_node, len);
8981 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8982 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8983 build_call_expr_loc (loc, fn, 3, dest, src, len));
8986 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8987 Return NULL_TREE if no simplification can be made. */
8989 static tree
8990 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8992 tree fn, len, lenp1, call, type;
8994 if (!validate_arg (dest, POINTER_TYPE)
8995 || !validate_arg (src, POINTER_TYPE))
8996 return NULL_TREE;
8998 len = c_strlen (src, 1);
8999 if (!len
9000 || TREE_CODE (len) != INTEGER_CST)
9001 return NULL_TREE;
9003 if (optimize_function_for_size_p (cfun)
9004 /* If length is zero it's small enough. */
9005 && !integer_zerop (len))
9006 return NULL_TREE;
9008 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9009 if (!fn)
9010 return NULL_TREE;
9012 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9013 fold_convert_loc (loc, size_type_node, len),
9014 build_int_cst (size_type_node, 1));
9015 /* We use dest twice in building our expression. Save it from
9016 multiple expansions. */
9017 dest = builtin_save_expr (dest);
9018 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9020 type = TREE_TYPE (TREE_TYPE (fndecl));
9021 dest = fold_build_pointer_plus_loc (loc, dest, len);
9022 dest = fold_convert_loc (loc, type, dest);
9023 dest = omit_one_operand_loc (loc, type, dest, call);
9024 return dest;
9027 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9028 If SLEN is not NULL, it represents the length of the source string.
9029 Return NULL_TREE if no simplification can be made. */
9031 tree
9032 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9033 tree src, tree len, tree slen)
9035 tree fn;
9037 if (!validate_arg (dest, POINTER_TYPE)
9038 || !validate_arg (src, POINTER_TYPE)
9039 || !validate_arg (len, INTEGER_TYPE))
9040 return NULL_TREE;
9042 /* If the LEN parameter is zero, return DEST. */
9043 if (integer_zerop (len))
9044 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9046 /* We can't compare slen with len as constants below if len is not a
9047 constant. */
9048 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9049 return NULL_TREE;
9051 if (!slen)
9052 slen = c_strlen (src, 1);
9054 /* Now, we must be passed a constant src ptr parameter. */
9055 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9056 return NULL_TREE;
9058 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9060 /* We do not support simplification of this case, though we do
9061 support it when expanding trees into RTL. */
9062 /* FIXME: generate a call to __builtin_memset. */
9063 if (tree_int_cst_lt (slen, len))
9064 return NULL_TREE;
9066 /* OK transform into builtin memcpy. */
9067 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9068 if (!fn)
9069 return NULL_TREE;
9071 len = fold_convert_loc (loc, size_type_node, len);
9072 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9073 build_call_expr_loc (loc, fn, 3, dest, src, len));
9076 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9077 arguments to the call, and TYPE is its return type.
9078 Return NULL_TREE if no simplification can be made. */
9080 static tree
9081 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9083 if (!validate_arg (arg1, POINTER_TYPE)
9084 || !validate_arg (arg2, INTEGER_TYPE)
9085 || !validate_arg (len, INTEGER_TYPE))
9086 return NULL_TREE;
9087 else
9089 const char *p1;
9091 if (TREE_CODE (arg2) != INTEGER_CST
9092 || !host_integerp (len, 1))
9093 return NULL_TREE;
9095 p1 = c_getstr (arg1);
9096 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9098 char c;
9099 const char *r;
9100 tree tem;
9102 if (target_char_cast (arg2, &c))
9103 return NULL_TREE;
9105 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9107 if (r == NULL)
9108 return build_int_cst (TREE_TYPE (arg1), 0);
9110 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9111 return fold_convert_loc (loc, type, tem);
9113 return NULL_TREE;
9117 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9118 Return NULL_TREE if no simplification can be made. */
9120 static tree
9121 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9123 const char *p1, *p2;
9125 if (!validate_arg (arg1, POINTER_TYPE)
9126 || !validate_arg (arg2, POINTER_TYPE)
9127 || !validate_arg (len, INTEGER_TYPE))
9128 return NULL_TREE;
9130 /* If the LEN parameter is zero, return zero. */
9131 if (integer_zerop (len))
9132 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9133 arg1, arg2);
9135 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9136 if (operand_equal_p (arg1, arg2, 0))
9137 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9139 p1 = c_getstr (arg1);
9140 p2 = c_getstr (arg2);
9142 /* If all arguments are constant, and the value of len is not greater
9143 than the lengths of arg1 and arg2, evaluate at compile-time. */
9144 if (host_integerp (len, 1) && p1 && p2
9145 && compare_tree_int (len, strlen (p1) + 1) <= 0
9146 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9148 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9150 if (r > 0)
9151 return integer_one_node;
9152 else if (r < 0)
9153 return integer_minus_one_node;
9154 else
9155 return integer_zero_node;
9158 /* If len parameter is one, return an expression corresponding to
9159 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9160 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9162 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9163 tree cst_uchar_ptr_node
9164 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9166 tree ind1
9167 = fold_convert_loc (loc, integer_type_node,
9168 build1 (INDIRECT_REF, cst_uchar_node,
9169 fold_convert_loc (loc,
9170 cst_uchar_ptr_node,
9171 arg1)));
9172 tree ind2
9173 = fold_convert_loc (loc, integer_type_node,
9174 build1 (INDIRECT_REF, cst_uchar_node,
9175 fold_convert_loc (loc,
9176 cst_uchar_ptr_node,
9177 arg2)));
9178 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9181 return NULL_TREE;
9184 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9185 Return NULL_TREE if no simplification can be made. */
9187 static tree
9188 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9190 const char *p1, *p2;
9192 if (!validate_arg (arg1, POINTER_TYPE)
9193 || !validate_arg (arg2, POINTER_TYPE))
9194 return NULL_TREE;
9196 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9197 if (operand_equal_p (arg1, arg2, 0))
9198 return integer_zero_node;
9200 p1 = c_getstr (arg1);
9201 p2 = c_getstr (arg2);
9203 if (p1 && p2)
9205 const int i = strcmp (p1, p2);
9206 if (i < 0)
9207 return integer_minus_one_node;
9208 else if (i > 0)
9209 return integer_one_node;
9210 else
9211 return integer_zero_node;
9214 /* If the second arg is "", return *(const unsigned char*)arg1. */
9215 if (p2 && *p2 == '\0')
9217 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9218 tree cst_uchar_ptr_node
9219 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9221 return fold_convert_loc (loc, integer_type_node,
9222 build1 (INDIRECT_REF, cst_uchar_node,
9223 fold_convert_loc (loc,
9224 cst_uchar_ptr_node,
9225 arg1)));
9228 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9229 if (p1 && *p1 == '\0')
9231 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9232 tree cst_uchar_ptr_node
9233 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9235 tree temp
9236 = fold_convert_loc (loc, integer_type_node,
9237 build1 (INDIRECT_REF, cst_uchar_node,
9238 fold_convert_loc (loc,
9239 cst_uchar_ptr_node,
9240 arg2)));
9241 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9244 return NULL_TREE;
9247 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9248 Return NULL_TREE if no simplification can be made. */
9250 static tree
9251 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9253 const char *p1, *p2;
9255 if (!validate_arg (arg1, POINTER_TYPE)
9256 || !validate_arg (arg2, POINTER_TYPE)
9257 || !validate_arg (len, INTEGER_TYPE))
9258 return NULL_TREE;
9260 /* If the LEN parameter is zero, return zero. */
9261 if (integer_zerop (len))
9262 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9263 arg1, arg2);
9265 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9266 if (operand_equal_p (arg1, arg2, 0))
9267 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9269 p1 = c_getstr (arg1);
9270 p2 = c_getstr (arg2);
9272 if (host_integerp (len, 1) && p1 && p2)
9274 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9275 if (i > 0)
9276 return integer_one_node;
9277 else if (i < 0)
9278 return integer_minus_one_node;
9279 else
9280 return integer_zero_node;
9283 /* If the second arg is "", and the length is greater than zero,
9284 return *(const unsigned char*)arg1. */
9285 if (p2 && *p2 == '\0'
9286 && TREE_CODE (len) == INTEGER_CST
9287 && tree_int_cst_sgn (len) == 1)
9289 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9290 tree cst_uchar_ptr_node
9291 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9293 return fold_convert_loc (loc, integer_type_node,
9294 build1 (INDIRECT_REF, cst_uchar_node,
9295 fold_convert_loc (loc,
9296 cst_uchar_ptr_node,
9297 arg1)));
9300 /* If the first arg is "", and the length is greater than zero,
9301 return -*(const unsigned char*)arg2. */
9302 if (p1 && *p1 == '\0'
9303 && TREE_CODE (len) == INTEGER_CST
9304 && tree_int_cst_sgn (len) == 1)
9306 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9307 tree cst_uchar_ptr_node
9308 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9310 tree temp = fold_convert_loc (loc, integer_type_node,
9311 build1 (INDIRECT_REF, cst_uchar_node,
9312 fold_convert_loc (loc,
9313 cst_uchar_ptr_node,
9314 arg2)));
9315 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9318 /* If len parameter is one, return an expression corresponding to
9319 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9320 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9322 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9323 tree cst_uchar_ptr_node
9324 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9326 tree ind1 = fold_convert_loc (loc, integer_type_node,
9327 build1 (INDIRECT_REF, cst_uchar_node,
9328 fold_convert_loc (loc,
9329 cst_uchar_ptr_node,
9330 arg1)));
9331 tree ind2 = fold_convert_loc (loc, integer_type_node,
9332 build1 (INDIRECT_REF, cst_uchar_node,
9333 fold_convert_loc (loc,
9334 cst_uchar_ptr_node,
9335 arg2)));
9336 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9339 return NULL_TREE;
9342 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9343 ARG. Return NULL_TREE if no simplification can be made. */
9345 static tree
9346 fold_builtin_signbit (location_t loc, tree arg, tree type)
9348 if (!validate_arg (arg, REAL_TYPE))
9349 return NULL_TREE;
9351 /* If ARG is a compile-time constant, determine the result. */
9352 if (TREE_CODE (arg) == REAL_CST
9353 && !TREE_OVERFLOW (arg))
9355 REAL_VALUE_TYPE c;
9357 c = TREE_REAL_CST (arg);
9358 return (REAL_VALUE_NEGATIVE (c)
9359 ? build_one_cst (type)
9360 : build_zero_cst (type));
9363 /* If ARG is non-negative, the result is always zero. */
9364 if (tree_expr_nonnegative_p (arg))
9365 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9367 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9368 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9369 return fold_convert (type,
9370 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9371 build_real (TREE_TYPE (arg), dconst0)));
9373 return NULL_TREE;
9376 /* Fold function call to builtin copysign, copysignf or copysignl with
9377 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9378 be made. */
9380 static tree
9381 fold_builtin_copysign (location_t loc, tree fndecl,
9382 tree arg1, tree arg2, tree type)
9384 tree tem;
9386 if (!validate_arg (arg1, REAL_TYPE)
9387 || !validate_arg (arg2, REAL_TYPE))
9388 return NULL_TREE;
9390 /* copysign(X,X) is X. */
9391 if (operand_equal_p (arg1, arg2, 0))
9392 return fold_convert_loc (loc, type, arg1);
9394 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9395 if (TREE_CODE (arg1) == REAL_CST
9396 && TREE_CODE (arg2) == REAL_CST
9397 && !TREE_OVERFLOW (arg1)
9398 && !TREE_OVERFLOW (arg2))
9400 REAL_VALUE_TYPE c1, c2;
9402 c1 = TREE_REAL_CST (arg1);
9403 c2 = TREE_REAL_CST (arg2);
9404 /* c1.sign := c2.sign. */
9405 real_copysign (&c1, &c2);
9406 return build_real (type, c1);
9409 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9410 Remember to evaluate Y for side-effects. */
9411 if (tree_expr_nonnegative_p (arg2))
9412 return omit_one_operand_loc (loc, type,
9413 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9414 arg2);
9416 /* Strip sign changing operations for the first argument. */
9417 tem = fold_strip_sign_ops (arg1);
9418 if (tem)
9419 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9421 return NULL_TREE;
9424 /* Fold a call to builtin isascii with argument ARG. */
9426 static tree
9427 fold_builtin_isascii (location_t loc, tree arg)
9429 if (!validate_arg (arg, INTEGER_TYPE))
9430 return NULL_TREE;
9431 else
9433 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9434 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9435 build_int_cst (integer_type_node,
9436 ~ (unsigned HOST_WIDE_INT) 0x7f));
9437 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9438 arg, integer_zero_node);
9442 /* Fold a call to builtin toascii with argument ARG. */
9444 static tree
9445 fold_builtin_toascii (location_t loc, tree arg)
9447 if (!validate_arg (arg, INTEGER_TYPE))
9448 return NULL_TREE;
9450 /* Transform toascii(c) -> (c & 0x7f). */
9451 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9452 build_int_cst (integer_type_node, 0x7f));
9455 /* Fold a call to builtin isdigit with argument ARG. */
9457 static tree
9458 fold_builtin_isdigit (location_t loc, tree arg)
9460 if (!validate_arg (arg, INTEGER_TYPE))
9461 return NULL_TREE;
9462 else
9464 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9465 /* According to the C standard, isdigit is unaffected by locale.
9466 However, it definitely is affected by the target character set. */
9467 unsigned HOST_WIDE_INT target_digit0
9468 = lang_hooks.to_target_charset ('0');
9470 if (target_digit0 == 0)
9471 return NULL_TREE;
9473 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9474 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9475 build_int_cst (unsigned_type_node, target_digit0));
9476 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9477 build_int_cst (unsigned_type_node, 9));
9481 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9483 static tree
9484 fold_builtin_fabs (location_t loc, tree arg, tree type)
9486 if (!validate_arg (arg, REAL_TYPE))
9487 return NULL_TREE;
9489 arg = fold_convert_loc (loc, type, arg);
9490 if (TREE_CODE (arg) == REAL_CST)
9491 return fold_abs_const (arg, type);
9492 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9495 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9497 static tree
9498 fold_builtin_abs (location_t loc, tree arg, tree type)
9500 if (!validate_arg (arg, INTEGER_TYPE))
9501 return NULL_TREE;
9503 arg = fold_convert_loc (loc, type, arg);
9504 if (TREE_CODE (arg) == INTEGER_CST)
9505 return fold_abs_const (arg, type);
9506 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9509 /* Fold a fma operation with arguments ARG[012]. */
9511 tree
9512 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9513 tree type, tree arg0, tree arg1, tree arg2)
9515 if (TREE_CODE (arg0) == REAL_CST
9516 && TREE_CODE (arg1) == REAL_CST
9517 && TREE_CODE (arg2) == REAL_CST)
9518 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9520 return NULL_TREE;
9523 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9525 static tree
9526 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9528 if (validate_arg (arg0, REAL_TYPE)
9529 && validate_arg(arg1, REAL_TYPE)
9530 && validate_arg(arg2, REAL_TYPE))
9532 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9533 if (tem)
9534 return tem;
9536 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9537 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9538 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9540 return NULL_TREE;
9543 /* Fold a call to builtin fmin or fmax. */
9545 static tree
9546 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9547 tree type, bool max)
9549 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9551 /* Calculate the result when the argument is a constant. */
9552 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9554 if (res)
9555 return res;
9557 /* If either argument is NaN, return the other one. Avoid the
9558 transformation if we get (and honor) a signalling NaN. Using
9559 omit_one_operand() ensures we create a non-lvalue. */
9560 if (TREE_CODE (arg0) == REAL_CST
9561 && real_isnan (&TREE_REAL_CST (arg0))
9562 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9563 || ! TREE_REAL_CST (arg0).signalling))
9564 return omit_one_operand_loc (loc, type, arg1, arg0);
9565 if (TREE_CODE (arg1) == REAL_CST
9566 && real_isnan (&TREE_REAL_CST (arg1))
9567 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9568 || ! TREE_REAL_CST (arg1).signalling))
9569 return omit_one_operand_loc (loc, type, arg0, arg1);
9571 /* Transform fmin/fmax(x,x) -> x. */
9572 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9573 return omit_one_operand_loc (loc, type, arg0, arg1);
9575 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9576 functions to return the numeric arg if the other one is NaN.
9577 These tree codes don't honor that, so only transform if
9578 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9579 handled, so we don't have to worry about it either. */
9580 if (flag_finite_math_only)
9581 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9582 fold_convert_loc (loc, type, arg0),
9583 fold_convert_loc (loc, type, arg1));
9585 return NULL_TREE;
9588 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9590 static tree
9591 fold_builtin_carg (location_t loc, tree arg, tree type)
9593 if (validate_arg (arg, COMPLEX_TYPE)
9594 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9596 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9598 if (atan2_fn)
9600 tree new_arg = builtin_save_expr (arg);
9601 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9602 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9603 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9607 return NULL_TREE;
9610 /* Fold a call to builtin logb/ilogb. */
9612 static tree
9613 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9615 if (! validate_arg (arg, REAL_TYPE))
9616 return NULL_TREE;
9618 STRIP_NOPS (arg);
9620 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9622 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9624 switch (value->cl)
9626 case rvc_nan:
9627 case rvc_inf:
9628 /* If arg is Inf or NaN and we're logb, return it. */
9629 if (TREE_CODE (rettype) == REAL_TYPE)
9630 return fold_convert_loc (loc, rettype, arg);
9631 /* Fall through... */
9632 case rvc_zero:
9633 /* Zero may set errno and/or raise an exception for logb, also
9634 for ilogb we don't know FP_ILOGB0. */
9635 return NULL_TREE;
9636 case rvc_normal:
9637 /* For normal numbers, proceed iff radix == 2. In GCC,
9638 normalized significands are in the range [0.5, 1.0). We
9639 want the exponent as if they were [1.0, 2.0) so get the
9640 exponent and subtract 1. */
9641 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9642 return fold_convert_loc (loc, rettype,
9643 build_int_cst (integer_type_node,
9644 REAL_EXP (value)-1));
9645 break;
9649 return NULL_TREE;
9652 /* Fold a call to builtin significand, if radix == 2. */
9654 static tree
9655 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9657 if (! validate_arg (arg, REAL_TYPE))
9658 return NULL_TREE;
9660 STRIP_NOPS (arg);
9662 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9664 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9666 switch (value->cl)
9668 case rvc_zero:
9669 case rvc_nan:
9670 case rvc_inf:
9671 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9672 return fold_convert_loc (loc, rettype, arg);
9673 case rvc_normal:
9674 /* For normal numbers, proceed iff radix == 2. */
9675 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9677 REAL_VALUE_TYPE result = *value;
9678 /* In GCC, normalized significands are in the range [0.5,
9679 1.0). We want them to be [1.0, 2.0) so set the
9680 exponent to 1. */
9681 SET_REAL_EXP (&result, 1);
9682 return build_real (rettype, result);
9684 break;
9688 return NULL_TREE;
9691 /* Fold a call to builtin frexp, we can assume the base is 2. */
9693 static tree
9694 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9696 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9697 return NULL_TREE;
9699 STRIP_NOPS (arg0);
9701 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9702 return NULL_TREE;
9704 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9706 /* Proceed if a valid pointer type was passed in. */
9707 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9709 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9710 tree frac, exp;
9712 switch (value->cl)
9714 case rvc_zero:
9715 /* For +-0, return (*exp = 0, +-0). */
9716 exp = integer_zero_node;
9717 frac = arg0;
9718 break;
9719 case rvc_nan:
9720 case rvc_inf:
9721 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9722 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9723 case rvc_normal:
9725 /* Since the frexp function always expects base 2, and in
9726 GCC normalized significands are already in the range
9727 [0.5, 1.0), we have exactly what frexp wants. */
9728 REAL_VALUE_TYPE frac_rvt = *value;
9729 SET_REAL_EXP (&frac_rvt, 0);
9730 frac = build_real (rettype, frac_rvt);
9731 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9733 break;
9734 default:
9735 gcc_unreachable ();
9738 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9739 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9740 TREE_SIDE_EFFECTS (arg1) = 1;
9741 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9744 return NULL_TREE;
9747 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9748 then we can assume the base is two. If it's false, then we have to
9749 check the mode of the TYPE parameter in certain cases. */
9751 static tree
9752 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9753 tree type, bool ldexp)
9755 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9757 STRIP_NOPS (arg0);
9758 STRIP_NOPS (arg1);
9760 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9761 if (real_zerop (arg0) || integer_zerop (arg1)
9762 || (TREE_CODE (arg0) == REAL_CST
9763 && !real_isfinite (&TREE_REAL_CST (arg0))))
9764 return omit_one_operand_loc (loc, type, arg0, arg1);
9766 /* If both arguments are constant, then try to evaluate it. */
9767 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9768 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9769 && host_integerp (arg1, 0))
9771 /* Bound the maximum adjustment to twice the range of the
9772 mode's valid exponents. Use abs to ensure the range is
9773 positive as a sanity check. */
9774 const long max_exp_adj = 2 *
9775 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9776 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9778 /* Get the user-requested adjustment. */
9779 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9781 /* The requested adjustment must be inside this range. This
9782 is a preliminary cap to avoid things like overflow, we
9783 may still fail to compute the result for other reasons. */
9784 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9786 REAL_VALUE_TYPE initial_result;
9788 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9790 /* Ensure we didn't overflow. */
9791 if (! real_isinf (&initial_result))
9793 const REAL_VALUE_TYPE trunc_result
9794 = real_value_truncate (TYPE_MODE (type), initial_result);
9796 /* Only proceed if the target mode can hold the
9797 resulting value. */
9798 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9799 return build_real (type, trunc_result);
9805 return NULL_TREE;
9808 /* Fold a call to builtin modf. */
9810 static tree
9811 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9813 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9814 return NULL_TREE;
9816 STRIP_NOPS (arg0);
9818 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9819 return NULL_TREE;
9821 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9823 /* Proceed if a valid pointer type was passed in. */
9824 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9826 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9827 REAL_VALUE_TYPE trunc, frac;
9829 switch (value->cl)
9831 case rvc_nan:
9832 case rvc_zero:
9833 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9834 trunc = frac = *value;
9835 break;
9836 case rvc_inf:
9837 /* For +-Inf, return (*arg1 = arg0, +-0). */
9838 frac = dconst0;
9839 frac.sign = value->sign;
9840 trunc = *value;
9841 break;
9842 case rvc_normal:
9843 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9844 real_trunc (&trunc, VOIDmode, value);
9845 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9846 /* If the original number was negative and already
9847 integral, then the fractional part is -0.0. */
9848 if (value->sign && frac.cl == rvc_zero)
9849 frac.sign = value->sign;
9850 break;
9853 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9854 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9855 build_real (rettype, trunc));
9856 TREE_SIDE_EFFECTS (arg1) = 1;
9857 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9858 build_real (rettype, frac));
9861 return NULL_TREE;
9864 /* Given a location LOC, an interclass builtin function decl FNDECL
9865 and its single argument ARG, return an folded expression computing
9866 the same, or NULL_TREE if we either couldn't or didn't want to fold
9867 (the latter happen if there's an RTL instruction available). */
9869 static tree
9870 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9872 enum machine_mode mode;
9874 if (!validate_arg (arg, REAL_TYPE))
9875 return NULL_TREE;
9877 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9878 return NULL_TREE;
9880 mode = TYPE_MODE (TREE_TYPE (arg));
9882 /* If there is no optab, try generic code. */
9883 switch (DECL_FUNCTION_CODE (fndecl))
9885 tree result;
9887 CASE_FLT_FN (BUILT_IN_ISINF):
9889 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9890 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9891 tree const type = TREE_TYPE (arg);
9892 REAL_VALUE_TYPE r;
9893 char buf[128];
9895 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9896 real_from_string (&r, buf);
9897 result = build_call_expr (isgr_fn, 2,
9898 fold_build1_loc (loc, ABS_EXPR, type, arg),
9899 build_real (type, r));
9900 return result;
9902 CASE_FLT_FN (BUILT_IN_FINITE):
9903 case BUILT_IN_ISFINITE:
9905 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9906 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9907 tree const type = TREE_TYPE (arg);
9908 REAL_VALUE_TYPE r;
9909 char buf[128];
9911 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9912 real_from_string (&r, buf);
9913 result = build_call_expr (isle_fn, 2,
9914 fold_build1_loc (loc, ABS_EXPR, type, arg),
9915 build_real (type, r));
9916 /*result = fold_build2_loc (loc, UNGT_EXPR,
9917 TREE_TYPE (TREE_TYPE (fndecl)),
9918 fold_build1_loc (loc, ABS_EXPR, type, arg),
9919 build_real (type, r));
9920 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9921 TREE_TYPE (TREE_TYPE (fndecl)),
9922 result);*/
9923 return result;
9925 case BUILT_IN_ISNORMAL:
9927 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9928 islessequal(fabs(x),DBL_MAX). */
9929 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9930 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9931 tree const type = TREE_TYPE (arg);
9932 REAL_VALUE_TYPE rmax, rmin;
9933 char buf[128];
9935 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9936 real_from_string (&rmax, buf);
9937 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9938 real_from_string (&rmin, buf);
9939 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9940 result = build_call_expr (isle_fn, 2, arg,
9941 build_real (type, rmax));
9942 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9943 build_call_expr (isge_fn, 2, arg,
9944 build_real (type, rmin)));
9945 return result;
9947 default:
9948 break;
9951 return NULL_TREE;
9954 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9955 ARG is the argument for the call. */
9957 static tree
9958 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9960 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9961 REAL_VALUE_TYPE r;
9963 if (!validate_arg (arg, REAL_TYPE))
9964 return NULL_TREE;
9966 switch (builtin_index)
9968 case BUILT_IN_ISINF:
9969 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9970 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9972 if (TREE_CODE (arg) == REAL_CST)
9974 r = TREE_REAL_CST (arg);
9975 if (real_isinf (&r))
9976 return real_compare (GT_EXPR, &r, &dconst0)
9977 ? integer_one_node : integer_minus_one_node;
9978 else
9979 return integer_zero_node;
9982 return NULL_TREE;
9984 case BUILT_IN_ISINF_SIGN:
9986 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9987 /* In a boolean context, GCC will fold the inner COND_EXPR to
9988 1. So e.g. "if (isinf_sign(x))" would be folded to just
9989 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9990 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9991 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9992 tree tmp = NULL_TREE;
9994 arg = builtin_save_expr (arg);
9996 if (signbit_fn && isinf_fn)
9998 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9999 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10001 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10002 signbit_call, integer_zero_node);
10003 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10004 isinf_call, integer_zero_node);
10006 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10007 integer_minus_one_node, integer_one_node);
10008 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10009 isinf_call, tmp,
10010 integer_zero_node);
10013 return tmp;
10016 case BUILT_IN_ISFINITE:
10017 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10018 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10019 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10021 if (TREE_CODE (arg) == REAL_CST)
10023 r = TREE_REAL_CST (arg);
10024 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10027 return NULL_TREE;
10029 case BUILT_IN_ISNAN:
10030 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10031 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10033 if (TREE_CODE (arg) == REAL_CST)
10035 r = TREE_REAL_CST (arg);
10036 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10039 arg = builtin_save_expr (arg);
10040 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10042 default:
10043 gcc_unreachable ();
10047 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10048 This builtin will generate code to return the appropriate floating
10049 point classification depending on the value of the floating point
10050 number passed in. The possible return values must be supplied as
10051 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10052 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10053 one floating point argument which is "type generic". */
10055 static tree
10056 fold_builtin_fpclassify (location_t loc, tree exp)
10058 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10059 arg, type, res, tmp;
10060 enum machine_mode mode;
10061 REAL_VALUE_TYPE r;
10062 char buf[128];
10064 /* Verify the required arguments in the original call. */
10065 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10066 INTEGER_TYPE, INTEGER_TYPE,
10067 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10068 return NULL_TREE;
10070 fp_nan = CALL_EXPR_ARG (exp, 0);
10071 fp_infinite = CALL_EXPR_ARG (exp, 1);
10072 fp_normal = CALL_EXPR_ARG (exp, 2);
10073 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10074 fp_zero = CALL_EXPR_ARG (exp, 4);
10075 arg = CALL_EXPR_ARG (exp, 5);
10076 type = TREE_TYPE (arg);
10077 mode = TYPE_MODE (type);
10078 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10080 /* fpclassify(x) ->
10081 isnan(x) ? FP_NAN :
10082 (fabs(x) == Inf ? FP_INFINITE :
10083 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10084 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10086 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10087 build_real (type, dconst0));
10088 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10089 tmp, fp_zero, fp_subnormal);
10091 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10092 real_from_string (&r, buf);
10093 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10094 arg, build_real (type, r));
10095 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10097 if (HONOR_INFINITIES (mode))
10099 real_inf (&r);
10100 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10101 build_real (type, r));
10102 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10103 fp_infinite, res);
10106 if (HONOR_NANS (mode))
10108 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10109 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10112 return res;
10115 /* Fold a call to an unordered comparison function such as
10116 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10117 being called and ARG0 and ARG1 are the arguments for the call.
10118 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10119 the opposite of the desired result. UNORDERED_CODE is used
10120 for modes that can hold NaNs and ORDERED_CODE is used for
10121 the rest. */
10123 static tree
10124 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10125 enum tree_code unordered_code,
10126 enum tree_code ordered_code)
10128 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10129 enum tree_code code;
10130 tree type0, type1;
10131 enum tree_code code0, code1;
10132 tree cmp_type = NULL_TREE;
10134 type0 = TREE_TYPE (arg0);
10135 type1 = TREE_TYPE (arg1);
10137 code0 = TREE_CODE (type0);
10138 code1 = TREE_CODE (type1);
10140 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10141 /* Choose the wider of two real types. */
10142 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10143 ? type0 : type1;
10144 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10145 cmp_type = type0;
10146 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10147 cmp_type = type1;
10149 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10150 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10152 if (unordered_code == UNORDERED_EXPR)
10154 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10155 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10156 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10159 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10160 : ordered_code;
10161 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10162 fold_build2_loc (loc, code, type, arg0, arg1));
10165 /* Fold a call to built-in function FNDECL with 0 arguments.
10166 IGNORE is true if the result of the function call is ignored. This
10167 function returns NULL_TREE if no simplification was possible. */
10169 static tree
10170 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10172 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10173 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10174 switch (fcode)
10176 CASE_FLT_FN (BUILT_IN_INF):
10177 case BUILT_IN_INFD32:
10178 case BUILT_IN_INFD64:
10179 case BUILT_IN_INFD128:
10180 return fold_builtin_inf (loc, type, true);
10182 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10183 return fold_builtin_inf (loc, type, false);
10185 case BUILT_IN_CLASSIFY_TYPE:
10186 return fold_builtin_classify_type (NULL_TREE);
10188 default:
10189 break;
10191 return NULL_TREE;
10194 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10195 IGNORE is true if the result of the function call is ignored. This
10196 function returns NULL_TREE if no simplification was possible. */
10198 static tree
10199 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10201 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10202 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10203 switch (fcode)
10205 case BUILT_IN_CONSTANT_P:
10207 tree val = fold_builtin_constant_p (arg0);
10209 /* Gimplification will pull the CALL_EXPR for the builtin out of
10210 an if condition. When not optimizing, we'll not CSE it back.
10211 To avoid link error types of regressions, return false now. */
10212 if (!val && !optimize)
10213 val = integer_zero_node;
10215 return val;
10218 case BUILT_IN_CLASSIFY_TYPE:
10219 return fold_builtin_classify_type (arg0);
10221 case BUILT_IN_STRLEN:
10222 return fold_builtin_strlen (loc, type, arg0);
10224 CASE_FLT_FN (BUILT_IN_FABS):
10225 return fold_builtin_fabs (loc, arg0, type);
10227 case BUILT_IN_ABS:
10228 case BUILT_IN_LABS:
10229 case BUILT_IN_LLABS:
10230 case BUILT_IN_IMAXABS:
10231 return fold_builtin_abs (loc, arg0, type);
10233 CASE_FLT_FN (BUILT_IN_CONJ):
10234 if (validate_arg (arg0, COMPLEX_TYPE)
10235 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10236 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10237 break;
10239 CASE_FLT_FN (BUILT_IN_CREAL):
10240 if (validate_arg (arg0, COMPLEX_TYPE)
10241 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10242 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10243 break;
10245 CASE_FLT_FN (BUILT_IN_CIMAG):
10246 if (validate_arg (arg0, COMPLEX_TYPE)
10247 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10248 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10249 break;
10251 CASE_FLT_FN (BUILT_IN_CCOS):
10252 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10254 CASE_FLT_FN (BUILT_IN_CCOSH):
10255 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10257 CASE_FLT_FN (BUILT_IN_CPROJ):
10258 return fold_builtin_cproj(loc, arg0, type);
10260 CASE_FLT_FN (BUILT_IN_CSIN):
10261 if (validate_arg (arg0, COMPLEX_TYPE)
10262 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10263 return do_mpc_arg1 (arg0, type, mpc_sin);
10264 break;
10266 CASE_FLT_FN (BUILT_IN_CSINH):
10267 if (validate_arg (arg0, COMPLEX_TYPE)
10268 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10269 return do_mpc_arg1 (arg0, type, mpc_sinh);
10270 break;
10272 CASE_FLT_FN (BUILT_IN_CTAN):
10273 if (validate_arg (arg0, COMPLEX_TYPE)
10274 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10275 return do_mpc_arg1 (arg0, type, mpc_tan);
10276 break;
10278 CASE_FLT_FN (BUILT_IN_CTANH):
10279 if (validate_arg (arg0, COMPLEX_TYPE)
10280 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10281 return do_mpc_arg1 (arg0, type, mpc_tanh);
10282 break;
10284 CASE_FLT_FN (BUILT_IN_CLOG):
10285 if (validate_arg (arg0, COMPLEX_TYPE)
10286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10287 return do_mpc_arg1 (arg0, type, mpc_log);
10288 break;
10290 CASE_FLT_FN (BUILT_IN_CSQRT):
10291 if (validate_arg (arg0, COMPLEX_TYPE)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10293 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10294 break;
10296 CASE_FLT_FN (BUILT_IN_CASIN):
10297 if (validate_arg (arg0, COMPLEX_TYPE)
10298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10299 return do_mpc_arg1 (arg0, type, mpc_asin);
10300 break;
10302 CASE_FLT_FN (BUILT_IN_CACOS):
10303 if (validate_arg (arg0, COMPLEX_TYPE)
10304 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10305 return do_mpc_arg1 (arg0, type, mpc_acos);
10306 break;
10308 CASE_FLT_FN (BUILT_IN_CATAN):
10309 if (validate_arg (arg0, COMPLEX_TYPE)
10310 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10311 return do_mpc_arg1 (arg0, type, mpc_atan);
10312 break;
10314 CASE_FLT_FN (BUILT_IN_CASINH):
10315 if (validate_arg (arg0, COMPLEX_TYPE)
10316 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10317 return do_mpc_arg1 (arg0, type, mpc_asinh);
10318 break;
10320 CASE_FLT_FN (BUILT_IN_CACOSH):
10321 if (validate_arg (arg0, COMPLEX_TYPE)
10322 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10323 return do_mpc_arg1 (arg0, type, mpc_acosh);
10324 break;
10326 CASE_FLT_FN (BUILT_IN_CATANH):
10327 if (validate_arg (arg0, COMPLEX_TYPE)
10328 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10329 return do_mpc_arg1 (arg0, type, mpc_atanh);
10330 break;
10332 CASE_FLT_FN (BUILT_IN_CABS):
10333 return fold_builtin_cabs (loc, arg0, type, fndecl);
10335 CASE_FLT_FN (BUILT_IN_CARG):
10336 return fold_builtin_carg (loc, arg0, type);
10338 CASE_FLT_FN (BUILT_IN_SQRT):
10339 return fold_builtin_sqrt (loc, arg0, type);
10341 CASE_FLT_FN (BUILT_IN_CBRT):
10342 return fold_builtin_cbrt (loc, arg0, type);
10344 CASE_FLT_FN (BUILT_IN_ASIN):
10345 if (validate_arg (arg0, REAL_TYPE))
10346 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10347 &dconstm1, &dconst1, true);
10348 break;
10350 CASE_FLT_FN (BUILT_IN_ACOS):
10351 if (validate_arg (arg0, REAL_TYPE))
10352 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10353 &dconstm1, &dconst1, true);
10354 break;
10356 CASE_FLT_FN (BUILT_IN_ATAN):
10357 if (validate_arg (arg0, REAL_TYPE))
10358 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10359 break;
10361 CASE_FLT_FN (BUILT_IN_ASINH):
10362 if (validate_arg (arg0, REAL_TYPE))
10363 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10364 break;
10366 CASE_FLT_FN (BUILT_IN_ACOSH):
10367 if (validate_arg (arg0, REAL_TYPE))
10368 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10369 &dconst1, NULL, true);
10370 break;
10372 CASE_FLT_FN (BUILT_IN_ATANH):
10373 if (validate_arg (arg0, REAL_TYPE))
10374 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10375 &dconstm1, &dconst1, false);
10376 break;
10378 CASE_FLT_FN (BUILT_IN_SIN):
10379 if (validate_arg (arg0, REAL_TYPE))
10380 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10381 break;
10383 CASE_FLT_FN (BUILT_IN_COS):
10384 return fold_builtin_cos (loc, arg0, type, fndecl);
10386 CASE_FLT_FN (BUILT_IN_TAN):
10387 return fold_builtin_tan (arg0, type);
10389 CASE_FLT_FN (BUILT_IN_CEXP):
10390 return fold_builtin_cexp (loc, arg0, type);
10392 CASE_FLT_FN (BUILT_IN_CEXPI):
10393 if (validate_arg (arg0, REAL_TYPE))
10394 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10395 break;
10397 CASE_FLT_FN (BUILT_IN_SINH):
10398 if (validate_arg (arg0, REAL_TYPE))
10399 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10400 break;
10402 CASE_FLT_FN (BUILT_IN_COSH):
10403 return fold_builtin_cosh (loc, arg0, type, fndecl);
10405 CASE_FLT_FN (BUILT_IN_TANH):
10406 if (validate_arg (arg0, REAL_TYPE))
10407 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10408 break;
10410 CASE_FLT_FN (BUILT_IN_ERF):
10411 if (validate_arg (arg0, REAL_TYPE))
10412 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10413 break;
10415 CASE_FLT_FN (BUILT_IN_ERFC):
10416 if (validate_arg (arg0, REAL_TYPE))
10417 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10418 break;
10420 CASE_FLT_FN (BUILT_IN_TGAMMA):
10421 if (validate_arg (arg0, REAL_TYPE))
10422 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10423 break;
10425 CASE_FLT_FN (BUILT_IN_EXP):
10426 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10428 CASE_FLT_FN (BUILT_IN_EXP2):
10429 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10431 CASE_FLT_FN (BUILT_IN_EXP10):
10432 CASE_FLT_FN (BUILT_IN_POW10):
10433 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10435 CASE_FLT_FN (BUILT_IN_EXPM1):
10436 if (validate_arg (arg0, REAL_TYPE))
10437 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10438 break;
10440 CASE_FLT_FN (BUILT_IN_LOG):
10441 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10443 CASE_FLT_FN (BUILT_IN_LOG2):
10444 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10446 CASE_FLT_FN (BUILT_IN_LOG10):
10447 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10449 CASE_FLT_FN (BUILT_IN_LOG1P):
10450 if (validate_arg (arg0, REAL_TYPE))
10451 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10452 &dconstm1, NULL, false);
10453 break;
10455 CASE_FLT_FN (BUILT_IN_J0):
10456 if (validate_arg (arg0, REAL_TYPE))
10457 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10458 NULL, NULL, 0);
10459 break;
10461 CASE_FLT_FN (BUILT_IN_J1):
10462 if (validate_arg (arg0, REAL_TYPE))
10463 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10464 NULL, NULL, 0);
10465 break;
10467 CASE_FLT_FN (BUILT_IN_Y0):
10468 if (validate_arg (arg0, REAL_TYPE))
10469 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10470 &dconst0, NULL, false);
10471 break;
10473 CASE_FLT_FN (BUILT_IN_Y1):
10474 if (validate_arg (arg0, REAL_TYPE))
10475 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10476 &dconst0, NULL, false);
10477 break;
10479 CASE_FLT_FN (BUILT_IN_NAN):
10480 case BUILT_IN_NAND32:
10481 case BUILT_IN_NAND64:
10482 case BUILT_IN_NAND128:
10483 return fold_builtin_nan (arg0, type, true);
10485 CASE_FLT_FN (BUILT_IN_NANS):
10486 return fold_builtin_nan (arg0, type, false);
10488 CASE_FLT_FN (BUILT_IN_FLOOR):
10489 return fold_builtin_floor (loc, fndecl, arg0);
10491 CASE_FLT_FN (BUILT_IN_CEIL):
10492 return fold_builtin_ceil (loc, fndecl, arg0);
10494 CASE_FLT_FN (BUILT_IN_TRUNC):
10495 return fold_builtin_trunc (loc, fndecl, arg0);
10497 CASE_FLT_FN (BUILT_IN_ROUND):
10498 return fold_builtin_round (loc, fndecl, arg0);
10500 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10501 CASE_FLT_FN (BUILT_IN_RINT):
10502 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10504 CASE_FLT_FN (BUILT_IN_ICEIL):
10505 CASE_FLT_FN (BUILT_IN_LCEIL):
10506 CASE_FLT_FN (BUILT_IN_LLCEIL):
10507 CASE_FLT_FN (BUILT_IN_LFLOOR):
10508 CASE_FLT_FN (BUILT_IN_IFLOOR):
10509 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10510 CASE_FLT_FN (BUILT_IN_IROUND):
10511 CASE_FLT_FN (BUILT_IN_LROUND):
10512 CASE_FLT_FN (BUILT_IN_LLROUND):
10513 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10515 CASE_FLT_FN (BUILT_IN_IRINT):
10516 CASE_FLT_FN (BUILT_IN_LRINT):
10517 CASE_FLT_FN (BUILT_IN_LLRINT):
10518 return fold_fixed_mathfn (loc, fndecl, arg0);
10520 case BUILT_IN_BSWAP32:
10521 case BUILT_IN_BSWAP64:
10522 return fold_builtin_bswap (fndecl, arg0);
10524 CASE_INT_FN (BUILT_IN_FFS):
10525 CASE_INT_FN (BUILT_IN_CLZ):
10526 CASE_INT_FN (BUILT_IN_CTZ):
10527 CASE_INT_FN (BUILT_IN_CLRSB):
10528 CASE_INT_FN (BUILT_IN_POPCOUNT):
10529 CASE_INT_FN (BUILT_IN_PARITY):
10530 return fold_builtin_bitop (fndecl, arg0);
10532 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10533 return fold_builtin_signbit (loc, arg0, type);
10535 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10536 return fold_builtin_significand (loc, arg0, type);
10538 CASE_FLT_FN (BUILT_IN_ILOGB):
10539 CASE_FLT_FN (BUILT_IN_LOGB):
10540 return fold_builtin_logb (loc, arg0, type);
10542 case BUILT_IN_ISASCII:
10543 return fold_builtin_isascii (loc, arg0);
10545 case BUILT_IN_TOASCII:
10546 return fold_builtin_toascii (loc, arg0);
10548 case BUILT_IN_ISDIGIT:
10549 return fold_builtin_isdigit (loc, arg0);
10551 CASE_FLT_FN (BUILT_IN_FINITE):
10552 case BUILT_IN_FINITED32:
10553 case BUILT_IN_FINITED64:
10554 case BUILT_IN_FINITED128:
10555 case BUILT_IN_ISFINITE:
10557 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10558 if (ret)
10559 return ret;
10560 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10563 CASE_FLT_FN (BUILT_IN_ISINF):
10564 case BUILT_IN_ISINFD32:
10565 case BUILT_IN_ISINFD64:
10566 case BUILT_IN_ISINFD128:
10568 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10569 if (ret)
10570 return ret;
10571 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10574 case BUILT_IN_ISNORMAL:
10575 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10577 case BUILT_IN_ISINF_SIGN:
10578 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10580 CASE_FLT_FN (BUILT_IN_ISNAN):
10581 case BUILT_IN_ISNAND32:
10582 case BUILT_IN_ISNAND64:
10583 case BUILT_IN_ISNAND128:
10584 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10586 case BUILT_IN_PRINTF:
10587 case BUILT_IN_PRINTF_UNLOCKED:
10588 case BUILT_IN_VPRINTF:
10589 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10591 case BUILT_IN_FREE:
10592 if (integer_zerop (arg0))
10593 return build_empty_stmt (loc);
10594 break;
10596 default:
10597 break;
10600 return NULL_TREE;
10604 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10605 IGNORE is true if the result of the function call is ignored. This
10606 function returns NULL_TREE if no simplification was possible. */
10608 static tree
10609 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10611 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10612 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10614 switch (fcode)
10616 CASE_FLT_FN (BUILT_IN_JN):
10617 if (validate_arg (arg0, INTEGER_TYPE)
10618 && validate_arg (arg1, REAL_TYPE))
10619 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10620 break;
10622 CASE_FLT_FN (BUILT_IN_YN):
10623 if (validate_arg (arg0, INTEGER_TYPE)
10624 && validate_arg (arg1, REAL_TYPE))
10625 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10626 &dconst0, false);
10627 break;
10629 CASE_FLT_FN (BUILT_IN_DREM):
10630 CASE_FLT_FN (BUILT_IN_REMAINDER):
10631 if (validate_arg (arg0, REAL_TYPE)
10632 && validate_arg(arg1, REAL_TYPE))
10633 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10634 break;
10636 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10637 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10638 if (validate_arg (arg0, REAL_TYPE)
10639 && validate_arg(arg1, POINTER_TYPE))
10640 return do_mpfr_lgamma_r (arg0, arg1, type);
10641 break;
10643 CASE_FLT_FN (BUILT_IN_ATAN2):
10644 if (validate_arg (arg0, REAL_TYPE)
10645 && validate_arg(arg1, REAL_TYPE))
10646 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10647 break;
10649 CASE_FLT_FN (BUILT_IN_FDIM):
10650 if (validate_arg (arg0, REAL_TYPE)
10651 && validate_arg(arg1, REAL_TYPE))
10652 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10653 break;
10655 CASE_FLT_FN (BUILT_IN_HYPOT):
10656 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10658 CASE_FLT_FN (BUILT_IN_CPOW):
10659 if (validate_arg (arg0, COMPLEX_TYPE)
10660 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10661 && validate_arg (arg1, COMPLEX_TYPE)
10662 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10663 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10664 break;
10666 CASE_FLT_FN (BUILT_IN_LDEXP):
10667 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10668 CASE_FLT_FN (BUILT_IN_SCALBN):
10669 CASE_FLT_FN (BUILT_IN_SCALBLN):
10670 return fold_builtin_load_exponent (loc, arg0, arg1,
10671 type, /*ldexp=*/false);
10673 CASE_FLT_FN (BUILT_IN_FREXP):
10674 return fold_builtin_frexp (loc, arg0, arg1, type);
10676 CASE_FLT_FN (BUILT_IN_MODF):
10677 return fold_builtin_modf (loc, arg0, arg1, type);
10679 case BUILT_IN_BZERO:
10680 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10682 case BUILT_IN_FPUTS:
10683 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10685 case BUILT_IN_FPUTS_UNLOCKED:
10686 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10688 case BUILT_IN_STRSTR:
10689 return fold_builtin_strstr (loc, arg0, arg1, type);
10691 case BUILT_IN_STRCAT:
10692 return fold_builtin_strcat (loc, arg0, arg1);
10694 case BUILT_IN_STRSPN:
10695 return fold_builtin_strspn (loc, arg0, arg1);
10697 case BUILT_IN_STRCSPN:
10698 return fold_builtin_strcspn (loc, arg0, arg1);
10700 case BUILT_IN_STRCHR:
10701 case BUILT_IN_INDEX:
10702 return fold_builtin_strchr (loc, arg0, arg1, type);
10704 case BUILT_IN_STRRCHR:
10705 case BUILT_IN_RINDEX:
10706 return fold_builtin_strrchr (loc, arg0, arg1, type);
10708 case BUILT_IN_STRCPY:
10709 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10711 case BUILT_IN_STPCPY:
10712 if (ignore)
10714 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10715 if (!fn)
10716 break;
10718 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10720 else
10721 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10722 break;
10724 case BUILT_IN_STRCMP:
10725 return fold_builtin_strcmp (loc, arg0, arg1);
10727 case BUILT_IN_STRPBRK:
10728 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10730 case BUILT_IN_EXPECT:
10731 return fold_builtin_expect (loc, arg0, arg1);
10733 CASE_FLT_FN (BUILT_IN_POW):
10734 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10736 CASE_FLT_FN (BUILT_IN_POWI):
10737 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10739 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10740 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10742 CASE_FLT_FN (BUILT_IN_FMIN):
10743 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10745 CASE_FLT_FN (BUILT_IN_FMAX):
10746 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10748 case BUILT_IN_ISGREATER:
10749 return fold_builtin_unordered_cmp (loc, fndecl,
10750 arg0, arg1, UNLE_EXPR, LE_EXPR);
10751 case BUILT_IN_ISGREATEREQUAL:
10752 return fold_builtin_unordered_cmp (loc, fndecl,
10753 arg0, arg1, UNLT_EXPR, LT_EXPR);
10754 case BUILT_IN_ISLESS:
10755 return fold_builtin_unordered_cmp (loc, fndecl,
10756 arg0, arg1, UNGE_EXPR, GE_EXPR);
10757 case BUILT_IN_ISLESSEQUAL:
10758 return fold_builtin_unordered_cmp (loc, fndecl,
10759 arg0, arg1, UNGT_EXPR, GT_EXPR);
10760 case BUILT_IN_ISLESSGREATER:
10761 return fold_builtin_unordered_cmp (loc, fndecl,
10762 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10763 case BUILT_IN_ISUNORDERED:
10764 return fold_builtin_unordered_cmp (loc, fndecl,
10765 arg0, arg1, UNORDERED_EXPR,
10766 NOP_EXPR);
10768 /* We do the folding for va_start in the expander. */
10769 case BUILT_IN_VA_START:
10770 break;
10772 case BUILT_IN_SPRINTF:
10773 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10775 case BUILT_IN_OBJECT_SIZE:
10776 return fold_builtin_object_size (arg0, arg1);
10778 case BUILT_IN_PRINTF:
10779 case BUILT_IN_PRINTF_UNLOCKED:
10780 case BUILT_IN_VPRINTF:
10781 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10783 case BUILT_IN_PRINTF_CHK:
10784 case BUILT_IN_VPRINTF_CHK:
10785 if (!validate_arg (arg0, INTEGER_TYPE)
10786 || TREE_SIDE_EFFECTS (arg0))
10787 return NULL_TREE;
10788 else
10789 return fold_builtin_printf (loc, fndecl,
10790 arg1, NULL_TREE, ignore, fcode);
10791 break;
10793 case BUILT_IN_FPRINTF:
10794 case BUILT_IN_FPRINTF_UNLOCKED:
10795 case BUILT_IN_VFPRINTF:
10796 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10797 ignore, fcode);
10799 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10800 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10802 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10803 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10805 default:
10806 break;
10808 return NULL_TREE;
10811 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10812 and ARG2. IGNORE is true if the result of the function call is ignored.
10813 This function returns NULL_TREE if no simplification was possible. */
10815 static tree
10816 fold_builtin_3 (location_t loc, tree fndecl,
10817 tree arg0, tree arg1, tree arg2, bool ignore)
10819 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10820 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10821 switch (fcode)
10824 CASE_FLT_FN (BUILT_IN_SINCOS):
10825 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10827 CASE_FLT_FN (BUILT_IN_FMA):
10828 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10829 break;
10831 CASE_FLT_FN (BUILT_IN_REMQUO):
10832 if (validate_arg (arg0, REAL_TYPE)
10833 && validate_arg(arg1, REAL_TYPE)
10834 && validate_arg(arg2, POINTER_TYPE))
10835 return do_mpfr_remquo (arg0, arg1, arg2);
10836 break;
10838 case BUILT_IN_MEMSET:
10839 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10841 case BUILT_IN_BCOPY:
10842 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10843 void_type_node, true, /*endp=*/3);
10845 case BUILT_IN_MEMCPY:
10846 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10847 type, ignore, /*endp=*/0);
10849 case BUILT_IN_MEMPCPY:
10850 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10851 type, ignore, /*endp=*/1);
10853 case BUILT_IN_MEMMOVE:
10854 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10855 type, ignore, /*endp=*/3);
10857 case BUILT_IN_STRNCAT:
10858 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10860 case BUILT_IN_STRNCPY:
10861 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10863 case BUILT_IN_STRNCMP:
10864 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10866 case BUILT_IN_MEMCHR:
10867 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10869 case BUILT_IN_BCMP:
10870 case BUILT_IN_MEMCMP:
10871 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10873 case BUILT_IN_SPRINTF:
10874 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10876 case BUILT_IN_SNPRINTF:
10877 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10879 case BUILT_IN_STRCPY_CHK:
10880 case BUILT_IN_STPCPY_CHK:
10881 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10882 ignore, fcode);
10884 case BUILT_IN_STRCAT_CHK:
10885 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10887 case BUILT_IN_PRINTF_CHK:
10888 case BUILT_IN_VPRINTF_CHK:
10889 if (!validate_arg (arg0, INTEGER_TYPE)
10890 || TREE_SIDE_EFFECTS (arg0))
10891 return NULL_TREE;
10892 else
10893 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10894 break;
10896 case BUILT_IN_FPRINTF:
10897 case BUILT_IN_FPRINTF_UNLOCKED:
10898 case BUILT_IN_VFPRINTF:
10899 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10900 ignore, fcode);
10902 case BUILT_IN_FPRINTF_CHK:
10903 case BUILT_IN_VFPRINTF_CHK:
10904 if (!validate_arg (arg1, INTEGER_TYPE)
10905 || TREE_SIDE_EFFECTS (arg1))
10906 return NULL_TREE;
10907 else
10908 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10909 ignore, fcode);
10911 default:
10912 break;
10914 return NULL_TREE;
10917 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10918 ARG2, and ARG3. IGNORE is true if the result of the function call is
10919 ignored. This function returns NULL_TREE if no simplification was
10920 possible. */
10922 static tree
10923 fold_builtin_4 (location_t loc, tree fndecl,
10924 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10926 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10928 switch (fcode)
10930 case BUILT_IN_MEMCPY_CHK:
10931 case BUILT_IN_MEMPCPY_CHK:
10932 case BUILT_IN_MEMMOVE_CHK:
10933 case BUILT_IN_MEMSET_CHK:
10934 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10935 NULL_TREE, ignore,
10936 DECL_FUNCTION_CODE (fndecl));
10938 case BUILT_IN_STRNCPY_CHK:
10939 case BUILT_IN_STPNCPY_CHK:
10940 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10941 ignore, fcode);
10943 case BUILT_IN_STRNCAT_CHK:
10944 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10946 case BUILT_IN_SNPRINTF:
10947 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10949 case BUILT_IN_FPRINTF_CHK:
10950 case BUILT_IN_VFPRINTF_CHK:
10951 if (!validate_arg (arg1, INTEGER_TYPE)
10952 || TREE_SIDE_EFFECTS (arg1))
10953 return NULL_TREE;
10954 else
10955 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10956 ignore, fcode);
10957 break;
10959 default:
10960 break;
10962 return NULL_TREE;
10965 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10966 arguments, where NARGS <= 4. IGNORE is true if the result of the
10967 function call is ignored. This function returns NULL_TREE if no
10968 simplification was possible. Note that this only folds builtins with
10969 fixed argument patterns. Foldings that do varargs-to-varargs
10970 transformations, or that match calls with more than 4 arguments,
10971 need to be handled with fold_builtin_varargs instead. */
10973 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10975 static tree
10976 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10978 tree ret = NULL_TREE;
10980 switch (nargs)
10982 case 0:
10983 ret = fold_builtin_0 (loc, fndecl, ignore);
10984 break;
10985 case 1:
10986 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10987 break;
10988 case 2:
10989 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10990 break;
10991 case 3:
10992 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10993 break;
10994 case 4:
10995 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10996 ignore);
10997 break;
10998 default:
10999 break;
11001 if (ret)
11003 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11004 SET_EXPR_LOCATION (ret, loc);
11005 TREE_NO_WARNING (ret) = 1;
11006 return ret;
11008 return NULL_TREE;
11011 /* Builtins with folding operations that operate on "..." arguments
11012 need special handling; we need to store the arguments in a convenient
11013 data structure before attempting any folding. Fortunately there are
11014 only a few builtins that fall into this category. FNDECL is the
11015 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11016 result of the function call is ignored. */
11018 static tree
11019 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11020 bool ignore ATTRIBUTE_UNUSED)
11022 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11023 tree ret = NULL_TREE;
11025 switch (fcode)
11027 case BUILT_IN_SPRINTF_CHK:
11028 case BUILT_IN_VSPRINTF_CHK:
11029 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11030 break;
11032 case BUILT_IN_SNPRINTF_CHK:
11033 case BUILT_IN_VSNPRINTF_CHK:
11034 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11035 break;
11037 case BUILT_IN_FPCLASSIFY:
11038 ret = fold_builtin_fpclassify (loc, exp);
11039 break;
11041 default:
11042 break;
11044 if (ret)
11046 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11047 SET_EXPR_LOCATION (ret, loc);
11048 TREE_NO_WARNING (ret) = 1;
11049 return ret;
11051 return NULL_TREE;
11054 /* Return true if FNDECL shouldn't be folded right now.
11055 If a built-in function has an inline attribute always_inline
11056 wrapper, defer folding it after always_inline functions have
11057 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11058 might not be performed. */
11060 bool
11061 avoid_folding_inline_builtin (tree fndecl)
11063 return (DECL_DECLARED_INLINE_P (fndecl)
11064 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11065 && cfun
11066 && !cfun->always_inline_functions_inlined
11067 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11070 /* A wrapper function for builtin folding that prevents warnings for
11071 "statement without effect" and the like, caused by removing the
11072 call node earlier than the warning is generated. */
11074 tree
11075 fold_call_expr (location_t loc, tree exp, bool ignore)
11077 tree ret = NULL_TREE;
11078 tree fndecl = get_callee_fndecl (exp);
11079 if (fndecl
11080 && TREE_CODE (fndecl) == FUNCTION_DECL
11081 && DECL_BUILT_IN (fndecl)
11082 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11083 yet. Defer folding until we see all the arguments
11084 (after inlining). */
11085 && !CALL_EXPR_VA_ARG_PACK (exp))
11087 int nargs = call_expr_nargs (exp);
11089 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11090 instead last argument is __builtin_va_arg_pack (). Defer folding
11091 even in that case, until arguments are finalized. */
11092 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11094 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11095 if (fndecl2
11096 && TREE_CODE (fndecl2) == FUNCTION_DECL
11097 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11098 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11099 return NULL_TREE;
11102 if (avoid_folding_inline_builtin (fndecl))
11103 return NULL_TREE;
11105 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11106 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11107 CALL_EXPR_ARGP (exp), ignore);
11108 else
11110 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11112 tree *args = CALL_EXPR_ARGP (exp);
11113 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11115 if (!ret)
11116 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11117 if (ret)
11118 return ret;
11121 return NULL_TREE;
11124 /* Conveniently construct a function call expression. FNDECL names the
11125 function to be called and N arguments are passed in the array
11126 ARGARRAY. */
11128 tree
11129 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11131 tree fntype = TREE_TYPE (fndecl);
11132 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11134 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11137 /* Conveniently construct a function call expression. FNDECL names the
11138 function to be called and the arguments are passed in the vector
11139 VEC. */
11141 tree
11142 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11144 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11145 VEC_address (tree, vec));
11149 /* Conveniently construct a function call expression. FNDECL names the
11150 function to be called, N is the number of arguments, and the "..."
11151 parameters are the argument expressions. */
11153 tree
11154 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11156 va_list ap;
11157 tree *argarray = XALLOCAVEC (tree, n);
11158 int i;
11160 va_start (ap, n);
11161 for (i = 0; i < n; i++)
11162 argarray[i] = va_arg (ap, tree);
11163 va_end (ap);
11164 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11167 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11168 varargs macros aren't supported by all bootstrap compilers. */
11170 tree
11171 build_call_expr (tree fndecl, int n, ...)
11173 va_list ap;
11174 tree *argarray = XALLOCAVEC (tree, n);
11175 int i;
11177 va_start (ap, n);
11178 for (i = 0; i < n; i++)
11179 argarray[i] = va_arg (ap, tree);
11180 va_end (ap);
11181 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11184 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11185 N arguments are passed in the array ARGARRAY. */
11187 tree
11188 fold_builtin_call_array (location_t loc, tree type,
11189 tree fn,
11190 int n,
11191 tree *argarray)
11193 tree ret = NULL_TREE;
11194 tree exp;
11196 if (TREE_CODE (fn) == ADDR_EXPR)
11198 tree fndecl = TREE_OPERAND (fn, 0);
11199 if (TREE_CODE (fndecl) == FUNCTION_DECL
11200 && DECL_BUILT_IN (fndecl))
11202 /* If last argument is __builtin_va_arg_pack (), arguments to this
11203 function are not finalized yet. Defer folding until they are. */
11204 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11206 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11207 if (fndecl2
11208 && TREE_CODE (fndecl2) == FUNCTION_DECL
11209 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11210 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11211 return build_call_array_loc (loc, type, fn, n, argarray);
11213 if (avoid_folding_inline_builtin (fndecl))
11214 return build_call_array_loc (loc, type, fn, n, argarray);
11215 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11217 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11218 if (ret)
11219 return ret;
11221 return build_call_array_loc (loc, type, fn, n, argarray);
11223 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11225 /* First try the transformations that don't require consing up
11226 an exp. */
11227 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11228 if (ret)
11229 return ret;
11232 /* If we got this far, we need to build an exp. */
11233 exp = build_call_array_loc (loc, type, fn, n, argarray);
11234 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11235 return ret ? ret : exp;
11239 return build_call_array_loc (loc, type, fn, n, argarray);
11242 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11243 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11244 of arguments in ARGS to be omitted. OLDNARGS is the number of
11245 elements in ARGS. */
11247 static tree
11248 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11249 int skip, tree fndecl, int n, va_list newargs)
11251 int nargs = oldnargs - skip + n;
11252 tree *buffer;
11254 if (n > 0)
11256 int i, j;
11258 buffer = XALLOCAVEC (tree, nargs);
11259 for (i = 0; i < n; i++)
11260 buffer[i] = va_arg (newargs, tree);
11261 for (j = skip; j < oldnargs; j++, i++)
11262 buffer[i] = args[j];
11264 else
11265 buffer = args + skip;
11267 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11270 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11271 list ARGS along with N new arguments specified as the "..."
11272 parameters. SKIP is the number of arguments in ARGS to be omitted.
11273 OLDNARGS is the number of elements in ARGS. */
11275 static tree
11276 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11277 int skip, tree fndecl, int n, ...)
11279 va_list ap;
11280 tree t;
11282 va_start (ap, n);
11283 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11284 va_end (ap);
11286 return t;
11289 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11290 along with N new arguments specified as the "..." parameters. SKIP
11291 is the number of arguments in EXP to be omitted. This function is used
11292 to do varargs-to-varargs transformations. */
11294 static tree
11295 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11297 va_list ap;
11298 tree t;
11300 va_start (ap, n);
11301 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11302 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11303 va_end (ap);
11305 return t;
11308 /* Validate a single argument ARG against a tree code CODE representing
11309 a type. */
11311 static bool
11312 validate_arg (const_tree arg, enum tree_code code)
11314 if (!arg)
11315 return false;
11316 else if (code == POINTER_TYPE)
11317 return POINTER_TYPE_P (TREE_TYPE (arg));
11318 else if (code == INTEGER_TYPE)
11319 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11320 return code == TREE_CODE (TREE_TYPE (arg));
11323 /* This function validates the types of a function call argument list
11324 against a specified list of tree_codes. If the last specifier is a 0,
11325 that represents an ellipses, otherwise the last specifier must be a
11326 VOID_TYPE.
11328 This is the GIMPLE version of validate_arglist. Eventually we want to
11329 completely convert builtins.c to work from GIMPLEs and the tree based
11330 validate_arglist will then be removed. */
11332 bool
11333 validate_gimple_arglist (const_gimple call, ...)
11335 enum tree_code code;
11336 bool res = 0;
11337 va_list ap;
11338 const_tree arg;
11339 size_t i;
11341 va_start (ap, call);
11342 i = 0;
11346 code = (enum tree_code) va_arg (ap, int);
11347 switch (code)
11349 case 0:
11350 /* This signifies an ellipses, any further arguments are all ok. */
11351 res = true;
11352 goto end;
11353 case VOID_TYPE:
11354 /* This signifies an endlink, if no arguments remain, return
11355 true, otherwise return false. */
11356 res = (i == gimple_call_num_args (call));
11357 goto end;
11358 default:
11359 /* If no parameters remain or the parameter's code does not
11360 match the specified code, return false. Otherwise continue
11361 checking any remaining arguments. */
11362 arg = gimple_call_arg (call, i++);
11363 if (!validate_arg (arg, code))
11364 goto end;
11365 break;
11368 while (1);
11370 /* We need gotos here since we can only have one VA_CLOSE in a
11371 function. */
11372 end: ;
11373 va_end (ap);
11375 return res;
11378 /* This function validates the types of a function call argument list
11379 against a specified list of tree_codes. If the last specifier is a 0,
11380 that represents an ellipses, otherwise the last specifier must be a
11381 VOID_TYPE. */
11383 bool
11384 validate_arglist (const_tree callexpr, ...)
11386 enum tree_code code;
11387 bool res = 0;
11388 va_list ap;
11389 const_call_expr_arg_iterator iter;
11390 const_tree arg;
11392 va_start (ap, callexpr);
11393 init_const_call_expr_arg_iterator (callexpr, &iter);
11397 code = (enum tree_code) va_arg (ap, int);
11398 switch (code)
11400 case 0:
11401 /* This signifies an ellipses, any further arguments are all ok. */
11402 res = true;
11403 goto end;
11404 case VOID_TYPE:
11405 /* This signifies an endlink, if no arguments remain, return
11406 true, otherwise return false. */
11407 res = !more_const_call_expr_args_p (&iter);
11408 goto end;
11409 default:
11410 /* If no parameters remain or the parameter's code does not
11411 match the specified code, return false. Otherwise continue
11412 checking any remaining arguments. */
11413 arg = next_const_call_expr_arg (&iter);
11414 if (!validate_arg (arg, code))
11415 goto end;
11416 break;
11419 while (1);
11421 /* We need gotos here since we can only have one VA_CLOSE in a
11422 function. */
11423 end: ;
11424 va_end (ap);
11426 return res;
11429 /* Default target-specific builtin expander that does nothing. */
11432 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11433 rtx target ATTRIBUTE_UNUSED,
11434 rtx subtarget ATTRIBUTE_UNUSED,
11435 enum machine_mode mode ATTRIBUTE_UNUSED,
11436 int ignore ATTRIBUTE_UNUSED)
11438 return NULL_RTX;
11441 /* Returns true is EXP represents data that would potentially reside
11442 in a readonly section. */
11444 static bool
11445 readonly_data_expr (tree exp)
11447 STRIP_NOPS (exp);
11449 if (TREE_CODE (exp) != ADDR_EXPR)
11450 return false;
11452 exp = get_base_address (TREE_OPERAND (exp, 0));
11453 if (!exp)
11454 return false;
11456 /* Make sure we call decl_readonly_section only for trees it
11457 can handle (since it returns true for everything it doesn't
11458 understand). */
11459 if (TREE_CODE (exp) == STRING_CST
11460 || TREE_CODE (exp) == CONSTRUCTOR
11461 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11462 return decl_readonly_section (exp, 0);
11463 else
11464 return false;
11467 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11468 to the call, and TYPE is its return type.
11470 Return NULL_TREE if no simplification was possible, otherwise return the
11471 simplified form of the call as a tree.
11473 The simplified form may be a constant or other expression which
11474 computes the same value, but in a more efficient manner (including
11475 calls to other builtin functions).
11477 The call may contain arguments which need to be evaluated, but
11478 which are not useful to determine the result of the call. In
11479 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11480 COMPOUND_EXPR will be an argument which must be evaluated.
11481 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11482 COMPOUND_EXPR in the chain will contain the tree for the simplified
11483 form of the builtin function call. */
11485 static tree
11486 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11488 if (!validate_arg (s1, POINTER_TYPE)
11489 || !validate_arg (s2, POINTER_TYPE))
11490 return NULL_TREE;
11491 else
11493 tree fn;
11494 const char *p1, *p2;
11496 p2 = c_getstr (s2);
11497 if (p2 == NULL)
11498 return NULL_TREE;
11500 p1 = c_getstr (s1);
11501 if (p1 != NULL)
11503 const char *r = strstr (p1, p2);
11504 tree tem;
11506 if (r == NULL)
11507 return build_int_cst (TREE_TYPE (s1), 0);
11509 /* Return an offset into the constant string argument. */
11510 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11511 return fold_convert_loc (loc, type, tem);
11514 /* The argument is const char *, and the result is char *, so we need
11515 a type conversion here to avoid a warning. */
11516 if (p2[0] == '\0')
11517 return fold_convert_loc (loc, type, s1);
11519 if (p2[1] != '\0')
11520 return NULL_TREE;
11522 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11523 if (!fn)
11524 return NULL_TREE;
11526 /* New argument list transforming strstr(s1, s2) to
11527 strchr(s1, s2[0]). */
11528 return build_call_expr_loc (loc, fn, 2, s1,
11529 build_int_cst (integer_type_node, p2[0]));
11533 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11534 the call, and TYPE is its return type.
11536 Return NULL_TREE if no simplification was possible, otherwise return the
11537 simplified form of the call as a tree.
11539 The simplified form may be a constant or other expression which
11540 computes the same value, but in a more efficient manner (including
11541 calls to other builtin functions).
11543 The call may contain arguments which need to be evaluated, but
11544 which are not useful to determine the result of the call. In
11545 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11546 COMPOUND_EXPR will be an argument which must be evaluated.
11547 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11548 COMPOUND_EXPR in the chain will contain the tree for the simplified
11549 form of the builtin function call. */
11551 static tree
11552 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11554 if (!validate_arg (s1, POINTER_TYPE)
11555 || !validate_arg (s2, INTEGER_TYPE))
11556 return NULL_TREE;
11557 else
11559 const char *p1;
11561 if (TREE_CODE (s2) != INTEGER_CST)
11562 return NULL_TREE;
11564 p1 = c_getstr (s1);
11565 if (p1 != NULL)
11567 char c;
11568 const char *r;
11569 tree tem;
11571 if (target_char_cast (s2, &c))
11572 return NULL_TREE;
11574 r = strchr (p1, c);
11576 if (r == NULL)
11577 return build_int_cst (TREE_TYPE (s1), 0);
11579 /* Return an offset into the constant string argument. */
11580 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11581 return fold_convert_loc (loc, type, tem);
11583 return NULL_TREE;
11587 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11588 the call, and TYPE is its return type.
11590 Return NULL_TREE if no simplification was possible, otherwise return the
11591 simplified form of the call as a tree.
11593 The simplified form may be a constant or other expression which
11594 computes the same value, but in a more efficient manner (including
11595 calls to other builtin functions).
11597 The call may contain arguments which need to be evaluated, but
11598 which are not useful to determine the result of the call. In
11599 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11600 COMPOUND_EXPR will be an argument which must be evaluated.
11601 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11602 COMPOUND_EXPR in the chain will contain the tree for the simplified
11603 form of the builtin function call. */
11605 static tree
11606 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11608 if (!validate_arg (s1, POINTER_TYPE)
11609 || !validate_arg (s2, INTEGER_TYPE))
11610 return NULL_TREE;
11611 else
11613 tree fn;
11614 const char *p1;
11616 if (TREE_CODE (s2) != INTEGER_CST)
11617 return NULL_TREE;
11619 p1 = c_getstr (s1);
11620 if (p1 != NULL)
11622 char c;
11623 const char *r;
11624 tree tem;
11626 if (target_char_cast (s2, &c))
11627 return NULL_TREE;
11629 r = strrchr (p1, c);
11631 if (r == NULL)
11632 return build_int_cst (TREE_TYPE (s1), 0);
11634 /* Return an offset into the constant string argument. */
11635 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11636 return fold_convert_loc (loc, type, tem);
11639 if (! integer_zerop (s2))
11640 return NULL_TREE;
11642 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11643 if (!fn)
11644 return NULL_TREE;
11646 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11647 return build_call_expr_loc (loc, fn, 2, s1, s2);
11651 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11652 to the call, and TYPE is its return type.
11654 Return NULL_TREE if no simplification was possible, otherwise return the
11655 simplified form of the call as a tree.
11657 The simplified form may be a constant or other expression which
11658 computes the same value, but in a more efficient manner (including
11659 calls to other builtin functions).
11661 The call may contain arguments which need to be evaluated, but
11662 which are not useful to determine the result of the call. In
11663 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11664 COMPOUND_EXPR will be an argument which must be evaluated.
11665 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11666 COMPOUND_EXPR in the chain will contain the tree for the simplified
11667 form of the builtin function call. */
11669 static tree
11670 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11672 if (!validate_arg (s1, POINTER_TYPE)
11673 || !validate_arg (s2, POINTER_TYPE))
11674 return NULL_TREE;
11675 else
11677 tree fn;
11678 const char *p1, *p2;
11680 p2 = c_getstr (s2);
11681 if (p2 == NULL)
11682 return NULL_TREE;
11684 p1 = c_getstr (s1);
11685 if (p1 != NULL)
11687 const char *r = strpbrk (p1, p2);
11688 tree tem;
11690 if (r == NULL)
11691 return build_int_cst (TREE_TYPE (s1), 0);
11693 /* Return an offset into the constant string argument. */
11694 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11695 return fold_convert_loc (loc, type, tem);
11698 if (p2[0] == '\0')
11699 /* strpbrk(x, "") == NULL.
11700 Evaluate and ignore s1 in case it had side-effects. */
11701 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11703 if (p2[1] != '\0')
11704 return NULL_TREE; /* Really call strpbrk. */
11706 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11707 if (!fn)
11708 return NULL_TREE;
11710 /* New argument list transforming strpbrk(s1, s2) to
11711 strchr(s1, s2[0]). */
11712 return build_call_expr_loc (loc, fn, 2, s1,
11713 build_int_cst (integer_type_node, p2[0]));
11717 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11718 to the call.
11720 Return NULL_TREE if no simplification was possible, otherwise return the
11721 simplified form of the call as a tree.
11723 The simplified form may be a constant or other expression which
11724 computes the same value, but in a more efficient manner (including
11725 calls to other builtin functions).
11727 The call may contain arguments which need to be evaluated, but
11728 which are not useful to determine the result of the call. In
11729 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11730 COMPOUND_EXPR will be an argument which must be evaluated.
11731 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11732 COMPOUND_EXPR in the chain will contain the tree for the simplified
11733 form of the builtin function call. */
11735 static tree
11736 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11738 if (!validate_arg (dst, POINTER_TYPE)
11739 || !validate_arg (src, POINTER_TYPE))
11740 return NULL_TREE;
11741 else
11743 const char *p = c_getstr (src);
11745 /* If the string length is zero, return the dst parameter. */
11746 if (p && *p == '\0')
11747 return dst;
11749 if (optimize_insn_for_speed_p ())
11751 /* See if we can store by pieces into (dst + strlen(dst)). */
11752 tree newdst, call;
11753 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11754 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11756 if (!strlen_fn || !strcpy_fn)
11757 return NULL_TREE;
11759 /* If we don't have a movstr we don't want to emit an strcpy
11760 call. We have to do that if the length of the source string
11761 isn't computable (in that case we can use memcpy probably
11762 later expanding to a sequence of mov instructions). If we
11763 have movstr instructions we can emit strcpy calls. */
11764 if (!HAVE_movstr)
11766 tree len = c_strlen (src, 1);
11767 if (! len || TREE_SIDE_EFFECTS (len))
11768 return NULL_TREE;
11771 /* Stabilize the argument list. */
11772 dst = builtin_save_expr (dst);
11774 /* Create strlen (dst). */
11775 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11776 /* Create (dst p+ strlen (dst)). */
11778 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11779 newdst = builtin_save_expr (newdst);
11781 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11782 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11784 return NULL_TREE;
11788 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11789 arguments to the call.
11791 Return NULL_TREE if no simplification was possible, otherwise return the
11792 simplified form of the call as a tree.
11794 The simplified form may be a constant or other expression which
11795 computes the same value, but in a more efficient manner (including
11796 calls to other builtin functions).
11798 The call may contain arguments which need to be evaluated, but
11799 which are not useful to determine the result of the call. In
11800 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11801 COMPOUND_EXPR will be an argument which must be evaluated.
11802 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11803 COMPOUND_EXPR in the chain will contain the tree for the simplified
11804 form of the builtin function call. */
11806 static tree
11807 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11809 if (!validate_arg (dst, POINTER_TYPE)
11810 || !validate_arg (src, POINTER_TYPE)
11811 || !validate_arg (len, INTEGER_TYPE))
11812 return NULL_TREE;
11813 else
11815 const char *p = c_getstr (src);
11817 /* If the requested length is zero, or the src parameter string
11818 length is zero, return the dst parameter. */
11819 if (integer_zerop (len) || (p && *p == '\0'))
11820 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11822 /* If the requested len is greater than or equal to the string
11823 length, call strcat. */
11824 if (TREE_CODE (len) == INTEGER_CST && p
11825 && compare_tree_int (len, strlen (p)) >= 0)
11827 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11829 /* If the replacement _DECL isn't initialized, don't do the
11830 transformation. */
11831 if (!fn)
11832 return NULL_TREE;
11834 return build_call_expr_loc (loc, fn, 2, dst, src);
11836 return NULL_TREE;
11840 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11841 to the call.
11843 Return NULL_TREE if no simplification was possible, otherwise return the
11844 simplified form of the call as a tree.
11846 The simplified form may be a constant or other expression which
11847 computes the same value, but in a more efficient manner (including
11848 calls to other builtin functions).
11850 The call may contain arguments which need to be evaluated, but
11851 which are not useful to determine the result of the call. In
11852 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11853 COMPOUND_EXPR will be an argument which must be evaluated.
11854 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11855 COMPOUND_EXPR in the chain will contain the tree for the simplified
11856 form of the builtin function call. */
11858 static tree
11859 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11861 if (!validate_arg (s1, POINTER_TYPE)
11862 || !validate_arg (s2, POINTER_TYPE))
11863 return NULL_TREE;
11864 else
11866 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11868 /* If both arguments are constants, evaluate at compile-time. */
11869 if (p1 && p2)
11871 const size_t r = strspn (p1, p2);
11872 return size_int (r);
11875 /* If either argument is "", return NULL_TREE. */
11876 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11877 /* Evaluate and ignore both arguments in case either one has
11878 side-effects. */
11879 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11880 s1, s2);
11881 return NULL_TREE;
11885 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11886 to the call.
11888 Return NULL_TREE if no simplification was possible, otherwise return the
11889 simplified form of the call as a tree.
11891 The simplified form may be a constant or other expression which
11892 computes the same value, but in a more efficient manner (including
11893 calls to other builtin functions).
11895 The call may contain arguments which need to be evaluated, but
11896 which are not useful to determine the result of the call. In
11897 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11898 COMPOUND_EXPR will be an argument which must be evaluated.
11899 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11900 COMPOUND_EXPR in the chain will contain the tree for the simplified
11901 form of the builtin function call. */
11903 static tree
11904 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11906 if (!validate_arg (s1, POINTER_TYPE)
11907 || !validate_arg (s2, POINTER_TYPE))
11908 return NULL_TREE;
11909 else
11911 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11913 /* If both arguments are constants, evaluate at compile-time. */
11914 if (p1 && p2)
11916 const size_t r = strcspn (p1, p2);
11917 return size_int (r);
11920 /* If the first argument is "", return NULL_TREE. */
11921 if (p1 && *p1 == '\0')
11923 /* Evaluate and ignore argument s2 in case it has
11924 side-effects. */
11925 return omit_one_operand_loc (loc, size_type_node,
11926 size_zero_node, s2);
11929 /* If the second argument is "", return __builtin_strlen(s1). */
11930 if (p2 && *p2 == '\0')
11932 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11934 /* If the replacement _DECL isn't initialized, don't do the
11935 transformation. */
11936 if (!fn)
11937 return NULL_TREE;
11939 return build_call_expr_loc (loc, fn, 1, s1);
11941 return NULL_TREE;
11945 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11946 to the call. IGNORE is true if the value returned
11947 by the builtin will be ignored. UNLOCKED is true is true if this
11948 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11949 the known length of the string. Return NULL_TREE if no simplification
11950 was possible. */
11952 tree
11953 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11954 bool ignore, bool unlocked, tree len)
11956 /* If we're using an unlocked function, assume the other unlocked
11957 functions exist explicitly. */
11958 tree const fn_fputc = (unlocked
11959 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11960 : builtin_decl_implicit (BUILT_IN_FPUTC));
11961 tree const fn_fwrite = (unlocked
11962 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11963 : builtin_decl_implicit (BUILT_IN_FWRITE));
11965 /* If the return value is used, don't do the transformation. */
11966 if (!ignore)
11967 return NULL_TREE;
11969 /* Verify the arguments in the original call. */
11970 if (!validate_arg (arg0, POINTER_TYPE)
11971 || !validate_arg (arg1, POINTER_TYPE))
11972 return NULL_TREE;
11974 if (! len)
11975 len = c_strlen (arg0, 0);
11977 /* Get the length of the string passed to fputs. If the length
11978 can't be determined, punt. */
11979 if (!len
11980 || TREE_CODE (len) != INTEGER_CST)
11981 return NULL_TREE;
11983 switch (compare_tree_int (len, 1))
11985 case -1: /* length is 0, delete the call entirely . */
11986 return omit_one_operand_loc (loc, integer_type_node,
11987 integer_zero_node, arg1);;
11989 case 0: /* length is 1, call fputc. */
11991 const char *p = c_getstr (arg0);
11993 if (p != NULL)
11995 if (fn_fputc)
11996 return build_call_expr_loc (loc, fn_fputc, 2,
11997 build_int_cst
11998 (integer_type_node, p[0]), arg1);
11999 else
12000 return NULL_TREE;
12003 /* FALLTHROUGH */
12004 case 1: /* length is greater than 1, call fwrite. */
12006 /* If optimizing for size keep fputs. */
12007 if (optimize_function_for_size_p (cfun))
12008 return NULL_TREE;
12009 /* New argument list transforming fputs(string, stream) to
12010 fwrite(string, 1, len, stream). */
12011 if (fn_fwrite)
12012 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12013 size_one_node, len, arg1);
12014 else
12015 return NULL_TREE;
12017 default:
12018 gcc_unreachable ();
12020 return NULL_TREE;
12023 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12024 produced. False otherwise. This is done so that we don't output the error
12025 or warning twice or three times. */
12027 bool
12028 fold_builtin_next_arg (tree exp, bool va_start_p)
12030 tree fntype = TREE_TYPE (current_function_decl);
12031 int nargs = call_expr_nargs (exp);
12032 tree arg;
12034 if (!stdarg_p (fntype))
12036 error ("%<va_start%> used in function with fixed args");
12037 return true;
12040 if (va_start_p)
12042 if (va_start_p && (nargs != 2))
12044 error ("wrong number of arguments to function %<va_start%>");
12045 return true;
12047 arg = CALL_EXPR_ARG (exp, 1);
12049 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12050 when we checked the arguments and if needed issued a warning. */
12051 else
12053 if (nargs == 0)
12055 /* Evidently an out of date version of <stdarg.h>; can't validate
12056 va_start's second argument, but can still work as intended. */
12057 warning (0, "%<__builtin_next_arg%> called without an argument");
12058 return true;
12060 else if (nargs > 1)
12062 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12063 return true;
12065 arg = CALL_EXPR_ARG (exp, 0);
12068 if (TREE_CODE (arg) == SSA_NAME)
12069 arg = SSA_NAME_VAR (arg);
12071 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12072 or __builtin_next_arg (0) the first time we see it, after checking
12073 the arguments and if needed issuing a warning. */
12074 if (!integer_zerop (arg))
12076 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12078 /* Strip off all nops for the sake of the comparison. This
12079 is not quite the same as STRIP_NOPS. It does more.
12080 We must also strip off INDIRECT_EXPR for C++ reference
12081 parameters. */
12082 while (CONVERT_EXPR_P (arg)
12083 || TREE_CODE (arg) == INDIRECT_REF)
12084 arg = TREE_OPERAND (arg, 0);
12085 if (arg != last_parm)
12087 /* FIXME: Sometimes with the tree optimizers we can get the
12088 not the last argument even though the user used the last
12089 argument. We just warn and set the arg to be the last
12090 argument so that we will get wrong-code because of
12091 it. */
12092 warning (0, "second parameter of %<va_start%> not last named argument");
12095 /* Undefined by C99 7.15.1.4p4 (va_start):
12096 "If the parameter parmN is declared with the register storage
12097 class, with a function or array type, or with a type that is
12098 not compatible with the type that results after application of
12099 the default argument promotions, the behavior is undefined."
12101 else if (DECL_REGISTER (arg))
12102 warning (0, "undefined behaviour when second parameter of "
12103 "%<va_start%> is declared with %<register%> storage");
12105 /* We want to verify the second parameter just once before the tree
12106 optimizers are run and then avoid keeping it in the tree,
12107 as otherwise we could warn even for correct code like:
12108 void foo (int i, ...)
12109 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12110 if (va_start_p)
12111 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12112 else
12113 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12115 return false;
12119 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12120 ORIG may be null if this is a 2-argument call. We don't attempt to
12121 simplify calls with more than 3 arguments.
12123 Return NULL_TREE if no simplification was possible, otherwise return the
12124 simplified form of the call as a tree. If IGNORED is true, it means that
12125 the caller does not use the returned value of the function. */
12127 static tree
12128 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12129 tree orig, int ignored)
12131 tree call, retval;
12132 const char *fmt_str = NULL;
12134 /* Verify the required arguments in the original call. We deal with two
12135 types of sprintf() calls: 'sprintf (str, fmt)' and
12136 'sprintf (dest, "%s", orig)'. */
12137 if (!validate_arg (dest, POINTER_TYPE)
12138 || !validate_arg (fmt, POINTER_TYPE))
12139 return NULL_TREE;
12140 if (orig && !validate_arg (orig, POINTER_TYPE))
12141 return NULL_TREE;
12143 /* Check whether the format is a literal string constant. */
12144 fmt_str = c_getstr (fmt);
12145 if (fmt_str == NULL)
12146 return NULL_TREE;
12148 call = NULL_TREE;
12149 retval = NULL_TREE;
12151 if (!init_target_chars ())
12152 return NULL_TREE;
12154 /* If the format doesn't contain % args or %%, use strcpy. */
12155 if (strchr (fmt_str, target_percent) == NULL)
12157 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12159 if (!fn)
12160 return NULL_TREE;
12162 /* Don't optimize sprintf (buf, "abc", ptr++). */
12163 if (orig)
12164 return NULL_TREE;
12166 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12167 'format' is known to contain no % formats. */
12168 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12169 if (!ignored)
12170 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12173 /* If the format is "%s", use strcpy if the result isn't used. */
12174 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12176 tree fn;
12177 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12179 if (!fn)
12180 return NULL_TREE;
12182 /* Don't crash on sprintf (str1, "%s"). */
12183 if (!orig)
12184 return NULL_TREE;
12186 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12187 if (!ignored)
12189 retval = c_strlen (orig, 1);
12190 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12191 return NULL_TREE;
12193 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12196 if (call && retval)
12198 retval = fold_convert_loc
12199 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12200 retval);
12201 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12203 else
12204 return call;
12207 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12208 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12209 attempt to simplify calls with more than 4 arguments.
12211 Return NULL_TREE if no simplification was possible, otherwise return the
12212 simplified form of the call as a tree. If IGNORED is true, it means that
12213 the caller does not use the returned value of the function. */
12215 static tree
12216 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12217 tree orig, int ignored)
12219 tree call, retval;
12220 const char *fmt_str = NULL;
12221 unsigned HOST_WIDE_INT destlen;
12223 /* Verify the required arguments in the original call. We deal with two
12224 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12225 'snprintf (dest, cst, "%s", orig)'. */
12226 if (!validate_arg (dest, POINTER_TYPE)
12227 || !validate_arg (destsize, INTEGER_TYPE)
12228 || !validate_arg (fmt, POINTER_TYPE))
12229 return NULL_TREE;
12230 if (orig && !validate_arg (orig, POINTER_TYPE))
12231 return NULL_TREE;
12233 if (!host_integerp (destsize, 1))
12234 return NULL_TREE;
12236 /* Check whether the format is a literal string constant. */
12237 fmt_str = c_getstr (fmt);
12238 if (fmt_str == NULL)
12239 return NULL_TREE;
12241 call = NULL_TREE;
12242 retval = NULL_TREE;
12244 if (!init_target_chars ())
12245 return NULL_TREE;
12247 destlen = tree_low_cst (destsize, 1);
12249 /* If the format doesn't contain % args or %%, use strcpy. */
12250 if (strchr (fmt_str, target_percent) == NULL)
12252 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12253 size_t len = strlen (fmt_str);
12255 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12256 if (orig)
12257 return NULL_TREE;
12259 /* We could expand this as
12260 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12261 or to
12262 memcpy (str, fmt_with_nul_at_cstm1, cst);
12263 but in the former case that might increase code size
12264 and in the latter case grow .rodata section too much.
12265 So punt for now. */
12266 if (len >= destlen)
12267 return NULL_TREE;
12269 if (!fn)
12270 return NULL_TREE;
12272 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12273 'format' is known to contain no % formats and
12274 strlen (fmt) < cst. */
12275 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12277 if (!ignored)
12278 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12281 /* If the format is "%s", use strcpy if the result isn't used. */
12282 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12284 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12285 unsigned HOST_WIDE_INT origlen;
12287 /* Don't crash on snprintf (str1, cst, "%s"). */
12288 if (!orig)
12289 return NULL_TREE;
12291 retval = c_strlen (orig, 1);
12292 if (!retval || !host_integerp (retval, 1))
12293 return NULL_TREE;
12295 origlen = tree_low_cst (retval, 1);
12296 /* We could expand this as
12297 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12298 or to
12299 memcpy (str1, str2_with_nul_at_cstm1, cst);
12300 but in the former case that might increase code size
12301 and in the latter case grow .rodata section too much.
12302 So punt for now. */
12303 if (origlen >= destlen)
12304 return NULL_TREE;
12306 /* Convert snprintf (str1, cst, "%s", str2) into
12307 strcpy (str1, str2) if strlen (str2) < cst. */
12308 if (!fn)
12309 return NULL_TREE;
12311 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12313 if (ignored)
12314 retval = NULL_TREE;
12317 if (call && retval)
12319 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12320 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12321 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12323 else
12324 return call;
12327 /* Expand a call EXP to __builtin_object_size. */
12330 expand_builtin_object_size (tree exp)
12332 tree ost;
12333 int object_size_type;
12334 tree fndecl = get_callee_fndecl (exp);
12336 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12338 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12339 exp, fndecl);
12340 expand_builtin_trap ();
12341 return const0_rtx;
12344 ost = CALL_EXPR_ARG (exp, 1);
12345 STRIP_NOPS (ost);
12347 if (TREE_CODE (ost) != INTEGER_CST
12348 || tree_int_cst_sgn (ost) < 0
12349 || compare_tree_int (ost, 3) > 0)
12351 error ("%Klast argument of %D is not integer constant between 0 and 3",
12352 exp, fndecl);
12353 expand_builtin_trap ();
12354 return const0_rtx;
12357 object_size_type = tree_low_cst (ost, 0);
12359 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12362 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12363 FCODE is the BUILT_IN_* to use.
12364 Return NULL_RTX if we failed; the caller should emit a normal call,
12365 otherwise try to get the result in TARGET, if convenient (and in
12366 mode MODE if that's convenient). */
12368 static rtx
12369 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12370 enum built_in_function fcode)
12372 tree dest, src, len, size;
12374 if (!validate_arglist (exp,
12375 POINTER_TYPE,
12376 fcode == BUILT_IN_MEMSET_CHK
12377 ? INTEGER_TYPE : POINTER_TYPE,
12378 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12379 return NULL_RTX;
12381 dest = CALL_EXPR_ARG (exp, 0);
12382 src = CALL_EXPR_ARG (exp, 1);
12383 len = CALL_EXPR_ARG (exp, 2);
12384 size = CALL_EXPR_ARG (exp, 3);
12386 if (! host_integerp (size, 1))
12387 return NULL_RTX;
12389 if (host_integerp (len, 1) || integer_all_onesp (size))
12391 tree fn;
12393 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12395 warning_at (tree_nonartificial_location (exp),
12396 0, "%Kcall to %D will always overflow destination buffer",
12397 exp, get_callee_fndecl (exp));
12398 return NULL_RTX;
12401 fn = NULL_TREE;
12402 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12403 mem{cpy,pcpy,move,set} is available. */
12404 switch (fcode)
12406 case BUILT_IN_MEMCPY_CHK:
12407 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12408 break;
12409 case BUILT_IN_MEMPCPY_CHK:
12410 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12411 break;
12412 case BUILT_IN_MEMMOVE_CHK:
12413 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12414 break;
12415 case BUILT_IN_MEMSET_CHK:
12416 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12417 break;
12418 default:
12419 break;
12422 if (! fn)
12423 return NULL_RTX;
12425 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12426 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12427 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12428 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12430 else if (fcode == BUILT_IN_MEMSET_CHK)
12431 return NULL_RTX;
12432 else
12434 unsigned int dest_align = get_pointer_alignment (dest);
12436 /* If DEST is not a pointer type, call the normal function. */
12437 if (dest_align == 0)
12438 return NULL_RTX;
12440 /* If SRC and DEST are the same (and not volatile), do nothing. */
12441 if (operand_equal_p (src, dest, 0))
12443 tree expr;
12445 if (fcode != BUILT_IN_MEMPCPY_CHK)
12447 /* Evaluate and ignore LEN in case it has side-effects. */
12448 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12449 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12452 expr = fold_build_pointer_plus (dest, len);
12453 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12456 /* __memmove_chk special case. */
12457 if (fcode == BUILT_IN_MEMMOVE_CHK)
12459 unsigned int src_align = get_pointer_alignment (src);
12461 if (src_align == 0)
12462 return NULL_RTX;
12464 /* If src is categorized for a readonly section we can use
12465 normal __memcpy_chk. */
12466 if (readonly_data_expr (src))
12468 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12469 if (!fn)
12470 return NULL_RTX;
12471 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12472 dest, src, len, size);
12473 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12474 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12475 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12478 return NULL_RTX;
12482 /* Emit warning if a buffer overflow is detected at compile time. */
12484 static void
12485 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12487 int is_strlen = 0;
12488 tree len, size;
12489 location_t loc = tree_nonartificial_location (exp);
12491 switch (fcode)
12493 case BUILT_IN_STRCPY_CHK:
12494 case BUILT_IN_STPCPY_CHK:
12495 /* For __strcat_chk the warning will be emitted only if overflowing
12496 by at least strlen (dest) + 1 bytes. */
12497 case BUILT_IN_STRCAT_CHK:
12498 len = CALL_EXPR_ARG (exp, 1);
12499 size = CALL_EXPR_ARG (exp, 2);
12500 is_strlen = 1;
12501 break;
12502 case BUILT_IN_STRNCAT_CHK:
12503 case BUILT_IN_STRNCPY_CHK:
12504 case BUILT_IN_STPNCPY_CHK:
12505 len = CALL_EXPR_ARG (exp, 2);
12506 size = CALL_EXPR_ARG (exp, 3);
12507 break;
12508 case BUILT_IN_SNPRINTF_CHK:
12509 case BUILT_IN_VSNPRINTF_CHK:
12510 len = CALL_EXPR_ARG (exp, 1);
12511 size = CALL_EXPR_ARG (exp, 3);
12512 break;
12513 default:
12514 gcc_unreachable ();
12517 if (!len || !size)
12518 return;
12520 if (! host_integerp (size, 1) || integer_all_onesp (size))
12521 return;
12523 if (is_strlen)
12525 len = c_strlen (len, 1);
12526 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12527 return;
12529 else if (fcode == BUILT_IN_STRNCAT_CHK)
12531 tree src = CALL_EXPR_ARG (exp, 1);
12532 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12533 return;
12534 src = c_strlen (src, 1);
12535 if (! src || ! host_integerp (src, 1))
12537 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12538 exp, get_callee_fndecl (exp));
12539 return;
12541 else if (tree_int_cst_lt (src, size))
12542 return;
12544 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12545 return;
12547 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12548 exp, get_callee_fndecl (exp));
12551 /* Emit warning if a buffer overflow is detected at compile time
12552 in __sprintf_chk/__vsprintf_chk calls. */
12554 static void
12555 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12557 tree size, len, fmt;
12558 const char *fmt_str;
12559 int nargs = call_expr_nargs (exp);
12561 /* Verify the required arguments in the original call. */
12563 if (nargs < 4)
12564 return;
12565 size = CALL_EXPR_ARG (exp, 2);
12566 fmt = CALL_EXPR_ARG (exp, 3);
12568 if (! host_integerp (size, 1) || integer_all_onesp (size))
12569 return;
12571 /* Check whether the format is a literal string constant. */
12572 fmt_str = c_getstr (fmt);
12573 if (fmt_str == NULL)
12574 return;
12576 if (!init_target_chars ())
12577 return;
12579 /* If the format doesn't contain % args or %%, we know its size. */
12580 if (strchr (fmt_str, target_percent) == 0)
12581 len = build_int_cstu (size_type_node, strlen (fmt_str));
12582 /* If the format is "%s" and first ... argument is a string literal,
12583 we know it too. */
12584 else if (fcode == BUILT_IN_SPRINTF_CHK
12585 && strcmp (fmt_str, target_percent_s) == 0)
12587 tree arg;
12589 if (nargs < 5)
12590 return;
12591 arg = CALL_EXPR_ARG (exp, 4);
12592 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12593 return;
12595 len = c_strlen (arg, 1);
12596 if (!len || ! host_integerp (len, 1))
12597 return;
12599 else
12600 return;
12602 if (! tree_int_cst_lt (len, size))
12603 warning_at (tree_nonartificial_location (exp),
12604 0, "%Kcall to %D will always overflow destination buffer",
12605 exp, get_callee_fndecl (exp));
12608 /* Emit warning if a free is called with address of a variable. */
12610 static void
12611 maybe_emit_free_warning (tree exp)
12613 tree arg = CALL_EXPR_ARG (exp, 0);
12615 STRIP_NOPS (arg);
12616 if (TREE_CODE (arg) != ADDR_EXPR)
12617 return;
12619 arg = get_base_address (TREE_OPERAND (arg, 0));
12620 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12621 return;
12623 if (SSA_VAR_P (arg))
12624 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12625 "%Kattempt to free a non-heap object %qD", exp, arg);
12626 else
12627 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12628 "%Kattempt to free a non-heap object", exp);
12631 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12632 if possible. */
12634 tree
12635 fold_builtin_object_size (tree ptr, tree ost)
12637 unsigned HOST_WIDE_INT bytes;
12638 int object_size_type;
12640 if (!validate_arg (ptr, POINTER_TYPE)
12641 || !validate_arg (ost, INTEGER_TYPE))
12642 return NULL_TREE;
12644 STRIP_NOPS (ost);
12646 if (TREE_CODE (ost) != INTEGER_CST
12647 || tree_int_cst_sgn (ost) < 0
12648 || compare_tree_int (ost, 3) > 0)
12649 return NULL_TREE;
12651 object_size_type = tree_low_cst (ost, 0);
12653 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12654 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12655 and (size_t) 0 for types 2 and 3. */
12656 if (TREE_SIDE_EFFECTS (ptr))
12657 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12659 if (TREE_CODE (ptr) == ADDR_EXPR)
12661 bytes = compute_builtin_object_size (ptr, object_size_type);
12662 if (double_int_fits_to_tree_p (size_type_node,
12663 uhwi_to_double_int (bytes)))
12664 return build_int_cstu (size_type_node, bytes);
12666 else if (TREE_CODE (ptr) == SSA_NAME)
12668 /* If object size is not known yet, delay folding until
12669 later. Maybe subsequent passes will help determining
12670 it. */
12671 bytes = compute_builtin_object_size (ptr, object_size_type);
12672 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12673 && double_int_fits_to_tree_p (size_type_node,
12674 uhwi_to_double_int (bytes)))
12675 return build_int_cstu (size_type_node, bytes);
12678 return NULL_TREE;
12681 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12682 DEST, SRC, LEN, and SIZE are the arguments to the call.
12683 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12684 code of the builtin. If MAXLEN is not NULL, it is maximum length
12685 passed as third argument. */
12687 tree
12688 fold_builtin_memory_chk (location_t loc, tree fndecl,
12689 tree dest, tree src, tree len, tree size,
12690 tree maxlen, bool ignore,
12691 enum built_in_function fcode)
12693 tree fn;
12695 if (!validate_arg (dest, POINTER_TYPE)
12696 || !validate_arg (src,
12697 (fcode == BUILT_IN_MEMSET_CHK
12698 ? INTEGER_TYPE : POINTER_TYPE))
12699 || !validate_arg (len, INTEGER_TYPE)
12700 || !validate_arg (size, INTEGER_TYPE))
12701 return NULL_TREE;
12703 /* If SRC and DEST are the same (and not volatile), return DEST
12704 (resp. DEST+LEN for __mempcpy_chk). */
12705 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12707 if (fcode != BUILT_IN_MEMPCPY_CHK)
12708 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12709 dest, len);
12710 else
12712 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12713 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12717 if (! host_integerp (size, 1))
12718 return NULL_TREE;
12720 if (! integer_all_onesp (size))
12722 if (! host_integerp (len, 1))
12724 /* If LEN is not constant, try MAXLEN too.
12725 For MAXLEN only allow optimizing into non-_ocs function
12726 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12727 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12729 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12731 /* (void) __mempcpy_chk () can be optimized into
12732 (void) __memcpy_chk (). */
12733 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12734 if (!fn)
12735 return NULL_TREE;
12737 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12739 return NULL_TREE;
12742 else
12743 maxlen = len;
12745 if (tree_int_cst_lt (size, maxlen))
12746 return NULL_TREE;
12749 fn = NULL_TREE;
12750 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12751 mem{cpy,pcpy,move,set} is available. */
12752 switch (fcode)
12754 case BUILT_IN_MEMCPY_CHK:
12755 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12756 break;
12757 case BUILT_IN_MEMPCPY_CHK:
12758 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12759 break;
12760 case BUILT_IN_MEMMOVE_CHK:
12761 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12762 break;
12763 case BUILT_IN_MEMSET_CHK:
12764 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12765 break;
12766 default:
12767 break;
12770 if (!fn)
12771 return NULL_TREE;
12773 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12776 /* Fold a call to the __st[rp]cpy_chk builtin.
12777 DEST, SRC, and SIZE are the arguments to the call.
12778 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12779 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12780 strings passed as second argument. */
12782 tree
12783 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12784 tree src, tree size,
12785 tree maxlen, bool ignore,
12786 enum built_in_function fcode)
12788 tree len, fn;
12790 if (!validate_arg (dest, POINTER_TYPE)
12791 || !validate_arg (src, POINTER_TYPE)
12792 || !validate_arg (size, INTEGER_TYPE))
12793 return NULL_TREE;
12795 /* If SRC and DEST are the same (and not volatile), return DEST. */
12796 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12797 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12799 if (! host_integerp (size, 1))
12800 return NULL_TREE;
12802 if (! integer_all_onesp (size))
12804 len = c_strlen (src, 1);
12805 if (! len || ! host_integerp (len, 1))
12807 /* If LEN is not constant, try MAXLEN too.
12808 For MAXLEN only allow optimizing into non-_ocs function
12809 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12810 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12812 if (fcode == BUILT_IN_STPCPY_CHK)
12814 if (! ignore)
12815 return NULL_TREE;
12817 /* If return value of __stpcpy_chk is ignored,
12818 optimize into __strcpy_chk. */
12819 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12820 if (!fn)
12821 return NULL_TREE;
12823 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12826 if (! len || TREE_SIDE_EFFECTS (len))
12827 return NULL_TREE;
12829 /* If c_strlen returned something, but not a constant,
12830 transform __strcpy_chk into __memcpy_chk. */
12831 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12832 if (!fn)
12833 return NULL_TREE;
12835 len = fold_convert_loc (loc, size_type_node, len);
12836 len = size_binop_loc (loc, PLUS_EXPR, len,
12837 build_int_cst (size_type_node, 1));
12838 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12839 build_call_expr_loc (loc, fn, 4,
12840 dest, src, len, size));
12843 else
12844 maxlen = len;
12846 if (! tree_int_cst_lt (maxlen, size))
12847 return NULL_TREE;
12850 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12851 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12852 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12853 if (!fn)
12854 return NULL_TREE;
12856 return build_call_expr_loc (loc, fn, 2, dest, src);
12859 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12860 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12861 length passed as third argument. IGNORE is true if return value can be
12862 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12864 tree
12865 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12866 tree len, tree size, tree maxlen, bool ignore,
12867 enum built_in_function fcode)
12869 tree fn;
12871 if (!validate_arg (dest, POINTER_TYPE)
12872 || !validate_arg (src, POINTER_TYPE)
12873 || !validate_arg (len, INTEGER_TYPE)
12874 || !validate_arg (size, INTEGER_TYPE))
12875 return NULL_TREE;
12877 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12879 /* If return value of __stpncpy_chk is ignored,
12880 optimize into __strncpy_chk. */
12881 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12882 if (fn)
12883 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12886 if (! host_integerp (size, 1))
12887 return NULL_TREE;
12889 if (! integer_all_onesp (size))
12891 if (! host_integerp (len, 1))
12893 /* If LEN is not constant, try MAXLEN too.
12894 For MAXLEN only allow optimizing into non-_ocs function
12895 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12896 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12897 return NULL_TREE;
12899 else
12900 maxlen = len;
12902 if (tree_int_cst_lt (size, maxlen))
12903 return NULL_TREE;
12906 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12907 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12908 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12909 if (!fn)
12910 return NULL_TREE;
12912 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12915 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12916 are the arguments to the call. */
12918 static tree
12919 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12920 tree src, tree size)
12922 tree fn;
12923 const char *p;
12925 if (!validate_arg (dest, POINTER_TYPE)
12926 || !validate_arg (src, POINTER_TYPE)
12927 || !validate_arg (size, INTEGER_TYPE))
12928 return NULL_TREE;
12930 p = c_getstr (src);
12931 /* If the SRC parameter is "", return DEST. */
12932 if (p && *p == '\0')
12933 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12935 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12936 return NULL_TREE;
12938 /* If __builtin_strcat_chk is used, assume strcat is available. */
12939 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12940 if (!fn)
12941 return NULL_TREE;
12943 return build_call_expr_loc (loc, fn, 2, dest, src);
12946 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12947 LEN, and SIZE. */
12949 static tree
12950 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12951 tree dest, tree src, tree len, tree size)
12953 tree fn;
12954 const char *p;
12956 if (!validate_arg (dest, POINTER_TYPE)
12957 || !validate_arg (src, POINTER_TYPE)
12958 || !validate_arg (size, INTEGER_TYPE)
12959 || !validate_arg (size, INTEGER_TYPE))
12960 return NULL_TREE;
12962 p = c_getstr (src);
12963 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12964 if (p && *p == '\0')
12965 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12966 else if (integer_zerop (len))
12967 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12969 if (! host_integerp (size, 1))
12970 return NULL_TREE;
12972 if (! integer_all_onesp (size))
12974 tree src_len = c_strlen (src, 1);
12975 if (src_len
12976 && host_integerp (src_len, 1)
12977 && host_integerp (len, 1)
12978 && ! tree_int_cst_lt (len, src_len))
12980 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12981 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12982 if (!fn)
12983 return NULL_TREE;
12985 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12987 return NULL_TREE;
12990 /* If __builtin_strncat_chk is used, assume strncat is available. */
12991 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
12992 if (!fn)
12993 return NULL_TREE;
12995 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12998 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12999 Return NULL_TREE if a normal call should be emitted rather than
13000 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13001 or BUILT_IN_VSPRINTF_CHK. */
13003 static tree
13004 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13005 enum built_in_function fcode)
13007 tree dest, size, len, fn, fmt, flag;
13008 const char *fmt_str;
13010 /* Verify the required arguments in the original call. */
13011 if (nargs < 4)
13012 return NULL_TREE;
13013 dest = args[0];
13014 if (!validate_arg (dest, POINTER_TYPE))
13015 return NULL_TREE;
13016 flag = args[1];
13017 if (!validate_arg (flag, INTEGER_TYPE))
13018 return NULL_TREE;
13019 size = args[2];
13020 if (!validate_arg (size, INTEGER_TYPE))
13021 return NULL_TREE;
13022 fmt = args[3];
13023 if (!validate_arg (fmt, POINTER_TYPE))
13024 return NULL_TREE;
13026 if (! host_integerp (size, 1))
13027 return NULL_TREE;
13029 len = NULL_TREE;
13031 if (!init_target_chars ())
13032 return NULL_TREE;
13034 /* Check whether the format is a literal string constant. */
13035 fmt_str = c_getstr (fmt);
13036 if (fmt_str != NULL)
13038 /* If the format doesn't contain % args or %%, we know the size. */
13039 if (strchr (fmt_str, target_percent) == 0)
13041 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13042 len = build_int_cstu (size_type_node, strlen (fmt_str));
13044 /* If the format is "%s" and first ... argument is a string literal,
13045 we know the size too. */
13046 else if (fcode == BUILT_IN_SPRINTF_CHK
13047 && strcmp (fmt_str, target_percent_s) == 0)
13049 tree arg;
13051 if (nargs == 5)
13053 arg = args[4];
13054 if (validate_arg (arg, POINTER_TYPE))
13056 len = c_strlen (arg, 1);
13057 if (! len || ! host_integerp (len, 1))
13058 len = NULL_TREE;
13064 if (! integer_all_onesp (size))
13066 if (! len || ! tree_int_cst_lt (len, size))
13067 return NULL_TREE;
13070 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13071 or if format doesn't contain % chars or is "%s". */
13072 if (! integer_zerop (flag))
13074 if (fmt_str == NULL)
13075 return NULL_TREE;
13076 if (strchr (fmt_str, target_percent) != NULL
13077 && strcmp (fmt_str, target_percent_s))
13078 return NULL_TREE;
13081 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13082 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13083 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13084 if (!fn)
13085 return NULL_TREE;
13087 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13090 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13091 a normal call should be emitted rather than expanding the function
13092 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13094 static tree
13095 fold_builtin_sprintf_chk (location_t loc, tree exp,
13096 enum built_in_function fcode)
13098 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13099 CALL_EXPR_ARGP (exp), fcode);
13102 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13103 NULL_TREE if a normal call should be emitted rather than expanding
13104 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13105 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13106 passed as second argument. */
13108 static tree
13109 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13110 tree maxlen, enum built_in_function fcode)
13112 tree dest, size, len, fn, fmt, flag;
13113 const char *fmt_str;
13115 /* Verify the required arguments in the original call. */
13116 if (nargs < 5)
13117 return NULL_TREE;
13118 dest = args[0];
13119 if (!validate_arg (dest, POINTER_TYPE))
13120 return NULL_TREE;
13121 len = args[1];
13122 if (!validate_arg (len, INTEGER_TYPE))
13123 return NULL_TREE;
13124 flag = args[2];
13125 if (!validate_arg (flag, INTEGER_TYPE))
13126 return NULL_TREE;
13127 size = args[3];
13128 if (!validate_arg (size, INTEGER_TYPE))
13129 return NULL_TREE;
13130 fmt = args[4];
13131 if (!validate_arg (fmt, POINTER_TYPE))
13132 return NULL_TREE;
13134 if (! host_integerp (size, 1))
13135 return NULL_TREE;
13137 if (! integer_all_onesp (size))
13139 if (! host_integerp (len, 1))
13141 /* If LEN is not constant, try MAXLEN too.
13142 For MAXLEN only allow optimizing into non-_ocs function
13143 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13144 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13145 return NULL_TREE;
13147 else
13148 maxlen = len;
13150 if (tree_int_cst_lt (size, maxlen))
13151 return NULL_TREE;
13154 if (!init_target_chars ())
13155 return NULL_TREE;
13157 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13158 or if format doesn't contain % chars or is "%s". */
13159 if (! integer_zerop (flag))
13161 fmt_str = c_getstr (fmt);
13162 if (fmt_str == NULL)
13163 return NULL_TREE;
13164 if (strchr (fmt_str, target_percent) != NULL
13165 && strcmp (fmt_str, target_percent_s))
13166 return NULL_TREE;
13169 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13170 available. */
13171 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13172 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13173 if (!fn)
13174 return NULL_TREE;
13176 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13179 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13180 a normal call should be emitted rather than expanding the function
13181 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13182 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13183 passed as second argument. */
13185 tree
13186 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13187 enum built_in_function fcode)
13189 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13190 CALL_EXPR_ARGP (exp), maxlen, fcode);
13193 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13194 FMT and ARG are the arguments to the call; we don't fold cases with
13195 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13197 Return NULL_TREE if no simplification was possible, otherwise return the
13198 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13199 code of the function to be simplified. */
13201 static tree
13202 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13203 tree arg, bool ignore,
13204 enum built_in_function fcode)
13206 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13207 const char *fmt_str = NULL;
13209 /* If the return value is used, don't do the transformation. */
13210 if (! ignore)
13211 return NULL_TREE;
13213 /* Verify the required arguments in the original call. */
13214 if (!validate_arg (fmt, POINTER_TYPE))
13215 return NULL_TREE;
13217 /* Check whether the format is a literal string constant. */
13218 fmt_str = c_getstr (fmt);
13219 if (fmt_str == NULL)
13220 return NULL_TREE;
13222 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13224 /* If we're using an unlocked function, assume the other
13225 unlocked functions exist explicitly. */
13226 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13227 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13229 else
13231 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13232 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13235 if (!init_target_chars ())
13236 return NULL_TREE;
13238 if (strcmp (fmt_str, target_percent_s) == 0
13239 || strchr (fmt_str, target_percent) == NULL)
13241 const char *str;
13243 if (strcmp (fmt_str, target_percent_s) == 0)
13245 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13246 return NULL_TREE;
13248 if (!arg || !validate_arg (arg, POINTER_TYPE))
13249 return NULL_TREE;
13251 str = c_getstr (arg);
13252 if (str == NULL)
13253 return NULL_TREE;
13255 else
13257 /* The format specifier doesn't contain any '%' characters. */
13258 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13259 && arg)
13260 return NULL_TREE;
13261 str = fmt_str;
13264 /* If the string was "", printf does nothing. */
13265 if (str[0] == '\0')
13266 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13268 /* If the string has length of 1, call putchar. */
13269 if (str[1] == '\0')
13271 /* Given printf("c"), (where c is any one character,)
13272 convert "c"[0] to an int and pass that to the replacement
13273 function. */
13274 newarg = build_int_cst (integer_type_node, str[0]);
13275 if (fn_putchar)
13276 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13278 else
13280 /* If the string was "string\n", call puts("string"). */
13281 size_t len = strlen (str);
13282 if ((unsigned char)str[len - 1] == target_newline
13283 && (size_t) (int) len == len
13284 && (int) len > 0)
13286 char *newstr;
13287 tree offset_node, string_cst;
13289 /* Create a NUL-terminated string that's one char shorter
13290 than the original, stripping off the trailing '\n'. */
13291 newarg = build_string_literal (len, str);
13292 string_cst = string_constant (newarg, &offset_node);
13293 gcc_checking_assert (string_cst
13294 && (TREE_STRING_LENGTH (string_cst)
13295 == (int) len)
13296 && integer_zerop (offset_node)
13297 && (unsigned char)
13298 TREE_STRING_POINTER (string_cst)[len - 1]
13299 == target_newline);
13300 /* build_string_literal creates a new STRING_CST,
13301 modify it in place to avoid double copying. */
13302 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13303 newstr[len - 1] = '\0';
13304 if (fn_puts)
13305 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13307 else
13308 /* We'd like to arrange to call fputs(string,stdout) here,
13309 but we need stdout and don't have a way to get it yet. */
13310 return NULL_TREE;
13314 /* The other optimizations can be done only on the non-va_list variants. */
13315 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13316 return NULL_TREE;
13318 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13319 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13321 if (!arg || !validate_arg (arg, POINTER_TYPE))
13322 return NULL_TREE;
13323 if (fn_puts)
13324 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13327 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13328 else if (strcmp (fmt_str, target_percent_c) == 0)
13330 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13331 return NULL_TREE;
13332 if (fn_putchar)
13333 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13336 if (!call)
13337 return NULL_TREE;
13339 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13342 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13343 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13344 more than 3 arguments, and ARG may be null in the 2-argument case.
13346 Return NULL_TREE if no simplification was possible, otherwise return the
13347 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13348 code of the function to be simplified. */
13350 static tree
13351 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13352 tree fmt, tree arg, bool ignore,
13353 enum built_in_function fcode)
13355 tree fn_fputc, fn_fputs, call = NULL_TREE;
13356 const char *fmt_str = NULL;
13358 /* If the return value is used, don't do the transformation. */
13359 if (! ignore)
13360 return NULL_TREE;
13362 /* Verify the required arguments in the original call. */
13363 if (!validate_arg (fp, POINTER_TYPE))
13364 return NULL_TREE;
13365 if (!validate_arg (fmt, POINTER_TYPE))
13366 return NULL_TREE;
13368 /* Check whether the format is a literal string constant. */
13369 fmt_str = c_getstr (fmt);
13370 if (fmt_str == NULL)
13371 return NULL_TREE;
13373 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13375 /* If we're using an unlocked function, assume the other
13376 unlocked functions exist explicitly. */
13377 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13378 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13380 else
13382 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13383 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13386 if (!init_target_chars ())
13387 return NULL_TREE;
13389 /* If the format doesn't contain % args or %%, use strcpy. */
13390 if (strchr (fmt_str, target_percent) == NULL)
13392 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13393 && arg)
13394 return NULL_TREE;
13396 /* If the format specifier was "", fprintf does nothing. */
13397 if (fmt_str[0] == '\0')
13399 /* If FP has side-effects, just wait until gimplification is
13400 done. */
13401 if (TREE_SIDE_EFFECTS (fp))
13402 return NULL_TREE;
13404 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13407 /* When "string" doesn't contain %, replace all cases of
13408 fprintf (fp, string) with fputs (string, fp). The fputs
13409 builtin will take care of special cases like length == 1. */
13410 if (fn_fputs)
13411 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13414 /* The other optimizations can be done only on the non-va_list variants. */
13415 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13416 return NULL_TREE;
13418 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13419 else if (strcmp (fmt_str, target_percent_s) == 0)
13421 if (!arg || !validate_arg (arg, POINTER_TYPE))
13422 return NULL_TREE;
13423 if (fn_fputs)
13424 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13427 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13428 else if (strcmp (fmt_str, target_percent_c) == 0)
13430 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13431 return NULL_TREE;
13432 if (fn_fputc)
13433 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13436 if (!call)
13437 return NULL_TREE;
13438 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13441 /* Initialize format string characters in the target charset. */
13443 static bool
13444 init_target_chars (void)
13446 static bool init;
13447 if (!init)
13449 target_newline = lang_hooks.to_target_charset ('\n');
13450 target_percent = lang_hooks.to_target_charset ('%');
13451 target_c = lang_hooks.to_target_charset ('c');
13452 target_s = lang_hooks.to_target_charset ('s');
13453 if (target_newline == 0 || target_percent == 0 || target_c == 0
13454 || target_s == 0)
13455 return false;
13457 target_percent_c[0] = target_percent;
13458 target_percent_c[1] = target_c;
13459 target_percent_c[2] = '\0';
13461 target_percent_s[0] = target_percent;
13462 target_percent_s[1] = target_s;
13463 target_percent_s[2] = '\0';
13465 target_percent_s_newline[0] = target_percent;
13466 target_percent_s_newline[1] = target_s;
13467 target_percent_s_newline[2] = target_newline;
13468 target_percent_s_newline[3] = '\0';
13470 init = true;
13472 return true;
13475 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13476 and no overflow/underflow occurred. INEXACT is true if M was not
13477 exactly calculated. TYPE is the tree type for the result. This
13478 function assumes that you cleared the MPFR flags and then
13479 calculated M to see if anything subsequently set a flag prior to
13480 entering this function. Return NULL_TREE if any checks fail. */
13482 static tree
13483 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13485 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13486 overflow/underflow occurred. If -frounding-math, proceed iff the
13487 result of calling FUNC was exact. */
13488 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13489 && (!flag_rounding_math || !inexact))
13491 REAL_VALUE_TYPE rr;
13493 real_from_mpfr (&rr, m, type, GMP_RNDN);
13494 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13495 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13496 but the mpft_t is not, then we underflowed in the
13497 conversion. */
13498 if (real_isfinite (&rr)
13499 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13501 REAL_VALUE_TYPE rmode;
13503 real_convert (&rmode, TYPE_MODE (type), &rr);
13504 /* Proceed iff the specified mode can hold the value. */
13505 if (real_identical (&rmode, &rr))
13506 return build_real (type, rmode);
13509 return NULL_TREE;
13512 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13513 number and no overflow/underflow occurred. INEXACT is true if M
13514 was not exactly calculated. TYPE is the tree type for the result.
13515 This function assumes that you cleared the MPFR flags and then
13516 calculated M to see if anything subsequently set a flag prior to
13517 entering this function. Return NULL_TREE if any checks fail, if
13518 FORCE_CONVERT is true, then bypass the checks. */
13520 static tree
13521 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13523 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13524 overflow/underflow occurred. If -frounding-math, proceed iff the
13525 result of calling FUNC was exact. */
13526 if (force_convert
13527 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13528 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13529 && (!flag_rounding_math || !inexact)))
13531 REAL_VALUE_TYPE re, im;
13533 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13534 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13535 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13536 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13537 but the mpft_t is not, then we underflowed in the
13538 conversion. */
13539 if (force_convert
13540 || (real_isfinite (&re) && real_isfinite (&im)
13541 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13542 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13544 REAL_VALUE_TYPE re_mode, im_mode;
13546 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13547 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13548 /* Proceed iff the specified mode can hold the value. */
13549 if (force_convert
13550 || (real_identical (&re_mode, &re)
13551 && real_identical (&im_mode, &im)))
13552 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13553 build_real (TREE_TYPE (type), im_mode));
13556 return NULL_TREE;
13559 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13560 FUNC on it and return the resulting value as a tree with type TYPE.
13561 If MIN and/or MAX are not NULL, then the supplied ARG must be
13562 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13563 acceptable values, otherwise they are not. The mpfr precision is
13564 set to the precision of TYPE. We assume that function FUNC returns
13565 zero if the result could be calculated exactly within the requested
13566 precision. */
13568 static tree
13569 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13570 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13571 bool inclusive)
13573 tree result = NULL_TREE;
13575 STRIP_NOPS (arg);
13577 /* To proceed, MPFR must exactly represent the target floating point
13578 format, which only happens when the target base equals two. */
13579 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13580 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13582 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13584 if (real_isfinite (ra)
13585 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13586 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13588 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13589 const int prec = fmt->p;
13590 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13591 int inexact;
13592 mpfr_t m;
13594 mpfr_init2 (m, prec);
13595 mpfr_from_real (m, ra, GMP_RNDN);
13596 mpfr_clear_flags ();
13597 inexact = func (m, m, rnd);
13598 result = do_mpfr_ckconv (m, type, inexact);
13599 mpfr_clear (m);
13603 return result;
13606 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13607 FUNC on it and return the resulting value as a tree with type TYPE.
13608 The mpfr precision is set to the precision of TYPE. We assume that
13609 function FUNC returns zero if the result could be calculated
13610 exactly within the requested precision. */
13612 static tree
13613 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13614 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13616 tree result = NULL_TREE;
13618 STRIP_NOPS (arg1);
13619 STRIP_NOPS (arg2);
13621 /* To proceed, MPFR must exactly represent the target floating point
13622 format, which only happens when the target base equals two. */
13623 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13624 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13625 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13627 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13628 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13630 if (real_isfinite (ra1) && real_isfinite (ra2))
13632 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13633 const int prec = fmt->p;
13634 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13635 int inexact;
13636 mpfr_t m1, m2;
13638 mpfr_inits2 (prec, m1, m2, NULL);
13639 mpfr_from_real (m1, ra1, GMP_RNDN);
13640 mpfr_from_real (m2, ra2, GMP_RNDN);
13641 mpfr_clear_flags ();
13642 inexact = func (m1, m1, m2, rnd);
13643 result = do_mpfr_ckconv (m1, type, inexact);
13644 mpfr_clears (m1, m2, NULL);
13648 return result;
13651 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13652 FUNC on it and return the resulting value as a tree with type TYPE.
13653 The mpfr precision is set to the precision of TYPE. We assume that
13654 function FUNC returns zero if the result could be calculated
13655 exactly within the requested precision. */
13657 static tree
13658 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13659 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13661 tree result = NULL_TREE;
13663 STRIP_NOPS (arg1);
13664 STRIP_NOPS (arg2);
13665 STRIP_NOPS (arg3);
13667 /* To proceed, MPFR must exactly represent the target floating point
13668 format, which only happens when the target base equals two. */
13669 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13670 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13671 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13672 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13674 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13675 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13676 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13678 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13680 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13681 const int prec = fmt->p;
13682 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13683 int inexact;
13684 mpfr_t m1, m2, m3;
13686 mpfr_inits2 (prec, m1, m2, m3, NULL);
13687 mpfr_from_real (m1, ra1, GMP_RNDN);
13688 mpfr_from_real (m2, ra2, GMP_RNDN);
13689 mpfr_from_real (m3, ra3, GMP_RNDN);
13690 mpfr_clear_flags ();
13691 inexact = func (m1, m1, m2, m3, rnd);
13692 result = do_mpfr_ckconv (m1, type, inexact);
13693 mpfr_clears (m1, m2, m3, NULL);
13697 return result;
13700 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13701 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13702 If ARG_SINP and ARG_COSP are NULL then the result is returned
13703 as a complex value.
13704 The type is taken from the type of ARG and is used for setting the
13705 precision of the calculation and results. */
13707 static tree
13708 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13710 tree const type = TREE_TYPE (arg);
13711 tree result = NULL_TREE;
13713 STRIP_NOPS (arg);
13715 /* To proceed, MPFR must exactly represent the target floating point
13716 format, which only happens when the target base equals two. */
13717 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13718 && TREE_CODE (arg) == REAL_CST
13719 && !TREE_OVERFLOW (arg))
13721 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13723 if (real_isfinite (ra))
13725 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13726 const int prec = fmt->p;
13727 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13728 tree result_s, result_c;
13729 int inexact;
13730 mpfr_t m, ms, mc;
13732 mpfr_inits2 (prec, m, ms, mc, NULL);
13733 mpfr_from_real (m, ra, GMP_RNDN);
13734 mpfr_clear_flags ();
13735 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13736 result_s = do_mpfr_ckconv (ms, type, inexact);
13737 result_c = do_mpfr_ckconv (mc, type, inexact);
13738 mpfr_clears (m, ms, mc, NULL);
13739 if (result_s && result_c)
13741 /* If we are to return in a complex value do so. */
13742 if (!arg_sinp && !arg_cosp)
13743 return build_complex (build_complex_type (type),
13744 result_c, result_s);
13746 /* Dereference the sin/cos pointer arguments. */
13747 arg_sinp = build_fold_indirect_ref (arg_sinp);
13748 arg_cosp = build_fold_indirect_ref (arg_cosp);
13749 /* Proceed if valid pointer type were passed in. */
13750 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13751 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13753 /* Set the values. */
13754 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13755 result_s);
13756 TREE_SIDE_EFFECTS (result_s) = 1;
13757 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13758 result_c);
13759 TREE_SIDE_EFFECTS (result_c) = 1;
13760 /* Combine the assignments into a compound expr. */
13761 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13762 result_s, result_c));
13767 return result;
13770 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13771 two-argument mpfr order N Bessel function FUNC on them and return
13772 the resulting value as a tree with type TYPE. The mpfr precision
13773 is set to the precision of TYPE. We assume that function FUNC
13774 returns zero if the result could be calculated exactly within the
13775 requested precision. */
13776 static tree
13777 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13778 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13779 const REAL_VALUE_TYPE *min, bool inclusive)
13781 tree result = NULL_TREE;
13783 STRIP_NOPS (arg1);
13784 STRIP_NOPS (arg2);
13786 /* To proceed, MPFR must exactly represent the target floating point
13787 format, which only happens when the target base equals two. */
13788 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13789 && host_integerp (arg1, 0)
13790 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13792 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13793 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13795 if (n == (long)n
13796 && real_isfinite (ra)
13797 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13799 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13800 const int prec = fmt->p;
13801 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13802 int inexact;
13803 mpfr_t m;
13805 mpfr_init2 (m, prec);
13806 mpfr_from_real (m, ra, GMP_RNDN);
13807 mpfr_clear_flags ();
13808 inexact = func (m, n, m, rnd);
13809 result = do_mpfr_ckconv (m, type, inexact);
13810 mpfr_clear (m);
13814 return result;
13817 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13818 the pointer *(ARG_QUO) and return the result. The type is taken
13819 from the type of ARG0 and is used for setting the precision of the
13820 calculation and results. */
13822 static tree
13823 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13825 tree const type = TREE_TYPE (arg0);
13826 tree result = NULL_TREE;
13828 STRIP_NOPS (arg0);
13829 STRIP_NOPS (arg1);
13831 /* To proceed, MPFR must exactly represent the target floating point
13832 format, which only happens when the target base equals two. */
13833 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13834 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13835 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13837 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13838 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13840 if (real_isfinite (ra0) && real_isfinite (ra1))
13842 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13843 const int prec = fmt->p;
13844 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13845 tree result_rem;
13846 long integer_quo;
13847 mpfr_t m0, m1;
13849 mpfr_inits2 (prec, m0, m1, NULL);
13850 mpfr_from_real (m0, ra0, GMP_RNDN);
13851 mpfr_from_real (m1, ra1, GMP_RNDN);
13852 mpfr_clear_flags ();
13853 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13854 /* Remquo is independent of the rounding mode, so pass
13855 inexact=0 to do_mpfr_ckconv(). */
13856 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13857 mpfr_clears (m0, m1, NULL);
13858 if (result_rem)
13860 /* MPFR calculates quo in the host's long so it may
13861 return more bits in quo than the target int can hold
13862 if sizeof(host long) > sizeof(target int). This can
13863 happen even for native compilers in LP64 mode. In
13864 these cases, modulo the quo value with the largest
13865 number that the target int can hold while leaving one
13866 bit for the sign. */
13867 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13868 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13870 /* Dereference the quo pointer argument. */
13871 arg_quo = build_fold_indirect_ref (arg_quo);
13872 /* Proceed iff a valid pointer type was passed in. */
13873 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13875 /* Set the value. */
13876 tree result_quo
13877 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13878 build_int_cst (TREE_TYPE (arg_quo),
13879 integer_quo));
13880 TREE_SIDE_EFFECTS (result_quo) = 1;
13881 /* Combine the quo assignment with the rem. */
13882 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13883 result_quo, result_rem));
13888 return result;
13891 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13892 resulting value as a tree with type TYPE. The mpfr precision is
13893 set to the precision of TYPE. We assume that this mpfr function
13894 returns zero if the result could be calculated exactly within the
13895 requested precision. In addition, the integer pointer represented
13896 by ARG_SG will be dereferenced and set to the appropriate signgam
13897 (-1,1) value. */
13899 static tree
13900 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13902 tree result = NULL_TREE;
13904 STRIP_NOPS (arg);
13906 /* To proceed, MPFR must exactly represent the target floating point
13907 format, which only happens when the target base equals two. Also
13908 verify ARG is a constant and that ARG_SG is an int pointer. */
13909 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13910 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13911 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13912 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13914 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13916 /* In addition to NaN and Inf, the argument cannot be zero or a
13917 negative integer. */
13918 if (real_isfinite (ra)
13919 && ra->cl != rvc_zero
13920 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13922 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13923 const int prec = fmt->p;
13924 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13925 int inexact, sg;
13926 mpfr_t m;
13927 tree result_lg;
13929 mpfr_init2 (m, prec);
13930 mpfr_from_real (m, ra, GMP_RNDN);
13931 mpfr_clear_flags ();
13932 inexact = mpfr_lgamma (m, &sg, m, rnd);
13933 result_lg = do_mpfr_ckconv (m, type, inexact);
13934 mpfr_clear (m);
13935 if (result_lg)
13937 tree result_sg;
13939 /* Dereference the arg_sg pointer argument. */
13940 arg_sg = build_fold_indirect_ref (arg_sg);
13941 /* Assign the signgam value into *arg_sg. */
13942 result_sg = fold_build2 (MODIFY_EXPR,
13943 TREE_TYPE (arg_sg), arg_sg,
13944 build_int_cst (TREE_TYPE (arg_sg), sg));
13945 TREE_SIDE_EFFECTS (result_sg) = 1;
13946 /* Combine the signgam assignment with the lgamma result. */
13947 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13948 result_sg, result_lg));
13953 return result;
13956 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13957 function FUNC on it and return the resulting value as a tree with
13958 type TYPE. The mpfr precision is set to the precision of TYPE. We
13959 assume that function FUNC returns zero if the result could be
13960 calculated exactly within the requested precision. */
13962 static tree
13963 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13965 tree result = NULL_TREE;
13967 STRIP_NOPS (arg);
13969 /* To proceed, MPFR must exactly represent the target floating point
13970 format, which only happens when the target base equals two. */
13971 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13972 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13973 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13975 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13976 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13978 if (real_isfinite (re) && real_isfinite (im))
13980 const struct real_format *const fmt =
13981 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13982 const int prec = fmt->p;
13983 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13984 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13985 int inexact;
13986 mpc_t m;
13988 mpc_init2 (m, prec);
13989 mpfr_from_real (mpc_realref(m), re, rnd);
13990 mpfr_from_real (mpc_imagref(m), im, rnd);
13991 mpfr_clear_flags ();
13992 inexact = func (m, m, crnd);
13993 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13994 mpc_clear (m);
13998 return result;
14001 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14002 mpc function FUNC on it and return the resulting value as a tree
14003 with type TYPE. The mpfr precision is set to the precision of
14004 TYPE. We assume that function FUNC returns zero if the result
14005 could be calculated exactly within the requested precision. If
14006 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14007 in the arguments and/or results. */
14009 tree
14010 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14011 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14013 tree result = NULL_TREE;
14015 STRIP_NOPS (arg0);
14016 STRIP_NOPS (arg1);
14018 /* To proceed, MPFR must exactly represent the target floating point
14019 format, which only happens when the target base equals two. */
14020 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14021 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14022 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14023 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14024 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14026 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14027 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14028 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14029 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14031 if (do_nonfinite
14032 || (real_isfinite (re0) && real_isfinite (im0)
14033 && real_isfinite (re1) && real_isfinite (im1)))
14035 const struct real_format *const fmt =
14036 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14037 const int prec = fmt->p;
14038 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14039 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14040 int inexact;
14041 mpc_t m0, m1;
14043 mpc_init2 (m0, prec);
14044 mpc_init2 (m1, prec);
14045 mpfr_from_real (mpc_realref(m0), re0, rnd);
14046 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14047 mpfr_from_real (mpc_realref(m1), re1, rnd);
14048 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14049 mpfr_clear_flags ();
14050 inexact = func (m0, m0, m1, crnd);
14051 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14052 mpc_clear (m0);
14053 mpc_clear (m1);
14057 return result;
14060 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14061 a normal call should be emitted rather than expanding the function
14062 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14064 static tree
14065 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14067 int nargs = gimple_call_num_args (stmt);
14069 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14070 (nargs > 0
14071 ? gimple_call_arg_ptr (stmt, 0)
14072 : &error_mark_node), fcode);
14075 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14076 a normal call should be emitted rather than expanding the function
14077 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14078 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14079 passed as second argument. */
14081 tree
14082 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14083 enum built_in_function fcode)
14085 int nargs = gimple_call_num_args (stmt);
14087 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14088 (nargs > 0
14089 ? gimple_call_arg_ptr (stmt, 0)
14090 : &error_mark_node), maxlen, fcode);
14093 /* Builtins with folding operations that operate on "..." arguments
14094 need special handling; we need to store the arguments in a convenient
14095 data structure before attempting any folding. Fortunately there are
14096 only a few builtins that fall into this category. FNDECL is the
14097 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14098 result of the function call is ignored. */
14100 static tree
14101 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14102 bool ignore ATTRIBUTE_UNUSED)
14104 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14105 tree ret = NULL_TREE;
14107 switch (fcode)
14109 case BUILT_IN_SPRINTF_CHK:
14110 case BUILT_IN_VSPRINTF_CHK:
14111 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14112 break;
14114 case BUILT_IN_SNPRINTF_CHK:
14115 case BUILT_IN_VSNPRINTF_CHK:
14116 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14118 default:
14119 break;
14121 if (ret)
14123 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14124 TREE_NO_WARNING (ret) = 1;
14125 return ret;
14127 return NULL_TREE;
14130 /* A wrapper function for builtin folding that prevents warnings for
14131 "statement without effect" and the like, caused by removing the
14132 call node earlier than the warning is generated. */
14134 tree
14135 fold_call_stmt (gimple stmt, bool ignore)
14137 tree ret = NULL_TREE;
14138 tree fndecl = gimple_call_fndecl (stmt);
14139 location_t loc = gimple_location (stmt);
14140 if (fndecl
14141 && TREE_CODE (fndecl) == FUNCTION_DECL
14142 && DECL_BUILT_IN (fndecl)
14143 && !gimple_call_va_arg_pack_p (stmt))
14145 int nargs = gimple_call_num_args (stmt);
14146 tree *args = (nargs > 0
14147 ? gimple_call_arg_ptr (stmt, 0)
14148 : &error_mark_node);
14150 if (avoid_folding_inline_builtin (fndecl))
14151 return NULL_TREE;
14152 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14154 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14156 else
14158 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14159 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14160 if (!ret)
14161 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14162 if (ret)
14164 /* Propagate location information from original call to
14165 expansion of builtin. Otherwise things like
14166 maybe_emit_chk_warning, that operate on the expansion
14167 of a builtin, will use the wrong location information. */
14168 if (gimple_has_location (stmt))
14170 tree realret = ret;
14171 if (TREE_CODE (ret) == NOP_EXPR)
14172 realret = TREE_OPERAND (ret, 0);
14173 if (CAN_HAVE_LOCATION_P (realret)
14174 && !EXPR_HAS_LOCATION (realret))
14175 SET_EXPR_LOCATION (realret, loc);
14176 return realret;
14178 return ret;
14182 return NULL_TREE;
14185 /* Look up the function in builtin_decl that corresponds to DECL
14186 and set ASMSPEC as its user assembler name. DECL must be a
14187 function decl that declares a builtin. */
14189 void
14190 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14192 tree builtin;
14193 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14194 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14195 && asmspec != 0);
14197 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14198 set_user_assembler_name (builtin, asmspec);
14199 switch (DECL_FUNCTION_CODE (decl))
14201 case BUILT_IN_MEMCPY:
14202 init_block_move_fn (asmspec);
14203 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14204 break;
14205 case BUILT_IN_MEMSET:
14206 init_block_clear_fn (asmspec);
14207 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14208 break;
14209 case BUILT_IN_MEMMOVE:
14210 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14211 break;
14212 case BUILT_IN_MEMCMP:
14213 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14214 break;
14215 case BUILT_IN_ABORT:
14216 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14217 break;
14218 case BUILT_IN_FFS:
14219 if (INT_TYPE_SIZE < BITS_PER_WORD)
14221 set_user_assembler_libfunc ("ffs", asmspec);
14222 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14223 MODE_INT, 0), "ffs");
14225 break;
14226 default:
14227 break;
14231 /* Return true if DECL is a builtin that expands to a constant or similarly
14232 simple code. */
14233 bool
14234 is_simple_builtin (tree decl)
14236 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14237 switch (DECL_FUNCTION_CODE (decl))
14239 /* Builtins that expand to constants. */
14240 case BUILT_IN_CONSTANT_P:
14241 case BUILT_IN_EXPECT:
14242 case BUILT_IN_OBJECT_SIZE:
14243 case BUILT_IN_UNREACHABLE:
14244 /* Simple register moves or loads from stack. */
14245 case BUILT_IN_ASSUME_ALIGNED:
14246 case BUILT_IN_RETURN_ADDRESS:
14247 case BUILT_IN_EXTRACT_RETURN_ADDR:
14248 case BUILT_IN_FROB_RETURN_ADDR:
14249 case BUILT_IN_RETURN:
14250 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14251 case BUILT_IN_FRAME_ADDRESS:
14252 case BUILT_IN_VA_END:
14253 case BUILT_IN_STACK_SAVE:
14254 case BUILT_IN_STACK_RESTORE:
14255 /* Exception state returns or moves registers around. */
14256 case BUILT_IN_EH_FILTER:
14257 case BUILT_IN_EH_POINTER:
14258 case BUILT_IN_EH_COPY_VALUES:
14259 return true;
14261 default:
14262 return false;
14265 return false;
14268 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14269 most probably expanded inline into reasonably simple code. This is a
14270 superset of is_simple_builtin. */
14271 bool
14272 is_inexpensive_builtin (tree decl)
14274 if (!decl)
14275 return false;
14276 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14277 return true;
14278 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14279 switch (DECL_FUNCTION_CODE (decl))
14281 case BUILT_IN_ABS:
14282 case BUILT_IN_ALLOCA:
14283 case BUILT_IN_ALLOCA_WITH_ALIGN:
14284 case BUILT_IN_BSWAP32:
14285 case BUILT_IN_BSWAP64:
14286 case BUILT_IN_CLZ:
14287 case BUILT_IN_CLZIMAX:
14288 case BUILT_IN_CLZL:
14289 case BUILT_IN_CLZLL:
14290 case BUILT_IN_CTZ:
14291 case BUILT_IN_CTZIMAX:
14292 case BUILT_IN_CTZL:
14293 case BUILT_IN_CTZLL:
14294 case BUILT_IN_FFS:
14295 case BUILT_IN_FFSIMAX:
14296 case BUILT_IN_FFSL:
14297 case BUILT_IN_FFSLL:
14298 case BUILT_IN_IMAXABS:
14299 case BUILT_IN_FINITE:
14300 case BUILT_IN_FINITEF:
14301 case BUILT_IN_FINITEL:
14302 case BUILT_IN_FINITED32:
14303 case BUILT_IN_FINITED64:
14304 case BUILT_IN_FINITED128:
14305 case BUILT_IN_FPCLASSIFY:
14306 case BUILT_IN_ISFINITE:
14307 case BUILT_IN_ISINF_SIGN:
14308 case BUILT_IN_ISINF:
14309 case BUILT_IN_ISINFF:
14310 case BUILT_IN_ISINFL:
14311 case BUILT_IN_ISINFD32:
14312 case BUILT_IN_ISINFD64:
14313 case BUILT_IN_ISINFD128:
14314 case BUILT_IN_ISNAN:
14315 case BUILT_IN_ISNANF:
14316 case BUILT_IN_ISNANL:
14317 case BUILT_IN_ISNAND32:
14318 case BUILT_IN_ISNAND64:
14319 case BUILT_IN_ISNAND128:
14320 case BUILT_IN_ISNORMAL:
14321 case BUILT_IN_ISGREATER:
14322 case BUILT_IN_ISGREATEREQUAL:
14323 case BUILT_IN_ISLESS:
14324 case BUILT_IN_ISLESSEQUAL:
14325 case BUILT_IN_ISLESSGREATER:
14326 case BUILT_IN_ISUNORDERED:
14327 case BUILT_IN_VA_ARG_PACK:
14328 case BUILT_IN_VA_ARG_PACK_LEN:
14329 case BUILT_IN_VA_COPY:
14330 case BUILT_IN_TRAP:
14331 case BUILT_IN_SAVEREGS:
14332 case BUILT_IN_POPCOUNTL:
14333 case BUILT_IN_POPCOUNTLL:
14334 case BUILT_IN_POPCOUNTIMAX:
14335 case BUILT_IN_POPCOUNT:
14336 case BUILT_IN_PARITYL:
14337 case BUILT_IN_PARITYLL:
14338 case BUILT_IN_PARITYIMAX:
14339 case BUILT_IN_PARITY:
14340 case BUILT_IN_LABS:
14341 case BUILT_IN_LLABS:
14342 case BUILT_IN_PREFETCH:
14343 return true;
14345 default:
14346 return is_simple_builtin (decl);
14349 return false;