re PR bootstrap/46810 (sparc64-linux bootstrap fails with "C++ preprocessor "/lib...
[official-gcc.git] / gcc / builtins.c
blob2816a7e1e618f54c5ea78ac887d1107d70b176dc
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
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"
54 #ifndef SLOW_UNALIGNED_ACCESS
55 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
56 #endif
58 #ifndef PAD_VARARGS_DOWN
59 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
60 #endif
61 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
63 struct target_builtins default_target_builtins;
64 #if SWITCHABLE_TARGET
65 struct target_builtins *this_target_builtins = &default_target_builtins;
66 #endif
68 /* Define the names of the builtin function types and codes. */
69 const char *const built_in_class_names[4]
70 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
72 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
73 const char * built_in_names[(int) END_BUILTINS] =
75 #include "builtins.def"
77 #undef DEF_BUILTIN
79 /* Setup an array of _DECL trees, make sure each element is
80 initialized to NULL_TREE. */
81 tree built_in_decls[(int) END_BUILTINS];
82 /* Declarations used when constructing the builtin implicitly in the compiler.
83 It may be NULL_TREE when this is invalid (for instance runtime is not
84 required to implement the function call in all cases). */
85 tree implicit_built_in_decls[(int) END_BUILTINS];
87 static const char *c_getstr (tree);
88 static rtx c_readstr (const char *, enum machine_mode);
89 static int target_char_cast (tree, char *);
90 static rtx get_memory_rtx (tree, tree);
91 static int apply_args_size (void);
92 static int apply_result_size (void);
93 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
94 static rtx result_vector (int, rtx);
95 #endif
96 static void expand_builtin_update_setjmp_buf (rtx);
97 static void expand_builtin_prefetch (tree);
98 static rtx expand_builtin_apply_args (void);
99 static rtx expand_builtin_apply_args_1 (void);
100 static rtx expand_builtin_apply (rtx, rtx, rtx);
101 static void expand_builtin_return (rtx);
102 static enum type_class type_to_class (tree);
103 static rtx expand_builtin_classify_type (tree);
104 static void expand_errno_check (tree, rtx);
105 static rtx expand_builtin_mathfn (tree, rtx, rtx);
106 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
107 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
109 static rtx expand_builtin_interclass_mathfn (tree, rtx);
110 static rtx expand_builtin_sincos (tree);
111 static rtx expand_builtin_cexpi (tree, rtx);
112 static rtx expand_builtin_int_roundingfn (tree, rtx);
113 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
114 static rtx expand_builtin_next_arg (void);
115 static rtx expand_builtin_va_start (tree);
116 static rtx expand_builtin_va_end (tree);
117 static rtx expand_builtin_va_copy (tree);
118 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_strcmp (tree, rtx);
120 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
121 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memcpy (tree, rtx);
123 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
125 enum machine_mode, int);
126 static rtx expand_builtin_strcpy (tree, rtx);
127 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
128 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strncpy (tree, rtx);
130 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
131 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
133 static rtx expand_builtin_bzero (tree);
134 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_alloca (tree, bool);
136 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
137 static rtx expand_builtin_frame_address (tree, tree);
138 static tree stabilize_va_list_loc (location_t, tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_expect (location_t, tree, tree);
142 static tree fold_builtin_classify_type (tree);
143 static tree fold_builtin_strlen (location_t, tree, tree);
144 static tree fold_builtin_inf (location_t, tree, int);
145 static tree fold_builtin_nan (tree, tree, int);
146 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
147 static bool validate_arg (const_tree, enum tree_code code);
148 static bool integer_valued_real_p (tree);
149 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
150 static bool readonly_data_expr (tree);
151 static rtx expand_builtin_fabs (tree, rtx, rtx);
152 static rtx expand_builtin_signbit (tree, rtx);
153 static tree fold_builtin_sqrt (location_t, tree, tree);
154 static tree fold_builtin_cbrt (location_t, tree, tree);
155 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
156 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_cos (location_t, tree, tree, tree);
158 static tree fold_builtin_cosh (location_t, tree, tree, tree);
159 static tree fold_builtin_tan (tree, tree);
160 static tree fold_builtin_trunc (location_t, tree, tree);
161 static tree fold_builtin_floor (location_t, tree, tree);
162 static tree fold_builtin_ceil (location_t, tree, tree);
163 static tree fold_builtin_round (location_t, tree, tree);
164 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
165 static tree fold_builtin_bitop (tree, tree);
166 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
167 static tree fold_builtin_strchr (location_t, tree, tree, tree);
168 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
169 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
170 static tree fold_builtin_strcmp (location_t, tree, tree);
171 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
172 static tree fold_builtin_signbit (location_t, tree, tree);
173 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
174 static tree fold_builtin_isascii (location_t, tree);
175 static tree fold_builtin_toascii (location_t, tree);
176 static tree fold_builtin_isdigit (location_t, tree);
177 static tree fold_builtin_fabs (location_t, tree, tree);
178 static tree fold_builtin_abs (location_t, tree, tree);
179 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
180 enum tree_code);
181 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
182 static tree fold_builtin_0 (location_t, tree, bool);
183 static tree fold_builtin_1 (location_t, tree, tree, bool);
184 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
185 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
186 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
187 static tree fold_builtin_varargs (location_t, tree, tree, bool);
189 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
190 static tree fold_builtin_strstr (location_t, tree, tree, tree);
191 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
192 static tree fold_builtin_strcat (location_t, tree, tree);
193 static tree fold_builtin_strncat (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
196 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198 static rtx expand_builtin_object_size (tree);
199 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
200 enum built_in_function);
201 static void maybe_emit_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_free_warning (tree);
204 static tree fold_builtin_object_size (tree, tree);
205 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
206 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
207 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
208 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
209 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
210 enum built_in_function);
211 static bool init_target_chars (void);
213 static unsigned HOST_WIDE_INT target_newline;
214 static unsigned HOST_WIDE_INT target_percent;
215 static unsigned HOST_WIDE_INT target_c;
216 static unsigned HOST_WIDE_INT target_s;
217 static char target_percent_c[3];
218 static char target_percent_s[3];
219 static char target_percent_s_newline[4];
220 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
221 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
222 static tree do_mpfr_arg2 (tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_arg3 (tree, tree, tree, tree,
225 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
226 static tree do_mpfr_sincos (tree, tree, tree);
227 static tree do_mpfr_bessel_n (tree, tree, tree,
228 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
229 const REAL_VALUE_TYPE *, bool);
230 static tree do_mpfr_remquo (tree, tree, tree);
231 static tree do_mpfr_lgamma_r (tree, tree, tree);
233 /* Return true if NAME starts with __builtin_ or __sync_. */
235 bool
236 is_builtin_name (const char *name)
238 if (strncmp (name, "__builtin_", 10) == 0)
239 return true;
240 if (strncmp (name, "__sync_", 7) == 0)
241 return true;
242 return false;
246 /* Return true if DECL is a function symbol representing a built-in. */
248 bool
249 is_builtin_fn (tree decl)
251 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
255 /* Return true if NODE should be considered for inline expansion regardless
256 of the optimization level. This means whenever a function is invoked with
257 its "internal" name, which normally contains the prefix "__builtin". */
259 static bool
260 called_as_built_in (tree node)
262 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
263 we want the name used to call the function, not the name it
264 will have. */
265 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
266 return is_builtin_name (name);
269 /* Return the alignment in bits of EXP, an object.
270 Don't return more than MAX_ALIGN no matter what. */
272 unsigned int
273 get_object_alignment (tree exp, unsigned int max_align)
275 HOST_WIDE_INT bitsize, bitpos;
276 tree offset;
277 enum machine_mode mode;
278 int unsignedp, volatilep;
279 unsigned int align, inner;
281 /* Get the innermost object and the constant (bitpos) and possibly
282 variable (offset) offset of the access. */
283 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
284 &mode, &unsignedp, &volatilep, true);
286 /* Extract alignment information from the innermost object and
287 possibly adjust bitpos and offset. */
288 if (TREE_CODE (exp) == CONST_DECL)
289 exp = DECL_INITIAL (exp);
290 if (DECL_P (exp)
291 && TREE_CODE (exp) != LABEL_DECL)
292 align = DECL_ALIGN (exp);
293 else if (CONSTANT_CLASS_P (exp))
295 align = TYPE_ALIGN (TREE_TYPE (exp));
296 #ifdef CONSTANT_ALIGNMENT
297 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
298 #endif
300 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
301 align = TYPE_ALIGN (TREE_TYPE (exp));
302 else if (TREE_CODE (exp) == INDIRECT_REF)
303 align = TYPE_ALIGN (TREE_TYPE (exp));
304 else if (TREE_CODE (exp) == MEM_REF)
306 tree addr = TREE_OPERAND (exp, 0);
307 struct ptr_info_def *pi;
308 if (TREE_CODE (addr) == BIT_AND_EXPR
309 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
311 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
312 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
313 align *= BITS_PER_UNIT;
314 addr = TREE_OPERAND (addr, 0);
316 else
317 align = BITS_PER_UNIT;
318 if (TREE_CODE (addr) == SSA_NAME
319 && (pi = SSA_NAME_PTR_INFO (addr)))
321 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
322 align = MAX (pi->align * BITS_PER_UNIT, align);
324 else if (TREE_CODE (addr) == ADDR_EXPR)
325 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0),
326 max_align));
327 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
329 else if (TREE_CODE (exp) == TARGET_MEM_REF)
331 struct ptr_info_def *pi;
332 tree addr = TMR_BASE (exp);
333 if (TREE_CODE (addr) == BIT_AND_EXPR
334 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
336 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
337 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
338 align *= BITS_PER_UNIT;
339 addr = TREE_OPERAND (addr, 0);
341 else
342 align = BITS_PER_UNIT;
343 if (TREE_CODE (addr) == SSA_NAME
344 && (pi = SSA_NAME_PTR_INFO (addr)))
346 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
347 align = MAX (pi->align * BITS_PER_UNIT, align);
349 else if (TREE_CODE (addr) == ADDR_EXPR)
350 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0),
351 max_align));
352 if (TMR_OFFSET (exp))
353 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
354 if (TMR_INDEX (exp) && TMR_STEP (exp))
356 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
357 align = MIN (align, (step & -step) * BITS_PER_UNIT);
359 else if (TMR_INDEX (exp))
360 align = BITS_PER_UNIT;
361 if (TMR_INDEX2 (exp))
362 align = BITS_PER_UNIT;
364 else
365 align = BITS_PER_UNIT;
367 /* If there is a non-constant offset part extract the maximum
368 alignment that can prevail. */
369 inner = max_align;
370 while (offset)
372 tree next_offset;
374 if (TREE_CODE (offset) == PLUS_EXPR)
376 next_offset = TREE_OPERAND (offset, 0);
377 offset = TREE_OPERAND (offset, 1);
379 else
380 next_offset = NULL;
381 if (host_integerp (offset, 1))
383 /* Any overflow in calculating offset_bits won't change
384 the alignment. */
385 unsigned offset_bits
386 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
388 if (offset_bits)
389 inner = MIN (inner, (offset_bits & -offset_bits));
391 else if (TREE_CODE (offset) == MULT_EXPR
392 && host_integerp (TREE_OPERAND (offset, 1), 1))
394 /* Any overflow in calculating offset_factor won't change
395 the alignment. */
396 unsigned offset_factor
397 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
398 * BITS_PER_UNIT);
400 if (offset_factor)
401 inner = MIN (inner, (offset_factor & -offset_factor));
403 else
405 inner = MIN (inner, BITS_PER_UNIT);
406 break;
408 offset = next_offset;
411 /* Alignment is innermost object alignment adjusted by the constant
412 and non-constant offset parts. */
413 align = MIN (align, inner);
414 bitpos = bitpos & (align - 1);
416 /* align and bitpos now specify known low bits of the pointer.
417 ptr & (align - 1) == bitpos. */
419 if (bitpos != 0)
420 align = (bitpos & -bitpos);
422 return MIN (align, max_align);
425 /* Returns true iff we can trust that alignment information has been
426 calculated properly. */
428 bool
429 can_trust_pointer_alignment (void)
431 /* We rely on TER to compute accurate alignment information. */
432 return (optimize && flag_tree_ter);
435 /* Return the alignment in bits of EXP, a pointer valued expression.
436 But don't return more than MAX_ALIGN no matter what.
437 The alignment returned is, by default, the alignment of the thing that
438 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
440 Otherwise, look at the expression to see if we can do better, i.e., if the
441 expression is actually pointing at an object whose alignment is tighter. */
443 unsigned int
444 get_pointer_alignment (tree exp, unsigned int max_align)
446 STRIP_NOPS (exp);
448 if (TREE_CODE (exp) == ADDR_EXPR)
449 return get_object_alignment (TREE_OPERAND (exp, 0), max_align);
450 else if (TREE_CODE (exp) == SSA_NAME
451 && POINTER_TYPE_P (TREE_TYPE (exp)))
453 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
454 unsigned align;
455 if (!pi)
456 return BITS_PER_UNIT;
457 if (pi->misalign != 0)
458 align = (pi->misalign & -pi->misalign);
459 else
460 align = pi->align;
461 return MIN (max_align, align * BITS_PER_UNIT);
464 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
467 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
468 way, because it could contain a zero byte in the middle.
469 TREE_STRING_LENGTH is the size of the character array, not the string.
471 ONLY_VALUE should be nonzero if the result is not going to be emitted
472 into the instruction stream and zero if it is going to be expanded.
473 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
474 is returned, otherwise NULL, since
475 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
476 evaluate the side-effects.
478 The value returned is of type `ssizetype'.
480 Unfortunately, string_constant can't access the values of const char
481 arrays with initializers, so neither can we do so here. */
483 tree
484 c_strlen (tree src, int only_value)
486 tree offset_node;
487 HOST_WIDE_INT offset;
488 int max;
489 const char *ptr;
490 location_t loc;
492 STRIP_NOPS (src);
493 if (TREE_CODE (src) == COND_EXPR
494 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
496 tree len1, len2;
498 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
499 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
500 if (tree_int_cst_equal (len1, len2))
501 return len1;
504 if (TREE_CODE (src) == COMPOUND_EXPR
505 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
506 return c_strlen (TREE_OPERAND (src, 1), only_value);
508 loc = EXPR_LOC_OR_HERE (src);
510 src = string_constant (src, &offset_node);
511 if (src == 0)
512 return NULL_TREE;
514 max = TREE_STRING_LENGTH (src) - 1;
515 ptr = TREE_STRING_POINTER (src);
517 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
519 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
520 compute the offset to the following null if we don't know where to
521 start searching for it. */
522 int i;
524 for (i = 0; i < max; i++)
525 if (ptr[i] == 0)
526 return NULL_TREE;
528 /* We don't know the starting offset, but we do know that the string
529 has no internal zero bytes. We can assume that the offset falls
530 within the bounds of the string; otherwise, the programmer deserves
531 what he gets. Subtract the offset from the length of the string,
532 and return that. This would perhaps not be valid if we were dealing
533 with named arrays in addition to literal string constants. */
535 return size_diffop_loc (loc, size_int (max), offset_node);
538 /* We have a known offset into the string. Start searching there for
539 a null character if we can represent it as a single HOST_WIDE_INT. */
540 if (offset_node == 0)
541 offset = 0;
542 else if (! host_integerp (offset_node, 0))
543 offset = -1;
544 else
545 offset = tree_low_cst (offset_node, 0);
547 /* If the offset is known to be out of bounds, warn, and call strlen at
548 runtime. */
549 if (offset < 0 || offset > max)
551 /* Suppress multiple warnings for propagated constant strings. */
552 if (! TREE_NO_WARNING (src))
554 warning_at (loc, 0, "offset outside bounds of constant string");
555 TREE_NO_WARNING (src) = 1;
557 return NULL_TREE;
560 /* Use strlen to search for the first zero byte. Since any strings
561 constructed with build_string will have nulls appended, we win even
562 if we get handed something like (char[4])"abcd".
564 Since OFFSET is our starting index into the string, no further
565 calculation is needed. */
566 return ssize_int (strlen (ptr + offset));
569 /* Return a char pointer for a C string if it is a string constant
570 or sum of string constant and integer constant. */
572 static const char *
573 c_getstr (tree src)
575 tree offset_node;
577 src = string_constant (src, &offset_node);
578 if (src == 0)
579 return 0;
581 if (offset_node == 0)
582 return TREE_STRING_POINTER (src);
583 else if (!host_integerp (offset_node, 1)
584 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
585 return 0;
587 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
590 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
591 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
593 static rtx
594 c_readstr (const char *str, enum machine_mode mode)
596 HOST_WIDE_INT c[2];
597 HOST_WIDE_INT ch;
598 unsigned int i, j;
600 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
602 c[0] = 0;
603 c[1] = 0;
604 ch = 1;
605 for (i = 0; i < GET_MODE_SIZE (mode); i++)
607 j = i;
608 if (WORDS_BIG_ENDIAN)
609 j = GET_MODE_SIZE (mode) - i - 1;
610 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
611 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
612 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
613 j *= BITS_PER_UNIT;
614 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
616 if (ch)
617 ch = (unsigned char) str[i];
618 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
620 return immed_double_const (c[0], c[1], mode);
623 /* Cast a target constant CST to target CHAR and if that value fits into
624 host char type, return zero and put that value into variable pointed to by
625 P. */
627 static int
628 target_char_cast (tree cst, char *p)
630 unsigned HOST_WIDE_INT val, hostval;
632 if (TREE_CODE (cst) != INTEGER_CST
633 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
634 return 1;
636 val = TREE_INT_CST_LOW (cst);
637 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
638 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
640 hostval = val;
641 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
642 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
644 if (val != hostval)
645 return 1;
647 *p = hostval;
648 return 0;
651 /* Similar to save_expr, but assumes that arbitrary code is not executed
652 in between the multiple evaluations. In particular, we assume that a
653 non-addressable local variable will not be modified. */
655 static tree
656 builtin_save_expr (tree exp)
658 if (TREE_ADDRESSABLE (exp) == 0
659 && (TREE_CODE (exp) == PARM_DECL
660 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
661 return exp;
663 return save_expr (exp);
666 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
667 times to get the address of either a higher stack frame, or a return
668 address located within it (depending on FNDECL_CODE). */
670 static rtx
671 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
673 int i;
675 #ifdef INITIAL_FRAME_ADDRESS_RTX
676 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
677 #else
678 rtx tem;
680 /* For a zero count with __builtin_return_address, we don't care what
681 frame address we return, because target-specific definitions will
682 override us. Therefore frame pointer elimination is OK, and using
683 the soft frame pointer is OK.
685 For a nonzero count, or a zero count with __builtin_frame_address,
686 we require a stable offset from the current frame pointer to the
687 previous one, so we must use the hard frame pointer, and
688 we must disable frame pointer elimination. */
689 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
690 tem = frame_pointer_rtx;
691 else
693 tem = hard_frame_pointer_rtx;
695 /* Tell reload not to eliminate the frame pointer. */
696 crtl->accesses_prior_frames = 1;
698 #endif
700 /* Some machines need special handling before we can access
701 arbitrary frames. For example, on the SPARC, we must first flush
702 all register windows to the stack. */
703 #ifdef SETUP_FRAME_ADDRESSES
704 if (count > 0)
705 SETUP_FRAME_ADDRESSES ();
706 #endif
708 /* On the SPARC, the return address is not in the frame, it is in a
709 register. There is no way to access it off of the current frame
710 pointer, but it can be accessed off the previous frame pointer by
711 reading the value from the register window save area. */
712 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
713 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
714 count--;
715 #endif
717 /* Scan back COUNT frames to the specified frame. */
718 for (i = 0; i < count; i++)
720 /* Assume the dynamic chain pointer is in the word that the
721 frame address points to, unless otherwise specified. */
722 #ifdef DYNAMIC_CHAIN_ADDRESS
723 tem = DYNAMIC_CHAIN_ADDRESS (tem);
724 #endif
725 tem = memory_address (Pmode, tem);
726 tem = gen_frame_mem (Pmode, tem);
727 tem = copy_to_reg (tem);
730 /* For __builtin_frame_address, return what we've got. But, on
731 the SPARC for example, we may have to add a bias. */
732 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
733 #ifdef FRAME_ADDR_RTX
734 return FRAME_ADDR_RTX (tem);
735 #else
736 return tem;
737 #endif
739 /* For __builtin_return_address, get the return address from that frame. */
740 #ifdef RETURN_ADDR_RTX
741 tem = RETURN_ADDR_RTX (count, tem);
742 #else
743 tem = memory_address (Pmode,
744 plus_constant (tem, GET_MODE_SIZE (Pmode)));
745 tem = gen_frame_mem (Pmode, tem);
746 #endif
747 return tem;
750 /* Alias set used for setjmp buffer. */
751 static alias_set_type setjmp_alias_set = -1;
753 /* Construct the leading half of a __builtin_setjmp call. Control will
754 return to RECEIVER_LABEL. This is also called directly by the SJLJ
755 exception handling code. */
757 void
758 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
760 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
761 rtx stack_save;
762 rtx mem;
764 if (setjmp_alias_set == -1)
765 setjmp_alias_set = new_alias_set ();
767 buf_addr = convert_memory_address (Pmode, buf_addr);
769 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
771 /* We store the frame pointer and the address of receiver_label in
772 the buffer and use the rest of it for the stack save area, which
773 is machine-dependent. */
775 mem = gen_rtx_MEM (Pmode, buf_addr);
776 set_mem_alias_set (mem, setjmp_alias_set);
777 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
779 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
780 set_mem_alias_set (mem, setjmp_alias_set);
782 emit_move_insn (validize_mem (mem),
783 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
785 stack_save = gen_rtx_MEM (sa_mode,
786 plus_constant (buf_addr,
787 2 * GET_MODE_SIZE (Pmode)));
788 set_mem_alias_set (stack_save, setjmp_alias_set);
789 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
791 /* If there is further processing to do, do it. */
792 #ifdef HAVE_builtin_setjmp_setup
793 if (HAVE_builtin_setjmp_setup)
794 emit_insn (gen_builtin_setjmp_setup (buf_addr));
795 #endif
797 /* Tell optimize_save_area_alloca that extra work is going to
798 need to go on during alloca. */
799 cfun->calls_setjmp = 1;
801 /* We have a nonlocal label. */
802 cfun->has_nonlocal_label = 1;
805 /* Construct the trailing part of a __builtin_setjmp call. This is
806 also called directly by the SJLJ exception handling code. */
808 void
809 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
811 rtx chain;
813 /* Clobber the FP when we get here, so we have to make sure it's
814 marked as used by this function. */
815 emit_use (hard_frame_pointer_rtx);
817 /* Mark the static chain as clobbered here so life information
818 doesn't get messed up for it. */
819 chain = targetm.calls.static_chain (current_function_decl, true);
820 if (chain && REG_P (chain))
821 emit_clobber (chain);
823 /* Now put in the code to restore the frame pointer, and argument
824 pointer, if needed. */
825 #ifdef HAVE_nonlocal_goto
826 if (! HAVE_nonlocal_goto)
827 #endif
829 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
830 /* This might change the hard frame pointer in ways that aren't
831 apparent to early optimization passes, so force a clobber. */
832 emit_clobber (hard_frame_pointer_rtx);
835 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
836 if (fixed_regs[ARG_POINTER_REGNUM])
838 #ifdef ELIMINABLE_REGS
839 size_t i;
840 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
842 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
843 if (elim_regs[i].from == ARG_POINTER_REGNUM
844 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
845 break;
847 if (i == ARRAY_SIZE (elim_regs))
848 #endif
850 /* Now restore our arg pointer from the address at which it
851 was saved in our stack frame. */
852 emit_move_insn (crtl->args.internal_arg_pointer,
853 copy_to_reg (get_arg_pointer_save_area ()));
856 #endif
858 #ifdef HAVE_builtin_setjmp_receiver
859 if (HAVE_builtin_setjmp_receiver)
860 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
861 else
862 #endif
863 #ifdef HAVE_nonlocal_goto_receiver
864 if (HAVE_nonlocal_goto_receiver)
865 emit_insn (gen_nonlocal_goto_receiver ());
866 else
867 #endif
868 { /* Nothing */ }
870 /* We must not allow the code we just generated to be reordered by
871 scheduling. Specifically, the update of the frame pointer must
872 happen immediately, not later. */
873 emit_insn (gen_blockage ());
876 /* __builtin_longjmp is passed a pointer to an array of five words (not
877 all will be used on all machines). It operates similarly to the C
878 library function of the same name, but is more efficient. Much of
879 the code below is copied from the handling of non-local gotos. */
881 static void
882 expand_builtin_longjmp (rtx buf_addr, rtx value)
884 rtx fp, lab, stack, insn, last;
885 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
887 /* DRAP is needed for stack realign if longjmp is expanded to current
888 function */
889 if (SUPPORTS_STACK_ALIGNMENT)
890 crtl->need_drap = true;
892 if (setjmp_alias_set == -1)
893 setjmp_alias_set = new_alias_set ();
895 buf_addr = convert_memory_address (Pmode, buf_addr);
897 buf_addr = force_reg (Pmode, buf_addr);
899 /* We require that the user must pass a second argument of 1, because
900 that is what builtin_setjmp will return. */
901 gcc_assert (value == const1_rtx);
903 last = get_last_insn ();
904 #ifdef HAVE_builtin_longjmp
905 if (HAVE_builtin_longjmp)
906 emit_insn (gen_builtin_longjmp (buf_addr));
907 else
908 #endif
910 fp = gen_rtx_MEM (Pmode, buf_addr);
911 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
912 GET_MODE_SIZE (Pmode)));
914 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
915 2 * GET_MODE_SIZE (Pmode)));
916 set_mem_alias_set (fp, setjmp_alias_set);
917 set_mem_alias_set (lab, setjmp_alias_set);
918 set_mem_alias_set (stack, setjmp_alias_set);
920 /* Pick up FP, label, and SP from the block and jump. This code is
921 from expand_goto in stmt.c; see there for detailed comments. */
922 #ifdef HAVE_nonlocal_goto
923 if (HAVE_nonlocal_goto)
924 /* We have to pass a value to the nonlocal_goto pattern that will
925 get copied into the static_chain pointer, but it does not matter
926 what that value is, because builtin_setjmp does not use it. */
927 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
928 else
929 #endif
931 lab = copy_to_reg (lab);
933 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
934 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
936 emit_move_insn (hard_frame_pointer_rtx, fp);
937 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
939 emit_use (hard_frame_pointer_rtx);
940 emit_use (stack_pointer_rtx);
941 emit_indirect_jump (lab);
945 /* Search backwards and mark the jump insn as a non-local goto.
946 Note that this precludes the use of __builtin_longjmp to a
947 __builtin_setjmp target in the same function. However, we've
948 already cautioned the user that these functions are for
949 internal exception handling use only. */
950 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
952 gcc_assert (insn != last);
954 if (JUMP_P (insn))
956 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
957 break;
959 else if (CALL_P (insn))
960 break;
964 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
965 and the address of the save area. */
967 static rtx
968 expand_builtin_nonlocal_goto (tree exp)
970 tree t_label, t_save_area;
971 rtx r_label, r_save_area, r_fp, r_sp, insn;
973 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
974 return NULL_RTX;
976 t_label = CALL_EXPR_ARG (exp, 0);
977 t_save_area = CALL_EXPR_ARG (exp, 1);
979 r_label = expand_normal (t_label);
980 r_label = convert_memory_address (Pmode, r_label);
981 r_save_area = expand_normal (t_save_area);
982 r_save_area = convert_memory_address (Pmode, r_save_area);
983 /* Copy the address of the save location to a register just in case it was based
984 on the frame pointer. */
985 r_save_area = copy_to_reg (r_save_area);
986 r_fp = gen_rtx_MEM (Pmode, r_save_area);
987 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
988 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
990 crtl->has_nonlocal_goto = 1;
992 #ifdef HAVE_nonlocal_goto
993 /* ??? We no longer need to pass the static chain value, afaik. */
994 if (HAVE_nonlocal_goto)
995 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
996 else
997 #endif
999 r_label = copy_to_reg (r_label);
1001 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1002 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1004 /* Restore frame pointer for containing function.
1005 This sets the actual hard register used for the frame pointer
1006 to the location of the function's incoming static chain info.
1007 The non-local goto handler will then adjust it to contain the
1008 proper value and reload the argument pointer, if needed. */
1009 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1010 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
1012 /* USE of hard_frame_pointer_rtx added for consistency;
1013 not clear if really needed. */
1014 emit_use (hard_frame_pointer_rtx);
1015 emit_use (stack_pointer_rtx);
1017 /* If the architecture is using a GP register, we must
1018 conservatively assume that the target function makes use of it.
1019 The prologue of functions with nonlocal gotos must therefore
1020 initialize the GP register to the appropriate value, and we
1021 must then make sure that this value is live at the point
1022 of the jump. (Note that this doesn't necessarily apply
1023 to targets with a nonlocal_goto pattern; they are free
1024 to implement it in their own way. Note also that this is
1025 a no-op if the GP register is a global invariant.) */
1026 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1027 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1028 emit_use (pic_offset_table_rtx);
1030 emit_indirect_jump (r_label);
1033 /* Search backwards to the jump insn and mark it as a
1034 non-local goto. */
1035 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1037 if (JUMP_P (insn))
1039 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1040 break;
1042 else if (CALL_P (insn))
1043 break;
1046 return const0_rtx;
1049 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1050 (not all will be used on all machines) that was passed to __builtin_setjmp.
1051 It updates the stack pointer in that block to correspond to the current
1052 stack pointer. */
1054 static void
1055 expand_builtin_update_setjmp_buf (rtx buf_addr)
1057 enum machine_mode sa_mode = Pmode;
1058 rtx stack_save;
1061 #ifdef HAVE_save_stack_nonlocal
1062 if (HAVE_save_stack_nonlocal)
1063 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1064 #endif
1065 #ifdef STACK_SAVEAREA_MODE
1066 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1067 #endif
1069 stack_save
1070 = gen_rtx_MEM (sa_mode,
1071 memory_address
1072 (sa_mode,
1073 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1075 #ifdef HAVE_setjmp
1076 if (HAVE_setjmp)
1077 emit_insn (gen_setjmp ());
1078 #endif
1080 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1083 /* Expand a call to __builtin_prefetch. For a target that does not support
1084 data prefetch, evaluate the memory address argument in case it has side
1085 effects. */
1087 static void
1088 expand_builtin_prefetch (tree exp)
1090 tree arg0, arg1, arg2;
1091 int nargs;
1092 rtx op0, op1, op2;
1094 if (!validate_arglist (exp, POINTER_TYPE, 0))
1095 return;
1097 arg0 = CALL_EXPR_ARG (exp, 0);
1099 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1100 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1101 locality). */
1102 nargs = call_expr_nargs (exp);
1103 if (nargs > 1)
1104 arg1 = CALL_EXPR_ARG (exp, 1);
1105 else
1106 arg1 = integer_zero_node;
1107 if (nargs > 2)
1108 arg2 = CALL_EXPR_ARG (exp, 2);
1109 else
1110 arg2 = integer_three_node;
1112 /* Argument 0 is an address. */
1113 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1115 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1116 if (TREE_CODE (arg1) != INTEGER_CST)
1118 error ("second argument to %<__builtin_prefetch%> must be a constant");
1119 arg1 = integer_zero_node;
1121 op1 = expand_normal (arg1);
1122 /* Argument 1 must be either zero or one. */
1123 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1125 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1126 " using zero");
1127 op1 = const0_rtx;
1130 /* Argument 2 (locality) must be a compile-time constant int. */
1131 if (TREE_CODE (arg2) != INTEGER_CST)
1133 error ("third argument to %<__builtin_prefetch%> must be a constant");
1134 arg2 = integer_zero_node;
1136 op2 = expand_normal (arg2);
1137 /* Argument 2 must be 0, 1, 2, or 3. */
1138 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1140 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1141 op2 = const0_rtx;
1144 #ifdef HAVE_prefetch
1145 if (HAVE_prefetch)
1147 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1148 (op0,
1149 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1150 || (GET_MODE (op0) != Pmode))
1152 op0 = convert_memory_address (Pmode, op0);
1153 op0 = force_reg (Pmode, op0);
1155 emit_insn (gen_prefetch (op0, op1, op2));
1157 #endif
1159 /* Don't do anything with direct references to volatile memory, but
1160 generate code to handle other side effects. */
1161 if (!MEM_P (op0) && side_effects_p (op0))
1162 emit_insn (op0);
1165 /* Get a MEM rtx for expression EXP which is the address of an operand
1166 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1167 the maximum length of the block of memory that might be accessed or
1168 NULL if unknown. */
1170 static rtx
1171 get_memory_rtx (tree exp, tree len)
1173 tree orig_exp = exp;
1174 rtx addr, mem;
1175 HOST_WIDE_INT off;
1177 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1178 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1179 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1180 exp = TREE_OPERAND (exp, 0);
1182 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1183 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1185 /* Get an expression we can use to find the attributes to assign to MEM.
1186 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1187 we can. First remove any nops. */
1188 while (CONVERT_EXPR_P (exp)
1189 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1190 exp = TREE_OPERAND (exp, 0);
1192 off = 0;
1193 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1194 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1195 && host_integerp (TREE_OPERAND (exp, 1), 0)
1196 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1197 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1198 else if (TREE_CODE (exp) == ADDR_EXPR)
1199 exp = TREE_OPERAND (exp, 0);
1200 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1201 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1202 else
1203 exp = NULL;
1205 /* Honor attributes derived from exp, except for the alias set
1206 (as builtin stringops may alias with anything) and the size
1207 (as stringops may access multiple array elements). */
1208 if (exp)
1210 set_mem_attributes (mem, exp, 0);
1212 if (off)
1213 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1215 /* Allow the string and memory builtins to overflow from one
1216 field into another, see http://gcc.gnu.org/PR23561.
1217 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1218 memory accessed by the string or memory builtin will fit
1219 within the field. */
1220 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1222 tree mem_expr = MEM_EXPR (mem);
1223 HOST_WIDE_INT offset = -1, length = -1;
1224 tree inner = exp;
1226 while (TREE_CODE (inner) == ARRAY_REF
1227 || CONVERT_EXPR_P (inner)
1228 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1229 || TREE_CODE (inner) == SAVE_EXPR)
1230 inner = TREE_OPERAND (inner, 0);
1232 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1234 if (MEM_OFFSET (mem)
1235 && CONST_INT_P (MEM_OFFSET (mem)))
1236 offset = INTVAL (MEM_OFFSET (mem));
1238 if (offset >= 0 && len && host_integerp (len, 0))
1239 length = tree_low_cst (len, 0);
1241 while (TREE_CODE (inner) == COMPONENT_REF)
1243 tree field = TREE_OPERAND (inner, 1);
1244 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1245 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1247 /* Bitfields are generally not byte-addressable. */
1248 gcc_assert (!DECL_BIT_FIELD (field)
1249 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1250 % BITS_PER_UNIT) == 0
1251 && host_integerp (DECL_SIZE (field), 0)
1252 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1253 % BITS_PER_UNIT) == 0));
1255 /* If we can prove that the memory starting at XEXP (mem, 0) and
1256 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1257 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1258 fields without DECL_SIZE_UNIT like flexible array members. */
1259 if (length >= 0
1260 && DECL_SIZE_UNIT (field)
1261 && host_integerp (DECL_SIZE_UNIT (field), 0))
1263 HOST_WIDE_INT size
1264 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1265 if (offset <= size
1266 && length <= size
1267 && offset + length <= size)
1268 break;
1271 if (offset >= 0
1272 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1273 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1274 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1275 / BITS_PER_UNIT;
1276 else
1278 offset = -1;
1279 length = -1;
1282 mem_expr = TREE_OPERAND (mem_expr, 0);
1283 inner = TREE_OPERAND (inner, 0);
1286 if (mem_expr == NULL)
1287 offset = -1;
1288 if (mem_expr != MEM_EXPR (mem))
1290 set_mem_expr (mem, mem_expr);
1291 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1294 set_mem_alias_set (mem, 0);
1295 set_mem_size (mem, NULL_RTX);
1298 return mem;
1301 /* Built-in functions to perform an untyped call and return. */
1303 #define apply_args_mode \
1304 (this_target_builtins->x_apply_args_mode)
1305 #define apply_result_mode \
1306 (this_target_builtins->x_apply_result_mode)
1308 /* Return the size required for the block returned by __builtin_apply_args,
1309 and initialize apply_args_mode. */
1311 static int
1312 apply_args_size (void)
1314 static int size = -1;
1315 int align;
1316 unsigned int regno;
1317 enum machine_mode mode;
1319 /* The values computed by this function never change. */
1320 if (size < 0)
1322 /* The first value is the incoming arg-pointer. */
1323 size = GET_MODE_SIZE (Pmode);
1325 /* The second value is the structure value address unless this is
1326 passed as an "invisible" first argument. */
1327 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1328 size += GET_MODE_SIZE (Pmode);
1330 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1331 if (FUNCTION_ARG_REGNO_P (regno))
1333 mode = targetm.calls.get_raw_arg_mode (regno);
1335 gcc_assert (mode != VOIDmode);
1337 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1338 if (size % align != 0)
1339 size = CEIL (size, align) * align;
1340 size += GET_MODE_SIZE (mode);
1341 apply_args_mode[regno] = mode;
1343 else
1345 apply_args_mode[regno] = VOIDmode;
1348 return size;
1351 /* Return the size required for the block returned by __builtin_apply,
1352 and initialize apply_result_mode. */
1354 static int
1355 apply_result_size (void)
1357 static int size = -1;
1358 int align, regno;
1359 enum machine_mode mode;
1361 /* The values computed by this function never change. */
1362 if (size < 0)
1364 size = 0;
1366 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1367 if (targetm.calls.function_value_regno_p (regno))
1369 mode = targetm.calls.get_raw_result_mode (regno);
1371 gcc_assert (mode != VOIDmode);
1373 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1374 if (size % align != 0)
1375 size = CEIL (size, align) * align;
1376 size += GET_MODE_SIZE (mode);
1377 apply_result_mode[regno] = mode;
1379 else
1380 apply_result_mode[regno] = VOIDmode;
1382 /* Allow targets that use untyped_call and untyped_return to override
1383 the size so that machine-specific information can be stored here. */
1384 #ifdef APPLY_RESULT_SIZE
1385 size = APPLY_RESULT_SIZE;
1386 #endif
1388 return size;
1391 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1392 /* Create a vector describing the result block RESULT. If SAVEP is true,
1393 the result block is used to save the values; otherwise it is used to
1394 restore the values. */
1396 static rtx
1397 result_vector (int savep, rtx result)
1399 int regno, size, align, nelts;
1400 enum machine_mode mode;
1401 rtx reg, mem;
1402 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1404 size = nelts = 0;
1405 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1406 if ((mode = apply_result_mode[regno]) != VOIDmode)
1408 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1409 if (size % align != 0)
1410 size = CEIL (size, align) * align;
1411 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1412 mem = adjust_address (result, mode, size);
1413 savevec[nelts++] = (savep
1414 ? gen_rtx_SET (VOIDmode, mem, reg)
1415 : gen_rtx_SET (VOIDmode, reg, mem));
1416 size += GET_MODE_SIZE (mode);
1418 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1420 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1422 /* Save the state required to perform an untyped call with the same
1423 arguments as were passed to the current function. */
1425 static rtx
1426 expand_builtin_apply_args_1 (void)
1428 rtx registers, tem;
1429 int size, align, regno;
1430 enum machine_mode mode;
1431 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1433 /* Create a block where the arg-pointer, structure value address,
1434 and argument registers can be saved. */
1435 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1437 /* Walk past the arg-pointer and structure value address. */
1438 size = GET_MODE_SIZE (Pmode);
1439 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1440 size += GET_MODE_SIZE (Pmode);
1442 /* Save each register used in calling a function to the block. */
1443 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1444 if ((mode = apply_args_mode[regno]) != VOIDmode)
1446 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1447 if (size % align != 0)
1448 size = CEIL (size, align) * align;
1450 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1452 emit_move_insn (adjust_address (registers, mode, size), tem);
1453 size += GET_MODE_SIZE (mode);
1456 /* Save the arg pointer to the block. */
1457 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1458 #ifdef STACK_GROWS_DOWNWARD
1459 /* We need the pointer as the caller actually passed them to us, not
1460 as we might have pretended they were passed. Make sure it's a valid
1461 operand, as emit_move_insn isn't expected to handle a PLUS. */
1463 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1464 NULL_RTX);
1465 #endif
1466 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1468 size = GET_MODE_SIZE (Pmode);
1470 /* Save the structure value address unless this is passed as an
1471 "invisible" first argument. */
1472 if (struct_incoming_value)
1474 emit_move_insn (adjust_address (registers, Pmode, size),
1475 copy_to_reg (struct_incoming_value));
1476 size += GET_MODE_SIZE (Pmode);
1479 /* Return the address of the block. */
1480 return copy_addr_to_reg (XEXP (registers, 0));
1483 /* __builtin_apply_args returns block of memory allocated on
1484 the stack into which is stored the arg pointer, structure
1485 value address, static chain, and all the registers that might
1486 possibly be used in performing a function call. The code is
1487 moved to the start of the function so the incoming values are
1488 saved. */
1490 static rtx
1491 expand_builtin_apply_args (void)
1493 /* Don't do __builtin_apply_args more than once in a function.
1494 Save the result of the first call and reuse it. */
1495 if (apply_args_value != 0)
1496 return apply_args_value;
1498 /* When this function is called, it means that registers must be
1499 saved on entry to this function. So we migrate the
1500 call to the first insn of this function. */
1501 rtx temp;
1502 rtx seq;
1504 start_sequence ();
1505 temp = expand_builtin_apply_args_1 ();
1506 seq = get_insns ();
1507 end_sequence ();
1509 apply_args_value = temp;
1511 /* Put the insns after the NOTE that starts the function.
1512 If this is inside a start_sequence, make the outer-level insn
1513 chain current, so the code is placed at the start of the
1514 function. If internal_arg_pointer is a non-virtual pseudo,
1515 it needs to be placed after the function that initializes
1516 that pseudo. */
1517 push_topmost_sequence ();
1518 if (REG_P (crtl->args.internal_arg_pointer)
1519 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1520 emit_insn_before (seq, parm_birth_insn);
1521 else
1522 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1523 pop_topmost_sequence ();
1524 return temp;
1528 /* Perform an untyped call and save the state required to perform an
1529 untyped return of whatever value was returned by the given function. */
1531 static rtx
1532 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1534 int size, align, regno;
1535 enum machine_mode mode;
1536 rtx incoming_args, result, reg, dest, src, call_insn;
1537 rtx old_stack_level = 0;
1538 rtx call_fusage = 0;
1539 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1541 arguments = convert_memory_address (Pmode, arguments);
1543 /* Create a block where the return registers can be saved. */
1544 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1546 /* Fetch the arg pointer from the ARGUMENTS block. */
1547 incoming_args = gen_reg_rtx (Pmode);
1548 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1549 #ifndef STACK_GROWS_DOWNWARD
1550 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1551 incoming_args, 0, OPTAB_LIB_WIDEN);
1552 #endif
1554 /* Push a new argument block and copy the arguments. Do not allow
1555 the (potential) memcpy call below to interfere with our stack
1556 manipulations. */
1557 do_pending_stack_adjust ();
1558 NO_DEFER_POP;
1560 /* Save the stack with nonlocal if available. */
1561 #ifdef HAVE_save_stack_nonlocal
1562 if (HAVE_save_stack_nonlocal)
1563 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1564 else
1565 #endif
1566 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1568 /* Allocate a block of memory onto the stack and copy the memory
1569 arguments to the outgoing arguments address. We can pass TRUE
1570 as the 4th argument because we just saved the stack pointer
1571 and will restore it right after the call. */
1572 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1574 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1575 may have already set current_function_calls_alloca to true.
1576 current_function_calls_alloca won't be set if argsize is zero,
1577 so we have to guarantee need_drap is true here. */
1578 if (SUPPORTS_STACK_ALIGNMENT)
1579 crtl->need_drap = true;
1581 dest = virtual_outgoing_args_rtx;
1582 #ifndef STACK_GROWS_DOWNWARD
1583 if (CONST_INT_P (argsize))
1584 dest = plus_constant (dest, -INTVAL (argsize));
1585 else
1586 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1587 #endif
1588 dest = gen_rtx_MEM (BLKmode, dest);
1589 set_mem_align (dest, PARM_BOUNDARY);
1590 src = gen_rtx_MEM (BLKmode, incoming_args);
1591 set_mem_align (src, PARM_BOUNDARY);
1592 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1594 /* Refer to the argument block. */
1595 apply_args_size ();
1596 arguments = gen_rtx_MEM (BLKmode, arguments);
1597 set_mem_align (arguments, PARM_BOUNDARY);
1599 /* Walk past the arg-pointer and structure value address. */
1600 size = GET_MODE_SIZE (Pmode);
1601 if (struct_value)
1602 size += GET_MODE_SIZE (Pmode);
1604 /* Restore each of the registers previously saved. Make USE insns
1605 for each of these registers for use in making the call. */
1606 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1607 if ((mode = apply_args_mode[regno]) != VOIDmode)
1609 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1610 if (size % align != 0)
1611 size = CEIL (size, align) * align;
1612 reg = gen_rtx_REG (mode, regno);
1613 emit_move_insn (reg, adjust_address (arguments, mode, size));
1614 use_reg (&call_fusage, reg);
1615 size += GET_MODE_SIZE (mode);
1618 /* Restore the structure value address unless this is passed as an
1619 "invisible" first argument. */
1620 size = GET_MODE_SIZE (Pmode);
1621 if (struct_value)
1623 rtx value = gen_reg_rtx (Pmode);
1624 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1625 emit_move_insn (struct_value, value);
1626 if (REG_P (struct_value))
1627 use_reg (&call_fusage, struct_value);
1628 size += GET_MODE_SIZE (Pmode);
1631 /* All arguments and registers used for the call are set up by now! */
1632 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1634 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1635 and we don't want to load it into a register as an optimization,
1636 because prepare_call_address already did it if it should be done. */
1637 if (GET_CODE (function) != SYMBOL_REF)
1638 function = memory_address (FUNCTION_MODE, function);
1640 /* Generate the actual call instruction and save the return value. */
1641 #ifdef HAVE_untyped_call
1642 if (HAVE_untyped_call)
1643 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1644 result, result_vector (1, result)));
1645 else
1646 #endif
1647 #ifdef HAVE_call_value
1648 if (HAVE_call_value)
1650 rtx valreg = 0;
1652 /* Locate the unique return register. It is not possible to
1653 express a call that sets more than one return register using
1654 call_value; use untyped_call for that. In fact, untyped_call
1655 only needs to save the return registers in the given block. */
1656 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1657 if ((mode = apply_result_mode[regno]) != VOIDmode)
1659 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1661 valreg = gen_rtx_REG (mode, regno);
1664 emit_call_insn (GEN_CALL_VALUE (valreg,
1665 gen_rtx_MEM (FUNCTION_MODE, function),
1666 const0_rtx, NULL_RTX, const0_rtx));
1668 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1670 else
1671 #endif
1672 gcc_unreachable ();
1674 /* Find the CALL insn we just emitted, and attach the register usage
1675 information. */
1676 call_insn = last_call_insn ();
1677 add_function_usage_to (call_insn, call_fusage);
1679 /* Restore the stack. */
1680 #ifdef HAVE_save_stack_nonlocal
1681 if (HAVE_save_stack_nonlocal)
1682 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1683 else
1684 #endif
1685 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1687 OK_DEFER_POP;
1689 /* Return the address of the result block. */
1690 result = copy_addr_to_reg (XEXP (result, 0));
1691 return convert_memory_address (ptr_mode, result);
1694 /* Perform an untyped return. */
1696 static void
1697 expand_builtin_return (rtx result)
1699 int size, align, regno;
1700 enum machine_mode mode;
1701 rtx reg;
1702 rtx call_fusage = 0;
1704 result = convert_memory_address (Pmode, result);
1706 apply_result_size ();
1707 result = gen_rtx_MEM (BLKmode, result);
1709 #ifdef HAVE_untyped_return
1710 if (HAVE_untyped_return)
1712 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1713 emit_barrier ();
1714 return;
1716 #endif
1718 /* Restore the return value and note that each value is used. */
1719 size = 0;
1720 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1721 if ((mode = apply_result_mode[regno]) != VOIDmode)
1723 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1724 if (size % align != 0)
1725 size = CEIL (size, align) * align;
1726 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1727 emit_move_insn (reg, adjust_address (result, mode, size));
1729 push_to_sequence (call_fusage);
1730 emit_use (reg);
1731 call_fusage = get_insns ();
1732 end_sequence ();
1733 size += GET_MODE_SIZE (mode);
1736 /* Put the USE insns before the return. */
1737 emit_insn (call_fusage);
1739 /* Return whatever values was restored by jumping directly to the end
1740 of the function. */
1741 expand_naked_return ();
1744 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1746 static enum type_class
1747 type_to_class (tree type)
1749 switch (TREE_CODE (type))
1751 case VOID_TYPE: return void_type_class;
1752 case INTEGER_TYPE: return integer_type_class;
1753 case ENUMERAL_TYPE: return enumeral_type_class;
1754 case BOOLEAN_TYPE: return boolean_type_class;
1755 case POINTER_TYPE: return pointer_type_class;
1756 case REFERENCE_TYPE: return reference_type_class;
1757 case OFFSET_TYPE: return offset_type_class;
1758 case REAL_TYPE: return real_type_class;
1759 case COMPLEX_TYPE: return complex_type_class;
1760 case FUNCTION_TYPE: return function_type_class;
1761 case METHOD_TYPE: return method_type_class;
1762 case RECORD_TYPE: return record_type_class;
1763 case UNION_TYPE:
1764 case QUAL_UNION_TYPE: return union_type_class;
1765 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1766 ? string_type_class : array_type_class);
1767 case LANG_TYPE: return lang_type_class;
1768 default: return no_type_class;
1772 /* Expand a call EXP to __builtin_classify_type. */
1774 static rtx
1775 expand_builtin_classify_type (tree exp)
1777 if (call_expr_nargs (exp))
1778 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1779 return GEN_INT (no_type_class);
1782 /* This helper macro, meant to be used in mathfn_built_in below,
1783 determines which among a set of three builtin math functions is
1784 appropriate for a given type mode. The `F' and `L' cases are
1785 automatically generated from the `double' case. */
1786 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1787 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1788 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1789 fcodel = BUILT_IN_MATHFN##L ; break;
1790 /* Similar to above, but appends _R after any F/L suffix. */
1791 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1792 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1793 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1794 fcodel = BUILT_IN_MATHFN##L_R ; break;
1796 /* Return mathematic function equivalent to FN but operating directly
1797 on TYPE, if available. If IMPLICIT is true find the function in
1798 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1799 can't do the conversion, return zero. */
1801 static tree
1802 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1804 tree const *const fn_arr
1805 = implicit ? implicit_built_in_decls : built_in_decls;
1806 enum built_in_function fcode, fcodef, fcodel;
1808 switch (fn)
1810 CASE_MATHFN (BUILT_IN_ACOS)
1811 CASE_MATHFN (BUILT_IN_ACOSH)
1812 CASE_MATHFN (BUILT_IN_ASIN)
1813 CASE_MATHFN (BUILT_IN_ASINH)
1814 CASE_MATHFN (BUILT_IN_ATAN)
1815 CASE_MATHFN (BUILT_IN_ATAN2)
1816 CASE_MATHFN (BUILT_IN_ATANH)
1817 CASE_MATHFN (BUILT_IN_CBRT)
1818 CASE_MATHFN (BUILT_IN_CEIL)
1819 CASE_MATHFN (BUILT_IN_CEXPI)
1820 CASE_MATHFN (BUILT_IN_COPYSIGN)
1821 CASE_MATHFN (BUILT_IN_COS)
1822 CASE_MATHFN (BUILT_IN_COSH)
1823 CASE_MATHFN (BUILT_IN_DREM)
1824 CASE_MATHFN (BUILT_IN_ERF)
1825 CASE_MATHFN (BUILT_IN_ERFC)
1826 CASE_MATHFN (BUILT_IN_EXP)
1827 CASE_MATHFN (BUILT_IN_EXP10)
1828 CASE_MATHFN (BUILT_IN_EXP2)
1829 CASE_MATHFN (BUILT_IN_EXPM1)
1830 CASE_MATHFN (BUILT_IN_FABS)
1831 CASE_MATHFN (BUILT_IN_FDIM)
1832 CASE_MATHFN (BUILT_IN_FLOOR)
1833 CASE_MATHFN (BUILT_IN_FMA)
1834 CASE_MATHFN (BUILT_IN_FMAX)
1835 CASE_MATHFN (BUILT_IN_FMIN)
1836 CASE_MATHFN (BUILT_IN_FMOD)
1837 CASE_MATHFN (BUILT_IN_FREXP)
1838 CASE_MATHFN (BUILT_IN_GAMMA)
1839 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1840 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1841 CASE_MATHFN (BUILT_IN_HYPOT)
1842 CASE_MATHFN (BUILT_IN_ILOGB)
1843 CASE_MATHFN (BUILT_IN_INF)
1844 CASE_MATHFN (BUILT_IN_ISINF)
1845 CASE_MATHFN (BUILT_IN_J0)
1846 CASE_MATHFN (BUILT_IN_J1)
1847 CASE_MATHFN (BUILT_IN_JN)
1848 CASE_MATHFN (BUILT_IN_LCEIL)
1849 CASE_MATHFN (BUILT_IN_LDEXP)
1850 CASE_MATHFN (BUILT_IN_LFLOOR)
1851 CASE_MATHFN (BUILT_IN_LGAMMA)
1852 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1853 CASE_MATHFN (BUILT_IN_LLCEIL)
1854 CASE_MATHFN (BUILT_IN_LLFLOOR)
1855 CASE_MATHFN (BUILT_IN_LLRINT)
1856 CASE_MATHFN (BUILT_IN_LLROUND)
1857 CASE_MATHFN (BUILT_IN_LOG)
1858 CASE_MATHFN (BUILT_IN_LOG10)
1859 CASE_MATHFN (BUILT_IN_LOG1P)
1860 CASE_MATHFN (BUILT_IN_LOG2)
1861 CASE_MATHFN (BUILT_IN_LOGB)
1862 CASE_MATHFN (BUILT_IN_LRINT)
1863 CASE_MATHFN (BUILT_IN_LROUND)
1864 CASE_MATHFN (BUILT_IN_MODF)
1865 CASE_MATHFN (BUILT_IN_NAN)
1866 CASE_MATHFN (BUILT_IN_NANS)
1867 CASE_MATHFN (BUILT_IN_NEARBYINT)
1868 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1869 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1870 CASE_MATHFN (BUILT_IN_POW)
1871 CASE_MATHFN (BUILT_IN_POWI)
1872 CASE_MATHFN (BUILT_IN_POW10)
1873 CASE_MATHFN (BUILT_IN_REMAINDER)
1874 CASE_MATHFN (BUILT_IN_REMQUO)
1875 CASE_MATHFN (BUILT_IN_RINT)
1876 CASE_MATHFN (BUILT_IN_ROUND)
1877 CASE_MATHFN (BUILT_IN_SCALB)
1878 CASE_MATHFN (BUILT_IN_SCALBLN)
1879 CASE_MATHFN (BUILT_IN_SCALBN)
1880 CASE_MATHFN (BUILT_IN_SIGNBIT)
1881 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1882 CASE_MATHFN (BUILT_IN_SIN)
1883 CASE_MATHFN (BUILT_IN_SINCOS)
1884 CASE_MATHFN (BUILT_IN_SINH)
1885 CASE_MATHFN (BUILT_IN_SQRT)
1886 CASE_MATHFN (BUILT_IN_TAN)
1887 CASE_MATHFN (BUILT_IN_TANH)
1888 CASE_MATHFN (BUILT_IN_TGAMMA)
1889 CASE_MATHFN (BUILT_IN_TRUNC)
1890 CASE_MATHFN (BUILT_IN_Y0)
1891 CASE_MATHFN (BUILT_IN_Y1)
1892 CASE_MATHFN (BUILT_IN_YN)
1894 default:
1895 return NULL_TREE;
1898 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1899 return fn_arr[fcode];
1900 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1901 return fn_arr[fcodef];
1902 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1903 return fn_arr[fcodel];
1904 else
1905 return NULL_TREE;
1908 /* Like mathfn_built_in_1(), but always use the implicit array. */
1910 tree
1911 mathfn_built_in (tree type, enum built_in_function fn)
1913 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1916 /* If errno must be maintained, expand the RTL to check if the result,
1917 TARGET, of a built-in function call, EXP, is NaN, and if so set
1918 errno to EDOM. */
1920 static void
1921 expand_errno_check (tree exp, rtx target)
1923 rtx lab = gen_label_rtx ();
1925 /* Test the result; if it is NaN, set errno=EDOM because
1926 the argument was not in the domain. */
1927 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1928 NULL_RTX, NULL_RTX, lab,
1929 /* The jump is very likely. */
1930 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1932 #ifdef TARGET_EDOM
1933 /* If this built-in doesn't throw an exception, set errno directly. */
1934 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1936 #ifdef GEN_ERRNO_RTX
1937 rtx errno_rtx = GEN_ERRNO_RTX;
1938 #else
1939 rtx errno_rtx
1940 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1941 #endif
1942 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1943 emit_label (lab);
1944 return;
1946 #endif
1948 /* Make sure the library call isn't expanded as a tail call. */
1949 CALL_EXPR_TAILCALL (exp) = 0;
1951 /* We can't set errno=EDOM directly; let the library call do it.
1952 Pop the arguments right away in case the call gets deleted. */
1953 NO_DEFER_POP;
1954 expand_call (exp, target, 0);
1955 OK_DEFER_POP;
1956 emit_label (lab);
1959 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1960 Return NULL_RTX if a normal call should be emitted rather than expanding
1961 the function in-line. EXP is the expression that is a call to the builtin
1962 function; if convenient, the result should be placed in TARGET.
1963 SUBTARGET may be used as the target for computing one of EXP's operands. */
1965 static rtx
1966 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1968 optab builtin_optab;
1969 rtx op0, insns;
1970 tree fndecl = get_callee_fndecl (exp);
1971 enum machine_mode mode;
1972 bool errno_set = false;
1973 tree arg;
1975 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1976 return NULL_RTX;
1978 arg = CALL_EXPR_ARG (exp, 0);
1980 switch (DECL_FUNCTION_CODE (fndecl))
1982 CASE_FLT_FN (BUILT_IN_SQRT):
1983 errno_set = ! tree_expr_nonnegative_p (arg);
1984 builtin_optab = sqrt_optab;
1985 break;
1986 CASE_FLT_FN (BUILT_IN_EXP):
1987 errno_set = true; builtin_optab = exp_optab; break;
1988 CASE_FLT_FN (BUILT_IN_EXP10):
1989 CASE_FLT_FN (BUILT_IN_POW10):
1990 errno_set = true; builtin_optab = exp10_optab; break;
1991 CASE_FLT_FN (BUILT_IN_EXP2):
1992 errno_set = true; builtin_optab = exp2_optab; break;
1993 CASE_FLT_FN (BUILT_IN_EXPM1):
1994 errno_set = true; builtin_optab = expm1_optab; break;
1995 CASE_FLT_FN (BUILT_IN_LOGB):
1996 errno_set = true; builtin_optab = logb_optab; break;
1997 CASE_FLT_FN (BUILT_IN_LOG):
1998 errno_set = true; builtin_optab = log_optab; break;
1999 CASE_FLT_FN (BUILT_IN_LOG10):
2000 errno_set = true; builtin_optab = log10_optab; break;
2001 CASE_FLT_FN (BUILT_IN_LOG2):
2002 errno_set = true; builtin_optab = log2_optab; break;
2003 CASE_FLT_FN (BUILT_IN_LOG1P):
2004 errno_set = true; builtin_optab = log1p_optab; break;
2005 CASE_FLT_FN (BUILT_IN_ASIN):
2006 builtin_optab = asin_optab; break;
2007 CASE_FLT_FN (BUILT_IN_ACOS):
2008 builtin_optab = acos_optab; break;
2009 CASE_FLT_FN (BUILT_IN_TAN):
2010 builtin_optab = tan_optab; break;
2011 CASE_FLT_FN (BUILT_IN_ATAN):
2012 builtin_optab = atan_optab; break;
2013 CASE_FLT_FN (BUILT_IN_FLOOR):
2014 builtin_optab = floor_optab; break;
2015 CASE_FLT_FN (BUILT_IN_CEIL):
2016 builtin_optab = ceil_optab; break;
2017 CASE_FLT_FN (BUILT_IN_TRUNC):
2018 builtin_optab = btrunc_optab; break;
2019 CASE_FLT_FN (BUILT_IN_ROUND):
2020 builtin_optab = round_optab; break;
2021 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2022 builtin_optab = nearbyint_optab;
2023 if (flag_trapping_math)
2024 break;
2025 /* Else fallthrough and expand as rint. */
2026 CASE_FLT_FN (BUILT_IN_RINT):
2027 builtin_optab = rint_optab; break;
2028 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2029 builtin_optab = significand_optab; break;
2030 default:
2031 gcc_unreachable ();
2034 /* Make a suitable register to place result in. */
2035 mode = TYPE_MODE (TREE_TYPE (exp));
2037 if (! flag_errno_math || ! HONOR_NANS (mode))
2038 errno_set = false;
2040 /* Before working hard, check whether the instruction is available. */
2041 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2042 && (!errno_set || !optimize_insn_for_size_p ()))
2044 target = gen_reg_rtx (mode);
2046 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2047 need to expand the argument again. This way, we will not perform
2048 side-effects more the once. */
2049 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2051 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2053 start_sequence ();
2055 /* Compute into TARGET.
2056 Set TARGET to wherever the result comes back. */
2057 target = expand_unop (mode, builtin_optab, op0, target, 0);
2059 if (target != 0)
2061 if (errno_set)
2062 expand_errno_check (exp, target);
2064 /* Output the entire sequence. */
2065 insns = get_insns ();
2066 end_sequence ();
2067 emit_insn (insns);
2068 return target;
2071 /* If we were unable to expand via the builtin, stop the sequence
2072 (without outputting the insns) and call to the library function
2073 with the stabilized argument list. */
2074 end_sequence ();
2077 return expand_call (exp, target, target == const0_rtx);
2080 /* Expand a call to the builtin binary math functions (pow and atan2).
2081 Return NULL_RTX if a normal call should be emitted rather than expanding the
2082 function in-line. EXP is the expression that is a call to the builtin
2083 function; if convenient, the result should be placed in TARGET.
2084 SUBTARGET may be used as the target for computing one of EXP's
2085 operands. */
2087 static rtx
2088 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2090 optab builtin_optab;
2091 rtx op0, op1, insns;
2092 int op1_type = REAL_TYPE;
2093 tree fndecl = get_callee_fndecl (exp);
2094 tree arg0, arg1;
2095 enum machine_mode mode;
2096 bool errno_set = true;
2098 switch (DECL_FUNCTION_CODE (fndecl))
2100 CASE_FLT_FN (BUILT_IN_SCALBN):
2101 CASE_FLT_FN (BUILT_IN_SCALBLN):
2102 CASE_FLT_FN (BUILT_IN_LDEXP):
2103 op1_type = INTEGER_TYPE;
2104 default:
2105 break;
2108 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2109 return NULL_RTX;
2111 arg0 = CALL_EXPR_ARG (exp, 0);
2112 arg1 = CALL_EXPR_ARG (exp, 1);
2114 switch (DECL_FUNCTION_CODE (fndecl))
2116 CASE_FLT_FN (BUILT_IN_POW):
2117 builtin_optab = pow_optab; break;
2118 CASE_FLT_FN (BUILT_IN_ATAN2):
2119 builtin_optab = atan2_optab; break;
2120 CASE_FLT_FN (BUILT_IN_SCALB):
2121 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2122 return 0;
2123 builtin_optab = scalb_optab; break;
2124 CASE_FLT_FN (BUILT_IN_SCALBN):
2125 CASE_FLT_FN (BUILT_IN_SCALBLN):
2126 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2127 return 0;
2128 /* Fall through... */
2129 CASE_FLT_FN (BUILT_IN_LDEXP):
2130 builtin_optab = ldexp_optab; break;
2131 CASE_FLT_FN (BUILT_IN_FMOD):
2132 builtin_optab = fmod_optab; break;
2133 CASE_FLT_FN (BUILT_IN_REMAINDER):
2134 CASE_FLT_FN (BUILT_IN_DREM):
2135 builtin_optab = remainder_optab; break;
2136 default:
2137 gcc_unreachable ();
2140 /* Make a suitable register to place result in. */
2141 mode = TYPE_MODE (TREE_TYPE (exp));
2143 /* Before working hard, check whether the instruction is available. */
2144 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2145 return NULL_RTX;
2147 target = gen_reg_rtx (mode);
2149 if (! flag_errno_math || ! HONOR_NANS (mode))
2150 errno_set = false;
2152 if (errno_set && optimize_insn_for_size_p ())
2153 return 0;
2155 /* Always stabilize the argument list. */
2156 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2157 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2159 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2160 op1 = expand_normal (arg1);
2162 start_sequence ();
2164 /* Compute into TARGET.
2165 Set TARGET to wherever the result comes back. */
2166 target = expand_binop (mode, builtin_optab, op0, op1,
2167 target, 0, OPTAB_DIRECT);
2169 /* If we were unable to expand via the builtin, stop the sequence
2170 (without outputting the insns) and call to the library function
2171 with the stabilized argument list. */
2172 if (target == 0)
2174 end_sequence ();
2175 return expand_call (exp, target, target == const0_rtx);
2178 if (errno_set)
2179 expand_errno_check (exp, target);
2181 /* Output the entire sequence. */
2182 insns = get_insns ();
2183 end_sequence ();
2184 emit_insn (insns);
2186 return target;
2189 /* Expand a call to the builtin trinary math functions (fma).
2190 Return NULL_RTX if a normal call should be emitted rather than expanding the
2191 function in-line. EXP is the expression that is a call to the builtin
2192 function; if convenient, the result should be placed in TARGET.
2193 SUBTARGET may be used as the target for computing one of EXP's
2194 operands. */
2196 static rtx
2197 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2199 optab builtin_optab;
2200 rtx op0, op1, op2, insns;
2201 tree fndecl = get_callee_fndecl (exp);
2202 tree arg0, arg1, arg2;
2203 enum machine_mode mode;
2205 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2206 return NULL_RTX;
2208 arg0 = CALL_EXPR_ARG (exp, 0);
2209 arg1 = CALL_EXPR_ARG (exp, 1);
2210 arg2 = CALL_EXPR_ARG (exp, 2);
2212 switch (DECL_FUNCTION_CODE (fndecl))
2214 CASE_FLT_FN (BUILT_IN_FMA):
2215 builtin_optab = fma_optab; break;
2216 default:
2217 gcc_unreachable ();
2220 /* Make a suitable register to place result in. */
2221 mode = TYPE_MODE (TREE_TYPE (exp));
2223 /* Before working hard, check whether the instruction is available. */
2224 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2225 return NULL_RTX;
2227 target = gen_reg_rtx (mode);
2229 /* Always stabilize the argument list. */
2230 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2231 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2232 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2234 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2235 op1 = expand_normal (arg1);
2236 op2 = expand_normal (arg2);
2238 start_sequence ();
2240 /* Compute into TARGET.
2241 Set TARGET to wherever the result comes back. */
2242 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2243 target, 0);
2245 /* If we were unable to expand via the builtin, stop the sequence
2246 (without outputting the insns) and call to the library function
2247 with the stabilized argument list. */
2248 if (target == 0)
2250 end_sequence ();
2251 return expand_call (exp, target, target == const0_rtx);
2254 /* Output the entire sequence. */
2255 insns = get_insns ();
2256 end_sequence ();
2257 emit_insn (insns);
2259 return target;
2262 /* Expand a call to the builtin sin and cos math functions.
2263 Return NULL_RTX if a normal call should be emitted rather than expanding the
2264 function in-line. EXP is the expression that is a call to the builtin
2265 function; if convenient, the result should be placed in TARGET.
2266 SUBTARGET may be used as the target for computing one of EXP's
2267 operands. */
2269 static rtx
2270 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2272 optab builtin_optab;
2273 rtx op0, insns;
2274 tree fndecl = get_callee_fndecl (exp);
2275 enum machine_mode mode;
2276 tree arg;
2278 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2279 return NULL_RTX;
2281 arg = CALL_EXPR_ARG (exp, 0);
2283 switch (DECL_FUNCTION_CODE (fndecl))
2285 CASE_FLT_FN (BUILT_IN_SIN):
2286 CASE_FLT_FN (BUILT_IN_COS):
2287 builtin_optab = sincos_optab; break;
2288 default:
2289 gcc_unreachable ();
2292 /* Make a suitable register to place result in. */
2293 mode = TYPE_MODE (TREE_TYPE (exp));
2295 /* Check if sincos insn is available, otherwise fallback
2296 to sin or cos insn. */
2297 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2298 switch (DECL_FUNCTION_CODE (fndecl))
2300 CASE_FLT_FN (BUILT_IN_SIN):
2301 builtin_optab = sin_optab; break;
2302 CASE_FLT_FN (BUILT_IN_COS):
2303 builtin_optab = cos_optab; break;
2304 default:
2305 gcc_unreachable ();
2308 /* Before working hard, check whether the instruction is available. */
2309 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2311 target = gen_reg_rtx (mode);
2313 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2314 need to expand the argument again. This way, we will not perform
2315 side-effects more the once. */
2316 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2318 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2320 start_sequence ();
2322 /* Compute into TARGET.
2323 Set TARGET to wherever the result comes back. */
2324 if (builtin_optab == sincos_optab)
2326 int result;
2328 switch (DECL_FUNCTION_CODE (fndecl))
2330 CASE_FLT_FN (BUILT_IN_SIN):
2331 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2332 break;
2333 CASE_FLT_FN (BUILT_IN_COS):
2334 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2335 break;
2336 default:
2337 gcc_unreachable ();
2339 gcc_assert (result);
2341 else
2343 target = expand_unop (mode, builtin_optab, op0, target, 0);
2346 if (target != 0)
2348 /* Output the entire sequence. */
2349 insns = get_insns ();
2350 end_sequence ();
2351 emit_insn (insns);
2352 return target;
2355 /* If we were unable to expand via the builtin, stop the sequence
2356 (without outputting the insns) and call to the library function
2357 with the stabilized argument list. */
2358 end_sequence ();
2361 target = expand_call (exp, target, target == const0_rtx);
2363 return target;
2366 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2367 return an RTL instruction code that implements the functionality.
2368 If that isn't possible or available return CODE_FOR_nothing. */
2370 static enum insn_code
2371 interclass_mathfn_icode (tree arg, tree fndecl)
2373 bool errno_set = false;
2374 optab builtin_optab = 0;
2375 enum machine_mode mode;
2377 switch (DECL_FUNCTION_CODE (fndecl))
2379 CASE_FLT_FN (BUILT_IN_ILOGB):
2380 errno_set = true; builtin_optab = ilogb_optab; break;
2381 CASE_FLT_FN (BUILT_IN_ISINF):
2382 builtin_optab = isinf_optab; break;
2383 case BUILT_IN_ISNORMAL:
2384 case BUILT_IN_ISFINITE:
2385 CASE_FLT_FN (BUILT_IN_FINITE):
2386 case BUILT_IN_FINITED32:
2387 case BUILT_IN_FINITED64:
2388 case BUILT_IN_FINITED128:
2389 case BUILT_IN_ISINFD32:
2390 case BUILT_IN_ISINFD64:
2391 case BUILT_IN_ISINFD128:
2392 /* These builtins have no optabs (yet). */
2393 break;
2394 default:
2395 gcc_unreachable ();
2398 /* There's no easy way to detect the case we need to set EDOM. */
2399 if (flag_errno_math && errno_set)
2400 return CODE_FOR_nothing;
2402 /* Optab mode depends on the mode of the input argument. */
2403 mode = TYPE_MODE (TREE_TYPE (arg));
2405 if (builtin_optab)
2406 return optab_handler (builtin_optab, mode);
2407 return CODE_FOR_nothing;
2410 /* Expand a call to one of the builtin math functions that operate on
2411 floating point argument and output an integer result (ilogb, isinf,
2412 isnan, etc).
2413 Return 0 if a normal call should be emitted rather than expanding the
2414 function in-line. EXP is the expression that is a call to the builtin
2415 function; if convenient, the result should be placed in TARGET. */
2417 static rtx
2418 expand_builtin_interclass_mathfn (tree exp, rtx target)
2420 enum insn_code icode = CODE_FOR_nothing;
2421 rtx op0;
2422 tree fndecl = get_callee_fndecl (exp);
2423 enum machine_mode mode;
2424 tree arg;
2426 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2427 return NULL_RTX;
2429 arg = CALL_EXPR_ARG (exp, 0);
2430 icode = interclass_mathfn_icode (arg, fndecl);
2431 mode = TYPE_MODE (TREE_TYPE (arg));
2433 if (icode != CODE_FOR_nothing)
2435 rtx last = get_last_insn ();
2436 tree orig_arg = arg;
2437 /* Make a suitable register to place result in. */
2438 if (!target
2439 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp))
2440 || !insn_data[icode].operand[0].predicate (target, GET_MODE (target)))
2441 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2443 gcc_assert (insn_data[icode].operand[0].predicate
2444 (target, GET_MODE (target)));
2446 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2447 need to expand the argument again. This way, we will not perform
2448 side-effects more the once. */
2449 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2451 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2453 if (mode != GET_MODE (op0))
2454 op0 = convert_to_mode (mode, op0, 0);
2456 /* Compute into TARGET.
2457 Set TARGET to wherever the result comes back. */
2458 if (maybe_emit_unop_insn (icode, target, op0, UNKNOWN))
2459 return target;
2460 delete_insns_since (last);
2461 CALL_EXPR_ARG (exp, 0) = orig_arg;
2464 return NULL_RTX;
2467 /* Expand a call to the builtin sincos math function.
2468 Return NULL_RTX if a normal call should be emitted rather than expanding the
2469 function in-line. EXP is the expression that is a call to the builtin
2470 function. */
2472 static rtx
2473 expand_builtin_sincos (tree exp)
2475 rtx op0, op1, op2, target1, target2;
2476 enum machine_mode mode;
2477 tree arg, sinp, cosp;
2478 int result;
2479 location_t loc = EXPR_LOCATION (exp);
2480 tree alias_type, alias_off;
2482 if (!validate_arglist (exp, REAL_TYPE,
2483 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2484 return NULL_RTX;
2486 arg = CALL_EXPR_ARG (exp, 0);
2487 sinp = CALL_EXPR_ARG (exp, 1);
2488 cosp = CALL_EXPR_ARG (exp, 2);
2490 /* Make a suitable register to place result in. */
2491 mode = TYPE_MODE (TREE_TYPE (arg));
2493 /* Check if sincos insn is available, otherwise emit the call. */
2494 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2495 return NULL_RTX;
2497 target1 = gen_reg_rtx (mode);
2498 target2 = gen_reg_rtx (mode);
2500 op0 = expand_normal (arg);
2501 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2502 alias_off = build_int_cst (alias_type, 0);
2503 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2504 sinp, alias_off));
2505 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2506 cosp, alias_off));
2508 /* Compute into target1 and target2.
2509 Set TARGET to wherever the result comes back. */
2510 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2511 gcc_assert (result);
2513 /* Move target1 and target2 to the memory locations indicated
2514 by op1 and op2. */
2515 emit_move_insn (op1, target1);
2516 emit_move_insn (op2, target2);
2518 return const0_rtx;
2521 /* Expand a call to the internal cexpi builtin to the sincos math function.
2522 EXP is the expression that is a call to the builtin function; if convenient,
2523 the result should be placed in TARGET. */
2525 static rtx
2526 expand_builtin_cexpi (tree exp, rtx target)
2528 tree fndecl = get_callee_fndecl (exp);
2529 tree arg, type;
2530 enum machine_mode mode;
2531 rtx op0, op1, op2;
2532 location_t loc = EXPR_LOCATION (exp);
2534 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2535 return NULL_RTX;
2537 arg = CALL_EXPR_ARG (exp, 0);
2538 type = TREE_TYPE (arg);
2539 mode = TYPE_MODE (TREE_TYPE (arg));
2541 /* Try expanding via a sincos optab, fall back to emitting a libcall
2542 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2543 is only generated from sincos, cexp or if we have either of them. */
2544 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2546 op1 = gen_reg_rtx (mode);
2547 op2 = gen_reg_rtx (mode);
2549 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2551 /* Compute into op1 and op2. */
2552 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2554 else if (TARGET_HAS_SINCOS)
2556 tree call, fn = NULL_TREE;
2557 tree top1, top2;
2558 rtx op1a, op2a;
2560 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2561 fn = built_in_decls[BUILT_IN_SINCOSF];
2562 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2563 fn = built_in_decls[BUILT_IN_SINCOS];
2564 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2565 fn = built_in_decls[BUILT_IN_SINCOSL];
2566 else
2567 gcc_unreachable ();
2569 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2570 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2571 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2572 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2573 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2574 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2576 /* Make sure not to fold the sincos call again. */
2577 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2578 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2579 call, 3, arg, top1, top2));
2581 else
2583 tree call, fn = NULL_TREE, narg;
2584 tree ctype = build_complex_type (type);
2586 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2587 fn = built_in_decls[BUILT_IN_CEXPF];
2588 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2589 fn = built_in_decls[BUILT_IN_CEXP];
2590 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2591 fn = built_in_decls[BUILT_IN_CEXPL];
2592 else
2593 gcc_unreachable ();
2595 /* If we don't have a decl for cexp create one. This is the
2596 friendliest fallback if the user calls __builtin_cexpi
2597 without full target C99 function support. */
2598 if (fn == NULL_TREE)
2600 tree fntype;
2601 const char *name = NULL;
2603 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2604 name = "cexpf";
2605 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2606 name = "cexp";
2607 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2608 name = "cexpl";
2610 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2611 fn = build_fn_decl (name, fntype);
2614 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2615 build_real (type, dconst0), arg);
2617 /* Make sure not to fold the cexp call again. */
2618 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2619 return expand_expr (build_call_nary (ctype, call, 1, narg),
2620 target, VOIDmode, EXPAND_NORMAL);
2623 /* Now build the proper return type. */
2624 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2625 make_tree (TREE_TYPE (arg), op2),
2626 make_tree (TREE_TYPE (arg), op1)),
2627 target, VOIDmode, EXPAND_NORMAL);
2630 /* Conveniently construct a function call expression. FNDECL names the
2631 function to be called, N is the number of arguments, and the "..."
2632 parameters are the argument expressions. Unlike build_call_exr
2633 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2635 static tree
2636 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2638 va_list ap;
2639 tree fntype = TREE_TYPE (fndecl);
2640 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2642 va_start (ap, n);
2643 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2644 va_end (ap);
2645 SET_EXPR_LOCATION (fn, loc);
2646 return fn;
2649 /* Expand a call to one of the builtin rounding functions gcc defines
2650 as an extension (lfloor and lceil). As these are gcc extensions we
2651 do not need to worry about setting errno to EDOM.
2652 If expanding via optab fails, lower expression to (int)(floor(x)).
2653 EXP is the expression that is a call to the builtin function;
2654 if convenient, the result should be placed in TARGET. */
2656 static rtx
2657 expand_builtin_int_roundingfn (tree exp, rtx target)
2659 convert_optab builtin_optab;
2660 rtx op0, insns, tmp;
2661 tree fndecl = get_callee_fndecl (exp);
2662 enum built_in_function fallback_fn;
2663 tree fallback_fndecl;
2664 enum machine_mode mode;
2665 tree arg;
2667 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2668 gcc_unreachable ();
2670 arg = CALL_EXPR_ARG (exp, 0);
2672 switch (DECL_FUNCTION_CODE (fndecl))
2674 CASE_FLT_FN (BUILT_IN_LCEIL):
2675 CASE_FLT_FN (BUILT_IN_LLCEIL):
2676 builtin_optab = lceil_optab;
2677 fallback_fn = BUILT_IN_CEIL;
2678 break;
2680 CASE_FLT_FN (BUILT_IN_LFLOOR):
2681 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2682 builtin_optab = lfloor_optab;
2683 fallback_fn = BUILT_IN_FLOOR;
2684 break;
2686 default:
2687 gcc_unreachable ();
2690 /* Make a suitable register to place result in. */
2691 mode = TYPE_MODE (TREE_TYPE (exp));
2693 target = gen_reg_rtx (mode);
2695 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2696 need to expand the argument again. This way, we will not perform
2697 side-effects more the once. */
2698 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2700 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2702 start_sequence ();
2704 /* Compute into TARGET. */
2705 if (expand_sfix_optab (target, op0, builtin_optab))
2707 /* Output the entire sequence. */
2708 insns = get_insns ();
2709 end_sequence ();
2710 emit_insn (insns);
2711 return target;
2714 /* If we were unable to expand via the builtin, stop the sequence
2715 (without outputting the insns). */
2716 end_sequence ();
2718 /* Fall back to floating point rounding optab. */
2719 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2721 /* For non-C99 targets we may end up without a fallback fndecl here
2722 if the user called __builtin_lfloor directly. In this case emit
2723 a call to the floor/ceil variants nevertheless. This should result
2724 in the best user experience for not full C99 targets. */
2725 if (fallback_fndecl == NULL_TREE)
2727 tree fntype;
2728 const char *name = NULL;
2730 switch (DECL_FUNCTION_CODE (fndecl))
2732 case BUILT_IN_LCEIL:
2733 case BUILT_IN_LLCEIL:
2734 name = "ceil";
2735 break;
2736 case BUILT_IN_LCEILF:
2737 case BUILT_IN_LLCEILF:
2738 name = "ceilf";
2739 break;
2740 case BUILT_IN_LCEILL:
2741 case BUILT_IN_LLCEILL:
2742 name = "ceill";
2743 break;
2744 case BUILT_IN_LFLOOR:
2745 case BUILT_IN_LLFLOOR:
2746 name = "floor";
2747 break;
2748 case BUILT_IN_LFLOORF:
2749 case BUILT_IN_LLFLOORF:
2750 name = "floorf";
2751 break;
2752 case BUILT_IN_LFLOORL:
2753 case BUILT_IN_LLFLOORL:
2754 name = "floorl";
2755 break;
2756 default:
2757 gcc_unreachable ();
2760 fntype = build_function_type_list (TREE_TYPE (arg),
2761 TREE_TYPE (arg), NULL_TREE);
2762 fallback_fndecl = build_fn_decl (name, fntype);
2765 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2767 tmp = expand_normal (exp);
2769 /* Truncate the result of floating point optab to integer
2770 via expand_fix (). */
2771 target = gen_reg_rtx (mode);
2772 expand_fix (target, tmp, 0);
2774 return target;
2777 /* Expand a call to one of the builtin math functions doing integer
2778 conversion (lrint).
2779 Return 0 if a normal call should be emitted rather than expanding the
2780 function in-line. EXP is the expression that is a call to the builtin
2781 function; if convenient, the result should be placed in TARGET. */
2783 static rtx
2784 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2786 convert_optab builtin_optab;
2787 rtx op0, insns;
2788 tree fndecl = get_callee_fndecl (exp);
2789 tree arg;
2790 enum machine_mode mode;
2792 /* There's no easy way to detect the case we need to set EDOM. */
2793 if (flag_errno_math)
2794 return NULL_RTX;
2796 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2797 gcc_unreachable ();
2799 arg = CALL_EXPR_ARG (exp, 0);
2801 switch (DECL_FUNCTION_CODE (fndecl))
2803 CASE_FLT_FN (BUILT_IN_LRINT):
2804 CASE_FLT_FN (BUILT_IN_LLRINT):
2805 builtin_optab = lrint_optab; break;
2806 CASE_FLT_FN (BUILT_IN_LROUND):
2807 CASE_FLT_FN (BUILT_IN_LLROUND):
2808 builtin_optab = lround_optab; break;
2809 default:
2810 gcc_unreachable ();
2813 /* Make a suitable register to place result in. */
2814 mode = TYPE_MODE (TREE_TYPE (exp));
2816 target = gen_reg_rtx (mode);
2818 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2819 need to expand the argument again. This way, we will not perform
2820 side-effects more the once. */
2821 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2823 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2825 start_sequence ();
2827 if (expand_sfix_optab (target, op0, builtin_optab))
2829 /* Output the entire sequence. */
2830 insns = get_insns ();
2831 end_sequence ();
2832 emit_insn (insns);
2833 return target;
2836 /* If we were unable to expand via the builtin, stop the sequence
2837 (without outputting the insns) and call to the library function
2838 with the stabilized argument list. */
2839 end_sequence ();
2841 target = expand_call (exp, target, target == const0_rtx);
2843 return target;
2846 /* To evaluate powi(x,n), the floating point value x raised to the
2847 constant integer exponent n, we use a hybrid algorithm that
2848 combines the "window method" with look-up tables. For an
2849 introduction to exponentiation algorithms and "addition chains",
2850 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2851 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2852 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2853 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2855 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2856 multiplications to inline before calling the system library's pow
2857 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2858 so this default never requires calling pow, powf or powl. */
2860 #ifndef POWI_MAX_MULTS
2861 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2862 #endif
2864 /* The size of the "optimal power tree" lookup table. All
2865 exponents less than this value are simply looked up in the
2866 powi_table below. This threshold is also used to size the
2867 cache of pseudo registers that hold intermediate results. */
2868 #define POWI_TABLE_SIZE 256
2870 /* The size, in bits of the window, used in the "window method"
2871 exponentiation algorithm. This is equivalent to a radix of
2872 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2873 #define POWI_WINDOW_SIZE 3
2875 /* The following table is an efficient representation of an
2876 "optimal power tree". For each value, i, the corresponding
2877 value, j, in the table states than an optimal evaluation
2878 sequence for calculating pow(x,i) can be found by evaluating
2879 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2880 100 integers is given in Knuth's "Seminumerical algorithms". */
2882 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2884 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2885 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2886 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2887 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2888 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2889 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2890 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2891 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2892 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2893 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2894 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2895 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2896 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2897 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2898 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2899 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2900 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2901 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2902 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2903 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2904 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2905 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2906 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2907 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2908 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2909 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2910 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2911 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2912 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2913 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2914 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2915 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2919 /* Return the number of multiplications required to calculate
2920 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2921 subroutine of powi_cost. CACHE is an array indicating
2922 which exponents have already been calculated. */
2924 static int
2925 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2927 /* If we've already calculated this exponent, then this evaluation
2928 doesn't require any additional multiplications. */
2929 if (cache[n])
2930 return 0;
2932 cache[n] = true;
2933 return powi_lookup_cost (n - powi_table[n], cache)
2934 + powi_lookup_cost (powi_table[n], cache) + 1;
2937 /* Return the number of multiplications required to calculate
2938 powi(x,n) for an arbitrary x, given the exponent N. This
2939 function needs to be kept in sync with expand_powi below. */
2941 static int
2942 powi_cost (HOST_WIDE_INT n)
2944 bool cache[POWI_TABLE_SIZE];
2945 unsigned HOST_WIDE_INT digit;
2946 unsigned HOST_WIDE_INT val;
2947 int result;
2949 if (n == 0)
2950 return 0;
2952 /* Ignore the reciprocal when calculating the cost. */
2953 val = (n < 0) ? -n : n;
2955 /* Initialize the exponent cache. */
2956 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2957 cache[1] = true;
2959 result = 0;
2961 while (val >= POWI_TABLE_SIZE)
2963 if (val & 1)
2965 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2966 result += powi_lookup_cost (digit, cache)
2967 + POWI_WINDOW_SIZE + 1;
2968 val >>= POWI_WINDOW_SIZE;
2970 else
2972 val >>= 1;
2973 result++;
2977 return result + powi_lookup_cost (val, cache);
2980 /* Recursive subroutine of expand_powi. This function takes the array,
2981 CACHE, of already calculated exponents and an exponent N and returns
2982 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2984 static rtx
2985 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2987 unsigned HOST_WIDE_INT digit;
2988 rtx target, result;
2989 rtx op0, op1;
2991 if (n < POWI_TABLE_SIZE)
2993 if (cache[n])
2994 return cache[n];
2996 target = gen_reg_rtx (mode);
2997 cache[n] = target;
2999 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
3000 op1 = expand_powi_1 (mode, powi_table[n], cache);
3002 else if (n & 1)
3004 target = gen_reg_rtx (mode);
3005 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3006 op0 = expand_powi_1 (mode, n - digit, cache);
3007 op1 = expand_powi_1 (mode, digit, cache);
3009 else
3011 target = gen_reg_rtx (mode);
3012 op0 = expand_powi_1 (mode, n >> 1, cache);
3013 op1 = op0;
3016 result = expand_mult (mode, op0, op1, target, 0);
3017 if (result != target)
3018 emit_move_insn (target, result);
3019 return target;
3022 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
3023 floating point operand in mode MODE, and N is the exponent. This
3024 function needs to be kept in sync with powi_cost above. */
3026 static rtx
3027 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
3029 rtx cache[POWI_TABLE_SIZE];
3030 rtx result;
3032 if (n == 0)
3033 return CONST1_RTX (mode);
3035 memset (cache, 0, sizeof (cache));
3036 cache[1] = x;
3038 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
3040 /* If the original exponent was negative, reciprocate the result. */
3041 if (n < 0)
3042 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3043 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3045 return result;
3048 /* Fold a builtin function call to pow, powf, or powl into a series of sqrts or
3049 cbrts. Return NULL_RTX if no simplification can be made or expand the tree
3050 if we can simplify it. */
3051 static rtx
3052 expand_builtin_pow_root (location_t loc, tree arg0, tree arg1, tree type,
3053 rtx subtarget)
3055 if (TREE_CODE (arg1) == REAL_CST
3056 && !TREE_OVERFLOW (arg1)
3057 && flag_unsafe_math_optimizations)
3059 enum machine_mode mode = TYPE_MODE (type);
3060 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
3061 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
3062 REAL_VALUE_TYPE c = TREE_REAL_CST (arg1);
3063 tree op = NULL_TREE;
3065 if (sqrtfn)
3067 /* Optimize pow (x, 0.5) into sqrt. */
3068 if (REAL_VALUES_EQUAL (c, dconsthalf))
3069 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3071 else
3073 REAL_VALUE_TYPE dconst1_4 = dconst1;
3074 REAL_VALUE_TYPE dconst3_4;
3075 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
3077 real_from_integer (&dconst3_4, VOIDmode, 3, 0, 0);
3078 SET_REAL_EXP (&dconst3_4, REAL_EXP (&dconst3_4) - 2);
3080 /* Optimize pow (x, 0.25) into sqrt (sqrt (x)). Assume on most
3081 machines that a builtin sqrt instruction is smaller than a
3082 call to pow with 0.25, so do this optimization even if
3083 -Os. */
3084 if (REAL_VALUES_EQUAL (c, dconst1_4))
3086 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3087 op = build_call_nofold_loc (loc, sqrtfn, 1, op);
3090 /* Optimize pow (x, 0.75) = sqrt (x) * sqrt (sqrt (x)) unless we
3091 are optimizing for space. */
3092 else if (optimize_insn_for_speed_p ()
3093 && !TREE_SIDE_EFFECTS (arg0)
3094 && REAL_VALUES_EQUAL (c, dconst3_4))
3096 tree sqrt1 = build_call_expr_loc (loc, sqrtfn, 1, arg0);
3097 tree sqrt2 = builtin_save_expr (sqrt1);
3098 tree sqrt3 = build_call_expr_loc (loc, sqrtfn, 1, sqrt1);
3099 op = fold_build2_loc (loc, MULT_EXPR, type, sqrt2, sqrt3);
3104 /* Check whether we can do cbrt insstead of pow (x, 1./3.) and
3105 cbrt/sqrts instead of pow (x, 1./6.). */
3106 if (cbrtfn && ! op
3107 && (tree_expr_nonnegative_p (arg0) || !HONOR_NANS (mode)))
3109 /* First try 1/3. */
3110 REAL_VALUE_TYPE dconst1_3
3111 = real_value_truncate (mode, dconst_third ());
3113 if (REAL_VALUES_EQUAL (c, dconst1_3))
3114 op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
3116 /* Now try 1/6. */
3117 else if (optimize_insn_for_speed_p ())
3119 REAL_VALUE_TYPE dconst1_6 = dconst1_3;
3120 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
3122 if (REAL_VALUES_EQUAL (c, dconst1_6))
3124 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3125 op = build_call_nofold_loc (loc, cbrtfn, 1, op);
3130 if (op)
3131 return expand_expr (op, subtarget, mode, EXPAND_NORMAL);
3134 return NULL_RTX;
3137 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
3138 a normal call should be emitted rather than expanding the function
3139 in-line. EXP is the expression that is a call to the builtin
3140 function; if convenient, the result should be placed in TARGET. */
3142 static rtx
3143 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
3145 tree arg0, arg1;
3146 tree fn, narg0;
3147 tree type = TREE_TYPE (exp);
3148 REAL_VALUE_TYPE cint, c, c2;
3149 HOST_WIDE_INT n;
3150 rtx op, op2;
3151 enum machine_mode mode = TYPE_MODE (type);
3153 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
3154 return NULL_RTX;
3156 arg0 = CALL_EXPR_ARG (exp, 0);
3157 arg1 = CALL_EXPR_ARG (exp, 1);
3159 if (TREE_CODE (arg1) != REAL_CST
3160 || TREE_OVERFLOW (arg1))
3161 return expand_builtin_mathfn_2 (exp, target, subtarget);
3163 /* Handle constant exponents. */
3165 /* For integer valued exponents we can expand to an optimal multiplication
3166 sequence using expand_powi. */
3167 c = TREE_REAL_CST (arg1);
3168 n = real_to_integer (&c);
3169 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3170 if (real_identical (&c, &cint)
3171 && ((n >= -1 && n <= 2)
3172 || (flag_unsafe_math_optimizations
3173 && optimize_insn_for_speed_p ()
3174 && powi_cost (n) <= POWI_MAX_MULTS)))
3176 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3177 if (n != 1)
3179 op = force_reg (mode, op);
3180 op = expand_powi (op, mode, n);
3182 return op;
3185 narg0 = builtin_save_expr (arg0);
3187 /* If the exponent is not integer valued, check if it is half of an integer.
3188 In this case we can expand to sqrt (x) * x**(n/2). */
3189 fn = mathfn_built_in (type, BUILT_IN_SQRT);
3190 if (fn != NULL_TREE)
3192 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3193 n = real_to_integer (&c2);
3194 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3195 if (real_identical (&c2, &cint)
3196 && ((flag_unsafe_math_optimizations
3197 && optimize_insn_for_speed_p ()
3198 && powi_cost (n/2) <= POWI_MAX_MULTS)
3199 /* Even the c == 0.5 case cannot be done unconditionally
3200 when we need to preserve signed zeros, as
3201 pow (-0, 0.5) is +0, while sqrt(-0) is -0. */
3202 || (!HONOR_SIGNED_ZEROS (mode) && n == 1)
3203 /* For c == 1.5 we can assume that x * sqrt (x) is always
3204 smaller than pow (x, 1.5) if sqrt will not be expanded
3205 as a call. */
3206 || (n == 3
3207 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)))
3209 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3210 narg0);
3211 /* Use expand_expr in case the newly built call expression
3212 was folded to a non-call. */
3213 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3214 if (n != 1)
3216 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3217 op2 = force_reg (mode, op2);
3218 op2 = expand_powi (op2, mode, abs (n / 2));
3219 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3220 0, OPTAB_LIB_WIDEN);
3221 /* If the original exponent was negative, reciprocate the
3222 result. */
3223 if (n < 0)
3224 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3225 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3227 return op;
3231 /* Check whether we can do a series of sqrt or cbrt's instead of the pow
3232 call. */
3233 op = expand_builtin_pow_root (EXPR_LOCATION (exp), arg0, arg1, type,
3234 subtarget);
3235 if (op)
3236 return op;
3238 /* Try if the exponent is a third of an integer. In this case
3239 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3240 different from pow (x, 1./3.) due to rounding and behavior
3241 with negative x we need to constrain this transformation to
3242 unsafe math and positive x or finite math. */
3243 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3244 if (fn != NULL_TREE
3245 && flag_unsafe_math_optimizations
3246 && (tree_expr_nonnegative_p (arg0)
3247 || !HONOR_NANS (mode)))
3249 REAL_VALUE_TYPE dconst3;
3250 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3251 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3252 real_round (&c2, mode, &c2);
3253 n = real_to_integer (&c2);
3254 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3255 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3256 real_convert (&c2, mode, &c2);
3257 if (real_identical (&c2, &c)
3258 && ((optimize_insn_for_speed_p ()
3259 && powi_cost (n/3) <= POWI_MAX_MULTS)
3260 || n == 1))
3262 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3263 narg0);
3264 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3265 if (abs (n) % 3 == 2)
3266 op = expand_simple_binop (mode, MULT, op, op, op,
3267 0, OPTAB_LIB_WIDEN);
3268 if (n != 1)
3270 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3271 op2 = force_reg (mode, op2);
3272 op2 = expand_powi (op2, mode, abs (n / 3));
3273 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3274 0, OPTAB_LIB_WIDEN);
3275 /* If the original exponent was negative, reciprocate the
3276 result. */
3277 if (n < 0)
3278 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3279 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3281 return op;
3285 /* Fall back to optab expansion. */
3286 return expand_builtin_mathfn_2 (exp, target, subtarget);
3289 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3290 a normal call should be emitted rather than expanding the function
3291 in-line. EXP is the expression that is a call to the builtin
3292 function; if convenient, the result should be placed in TARGET. */
3294 static rtx
3295 expand_builtin_powi (tree exp, rtx target)
3297 tree arg0, arg1;
3298 rtx op0, op1;
3299 enum machine_mode mode;
3300 enum machine_mode mode2;
3302 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3303 return NULL_RTX;
3305 arg0 = CALL_EXPR_ARG (exp, 0);
3306 arg1 = CALL_EXPR_ARG (exp, 1);
3307 mode = TYPE_MODE (TREE_TYPE (exp));
3309 /* Handle constant power. */
3311 if (TREE_CODE (arg1) == INTEGER_CST
3312 && !TREE_OVERFLOW (arg1))
3314 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3316 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3317 Otherwise, check the number of multiplications required. */
3318 if ((TREE_INT_CST_HIGH (arg1) == 0
3319 || TREE_INT_CST_HIGH (arg1) == -1)
3320 && ((n >= -1 && n <= 2)
3321 || (optimize_insn_for_speed_p ()
3322 && powi_cost (n) <= POWI_MAX_MULTS)))
3324 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3325 op0 = force_reg (mode, op0);
3326 return expand_powi (op0, mode, n);
3330 /* Emit a libcall to libgcc. */
3332 /* Mode of the 2nd argument must match that of an int. */
3333 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3335 if (target == NULL_RTX)
3336 target = gen_reg_rtx (mode);
3338 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3339 if (GET_MODE (op0) != mode)
3340 op0 = convert_to_mode (mode, op0, 0);
3341 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3342 if (GET_MODE (op1) != mode2)
3343 op1 = convert_to_mode (mode2, op1, 0);
3345 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3346 target, LCT_CONST, mode, 2,
3347 op0, mode, op1, mode2);
3349 return target;
3352 /* Expand expression EXP which is a call to the strlen builtin. Return
3353 NULL_RTX if we failed the caller should emit a normal call, otherwise
3354 try to get the result in TARGET, if convenient. */
3356 static rtx
3357 expand_builtin_strlen (tree exp, rtx target,
3358 enum machine_mode target_mode)
3360 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3361 return NULL_RTX;
3362 else
3364 rtx pat;
3365 tree len;
3366 tree src = CALL_EXPR_ARG (exp, 0);
3367 rtx result, src_reg, char_rtx, before_strlen;
3368 enum machine_mode insn_mode = target_mode, char_mode;
3369 enum insn_code icode = CODE_FOR_nothing;
3370 unsigned int align;
3372 /* If the length can be computed at compile-time, return it. */
3373 len = c_strlen (src, 0);
3374 if (len)
3375 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3377 /* If the length can be computed at compile-time and is constant
3378 integer, but there are side-effects in src, evaluate
3379 src for side-effects, then return len.
3380 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3381 can be optimized into: i++; x = 3; */
3382 len = c_strlen (src, 1);
3383 if (len && TREE_CODE (len) == INTEGER_CST)
3385 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3386 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3389 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3391 /* If SRC is not a pointer type, don't do this operation inline. */
3392 if (align == 0)
3393 return NULL_RTX;
3395 /* Bail out if we can't compute strlen in the right mode. */
3396 while (insn_mode != VOIDmode)
3398 icode = optab_handler (strlen_optab, insn_mode);
3399 if (icode != CODE_FOR_nothing)
3400 break;
3402 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3404 if (insn_mode == VOIDmode)
3405 return NULL_RTX;
3407 /* Make a place to write the result of the instruction. */
3408 result = target;
3409 if (! (result != 0
3410 && REG_P (result)
3411 && GET_MODE (result) == insn_mode
3412 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3413 result = gen_reg_rtx (insn_mode);
3415 /* Make a place to hold the source address. We will not expand
3416 the actual source until we are sure that the expansion will
3417 not fail -- there are trees that cannot be expanded twice. */
3418 src_reg = gen_reg_rtx (Pmode);
3420 /* Mark the beginning of the strlen sequence so we can emit the
3421 source operand later. */
3422 before_strlen = get_last_insn ();
3424 char_rtx = const0_rtx;
3425 char_mode = insn_data[(int) icode].operand[2].mode;
3426 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3427 char_mode))
3428 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3430 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3431 char_rtx, GEN_INT (align));
3432 if (! pat)
3433 return NULL_RTX;
3434 emit_insn (pat);
3436 /* Now that we are assured of success, expand the source. */
3437 start_sequence ();
3438 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3439 if (pat != src_reg)
3440 emit_move_insn (src_reg, pat);
3441 pat = get_insns ();
3442 end_sequence ();
3444 if (before_strlen)
3445 emit_insn_after (pat, before_strlen);
3446 else
3447 emit_insn_before (pat, get_insns ());
3449 /* Return the value in the proper mode for this function. */
3450 if (GET_MODE (result) == target_mode)
3451 target = result;
3452 else if (target != 0)
3453 convert_move (target, result, 0);
3454 else
3455 target = convert_to_mode (target_mode, result, 0);
3457 return target;
3461 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3462 bytes from constant string DATA + OFFSET and return it as target
3463 constant. */
3465 static rtx
3466 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3467 enum machine_mode mode)
3469 const char *str = (const char *) data;
3471 gcc_assert (offset >= 0
3472 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3473 <= strlen (str) + 1));
3475 return c_readstr (str + offset, mode);
3478 /* Expand a call EXP to the memcpy builtin.
3479 Return NULL_RTX if we failed, the caller should emit a normal call,
3480 otherwise try to get the result in TARGET, if convenient (and in
3481 mode MODE if that's convenient). */
3483 static rtx
3484 expand_builtin_memcpy (tree exp, rtx target)
3486 if (!validate_arglist (exp,
3487 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3488 return NULL_RTX;
3489 else
3491 tree dest = CALL_EXPR_ARG (exp, 0);
3492 tree src = CALL_EXPR_ARG (exp, 1);
3493 tree len = CALL_EXPR_ARG (exp, 2);
3494 const char *src_str;
3495 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3496 unsigned int dest_align
3497 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3498 rtx dest_mem, src_mem, dest_addr, len_rtx;
3499 HOST_WIDE_INT expected_size = -1;
3500 unsigned int expected_align = 0;
3502 /* If DEST is not a pointer type, call the normal function. */
3503 if (dest_align == 0)
3504 return NULL_RTX;
3506 /* If either SRC is not a pointer type, don't do this
3507 operation in-line. */
3508 if (src_align == 0)
3509 return NULL_RTX;
3511 if (currently_expanding_gimple_stmt)
3512 stringop_block_profile (currently_expanding_gimple_stmt,
3513 &expected_align, &expected_size);
3515 if (expected_align < dest_align)
3516 expected_align = dest_align;
3517 dest_mem = get_memory_rtx (dest, len);
3518 set_mem_align (dest_mem, dest_align);
3519 len_rtx = expand_normal (len);
3520 src_str = c_getstr (src);
3522 /* If SRC is a string constant and block move would be done
3523 by pieces, we can avoid loading the string from memory
3524 and only stored the computed constants. */
3525 if (src_str
3526 && CONST_INT_P (len_rtx)
3527 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3528 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3529 CONST_CAST (char *, src_str),
3530 dest_align, false))
3532 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3533 builtin_memcpy_read_str,
3534 CONST_CAST (char *, src_str),
3535 dest_align, false, 0);
3536 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3537 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3538 return dest_mem;
3541 src_mem = get_memory_rtx (src, len);
3542 set_mem_align (src_mem, src_align);
3544 /* Copy word part most expediently. */
3545 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3546 CALL_EXPR_TAILCALL (exp)
3547 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3548 expected_align, expected_size);
3550 if (dest_addr == 0)
3552 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3553 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3555 return dest_addr;
3559 /* Expand a call EXP to the mempcpy builtin.
3560 Return NULL_RTX if we failed; the caller should emit a normal call,
3561 otherwise try to get the result in TARGET, if convenient (and in
3562 mode MODE if that's convenient). If ENDP is 0 return the
3563 destination pointer, if ENDP is 1 return the end pointer ala
3564 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3565 stpcpy. */
3567 static rtx
3568 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3570 if (!validate_arglist (exp,
3571 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3572 return NULL_RTX;
3573 else
3575 tree dest = CALL_EXPR_ARG (exp, 0);
3576 tree src = CALL_EXPR_ARG (exp, 1);
3577 tree len = CALL_EXPR_ARG (exp, 2);
3578 return expand_builtin_mempcpy_args (dest, src, len,
3579 target, mode, /*endp=*/ 1);
3583 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3584 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3585 so that this can also be called without constructing an actual CALL_EXPR.
3586 The other arguments and return value are the same as for
3587 expand_builtin_mempcpy. */
3589 static rtx
3590 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3591 rtx target, enum machine_mode mode, int endp)
3593 /* If return value is ignored, transform mempcpy into memcpy. */
3594 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3596 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3597 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3598 dest, src, len);
3599 return expand_expr (result, target, mode, EXPAND_NORMAL);
3601 else
3603 const char *src_str;
3604 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3605 unsigned int dest_align
3606 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3607 rtx dest_mem, src_mem, len_rtx;
3609 /* If either SRC or DEST is not a pointer type, don't do this
3610 operation in-line. */
3611 if (dest_align == 0 || src_align == 0)
3612 return NULL_RTX;
3614 /* If LEN is not constant, call the normal function. */
3615 if (! host_integerp (len, 1))
3616 return NULL_RTX;
3618 len_rtx = expand_normal (len);
3619 src_str = c_getstr (src);
3621 /* If SRC is a string constant and block move would be done
3622 by pieces, we can avoid loading the string from memory
3623 and only stored the computed constants. */
3624 if (src_str
3625 && CONST_INT_P (len_rtx)
3626 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3627 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3628 CONST_CAST (char *, src_str),
3629 dest_align, false))
3631 dest_mem = get_memory_rtx (dest, len);
3632 set_mem_align (dest_mem, dest_align);
3633 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3634 builtin_memcpy_read_str,
3635 CONST_CAST (char *, src_str),
3636 dest_align, false, endp);
3637 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3638 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3639 return dest_mem;
3642 if (CONST_INT_P (len_rtx)
3643 && can_move_by_pieces (INTVAL (len_rtx),
3644 MIN (dest_align, src_align)))
3646 dest_mem = get_memory_rtx (dest, len);
3647 set_mem_align (dest_mem, dest_align);
3648 src_mem = get_memory_rtx (src, len);
3649 set_mem_align (src_mem, src_align);
3650 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3651 MIN (dest_align, src_align), endp);
3652 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3653 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3654 return dest_mem;
3657 return NULL_RTX;
3661 #ifndef HAVE_movstr
3662 # define HAVE_movstr 0
3663 # define CODE_FOR_movstr CODE_FOR_nothing
3664 #endif
3666 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3667 we failed, the caller should emit a normal call, otherwise try to
3668 get the result in TARGET, if convenient. If ENDP is 0 return the
3669 destination pointer, if ENDP is 1 return the end pointer ala
3670 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3671 stpcpy. */
3673 static rtx
3674 expand_movstr (tree dest, tree src, rtx target, int endp)
3676 rtx end;
3677 rtx dest_mem;
3678 rtx src_mem;
3679 rtx insn;
3680 const struct insn_data_d * data;
3682 if (!HAVE_movstr)
3683 return NULL_RTX;
3685 dest_mem = get_memory_rtx (dest, NULL);
3686 src_mem = get_memory_rtx (src, NULL);
3687 data = insn_data + CODE_FOR_movstr;
3688 if (!endp)
3690 target = force_reg (Pmode, XEXP (dest_mem, 0));
3691 dest_mem = replace_equiv_address (dest_mem, target);
3692 end = gen_reg_rtx (Pmode);
3694 else
3696 if (target == 0
3697 || target == const0_rtx
3698 || ! (*data->operand[0].predicate) (target, Pmode))
3700 end = gen_reg_rtx (Pmode);
3701 if (target != const0_rtx)
3702 target = end;
3704 else
3705 end = target;
3708 if (data->operand[0].mode != VOIDmode)
3709 end = gen_lowpart (data->operand[0].mode, end);
3711 insn = data->genfun (end, dest_mem, src_mem);
3713 gcc_assert (insn);
3715 emit_insn (insn);
3717 /* movstr is supposed to set end to the address of the NUL
3718 terminator. If the caller requested a mempcpy-like return value,
3719 adjust it. */
3720 if (endp == 1 && target != const0_rtx)
3722 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3723 emit_move_insn (target, force_operand (tem, NULL_RTX));
3726 return target;
3729 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3730 NULL_RTX if we failed the caller should emit a normal call, otherwise
3731 try to get the result in TARGET, if convenient (and in mode MODE if that's
3732 convenient). */
3734 static rtx
3735 expand_builtin_strcpy (tree exp, rtx target)
3737 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3739 tree dest = CALL_EXPR_ARG (exp, 0);
3740 tree src = CALL_EXPR_ARG (exp, 1);
3741 return expand_builtin_strcpy_args (dest, src, target);
3743 return NULL_RTX;
3746 /* Helper function to do the actual work for expand_builtin_strcpy. The
3747 arguments to the builtin_strcpy call DEST and SRC are broken out
3748 so that this can also be called without constructing an actual CALL_EXPR.
3749 The other arguments and return value are the same as for
3750 expand_builtin_strcpy. */
3752 static rtx
3753 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3755 return expand_movstr (dest, src, target, /*endp=*/0);
3758 /* Expand a call EXP to the stpcpy builtin.
3759 Return NULL_RTX if we failed the caller should emit a normal call,
3760 otherwise try to get the result in TARGET, if convenient (and in
3761 mode MODE if that's convenient). */
3763 static rtx
3764 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3766 tree dst, src;
3767 location_t loc = EXPR_LOCATION (exp);
3769 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3770 return NULL_RTX;
3772 dst = CALL_EXPR_ARG (exp, 0);
3773 src = CALL_EXPR_ARG (exp, 1);
3775 /* If return value is ignored, transform stpcpy into strcpy. */
3776 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3778 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3779 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3780 return expand_expr (result, target, mode, EXPAND_NORMAL);
3782 else
3784 tree len, lenp1;
3785 rtx ret;
3787 /* Ensure we get an actual string whose length can be evaluated at
3788 compile-time, not an expression containing a string. This is
3789 because the latter will potentially produce pessimized code
3790 when used to produce the return value. */
3791 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3792 return expand_movstr (dst, src, target, /*endp=*/2);
3794 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3795 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3796 target, mode, /*endp=*/2);
3798 if (ret)
3799 return ret;
3801 if (TREE_CODE (len) == INTEGER_CST)
3803 rtx len_rtx = expand_normal (len);
3805 if (CONST_INT_P (len_rtx))
3807 ret = expand_builtin_strcpy_args (dst, src, target);
3809 if (ret)
3811 if (! target)
3813 if (mode != VOIDmode)
3814 target = gen_reg_rtx (mode);
3815 else
3816 target = gen_reg_rtx (GET_MODE (ret));
3818 if (GET_MODE (target) != GET_MODE (ret))
3819 ret = gen_lowpart (GET_MODE (target), ret);
3821 ret = plus_constant (ret, INTVAL (len_rtx));
3822 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3823 gcc_assert (ret);
3825 return target;
3830 return expand_movstr (dst, src, target, /*endp=*/2);
3834 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3835 bytes from constant string DATA + OFFSET and return it as target
3836 constant. */
3839 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3840 enum machine_mode mode)
3842 const char *str = (const char *) data;
3844 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3845 return const0_rtx;
3847 return c_readstr (str + offset, mode);
3850 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3851 NULL_RTX if we failed the caller should emit a normal call. */
3853 static rtx
3854 expand_builtin_strncpy (tree exp, rtx target)
3856 location_t loc = EXPR_LOCATION (exp);
3858 if (validate_arglist (exp,
3859 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3861 tree dest = CALL_EXPR_ARG (exp, 0);
3862 tree src = CALL_EXPR_ARG (exp, 1);
3863 tree len = CALL_EXPR_ARG (exp, 2);
3864 tree slen = c_strlen (src, 1);
3866 /* We must be passed a constant len and src parameter. */
3867 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3868 return NULL_RTX;
3870 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3872 /* We're required to pad with trailing zeros if the requested
3873 len is greater than strlen(s2)+1. In that case try to
3874 use store_by_pieces, if it fails, punt. */
3875 if (tree_int_cst_lt (slen, len))
3877 unsigned int dest_align
3878 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3879 const char *p = c_getstr (src);
3880 rtx dest_mem;
3882 if (!p || dest_align == 0 || !host_integerp (len, 1)
3883 || !can_store_by_pieces (tree_low_cst (len, 1),
3884 builtin_strncpy_read_str,
3885 CONST_CAST (char *, p),
3886 dest_align, false))
3887 return NULL_RTX;
3889 dest_mem = get_memory_rtx (dest, len);
3890 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3891 builtin_strncpy_read_str,
3892 CONST_CAST (char *, p), dest_align, false, 0);
3893 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3894 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3895 return dest_mem;
3898 return NULL_RTX;
3901 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3902 bytes from constant string DATA + OFFSET and return it as target
3903 constant. */
3906 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3907 enum machine_mode mode)
3909 const char *c = (const char *) data;
3910 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3912 memset (p, *c, GET_MODE_SIZE (mode));
3914 return c_readstr (p, mode);
3917 /* Callback routine for store_by_pieces. Return the RTL of a register
3918 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3919 char value given in the RTL register data. For example, if mode is
3920 4 bytes wide, return the RTL for 0x01010101*data. */
3922 static rtx
3923 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3924 enum machine_mode mode)
3926 rtx target, coeff;
3927 size_t size;
3928 char *p;
3930 size = GET_MODE_SIZE (mode);
3931 if (size == 1)
3932 return (rtx) data;
3934 p = XALLOCAVEC (char, size);
3935 memset (p, 1, size);
3936 coeff = c_readstr (p, mode);
3938 target = convert_to_mode (mode, (rtx) data, 1);
3939 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3940 return force_reg (mode, target);
3943 /* Expand expression EXP, which is a call to the memset builtin. Return
3944 NULL_RTX if we failed the caller should emit a normal call, otherwise
3945 try to get the result in TARGET, if convenient (and in mode MODE if that's
3946 convenient). */
3948 static rtx
3949 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3951 if (!validate_arglist (exp,
3952 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3953 return NULL_RTX;
3954 else
3956 tree dest = CALL_EXPR_ARG (exp, 0);
3957 tree val = CALL_EXPR_ARG (exp, 1);
3958 tree len = CALL_EXPR_ARG (exp, 2);
3959 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3963 /* Helper function to do the actual work for expand_builtin_memset. The
3964 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3965 so that this can also be called without constructing an actual CALL_EXPR.
3966 The other arguments and return value are the same as for
3967 expand_builtin_memset. */
3969 static rtx
3970 expand_builtin_memset_args (tree dest, tree val, tree len,
3971 rtx target, enum machine_mode mode, tree orig_exp)
3973 tree fndecl, fn;
3974 enum built_in_function fcode;
3975 char c;
3976 unsigned int dest_align;
3977 rtx dest_mem, dest_addr, len_rtx;
3978 HOST_WIDE_INT expected_size = -1;
3979 unsigned int expected_align = 0;
3981 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3983 /* If DEST is not a pointer type, don't do this operation in-line. */
3984 if (dest_align == 0)
3985 return NULL_RTX;
3987 if (currently_expanding_gimple_stmt)
3988 stringop_block_profile (currently_expanding_gimple_stmt,
3989 &expected_align, &expected_size);
3991 if (expected_align < dest_align)
3992 expected_align = dest_align;
3994 /* If the LEN parameter is zero, return DEST. */
3995 if (integer_zerop (len))
3997 /* Evaluate and ignore VAL in case it has side-effects. */
3998 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3999 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4002 /* Stabilize the arguments in case we fail. */
4003 dest = builtin_save_expr (dest);
4004 val = builtin_save_expr (val);
4005 len = builtin_save_expr (len);
4007 len_rtx = expand_normal (len);
4008 dest_mem = get_memory_rtx (dest, len);
4010 if (TREE_CODE (val) != INTEGER_CST)
4012 rtx val_rtx;
4014 val_rtx = expand_normal (val);
4015 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
4016 val_rtx, 0);
4018 /* Assume that we can memset by pieces if we can store
4019 * the coefficients by pieces (in the required modes).
4020 * We can't pass builtin_memset_gen_str as that emits RTL. */
4021 c = 1;
4022 if (host_integerp (len, 1)
4023 && can_store_by_pieces (tree_low_cst (len, 1),
4024 builtin_memset_read_str, &c, dest_align,
4025 true))
4027 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
4028 val_rtx);
4029 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4030 builtin_memset_gen_str, val_rtx, dest_align,
4031 true, 0);
4033 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4034 dest_align, expected_align,
4035 expected_size))
4036 goto do_libcall;
4038 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4039 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4040 return dest_mem;
4043 if (target_char_cast (val, &c))
4044 goto do_libcall;
4046 if (c)
4048 if (host_integerp (len, 1)
4049 && can_store_by_pieces (tree_low_cst (len, 1),
4050 builtin_memset_read_str, &c, dest_align,
4051 true))
4052 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4053 builtin_memset_read_str, &c, dest_align, true, 0);
4054 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
4055 dest_align, expected_align,
4056 expected_size))
4057 goto do_libcall;
4059 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4060 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4061 return dest_mem;
4064 set_mem_align (dest_mem, dest_align);
4065 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4066 CALL_EXPR_TAILCALL (orig_exp)
4067 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4068 expected_align, expected_size);
4070 if (dest_addr == 0)
4072 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4073 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4076 return dest_addr;
4078 do_libcall:
4079 fndecl = get_callee_fndecl (orig_exp);
4080 fcode = DECL_FUNCTION_CODE (fndecl);
4081 if (fcode == BUILT_IN_MEMSET)
4082 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4083 dest, val, len);
4084 else if (fcode == BUILT_IN_BZERO)
4085 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4086 dest, len);
4087 else
4088 gcc_unreachable ();
4089 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4090 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4091 return expand_call (fn, target, target == const0_rtx);
4094 /* Expand expression EXP, which is a call to the bzero builtin. Return
4095 NULL_RTX if we failed the caller should emit a normal call. */
4097 static rtx
4098 expand_builtin_bzero (tree exp)
4100 tree dest, size;
4101 location_t loc = EXPR_LOCATION (exp);
4103 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4104 return NULL_RTX;
4106 dest = CALL_EXPR_ARG (exp, 0);
4107 size = CALL_EXPR_ARG (exp, 1);
4109 /* New argument list transforming bzero(ptr x, int y) to
4110 memset(ptr x, int 0, size_t y). This is done this way
4111 so that if it isn't expanded inline, we fallback to
4112 calling bzero instead of memset. */
4114 return expand_builtin_memset_args (dest, integer_zero_node,
4115 fold_convert_loc (loc, sizetype, size),
4116 const0_rtx, VOIDmode, exp);
4119 /* Expand expression EXP, which is a call to the memcmp built-in function.
4120 Return NULL_RTX if we failed and the
4121 caller should emit a normal call, otherwise try to get the result in
4122 TARGET, if convenient (and in mode MODE, if that's convenient). */
4124 static rtx
4125 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4126 ATTRIBUTE_UNUSED enum machine_mode mode)
4128 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4130 if (!validate_arglist (exp,
4131 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4132 return NULL_RTX;
4134 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4136 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4137 rtx result;
4138 rtx insn;
4139 tree arg1 = CALL_EXPR_ARG (exp, 0);
4140 tree arg2 = CALL_EXPR_ARG (exp, 1);
4141 tree len = CALL_EXPR_ARG (exp, 2);
4143 unsigned int arg1_align
4144 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4145 unsigned int arg2_align
4146 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4147 enum machine_mode insn_mode;
4149 #ifdef HAVE_cmpmemsi
4150 if (HAVE_cmpmemsi)
4151 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4152 else
4153 #endif
4154 #ifdef HAVE_cmpstrnsi
4155 if (HAVE_cmpstrnsi)
4156 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4157 else
4158 #endif
4159 return NULL_RTX;
4161 /* If we don't have POINTER_TYPE, call the function. */
4162 if (arg1_align == 0 || arg2_align == 0)
4163 return NULL_RTX;
4165 /* Make a place to write the result of the instruction. */
4166 result = target;
4167 if (! (result != 0
4168 && REG_P (result) && GET_MODE (result) == insn_mode
4169 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4170 result = gen_reg_rtx (insn_mode);
4172 arg1_rtx = get_memory_rtx (arg1, len);
4173 arg2_rtx = get_memory_rtx (arg2, len);
4174 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4176 /* Set MEM_SIZE as appropriate. */
4177 if (CONST_INT_P (arg3_rtx))
4179 set_mem_size (arg1_rtx, arg3_rtx);
4180 set_mem_size (arg2_rtx, arg3_rtx);
4183 #ifdef HAVE_cmpmemsi
4184 if (HAVE_cmpmemsi)
4185 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4186 GEN_INT (MIN (arg1_align, arg2_align)));
4187 else
4188 #endif
4189 #ifdef HAVE_cmpstrnsi
4190 if (HAVE_cmpstrnsi)
4191 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4192 GEN_INT (MIN (arg1_align, arg2_align)));
4193 else
4194 #endif
4195 gcc_unreachable ();
4197 if (insn)
4198 emit_insn (insn);
4199 else
4200 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4201 TYPE_MODE (integer_type_node), 3,
4202 XEXP (arg1_rtx, 0), Pmode,
4203 XEXP (arg2_rtx, 0), Pmode,
4204 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4205 TYPE_UNSIGNED (sizetype)),
4206 TYPE_MODE (sizetype));
4208 /* Return the value in the proper mode for this function. */
4209 mode = TYPE_MODE (TREE_TYPE (exp));
4210 if (GET_MODE (result) == mode)
4211 return result;
4212 else if (target != 0)
4214 convert_move (target, result, 0);
4215 return target;
4217 else
4218 return convert_to_mode (mode, result, 0);
4220 #endif
4222 return NULL_RTX;
4225 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4226 if we failed the caller should emit a normal call, otherwise try to get
4227 the result in TARGET, if convenient. */
4229 static rtx
4230 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4232 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4233 return NULL_RTX;
4235 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4236 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4237 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4239 rtx arg1_rtx, arg2_rtx;
4240 rtx result, insn = NULL_RTX;
4241 tree fndecl, fn;
4242 tree arg1 = CALL_EXPR_ARG (exp, 0);
4243 tree arg2 = CALL_EXPR_ARG (exp, 1);
4245 unsigned int arg1_align
4246 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4247 unsigned int arg2_align
4248 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4250 /* If we don't have POINTER_TYPE, call the function. */
4251 if (arg1_align == 0 || arg2_align == 0)
4252 return NULL_RTX;
4254 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4255 arg1 = builtin_save_expr (arg1);
4256 arg2 = builtin_save_expr (arg2);
4258 arg1_rtx = get_memory_rtx (arg1, NULL);
4259 arg2_rtx = get_memory_rtx (arg2, NULL);
4261 #ifdef HAVE_cmpstrsi
4262 /* Try to call cmpstrsi. */
4263 if (HAVE_cmpstrsi)
4265 enum machine_mode insn_mode
4266 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4268 /* Make a place to write the result of the instruction. */
4269 result = target;
4270 if (! (result != 0
4271 && REG_P (result) && GET_MODE (result) == insn_mode
4272 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4273 result = gen_reg_rtx (insn_mode);
4275 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4276 GEN_INT (MIN (arg1_align, arg2_align)));
4278 #endif
4279 #ifdef HAVE_cmpstrnsi
4280 /* Try to determine at least one length and call cmpstrnsi. */
4281 if (!insn && HAVE_cmpstrnsi)
4283 tree len;
4284 rtx arg3_rtx;
4286 enum machine_mode insn_mode
4287 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4288 tree len1 = c_strlen (arg1, 1);
4289 tree len2 = c_strlen (arg2, 1);
4291 if (len1)
4292 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4293 if (len2)
4294 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4296 /* If we don't have a constant length for the first, use the length
4297 of the second, if we know it. We don't require a constant for
4298 this case; some cost analysis could be done if both are available
4299 but neither is constant. For now, assume they're equally cheap,
4300 unless one has side effects. If both strings have constant lengths,
4301 use the smaller. */
4303 if (!len1)
4304 len = len2;
4305 else if (!len2)
4306 len = len1;
4307 else if (TREE_SIDE_EFFECTS (len1))
4308 len = len2;
4309 else if (TREE_SIDE_EFFECTS (len2))
4310 len = len1;
4311 else if (TREE_CODE (len1) != INTEGER_CST)
4312 len = len2;
4313 else if (TREE_CODE (len2) != INTEGER_CST)
4314 len = len1;
4315 else if (tree_int_cst_lt (len1, len2))
4316 len = len1;
4317 else
4318 len = len2;
4320 /* If both arguments have side effects, we cannot optimize. */
4321 if (!len || TREE_SIDE_EFFECTS (len))
4322 goto do_libcall;
4324 arg3_rtx = expand_normal (len);
4326 /* Make a place to write the result of the instruction. */
4327 result = target;
4328 if (! (result != 0
4329 && REG_P (result) && GET_MODE (result) == insn_mode
4330 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4331 result = gen_reg_rtx (insn_mode);
4333 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4334 GEN_INT (MIN (arg1_align, arg2_align)));
4336 #endif
4338 if (insn)
4340 enum machine_mode mode;
4341 emit_insn (insn);
4343 /* Return the value in the proper mode for this function. */
4344 mode = TYPE_MODE (TREE_TYPE (exp));
4345 if (GET_MODE (result) == mode)
4346 return result;
4347 if (target == 0)
4348 return convert_to_mode (mode, result, 0);
4349 convert_move (target, result, 0);
4350 return target;
4353 /* Expand the library call ourselves using a stabilized argument
4354 list to avoid re-evaluating the function's arguments twice. */
4355 #ifdef HAVE_cmpstrnsi
4356 do_libcall:
4357 #endif
4358 fndecl = get_callee_fndecl (exp);
4359 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4360 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4361 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4362 return expand_call (fn, target, target == const0_rtx);
4364 #endif
4365 return NULL_RTX;
4368 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4369 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4370 the result in TARGET, if convenient. */
4372 static rtx
4373 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4374 ATTRIBUTE_UNUSED enum machine_mode mode)
4376 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4378 if (!validate_arglist (exp,
4379 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4380 return NULL_RTX;
4382 /* If c_strlen can determine an expression for one of the string
4383 lengths, and it doesn't have side effects, then emit cmpstrnsi
4384 using length MIN(strlen(string)+1, arg3). */
4385 #ifdef HAVE_cmpstrnsi
4386 if (HAVE_cmpstrnsi)
4388 tree len, len1, len2;
4389 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4390 rtx result, insn;
4391 tree fndecl, fn;
4392 tree arg1 = CALL_EXPR_ARG (exp, 0);
4393 tree arg2 = CALL_EXPR_ARG (exp, 1);
4394 tree arg3 = CALL_EXPR_ARG (exp, 2);
4396 unsigned int arg1_align
4397 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4398 unsigned int arg2_align
4399 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4400 enum machine_mode insn_mode
4401 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4403 len1 = c_strlen (arg1, 1);
4404 len2 = c_strlen (arg2, 1);
4406 if (len1)
4407 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4408 if (len2)
4409 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4411 /* If we don't have a constant length for the first, use the length
4412 of the second, if we know it. We don't require a constant for
4413 this case; some cost analysis could be done if both are available
4414 but neither is constant. For now, assume they're equally cheap,
4415 unless one has side effects. If both strings have constant lengths,
4416 use the smaller. */
4418 if (!len1)
4419 len = len2;
4420 else if (!len2)
4421 len = len1;
4422 else if (TREE_SIDE_EFFECTS (len1))
4423 len = len2;
4424 else if (TREE_SIDE_EFFECTS (len2))
4425 len = len1;
4426 else if (TREE_CODE (len1) != INTEGER_CST)
4427 len = len2;
4428 else if (TREE_CODE (len2) != INTEGER_CST)
4429 len = len1;
4430 else if (tree_int_cst_lt (len1, len2))
4431 len = len1;
4432 else
4433 len = len2;
4435 /* If both arguments have side effects, we cannot optimize. */
4436 if (!len || TREE_SIDE_EFFECTS (len))
4437 return NULL_RTX;
4439 /* The actual new length parameter is MIN(len,arg3). */
4440 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4441 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4443 /* If we don't have POINTER_TYPE, call the function. */
4444 if (arg1_align == 0 || arg2_align == 0)
4445 return NULL_RTX;
4447 /* Make a place to write the result of the instruction. */
4448 result = target;
4449 if (! (result != 0
4450 && REG_P (result) && GET_MODE (result) == insn_mode
4451 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4452 result = gen_reg_rtx (insn_mode);
4454 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4455 arg1 = builtin_save_expr (arg1);
4456 arg2 = builtin_save_expr (arg2);
4457 len = builtin_save_expr (len);
4459 arg1_rtx = get_memory_rtx (arg1, len);
4460 arg2_rtx = get_memory_rtx (arg2, len);
4461 arg3_rtx = expand_normal (len);
4462 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4463 GEN_INT (MIN (arg1_align, arg2_align)));
4464 if (insn)
4466 emit_insn (insn);
4468 /* Return the value in the proper mode for this function. */
4469 mode = TYPE_MODE (TREE_TYPE (exp));
4470 if (GET_MODE (result) == mode)
4471 return result;
4472 if (target == 0)
4473 return convert_to_mode (mode, result, 0);
4474 convert_move (target, result, 0);
4475 return target;
4478 /* Expand the library call ourselves using a stabilized argument
4479 list to avoid re-evaluating the function's arguments twice. */
4480 fndecl = get_callee_fndecl (exp);
4481 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4482 arg1, arg2, len);
4483 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4484 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4485 return expand_call (fn, target, target == const0_rtx);
4487 #endif
4488 return NULL_RTX;
4491 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4492 if that's convenient. */
4495 expand_builtin_saveregs (void)
4497 rtx val, seq;
4499 /* Don't do __builtin_saveregs more than once in a function.
4500 Save the result of the first call and reuse it. */
4501 if (saveregs_value != 0)
4502 return saveregs_value;
4504 /* When this function is called, it means that registers must be
4505 saved on entry to this function. So we migrate the call to the
4506 first insn of this function. */
4508 start_sequence ();
4510 /* Do whatever the machine needs done in this case. */
4511 val = targetm.calls.expand_builtin_saveregs ();
4513 seq = get_insns ();
4514 end_sequence ();
4516 saveregs_value = val;
4518 /* Put the insns after the NOTE that starts the function. If this
4519 is inside a start_sequence, make the outer-level insn chain current, so
4520 the code is placed at the start of the function. */
4521 push_topmost_sequence ();
4522 emit_insn_after (seq, entry_of_function ());
4523 pop_topmost_sequence ();
4525 return val;
4528 /* Expand a call to __builtin_next_arg. */
4530 static rtx
4531 expand_builtin_next_arg (void)
4533 /* Checking arguments is already done in fold_builtin_next_arg
4534 that must be called before this function. */
4535 return expand_binop (ptr_mode, add_optab,
4536 crtl->args.internal_arg_pointer,
4537 crtl->args.arg_offset_rtx,
4538 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4541 /* Make it easier for the backends by protecting the valist argument
4542 from multiple evaluations. */
4544 static tree
4545 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4547 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4549 /* The current way of determining the type of valist is completely
4550 bogus. We should have the information on the va builtin instead. */
4551 if (!vatype)
4552 vatype = targetm.fn_abi_va_list (cfun->decl);
4554 if (TREE_CODE (vatype) == ARRAY_TYPE)
4556 if (TREE_SIDE_EFFECTS (valist))
4557 valist = save_expr (valist);
4559 /* For this case, the backends will be expecting a pointer to
4560 vatype, but it's possible we've actually been given an array
4561 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4562 So fix it. */
4563 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4565 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4566 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4569 else
4571 tree pt = build_pointer_type (vatype);
4573 if (! needs_lvalue)
4575 if (! TREE_SIDE_EFFECTS (valist))
4576 return valist;
4578 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4579 TREE_SIDE_EFFECTS (valist) = 1;
4582 if (TREE_SIDE_EFFECTS (valist))
4583 valist = save_expr (valist);
4584 valist = fold_build2_loc (loc, MEM_REF,
4585 vatype, valist, build_int_cst (pt, 0));
4588 return valist;
4591 /* The "standard" definition of va_list is void*. */
4593 tree
4594 std_build_builtin_va_list (void)
4596 return ptr_type_node;
4599 /* The "standard" abi va_list is va_list_type_node. */
4601 tree
4602 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4604 return va_list_type_node;
4607 /* The "standard" type of va_list is va_list_type_node. */
4609 tree
4610 std_canonical_va_list_type (tree type)
4612 tree wtype, htype;
4614 if (INDIRECT_REF_P (type))
4615 type = TREE_TYPE (type);
4616 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4617 type = TREE_TYPE (type);
4618 wtype = va_list_type_node;
4619 htype = type;
4620 /* Treat structure va_list types. */
4621 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4622 htype = TREE_TYPE (htype);
4623 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4625 /* If va_list is an array type, the argument may have decayed
4626 to a pointer type, e.g. by being passed to another function.
4627 In that case, unwrap both types so that we can compare the
4628 underlying records. */
4629 if (TREE_CODE (htype) == ARRAY_TYPE
4630 || POINTER_TYPE_P (htype))
4632 wtype = TREE_TYPE (wtype);
4633 htype = TREE_TYPE (htype);
4636 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4637 return va_list_type_node;
4639 return NULL_TREE;
4642 /* The "standard" implementation of va_start: just assign `nextarg' to
4643 the variable. */
4645 void
4646 std_expand_builtin_va_start (tree valist, rtx nextarg)
4648 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4649 convert_move (va_r, nextarg, 0);
4652 /* Expand EXP, a call to __builtin_va_start. */
4654 static rtx
4655 expand_builtin_va_start (tree exp)
4657 rtx nextarg;
4658 tree valist;
4659 location_t loc = EXPR_LOCATION (exp);
4661 if (call_expr_nargs (exp) < 2)
4663 error_at (loc, "too few arguments to function %<va_start%>");
4664 return const0_rtx;
4667 if (fold_builtin_next_arg (exp, true))
4668 return const0_rtx;
4670 nextarg = expand_builtin_next_arg ();
4671 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4673 if (targetm.expand_builtin_va_start)
4674 targetm.expand_builtin_va_start (valist, nextarg);
4675 else
4676 std_expand_builtin_va_start (valist, nextarg);
4678 return const0_rtx;
4681 /* The "standard" implementation of va_arg: read the value from the
4682 current (padded) address and increment by the (padded) size. */
4684 tree
4685 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4686 gimple_seq *post_p)
4688 tree addr, t, type_size, rounded_size, valist_tmp;
4689 unsigned HOST_WIDE_INT align, boundary;
4690 bool indirect;
4692 #ifdef ARGS_GROW_DOWNWARD
4693 /* All of the alignment and movement below is for args-grow-up machines.
4694 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4695 implement their own specialized gimplify_va_arg_expr routines. */
4696 gcc_unreachable ();
4697 #endif
4699 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4700 if (indirect)
4701 type = build_pointer_type (type);
4703 align = PARM_BOUNDARY / BITS_PER_UNIT;
4704 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4706 /* When we align parameter on stack for caller, if the parameter
4707 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4708 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4709 here with caller. */
4710 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4711 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4713 boundary /= BITS_PER_UNIT;
4715 /* Hoist the valist value into a temporary for the moment. */
4716 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4718 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4719 requires greater alignment, we must perform dynamic alignment. */
4720 if (boundary > align
4721 && !integer_zerop (TYPE_SIZE (type)))
4723 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4724 fold_build2 (POINTER_PLUS_EXPR,
4725 TREE_TYPE (valist),
4726 valist_tmp, size_int (boundary - 1)));
4727 gimplify_and_add (t, pre_p);
4729 t = fold_convert (sizetype, valist_tmp);
4730 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4731 fold_convert (TREE_TYPE (valist),
4732 fold_build2 (BIT_AND_EXPR, sizetype, t,
4733 size_int (-boundary))));
4734 gimplify_and_add (t, pre_p);
4736 else
4737 boundary = align;
4739 /* If the actual alignment is less than the alignment of the type,
4740 adjust the type accordingly so that we don't assume strict alignment
4741 when dereferencing the pointer. */
4742 boundary *= BITS_PER_UNIT;
4743 if (boundary < TYPE_ALIGN (type))
4745 type = build_variant_type_copy (type);
4746 TYPE_ALIGN (type) = boundary;
4749 /* Compute the rounded size of the type. */
4750 type_size = size_in_bytes (type);
4751 rounded_size = round_up (type_size, align);
4753 /* Reduce rounded_size so it's sharable with the postqueue. */
4754 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4756 /* Get AP. */
4757 addr = valist_tmp;
4758 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4760 /* Small args are padded downward. */
4761 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4762 rounded_size, size_int (align));
4763 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4764 size_binop (MINUS_EXPR, rounded_size, type_size));
4765 addr = fold_build2 (POINTER_PLUS_EXPR,
4766 TREE_TYPE (addr), addr, t);
4769 /* Compute new value for AP. */
4770 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4771 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4772 gimplify_and_add (t, pre_p);
4774 addr = fold_convert (build_pointer_type (type), addr);
4776 if (indirect)
4777 addr = build_va_arg_indirect_ref (addr);
4779 return build_va_arg_indirect_ref (addr);
4782 /* Build an indirect-ref expression over the given TREE, which represents a
4783 piece of a va_arg() expansion. */
4784 tree
4785 build_va_arg_indirect_ref (tree addr)
4787 addr = build_fold_indirect_ref_loc (EXPR_LOCATION (addr), addr);
4789 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4790 mf_mark (addr);
4792 return addr;
4795 /* Return a dummy expression of type TYPE in order to keep going after an
4796 error. */
4798 static tree
4799 dummy_object (tree type)
4801 tree t = build_int_cst (build_pointer_type (type), 0);
4802 return build2 (MEM_REF, type, t, t);
4805 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4806 builtin function, but a very special sort of operator. */
4808 enum gimplify_status
4809 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4811 tree promoted_type, have_va_type;
4812 tree valist = TREE_OPERAND (*expr_p, 0);
4813 tree type = TREE_TYPE (*expr_p);
4814 tree t;
4815 location_t loc = EXPR_LOCATION (*expr_p);
4817 /* Verify that valist is of the proper type. */
4818 have_va_type = TREE_TYPE (valist);
4819 if (have_va_type == error_mark_node)
4820 return GS_ERROR;
4821 have_va_type = targetm.canonical_va_list_type (have_va_type);
4823 if (have_va_type == NULL_TREE)
4825 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4826 return GS_ERROR;
4829 /* Generate a diagnostic for requesting data of a type that cannot
4830 be passed through `...' due to type promotion at the call site. */
4831 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4832 != type)
4834 static bool gave_help;
4835 bool warned;
4837 /* Unfortunately, this is merely undefined, rather than a constraint
4838 violation, so we cannot make this an error. If this call is never
4839 executed, the program is still strictly conforming. */
4840 warned = warning_at (loc, 0,
4841 "%qT is promoted to %qT when passed through %<...%>",
4842 type, promoted_type);
4843 if (!gave_help && warned)
4845 gave_help = true;
4846 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4847 promoted_type, type);
4850 /* We can, however, treat "undefined" any way we please.
4851 Call abort to encourage the user to fix the program. */
4852 if (warned)
4853 inform (loc, "if this code is reached, the program will abort");
4854 /* Before the abort, allow the evaluation of the va_list
4855 expression to exit or longjmp. */
4856 gimplify_and_add (valist, pre_p);
4857 t = build_call_expr_loc (loc,
4858 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4859 gimplify_and_add (t, pre_p);
4861 /* This is dead code, but go ahead and finish so that the
4862 mode of the result comes out right. */
4863 *expr_p = dummy_object (type);
4864 return GS_ALL_DONE;
4866 else
4868 /* Make it easier for the backends by protecting the valist argument
4869 from multiple evaluations. */
4870 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4872 /* For this case, the backends will be expecting a pointer to
4873 TREE_TYPE (abi), but it's possible we've
4874 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4875 So fix it. */
4876 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4878 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4879 valist = fold_convert_loc (loc, p1,
4880 build_fold_addr_expr_loc (loc, valist));
4883 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4885 else
4886 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4888 if (!targetm.gimplify_va_arg_expr)
4889 /* FIXME: Once most targets are converted we should merely
4890 assert this is non-null. */
4891 return GS_ALL_DONE;
4893 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4894 return GS_OK;
4898 /* Expand EXP, a call to __builtin_va_end. */
4900 static rtx
4901 expand_builtin_va_end (tree exp)
4903 tree valist = CALL_EXPR_ARG (exp, 0);
4905 /* Evaluate for side effects, if needed. I hate macros that don't
4906 do that. */
4907 if (TREE_SIDE_EFFECTS (valist))
4908 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4910 return const0_rtx;
4913 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4914 builtin rather than just as an assignment in stdarg.h because of the
4915 nastiness of array-type va_list types. */
4917 static rtx
4918 expand_builtin_va_copy (tree exp)
4920 tree dst, src, t;
4921 location_t loc = EXPR_LOCATION (exp);
4923 dst = CALL_EXPR_ARG (exp, 0);
4924 src = CALL_EXPR_ARG (exp, 1);
4926 dst = stabilize_va_list_loc (loc, dst, 1);
4927 src = stabilize_va_list_loc (loc, src, 0);
4929 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4931 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4933 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4934 TREE_SIDE_EFFECTS (t) = 1;
4935 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4937 else
4939 rtx dstb, srcb, size;
4941 /* Evaluate to pointers. */
4942 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4943 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4944 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4945 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4947 dstb = convert_memory_address (Pmode, dstb);
4948 srcb = convert_memory_address (Pmode, srcb);
4950 /* "Dereference" to BLKmode memories. */
4951 dstb = gen_rtx_MEM (BLKmode, dstb);
4952 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4953 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4954 srcb = gen_rtx_MEM (BLKmode, srcb);
4955 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4956 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4958 /* Copy. */
4959 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4962 return const0_rtx;
4965 /* Expand a call to one of the builtin functions __builtin_frame_address or
4966 __builtin_return_address. */
4968 static rtx
4969 expand_builtin_frame_address (tree fndecl, tree exp)
4971 /* The argument must be a nonnegative integer constant.
4972 It counts the number of frames to scan up the stack.
4973 The value is the return address saved in that frame. */
4974 if (call_expr_nargs (exp) == 0)
4975 /* Warning about missing arg was already issued. */
4976 return const0_rtx;
4977 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4979 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4980 error ("invalid argument to %<__builtin_frame_address%>");
4981 else
4982 error ("invalid argument to %<__builtin_return_address%>");
4983 return const0_rtx;
4985 else
4987 rtx tem
4988 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4989 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4991 /* Some ports cannot access arbitrary stack frames. */
4992 if (tem == NULL)
4994 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4995 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4996 else
4997 warning (0, "unsupported argument to %<__builtin_return_address%>");
4998 return const0_rtx;
5001 /* For __builtin_frame_address, return what we've got. */
5002 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5003 return tem;
5005 if (!REG_P (tem)
5006 && ! CONSTANT_P (tem))
5007 tem = copy_to_mode_reg (Pmode, tem);
5008 return tem;
5012 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5013 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
5014 is the same as for allocate_dynamic_stack_space. */
5016 static rtx
5017 expand_builtin_alloca (tree exp, bool cannot_accumulate)
5019 rtx op0;
5020 rtx result;
5022 /* Emit normal call if marked not-inlineable. */
5023 if (CALL_CANNOT_INLINE_P (exp))
5024 return NULL_RTX;
5026 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5027 return NULL_RTX;
5029 /* Compute the argument. */
5030 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5032 /* Allocate the desired space. */
5033 result = allocate_dynamic_stack_space (op0, 0, BIGGEST_ALIGNMENT,
5034 cannot_accumulate);
5035 result = convert_memory_address (ptr_mode, result);
5037 return result;
5040 /* Expand a call to a bswap builtin with argument ARG0. MODE
5041 is the mode to expand with. */
5043 static rtx
5044 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5046 enum machine_mode mode;
5047 tree arg;
5048 rtx op0;
5050 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5051 return NULL_RTX;
5053 arg = CALL_EXPR_ARG (exp, 0);
5054 mode = TYPE_MODE (TREE_TYPE (arg));
5055 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5057 target = expand_unop (mode, bswap_optab, op0, target, 1);
5059 gcc_assert (target);
5061 return convert_to_mode (mode, target, 0);
5064 /* Expand a call to a unary builtin in EXP.
5065 Return NULL_RTX if a normal call should be emitted rather than expanding the
5066 function in-line. If convenient, the result should be placed in TARGET.
5067 SUBTARGET may be used as the target for computing one of EXP's operands. */
5069 static rtx
5070 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5071 rtx subtarget, optab op_optab)
5073 rtx op0;
5075 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5076 return NULL_RTX;
5078 /* Compute the argument. */
5079 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5080 (subtarget
5081 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5082 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5083 VOIDmode, EXPAND_NORMAL);
5084 /* Compute op, into TARGET if possible.
5085 Set TARGET to wherever the result comes back. */
5086 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5087 op_optab, op0, target, 1);
5088 gcc_assert (target);
5090 return convert_to_mode (target_mode, target, 0);
5093 /* Expand a call to __builtin_expect. We just return our argument
5094 as the builtin_expect semantic should've been already executed by
5095 tree branch prediction pass. */
5097 static rtx
5098 expand_builtin_expect (tree exp, rtx target)
5100 tree arg;
5102 if (call_expr_nargs (exp) < 2)
5103 return const0_rtx;
5104 arg = CALL_EXPR_ARG (exp, 0);
5106 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5107 /* When guessing was done, the hints should be already stripped away. */
5108 gcc_assert (!flag_guess_branch_prob
5109 || optimize == 0 || seen_error ());
5110 return target;
5113 void
5114 expand_builtin_trap (void)
5116 #ifdef HAVE_trap
5117 if (HAVE_trap)
5118 emit_insn (gen_trap ());
5119 else
5120 #endif
5121 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5122 emit_barrier ();
5125 /* Expand a call to __builtin_unreachable. We do nothing except emit
5126 a barrier saying that control flow will not pass here.
5128 It is the responsibility of the program being compiled to ensure
5129 that control flow does never reach __builtin_unreachable. */
5130 static void
5131 expand_builtin_unreachable (void)
5133 emit_barrier ();
5136 /* Expand EXP, a call to fabs, fabsf or fabsl.
5137 Return NULL_RTX if a normal call should be emitted rather than expanding
5138 the function inline. If convenient, the result should be placed
5139 in TARGET. SUBTARGET may be used as the target for computing
5140 the operand. */
5142 static rtx
5143 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5145 enum machine_mode mode;
5146 tree arg;
5147 rtx op0;
5149 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5150 return NULL_RTX;
5152 arg = CALL_EXPR_ARG (exp, 0);
5153 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5154 mode = TYPE_MODE (TREE_TYPE (arg));
5155 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5156 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5159 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5160 Return NULL is a normal call should be emitted rather than expanding the
5161 function inline. If convenient, the result should be placed in TARGET.
5162 SUBTARGET may be used as the target for computing the operand. */
5164 static rtx
5165 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5167 rtx op0, op1;
5168 tree arg;
5170 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5171 return NULL_RTX;
5173 arg = CALL_EXPR_ARG (exp, 0);
5174 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5176 arg = CALL_EXPR_ARG (exp, 1);
5177 op1 = expand_normal (arg);
5179 return expand_copysign (op0, op1, target);
5182 /* Create a new constant string literal and return a char* pointer to it.
5183 The STRING_CST value is the LEN characters at STR. */
5184 tree
5185 build_string_literal (int len, const char *str)
5187 tree t, elem, index, type;
5189 t = build_string (len, str);
5190 elem = build_type_variant (char_type_node, 1, 0);
5191 index = build_index_type (size_int (len - 1));
5192 type = build_array_type (elem, index);
5193 TREE_TYPE (t) = type;
5194 TREE_CONSTANT (t) = 1;
5195 TREE_READONLY (t) = 1;
5196 TREE_STATIC (t) = 1;
5198 type = build_pointer_type (elem);
5199 t = build1 (ADDR_EXPR, type,
5200 build4 (ARRAY_REF, elem,
5201 t, integer_zero_node, NULL_TREE, NULL_TREE));
5202 return t;
5205 /* Expand a call to __builtin___clear_cache. */
5207 static rtx
5208 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5210 #ifndef HAVE_clear_cache
5211 #ifdef CLEAR_INSN_CACHE
5212 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5213 does something. Just do the default expansion to a call to
5214 __clear_cache(). */
5215 return NULL_RTX;
5216 #else
5217 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5218 does nothing. There is no need to call it. Do nothing. */
5219 return const0_rtx;
5220 #endif /* CLEAR_INSN_CACHE */
5221 #else
5222 /* We have a "clear_cache" insn, and it will handle everything. */
5223 tree begin, end;
5224 rtx begin_rtx, end_rtx;
5225 enum insn_code icode;
5227 /* We must not expand to a library call. If we did, any
5228 fallback library function in libgcc that might contain a call to
5229 __builtin___clear_cache() would recurse infinitely. */
5230 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5232 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5233 return const0_rtx;
5236 if (HAVE_clear_cache)
5238 icode = CODE_FOR_clear_cache;
5240 begin = CALL_EXPR_ARG (exp, 0);
5241 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5242 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5243 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5244 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5246 end = CALL_EXPR_ARG (exp, 1);
5247 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5248 end_rtx = convert_memory_address (Pmode, end_rtx);
5249 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5250 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5252 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5254 return const0_rtx;
5255 #endif /* HAVE_clear_cache */
5258 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5260 static rtx
5261 round_trampoline_addr (rtx tramp)
5263 rtx temp, addend, mask;
5265 /* If we don't need too much alignment, we'll have been guaranteed
5266 proper alignment by get_trampoline_type. */
5267 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5268 return tramp;
5270 /* Round address up to desired boundary. */
5271 temp = gen_reg_rtx (Pmode);
5272 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5273 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5275 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5276 temp, 0, OPTAB_LIB_WIDEN);
5277 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5278 temp, 0, OPTAB_LIB_WIDEN);
5280 return tramp;
5283 static rtx
5284 expand_builtin_init_trampoline (tree exp)
5286 tree t_tramp, t_func, t_chain;
5287 rtx m_tramp, r_tramp, r_chain, tmp;
5289 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5290 POINTER_TYPE, VOID_TYPE))
5291 return NULL_RTX;
5293 t_tramp = CALL_EXPR_ARG (exp, 0);
5294 t_func = CALL_EXPR_ARG (exp, 1);
5295 t_chain = CALL_EXPR_ARG (exp, 2);
5297 r_tramp = expand_normal (t_tramp);
5298 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5299 MEM_NOTRAP_P (m_tramp) = 1;
5301 /* The TRAMP argument should be the address of a field within the
5302 local function's FRAME decl. Let's see if we can fill in the
5303 to fill in the MEM_ATTRs for this memory. */
5304 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5305 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5306 true, 0);
5308 tmp = round_trampoline_addr (r_tramp);
5309 if (tmp != r_tramp)
5311 m_tramp = change_address (m_tramp, BLKmode, tmp);
5312 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5313 set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5316 /* The FUNC argument should be the address of the nested function.
5317 Extract the actual function decl to pass to the hook. */
5318 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5319 t_func = TREE_OPERAND (t_func, 0);
5320 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5322 r_chain = expand_normal (t_chain);
5324 /* Generate insns to initialize the trampoline. */
5325 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5327 trampolines_created = 1;
5329 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5330 "trampoline generated for nested function %qD", t_func);
5332 return const0_rtx;
5335 static rtx
5336 expand_builtin_adjust_trampoline (tree exp)
5338 rtx tramp;
5340 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5341 return NULL_RTX;
5343 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5344 tramp = round_trampoline_addr (tramp);
5345 if (targetm.calls.trampoline_adjust_address)
5346 tramp = targetm.calls.trampoline_adjust_address (tramp);
5348 return tramp;
5351 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5352 function. The function first checks whether the back end provides
5353 an insn to implement signbit for the respective mode. If not, it
5354 checks whether the floating point format of the value is such that
5355 the sign bit can be extracted. If that is not the case, the
5356 function returns NULL_RTX to indicate that a normal call should be
5357 emitted rather than expanding the function in-line. EXP is the
5358 expression that is a call to the builtin function; if convenient,
5359 the result should be placed in TARGET. */
5360 static rtx
5361 expand_builtin_signbit (tree exp, rtx target)
5363 const struct real_format *fmt;
5364 enum machine_mode fmode, imode, rmode;
5365 tree arg;
5366 int word, bitpos;
5367 enum insn_code icode;
5368 rtx temp;
5369 location_t loc = EXPR_LOCATION (exp);
5371 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5372 return NULL_RTX;
5374 arg = CALL_EXPR_ARG (exp, 0);
5375 fmode = TYPE_MODE (TREE_TYPE (arg));
5376 rmode = TYPE_MODE (TREE_TYPE (exp));
5377 fmt = REAL_MODE_FORMAT (fmode);
5379 arg = builtin_save_expr (arg);
5381 /* Expand the argument yielding a RTX expression. */
5382 temp = expand_normal (arg);
5384 /* Check if the back end provides an insn that handles signbit for the
5385 argument's mode. */
5386 icode = optab_handler (signbit_optab, fmode);
5387 if (icode != CODE_FOR_nothing)
5389 rtx last = get_last_insn ();
5390 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5391 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5392 return target;
5393 delete_insns_since (last);
5396 /* For floating point formats without a sign bit, implement signbit
5397 as "ARG < 0.0". */
5398 bitpos = fmt->signbit_ro;
5399 if (bitpos < 0)
5401 /* But we can't do this if the format supports signed zero. */
5402 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5403 return NULL_RTX;
5405 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5406 build_real (TREE_TYPE (arg), dconst0));
5407 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5410 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5412 imode = int_mode_for_mode (fmode);
5413 if (imode == BLKmode)
5414 return NULL_RTX;
5415 temp = gen_lowpart (imode, temp);
5417 else
5419 imode = word_mode;
5420 /* Handle targets with different FP word orders. */
5421 if (FLOAT_WORDS_BIG_ENDIAN)
5422 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5423 else
5424 word = bitpos / BITS_PER_WORD;
5425 temp = operand_subword_force (temp, word, fmode);
5426 bitpos = bitpos % BITS_PER_WORD;
5429 /* Force the intermediate word_mode (or narrower) result into a
5430 register. This avoids attempting to create paradoxical SUBREGs
5431 of floating point modes below. */
5432 temp = force_reg (imode, temp);
5434 /* If the bitpos is within the "result mode" lowpart, the operation
5435 can be implement with a single bitwise AND. Otherwise, we need
5436 a right shift and an AND. */
5438 if (bitpos < GET_MODE_BITSIZE (rmode))
5440 double_int mask = double_int_setbit (double_int_zero, bitpos);
5442 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5443 temp = gen_lowpart (rmode, temp);
5444 temp = expand_binop (rmode, and_optab, temp,
5445 immed_double_int_const (mask, rmode),
5446 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5448 else
5450 /* Perform a logical right shift to place the signbit in the least
5451 significant bit, then truncate the result to the desired mode
5452 and mask just this bit. */
5453 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5454 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5455 temp = gen_lowpart (rmode, temp);
5456 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5457 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5460 return temp;
5463 /* Expand fork or exec calls. TARGET is the desired target of the
5464 call. EXP is the call. FN is the
5465 identificator of the actual function. IGNORE is nonzero if the
5466 value is to be ignored. */
5468 static rtx
5469 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5471 tree id, decl;
5472 tree call;
5474 /* If we are not profiling, just call the function. */
5475 if (!profile_arc_flag)
5476 return NULL_RTX;
5478 /* Otherwise call the wrapper. This should be equivalent for the rest of
5479 compiler, so the code does not diverge, and the wrapper may run the
5480 code necessary for keeping the profiling sane. */
5482 switch (DECL_FUNCTION_CODE (fn))
5484 case BUILT_IN_FORK:
5485 id = get_identifier ("__gcov_fork");
5486 break;
5488 case BUILT_IN_EXECL:
5489 id = get_identifier ("__gcov_execl");
5490 break;
5492 case BUILT_IN_EXECV:
5493 id = get_identifier ("__gcov_execv");
5494 break;
5496 case BUILT_IN_EXECLP:
5497 id = get_identifier ("__gcov_execlp");
5498 break;
5500 case BUILT_IN_EXECLE:
5501 id = get_identifier ("__gcov_execle");
5502 break;
5504 case BUILT_IN_EXECVP:
5505 id = get_identifier ("__gcov_execvp");
5506 break;
5508 case BUILT_IN_EXECVE:
5509 id = get_identifier ("__gcov_execve");
5510 break;
5512 default:
5513 gcc_unreachable ();
5516 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5517 FUNCTION_DECL, id, TREE_TYPE (fn));
5518 DECL_EXTERNAL (decl) = 1;
5519 TREE_PUBLIC (decl) = 1;
5520 DECL_ARTIFICIAL (decl) = 1;
5521 TREE_NOTHROW (decl) = 1;
5522 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5523 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5524 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5525 return expand_call (call, target, ignore);
5530 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5531 the pointer in these functions is void*, the tree optimizers may remove
5532 casts. The mode computed in expand_builtin isn't reliable either, due
5533 to __sync_bool_compare_and_swap.
5535 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5536 group of builtins. This gives us log2 of the mode size. */
5538 static inline enum machine_mode
5539 get_builtin_sync_mode (int fcode_diff)
5541 /* The size is not negotiable, so ask not to get BLKmode in return
5542 if the target indicates that a smaller size would be better. */
5543 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5546 /* Expand the memory expression LOC and return the appropriate memory operand
5547 for the builtin_sync operations. */
5549 static rtx
5550 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5552 rtx addr, mem;
5554 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5555 addr = convert_memory_address (Pmode, addr);
5557 /* Note that we explicitly do not want any alias information for this
5558 memory, so that we kill all other live memories. Otherwise we don't
5559 satisfy the full barrier semantics of the intrinsic. */
5560 mem = validize_mem (gen_rtx_MEM (mode, addr));
5562 /* The alignment needs to be at least according to that of the mode. */
5563 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5564 get_pointer_alignment (loc, BIGGEST_ALIGNMENT)));
5565 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5566 MEM_VOLATILE_P (mem) = 1;
5568 return mem;
5571 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5572 EXP is the CALL_EXPR. CODE is the rtx code
5573 that corresponds to the arithmetic or logical operation from the name;
5574 an exception here is that NOT actually means NAND. TARGET is an optional
5575 place for us to store the results; AFTER is true if this is the
5576 fetch_and_xxx form. IGNORE is true if we don't actually care about
5577 the result of the operation at all. */
5579 static rtx
5580 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5581 enum rtx_code code, bool after,
5582 rtx target, bool ignore)
5584 rtx val, mem;
5585 enum machine_mode old_mode;
5586 location_t loc = EXPR_LOCATION (exp);
5588 if (code == NOT && warn_sync_nand)
5590 tree fndecl = get_callee_fndecl (exp);
5591 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5593 static bool warned_f_a_n, warned_n_a_f;
5595 switch (fcode)
5597 case BUILT_IN_FETCH_AND_NAND_1:
5598 case BUILT_IN_FETCH_AND_NAND_2:
5599 case BUILT_IN_FETCH_AND_NAND_4:
5600 case BUILT_IN_FETCH_AND_NAND_8:
5601 case BUILT_IN_FETCH_AND_NAND_16:
5603 if (warned_f_a_n)
5604 break;
5606 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5607 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5608 warned_f_a_n = true;
5609 break;
5611 case BUILT_IN_NAND_AND_FETCH_1:
5612 case BUILT_IN_NAND_AND_FETCH_2:
5613 case BUILT_IN_NAND_AND_FETCH_4:
5614 case BUILT_IN_NAND_AND_FETCH_8:
5615 case BUILT_IN_NAND_AND_FETCH_16:
5617 if (warned_n_a_f)
5618 break;
5620 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5621 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5622 warned_n_a_f = true;
5623 break;
5625 default:
5626 gcc_unreachable ();
5630 /* Expand the operands. */
5631 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5633 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5634 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5635 of CONST_INTs, where we know the old_mode only from the call argument. */
5636 old_mode = GET_MODE (val);
5637 if (old_mode == VOIDmode)
5638 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5639 val = convert_modes (mode, old_mode, val, 1);
5641 if (ignore)
5642 return expand_sync_operation (mem, val, code);
5643 else
5644 return expand_sync_fetch_operation (mem, val, code, after, target);
5647 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5648 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5649 true if this is the boolean form. TARGET is a place for us to store the
5650 results; this is NOT optional if IS_BOOL is true. */
5652 static rtx
5653 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5654 bool is_bool, rtx target)
5656 rtx old_val, new_val, mem;
5657 enum machine_mode old_mode;
5659 /* Expand the operands. */
5660 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5663 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5664 mode, EXPAND_NORMAL);
5665 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5666 of CONST_INTs, where we know the old_mode only from the call argument. */
5667 old_mode = GET_MODE (old_val);
5668 if (old_mode == VOIDmode)
5669 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5670 old_val = convert_modes (mode, old_mode, old_val, 1);
5672 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5673 mode, EXPAND_NORMAL);
5674 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5675 of CONST_INTs, where we know the old_mode only from the call argument. */
5676 old_mode = GET_MODE (new_val);
5677 if (old_mode == VOIDmode)
5678 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5679 new_val = convert_modes (mode, old_mode, new_val, 1);
5681 if (is_bool)
5682 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5683 else
5684 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5687 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5688 general form is actually an atomic exchange, and some targets only
5689 support a reduced form with the second argument being a constant 1.
5690 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5691 the results. */
5693 static rtx
5694 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5695 rtx target)
5697 rtx val, mem;
5698 enum machine_mode old_mode;
5700 /* Expand the operands. */
5701 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5702 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5703 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5704 of CONST_INTs, where we know the old_mode only from the call argument. */
5705 old_mode = GET_MODE (val);
5706 if (old_mode == VOIDmode)
5707 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5708 val = convert_modes (mode, old_mode, val, 1);
5710 return expand_sync_lock_test_and_set (mem, val, target);
5713 /* Expand the __sync_synchronize intrinsic. */
5715 static void
5716 expand_builtin_synchronize (void)
5718 gimple x;
5719 VEC (tree, gc) *v_clobbers;
5721 #ifdef HAVE_memory_barrier
5722 if (HAVE_memory_barrier)
5724 emit_insn (gen_memory_barrier ());
5725 return;
5727 #endif
5729 if (synchronize_libfunc != NULL_RTX)
5731 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5732 return;
5735 /* If no explicit memory barrier instruction is available, create an
5736 empty asm stmt with a memory clobber. */
5737 v_clobbers = VEC_alloc (tree, gc, 1);
5738 VEC_quick_push (tree, v_clobbers,
5739 tree_cons (NULL, build_string (6, "memory"), NULL));
5740 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5741 gimple_asm_set_volatile (x, true);
5742 expand_asm_stmt (x);
5745 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5747 static void
5748 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5750 enum insn_code icode;
5751 rtx mem, insn;
5752 rtx val = const0_rtx;
5754 /* Expand the operands. */
5755 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5757 /* If there is an explicit operation in the md file, use it. */
5758 icode = direct_optab_handler (sync_lock_release_optab, mode);
5759 if (icode != CODE_FOR_nothing)
5761 if (!insn_data[icode].operand[1].predicate (val, mode))
5762 val = force_reg (mode, val);
5764 insn = GEN_FCN (icode) (mem, val);
5765 if (insn)
5767 emit_insn (insn);
5768 return;
5772 /* Otherwise we can implement this operation by emitting a barrier
5773 followed by a store of zero. */
5774 expand_builtin_synchronize ();
5775 emit_move_insn (mem, val);
5778 /* Expand an expression EXP that calls a built-in function,
5779 with result going to TARGET if that's convenient
5780 (and in mode MODE if that's convenient).
5781 SUBTARGET may be used as the target for computing one of EXP's operands.
5782 IGNORE is nonzero if the value is to be ignored. */
5785 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5786 int ignore)
5788 tree fndecl = get_callee_fndecl (exp);
5789 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5790 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5791 int flags;
5793 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5794 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5796 /* When not optimizing, generate calls to library functions for a certain
5797 set of builtins. */
5798 if (!optimize
5799 && !called_as_built_in (fndecl)
5800 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5801 && fcode != BUILT_IN_ALLOCA
5802 && fcode != BUILT_IN_FREE)
5803 return expand_call (exp, target, ignore);
5805 /* The built-in function expanders test for target == const0_rtx
5806 to determine whether the function's result will be ignored. */
5807 if (ignore)
5808 target = const0_rtx;
5810 /* If the result of a pure or const built-in function is ignored, and
5811 none of its arguments are volatile, we can avoid expanding the
5812 built-in call and just evaluate the arguments for side-effects. */
5813 if (target == const0_rtx
5814 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5815 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5817 bool volatilep = false;
5818 tree arg;
5819 call_expr_arg_iterator iter;
5821 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5822 if (TREE_THIS_VOLATILE (arg))
5824 volatilep = true;
5825 break;
5828 if (! volatilep)
5830 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5831 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5832 return const0_rtx;
5836 switch (fcode)
5838 CASE_FLT_FN (BUILT_IN_FABS):
5839 target = expand_builtin_fabs (exp, target, subtarget);
5840 if (target)
5841 return target;
5842 break;
5844 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5845 target = expand_builtin_copysign (exp, target, subtarget);
5846 if (target)
5847 return target;
5848 break;
5850 /* Just do a normal library call if we were unable to fold
5851 the values. */
5852 CASE_FLT_FN (BUILT_IN_CABS):
5853 break;
5855 CASE_FLT_FN (BUILT_IN_EXP):
5856 CASE_FLT_FN (BUILT_IN_EXP10):
5857 CASE_FLT_FN (BUILT_IN_POW10):
5858 CASE_FLT_FN (BUILT_IN_EXP2):
5859 CASE_FLT_FN (BUILT_IN_EXPM1):
5860 CASE_FLT_FN (BUILT_IN_LOGB):
5861 CASE_FLT_FN (BUILT_IN_LOG):
5862 CASE_FLT_FN (BUILT_IN_LOG10):
5863 CASE_FLT_FN (BUILT_IN_LOG2):
5864 CASE_FLT_FN (BUILT_IN_LOG1P):
5865 CASE_FLT_FN (BUILT_IN_TAN):
5866 CASE_FLT_FN (BUILT_IN_ASIN):
5867 CASE_FLT_FN (BUILT_IN_ACOS):
5868 CASE_FLT_FN (BUILT_IN_ATAN):
5869 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5870 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5871 because of possible accuracy problems. */
5872 if (! flag_unsafe_math_optimizations)
5873 break;
5874 CASE_FLT_FN (BUILT_IN_SQRT):
5875 CASE_FLT_FN (BUILT_IN_FLOOR):
5876 CASE_FLT_FN (BUILT_IN_CEIL):
5877 CASE_FLT_FN (BUILT_IN_TRUNC):
5878 CASE_FLT_FN (BUILT_IN_ROUND):
5879 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5880 CASE_FLT_FN (BUILT_IN_RINT):
5881 target = expand_builtin_mathfn (exp, target, subtarget);
5882 if (target)
5883 return target;
5884 break;
5886 CASE_FLT_FN (BUILT_IN_FMA):
5887 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5888 if (target)
5889 return target;
5890 break;
5892 CASE_FLT_FN (BUILT_IN_ILOGB):
5893 if (! flag_unsafe_math_optimizations)
5894 break;
5895 CASE_FLT_FN (BUILT_IN_ISINF):
5896 CASE_FLT_FN (BUILT_IN_FINITE):
5897 case BUILT_IN_ISFINITE:
5898 case BUILT_IN_ISNORMAL:
5899 target = expand_builtin_interclass_mathfn (exp, target);
5900 if (target)
5901 return target;
5902 break;
5904 CASE_FLT_FN (BUILT_IN_LCEIL):
5905 CASE_FLT_FN (BUILT_IN_LLCEIL):
5906 CASE_FLT_FN (BUILT_IN_LFLOOR):
5907 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5908 target = expand_builtin_int_roundingfn (exp, target);
5909 if (target)
5910 return target;
5911 break;
5913 CASE_FLT_FN (BUILT_IN_LRINT):
5914 CASE_FLT_FN (BUILT_IN_LLRINT):
5915 CASE_FLT_FN (BUILT_IN_LROUND):
5916 CASE_FLT_FN (BUILT_IN_LLROUND):
5917 target = expand_builtin_int_roundingfn_2 (exp, target);
5918 if (target)
5919 return target;
5920 break;
5922 CASE_FLT_FN (BUILT_IN_POW):
5923 target = expand_builtin_pow (exp, target, subtarget);
5924 if (target)
5925 return target;
5926 break;
5928 CASE_FLT_FN (BUILT_IN_POWI):
5929 target = expand_builtin_powi (exp, target);
5930 if (target)
5931 return target;
5932 break;
5934 CASE_FLT_FN (BUILT_IN_ATAN2):
5935 CASE_FLT_FN (BUILT_IN_LDEXP):
5936 CASE_FLT_FN (BUILT_IN_SCALB):
5937 CASE_FLT_FN (BUILT_IN_SCALBN):
5938 CASE_FLT_FN (BUILT_IN_SCALBLN):
5939 if (! flag_unsafe_math_optimizations)
5940 break;
5942 CASE_FLT_FN (BUILT_IN_FMOD):
5943 CASE_FLT_FN (BUILT_IN_REMAINDER):
5944 CASE_FLT_FN (BUILT_IN_DREM):
5945 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5946 if (target)
5947 return target;
5948 break;
5950 CASE_FLT_FN (BUILT_IN_CEXPI):
5951 target = expand_builtin_cexpi (exp, target);
5952 gcc_assert (target);
5953 return target;
5955 CASE_FLT_FN (BUILT_IN_SIN):
5956 CASE_FLT_FN (BUILT_IN_COS):
5957 if (! flag_unsafe_math_optimizations)
5958 break;
5959 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5960 if (target)
5961 return target;
5962 break;
5964 CASE_FLT_FN (BUILT_IN_SINCOS):
5965 if (! flag_unsafe_math_optimizations)
5966 break;
5967 target = expand_builtin_sincos (exp);
5968 if (target)
5969 return target;
5970 break;
5972 case BUILT_IN_APPLY_ARGS:
5973 return expand_builtin_apply_args ();
5975 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5976 FUNCTION with a copy of the parameters described by
5977 ARGUMENTS, and ARGSIZE. It returns a block of memory
5978 allocated on the stack into which is stored all the registers
5979 that might possibly be used for returning the result of a
5980 function. ARGUMENTS is the value returned by
5981 __builtin_apply_args. ARGSIZE is the number of bytes of
5982 arguments that must be copied. ??? How should this value be
5983 computed? We'll also need a safe worst case value for varargs
5984 functions. */
5985 case BUILT_IN_APPLY:
5986 if (!validate_arglist (exp, POINTER_TYPE,
5987 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5988 && !validate_arglist (exp, REFERENCE_TYPE,
5989 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5990 return const0_rtx;
5991 else
5993 rtx ops[3];
5995 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5996 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5997 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5999 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6002 /* __builtin_return (RESULT) causes the function to return the
6003 value described by RESULT. RESULT is address of the block of
6004 memory returned by __builtin_apply. */
6005 case BUILT_IN_RETURN:
6006 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6007 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6008 return const0_rtx;
6010 case BUILT_IN_SAVEREGS:
6011 return expand_builtin_saveregs ();
6013 case BUILT_IN_VA_ARG_PACK:
6014 /* All valid uses of __builtin_va_arg_pack () are removed during
6015 inlining. */
6016 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6017 return const0_rtx;
6019 case BUILT_IN_VA_ARG_PACK_LEN:
6020 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6021 inlining. */
6022 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6023 return const0_rtx;
6025 /* Return the address of the first anonymous stack arg. */
6026 case BUILT_IN_NEXT_ARG:
6027 if (fold_builtin_next_arg (exp, false))
6028 return const0_rtx;
6029 return expand_builtin_next_arg ();
6031 case BUILT_IN_CLEAR_CACHE:
6032 target = expand_builtin___clear_cache (exp);
6033 if (target)
6034 return target;
6035 break;
6037 case BUILT_IN_CLASSIFY_TYPE:
6038 return expand_builtin_classify_type (exp);
6040 case BUILT_IN_CONSTANT_P:
6041 return const0_rtx;
6043 case BUILT_IN_FRAME_ADDRESS:
6044 case BUILT_IN_RETURN_ADDRESS:
6045 return expand_builtin_frame_address (fndecl, exp);
6047 /* Returns the address of the area where the structure is returned.
6048 0 otherwise. */
6049 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6050 if (call_expr_nargs (exp) != 0
6051 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6052 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6053 return const0_rtx;
6054 else
6055 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6057 case BUILT_IN_ALLOCA:
6058 /* If the allocation stems from the declaration of a variable-sized
6059 object, it cannot accumulate. */
6060 target = expand_builtin_alloca (exp, ALLOCA_FOR_VAR_P (exp));
6061 if (target)
6062 return target;
6063 break;
6065 case BUILT_IN_STACK_SAVE:
6066 return expand_stack_save ();
6068 case BUILT_IN_STACK_RESTORE:
6069 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6070 return const0_rtx;
6072 case BUILT_IN_BSWAP32:
6073 case BUILT_IN_BSWAP64:
6074 target = expand_builtin_bswap (exp, target, subtarget);
6076 if (target)
6077 return target;
6078 break;
6080 CASE_INT_FN (BUILT_IN_FFS):
6081 case BUILT_IN_FFSIMAX:
6082 target = expand_builtin_unop (target_mode, exp, target,
6083 subtarget, ffs_optab);
6084 if (target)
6085 return target;
6086 break;
6088 CASE_INT_FN (BUILT_IN_CLZ):
6089 case BUILT_IN_CLZIMAX:
6090 target = expand_builtin_unop (target_mode, exp, target,
6091 subtarget, clz_optab);
6092 if (target)
6093 return target;
6094 break;
6096 CASE_INT_FN (BUILT_IN_CTZ):
6097 case BUILT_IN_CTZIMAX:
6098 target = expand_builtin_unop (target_mode, exp, target,
6099 subtarget, ctz_optab);
6100 if (target)
6101 return target;
6102 break;
6104 CASE_INT_FN (BUILT_IN_POPCOUNT):
6105 case BUILT_IN_POPCOUNTIMAX:
6106 target = expand_builtin_unop (target_mode, exp, target,
6107 subtarget, popcount_optab);
6108 if (target)
6109 return target;
6110 break;
6112 CASE_INT_FN (BUILT_IN_PARITY):
6113 case BUILT_IN_PARITYIMAX:
6114 target = expand_builtin_unop (target_mode, exp, target,
6115 subtarget, parity_optab);
6116 if (target)
6117 return target;
6118 break;
6120 case BUILT_IN_STRLEN:
6121 target = expand_builtin_strlen (exp, target, target_mode);
6122 if (target)
6123 return target;
6124 break;
6126 case BUILT_IN_STRCPY:
6127 target = expand_builtin_strcpy (exp, target);
6128 if (target)
6129 return target;
6130 break;
6132 case BUILT_IN_STRNCPY:
6133 target = expand_builtin_strncpy (exp, target);
6134 if (target)
6135 return target;
6136 break;
6138 case BUILT_IN_STPCPY:
6139 target = expand_builtin_stpcpy (exp, target, mode);
6140 if (target)
6141 return target;
6142 break;
6144 case BUILT_IN_MEMCPY:
6145 target = expand_builtin_memcpy (exp, target);
6146 if (target)
6147 return target;
6148 break;
6150 case BUILT_IN_MEMPCPY:
6151 target = expand_builtin_mempcpy (exp, target, mode);
6152 if (target)
6153 return target;
6154 break;
6156 case BUILT_IN_MEMSET:
6157 target = expand_builtin_memset (exp, target, mode);
6158 if (target)
6159 return target;
6160 break;
6162 case BUILT_IN_BZERO:
6163 target = expand_builtin_bzero (exp);
6164 if (target)
6165 return target;
6166 break;
6168 case BUILT_IN_STRCMP:
6169 target = expand_builtin_strcmp (exp, target);
6170 if (target)
6171 return target;
6172 break;
6174 case BUILT_IN_STRNCMP:
6175 target = expand_builtin_strncmp (exp, target, mode);
6176 if (target)
6177 return target;
6178 break;
6180 case BUILT_IN_BCMP:
6181 case BUILT_IN_MEMCMP:
6182 target = expand_builtin_memcmp (exp, target, mode);
6183 if (target)
6184 return target;
6185 break;
6187 case BUILT_IN_SETJMP:
6188 /* This should have been lowered to the builtins below. */
6189 gcc_unreachable ();
6191 case BUILT_IN_SETJMP_SETUP:
6192 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6193 and the receiver label. */
6194 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6196 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6197 VOIDmode, EXPAND_NORMAL);
6198 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6199 rtx label_r = label_rtx (label);
6201 /* This is copied from the handling of non-local gotos. */
6202 expand_builtin_setjmp_setup (buf_addr, label_r);
6203 nonlocal_goto_handler_labels
6204 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6205 nonlocal_goto_handler_labels);
6206 /* ??? Do not let expand_label treat us as such since we would
6207 not want to be both on the list of non-local labels and on
6208 the list of forced labels. */
6209 FORCED_LABEL (label) = 0;
6210 return const0_rtx;
6212 break;
6214 case BUILT_IN_SETJMP_DISPATCHER:
6215 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6216 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6218 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6219 rtx label_r = label_rtx (label);
6221 /* Remove the dispatcher label from the list of non-local labels
6222 since the receiver labels have been added to it above. */
6223 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6224 return const0_rtx;
6226 break;
6228 case BUILT_IN_SETJMP_RECEIVER:
6229 /* __builtin_setjmp_receiver is passed the receiver label. */
6230 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6232 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6233 rtx label_r = label_rtx (label);
6235 expand_builtin_setjmp_receiver (label_r);
6236 return const0_rtx;
6238 break;
6240 /* __builtin_longjmp is passed a pointer to an array of five words.
6241 It's similar to the C library longjmp function but works with
6242 __builtin_setjmp above. */
6243 case BUILT_IN_LONGJMP:
6244 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6246 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6247 VOIDmode, EXPAND_NORMAL);
6248 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6250 if (value != const1_rtx)
6252 error ("%<__builtin_longjmp%> second argument must be 1");
6253 return const0_rtx;
6256 expand_builtin_longjmp (buf_addr, value);
6257 return const0_rtx;
6259 break;
6261 case BUILT_IN_NONLOCAL_GOTO:
6262 target = expand_builtin_nonlocal_goto (exp);
6263 if (target)
6264 return target;
6265 break;
6267 /* This updates the setjmp buffer that is its argument with the value
6268 of the current stack pointer. */
6269 case BUILT_IN_UPDATE_SETJMP_BUF:
6270 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6272 rtx buf_addr
6273 = expand_normal (CALL_EXPR_ARG (exp, 0));
6275 expand_builtin_update_setjmp_buf (buf_addr);
6276 return const0_rtx;
6278 break;
6280 case BUILT_IN_TRAP:
6281 expand_builtin_trap ();
6282 return const0_rtx;
6284 case BUILT_IN_UNREACHABLE:
6285 expand_builtin_unreachable ();
6286 return const0_rtx;
6288 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6289 case BUILT_IN_SIGNBITD32:
6290 case BUILT_IN_SIGNBITD64:
6291 case BUILT_IN_SIGNBITD128:
6292 target = expand_builtin_signbit (exp, target);
6293 if (target)
6294 return target;
6295 break;
6297 /* Various hooks for the DWARF 2 __throw routine. */
6298 case BUILT_IN_UNWIND_INIT:
6299 expand_builtin_unwind_init ();
6300 return const0_rtx;
6301 case BUILT_IN_DWARF_CFA:
6302 return virtual_cfa_rtx;
6303 #ifdef DWARF2_UNWIND_INFO
6304 case BUILT_IN_DWARF_SP_COLUMN:
6305 return expand_builtin_dwarf_sp_column ();
6306 case BUILT_IN_INIT_DWARF_REG_SIZES:
6307 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6308 return const0_rtx;
6309 #endif
6310 case BUILT_IN_FROB_RETURN_ADDR:
6311 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6312 case BUILT_IN_EXTRACT_RETURN_ADDR:
6313 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6314 case BUILT_IN_EH_RETURN:
6315 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6316 CALL_EXPR_ARG (exp, 1));
6317 return const0_rtx;
6318 #ifdef EH_RETURN_DATA_REGNO
6319 case BUILT_IN_EH_RETURN_DATA_REGNO:
6320 return expand_builtin_eh_return_data_regno (exp);
6321 #endif
6322 case BUILT_IN_EXTEND_POINTER:
6323 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6324 case BUILT_IN_EH_POINTER:
6325 return expand_builtin_eh_pointer (exp);
6326 case BUILT_IN_EH_FILTER:
6327 return expand_builtin_eh_filter (exp);
6328 case BUILT_IN_EH_COPY_VALUES:
6329 return expand_builtin_eh_copy_values (exp);
6331 case BUILT_IN_VA_START:
6332 return expand_builtin_va_start (exp);
6333 case BUILT_IN_VA_END:
6334 return expand_builtin_va_end (exp);
6335 case BUILT_IN_VA_COPY:
6336 return expand_builtin_va_copy (exp);
6337 case BUILT_IN_EXPECT:
6338 return expand_builtin_expect (exp, target);
6339 case BUILT_IN_PREFETCH:
6340 expand_builtin_prefetch (exp);
6341 return const0_rtx;
6343 case BUILT_IN_INIT_TRAMPOLINE:
6344 return expand_builtin_init_trampoline (exp);
6345 case BUILT_IN_ADJUST_TRAMPOLINE:
6346 return expand_builtin_adjust_trampoline (exp);
6348 case BUILT_IN_FORK:
6349 case BUILT_IN_EXECL:
6350 case BUILT_IN_EXECV:
6351 case BUILT_IN_EXECLP:
6352 case BUILT_IN_EXECLE:
6353 case BUILT_IN_EXECVP:
6354 case BUILT_IN_EXECVE:
6355 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6356 if (target)
6357 return target;
6358 break;
6360 case BUILT_IN_FETCH_AND_ADD_1:
6361 case BUILT_IN_FETCH_AND_ADD_2:
6362 case BUILT_IN_FETCH_AND_ADD_4:
6363 case BUILT_IN_FETCH_AND_ADD_8:
6364 case BUILT_IN_FETCH_AND_ADD_16:
6365 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6366 target = expand_builtin_sync_operation (mode, exp, PLUS,
6367 false, target, ignore);
6368 if (target)
6369 return target;
6370 break;
6372 case BUILT_IN_FETCH_AND_SUB_1:
6373 case BUILT_IN_FETCH_AND_SUB_2:
6374 case BUILT_IN_FETCH_AND_SUB_4:
6375 case BUILT_IN_FETCH_AND_SUB_8:
6376 case BUILT_IN_FETCH_AND_SUB_16:
6377 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6378 target = expand_builtin_sync_operation (mode, exp, MINUS,
6379 false, target, ignore);
6380 if (target)
6381 return target;
6382 break;
6384 case BUILT_IN_FETCH_AND_OR_1:
6385 case BUILT_IN_FETCH_AND_OR_2:
6386 case BUILT_IN_FETCH_AND_OR_4:
6387 case BUILT_IN_FETCH_AND_OR_8:
6388 case BUILT_IN_FETCH_AND_OR_16:
6389 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6390 target = expand_builtin_sync_operation (mode, exp, IOR,
6391 false, target, ignore);
6392 if (target)
6393 return target;
6394 break;
6396 case BUILT_IN_FETCH_AND_AND_1:
6397 case BUILT_IN_FETCH_AND_AND_2:
6398 case BUILT_IN_FETCH_AND_AND_4:
6399 case BUILT_IN_FETCH_AND_AND_8:
6400 case BUILT_IN_FETCH_AND_AND_16:
6401 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6402 target = expand_builtin_sync_operation (mode, exp, AND,
6403 false, target, ignore);
6404 if (target)
6405 return target;
6406 break;
6408 case BUILT_IN_FETCH_AND_XOR_1:
6409 case BUILT_IN_FETCH_AND_XOR_2:
6410 case BUILT_IN_FETCH_AND_XOR_4:
6411 case BUILT_IN_FETCH_AND_XOR_8:
6412 case BUILT_IN_FETCH_AND_XOR_16:
6413 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6414 target = expand_builtin_sync_operation (mode, exp, XOR,
6415 false, target, ignore);
6416 if (target)
6417 return target;
6418 break;
6420 case BUILT_IN_FETCH_AND_NAND_1:
6421 case BUILT_IN_FETCH_AND_NAND_2:
6422 case BUILT_IN_FETCH_AND_NAND_4:
6423 case BUILT_IN_FETCH_AND_NAND_8:
6424 case BUILT_IN_FETCH_AND_NAND_16:
6425 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6426 target = expand_builtin_sync_operation (mode, exp, NOT,
6427 false, target, ignore);
6428 if (target)
6429 return target;
6430 break;
6432 case BUILT_IN_ADD_AND_FETCH_1:
6433 case BUILT_IN_ADD_AND_FETCH_2:
6434 case BUILT_IN_ADD_AND_FETCH_4:
6435 case BUILT_IN_ADD_AND_FETCH_8:
6436 case BUILT_IN_ADD_AND_FETCH_16:
6437 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6438 target = expand_builtin_sync_operation (mode, exp, PLUS,
6439 true, target, ignore);
6440 if (target)
6441 return target;
6442 break;
6444 case BUILT_IN_SUB_AND_FETCH_1:
6445 case BUILT_IN_SUB_AND_FETCH_2:
6446 case BUILT_IN_SUB_AND_FETCH_4:
6447 case BUILT_IN_SUB_AND_FETCH_8:
6448 case BUILT_IN_SUB_AND_FETCH_16:
6449 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6450 target = expand_builtin_sync_operation (mode, exp, MINUS,
6451 true, target, ignore);
6452 if (target)
6453 return target;
6454 break;
6456 case BUILT_IN_OR_AND_FETCH_1:
6457 case BUILT_IN_OR_AND_FETCH_2:
6458 case BUILT_IN_OR_AND_FETCH_4:
6459 case BUILT_IN_OR_AND_FETCH_8:
6460 case BUILT_IN_OR_AND_FETCH_16:
6461 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6462 target = expand_builtin_sync_operation (mode, exp, IOR,
6463 true, target, ignore);
6464 if (target)
6465 return target;
6466 break;
6468 case BUILT_IN_AND_AND_FETCH_1:
6469 case BUILT_IN_AND_AND_FETCH_2:
6470 case BUILT_IN_AND_AND_FETCH_4:
6471 case BUILT_IN_AND_AND_FETCH_8:
6472 case BUILT_IN_AND_AND_FETCH_16:
6473 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6474 target = expand_builtin_sync_operation (mode, exp, AND,
6475 true, target, ignore);
6476 if (target)
6477 return target;
6478 break;
6480 case BUILT_IN_XOR_AND_FETCH_1:
6481 case BUILT_IN_XOR_AND_FETCH_2:
6482 case BUILT_IN_XOR_AND_FETCH_4:
6483 case BUILT_IN_XOR_AND_FETCH_8:
6484 case BUILT_IN_XOR_AND_FETCH_16:
6485 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6486 target = expand_builtin_sync_operation (mode, exp, XOR,
6487 true, target, ignore);
6488 if (target)
6489 return target;
6490 break;
6492 case BUILT_IN_NAND_AND_FETCH_1:
6493 case BUILT_IN_NAND_AND_FETCH_2:
6494 case BUILT_IN_NAND_AND_FETCH_4:
6495 case BUILT_IN_NAND_AND_FETCH_8:
6496 case BUILT_IN_NAND_AND_FETCH_16:
6497 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6498 target = expand_builtin_sync_operation (mode, exp, NOT,
6499 true, target, ignore);
6500 if (target)
6501 return target;
6502 break;
6504 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6505 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6506 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6507 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6508 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6509 if (mode == VOIDmode)
6510 mode = TYPE_MODE (boolean_type_node);
6511 if (!target || !register_operand (target, mode))
6512 target = gen_reg_rtx (mode);
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6515 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6516 if (target)
6517 return target;
6518 break;
6520 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6521 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6522 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6523 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6524 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6525 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6526 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6527 if (target)
6528 return target;
6529 break;
6531 case BUILT_IN_LOCK_TEST_AND_SET_1:
6532 case BUILT_IN_LOCK_TEST_AND_SET_2:
6533 case BUILT_IN_LOCK_TEST_AND_SET_4:
6534 case BUILT_IN_LOCK_TEST_AND_SET_8:
6535 case BUILT_IN_LOCK_TEST_AND_SET_16:
6536 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6537 target = expand_builtin_lock_test_and_set (mode, exp, target);
6538 if (target)
6539 return target;
6540 break;
6542 case BUILT_IN_LOCK_RELEASE_1:
6543 case BUILT_IN_LOCK_RELEASE_2:
6544 case BUILT_IN_LOCK_RELEASE_4:
6545 case BUILT_IN_LOCK_RELEASE_8:
6546 case BUILT_IN_LOCK_RELEASE_16:
6547 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6548 expand_builtin_lock_release (mode, exp);
6549 return const0_rtx;
6551 case BUILT_IN_SYNCHRONIZE:
6552 expand_builtin_synchronize ();
6553 return const0_rtx;
6555 case BUILT_IN_OBJECT_SIZE:
6556 return expand_builtin_object_size (exp);
6558 case BUILT_IN_MEMCPY_CHK:
6559 case BUILT_IN_MEMPCPY_CHK:
6560 case BUILT_IN_MEMMOVE_CHK:
6561 case BUILT_IN_MEMSET_CHK:
6562 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6563 if (target)
6564 return target;
6565 break;
6567 case BUILT_IN_STRCPY_CHK:
6568 case BUILT_IN_STPCPY_CHK:
6569 case BUILT_IN_STRNCPY_CHK:
6570 case BUILT_IN_STRCAT_CHK:
6571 case BUILT_IN_STRNCAT_CHK:
6572 case BUILT_IN_SNPRINTF_CHK:
6573 case BUILT_IN_VSNPRINTF_CHK:
6574 maybe_emit_chk_warning (exp, fcode);
6575 break;
6577 case BUILT_IN_SPRINTF_CHK:
6578 case BUILT_IN_VSPRINTF_CHK:
6579 maybe_emit_sprintf_chk_warning (exp, fcode);
6580 break;
6582 case BUILT_IN_FREE:
6583 maybe_emit_free_warning (exp);
6584 break;
6586 default: /* just do library call, if unknown builtin */
6587 break;
6590 /* The switch statement above can drop through to cause the function
6591 to be called normally. */
6592 return expand_call (exp, target, ignore);
6595 /* Determine whether a tree node represents a call to a built-in
6596 function. If the tree T is a call to a built-in function with
6597 the right number of arguments of the appropriate types, return
6598 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6599 Otherwise the return value is END_BUILTINS. */
6601 enum built_in_function
6602 builtin_mathfn_code (const_tree t)
6604 const_tree fndecl, arg, parmlist;
6605 const_tree argtype, parmtype;
6606 const_call_expr_arg_iterator iter;
6608 if (TREE_CODE (t) != CALL_EXPR
6609 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6610 return END_BUILTINS;
6612 fndecl = get_callee_fndecl (t);
6613 if (fndecl == NULL_TREE
6614 || TREE_CODE (fndecl) != FUNCTION_DECL
6615 || ! DECL_BUILT_IN (fndecl)
6616 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6617 return END_BUILTINS;
6619 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6620 init_const_call_expr_arg_iterator (t, &iter);
6621 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6623 /* If a function doesn't take a variable number of arguments,
6624 the last element in the list will have type `void'. */
6625 parmtype = TREE_VALUE (parmlist);
6626 if (VOID_TYPE_P (parmtype))
6628 if (more_const_call_expr_args_p (&iter))
6629 return END_BUILTINS;
6630 return DECL_FUNCTION_CODE (fndecl);
6633 if (! more_const_call_expr_args_p (&iter))
6634 return END_BUILTINS;
6636 arg = next_const_call_expr_arg (&iter);
6637 argtype = TREE_TYPE (arg);
6639 if (SCALAR_FLOAT_TYPE_P (parmtype))
6641 if (! SCALAR_FLOAT_TYPE_P (argtype))
6642 return END_BUILTINS;
6644 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6646 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6647 return END_BUILTINS;
6649 else if (POINTER_TYPE_P (parmtype))
6651 if (! POINTER_TYPE_P (argtype))
6652 return END_BUILTINS;
6654 else if (INTEGRAL_TYPE_P (parmtype))
6656 if (! INTEGRAL_TYPE_P (argtype))
6657 return END_BUILTINS;
6659 else
6660 return END_BUILTINS;
6663 /* Variable-length argument list. */
6664 return DECL_FUNCTION_CODE (fndecl);
6667 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6668 evaluate to a constant. */
6670 static tree
6671 fold_builtin_constant_p (tree arg)
6673 /* We return 1 for a numeric type that's known to be a constant
6674 value at compile-time or for an aggregate type that's a
6675 literal constant. */
6676 STRIP_NOPS (arg);
6678 /* If we know this is a constant, emit the constant of one. */
6679 if (CONSTANT_CLASS_P (arg)
6680 || (TREE_CODE (arg) == CONSTRUCTOR
6681 && TREE_CONSTANT (arg)))
6682 return integer_one_node;
6683 if (TREE_CODE (arg) == ADDR_EXPR)
6685 tree op = TREE_OPERAND (arg, 0);
6686 if (TREE_CODE (op) == STRING_CST
6687 || (TREE_CODE (op) == ARRAY_REF
6688 && integer_zerop (TREE_OPERAND (op, 1))
6689 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6690 return integer_one_node;
6693 /* If this expression has side effects, show we don't know it to be a
6694 constant. Likewise if it's a pointer or aggregate type since in
6695 those case we only want literals, since those are only optimized
6696 when generating RTL, not later.
6697 And finally, if we are compiling an initializer, not code, we
6698 need to return a definite result now; there's not going to be any
6699 more optimization done. */
6700 if (TREE_SIDE_EFFECTS (arg)
6701 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6702 || POINTER_TYPE_P (TREE_TYPE (arg))
6703 || cfun == 0
6704 || folding_initializer)
6705 return integer_zero_node;
6707 return NULL_TREE;
6710 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6711 return it as a truthvalue. */
6713 static tree
6714 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6716 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6718 fn = built_in_decls[BUILT_IN_EXPECT];
6719 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6720 ret_type = TREE_TYPE (TREE_TYPE (fn));
6721 pred_type = TREE_VALUE (arg_types);
6722 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6724 pred = fold_convert_loc (loc, pred_type, pred);
6725 expected = fold_convert_loc (loc, expected_type, expected);
6726 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6728 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6729 build_int_cst (ret_type, 0));
6732 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6733 NULL_TREE if no simplification is possible. */
6735 static tree
6736 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6738 tree inner, fndecl;
6739 enum tree_code code;
6741 /* If this is a builtin_expect within a builtin_expect keep the
6742 inner one. See through a comparison against a constant. It
6743 might have been added to create a thruthvalue. */
6744 inner = arg0;
6745 if (COMPARISON_CLASS_P (inner)
6746 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6747 inner = TREE_OPERAND (inner, 0);
6749 if (TREE_CODE (inner) == CALL_EXPR
6750 && (fndecl = get_callee_fndecl (inner))
6751 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6752 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6753 return arg0;
6755 /* Distribute the expected value over short-circuiting operators.
6756 See through the cast from truthvalue_type_node to long. */
6757 inner = arg0;
6758 while (TREE_CODE (inner) == NOP_EXPR
6759 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6760 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6761 inner = TREE_OPERAND (inner, 0);
6763 code = TREE_CODE (inner);
6764 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6766 tree op0 = TREE_OPERAND (inner, 0);
6767 tree op1 = TREE_OPERAND (inner, 1);
6769 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6770 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6771 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6773 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6776 /* If the argument isn't invariant then there's nothing else we can do. */
6777 if (!TREE_CONSTANT (arg0))
6778 return NULL_TREE;
6780 /* If we expect that a comparison against the argument will fold to
6781 a constant return the constant. In practice, this means a true
6782 constant or the address of a non-weak symbol. */
6783 inner = arg0;
6784 STRIP_NOPS (inner);
6785 if (TREE_CODE (inner) == ADDR_EXPR)
6789 inner = TREE_OPERAND (inner, 0);
6791 while (TREE_CODE (inner) == COMPONENT_REF
6792 || TREE_CODE (inner) == ARRAY_REF);
6793 if ((TREE_CODE (inner) == VAR_DECL
6794 || TREE_CODE (inner) == FUNCTION_DECL)
6795 && DECL_WEAK (inner))
6796 return NULL_TREE;
6799 /* Otherwise, ARG0 already has the proper type for the return value. */
6800 return arg0;
6803 /* Fold a call to __builtin_classify_type with argument ARG. */
6805 static tree
6806 fold_builtin_classify_type (tree arg)
6808 if (arg == 0)
6809 return build_int_cst (NULL_TREE, no_type_class);
6811 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
6814 /* Fold a call to __builtin_strlen with argument ARG. */
6816 static tree
6817 fold_builtin_strlen (location_t loc, tree type, tree arg)
6819 if (!validate_arg (arg, POINTER_TYPE))
6820 return NULL_TREE;
6821 else
6823 tree len = c_strlen (arg, 0);
6825 if (len)
6826 return fold_convert_loc (loc, type, len);
6828 return NULL_TREE;
6832 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6834 static tree
6835 fold_builtin_inf (location_t loc, tree type, int warn)
6837 REAL_VALUE_TYPE real;
6839 /* __builtin_inff is intended to be usable to define INFINITY on all
6840 targets. If an infinity is not available, INFINITY expands "to a
6841 positive constant of type float that overflows at translation
6842 time", footnote "In this case, using INFINITY will violate the
6843 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6844 Thus we pedwarn to ensure this constraint violation is
6845 diagnosed. */
6846 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6847 pedwarn (loc, 0, "target format does not support infinity");
6849 real_inf (&real);
6850 return build_real (type, real);
6853 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6855 static tree
6856 fold_builtin_nan (tree arg, tree type, int quiet)
6858 REAL_VALUE_TYPE real;
6859 const char *str;
6861 if (!validate_arg (arg, POINTER_TYPE))
6862 return NULL_TREE;
6863 str = c_getstr (arg);
6864 if (!str)
6865 return NULL_TREE;
6867 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6868 return NULL_TREE;
6870 return build_real (type, real);
6873 /* Return true if the floating point expression T has an integer value.
6874 We also allow +Inf, -Inf and NaN to be considered integer values. */
6876 static bool
6877 integer_valued_real_p (tree t)
6879 switch (TREE_CODE (t))
6881 case FLOAT_EXPR:
6882 return true;
6884 case ABS_EXPR:
6885 case SAVE_EXPR:
6886 return integer_valued_real_p (TREE_OPERAND (t, 0));
6888 case COMPOUND_EXPR:
6889 case MODIFY_EXPR:
6890 case BIND_EXPR:
6891 return integer_valued_real_p (TREE_OPERAND (t, 1));
6893 case PLUS_EXPR:
6894 case MINUS_EXPR:
6895 case MULT_EXPR:
6896 case MIN_EXPR:
6897 case MAX_EXPR:
6898 return integer_valued_real_p (TREE_OPERAND (t, 0))
6899 && integer_valued_real_p (TREE_OPERAND (t, 1));
6901 case COND_EXPR:
6902 return integer_valued_real_p (TREE_OPERAND (t, 1))
6903 && integer_valued_real_p (TREE_OPERAND (t, 2));
6905 case REAL_CST:
6906 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6908 case NOP_EXPR:
6910 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6911 if (TREE_CODE (type) == INTEGER_TYPE)
6912 return true;
6913 if (TREE_CODE (type) == REAL_TYPE)
6914 return integer_valued_real_p (TREE_OPERAND (t, 0));
6915 break;
6918 case CALL_EXPR:
6919 switch (builtin_mathfn_code (t))
6921 CASE_FLT_FN (BUILT_IN_CEIL):
6922 CASE_FLT_FN (BUILT_IN_FLOOR):
6923 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6924 CASE_FLT_FN (BUILT_IN_RINT):
6925 CASE_FLT_FN (BUILT_IN_ROUND):
6926 CASE_FLT_FN (BUILT_IN_TRUNC):
6927 return true;
6929 CASE_FLT_FN (BUILT_IN_FMIN):
6930 CASE_FLT_FN (BUILT_IN_FMAX):
6931 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6932 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6934 default:
6935 break;
6937 break;
6939 default:
6940 break;
6942 return false;
6945 /* FNDECL is assumed to be a builtin where truncation can be propagated
6946 across (for instance floor((double)f) == (double)floorf (f).
6947 Do the transformation for a call with argument ARG. */
6949 static tree
6950 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6952 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6954 if (!validate_arg (arg, REAL_TYPE))
6955 return NULL_TREE;
6957 /* Integer rounding functions are idempotent. */
6958 if (fcode == builtin_mathfn_code (arg))
6959 return arg;
6961 /* If argument is already integer valued, and we don't need to worry
6962 about setting errno, there's no need to perform rounding. */
6963 if (! flag_errno_math && integer_valued_real_p (arg))
6964 return arg;
6966 if (optimize)
6968 tree arg0 = strip_float_extensions (arg);
6969 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6970 tree newtype = TREE_TYPE (arg0);
6971 tree decl;
6973 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6974 && (decl = mathfn_built_in (newtype, fcode)))
6975 return fold_convert_loc (loc, ftype,
6976 build_call_expr_loc (loc, decl, 1,
6977 fold_convert_loc (loc,
6978 newtype,
6979 arg0)));
6981 return NULL_TREE;
6984 /* FNDECL is assumed to be builtin which can narrow the FP type of
6985 the argument, for instance lround((double)f) -> lroundf (f).
6986 Do the transformation for a call with argument ARG. */
6988 static tree
6989 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
6991 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6993 if (!validate_arg (arg, REAL_TYPE))
6994 return NULL_TREE;
6996 /* If argument is already integer valued, and we don't need to worry
6997 about setting errno, there's no need to perform rounding. */
6998 if (! flag_errno_math && integer_valued_real_p (arg))
6999 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7000 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7002 if (optimize)
7004 tree ftype = TREE_TYPE (arg);
7005 tree arg0 = strip_float_extensions (arg);
7006 tree newtype = TREE_TYPE (arg0);
7007 tree decl;
7009 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7010 && (decl = mathfn_built_in (newtype, fcode)))
7011 return build_call_expr_loc (loc, decl, 1,
7012 fold_convert_loc (loc, newtype, arg0));
7015 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7016 sizeof (long long) == sizeof (long). */
7017 if (TYPE_PRECISION (long_long_integer_type_node)
7018 == TYPE_PRECISION (long_integer_type_node))
7020 tree newfn = NULL_TREE;
7021 switch (fcode)
7023 CASE_FLT_FN (BUILT_IN_LLCEIL):
7024 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7025 break;
7027 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7028 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7029 break;
7031 CASE_FLT_FN (BUILT_IN_LLROUND):
7032 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7033 break;
7035 CASE_FLT_FN (BUILT_IN_LLRINT):
7036 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7037 break;
7039 default:
7040 break;
7043 if (newfn)
7045 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7046 return fold_convert_loc (loc,
7047 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7051 return NULL_TREE;
7054 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7055 return type. Return NULL_TREE if no simplification can be made. */
7057 static tree
7058 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7060 tree res;
7062 if (!validate_arg (arg, COMPLEX_TYPE)
7063 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7064 return NULL_TREE;
7066 /* Calculate the result when the argument is a constant. */
7067 if (TREE_CODE (arg) == COMPLEX_CST
7068 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7069 type, mpfr_hypot)))
7070 return res;
7072 if (TREE_CODE (arg) == COMPLEX_EXPR)
7074 tree real = TREE_OPERAND (arg, 0);
7075 tree imag = TREE_OPERAND (arg, 1);
7077 /* If either part is zero, cabs is fabs of the other. */
7078 if (real_zerop (real))
7079 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7080 if (real_zerop (imag))
7081 return fold_build1_loc (loc, ABS_EXPR, type, real);
7083 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7084 if (flag_unsafe_math_optimizations
7085 && operand_equal_p (real, imag, OEP_PURE_SAME))
7087 const REAL_VALUE_TYPE sqrt2_trunc
7088 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7089 STRIP_NOPS (real);
7090 return fold_build2_loc (loc, MULT_EXPR, type,
7091 fold_build1_loc (loc, ABS_EXPR, type, real),
7092 build_real (type, sqrt2_trunc));
7096 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7097 if (TREE_CODE (arg) == NEGATE_EXPR
7098 || TREE_CODE (arg) == CONJ_EXPR)
7099 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7101 /* Don't do this when optimizing for size. */
7102 if (flag_unsafe_math_optimizations
7103 && optimize && optimize_function_for_speed_p (cfun))
7105 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7107 if (sqrtfn != NULL_TREE)
7109 tree rpart, ipart, result;
7111 arg = builtin_save_expr (arg);
7113 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7114 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7116 rpart = builtin_save_expr (rpart);
7117 ipart = builtin_save_expr (ipart);
7119 result = fold_build2_loc (loc, PLUS_EXPR, type,
7120 fold_build2_loc (loc, MULT_EXPR, type,
7121 rpart, rpart),
7122 fold_build2_loc (loc, MULT_EXPR, type,
7123 ipart, ipart));
7125 return build_call_expr_loc (loc, sqrtfn, 1, result);
7129 return NULL_TREE;
7132 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7133 complex tree type of the result. If NEG is true, the imaginary
7134 zero is negative. */
7136 static tree
7137 build_complex_cproj (tree type, bool neg)
7139 REAL_VALUE_TYPE rinf, rzero = dconst0;
7141 real_inf (&rinf);
7142 rzero.sign = neg;
7143 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7144 build_real (TREE_TYPE (type), rzero));
7147 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7148 return type. Return NULL_TREE if no simplification can be made. */
7150 static tree
7151 fold_builtin_cproj (location_t loc, tree arg, tree type)
7153 if (!validate_arg (arg, COMPLEX_TYPE)
7154 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7155 return NULL_TREE;
7157 /* If there are no infinities, return arg. */
7158 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7159 return non_lvalue_loc (loc, arg);
7161 /* Calculate the result when the argument is a constant. */
7162 if (TREE_CODE (arg) == COMPLEX_CST)
7164 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7165 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7167 if (real_isinf (real) || real_isinf (imag))
7168 return build_complex_cproj (type, imag->sign);
7169 else
7170 return arg;
7172 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7174 tree real = TREE_OPERAND (arg, 0);
7175 tree imag = TREE_OPERAND (arg, 1);
7177 STRIP_NOPS (real);
7178 STRIP_NOPS (imag);
7180 /* If the real part is inf and the imag part is known to be
7181 nonnegative, return (inf + 0i). Remember side-effects are
7182 possible in the imag part. */
7183 if (TREE_CODE (real) == REAL_CST
7184 && real_isinf (TREE_REAL_CST_PTR (real))
7185 && tree_expr_nonnegative_p (imag))
7186 return omit_one_operand_loc (loc, type,
7187 build_complex_cproj (type, false),
7188 arg);
7190 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7191 Remember side-effects are possible in the real part. */
7192 if (TREE_CODE (imag) == REAL_CST
7193 && real_isinf (TREE_REAL_CST_PTR (imag)))
7194 return
7195 omit_one_operand_loc (loc, type,
7196 build_complex_cproj (type, TREE_REAL_CST_PTR
7197 (imag)->sign), arg);
7200 return NULL_TREE;
7203 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7204 Return NULL_TREE if no simplification can be made. */
7206 static tree
7207 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7210 enum built_in_function fcode;
7211 tree res;
7213 if (!validate_arg (arg, REAL_TYPE))
7214 return NULL_TREE;
7216 /* Calculate the result when the argument is a constant. */
7217 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7218 return res;
7220 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7221 fcode = builtin_mathfn_code (arg);
7222 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7224 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7225 arg = fold_build2_loc (loc, MULT_EXPR, type,
7226 CALL_EXPR_ARG (arg, 0),
7227 build_real (type, dconsthalf));
7228 return build_call_expr_loc (loc, expfn, 1, arg);
7231 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7232 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7234 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7236 if (powfn)
7238 tree arg0 = CALL_EXPR_ARG (arg, 0);
7239 tree tree_root;
7240 /* The inner root was either sqrt or cbrt. */
7241 /* This was a conditional expression but it triggered a bug
7242 in Sun C 5.5. */
7243 REAL_VALUE_TYPE dconstroot;
7244 if (BUILTIN_SQRT_P (fcode))
7245 dconstroot = dconsthalf;
7246 else
7247 dconstroot = dconst_third ();
7249 /* Adjust for the outer root. */
7250 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7251 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7252 tree_root = build_real (type, dconstroot);
7253 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7257 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7258 if (flag_unsafe_math_optimizations
7259 && (fcode == BUILT_IN_POW
7260 || fcode == BUILT_IN_POWF
7261 || fcode == BUILT_IN_POWL))
7263 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7264 tree arg0 = CALL_EXPR_ARG (arg, 0);
7265 tree arg1 = CALL_EXPR_ARG (arg, 1);
7266 tree narg1;
7267 if (!tree_expr_nonnegative_p (arg0))
7268 arg0 = build1 (ABS_EXPR, type, arg0);
7269 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7270 build_real (type, dconsthalf));
7271 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7274 return NULL_TREE;
7277 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7278 Return NULL_TREE if no simplification can be made. */
7280 static tree
7281 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7283 const enum built_in_function fcode = builtin_mathfn_code (arg);
7284 tree res;
7286 if (!validate_arg (arg, REAL_TYPE))
7287 return NULL_TREE;
7289 /* Calculate the result when the argument is a constant. */
7290 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7291 return res;
7293 if (flag_unsafe_math_optimizations)
7295 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7296 if (BUILTIN_EXPONENT_P (fcode))
7298 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7299 const REAL_VALUE_TYPE third_trunc =
7300 real_value_truncate (TYPE_MODE (type), dconst_third ());
7301 arg = fold_build2_loc (loc, MULT_EXPR, type,
7302 CALL_EXPR_ARG (arg, 0),
7303 build_real (type, third_trunc));
7304 return build_call_expr_loc (loc, expfn, 1, arg);
7307 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7308 if (BUILTIN_SQRT_P (fcode))
7310 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7312 if (powfn)
7314 tree arg0 = CALL_EXPR_ARG (arg, 0);
7315 tree tree_root;
7316 REAL_VALUE_TYPE dconstroot = dconst_third ();
7318 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7319 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7320 tree_root = build_real (type, dconstroot);
7321 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7325 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7326 if (BUILTIN_CBRT_P (fcode))
7328 tree arg0 = CALL_EXPR_ARG (arg, 0);
7329 if (tree_expr_nonnegative_p (arg0))
7331 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7333 if (powfn)
7335 tree tree_root;
7336 REAL_VALUE_TYPE dconstroot;
7338 real_arithmetic (&dconstroot, MULT_EXPR,
7339 dconst_third_ptr (), dconst_third_ptr ());
7340 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7341 tree_root = build_real (type, dconstroot);
7342 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7347 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7348 if (fcode == BUILT_IN_POW
7349 || fcode == BUILT_IN_POWF
7350 || fcode == BUILT_IN_POWL)
7352 tree arg00 = CALL_EXPR_ARG (arg, 0);
7353 tree arg01 = CALL_EXPR_ARG (arg, 1);
7354 if (tree_expr_nonnegative_p (arg00))
7356 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7357 const REAL_VALUE_TYPE dconstroot
7358 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7359 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7360 build_real (type, dconstroot));
7361 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7365 return NULL_TREE;
7368 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7369 TYPE is the type of the return value. Return NULL_TREE if no
7370 simplification can be made. */
7372 static tree
7373 fold_builtin_cos (location_t loc,
7374 tree arg, tree type, tree fndecl)
7376 tree res, narg;
7378 if (!validate_arg (arg, REAL_TYPE))
7379 return NULL_TREE;
7381 /* Calculate the result when the argument is a constant. */
7382 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7383 return res;
7385 /* Optimize cos(-x) into cos (x). */
7386 if ((narg = fold_strip_sign_ops (arg)))
7387 return build_call_expr_loc (loc, fndecl, 1, narg);
7389 return NULL_TREE;
7392 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7393 Return NULL_TREE if no simplification can be made. */
7395 static tree
7396 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7398 if (validate_arg (arg, REAL_TYPE))
7400 tree res, narg;
7402 /* Calculate the result when the argument is a constant. */
7403 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7404 return res;
7406 /* Optimize cosh(-x) into cosh (x). */
7407 if ((narg = fold_strip_sign_ops (arg)))
7408 return build_call_expr_loc (loc, fndecl, 1, narg);
7411 return NULL_TREE;
7414 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7415 argument ARG. TYPE is the type of the return value. Return
7416 NULL_TREE if no simplification can be made. */
7418 static tree
7419 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7420 bool hyper)
7422 if (validate_arg (arg, COMPLEX_TYPE)
7423 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7425 tree tmp;
7427 /* Calculate the result when the argument is a constant. */
7428 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7429 return tmp;
7431 /* Optimize fn(-x) into fn(x). */
7432 if ((tmp = fold_strip_sign_ops (arg)))
7433 return build_call_expr_loc (loc, fndecl, 1, tmp);
7436 return NULL_TREE;
7439 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7440 Return NULL_TREE if no simplification can be made. */
7442 static tree
7443 fold_builtin_tan (tree arg, tree type)
7445 enum built_in_function fcode;
7446 tree res;
7448 if (!validate_arg (arg, REAL_TYPE))
7449 return NULL_TREE;
7451 /* Calculate the result when the argument is a constant. */
7452 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7453 return res;
7455 /* Optimize tan(atan(x)) = x. */
7456 fcode = builtin_mathfn_code (arg);
7457 if (flag_unsafe_math_optimizations
7458 && (fcode == BUILT_IN_ATAN
7459 || fcode == BUILT_IN_ATANF
7460 || fcode == BUILT_IN_ATANL))
7461 return CALL_EXPR_ARG (arg, 0);
7463 return NULL_TREE;
7466 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7467 NULL_TREE if no simplification can be made. */
7469 static tree
7470 fold_builtin_sincos (location_t loc,
7471 tree arg0, tree arg1, tree arg2)
7473 tree type;
7474 tree res, fn, call;
7476 if (!validate_arg (arg0, REAL_TYPE)
7477 || !validate_arg (arg1, POINTER_TYPE)
7478 || !validate_arg (arg2, POINTER_TYPE))
7479 return NULL_TREE;
7481 type = TREE_TYPE (arg0);
7483 /* Calculate the result when the argument is a constant. */
7484 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7485 return res;
7487 /* Canonicalize sincos to cexpi. */
7488 if (!TARGET_C99_FUNCTIONS)
7489 return NULL_TREE;
7490 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7491 if (!fn)
7492 return NULL_TREE;
7494 call = build_call_expr_loc (loc, fn, 1, arg0);
7495 call = builtin_save_expr (call);
7497 return build2 (COMPOUND_EXPR, void_type_node,
7498 build2 (MODIFY_EXPR, void_type_node,
7499 build_fold_indirect_ref_loc (loc, arg1),
7500 build1 (IMAGPART_EXPR, type, call)),
7501 build2 (MODIFY_EXPR, void_type_node,
7502 build_fold_indirect_ref_loc (loc, arg2),
7503 build1 (REALPART_EXPR, type, call)));
7506 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7507 NULL_TREE if no simplification can be made. */
7509 static tree
7510 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7512 tree rtype;
7513 tree realp, imagp, ifn;
7514 tree res;
7516 if (!validate_arg (arg0, COMPLEX_TYPE)
7517 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7518 return NULL_TREE;
7520 /* Calculate the result when the argument is a constant. */
7521 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7522 return res;
7524 rtype = TREE_TYPE (TREE_TYPE (arg0));
7526 /* In case we can figure out the real part of arg0 and it is constant zero
7527 fold to cexpi. */
7528 if (!TARGET_C99_FUNCTIONS)
7529 return NULL_TREE;
7530 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7531 if (!ifn)
7532 return NULL_TREE;
7534 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7535 && real_zerop (realp))
7537 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7538 return build_call_expr_loc (loc, ifn, 1, narg);
7541 /* In case we can easily decompose real and imaginary parts split cexp
7542 to exp (r) * cexpi (i). */
7543 if (flag_unsafe_math_optimizations
7544 && realp)
7546 tree rfn, rcall, icall;
7548 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7549 if (!rfn)
7550 return NULL_TREE;
7552 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7553 if (!imagp)
7554 return NULL_TREE;
7556 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7557 icall = builtin_save_expr (icall);
7558 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7559 rcall = builtin_save_expr (rcall);
7560 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7561 fold_build2_loc (loc, MULT_EXPR, rtype,
7562 rcall,
7563 fold_build1_loc (loc, REALPART_EXPR,
7564 rtype, icall)),
7565 fold_build2_loc (loc, MULT_EXPR, rtype,
7566 rcall,
7567 fold_build1_loc (loc, IMAGPART_EXPR,
7568 rtype, icall)));
7571 return NULL_TREE;
7574 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7575 Return NULL_TREE if no simplification can be made. */
7577 static tree
7578 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7580 if (!validate_arg (arg, REAL_TYPE))
7581 return NULL_TREE;
7583 /* Optimize trunc of constant value. */
7584 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7586 REAL_VALUE_TYPE r, x;
7587 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7589 x = TREE_REAL_CST (arg);
7590 real_trunc (&r, TYPE_MODE (type), &x);
7591 return build_real (type, r);
7594 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7597 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7598 Return NULL_TREE if no simplification can be made. */
7600 static tree
7601 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7603 if (!validate_arg (arg, REAL_TYPE))
7604 return NULL_TREE;
7606 /* Optimize floor of constant value. */
7607 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7609 REAL_VALUE_TYPE x;
7611 x = TREE_REAL_CST (arg);
7612 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7614 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7615 REAL_VALUE_TYPE r;
7617 real_floor (&r, TYPE_MODE (type), &x);
7618 return build_real (type, r);
7622 /* Fold floor (x) where x is nonnegative to trunc (x). */
7623 if (tree_expr_nonnegative_p (arg))
7625 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7626 if (truncfn)
7627 return build_call_expr_loc (loc, truncfn, 1, arg);
7630 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7633 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7634 Return NULL_TREE if no simplification can be made. */
7636 static tree
7637 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7639 if (!validate_arg (arg, REAL_TYPE))
7640 return NULL_TREE;
7642 /* Optimize ceil of constant value. */
7643 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7645 REAL_VALUE_TYPE x;
7647 x = TREE_REAL_CST (arg);
7648 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7650 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7651 REAL_VALUE_TYPE r;
7653 real_ceil (&r, TYPE_MODE (type), &x);
7654 return build_real (type, r);
7658 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7661 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7662 Return NULL_TREE if no simplification can be made. */
7664 static tree
7665 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7667 if (!validate_arg (arg, REAL_TYPE))
7668 return NULL_TREE;
7670 /* Optimize round of constant value. */
7671 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7673 REAL_VALUE_TYPE x;
7675 x = TREE_REAL_CST (arg);
7676 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7678 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7679 REAL_VALUE_TYPE r;
7681 real_round (&r, TYPE_MODE (type), &x);
7682 return build_real (type, r);
7686 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7689 /* Fold function call to builtin lround, lroundf or lroundl (or the
7690 corresponding long long versions) and other rounding functions. ARG
7691 is the argument to the call. Return NULL_TREE if no simplification
7692 can be made. */
7694 static tree
7695 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7697 if (!validate_arg (arg, REAL_TYPE))
7698 return NULL_TREE;
7700 /* Optimize lround of constant value. */
7701 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7703 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7705 if (real_isfinite (&x))
7707 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7708 tree ftype = TREE_TYPE (arg);
7709 double_int val;
7710 REAL_VALUE_TYPE r;
7712 switch (DECL_FUNCTION_CODE (fndecl))
7714 CASE_FLT_FN (BUILT_IN_LFLOOR):
7715 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7716 real_floor (&r, TYPE_MODE (ftype), &x);
7717 break;
7719 CASE_FLT_FN (BUILT_IN_LCEIL):
7720 CASE_FLT_FN (BUILT_IN_LLCEIL):
7721 real_ceil (&r, TYPE_MODE (ftype), &x);
7722 break;
7724 CASE_FLT_FN (BUILT_IN_LROUND):
7725 CASE_FLT_FN (BUILT_IN_LLROUND):
7726 real_round (&r, TYPE_MODE (ftype), &x);
7727 break;
7729 default:
7730 gcc_unreachable ();
7733 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7734 if (double_int_fits_to_tree_p (itype, val))
7735 return double_int_to_tree (itype, val);
7739 switch (DECL_FUNCTION_CODE (fndecl))
7741 CASE_FLT_FN (BUILT_IN_LFLOOR):
7742 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7743 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7744 if (tree_expr_nonnegative_p (arg))
7745 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7746 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7747 break;
7748 default:;
7751 return fold_fixed_mathfn (loc, fndecl, arg);
7754 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7755 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7756 the argument to the call. Return NULL_TREE if no simplification can
7757 be made. */
7759 static tree
7760 fold_builtin_bitop (tree fndecl, tree arg)
7762 if (!validate_arg (arg, INTEGER_TYPE))
7763 return NULL_TREE;
7765 /* Optimize for constant argument. */
7766 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7768 HOST_WIDE_INT hi, width, result;
7769 unsigned HOST_WIDE_INT lo;
7770 tree type;
7772 type = TREE_TYPE (arg);
7773 width = TYPE_PRECISION (type);
7774 lo = TREE_INT_CST_LOW (arg);
7776 /* Clear all the bits that are beyond the type's precision. */
7777 if (width > HOST_BITS_PER_WIDE_INT)
7779 hi = TREE_INT_CST_HIGH (arg);
7780 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7781 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7783 else
7785 hi = 0;
7786 if (width < HOST_BITS_PER_WIDE_INT)
7787 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7790 switch (DECL_FUNCTION_CODE (fndecl))
7792 CASE_INT_FN (BUILT_IN_FFS):
7793 if (lo != 0)
7794 result = ffs_hwi (lo);
7795 else if (hi != 0)
7796 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
7797 else
7798 result = 0;
7799 break;
7801 CASE_INT_FN (BUILT_IN_CLZ):
7802 if (hi != 0)
7803 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7804 else if (lo != 0)
7805 result = width - floor_log2 (lo) - 1;
7806 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7807 result = width;
7808 break;
7810 CASE_INT_FN (BUILT_IN_CTZ):
7811 if (lo != 0)
7812 result = ctz_hwi (lo);
7813 else if (hi != 0)
7814 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
7815 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7816 result = width;
7817 break;
7819 CASE_INT_FN (BUILT_IN_POPCOUNT):
7820 result = 0;
7821 while (lo)
7822 result++, lo &= lo - 1;
7823 while (hi)
7824 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7825 break;
7827 CASE_INT_FN (BUILT_IN_PARITY):
7828 result = 0;
7829 while (lo)
7830 result++, lo &= lo - 1;
7831 while (hi)
7832 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7833 result &= 1;
7834 break;
7836 default:
7837 gcc_unreachable ();
7840 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7843 return NULL_TREE;
7846 /* Fold function call to builtin_bswap and the long and long long
7847 variants. Return NULL_TREE if no simplification can be made. */
7848 static tree
7849 fold_builtin_bswap (tree fndecl, tree arg)
7851 if (! validate_arg (arg, INTEGER_TYPE))
7852 return NULL_TREE;
7854 /* Optimize constant value. */
7855 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7857 HOST_WIDE_INT hi, width, r_hi = 0;
7858 unsigned HOST_WIDE_INT lo, r_lo = 0;
7859 tree type;
7861 type = TREE_TYPE (arg);
7862 width = TYPE_PRECISION (type);
7863 lo = TREE_INT_CST_LOW (arg);
7864 hi = TREE_INT_CST_HIGH (arg);
7866 switch (DECL_FUNCTION_CODE (fndecl))
7868 case BUILT_IN_BSWAP32:
7869 case BUILT_IN_BSWAP64:
7871 int s;
7873 for (s = 0; s < width; s += 8)
7875 int d = width - s - 8;
7876 unsigned HOST_WIDE_INT byte;
7878 if (s < HOST_BITS_PER_WIDE_INT)
7879 byte = (lo >> s) & 0xff;
7880 else
7881 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7883 if (d < HOST_BITS_PER_WIDE_INT)
7884 r_lo |= byte << d;
7885 else
7886 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7890 break;
7892 default:
7893 gcc_unreachable ();
7896 if (width < HOST_BITS_PER_WIDE_INT)
7897 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7898 else
7899 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7902 return NULL_TREE;
7905 /* A subroutine of fold_builtin to fold the various logarithmic
7906 functions. Return NULL_TREE if no simplification can me made.
7907 FUNC is the corresponding MPFR logarithm function. */
7909 static tree
7910 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7911 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7913 if (validate_arg (arg, REAL_TYPE))
7915 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7916 tree res;
7917 const enum built_in_function fcode = builtin_mathfn_code (arg);
7919 /* Calculate the result when the argument is a constant. */
7920 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7921 return res;
7923 /* Special case, optimize logN(expN(x)) = x. */
7924 if (flag_unsafe_math_optimizations
7925 && ((func == mpfr_log
7926 && (fcode == BUILT_IN_EXP
7927 || fcode == BUILT_IN_EXPF
7928 || fcode == BUILT_IN_EXPL))
7929 || (func == mpfr_log2
7930 && (fcode == BUILT_IN_EXP2
7931 || fcode == BUILT_IN_EXP2F
7932 || fcode == BUILT_IN_EXP2L))
7933 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7934 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7936 /* Optimize logN(func()) for various exponential functions. We
7937 want to determine the value "x" and the power "exponent" in
7938 order to transform logN(x**exponent) into exponent*logN(x). */
7939 if (flag_unsafe_math_optimizations)
7941 tree exponent = 0, x = 0;
7943 switch (fcode)
7945 CASE_FLT_FN (BUILT_IN_EXP):
7946 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7947 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7948 dconst_e ()));
7949 exponent = CALL_EXPR_ARG (arg, 0);
7950 break;
7951 CASE_FLT_FN (BUILT_IN_EXP2):
7952 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7953 x = build_real (type, dconst2);
7954 exponent = CALL_EXPR_ARG (arg, 0);
7955 break;
7956 CASE_FLT_FN (BUILT_IN_EXP10):
7957 CASE_FLT_FN (BUILT_IN_POW10):
7958 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7960 REAL_VALUE_TYPE dconst10;
7961 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7962 x = build_real (type, dconst10);
7964 exponent = CALL_EXPR_ARG (arg, 0);
7965 break;
7966 CASE_FLT_FN (BUILT_IN_SQRT):
7967 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7968 x = CALL_EXPR_ARG (arg, 0);
7969 exponent = build_real (type, dconsthalf);
7970 break;
7971 CASE_FLT_FN (BUILT_IN_CBRT):
7972 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7973 x = CALL_EXPR_ARG (arg, 0);
7974 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7975 dconst_third ()));
7976 break;
7977 CASE_FLT_FN (BUILT_IN_POW):
7978 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7979 x = CALL_EXPR_ARG (arg, 0);
7980 exponent = CALL_EXPR_ARG (arg, 1);
7981 break;
7982 default:
7983 break;
7986 /* Now perform the optimization. */
7987 if (x && exponent)
7989 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
7990 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
7995 return NULL_TREE;
7998 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7999 NULL_TREE if no simplification can be made. */
8001 static tree
8002 fold_builtin_hypot (location_t loc, tree fndecl,
8003 tree arg0, tree arg1, tree type)
8005 tree res, narg0, narg1;
8007 if (!validate_arg (arg0, REAL_TYPE)
8008 || !validate_arg (arg1, REAL_TYPE))
8009 return NULL_TREE;
8011 /* Calculate the result when the argument is a constant. */
8012 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8013 return res;
8015 /* If either argument to hypot has a negate or abs, strip that off.
8016 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8017 narg0 = fold_strip_sign_ops (arg0);
8018 narg1 = fold_strip_sign_ops (arg1);
8019 if (narg0 || narg1)
8021 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8022 narg1 ? narg1 : arg1);
8025 /* If either argument is zero, hypot is fabs of the other. */
8026 if (real_zerop (arg0))
8027 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8028 else if (real_zerop (arg1))
8029 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8031 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8032 if (flag_unsafe_math_optimizations
8033 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8035 const REAL_VALUE_TYPE sqrt2_trunc
8036 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8037 return fold_build2_loc (loc, MULT_EXPR, type,
8038 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8039 build_real (type, sqrt2_trunc));
8042 return NULL_TREE;
8046 /* Fold a builtin function call to pow, powf, or powl. Return
8047 NULL_TREE if no simplification can be made. */
8048 static tree
8049 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8051 tree res;
8053 if (!validate_arg (arg0, REAL_TYPE)
8054 || !validate_arg (arg1, REAL_TYPE))
8055 return NULL_TREE;
8057 /* Calculate the result when the argument is a constant. */
8058 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8059 return res;
8061 /* Optimize pow(1.0,y) = 1.0. */
8062 if (real_onep (arg0))
8063 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8065 if (TREE_CODE (arg1) == REAL_CST
8066 && !TREE_OVERFLOW (arg1))
8068 REAL_VALUE_TYPE cint;
8069 REAL_VALUE_TYPE c;
8070 HOST_WIDE_INT n;
8072 c = TREE_REAL_CST (arg1);
8074 /* Optimize pow(x,0.0) = 1.0. */
8075 if (REAL_VALUES_EQUAL (c, dconst0))
8076 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8077 arg0);
8079 /* Optimize pow(x,1.0) = x. */
8080 if (REAL_VALUES_EQUAL (c, dconst1))
8081 return arg0;
8083 /* Optimize pow(x,-1.0) = 1.0/x. */
8084 if (REAL_VALUES_EQUAL (c, dconstm1))
8085 return fold_build2_loc (loc, RDIV_EXPR, type,
8086 build_real (type, dconst1), arg0);
8088 /* Optimize pow(x,0.5) = sqrt(x). */
8089 if (flag_unsafe_math_optimizations
8090 && REAL_VALUES_EQUAL (c, dconsthalf))
8092 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8094 if (sqrtfn != NULL_TREE)
8095 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8098 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8099 if (flag_unsafe_math_optimizations)
8101 const REAL_VALUE_TYPE dconstroot
8102 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8104 if (REAL_VALUES_EQUAL (c, dconstroot))
8106 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8107 if (cbrtfn != NULL_TREE)
8108 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8112 /* Check for an integer exponent. */
8113 n = real_to_integer (&c);
8114 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8115 if (real_identical (&c, &cint))
8117 /* Attempt to evaluate pow at compile-time, unless this should
8118 raise an exception. */
8119 if (TREE_CODE (arg0) == REAL_CST
8120 && !TREE_OVERFLOW (arg0)
8121 && (n > 0
8122 || (!flag_trapping_math && !flag_errno_math)
8123 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8125 REAL_VALUE_TYPE x;
8126 bool inexact;
8128 x = TREE_REAL_CST (arg0);
8129 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8130 if (flag_unsafe_math_optimizations || !inexact)
8131 return build_real (type, x);
8134 /* Strip sign ops from even integer powers. */
8135 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8137 tree narg0 = fold_strip_sign_ops (arg0);
8138 if (narg0)
8139 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8144 if (flag_unsafe_math_optimizations)
8146 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8148 /* Optimize pow(expN(x),y) = expN(x*y). */
8149 if (BUILTIN_EXPONENT_P (fcode))
8151 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8152 tree arg = CALL_EXPR_ARG (arg0, 0);
8153 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8154 return build_call_expr_loc (loc, expfn, 1, arg);
8157 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8158 if (BUILTIN_SQRT_P (fcode))
8160 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8161 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8162 build_real (type, dconsthalf));
8163 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8166 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8167 if (BUILTIN_CBRT_P (fcode))
8169 tree arg = CALL_EXPR_ARG (arg0, 0);
8170 if (tree_expr_nonnegative_p (arg))
8172 const REAL_VALUE_TYPE dconstroot
8173 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8174 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8175 build_real (type, dconstroot));
8176 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8180 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8181 if (fcode == BUILT_IN_POW
8182 || fcode == BUILT_IN_POWF
8183 || fcode == BUILT_IN_POWL)
8185 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8186 if (tree_expr_nonnegative_p (arg00))
8188 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8189 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8190 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8195 return NULL_TREE;
8198 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8199 Return NULL_TREE if no simplification can be made. */
8200 static tree
8201 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8202 tree arg0, tree arg1, tree type)
8204 if (!validate_arg (arg0, REAL_TYPE)
8205 || !validate_arg (arg1, INTEGER_TYPE))
8206 return NULL_TREE;
8208 /* Optimize pow(1.0,y) = 1.0. */
8209 if (real_onep (arg0))
8210 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8212 if (host_integerp (arg1, 0))
8214 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8216 /* Evaluate powi at compile-time. */
8217 if (TREE_CODE (arg0) == REAL_CST
8218 && !TREE_OVERFLOW (arg0))
8220 REAL_VALUE_TYPE x;
8221 x = TREE_REAL_CST (arg0);
8222 real_powi (&x, TYPE_MODE (type), &x, c);
8223 return build_real (type, x);
8226 /* Optimize pow(x,0) = 1.0. */
8227 if (c == 0)
8228 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8229 arg0);
8231 /* Optimize pow(x,1) = x. */
8232 if (c == 1)
8233 return arg0;
8235 /* Optimize pow(x,-1) = 1.0/x. */
8236 if (c == -1)
8237 return fold_build2_loc (loc, RDIV_EXPR, type,
8238 build_real (type, dconst1), arg0);
8241 return NULL_TREE;
8244 /* A subroutine of fold_builtin to fold the various exponent
8245 functions. Return NULL_TREE if no simplification can be made.
8246 FUNC is the corresponding MPFR exponent function. */
8248 static tree
8249 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8250 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8252 if (validate_arg (arg, REAL_TYPE))
8254 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8255 tree res;
8257 /* Calculate the result when the argument is a constant. */
8258 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8259 return res;
8261 /* Optimize expN(logN(x)) = x. */
8262 if (flag_unsafe_math_optimizations)
8264 const enum built_in_function fcode = builtin_mathfn_code (arg);
8266 if ((func == mpfr_exp
8267 && (fcode == BUILT_IN_LOG
8268 || fcode == BUILT_IN_LOGF
8269 || fcode == BUILT_IN_LOGL))
8270 || (func == mpfr_exp2
8271 && (fcode == BUILT_IN_LOG2
8272 || fcode == BUILT_IN_LOG2F
8273 || fcode == BUILT_IN_LOG2L))
8274 || (func == mpfr_exp10
8275 && (fcode == BUILT_IN_LOG10
8276 || fcode == BUILT_IN_LOG10F
8277 || fcode == BUILT_IN_LOG10L)))
8278 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8282 return NULL_TREE;
8285 /* Return true if VAR is a VAR_DECL or a component thereof. */
8287 static bool
8288 var_decl_component_p (tree var)
8290 tree inner = var;
8291 while (handled_component_p (inner))
8292 inner = TREE_OPERAND (inner, 0);
8293 return SSA_VAR_P (inner);
8296 /* Fold function call to builtin memset. Return
8297 NULL_TREE if no simplification can be made. */
8299 static tree
8300 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8301 tree type, bool ignore)
8303 tree var, ret, etype;
8304 unsigned HOST_WIDE_INT length, cval;
8306 if (! validate_arg (dest, POINTER_TYPE)
8307 || ! validate_arg (c, INTEGER_TYPE)
8308 || ! validate_arg (len, INTEGER_TYPE))
8309 return NULL_TREE;
8311 if (! host_integerp (len, 1))
8312 return NULL_TREE;
8314 /* If the LEN parameter is zero, return DEST. */
8315 if (integer_zerop (len))
8316 return omit_one_operand_loc (loc, type, dest, c);
8318 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8319 return NULL_TREE;
8321 var = dest;
8322 STRIP_NOPS (var);
8323 if (TREE_CODE (var) != ADDR_EXPR)
8324 return NULL_TREE;
8326 var = TREE_OPERAND (var, 0);
8327 if (TREE_THIS_VOLATILE (var))
8328 return NULL_TREE;
8330 etype = TREE_TYPE (var);
8331 if (TREE_CODE (etype) == ARRAY_TYPE)
8332 etype = TREE_TYPE (etype);
8334 if (!INTEGRAL_TYPE_P (etype)
8335 && !POINTER_TYPE_P (etype))
8336 return NULL_TREE;
8338 if (! var_decl_component_p (var))
8339 return NULL_TREE;
8341 length = tree_low_cst (len, 1);
8342 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8343 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8344 < length)
8345 return NULL_TREE;
8347 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8348 return NULL_TREE;
8350 if (integer_zerop (c))
8351 cval = 0;
8352 else
8354 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8355 return NULL_TREE;
8357 cval = TREE_INT_CST_LOW (c);
8358 cval &= 0xff;
8359 cval |= cval << 8;
8360 cval |= cval << 16;
8361 cval |= (cval << 31) << 1;
8364 ret = build_int_cst_type (etype, cval);
8365 var = build_fold_indirect_ref_loc (loc,
8366 fold_convert_loc (loc,
8367 build_pointer_type (etype),
8368 dest));
8369 ret = build2 (MODIFY_EXPR, etype, var, ret);
8370 if (ignore)
8371 return ret;
8373 return omit_one_operand_loc (loc, type, dest, ret);
8376 /* Fold function call to builtin memset. Return
8377 NULL_TREE if no simplification can be made. */
8379 static tree
8380 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8382 if (! validate_arg (dest, POINTER_TYPE)
8383 || ! validate_arg (size, INTEGER_TYPE))
8384 return NULL_TREE;
8386 if (!ignore)
8387 return NULL_TREE;
8389 /* New argument list transforming bzero(ptr x, int y) to
8390 memset(ptr x, int 0, size_t y). This is done this way
8391 so that if it isn't expanded inline, we fallback to
8392 calling bzero instead of memset. */
8394 return fold_builtin_memset (loc, dest, integer_zero_node,
8395 fold_convert_loc (loc, sizetype, size),
8396 void_type_node, ignore);
8399 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8400 NULL_TREE if no simplification can be made.
8401 If ENDP is 0, return DEST (like memcpy).
8402 If ENDP is 1, return DEST+LEN (like mempcpy).
8403 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8404 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8405 (memmove). */
8407 static tree
8408 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8409 tree len, tree type, bool ignore, int endp)
8411 tree destvar, srcvar, expr;
8413 if (! validate_arg (dest, POINTER_TYPE)
8414 || ! validate_arg (src, POINTER_TYPE)
8415 || ! validate_arg (len, INTEGER_TYPE))
8416 return NULL_TREE;
8418 /* If the LEN parameter is zero, return DEST. */
8419 if (integer_zerop (len))
8420 return omit_one_operand_loc (loc, type, dest, src);
8422 /* If SRC and DEST are the same (and not volatile), return
8423 DEST{,+LEN,+LEN-1}. */
8424 if (operand_equal_p (src, dest, 0))
8425 expr = len;
8426 else
8428 tree srctype, desttype;
8429 unsigned int src_align, dest_align;
8430 tree off0;
8432 if (endp == 3)
8434 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8435 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8437 /* Both DEST and SRC must be pointer types.
8438 ??? This is what old code did. Is the testing for pointer types
8439 really mandatory?
8441 If either SRC is readonly or length is 1, we can use memcpy. */
8442 if (!dest_align || !src_align)
8443 return NULL_TREE;
8444 if (readonly_data_expr (src)
8445 || (host_integerp (len, 1)
8446 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8447 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8449 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8450 if (!fn)
8451 return NULL_TREE;
8452 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8455 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8456 if (TREE_CODE (src) == ADDR_EXPR
8457 && TREE_CODE (dest) == ADDR_EXPR)
8459 tree src_base, dest_base, fn;
8460 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8461 HOST_WIDE_INT size = -1;
8462 HOST_WIDE_INT maxsize = -1;
8464 srcvar = TREE_OPERAND (src, 0);
8465 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8466 &size, &maxsize);
8467 destvar = TREE_OPERAND (dest, 0);
8468 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8469 &size, &maxsize);
8470 if (host_integerp (len, 1))
8471 maxsize = tree_low_cst (len, 1);
8472 else
8473 maxsize = -1;
8474 src_offset /= BITS_PER_UNIT;
8475 dest_offset /= BITS_PER_UNIT;
8476 if (SSA_VAR_P (src_base)
8477 && SSA_VAR_P (dest_base))
8479 if (operand_equal_p (src_base, dest_base, 0)
8480 && ranges_overlap_p (src_offset, maxsize,
8481 dest_offset, maxsize))
8482 return NULL_TREE;
8484 else if (TREE_CODE (src_base) == MEM_REF
8485 && TREE_CODE (dest_base) == MEM_REF)
8487 double_int off;
8488 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8489 TREE_OPERAND (dest_base, 0), 0))
8490 return NULL_TREE;
8491 off = double_int_add (mem_ref_offset (src_base),
8492 shwi_to_double_int (src_offset));
8493 if (!double_int_fits_in_shwi_p (off))
8494 return NULL_TREE;
8495 src_offset = off.low;
8496 off = double_int_add (mem_ref_offset (dest_base),
8497 shwi_to_double_int (dest_offset));
8498 if (!double_int_fits_in_shwi_p (off))
8499 return NULL_TREE;
8500 dest_offset = off.low;
8501 if (ranges_overlap_p (src_offset, maxsize,
8502 dest_offset, maxsize))
8503 return NULL_TREE;
8505 else
8506 return NULL_TREE;
8508 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8509 if (!fn)
8510 return NULL_TREE;
8511 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8514 /* If the destination and source do not alias optimize into
8515 memcpy as well. */
8516 if ((is_gimple_min_invariant (dest)
8517 || TREE_CODE (dest) == SSA_NAME)
8518 && (is_gimple_min_invariant (src)
8519 || TREE_CODE (src) == SSA_NAME))
8521 ao_ref destr, srcr;
8522 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8523 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8524 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8526 tree fn;
8527 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8528 if (!fn)
8529 return NULL_TREE;
8530 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8534 return NULL_TREE;
8537 if (!host_integerp (len, 0))
8538 return NULL_TREE;
8539 /* FIXME:
8540 This logic lose for arguments like (type *)malloc (sizeof (type)),
8541 since we strip the casts of up to VOID return value from malloc.
8542 Perhaps we ought to inherit type from non-VOID argument here? */
8543 STRIP_NOPS (src);
8544 STRIP_NOPS (dest);
8545 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8546 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8548 tree tem = TREE_OPERAND (src, 0);
8549 STRIP_NOPS (tem);
8550 if (tem != TREE_OPERAND (src, 0))
8551 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8553 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8555 tree tem = TREE_OPERAND (dest, 0);
8556 STRIP_NOPS (tem);
8557 if (tem != TREE_OPERAND (dest, 0))
8558 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8560 srctype = TREE_TYPE (TREE_TYPE (src));
8561 if (srctype
8562 && TREE_CODE (srctype) == ARRAY_TYPE
8563 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8565 srctype = TREE_TYPE (srctype);
8566 STRIP_NOPS (src);
8567 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8569 desttype = TREE_TYPE (TREE_TYPE (dest));
8570 if (desttype
8571 && TREE_CODE (desttype) == ARRAY_TYPE
8572 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8574 desttype = TREE_TYPE (desttype);
8575 STRIP_NOPS (dest);
8576 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8578 if (!srctype || !desttype
8579 || TREE_ADDRESSABLE (srctype)
8580 || TREE_ADDRESSABLE (desttype)
8581 || !TYPE_SIZE_UNIT (srctype)
8582 || !TYPE_SIZE_UNIT (desttype)
8583 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8584 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8585 return NULL_TREE;
8587 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8588 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8589 if (dest_align < TYPE_ALIGN (desttype)
8590 || src_align < TYPE_ALIGN (srctype))
8591 return NULL_TREE;
8593 if (!ignore)
8594 dest = builtin_save_expr (dest);
8596 /* Build accesses at offset zero with a ref-all character type. */
8597 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8598 ptr_mode, true), 0);
8600 destvar = dest;
8601 STRIP_NOPS (destvar);
8602 if (TREE_CODE (destvar) == ADDR_EXPR
8603 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8604 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8605 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8606 else
8607 destvar = NULL_TREE;
8609 srcvar = src;
8610 STRIP_NOPS (srcvar);
8611 if (TREE_CODE (srcvar) == ADDR_EXPR
8612 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8613 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8615 if (!destvar
8616 || src_align >= TYPE_ALIGN (desttype))
8617 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8618 srcvar, off0);
8619 else if (!STRICT_ALIGNMENT)
8621 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8622 src_align);
8623 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8625 else
8626 srcvar = NULL_TREE;
8628 else
8629 srcvar = NULL_TREE;
8631 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8632 return NULL_TREE;
8634 if (srcvar == NULL_TREE)
8636 STRIP_NOPS (src);
8637 if (src_align >= TYPE_ALIGN (desttype))
8638 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8639 else
8641 if (STRICT_ALIGNMENT)
8642 return NULL_TREE;
8643 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8644 src_align);
8645 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8648 else if (destvar == NULL_TREE)
8650 STRIP_NOPS (dest);
8651 if (dest_align >= TYPE_ALIGN (srctype))
8652 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8653 else
8655 if (STRICT_ALIGNMENT)
8656 return NULL_TREE;
8657 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8658 dest_align);
8659 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8663 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8666 if (ignore)
8667 return expr;
8669 if (endp == 0 || endp == 3)
8670 return omit_one_operand_loc (loc, type, dest, expr);
8672 if (expr == len)
8673 expr = NULL_TREE;
8675 if (endp == 2)
8676 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8677 ssize_int (1));
8679 len = fold_convert_loc (loc, sizetype, len);
8680 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8681 dest = fold_convert_loc (loc, type, dest);
8682 if (expr)
8683 dest = omit_one_operand_loc (loc, type, dest, expr);
8684 return dest;
8687 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8688 If LEN is not NULL, it represents the length of the string to be
8689 copied. Return NULL_TREE if no simplification can be made. */
8691 tree
8692 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8694 tree fn;
8696 if (!validate_arg (dest, POINTER_TYPE)
8697 || !validate_arg (src, POINTER_TYPE))
8698 return NULL_TREE;
8700 /* If SRC and DEST are the same (and not volatile), return DEST. */
8701 if (operand_equal_p (src, dest, 0))
8702 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8704 if (optimize_function_for_size_p (cfun))
8705 return NULL_TREE;
8707 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8708 if (!fn)
8709 return NULL_TREE;
8711 if (!len)
8713 len = c_strlen (src, 1);
8714 if (! len || TREE_SIDE_EFFECTS (len))
8715 return NULL_TREE;
8718 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8719 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8720 build_call_expr_loc (loc, fn, 3, dest, src, len));
8723 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8724 Return NULL_TREE if no simplification can be made. */
8726 static tree
8727 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8729 tree fn, len, lenp1, call, type;
8731 if (!validate_arg (dest, POINTER_TYPE)
8732 || !validate_arg (src, POINTER_TYPE))
8733 return NULL_TREE;
8735 len = c_strlen (src, 1);
8736 if (!len
8737 || TREE_CODE (len) != INTEGER_CST)
8738 return NULL_TREE;
8740 if (optimize_function_for_size_p (cfun)
8741 /* If length is zero it's small enough. */
8742 && !integer_zerop (len))
8743 return NULL_TREE;
8745 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8746 if (!fn)
8747 return NULL_TREE;
8749 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8750 /* We use dest twice in building our expression. Save it from
8751 multiple expansions. */
8752 dest = builtin_save_expr (dest);
8753 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8755 type = TREE_TYPE (TREE_TYPE (fndecl));
8756 len = fold_convert_loc (loc, sizetype, len);
8757 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8758 dest = fold_convert_loc (loc, type, dest);
8759 dest = omit_one_operand_loc (loc, type, dest, call);
8760 return dest;
8763 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8764 If SLEN is not NULL, it represents the length of the source string.
8765 Return NULL_TREE if no simplification can be made. */
8767 tree
8768 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8769 tree src, tree len, tree slen)
8771 tree fn;
8773 if (!validate_arg (dest, POINTER_TYPE)
8774 || !validate_arg (src, POINTER_TYPE)
8775 || !validate_arg (len, INTEGER_TYPE))
8776 return NULL_TREE;
8778 /* If the LEN parameter is zero, return DEST. */
8779 if (integer_zerop (len))
8780 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8782 /* We can't compare slen with len as constants below if len is not a
8783 constant. */
8784 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8785 return NULL_TREE;
8787 if (!slen)
8788 slen = c_strlen (src, 1);
8790 /* Now, we must be passed a constant src ptr parameter. */
8791 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8792 return NULL_TREE;
8794 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8796 /* We do not support simplification of this case, though we do
8797 support it when expanding trees into RTL. */
8798 /* FIXME: generate a call to __builtin_memset. */
8799 if (tree_int_cst_lt (slen, len))
8800 return NULL_TREE;
8802 /* OK transform into builtin memcpy. */
8803 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8804 if (!fn)
8805 return NULL_TREE;
8806 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8807 build_call_expr_loc (loc, fn, 3, dest, src, len));
8810 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8811 arguments to the call, and TYPE is its return type.
8812 Return NULL_TREE if no simplification can be made. */
8814 static tree
8815 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8817 if (!validate_arg (arg1, POINTER_TYPE)
8818 || !validate_arg (arg2, INTEGER_TYPE)
8819 || !validate_arg (len, INTEGER_TYPE))
8820 return NULL_TREE;
8821 else
8823 const char *p1;
8825 if (TREE_CODE (arg2) != INTEGER_CST
8826 || !host_integerp (len, 1))
8827 return NULL_TREE;
8829 p1 = c_getstr (arg1);
8830 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8832 char c;
8833 const char *r;
8834 tree tem;
8836 if (target_char_cast (arg2, &c))
8837 return NULL_TREE;
8839 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8841 if (r == NULL)
8842 return build_int_cst (TREE_TYPE (arg1), 0);
8844 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8845 size_int (r - p1));
8846 return fold_convert_loc (loc, type, tem);
8848 return NULL_TREE;
8852 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8853 Return NULL_TREE if no simplification can be made. */
8855 static tree
8856 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8858 const char *p1, *p2;
8860 if (!validate_arg (arg1, POINTER_TYPE)
8861 || !validate_arg (arg2, POINTER_TYPE)
8862 || !validate_arg (len, INTEGER_TYPE))
8863 return NULL_TREE;
8865 /* If the LEN parameter is zero, return zero. */
8866 if (integer_zerop (len))
8867 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8868 arg1, arg2);
8870 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8871 if (operand_equal_p (arg1, arg2, 0))
8872 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8874 p1 = c_getstr (arg1);
8875 p2 = c_getstr (arg2);
8877 /* If all arguments are constant, and the value of len is not greater
8878 than the lengths of arg1 and arg2, evaluate at compile-time. */
8879 if (host_integerp (len, 1) && p1 && p2
8880 && compare_tree_int (len, strlen (p1) + 1) <= 0
8881 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8883 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8885 if (r > 0)
8886 return integer_one_node;
8887 else if (r < 0)
8888 return integer_minus_one_node;
8889 else
8890 return integer_zero_node;
8893 /* If len parameter is one, return an expression corresponding to
8894 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8895 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8897 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8898 tree cst_uchar_ptr_node
8899 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8901 tree ind1
8902 = fold_convert_loc (loc, integer_type_node,
8903 build1 (INDIRECT_REF, cst_uchar_node,
8904 fold_convert_loc (loc,
8905 cst_uchar_ptr_node,
8906 arg1)));
8907 tree ind2
8908 = fold_convert_loc (loc, integer_type_node,
8909 build1 (INDIRECT_REF, cst_uchar_node,
8910 fold_convert_loc (loc,
8911 cst_uchar_ptr_node,
8912 arg2)));
8913 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8916 return NULL_TREE;
8919 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8920 Return NULL_TREE if no simplification can be made. */
8922 static tree
8923 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8925 const char *p1, *p2;
8927 if (!validate_arg (arg1, POINTER_TYPE)
8928 || !validate_arg (arg2, POINTER_TYPE))
8929 return NULL_TREE;
8931 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8932 if (operand_equal_p (arg1, arg2, 0))
8933 return integer_zero_node;
8935 p1 = c_getstr (arg1);
8936 p2 = c_getstr (arg2);
8938 if (p1 && p2)
8940 const int i = strcmp (p1, p2);
8941 if (i < 0)
8942 return integer_minus_one_node;
8943 else if (i > 0)
8944 return integer_one_node;
8945 else
8946 return integer_zero_node;
8949 /* If the second arg is "", return *(const unsigned char*)arg1. */
8950 if (p2 && *p2 == '\0')
8952 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8953 tree cst_uchar_ptr_node
8954 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8956 return fold_convert_loc (loc, integer_type_node,
8957 build1 (INDIRECT_REF, cst_uchar_node,
8958 fold_convert_loc (loc,
8959 cst_uchar_ptr_node,
8960 arg1)));
8963 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8964 if (p1 && *p1 == '\0')
8966 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8967 tree cst_uchar_ptr_node
8968 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8970 tree temp
8971 = fold_convert_loc (loc, integer_type_node,
8972 build1 (INDIRECT_REF, cst_uchar_node,
8973 fold_convert_loc (loc,
8974 cst_uchar_ptr_node,
8975 arg2)));
8976 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8979 return NULL_TREE;
8982 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8983 Return NULL_TREE if no simplification can be made. */
8985 static tree
8986 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8988 const char *p1, *p2;
8990 if (!validate_arg (arg1, POINTER_TYPE)
8991 || !validate_arg (arg2, POINTER_TYPE)
8992 || !validate_arg (len, INTEGER_TYPE))
8993 return NULL_TREE;
8995 /* If the LEN parameter is zero, return zero. */
8996 if (integer_zerop (len))
8997 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8998 arg1, arg2);
9000 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9001 if (operand_equal_p (arg1, arg2, 0))
9002 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9004 p1 = c_getstr (arg1);
9005 p2 = c_getstr (arg2);
9007 if (host_integerp (len, 1) && p1 && p2)
9009 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9010 if (i > 0)
9011 return integer_one_node;
9012 else if (i < 0)
9013 return integer_minus_one_node;
9014 else
9015 return integer_zero_node;
9018 /* If the second arg is "", and the length is greater than zero,
9019 return *(const unsigned char*)arg1. */
9020 if (p2 && *p2 == '\0'
9021 && TREE_CODE (len) == INTEGER_CST
9022 && tree_int_cst_sgn (len) == 1)
9024 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9025 tree cst_uchar_ptr_node
9026 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9028 return fold_convert_loc (loc, integer_type_node,
9029 build1 (INDIRECT_REF, cst_uchar_node,
9030 fold_convert_loc (loc,
9031 cst_uchar_ptr_node,
9032 arg1)));
9035 /* If the first arg is "", and the length is greater than zero,
9036 return -*(const unsigned char*)arg2. */
9037 if (p1 && *p1 == '\0'
9038 && TREE_CODE (len) == INTEGER_CST
9039 && tree_int_cst_sgn (len) == 1)
9041 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9042 tree cst_uchar_ptr_node
9043 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9045 tree temp = fold_convert_loc (loc, integer_type_node,
9046 build1 (INDIRECT_REF, cst_uchar_node,
9047 fold_convert_loc (loc,
9048 cst_uchar_ptr_node,
9049 arg2)));
9050 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9053 /* If len parameter is one, return an expression corresponding to
9054 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9055 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9057 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9058 tree cst_uchar_ptr_node
9059 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9061 tree ind1 = fold_convert_loc (loc, integer_type_node,
9062 build1 (INDIRECT_REF, cst_uchar_node,
9063 fold_convert_loc (loc,
9064 cst_uchar_ptr_node,
9065 arg1)));
9066 tree ind2 = fold_convert_loc (loc, integer_type_node,
9067 build1 (INDIRECT_REF, cst_uchar_node,
9068 fold_convert_loc (loc,
9069 cst_uchar_ptr_node,
9070 arg2)));
9071 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9074 return NULL_TREE;
9077 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9078 ARG. Return NULL_TREE if no simplification can be made. */
9080 static tree
9081 fold_builtin_signbit (location_t loc, tree arg, tree type)
9083 if (!validate_arg (arg, REAL_TYPE))
9084 return NULL_TREE;
9086 /* If ARG is a compile-time constant, determine the result. */
9087 if (TREE_CODE (arg) == REAL_CST
9088 && !TREE_OVERFLOW (arg))
9090 REAL_VALUE_TYPE c;
9092 c = TREE_REAL_CST (arg);
9093 return (REAL_VALUE_NEGATIVE (c)
9094 ? build_one_cst (type)
9095 : build_zero_cst (type));
9098 /* If ARG is non-negative, the result is always zero. */
9099 if (tree_expr_nonnegative_p (arg))
9100 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9102 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9103 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9104 return fold_build2_loc (loc, LT_EXPR, type, arg,
9105 build_real (TREE_TYPE (arg), dconst0));
9107 return NULL_TREE;
9110 /* Fold function call to builtin copysign, copysignf or copysignl with
9111 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9112 be made. */
9114 static tree
9115 fold_builtin_copysign (location_t loc, tree fndecl,
9116 tree arg1, tree arg2, tree type)
9118 tree tem;
9120 if (!validate_arg (arg1, REAL_TYPE)
9121 || !validate_arg (arg2, REAL_TYPE))
9122 return NULL_TREE;
9124 /* copysign(X,X) is X. */
9125 if (operand_equal_p (arg1, arg2, 0))
9126 return fold_convert_loc (loc, type, arg1);
9128 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9129 if (TREE_CODE (arg1) == REAL_CST
9130 && TREE_CODE (arg2) == REAL_CST
9131 && !TREE_OVERFLOW (arg1)
9132 && !TREE_OVERFLOW (arg2))
9134 REAL_VALUE_TYPE c1, c2;
9136 c1 = TREE_REAL_CST (arg1);
9137 c2 = TREE_REAL_CST (arg2);
9138 /* c1.sign := c2.sign. */
9139 real_copysign (&c1, &c2);
9140 return build_real (type, c1);
9143 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9144 Remember to evaluate Y for side-effects. */
9145 if (tree_expr_nonnegative_p (arg2))
9146 return omit_one_operand_loc (loc, type,
9147 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9148 arg2);
9150 /* Strip sign changing operations for the first argument. */
9151 tem = fold_strip_sign_ops (arg1);
9152 if (tem)
9153 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9155 return NULL_TREE;
9158 /* Fold a call to builtin isascii with argument ARG. */
9160 static tree
9161 fold_builtin_isascii (location_t loc, tree arg)
9163 if (!validate_arg (arg, INTEGER_TYPE))
9164 return NULL_TREE;
9165 else
9167 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9168 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9169 build_int_cst (NULL_TREE,
9170 ~ (unsigned HOST_WIDE_INT) 0x7f));
9171 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9172 arg, integer_zero_node);
9176 /* Fold a call to builtin toascii with argument ARG. */
9178 static tree
9179 fold_builtin_toascii (location_t loc, tree arg)
9181 if (!validate_arg (arg, INTEGER_TYPE))
9182 return NULL_TREE;
9184 /* Transform toascii(c) -> (c & 0x7f). */
9185 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9186 build_int_cst (NULL_TREE, 0x7f));
9189 /* Fold a call to builtin isdigit with argument ARG. */
9191 static tree
9192 fold_builtin_isdigit (location_t loc, tree arg)
9194 if (!validate_arg (arg, INTEGER_TYPE))
9195 return NULL_TREE;
9196 else
9198 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9199 /* According to the C standard, isdigit is unaffected by locale.
9200 However, it definitely is affected by the target character set. */
9201 unsigned HOST_WIDE_INT target_digit0
9202 = lang_hooks.to_target_charset ('0');
9204 if (target_digit0 == 0)
9205 return NULL_TREE;
9207 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9208 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9209 build_int_cst (unsigned_type_node, target_digit0));
9210 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9211 build_int_cst (unsigned_type_node, 9));
9215 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9217 static tree
9218 fold_builtin_fabs (location_t loc, tree arg, tree type)
9220 if (!validate_arg (arg, REAL_TYPE))
9221 return NULL_TREE;
9223 arg = fold_convert_loc (loc, type, arg);
9224 if (TREE_CODE (arg) == REAL_CST)
9225 return fold_abs_const (arg, type);
9226 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9229 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9231 static tree
9232 fold_builtin_abs (location_t loc, tree arg, tree type)
9234 if (!validate_arg (arg, INTEGER_TYPE))
9235 return NULL_TREE;
9237 arg = fold_convert_loc (loc, type, arg);
9238 if (TREE_CODE (arg) == INTEGER_CST)
9239 return fold_abs_const (arg, type);
9240 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9243 /* Fold a fma operation with arguments ARG[012]. */
9245 tree
9246 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9247 tree type, tree arg0, tree arg1, tree arg2)
9249 if (TREE_CODE (arg0) == REAL_CST
9250 && TREE_CODE (arg1) == REAL_CST
9251 && TREE_CODE (arg2) == REAL_CST)
9252 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9254 return NULL_TREE;
9257 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9259 static tree
9260 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9262 if (validate_arg (arg0, REAL_TYPE)
9263 && validate_arg(arg1, REAL_TYPE)
9264 && validate_arg(arg2, REAL_TYPE))
9266 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9267 if (tem)
9268 return tem;
9270 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9271 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9272 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9274 return NULL_TREE;
9277 /* Fold a call to builtin fmin or fmax. */
9279 static tree
9280 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9281 tree type, bool max)
9283 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9285 /* Calculate the result when the argument is a constant. */
9286 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9288 if (res)
9289 return res;
9291 /* If either argument is NaN, return the other one. Avoid the
9292 transformation if we get (and honor) a signalling NaN. Using
9293 omit_one_operand() ensures we create a non-lvalue. */
9294 if (TREE_CODE (arg0) == REAL_CST
9295 && real_isnan (&TREE_REAL_CST (arg0))
9296 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9297 || ! TREE_REAL_CST (arg0).signalling))
9298 return omit_one_operand_loc (loc, type, arg1, arg0);
9299 if (TREE_CODE (arg1) == REAL_CST
9300 && real_isnan (&TREE_REAL_CST (arg1))
9301 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9302 || ! TREE_REAL_CST (arg1).signalling))
9303 return omit_one_operand_loc (loc, type, arg0, arg1);
9305 /* Transform fmin/fmax(x,x) -> x. */
9306 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9307 return omit_one_operand_loc (loc, type, arg0, arg1);
9309 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9310 functions to return the numeric arg if the other one is NaN.
9311 These tree codes don't honor that, so only transform if
9312 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9313 handled, so we don't have to worry about it either. */
9314 if (flag_finite_math_only)
9315 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9316 fold_convert_loc (loc, type, arg0),
9317 fold_convert_loc (loc, type, arg1));
9319 return NULL_TREE;
9322 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9324 static tree
9325 fold_builtin_carg (location_t loc, tree arg, tree type)
9327 if (validate_arg (arg, COMPLEX_TYPE)
9328 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9330 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9332 if (atan2_fn)
9334 tree new_arg = builtin_save_expr (arg);
9335 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9336 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9337 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9341 return NULL_TREE;
9344 /* Fold a call to builtin logb/ilogb. */
9346 static tree
9347 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9349 if (! validate_arg (arg, REAL_TYPE))
9350 return NULL_TREE;
9352 STRIP_NOPS (arg);
9354 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9356 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9358 switch (value->cl)
9360 case rvc_nan:
9361 case rvc_inf:
9362 /* If arg is Inf or NaN and we're logb, return it. */
9363 if (TREE_CODE (rettype) == REAL_TYPE)
9364 return fold_convert_loc (loc, rettype, arg);
9365 /* Fall through... */
9366 case rvc_zero:
9367 /* Zero may set errno and/or raise an exception for logb, also
9368 for ilogb we don't know FP_ILOGB0. */
9369 return NULL_TREE;
9370 case rvc_normal:
9371 /* For normal numbers, proceed iff radix == 2. In GCC,
9372 normalized significands are in the range [0.5, 1.0). We
9373 want the exponent as if they were [1.0, 2.0) so get the
9374 exponent and subtract 1. */
9375 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9376 return fold_convert_loc (loc, rettype,
9377 build_int_cst (NULL_TREE,
9378 REAL_EXP (value)-1));
9379 break;
9383 return NULL_TREE;
9386 /* Fold a call to builtin significand, if radix == 2. */
9388 static tree
9389 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9391 if (! validate_arg (arg, REAL_TYPE))
9392 return NULL_TREE;
9394 STRIP_NOPS (arg);
9396 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9398 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9400 switch (value->cl)
9402 case rvc_zero:
9403 case rvc_nan:
9404 case rvc_inf:
9405 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9406 return fold_convert_loc (loc, rettype, arg);
9407 case rvc_normal:
9408 /* For normal numbers, proceed iff radix == 2. */
9409 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9411 REAL_VALUE_TYPE result = *value;
9412 /* In GCC, normalized significands are in the range [0.5,
9413 1.0). We want them to be [1.0, 2.0) so set the
9414 exponent to 1. */
9415 SET_REAL_EXP (&result, 1);
9416 return build_real (rettype, result);
9418 break;
9422 return NULL_TREE;
9425 /* Fold a call to builtin frexp, we can assume the base is 2. */
9427 static tree
9428 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9430 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9431 return NULL_TREE;
9433 STRIP_NOPS (arg0);
9435 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9436 return NULL_TREE;
9438 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9440 /* Proceed if a valid pointer type was passed in. */
9441 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9443 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9444 tree frac, exp;
9446 switch (value->cl)
9448 case rvc_zero:
9449 /* For +-0, return (*exp = 0, +-0). */
9450 exp = integer_zero_node;
9451 frac = arg0;
9452 break;
9453 case rvc_nan:
9454 case rvc_inf:
9455 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9456 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9457 case rvc_normal:
9459 /* Since the frexp function always expects base 2, and in
9460 GCC normalized significands are already in the range
9461 [0.5, 1.0), we have exactly what frexp wants. */
9462 REAL_VALUE_TYPE frac_rvt = *value;
9463 SET_REAL_EXP (&frac_rvt, 0);
9464 frac = build_real (rettype, frac_rvt);
9465 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9467 break;
9468 default:
9469 gcc_unreachable ();
9472 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9473 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9474 TREE_SIDE_EFFECTS (arg1) = 1;
9475 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9478 return NULL_TREE;
9481 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9482 then we can assume the base is two. If it's false, then we have to
9483 check the mode of the TYPE parameter in certain cases. */
9485 static tree
9486 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9487 tree type, bool ldexp)
9489 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9491 STRIP_NOPS (arg0);
9492 STRIP_NOPS (arg1);
9494 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9495 if (real_zerop (arg0) || integer_zerop (arg1)
9496 || (TREE_CODE (arg0) == REAL_CST
9497 && !real_isfinite (&TREE_REAL_CST (arg0))))
9498 return omit_one_operand_loc (loc, type, arg0, arg1);
9500 /* If both arguments are constant, then try to evaluate it. */
9501 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9502 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9503 && host_integerp (arg1, 0))
9505 /* Bound the maximum adjustment to twice the range of the
9506 mode's valid exponents. Use abs to ensure the range is
9507 positive as a sanity check. */
9508 const long max_exp_adj = 2 *
9509 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9510 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9512 /* Get the user-requested adjustment. */
9513 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9515 /* The requested adjustment must be inside this range. This
9516 is a preliminary cap to avoid things like overflow, we
9517 may still fail to compute the result for other reasons. */
9518 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9520 REAL_VALUE_TYPE initial_result;
9522 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9524 /* Ensure we didn't overflow. */
9525 if (! real_isinf (&initial_result))
9527 const REAL_VALUE_TYPE trunc_result
9528 = real_value_truncate (TYPE_MODE (type), initial_result);
9530 /* Only proceed if the target mode can hold the
9531 resulting value. */
9532 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9533 return build_real (type, trunc_result);
9539 return NULL_TREE;
9542 /* Fold a call to builtin modf. */
9544 static tree
9545 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9547 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9548 return NULL_TREE;
9550 STRIP_NOPS (arg0);
9552 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9553 return NULL_TREE;
9555 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9557 /* Proceed if a valid pointer type was passed in. */
9558 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9560 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9561 REAL_VALUE_TYPE trunc, frac;
9563 switch (value->cl)
9565 case rvc_nan:
9566 case rvc_zero:
9567 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9568 trunc = frac = *value;
9569 break;
9570 case rvc_inf:
9571 /* For +-Inf, return (*arg1 = arg0, +-0). */
9572 frac = dconst0;
9573 frac.sign = value->sign;
9574 trunc = *value;
9575 break;
9576 case rvc_normal:
9577 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9578 real_trunc (&trunc, VOIDmode, value);
9579 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9580 /* If the original number was negative and already
9581 integral, then the fractional part is -0.0. */
9582 if (value->sign && frac.cl == rvc_zero)
9583 frac.sign = value->sign;
9584 break;
9587 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9588 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9589 build_real (rettype, trunc));
9590 TREE_SIDE_EFFECTS (arg1) = 1;
9591 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9592 build_real (rettype, frac));
9595 return NULL_TREE;
9598 /* Given a location LOC, an interclass builtin function decl FNDECL
9599 and its single argument ARG, return an folded expression computing
9600 the same, or NULL_TREE if we either couldn't or didn't want to fold
9601 (the latter happen if there's an RTL instruction available). */
9603 static tree
9604 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9606 enum machine_mode mode;
9608 if (!validate_arg (arg, REAL_TYPE))
9609 return NULL_TREE;
9611 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9612 return NULL_TREE;
9614 mode = TYPE_MODE (TREE_TYPE (arg));
9616 /* If there is no optab, try generic code. */
9617 switch (DECL_FUNCTION_CODE (fndecl))
9619 tree result;
9621 CASE_FLT_FN (BUILT_IN_ISINF):
9623 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9624 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9625 tree const type = TREE_TYPE (arg);
9626 REAL_VALUE_TYPE r;
9627 char buf[128];
9629 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9630 real_from_string (&r, buf);
9631 result = build_call_expr (isgr_fn, 2,
9632 fold_build1_loc (loc, ABS_EXPR, type, arg),
9633 build_real (type, r));
9634 return result;
9636 CASE_FLT_FN (BUILT_IN_FINITE):
9637 case BUILT_IN_ISFINITE:
9639 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9640 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9641 tree const type = TREE_TYPE (arg);
9642 REAL_VALUE_TYPE r;
9643 char buf[128];
9645 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9646 real_from_string (&r, buf);
9647 result = build_call_expr (isle_fn, 2,
9648 fold_build1_loc (loc, ABS_EXPR, type, arg),
9649 build_real (type, r));
9650 /*result = fold_build2_loc (loc, UNGT_EXPR,
9651 TREE_TYPE (TREE_TYPE (fndecl)),
9652 fold_build1_loc (loc, ABS_EXPR, type, arg),
9653 build_real (type, r));
9654 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9655 TREE_TYPE (TREE_TYPE (fndecl)),
9656 result);*/
9657 return result;
9659 case BUILT_IN_ISNORMAL:
9661 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9662 islessequal(fabs(x),DBL_MAX). */
9663 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9664 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9665 tree const type = TREE_TYPE (arg);
9666 REAL_VALUE_TYPE rmax, rmin;
9667 char buf[128];
9669 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9670 real_from_string (&rmax, buf);
9671 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9672 real_from_string (&rmin, buf);
9673 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9674 result = build_call_expr (isle_fn, 2, arg,
9675 build_real (type, rmax));
9676 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9677 build_call_expr (isge_fn, 2, arg,
9678 build_real (type, rmin)));
9679 return result;
9681 default:
9682 break;
9685 return NULL_TREE;
9688 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9689 ARG is the argument for the call. */
9691 static tree
9692 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9694 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9695 REAL_VALUE_TYPE r;
9697 if (!validate_arg (arg, REAL_TYPE))
9698 return NULL_TREE;
9700 switch (builtin_index)
9702 case BUILT_IN_ISINF:
9703 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9704 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9706 if (TREE_CODE (arg) == REAL_CST)
9708 r = TREE_REAL_CST (arg);
9709 if (real_isinf (&r))
9710 return real_compare (GT_EXPR, &r, &dconst0)
9711 ? integer_one_node : integer_minus_one_node;
9712 else
9713 return integer_zero_node;
9716 return NULL_TREE;
9718 case BUILT_IN_ISINF_SIGN:
9720 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9721 /* In a boolean context, GCC will fold the inner COND_EXPR to
9722 1. So e.g. "if (isinf_sign(x))" would be folded to just
9723 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9724 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9725 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9726 tree tmp = NULL_TREE;
9728 arg = builtin_save_expr (arg);
9730 if (signbit_fn && isinf_fn)
9732 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9733 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9735 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9736 signbit_call, integer_zero_node);
9737 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9738 isinf_call, integer_zero_node);
9740 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9741 integer_minus_one_node, integer_one_node);
9742 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9743 isinf_call, tmp,
9744 integer_zero_node);
9747 return tmp;
9750 case BUILT_IN_ISFINITE:
9751 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9752 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9753 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9755 if (TREE_CODE (arg) == REAL_CST)
9757 r = TREE_REAL_CST (arg);
9758 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9761 return NULL_TREE;
9763 case BUILT_IN_ISNAN:
9764 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9765 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9767 if (TREE_CODE (arg) == REAL_CST)
9769 r = TREE_REAL_CST (arg);
9770 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9773 arg = builtin_save_expr (arg);
9774 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9776 default:
9777 gcc_unreachable ();
9781 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9782 This builtin will generate code to return the appropriate floating
9783 point classification depending on the value of the floating point
9784 number passed in. The possible return values must be supplied as
9785 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9786 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9787 one floating point argument which is "type generic". */
9789 static tree
9790 fold_builtin_fpclassify (location_t loc, tree exp)
9792 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9793 arg, type, res, tmp;
9794 enum machine_mode mode;
9795 REAL_VALUE_TYPE r;
9796 char buf[128];
9798 /* Verify the required arguments in the original call. */
9799 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9800 INTEGER_TYPE, INTEGER_TYPE,
9801 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9802 return NULL_TREE;
9804 fp_nan = CALL_EXPR_ARG (exp, 0);
9805 fp_infinite = CALL_EXPR_ARG (exp, 1);
9806 fp_normal = CALL_EXPR_ARG (exp, 2);
9807 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9808 fp_zero = CALL_EXPR_ARG (exp, 4);
9809 arg = CALL_EXPR_ARG (exp, 5);
9810 type = TREE_TYPE (arg);
9811 mode = TYPE_MODE (type);
9812 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9814 /* fpclassify(x) ->
9815 isnan(x) ? FP_NAN :
9816 (fabs(x) == Inf ? FP_INFINITE :
9817 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9818 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9820 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9821 build_real (type, dconst0));
9822 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9823 tmp, fp_zero, fp_subnormal);
9825 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9826 real_from_string (&r, buf);
9827 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9828 arg, build_real (type, r));
9829 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9831 if (HONOR_INFINITIES (mode))
9833 real_inf (&r);
9834 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9835 build_real (type, r));
9836 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9837 fp_infinite, res);
9840 if (HONOR_NANS (mode))
9842 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9843 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9846 return res;
9849 /* Fold a call to an unordered comparison function such as
9850 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9851 being called and ARG0 and ARG1 are the arguments for the call.
9852 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9853 the opposite of the desired result. UNORDERED_CODE is used
9854 for modes that can hold NaNs and ORDERED_CODE is used for
9855 the rest. */
9857 static tree
9858 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9859 enum tree_code unordered_code,
9860 enum tree_code ordered_code)
9862 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9863 enum tree_code code;
9864 tree type0, type1;
9865 enum tree_code code0, code1;
9866 tree cmp_type = NULL_TREE;
9868 type0 = TREE_TYPE (arg0);
9869 type1 = TREE_TYPE (arg1);
9871 code0 = TREE_CODE (type0);
9872 code1 = TREE_CODE (type1);
9874 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9875 /* Choose the wider of two real types. */
9876 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9877 ? type0 : type1;
9878 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9879 cmp_type = type0;
9880 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9881 cmp_type = type1;
9883 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9884 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9886 if (unordered_code == UNORDERED_EXPR)
9888 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9889 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9890 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9893 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9894 : ordered_code;
9895 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9896 fold_build2_loc (loc, code, type, arg0, arg1));
9899 /* Fold a call to built-in function FNDECL with 0 arguments.
9900 IGNORE is true if the result of the function call is ignored. This
9901 function returns NULL_TREE if no simplification was possible. */
9903 static tree
9904 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9906 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9907 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9908 switch (fcode)
9910 CASE_FLT_FN (BUILT_IN_INF):
9911 case BUILT_IN_INFD32:
9912 case BUILT_IN_INFD64:
9913 case BUILT_IN_INFD128:
9914 return fold_builtin_inf (loc, type, true);
9916 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9917 return fold_builtin_inf (loc, type, false);
9919 case BUILT_IN_CLASSIFY_TYPE:
9920 return fold_builtin_classify_type (NULL_TREE);
9922 default:
9923 break;
9925 return NULL_TREE;
9928 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9929 IGNORE is true if the result of the function call is ignored. This
9930 function returns NULL_TREE if no simplification was possible. */
9932 static tree
9933 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9935 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9936 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9937 switch (fcode)
9939 case BUILT_IN_CONSTANT_P:
9941 tree val = fold_builtin_constant_p (arg0);
9943 /* Gimplification will pull the CALL_EXPR for the builtin out of
9944 an if condition. When not optimizing, we'll not CSE it back.
9945 To avoid link error types of regressions, return false now. */
9946 if (!val && !optimize)
9947 val = integer_zero_node;
9949 return val;
9952 case BUILT_IN_CLASSIFY_TYPE:
9953 return fold_builtin_classify_type (arg0);
9955 case BUILT_IN_STRLEN:
9956 return fold_builtin_strlen (loc, type, arg0);
9958 CASE_FLT_FN (BUILT_IN_FABS):
9959 return fold_builtin_fabs (loc, arg0, type);
9961 case BUILT_IN_ABS:
9962 case BUILT_IN_LABS:
9963 case BUILT_IN_LLABS:
9964 case BUILT_IN_IMAXABS:
9965 return fold_builtin_abs (loc, arg0, type);
9967 CASE_FLT_FN (BUILT_IN_CONJ):
9968 if (validate_arg (arg0, COMPLEX_TYPE)
9969 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9970 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9971 break;
9973 CASE_FLT_FN (BUILT_IN_CREAL):
9974 if (validate_arg (arg0, COMPLEX_TYPE)
9975 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9976 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9977 break;
9979 CASE_FLT_FN (BUILT_IN_CIMAG):
9980 if (validate_arg (arg0, COMPLEX_TYPE)
9981 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9982 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9983 break;
9985 CASE_FLT_FN (BUILT_IN_CCOS):
9986 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9988 CASE_FLT_FN (BUILT_IN_CCOSH):
9989 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9991 CASE_FLT_FN (BUILT_IN_CPROJ):
9992 return fold_builtin_cproj(loc, arg0, type);
9994 CASE_FLT_FN (BUILT_IN_CSIN):
9995 if (validate_arg (arg0, COMPLEX_TYPE)
9996 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9997 return do_mpc_arg1 (arg0, type, mpc_sin);
9998 break;
10000 CASE_FLT_FN (BUILT_IN_CSINH):
10001 if (validate_arg (arg0, COMPLEX_TYPE)
10002 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10003 return do_mpc_arg1 (arg0, type, mpc_sinh);
10004 break;
10006 CASE_FLT_FN (BUILT_IN_CTAN):
10007 if (validate_arg (arg0, COMPLEX_TYPE)
10008 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10009 return do_mpc_arg1 (arg0, type, mpc_tan);
10010 break;
10012 CASE_FLT_FN (BUILT_IN_CTANH):
10013 if (validate_arg (arg0, COMPLEX_TYPE)
10014 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10015 return do_mpc_arg1 (arg0, type, mpc_tanh);
10016 break;
10018 CASE_FLT_FN (BUILT_IN_CLOG):
10019 if (validate_arg (arg0, COMPLEX_TYPE)
10020 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10021 return do_mpc_arg1 (arg0, type, mpc_log);
10022 break;
10024 CASE_FLT_FN (BUILT_IN_CSQRT):
10025 if (validate_arg (arg0, COMPLEX_TYPE)
10026 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10027 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10028 break;
10030 CASE_FLT_FN (BUILT_IN_CASIN):
10031 if (validate_arg (arg0, COMPLEX_TYPE)
10032 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10033 return do_mpc_arg1 (arg0, type, mpc_asin);
10034 break;
10036 CASE_FLT_FN (BUILT_IN_CACOS):
10037 if (validate_arg (arg0, COMPLEX_TYPE)
10038 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10039 return do_mpc_arg1 (arg0, type, mpc_acos);
10040 break;
10042 CASE_FLT_FN (BUILT_IN_CATAN):
10043 if (validate_arg (arg0, COMPLEX_TYPE)
10044 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10045 return do_mpc_arg1 (arg0, type, mpc_atan);
10046 break;
10048 CASE_FLT_FN (BUILT_IN_CASINH):
10049 if (validate_arg (arg0, COMPLEX_TYPE)
10050 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10051 return do_mpc_arg1 (arg0, type, mpc_asinh);
10052 break;
10054 CASE_FLT_FN (BUILT_IN_CACOSH):
10055 if (validate_arg (arg0, COMPLEX_TYPE)
10056 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10057 return do_mpc_arg1 (arg0, type, mpc_acosh);
10058 break;
10060 CASE_FLT_FN (BUILT_IN_CATANH):
10061 if (validate_arg (arg0, COMPLEX_TYPE)
10062 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10063 return do_mpc_arg1 (arg0, type, mpc_atanh);
10064 break;
10066 CASE_FLT_FN (BUILT_IN_CABS):
10067 return fold_builtin_cabs (loc, arg0, type, fndecl);
10069 CASE_FLT_FN (BUILT_IN_CARG):
10070 return fold_builtin_carg (loc, arg0, type);
10072 CASE_FLT_FN (BUILT_IN_SQRT):
10073 return fold_builtin_sqrt (loc, arg0, type);
10075 CASE_FLT_FN (BUILT_IN_CBRT):
10076 return fold_builtin_cbrt (loc, arg0, type);
10078 CASE_FLT_FN (BUILT_IN_ASIN):
10079 if (validate_arg (arg0, REAL_TYPE))
10080 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10081 &dconstm1, &dconst1, true);
10082 break;
10084 CASE_FLT_FN (BUILT_IN_ACOS):
10085 if (validate_arg (arg0, REAL_TYPE))
10086 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10087 &dconstm1, &dconst1, true);
10088 break;
10090 CASE_FLT_FN (BUILT_IN_ATAN):
10091 if (validate_arg (arg0, REAL_TYPE))
10092 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10093 break;
10095 CASE_FLT_FN (BUILT_IN_ASINH):
10096 if (validate_arg (arg0, REAL_TYPE))
10097 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10098 break;
10100 CASE_FLT_FN (BUILT_IN_ACOSH):
10101 if (validate_arg (arg0, REAL_TYPE))
10102 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10103 &dconst1, NULL, true);
10104 break;
10106 CASE_FLT_FN (BUILT_IN_ATANH):
10107 if (validate_arg (arg0, REAL_TYPE))
10108 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10109 &dconstm1, &dconst1, false);
10110 break;
10112 CASE_FLT_FN (BUILT_IN_SIN):
10113 if (validate_arg (arg0, REAL_TYPE))
10114 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10115 break;
10117 CASE_FLT_FN (BUILT_IN_COS):
10118 return fold_builtin_cos (loc, arg0, type, fndecl);
10120 CASE_FLT_FN (BUILT_IN_TAN):
10121 return fold_builtin_tan (arg0, type);
10123 CASE_FLT_FN (BUILT_IN_CEXP):
10124 return fold_builtin_cexp (loc, arg0, type);
10126 CASE_FLT_FN (BUILT_IN_CEXPI):
10127 if (validate_arg (arg0, REAL_TYPE))
10128 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10129 break;
10131 CASE_FLT_FN (BUILT_IN_SINH):
10132 if (validate_arg (arg0, REAL_TYPE))
10133 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10134 break;
10136 CASE_FLT_FN (BUILT_IN_COSH):
10137 return fold_builtin_cosh (loc, arg0, type, fndecl);
10139 CASE_FLT_FN (BUILT_IN_TANH):
10140 if (validate_arg (arg0, REAL_TYPE))
10141 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10142 break;
10144 CASE_FLT_FN (BUILT_IN_ERF):
10145 if (validate_arg (arg0, REAL_TYPE))
10146 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10147 break;
10149 CASE_FLT_FN (BUILT_IN_ERFC):
10150 if (validate_arg (arg0, REAL_TYPE))
10151 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10152 break;
10154 CASE_FLT_FN (BUILT_IN_TGAMMA):
10155 if (validate_arg (arg0, REAL_TYPE))
10156 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10157 break;
10159 CASE_FLT_FN (BUILT_IN_EXP):
10160 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10162 CASE_FLT_FN (BUILT_IN_EXP2):
10163 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10165 CASE_FLT_FN (BUILT_IN_EXP10):
10166 CASE_FLT_FN (BUILT_IN_POW10):
10167 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10169 CASE_FLT_FN (BUILT_IN_EXPM1):
10170 if (validate_arg (arg0, REAL_TYPE))
10171 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10172 break;
10174 CASE_FLT_FN (BUILT_IN_LOG):
10175 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10177 CASE_FLT_FN (BUILT_IN_LOG2):
10178 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10180 CASE_FLT_FN (BUILT_IN_LOG10):
10181 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10183 CASE_FLT_FN (BUILT_IN_LOG1P):
10184 if (validate_arg (arg0, REAL_TYPE))
10185 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10186 &dconstm1, NULL, false);
10187 break;
10189 CASE_FLT_FN (BUILT_IN_J0):
10190 if (validate_arg (arg0, REAL_TYPE))
10191 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10192 NULL, NULL, 0);
10193 break;
10195 CASE_FLT_FN (BUILT_IN_J1):
10196 if (validate_arg (arg0, REAL_TYPE))
10197 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10198 NULL, NULL, 0);
10199 break;
10201 CASE_FLT_FN (BUILT_IN_Y0):
10202 if (validate_arg (arg0, REAL_TYPE))
10203 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10204 &dconst0, NULL, false);
10205 break;
10207 CASE_FLT_FN (BUILT_IN_Y1):
10208 if (validate_arg (arg0, REAL_TYPE))
10209 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10210 &dconst0, NULL, false);
10211 break;
10213 CASE_FLT_FN (BUILT_IN_NAN):
10214 case BUILT_IN_NAND32:
10215 case BUILT_IN_NAND64:
10216 case BUILT_IN_NAND128:
10217 return fold_builtin_nan (arg0, type, true);
10219 CASE_FLT_FN (BUILT_IN_NANS):
10220 return fold_builtin_nan (arg0, type, false);
10222 CASE_FLT_FN (BUILT_IN_FLOOR):
10223 return fold_builtin_floor (loc, fndecl, arg0);
10225 CASE_FLT_FN (BUILT_IN_CEIL):
10226 return fold_builtin_ceil (loc, fndecl, arg0);
10228 CASE_FLT_FN (BUILT_IN_TRUNC):
10229 return fold_builtin_trunc (loc, fndecl, arg0);
10231 CASE_FLT_FN (BUILT_IN_ROUND):
10232 return fold_builtin_round (loc, fndecl, arg0);
10234 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10235 CASE_FLT_FN (BUILT_IN_RINT):
10236 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10238 CASE_FLT_FN (BUILT_IN_LCEIL):
10239 CASE_FLT_FN (BUILT_IN_LLCEIL):
10240 CASE_FLT_FN (BUILT_IN_LFLOOR):
10241 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10242 CASE_FLT_FN (BUILT_IN_LROUND):
10243 CASE_FLT_FN (BUILT_IN_LLROUND):
10244 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10246 CASE_FLT_FN (BUILT_IN_LRINT):
10247 CASE_FLT_FN (BUILT_IN_LLRINT):
10248 return fold_fixed_mathfn (loc, fndecl, arg0);
10250 case BUILT_IN_BSWAP32:
10251 case BUILT_IN_BSWAP64:
10252 return fold_builtin_bswap (fndecl, arg0);
10254 CASE_INT_FN (BUILT_IN_FFS):
10255 CASE_INT_FN (BUILT_IN_CLZ):
10256 CASE_INT_FN (BUILT_IN_CTZ):
10257 CASE_INT_FN (BUILT_IN_POPCOUNT):
10258 CASE_INT_FN (BUILT_IN_PARITY):
10259 return fold_builtin_bitop (fndecl, arg0);
10261 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10262 return fold_builtin_signbit (loc, arg0, type);
10264 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10265 return fold_builtin_significand (loc, arg0, type);
10267 CASE_FLT_FN (BUILT_IN_ILOGB):
10268 CASE_FLT_FN (BUILT_IN_LOGB):
10269 return fold_builtin_logb (loc, arg0, type);
10271 case BUILT_IN_ISASCII:
10272 return fold_builtin_isascii (loc, arg0);
10274 case BUILT_IN_TOASCII:
10275 return fold_builtin_toascii (loc, arg0);
10277 case BUILT_IN_ISDIGIT:
10278 return fold_builtin_isdigit (loc, arg0);
10280 CASE_FLT_FN (BUILT_IN_FINITE):
10281 case BUILT_IN_FINITED32:
10282 case BUILT_IN_FINITED64:
10283 case BUILT_IN_FINITED128:
10284 case BUILT_IN_ISFINITE:
10286 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10287 if (ret)
10288 return ret;
10289 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10292 CASE_FLT_FN (BUILT_IN_ISINF):
10293 case BUILT_IN_ISINFD32:
10294 case BUILT_IN_ISINFD64:
10295 case BUILT_IN_ISINFD128:
10297 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10298 if (ret)
10299 return ret;
10300 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10303 case BUILT_IN_ISNORMAL:
10304 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10306 case BUILT_IN_ISINF_SIGN:
10307 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10309 CASE_FLT_FN (BUILT_IN_ISNAN):
10310 case BUILT_IN_ISNAND32:
10311 case BUILT_IN_ISNAND64:
10312 case BUILT_IN_ISNAND128:
10313 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10315 case BUILT_IN_PRINTF:
10316 case BUILT_IN_PRINTF_UNLOCKED:
10317 case BUILT_IN_VPRINTF:
10318 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10320 case BUILT_IN_FREE:
10321 if (integer_zerop (arg0))
10322 return build_empty_stmt (loc);
10323 break;
10325 default:
10326 break;
10329 return NULL_TREE;
10333 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10334 IGNORE is true if the result of the function call is ignored. This
10335 function returns NULL_TREE if no simplification was possible. */
10337 static tree
10338 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10340 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10341 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10343 switch (fcode)
10345 CASE_FLT_FN (BUILT_IN_JN):
10346 if (validate_arg (arg0, INTEGER_TYPE)
10347 && validate_arg (arg1, REAL_TYPE))
10348 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10349 break;
10351 CASE_FLT_FN (BUILT_IN_YN):
10352 if (validate_arg (arg0, INTEGER_TYPE)
10353 && validate_arg (arg1, REAL_TYPE))
10354 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10355 &dconst0, false);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_DREM):
10359 CASE_FLT_FN (BUILT_IN_REMAINDER):
10360 if (validate_arg (arg0, REAL_TYPE)
10361 && validate_arg(arg1, REAL_TYPE))
10362 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10363 break;
10365 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10366 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10367 if (validate_arg (arg0, REAL_TYPE)
10368 && validate_arg(arg1, POINTER_TYPE))
10369 return do_mpfr_lgamma_r (arg0, arg1, type);
10370 break;
10372 CASE_FLT_FN (BUILT_IN_ATAN2):
10373 if (validate_arg (arg0, REAL_TYPE)
10374 && validate_arg(arg1, REAL_TYPE))
10375 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10376 break;
10378 CASE_FLT_FN (BUILT_IN_FDIM):
10379 if (validate_arg (arg0, REAL_TYPE)
10380 && validate_arg(arg1, REAL_TYPE))
10381 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10382 break;
10384 CASE_FLT_FN (BUILT_IN_HYPOT):
10385 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10387 CASE_FLT_FN (BUILT_IN_CPOW):
10388 if (validate_arg (arg0, COMPLEX_TYPE)
10389 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10390 && validate_arg (arg1, COMPLEX_TYPE)
10391 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10392 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10393 break;
10395 CASE_FLT_FN (BUILT_IN_LDEXP):
10396 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10397 CASE_FLT_FN (BUILT_IN_SCALBN):
10398 CASE_FLT_FN (BUILT_IN_SCALBLN):
10399 return fold_builtin_load_exponent (loc, arg0, arg1,
10400 type, /*ldexp=*/false);
10402 CASE_FLT_FN (BUILT_IN_FREXP):
10403 return fold_builtin_frexp (loc, arg0, arg1, type);
10405 CASE_FLT_FN (BUILT_IN_MODF):
10406 return fold_builtin_modf (loc, arg0, arg1, type);
10408 case BUILT_IN_BZERO:
10409 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10411 case BUILT_IN_FPUTS:
10412 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10414 case BUILT_IN_FPUTS_UNLOCKED:
10415 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10417 case BUILT_IN_STRSTR:
10418 return fold_builtin_strstr (loc, arg0, arg1, type);
10420 case BUILT_IN_STRCAT:
10421 return fold_builtin_strcat (loc, arg0, arg1);
10423 case BUILT_IN_STRSPN:
10424 return fold_builtin_strspn (loc, arg0, arg1);
10426 case BUILT_IN_STRCSPN:
10427 return fold_builtin_strcspn (loc, arg0, arg1);
10429 case BUILT_IN_STRCHR:
10430 case BUILT_IN_INDEX:
10431 return fold_builtin_strchr (loc, arg0, arg1, type);
10433 case BUILT_IN_STRRCHR:
10434 case BUILT_IN_RINDEX:
10435 return fold_builtin_strrchr (loc, arg0, arg1, type);
10437 case BUILT_IN_STRCPY:
10438 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10440 case BUILT_IN_STPCPY:
10441 if (ignore)
10443 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10444 if (!fn)
10445 break;
10447 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10449 else
10450 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10451 break;
10453 case BUILT_IN_STRCMP:
10454 return fold_builtin_strcmp (loc, arg0, arg1);
10456 case BUILT_IN_STRPBRK:
10457 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10459 case BUILT_IN_EXPECT:
10460 return fold_builtin_expect (loc, arg0, arg1);
10462 CASE_FLT_FN (BUILT_IN_POW):
10463 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10465 CASE_FLT_FN (BUILT_IN_POWI):
10466 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10468 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10469 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10471 CASE_FLT_FN (BUILT_IN_FMIN):
10472 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10474 CASE_FLT_FN (BUILT_IN_FMAX):
10475 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10477 case BUILT_IN_ISGREATER:
10478 return fold_builtin_unordered_cmp (loc, fndecl,
10479 arg0, arg1, UNLE_EXPR, LE_EXPR);
10480 case BUILT_IN_ISGREATEREQUAL:
10481 return fold_builtin_unordered_cmp (loc, fndecl,
10482 arg0, arg1, UNLT_EXPR, LT_EXPR);
10483 case BUILT_IN_ISLESS:
10484 return fold_builtin_unordered_cmp (loc, fndecl,
10485 arg0, arg1, UNGE_EXPR, GE_EXPR);
10486 case BUILT_IN_ISLESSEQUAL:
10487 return fold_builtin_unordered_cmp (loc, fndecl,
10488 arg0, arg1, UNGT_EXPR, GT_EXPR);
10489 case BUILT_IN_ISLESSGREATER:
10490 return fold_builtin_unordered_cmp (loc, fndecl,
10491 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10492 case BUILT_IN_ISUNORDERED:
10493 return fold_builtin_unordered_cmp (loc, fndecl,
10494 arg0, arg1, UNORDERED_EXPR,
10495 NOP_EXPR);
10497 /* We do the folding for va_start in the expander. */
10498 case BUILT_IN_VA_START:
10499 break;
10501 case BUILT_IN_SPRINTF:
10502 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10504 case BUILT_IN_OBJECT_SIZE:
10505 return fold_builtin_object_size (arg0, arg1);
10507 case BUILT_IN_PRINTF:
10508 case BUILT_IN_PRINTF_UNLOCKED:
10509 case BUILT_IN_VPRINTF:
10510 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10512 case BUILT_IN_PRINTF_CHK:
10513 case BUILT_IN_VPRINTF_CHK:
10514 if (!validate_arg (arg0, INTEGER_TYPE)
10515 || TREE_SIDE_EFFECTS (arg0))
10516 return NULL_TREE;
10517 else
10518 return fold_builtin_printf (loc, fndecl,
10519 arg1, NULL_TREE, ignore, fcode);
10520 break;
10522 case BUILT_IN_FPRINTF:
10523 case BUILT_IN_FPRINTF_UNLOCKED:
10524 case BUILT_IN_VFPRINTF:
10525 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10526 ignore, fcode);
10528 default:
10529 break;
10531 return NULL_TREE;
10534 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10535 and ARG2. IGNORE is true if the result of the function call is ignored.
10536 This function returns NULL_TREE if no simplification was possible. */
10538 static tree
10539 fold_builtin_3 (location_t loc, tree fndecl,
10540 tree arg0, tree arg1, tree arg2, bool ignore)
10542 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10543 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10544 switch (fcode)
10547 CASE_FLT_FN (BUILT_IN_SINCOS):
10548 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10550 CASE_FLT_FN (BUILT_IN_FMA):
10551 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10552 break;
10554 CASE_FLT_FN (BUILT_IN_REMQUO):
10555 if (validate_arg (arg0, REAL_TYPE)
10556 && validate_arg(arg1, REAL_TYPE)
10557 && validate_arg(arg2, POINTER_TYPE))
10558 return do_mpfr_remquo (arg0, arg1, arg2);
10559 break;
10561 case BUILT_IN_MEMSET:
10562 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10564 case BUILT_IN_BCOPY:
10565 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10566 void_type_node, true, /*endp=*/3);
10568 case BUILT_IN_MEMCPY:
10569 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10570 type, ignore, /*endp=*/0);
10572 case BUILT_IN_MEMPCPY:
10573 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10574 type, ignore, /*endp=*/1);
10576 case BUILT_IN_MEMMOVE:
10577 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10578 type, ignore, /*endp=*/3);
10580 case BUILT_IN_STRNCAT:
10581 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10583 case BUILT_IN_STRNCPY:
10584 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10586 case BUILT_IN_STRNCMP:
10587 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10589 case BUILT_IN_MEMCHR:
10590 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10592 case BUILT_IN_BCMP:
10593 case BUILT_IN_MEMCMP:
10594 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10596 case BUILT_IN_SPRINTF:
10597 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10599 case BUILT_IN_STRCPY_CHK:
10600 case BUILT_IN_STPCPY_CHK:
10601 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10602 ignore, fcode);
10604 case BUILT_IN_STRCAT_CHK:
10605 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10607 case BUILT_IN_PRINTF_CHK:
10608 case BUILT_IN_VPRINTF_CHK:
10609 if (!validate_arg (arg0, INTEGER_TYPE)
10610 || TREE_SIDE_EFFECTS (arg0))
10611 return NULL_TREE;
10612 else
10613 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10614 break;
10616 case BUILT_IN_FPRINTF:
10617 case BUILT_IN_FPRINTF_UNLOCKED:
10618 case BUILT_IN_VFPRINTF:
10619 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10620 ignore, fcode);
10622 case BUILT_IN_FPRINTF_CHK:
10623 case BUILT_IN_VFPRINTF_CHK:
10624 if (!validate_arg (arg1, INTEGER_TYPE)
10625 || TREE_SIDE_EFFECTS (arg1))
10626 return NULL_TREE;
10627 else
10628 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10629 ignore, fcode);
10631 default:
10632 break;
10634 return NULL_TREE;
10637 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10638 ARG2, and ARG3. IGNORE is true if the result of the function call is
10639 ignored. This function returns NULL_TREE if no simplification was
10640 possible. */
10642 static tree
10643 fold_builtin_4 (location_t loc, tree fndecl,
10644 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10646 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10648 switch (fcode)
10650 case BUILT_IN_MEMCPY_CHK:
10651 case BUILT_IN_MEMPCPY_CHK:
10652 case BUILT_IN_MEMMOVE_CHK:
10653 case BUILT_IN_MEMSET_CHK:
10654 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10655 NULL_TREE, ignore,
10656 DECL_FUNCTION_CODE (fndecl));
10658 case BUILT_IN_STRNCPY_CHK:
10659 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10661 case BUILT_IN_STRNCAT_CHK:
10662 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10664 case BUILT_IN_FPRINTF_CHK:
10665 case BUILT_IN_VFPRINTF_CHK:
10666 if (!validate_arg (arg1, INTEGER_TYPE)
10667 || TREE_SIDE_EFFECTS (arg1))
10668 return NULL_TREE;
10669 else
10670 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10671 ignore, fcode);
10672 break;
10674 default:
10675 break;
10677 return NULL_TREE;
10680 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10681 arguments, where NARGS <= 4. IGNORE is true if the result of the
10682 function call is ignored. This function returns NULL_TREE if no
10683 simplification was possible. Note that this only folds builtins with
10684 fixed argument patterns. Foldings that do varargs-to-varargs
10685 transformations, or that match calls with more than 4 arguments,
10686 need to be handled with fold_builtin_varargs instead. */
10688 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10690 static tree
10691 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10693 tree ret = NULL_TREE;
10695 switch (nargs)
10697 case 0:
10698 ret = fold_builtin_0 (loc, fndecl, ignore);
10699 break;
10700 case 1:
10701 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10702 break;
10703 case 2:
10704 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10705 break;
10706 case 3:
10707 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10708 break;
10709 case 4:
10710 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10711 ignore);
10712 break;
10713 default:
10714 break;
10716 if (ret)
10718 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10719 SET_EXPR_LOCATION (ret, loc);
10720 TREE_NO_WARNING (ret) = 1;
10721 return ret;
10723 return NULL_TREE;
10726 /* Builtins with folding operations that operate on "..." arguments
10727 need special handling; we need to store the arguments in a convenient
10728 data structure before attempting any folding. Fortunately there are
10729 only a few builtins that fall into this category. FNDECL is the
10730 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10731 result of the function call is ignored. */
10733 static tree
10734 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10735 bool ignore ATTRIBUTE_UNUSED)
10737 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10738 tree ret = NULL_TREE;
10740 switch (fcode)
10742 case BUILT_IN_SPRINTF_CHK:
10743 case BUILT_IN_VSPRINTF_CHK:
10744 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10745 break;
10747 case BUILT_IN_SNPRINTF_CHK:
10748 case BUILT_IN_VSNPRINTF_CHK:
10749 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10750 break;
10752 case BUILT_IN_FPCLASSIFY:
10753 ret = fold_builtin_fpclassify (loc, exp);
10754 break;
10756 default:
10757 break;
10759 if (ret)
10761 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10762 SET_EXPR_LOCATION (ret, loc);
10763 TREE_NO_WARNING (ret) = 1;
10764 return ret;
10766 return NULL_TREE;
10769 /* Return true if FNDECL shouldn't be folded right now.
10770 If a built-in function has an inline attribute always_inline
10771 wrapper, defer folding it after always_inline functions have
10772 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10773 might not be performed. */
10775 static bool
10776 avoid_folding_inline_builtin (tree fndecl)
10778 return (DECL_DECLARED_INLINE_P (fndecl)
10779 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10780 && cfun
10781 && !cfun->always_inline_functions_inlined
10782 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10785 /* A wrapper function for builtin folding that prevents warnings for
10786 "statement without effect" and the like, caused by removing the
10787 call node earlier than the warning is generated. */
10789 tree
10790 fold_call_expr (location_t loc, tree exp, bool ignore)
10792 tree ret = NULL_TREE;
10793 tree fndecl = get_callee_fndecl (exp);
10794 if (fndecl
10795 && TREE_CODE (fndecl) == FUNCTION_DECL
10796 && DECL_BUILT_IN (fndecl)
10797 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10798 yet. Defer folding until we see all the arguments
10799 (after inlining). */
10800 && !CALL_EXPR_VA_ARG_PACK (exp))
10802 int nargs = call_expr_nargs (exp);
10804 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10805 instead last argument is __builtin_va_arg_pack (). Defer folding
10806 even in that case, until arguments are finalized. */
10807 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10809 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10810 if (fndecl2
10811 && TREE_CODE (fndecl2) == FUNCTION_DECL
10812 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10813 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10814 return NULL_TREE;
10817 if (avoid_folding_inline_builtin (fndecl))
10818 return NULL_TREE;
10820 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10821 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10822 CALL_EXPR_ARGP (exp), ignore);
10823 else
10825 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10827 tree *args = CALL_EXPR_ARGP (exp);
10828 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10830 if (!ret)
10831 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10832 if (ret)
10833 return ret;
10836 return NULL_TREE;
10839 /* Conveniently construct a function call expression. FNDECL names the
10840 function to be called and N arguments are passed in the array
10841 ARGARRAY. */
10843 tree
10844 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10846 tree fntype = TREE_TYPE (fndecl);
10847 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10849 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10852 /* Conveniently construct a function call expression. FNDECL names the
10853 function to be called and the arguments are passed in the vector
10854 VEC. */
10856 tree
10857 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10859 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10860 VEC_address (tree, vec));
10864 /* Conveniently construct a function call expression. FNDECL names the
10865 function to be called, N is the number of arguments, and the "..."
10866 parameters are the argument expressions. */
10868 tree
10869 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10871 va_list ap;
10872 tree *argarray = XALLOCAVEC (tree, n);
10873 int i;
10875 va_start (ap, n);
10876 for (i = 0; i < n; i++)
10877 argarray[i] = va_arg (ap, tree);
10878 va_end (ap);
10879 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10882 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10883 varargs macros aren't supported by all bootstrap compilers. */
10885 tree
10886 build_call_expr (tree fndecl, int n, ...)
10888 va_list ap;
10889 tree *argarray = XALLOCAVEC (tree, n);
10890 int i;
10892 va_start (ap, n);
10893 for (i = 0; i < n; i++)
10894 argarray[i] = va_arg (ap, tree);
10895 va_end (ap);
10896 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10899 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10900 N arguments are passed in the array ARGARRAY. */
10902 tree
10903 fold_builtin_call_array (location_t loc, tree type,
10904 tree fn,
10905 int n,
10906 tree *argarray)
10908 tree ret = NULL_TREE;
10909 tree exp;
10911 if (TREE_CODE (fn) == ADDR_EXPR)
10913 tree fndecl = TREE_OPERAND (fn, 0);
10914 if (TREE_CODE (fndecl) == FUNCTION_DECL
10915 && DECL_BUILT_IN (fndecl))
10917 /* If last argument is __builtin_va_arg_pack (), arguments to this
10918 function are not finalized yet. Defer folding until they are. */
10919 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10921 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10922 if (fndecl2
10923 && TREE_CODE (fndecl2) == FUNCTION_DECL
10924 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10925 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10926 return build_call_array_loc (loc, type, fn, n, argarray);
10928 if (avoid_folding_inline_builtin (fndecl))
10929 return build_call_array_loc (loc, type, fn, n, argarray);
10930 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10932 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10933 if (ret)
10934 return ret;
10936 return build_call_array_loc (loc, type, fn, n, argarray);
10938 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10940 /* First try the transformations that don't require consing up
10941 an exp. */
10942 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10943 if (ret)
10944 return ret;
10947 /* If we got this far, we need to build an exp. */
10948 exp = build_call_array_loc (loc, type, fn, n, argarray);
10949 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10950 return ret ? ret : exp;
10954 return build_call_array_loc (loc, type, fn, n, argarray);
10957 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10958 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10959 of arguments in ARGS to be omitted. OLDNARGS is the number of
10960 elements in ARGS. */
10962 static tree
10963 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10964 int skip, tree fndecl, int n, va_list newargs)
10966 int nargs = oldnargs - skip + n;
10967 tree *buffer;
10969 if (n > 0)
10971 int i, j;
10973 buffer = XALLOCAVEC (tree, nargs);
10974 for (i = 0; i < n; i++)
10975 buffer[i] = va_arg (newargs, tree);
10976 for (j = skip; j < oldnargs; j++, i++)
10977 buffer[i] = args[j];
10979 else
10980 buffer = args + skip;
10982 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10985 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10986 list ARGS along with N new arguments specified as the "..."
10987 parameters. SKIP is the number of arguments in ARGS to be omitted.
10988 OLDNARGS is the number of elements in ARGS. */
10990 static tree
10991 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10992 int skip, tree fndecl, int n, ...)
10994 va_list ap;
10995 tree t;
10997 va_start (ap, n);
10998 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10999 va_end (ap);
11001 return t;
11004 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11005 along with N new arguments specified as the "..." parameters. SKIP
11006 is the number of arguments in EXP to be omitted. This function is used
11007 to do varargs-to-varargs transformations. */
11009 static tree
11010 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11012 va_list ap;
11013 tree t;
11015 va_start (ap, n);
11016 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11017 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11018 va_end (ap);
11020 return t;
11023 /* Validate a single argument ARG against a tree code CODE representing
11024 a type. */
11026 static bool
11027 validate_arg (const_tree arg, enum tree_code code)
11029 if (!arg)
11030 return false;
11031 else if (code == POINTER_TYPE)
11032 return POINTER_TYPE_P (TREE_TYPE (arg));
11033 else if (code == INTEGER_TYPE)
11034 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11035 return code == TREE_CODE (TREE_TYPE (arg));
11038 /* This function validates the types of a function call argument list
11039 against a specified list of tree_codes. If the last specifier is a 0,
11040 that represents an ellipses, otherwise the last specifier must be a
11041 VOID_TYPE.
11043 This is the GIMPLE version of validate_arglist. Eventually we want to
11044 completely convert builtins.c to work from GIMPLEs and the tree based
11045 validate_arglist will then be removed. */
11047 bool
11048 validate_gimple_arglist (const_gimple call, ...)
11050 enum tree_code code;
11051 bool res = 0;
11052 va_list ap;
11053 const_tree arg;
11054 size_t i;
11056 va_start (ap, call);
11057 i = 0;
11061 code = (enum tree_code) va_arg (ap, int);
11062 switch (code)
11064 case 0:
11065 /* This signifies an ellipses, any further arguments are all ok. */
11066 res = true;
11067 goto end;
11068 case VOID_TYPE:
11069 /* This signifies an endlink, if no arguments remain, return
11070 true, otherwise return false. */
11071 res = (i == gimple_call_num_args (call));
11072 goto end;
11073 default:
11074 /* If no parameters remain or the parameter's code does not
11075 match the specified code, return false. Otherwise continue
11076 checking any remaining arguments. */
11077 arg = gimple_call_arg (call, i++);
11078 if (!validate_arg (arg, code))
11079 goto end;
11080 break;
11083 while (1);
11085 /* We need gotos here since we can only have one VA_CLOSE in a
11086 function. */
11087 end: ;
11088 va_end (ap);
11090 return res;
11093 /* This function validates the types of a function call argument list
11094 against a specified list of tree_codes. If the last specifier is a 0,
11095 that represents an ellipses, otherwise the last specifier must be a
11096 VOID_TYPE. */
11098 bool
11099 validate_arglist (const_tree callexpr, ...)
11101 enum tree_code code;
11102 bool res = 0;
11103 va_list ap;
11104 const_call_expr_arg_iterator iter;
11105 const_tree arg;
11107 va_start (ap, callexpr);
11108 init_const_call_expr_arg_iterator (callexpr, &iter);
11112 code = (enum tree_code) va_arg (ap, int);
11113 switch (code)
11115 case 0:
11116 /* This signifies an ellipses, any further arguments are all ok. */
11117 res = true;
11118 goto end;
11119 case VOID_TYPE:
11120 /* This signifies an endlink, if no arguments remain, return
11121 true, otherwise return false. */
11122 res = !more_const_call_expr_args_p (&iter);
11123 goto end;
11124 default:
11125 /* If no parameters remain or the parameter's code does not
11126 match the specified code, return false. Otherwise continue
11127 checking any remaining arguments. */
11128 arg = next_const_call_expr_arg (&iter);
11129 if (!validate_arg (arg, code))
11130 goto end;
11131 break;
11134 while (1);
11136 /* We need gotos here since we can only have one VA_CLOSE in a
11137 function. */
11138 end: ;
11139 va_end (ap);
11141 return res;
11144 /* Default target-specific builtin expander that does nothing. */
11147 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11148 rtx target ATTRIBUTE_UNUSED,
11149 rtx subtarget ATTRIBUTE_UNUSED,
11150 enum machine_mode mode ATTRIBUTE_UNUSED,
11151 int ignore ATTRIBUTE_UNUSED)
11153 return NULL_RTX;
11156 /* Returns true is EXP represents data that would potentially reside
11157 in a readonly section. */
11159 static bool
11160 readonly_data_expr (tree exp)
11162 STRIP_NOPS (exp);
11164 if (TREE_CODE (exp) != ADDR_EXPR)
11165 return false;
11167 exp = get_base_address (TREE_OPERAND (exp, 0));
11168 if (!exp)
11169 return false;
11171 /* Make sure we call decl_readonly_section only for trees it
11172 can handle (since it returns true for everything it doesn't
11173 understand). */
11174 if (TREE_CODE (exp) == STRING_CST
11175 || TREE_CODE (exp) == CONSTRUCTOR
11176 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11177 return decl_readonly_section (exp, 0);
11178 else
11179 return false;
11182 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11183 to the call, and TYPE is its return type.
11185 Return NULL_TREE if no simplification was possible, otherwise return the
11186 simplified form of the call as a tree.
11188 The simplified form may be a constant or other expression which
11189 computes the same value, but in a more efficient manner (including
11190 calls to other builtin functions).
11192 The call may contain arguments which need to be evaluated, but
11193 which are not useful to determine the result of the call. In
11194 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11195 COMPOUND_EXPR will be an argument which must be evaluated.
11196 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11197 COMPOUND_EXPR in the chain will contain the tree for the simplified
11198 form of the builtin function call. */
11200 static tree
11201 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11203 if (!validate_arg (s1, POINTER_TYPE)
11204 || !validate_arg (s2, POINTER_TYPE))
11205 return NULL_TREE;
11206 else
11208 tree fn;
11209 const char *p1, *p2;
11211 p2 = c_getstr (s2);
11212 if (p2 == NULL)
11213 return NULL_TREE;
11215 p1 = c_getstr (s1);
11216 if (p1 != NULL)
11218 const char *r = strstr (p1, p2);
11219 tree tem;
11221 if (r == NULL)
11222 return build_int_cst (TREE_TYPE (s1), 0);
11224 /* Return an offset into the constant string argument. */
11225 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11226 s1, size_int (r - p1));
11227 return fold_convert_loc (loc, type, tem);
11230 /* The argument is const char *, and the result is char *, so we need
11231 a type conversion here to avoid a warning. */
11232 if (p2[0] == '\0')
11233 return fold_convert_loc (loc, type, s1);
11235 if (p2[1] != '\0')
11236 return NULL_TREE;
11238 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11239 if (!fn)
11240 return NULL_TREE;
11242 /* New argument list transforming strstr(s1, s2) to
11243 strchr(s1, s2[0]). */
11244 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11248 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11249 the call, and TYPE is its return type.
11251 Return NULL_TREE if no simplification was possible, otherwise return the
11252 simplified form of the call as a tree.
11254 The simplified form may be a constant or other expression which
11255 computes the same value, but in a more efficient manner (including
11256 calls to other builtin functions).
11258 The call may contain arguments which need to be evaluated, but
11259 which are not useful to determine the result of the call. In
11260 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11261 COMPOUND_EXPR will be an argument which must be evaluated.
11262 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11263 COMPOUND_EXPR in the chain will contain the tree for the simplified
11264 form of the builtin function call. */
11266 static tree
11267 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11269 if (!validate_arg (s1, POINTER_TYPE)
11270 || !validate_arg (s2, INTEGER_TYPE))
11271 return NULL_TREE;
11272 else
11274 const char *p1;
11276 if (TREE_CODE (s2) != INTEGER_CST)
11277 return NULL_TREE;
11279 p1 = c_getstr (s1);
11280 if (p1 != NULL)
11282 char c;
11283 const char *r;
11284 tree tem;
11286 if (target_char_cast (s2, &c))
11287 return NULL_TREE;
11289 r = strchr (p1, c);
11291 if (r == NULL)
11292 return build_int_cst (TREE_TYPE (s1), 0);
11294 /* Return an offset into the constant string argument. */
11295 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11296 s1, size_int (r - p1));
11297 return fold_convert_loc (loc, type, tem);
11299 return NULL_TREE;
11303 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11304 the call, and TYPE is its return type.
11306 Return NULL_TREE if no simplification was possible, otherwise return the
11307 simplified form of the call as a tree.
11309 The simplified form may be a constant or other expression which
11310 computes the same value, but in a more efficient manner (including
11311 calls to other builtin functions).
11313 The call may contain arguments which need to be evaluated, but
11314 which are not useful to determine the result of the call. In
11315 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11316 COMPOUND_EXPR will be an argument which must be evaluated.
11317 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11318 COMPOUND_EXPR in the chain will contain the tree for the simplified
11319 form of the builtin function call. */
11321 static tree
11322 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11324 if (!validate_arg (s1, POINTER_TYPE)
11325 || !validate_arg (s2, INTEGER_TYPE))
11326 return NULL_TREE;
11327 else
11329 tree fn;
11330 const char *p1;
11332 if (TREE_CODE (s2) != INTEGER_CST)
11333 return NULL_TREE;
11335 p1 = c_getstr (s1);
11336 if (p1 != NULL)
11338 char c;
11339 const char *r;
11340 tree tem;
11342 if (target_char_cast (s2, &c))
11343 return NULL_TREE;
11345 r = strrchr (p1, c);
11347 if (r == NULL)
11348 return build_int_cst (TREE_TYPE (s1), 0);
11350 /* Return an offset into the constant string argument. */
11351 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11352 s1, size_int (r - p1));
11353 return fold_convert_loc (loc, type, tem);
11356 if (! integer_zerop (s2))
11357 return NULL_TREE;
11359 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11360 if (!fn)
11361 return NULL_TREE;
11363 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11364 return build_call_expr_loc (loc, fn, 2, s1, s2);
11368 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11369 to the call, and TYPE is its return type.
11371 Return NULL_TREE if no simplification was possible, otherwise return the
11372 simplified form of the call as a tree.
11374 The simplified form may be a constant or other expression which
11375 computes the same value, but in a more efficient manner (including
11376 calls to other builtin functions).
11378 The call may contain arguments which need to be evaluated, but
11379 which are not useful to determine the result of the call. In
11380 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11381 COMPOUND_EXPR will be an argument which must be evaluated.
11382 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11383 COMPOUND_EXPR in the chain will contain the tree for the simplified
11384 form of the builtin function call. */
11386 static tree
11387 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11389 if (!validate_arg (s1, POINTER_TYPE)
11390 || !validate_arg (s2, POINTER_TYPE))
11391 return NULL_TREE;
11392 else
11394 tree fn;
11395 const char *p1, *p2;
11397 p2 = c_getstr (s2);
11398 if (p2 == NULL)
11399 return NULL_TREE;
11401 p1 = c_getstr (s1);
11402 if (p1 != NULL)
11404 const char *r = strpbrk (p1, p2);
11405 tree tem;
11407 if (r == NULL)
11408 return build_int_cst (TREE_TYPE (s1), 0);
11410 /* Return an offset into the constant string argument. */
11411 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11412 s1, size_int (r - p1));
11413 return fold_convert_loc (loc, type, tem);
11416 if (p2[0] == '\0')
11417 /* strpbrk(x, "") == NULL.
11418 Evaluate and ignore s1 in case it had side-effects. */
11419 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11421 if (p2[1] != '\0')
11422 return NULL_TREE; /* Really call strpbrk. */
11424 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11425 if (!fn)
11426 return NULL_TREE;
11428 /* New argument list transforming strpbrk(s1, s2) to
11429 strchr(s1, s2[0]). */
11430 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11434 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11435 to the call.
11437 Return NULL_TREE if no simplification was possible, otherwise return the
11438 simplified form of the call as a tree.
11440 The simplified form may be a constant or other expression which
11441 computes the same value, but in a more efficient manner (including
11442 calls to other builtin functions).
11444 The call may contain arguments which need to be evaluated, but
11445 which are not useful to determine the result of the call. In
11446 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11447 COMPOUND_EXPR will be an argument which must be evaluated.
11448 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11449 COMPOUND_EXPR in the chain will contain the tree for the simplified
11450 form of the builtin function call. */
11452 static tree
11453 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11455 if (!validate_arg (dst, POINTER_TYPE)
11456 || !validate_arg (src, POINTER_TYPE))
11457 return NULL_TREE;
11458 else
11460 const char *p = c_getstr (src);
11462 /* If the string length is zero, return the dst parameter. */
11463 if (p && *p == '\0')
11464 return dst;
11466 if (optimize_insn_for_speed_p ())
11468 /* See if we can store by pieces into (dst + strlen(dst)). */
11469 tree newdst, call;
11470 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11471 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11473 if (!strlen_fn || !strcpy_fn)
11474 return NULL_TREE;
11476 /* If we don't have a movstr we don't want to emit an strcpy
11477 call. We have to do that if the length of the source string
11478 isn't computable (in that case we can use memcpy probably
11479 later expanding to a sequence of mov instructions). If we
11480 have movstr instructions we can emit strcpy calls. */
11481 if (!HAVE_movstr)
11483 tree len = c_strlen (src, 1);
11484 if (! len || TREE_SIDE_EFFECTS (len))
11485 return NULL_TREE;
11488 /* Stabilize the argument list. */
11489 dst = builtin_save_expr (dst);
11491 /* Create strlen (dst). */
11492 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11493 /* Create (dst p+ strlen (dst)). */
11495 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11496 TREE_TYPE (dst), dst, newdst);
11497 newdst = builtin_save_expr (newdst);
11499 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11500 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11502 return NULL_TREE;
11506 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11507 arguments to the call.
11509 Return NULL_TREE if no simplification was possible, otherwise return the
11510 simplified form of the call as a tree.
11512 The simplified form may be a constant or other expression which
11513 computes the same value, but in a more efficient manner (including
11514 calls to other builtin functions).
11516 The call may contain arguments which need to be evaluated, but
11517 which are not useful to determine the result of the call. In
11518 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11519 COMPOUND_EXPR will be an argument which must be evaluated.
11520 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11521 COMPOUND_EXPR in the chain will contain the tree for the simplified
11522 form of the builtin function call. */
11524 static tree
11525 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11527 if (!validate_arg (dst, POINTER_TYPE)
11528 || !validate_arg (src, POINTER_TYPE)
11529 || !validate_arg (len, INTEGER_TYPE))
11530 return NULL_TREE;
11531 else
11533 const char *p = c_getstr (src);
11535 /* If the requested length is zero, or the src parameter string
11536 length is zero, return the dst parameter. */
11537 if (integer_zerop (len) || (p && *p == '\0'))
11538 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11540 /* If the requested len is greater than or equal to the string
11541 length, call strcat. */
11542 if (TREE_CODE (len) == INTEGER_CST && p
11543 && compare_tree_int (len, strlen (p)) >= 0)
11545 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11547 /* If the replacement _DECL isn't initialized, don't do the
11548 transformation. */
11549 if (!fn)
11550 return NULL_TREE;
11552 return build_call_expr_loc (loc, fn, 2, dst, src);
11554 return NULL_TREE;
11558 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11559 to the call.
11561 Return NULL_TREE if no simplification was possible, otherwise return the
11562 simplified form of the call as a tree.
11564 The simplified form may be a constant or other expression which
11565 computes the same value, but in a more efficient manner (including
11566 calls to other builtin functions).
11568 The call may contain arguments which need to be evaluated, but
11569 which are not useful to determine the result of the call. In
11570 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11571 COMPOUND_EXPR will be an argument which must be evaluated.
11572 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11573 COMPOUND_EXPR in the chain will contain the tree for the simplified
11574 form of the builtin function call. */
11576 static tree
11577 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11579 if (!validate_arg (s1, POINTER_TYPE)
11580 || !validate_arg (s2, POINTER_TYPE))
11581 return NULL_TREE;
11582 else
11584 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11586 /* If both arguments are constants, evaluate at compile-time. */
11587 if (p1 && p2)
11589 const size_t r = strspn (p1, p2);
11590 return size_int (r);
11593 /* If either argument is "", return NULL_TREE. */
11594 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11595 /* Evaluate and ignore both arguments in case either one has
11596 side-effects. */
11597 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11598 s1, s2);
11599 return NULL_TREE;
11603 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11604 to the call.
11606 Return NULL_TREE if no simplification was possible, otherwise return the
11607 simplified form of the call as a tree.
11609 The simplified form may be a constant or other expression which
11610 computes the same value, but in a more efficient manner (including
11611 calls to other builtin functions).
11613 The call may contain arguments which need to be evaluated, but
11614 which are not useful to determine the result of the call. In
11615 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11616 COMPOUND_EXPR will be an argument which must be evaluated.
11617 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11618 COMPOUND_EXPR in the chain will contain the tree for the simplified
11619 form of the builtin function call. */
11621 static tree
11622 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11624 if (!validate_arg (s1, POINTER_TYPE)
11625 || !validate_arg (s2, POINTER_TYPE))
11626 return NULL_TREE;
11627 else
11629 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11631 /* If both arguments are constants, evaluate at compile-time. */
11632 if (p1 && p2)
11634 const size_t r = strcspn (p1, p2);
11635 return size_int (r);
11638 /* If the first argument is "", return NULL_TREE. */
11639 if (p1 && *p1 == '\0')
11641 /* Evaluate and ignore argument s2 in case it has
11642 side-effects. */
11643 return omit_one_operand_loc (loc, size_type_node,
11644 size_zero_node, s2);
11647 /* If the second argument is "", return __builtin_strlen(s1). */
11648 if (p2 && *p2 == '\0')
11650 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11652 /* If the replacement _DECL isn't initialized, don't do the
11653 transformation. */
11654 if (!fn)
11655 return NULL_TREE;
11657 return build_call_expr_loc (loc, fn, 1, s1);
11659 return NULL_TREE;
11663 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11664 to the call. IGNORE is true if the value returned
11665 by the builtin will be ignored. UNLOCKED is true is true if this
11666 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11667 the known length of the string. Return NULL_TREE if no simplification
11668 was possible. */
11670 tree
11671 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11672 bool ignore, bool unlocked, tree len)
11674 /* If we're using an unlocked function, assume the other unlocked
11675 functions exist explicitly. */
11676 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11677 : implicit_built_in_decls[BUILT_IN_FPUTC];
11678 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11679 : implicit_built_in_decls[BUILT_IN_FWRITE];
11681 /* If the return value is used, don't do the transformation. */
11682 if (!ignore)
11683 return NULL_TREE;
11685 /* Verify the arguments in the original call. */
11686 if (!validate_arg (arg0, POINTER_TYPE)
11687 || !validate_arg (arg1, POINTER_TYPE))
11688 return NULL_TREE;
11690 if (! len)
11691 len = c_strlen (arg0, 0);
11693 /* Get the length of the string passed to fputs. If the length
11694 can't be determined, punt. */
11695 if (!len
11696 || TREE_CODE (len) != INTEGER_CST)
11697 return NULL_TREE;
11699 switch (compare_tree_int (len, 1))
11701 case -1: /* length is 0, delete the call entirely . */
11702 return omit_one_operand_loc (loc, integer_type_node,
11703 integer_zero_node, arg1);;
11705 case 0: /* length is 1, call fputc. */
11707 const char *p = c_getstr (arg0);
11709 if (p != NULL)
11711 if (fn_fputc)
11712 return build_call_expr_loc (loc, fn_fputc, 2,
11713 build_int_cst (NULL_TREE, p[0]), arg1);
11714 else
11715 return NULL_TREE;
11718 /* FALLTHROUGH */
11719 case 1: /* length is greater than 1, call fwrite. */
11721 /* If optimizing for size keep fputs. */
11722 if (optimize_function_for_size_p (cfun))
11723 return NULL_TREE;
11724 /* New argument list transforming fputs(string, stream) to
11725 fwrite(string, 1, len, stream). */
11726 if (fn_fwrite)
11727 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11728 size_one_node, len, arg1);
11729 else
11730 return NULL_TREE;
11732 default:
11733 gcc_unreachable ();
11735 return NULL_TREE;
11738 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11739 produced. False otherwise. This is done so that we don't output the error
11740 or warning twice or three times. */
11742 bool
11743 fold_builtin_next_arg (tree exp, bool va_start_p)
11745 tree fntype = TREE_TYPE (current_function_decl);
11746 int nargs = call_expr_nargs (exp);
11747 tree arg;
11749 if (!stdarg_p (fntype))
11751 error ("%<va_start%> used in function with fixed args");
11752 return true;
11755 if (va_start_p)
11757 if (va_start_p && (nargs != 2))
11759 error ("wrong number of arguments to function %<va_start%>");
11760 return true;
11762 arg = CALL_EXPR_ARG (exp, 1);
11764 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11765 when we checked the arguments and if needed issued a warning. */
11766 else
11768 if (nargs == 0)
11770 /* Evidently an out of date version of <stdarg.h>; can't validate
11771 va_start's second argument, but can still work as intended. */
11772 warning (0, "%<__builtin_next_arg%> called without an argument");
11773 return true;
11775 else if (nargs > 1)
11777 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11778 return true;
11780 arg = CALL_EXPR_ARG (exp, 0);
11783 if (TREE_CODE (arg) == SSA_NAME)
11784 arg = SSA_NAME_VAR (arg);
11786 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11787 or __builtin_next_arg (0) the first time we see it, after checking
11788 the arguments and if needed issuing a warning. */
11789 if (!integer_zerop (arg))
11791 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11793 /* Strip off all nops for the sake of the comparison. This
11794 is not quite the same as STRIP_NOPS. It does more.
11795 We must also strip off INDIRECT_EXPR for C++ reference
11796 parameters. */
11797 while (CONVERT_EXPR_P (arg)
11798 || TREE_CODE (arg) == INDIRECT_REF)
11799 arg = TREE_OPERAND (arg, 0);
11800 if (arg != last_parm)
11802 /* FIXME: Sometimes with the tree optimizers we can get the
11803 not the last argument even though the user used the last
11804 argument. We just warn and set the arg to be the last
11805 argument so that we will get wrong-code because of
11806 it. */
11807 warning (0, "second parameter of %<va_start%> not last named argument");
11810 /* Undefined by C99 7.15.1.4p4 (va_start):
11811 "If the parameter parmN is declared with the register storage
11812 class, with a function or array type, or with a type that is
11813 not compatible with the type that results after application of
11814 the default argument promotions, the behavior is undefined."
11816 else if (DECL_REGISTER (arg))
11817 warning (0, "undefined behaviour when second parameter of "
11818 "%<va_start%> is declared with %<register%> storage");
11820 /* We want to verify the second parameter just once before the tree
11821 optimizers are run and then avoid keeping it in the tree,
11822 as otherwise we could warn even for correct code like:
11823 void foo (int i, ...)
11824 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11825 if (va_start_p)
11826 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11827 else
11828 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11830 return false;
11834 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11835 ORIG may be null if this is a 2-argument call. We don't attempt to
11836 simplify calls with more than 3 arguments.
11838 Return NULL_TREE if no simplification was possible, otherwise return the
11839 simplified form of the call as a tree. If IGNORED is true, it means that
11840 the caller does not use the returned value of the function. */
11842 static tree
11843 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11844 tree orig, int ignored)
11846 tree call, retval;
11847 const char *fmt_str = NULL;
11849 /* Verify the required arguments in the original call. We deal with two
11850 types of sprintf() calls: 'sprintf (str, fmt)' and
11851 'sprintf (dest, "%s", orig)'. */
11852 if (!validate_arg (dest, POINTER_TYPE)
11853 || !validate_arg (fmt, POINTER_TYPE))
11854 return NULL_TREE;
11855 if (orig && !validate_arg (orig, POINTER_TYPE))
11856 return NULL_TREE;
11858 /* Check whether the format is a literal string constant. */
11859 fmt_str = c_getstr (fmt);
11860 if (fmt_str == NULL)
11861 return NULL_TREE;
11863 call = NULL_TREE;
11864 retval = NULL_TREE;
11866 if (!init_target_chars ())
11867 return NULL_TREE;
11869 /* If the format doesn't contain % args or %%, use strcpy. */
11870 if (strchr (fmt_str, target_percent) == NULL)
11872 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11874 if (!fn)
11875 return NULL_TREE;
11877 /* Don't optimize sprintf (buf, "abc", ptr++). */
11878 if (orig)
11879 return NULL_TREE;
11881 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11882 'format' is known to contain no % formats. */
11883 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11884 if (!ignored)
11885 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11888 /* If the format is "%s", use strcpy if the result isn't used. */
11889 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11891 tree fn;
11892 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11894 if (!fn)
11895 return NULL_TREE;
11897 /* Don't crash on sprintf (str1, "%s"). */
11898 if (!orig)
11899 return NULL_TREE;
11901 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11902 if (!ignored)
11904 retval = c_strlen (orig, 1);
11905 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11906 return NULL_TREE;
11908 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11911 if (call && retval)
11913 retval = fold_convert_loc
11914 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11915 retval);
11916 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11918 else
11919 return call;
11922 /* Expand a call EXP to __builtin_object_size. */
11925 expand_builtin_object_size (tree exp)
11927 tree ost;
11928 int object_size_type;
11929 tree fndecl = get_callee_fndecl (exp);
11931 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11933 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11934 exp, fndecl);
11935 expand_builtin_trap ();
11936 return const0_rtx;
11939 ost = CALL_EXPR_ARG (exp, 1);
11940 STRIP_NOPS (ost);
11942 if (TREE_CODE (ost) != INTEGER_CST
11943 || tree_int_cst_sgn (ost) < 0
11944 || compare_tree_int (ost, 3) > 0)
11946 error ("%Klast argument of %D is not integer constant between 0 and 3",
11947 exp, fndecl);
11948 expand_builtin_trap ();
11949 return const0_rtx;
11952 object_size_type = tree_low_cst (ost, 0);
11954 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11957 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11958 FCODE is the BUILT_IN_* to use.
11959 Return NULL_RTX if we failed; the caller should emit a normal call,
11960 otherwise try to get the result in TARGET, if convenient (and in
11961 mode MODE if that's convenient). */
11963 static rtx
11964 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11965 enum built_in_function fcode)
11967 tree dest, src, len, size;
11969 if (!validate_arglist (exp,
11970 POINTER_TYPE,
11971 fcode == BUILT_IN_MEMSET_CHK
11972 ? INTEGER_TYPE : POINTER_TYPE,
11973 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11974 return NULL_RTX;
11976 dest = CALL_EXPR_ARG (exp, 0);
11977 src = CALL_EXPR_ARG (exp, 1);
11978 len = CALL_EXPR_ARG (exp, 2);
11979 size = CALL_EXPR_ARG (exp, 3);
11981 if (! host_integerp (size, 1))
11982 return NULL_RTX;
11984 if (host_integerp (len, 1) || integer_all_onesp (size))
11986 tree fn;
11988 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11990 warning_at (tree_nonartificial_location (exp),
11991 0, "%Kcall to %D will always overflow destination buffer",
11992 exp, get_callee_fndecl (exp));
11993 return NULL_RTX;
11996 fn = NULL_TREE;
11997 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11998 mem{cpy,pcpy,move,set} is available. */
11999 switch (fcode)
12001 case BUILT_IN_MEMCPY_CHK:
12002 fn = built_in_decls[BUILT_IN_MEMCPY];
12003 break;
12004 case BUILT_IN_MEMPCPY_CHK:
12005 fn = built_in_decls[BUILT_IN_MEMPCPY];
12006 break;
12007 case BUILT_IN_MEMMOVE_CHK:
12008 fn = built_in_decls[BUILT_IN_MEMMOVE];
12009 break;
12010 case BUILT_IN_MEMSET_CHK:
12011 fn = built_in_decls[BUILT_IN_MEMSET];
12012 break;
12013 default:
12014 break;
12017 if (! fn)
12018 return NULL_RTX;
12020 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12021 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12022 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12023 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12025 else if (fcode == BUILT_IN_MEMSET_CHK)
12026 return NULL_RTX;
12027 else
12029 unsigned int dest_align
12030 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12032 /* If DEST is not a pointer type, call the normal function. */
12033 if (dest_align == 0)
12034 return NULL_RTX;
12036 /* If SRC and DEST are the same (and not volatile), do nothing. */
12037 if (operand_equal_p (src, dest, 0))
12039 tree expr;
12041 if (fcode != BUILT_IN_MEMPCPY_CHK)
12043 /* Evaluate and ignore LEN in case it has side-effects. */
12044 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12045 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12048 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12049 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12052 /* __memmove_chk special case. */
12053 if (fcode == BUILT_IN_MEMMOVE_CHK)
12055 unsigned int src_align
12056 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12058 if (src_align == 0)
12059 return NULL_RTX;
12061 /* If src is categorized for a readonly section we can use
12062 normal __memcpy_chk. */
12063 if (readonly_data_expr (src))
12065 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12066 if (!fn)
12067 return NULL_RTX;
12068 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12069 dest, src, len, size);
12070 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12071 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12072 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12075 return NULL_RTX;
12079 /* Emit warning if a buffer overflow is detected at compile time. */
12081 static void
12082 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12084 int is_strlen = 0;
12085 tree len, size;
12086 location_t loc = tree_nonartificial_location (exp);
12088 switch (fcode)
12090 case BUILT_IN_STRCPY_CHK:
12091 case BUILT_IN_STPCPY_CHK:
12092 /* For __strcat_chk the warning will be emitted only if overflowing
12093 by at least strlen (dest) + 1 bytes. */
12094 case BUILT_IN_STRCAT_CHK:
12095 len = CALL_EXPR_ARG (exp, 1);
12096 size = CALL_EXPR_ARG (exp, 2);
12097 is_strlen = 1;
12098 break;
12099 case BUILT_IN_STRNCAT_CHK:
12100 case BUILT_IN_STRNCPY_CHK:
12101 len = CALL_EXPR_ARG (exp, 2);
12102 size = CALL_EXPR_ARG (exp, 3);
12103 break;
12104 case BUILT_IN_SNPRINTF_CHK:
12105 case BUILT_IN_VSNPRINTF_CHK:
12106 len = CALL_EXPR_ARG (exp, 1);
12107 size = CALL_EXPR_ARG (exp, 3);
12108 break;
12109 default:
12110 gcc_unreachable ();
12113 if (!len || !size)
12114 return;
12116 if (! host_integerp (size, 1) || integer_all_onesp (size))
12117 return;
12119 if (is_strlen)
12121 len = c_strlen (len, 1);
12122 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12123 return;
12125 else if (fcode == BUILT_IN_STRNCAT_CHK)
12127 tree src = CALL_EXPR_ARG (exp, 1);
12128 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12129 return;
12130 src = c_strlen (src, 1);
12131 if (! src || ! host_integerp (src, 1))
12133 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12134 exp, get_callee_fndecl (exp));
12135 return;
12137 else if (tree_int_cst_lt (src, size))
12138 return;
12140 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12141 return;
12143 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12144 exp, get_callee_fndecl (exp));
12147 /* Emit warning if a buffer overflow is detected at compile time
12148 in __sprintf_chk/__vsprintf_chk calls. */
12150 static void
12151 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12153 tree size, len, fmt;
12154 const char *fmt_str;
12155 int nargs = call_expr_nargs (exp);
12157 /* Verify the required arguments in the original call. */
12159 if (nargs < 4)
12160 return;
12161 size = CALL_EXPR_ARG (exp, 2);
12162 fmt = CALL_EXPR_ARG (exp, 3);
12164 if (! host_integerp (size, 1) || integer_all_onesp (size))
12165 return;
12167 /* Check whether the format is a literal string constant. */
12168 fmt_str = c_getstr (fmt);
12169 if (fmt_str == NULL)
12170 return;
12172 if (!init_target_chars ())
12173 return;
12175 /* If the format doesn't contain % args or %%, we know its size. */
12176 if (strchr (fmt_str, target_percent) == 0)
12177 len = build_int_cstu (size_type_node, strlen (fmt_str));
12178 /* If the format is "%s" and first ... argument is a string literal,
12179 we know it too. */
12180 else if (fcode == BUILT_IN_SPRINTF_CHK
12181 && strcmp (fmt_str, target_percent_s) == 0)
12183 tree arg;
12185 if (nargs < 5)
12186 return;
12187 arg = CALL_EXPR_ARG (exp, 4);
12188 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12189 return;
12191 len = c_strlen (arg, 1);
12192 if (!len || ! host_integerp (len, 1))
12193 return;
12195 else
12196 return;
12198 if (! tree_int_cst_lt (len, size))
12199 warning_at (tree_nonartificial_location (exp),
12200 0, "%Kcall to %D will always overflow destination buffer",
12201 exp, get_callee_fndecl (exp));
12204 /* Emit warning if a free is called with address of a variable. */
12206 static void
12207 maybe_emit_free_warning (tree exp)
12209 tree arg = CALL_EXPR_ARG (exp, 0);
12211 STRIP_NOPS (arg);
12212 if (TREE_CODE (arg) != ADDR_EXPR)
12213 return;
12215 arg = get_base_address (TREE_OPERAND (arg, 0));
12216 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12217 return;
12219 if (SSA_VAR_P (arg))
12220 warning_at (tree_nonartificial_location (exp),
12221 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12222 else
12223 warning_at (tree_nonartificial_location (exp),
12224 0, "%Kattempt to free a non-heap object", exp);
12227 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12228 if possible. */
12230 tree
12231 fold_builtin_object_size (tree ptr, tree ost)
12233 unsigned HOST_WIDE_INT bytes;
12234 int object_size_type;
12236 if (!validate_arg (ptr, POINTER_TYPE)
12237 || !validate_arg (ost, INTEGER_TYPE))
12238 return NULL_TREE;
12240 STRIP_NOPS (ost);
12242 if (TREE_CODE (ost) != INTEGER_CST
12243 || tree_int_cst_sgn (ost) < 0
12244 || compare_tree_int (ost, 3) > 0)
12245 return NULL_TREE;
12247 object_size_type = tree_low_cst (ost, 0);
12249 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12250 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12251 and (size_t) 0 for types 2 and 3. */
12252 if (TREE_SIDE_EFFECTS (ptr))
12253 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12255 if (TREE_CODE (ptr) == ADDR_EXPR)
12257 bytes = compute_builtin_object_size (ptr, object_size_type);
12258 if (double_int_fits_to_tree_p (size_type_node,
12259 uhwi_to_double_int (bytes)))
12260 return build_int_cstu (size_type_node, bytes);
12262 else if (TREE_CODE (ptr) == SSA_NAME)
12264 /* If object size is not known yet, delay folding until
12265 later. Maybe subsequent passes will help determining
12266 it. */
12267 bytes = compute_builtin_object_size (ptr, object_size_type);
12268 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12269 && double_int_fits_to_tree_p (size_type_node,
12270 uhwi_to_double_int (bytes)))
12271 return build_int_cstu (size_type_node, bytes);
12274 return NULL_TREE;
12277 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12278 DEST, SRC, LEN, and SIZE are the arguments to the call.
12279 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12280 code of the builtin. If MAXLEN is not NULL, it is maximum length
12281 passed as third argument. */
12283 tree
12284 fold_builtin_memory_chk (location_t loc, tree fndecl,
12285 tree dest, tree src, tree len, tree size,
12286 tree maxlen, bool ignore,
12287 enum built_in_function fcode)
12289 tree fn;
12291 if (!validate_arg (dest, POINTER_TYPE)
12292 || !validate_arg (src,
12293 (fcode == BUILT_IN_MEMSET_CHK
12294 ? INTEGER_TYPE : POINTER_TYPE))
12295 || !validate_arg (len, INTEGER_TYPE)
12296 || !validate_arg (size, INTEGER_TYPE))
12297 return NULL_TREE;
12299 /* If SRC and DEST are the same (and not volatile), return DEST
12300 (resp. DEST+LEN for __mempcpy_chk). */
12301 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12303 if (fcode != BUILT_IN_MEMPCPY_CHK)
12304 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12305 dest, len);
12306 else
12308 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12309 dest, len);
12310 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12314 if (! host_integerp (size, 1))
12315 return NULL_TREE;
12317 if (! integer_all_onesp (size))
12319 if (! host_integerp (len, 1))
12321 /* If LEN is not constant, try MAXLEN too.
12322 For MAXLEN only allow optimizing into non-_ocs function
12323 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12324 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12326 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12328 /* (void) __mempcpy_chk () can be optimized into
12329 (void) __memcpy_chk (). */
12330 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12331 if (!fn)
12332 return NULL_TREE;
12334 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12336 return NULL_TREE;
12339 else
12340 maxlen = len;
12342 if (tree_int_cst_lt (size, maxlen))
12343 return NULL_TREE;
12346 fn = NULL_TREE;
12347 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12348 mem{cpy,pcpy,move,set} is available. */
12349 switch (fcode)
12351 case BUILT_IN_MEMCPY_CHK:
12352 fn = built_in_decls[BUILT_IN_MEMCPY];
12353 break;
12354 case BUILT_IN_MEMPCPY_CHK:
12355 fn = built_in_decls[BUILT_IN_MEMPCPY];
12356 break;
12357 case BUILT_IN_MEMMOVE_CHK:
12358 fn = built_in_decls[BUILT_IN_MEMMOVE];
12359 break;
12360 case BUILT_IN_MEMSET_CHK:
12361 fn = built_in_decls[BUILT_IN_MEMSET];
12362 break;
12363 default:
12364 break;
12367 if (!fn)
12368 return NULL_TREE;
12370 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12373 /* Fold a call to the __st[rp]cpy_chk builtin.
12374 DEST, SRC, and SIZE are the arguments to the call.
12375 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12376 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12377 strings passed as second argument. */
12379 tree
12380 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12381 tree src, tree size,
12382 tree maxlen, bool ignore,
12383 enum built_in_function fcode)
12385 tree len, fn;
12387 if (!validate_arg (dest, POINTER_TYPE)
12388 || !validate_arg (src, POINTER_TYPE)
12389 || !validate_arg (size, INTEGER_TYPE))
12390 return NULL_TREE;
12392 /* If SRC and DEST are the same (and not volatile), return DEST. */
12393 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12394 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12396 if (! host_integerp (size, 1))
12397 return NULL_TREE;
12399 if (! integer_all_onesp (size))
12401 len = c_strlen (src, 1);
12402 if (! len || ! host_integerp (len, 1))
12404 /* If LEN is not constant, try MAXLEN too.
12405 For MAXLEN only allow optimizing into non-_ocs function
12406 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12407 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12409 if (fcode == BUILT_IN_STPCPY_CHK)
12411 if (! ignore)
12412 return NULL_TREE;
12414 /* If return value of __stpcpy_chk is ignored,
12415 optimize into __strcpy_chk. */
12416 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12417 if (!fn)
12418 return NULL_TREE;
12420 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12423 if (! len || TREE_SIDE_EFFECTS (len))
12424 return NULL_TREE;
12426 /* If c_strlen returned something, but not a constant,
12427 transform __strcpy_chk into __memcpy_chk. */
12428 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12429 if (!fn)
12430 return NULL_TREE;
12432 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12433 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12434 build_call_expr_loc (loc, fn, 4,
12435 dest, src, len, size));
12438 else
12439 maxlen = len;
12441 if (! tree_int_cst_lt (maxlen, size))
12442 return NULL_TREE;
12445 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12446 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12447 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12448 if (!fn)
12449 return NULL_TREE;
12451 return build_call_expr_loc (loc, fn, 2, dest, src);
12454 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12455 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12456 length passed as third argument. */
12458 tree
12459 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12460 tree len, tree size, tree maxlen)
12462 tree fn;
12464 if (!validate_arg (dest, POINTER_TYPE)
12465 || !validate_arg (src, POINTER_TYPE)
12466 || !validate_arg (len, INTEGER_TYPE)
12467 || !validate_arg (size, INTEGER_TYPE))
12468 return NULL_TREE;
12470 if (! host_integerp (size, 1))
12471 return NULL_TREE;
12473 if (! integer_all_onesp (size))
12475 if (! host_integerp (len, 1))
12477 /* If LEN is not constant, try MAXLEN too.
12478 For MAXLEN only allow optimizing into non-_ocs function
12479 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12480 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12481 return NULL_TREE;
12483 else
12484 maxlen = len;
12486 if (tree_int_cst_lt (size, maxlen))
12487 return NULL_TREE;
12490 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12491 fn = built_in_decls[BUILT_IN_STRNCPY];
12492 if (!fn)
12493 return NULL_TREE;
12495 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12498 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12499 are the arguments to the call. */
12501 static tree
12502 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12503 tree src, tree size)
12505 tree fn;
12506 const char *p;
12508 if (!validate_arg (dest, POINTER_TYPE)
12509 || !validate_arg (src, POINTER_TYPE)
12510 || !validate_arg (size, INTEGER_TYPE))
12511 return NULL_TREE;
12513 p = c_getstr (src);
12514 /* If the SRC parameter is "", return DEST. */
12515 if (p && *p == '\0')
12516 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12518 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12519 return NULL_TREE;
12521 /* If __builtin_strcat_chk is used, assume strcat is available. */
12522 fn = built_in_decls[BUILT_IN_STRCAT];
12523 if (!fn)
12524 return NULL_TREE;
12526 return build_call_expr_loc (loc, fn, 2, dest, src);
12529 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12530 LEN, and SIZE. */
12532 static tree
12533 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12534 tree dest, tree src, tree len, tree size)
12536 tree fn;
12537 const char *p;
12539 if (!validate_arg (dest, POINTER_TYPE)
12540 || !validate_arg (src, POINTER_TYPE)
12541 || !validate_arg (size, INTEGER_TYPE)
12542 || !validate_arg (size, INTEGER_TYPE))
12543 return NULL_TREE;
12545 p = c_getstr (src);
12546 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12547 if (p && *p == '\0')
12548 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12549 else if (integer_zerop (len))
12550 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12552 if (! host_integerp (size, 1))
12553 return NULL_TREE;
12555 if (! integer_all_onesp (size))
12557 tree src_len = c_strlen (src, 1);
12558 if (src_len
12559 && host_integerp (src_len, 1)
12560 && host_integerp (len, 1)
12561 && ! tree_int_cst_lt (len, src_len))
12563 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12564 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12565 if (!fn)
12566 return NULL_TREE;
12568 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12570 return NULL_TREE;
12573 /* If __builtin_strncat_chk is used, assume strncat is available. */
12574 fn = built_in_decls[BUILT_IN_STRNCAT];
12575 if (!fn)
12576 return NULL_TREE;
12578 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12581 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12582 Return NULL_TREE if a normal call should be emitted rather than
12583 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12584 or BUILT_IN_VSPRINTF_CHK. */
12586 static tree
12587 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12588 enum built_in_function fcode)
12590 tree dest, size, len, fn, fmt, flag;
12591 const char *fmt_str;
12593 /* Verify the required arguments in the original call. */
12594 if (nargs < 4)
12595 return NULL_TREE;
12596 dest = args[0];
12597 if (!validate_arg (dest, POINTER_TYPE))
12598 return NULL_TREE;
12599 flag = args[1];
12600 if (!validate_arg (flag, INTEGER_TYPE))
12601 return NULL_TREE;
12602 size = args[2];
12603 if (!validate_arg (size, INTEGER_TYPE))
12604 return NULL_TREE;
12605 fmt = args[3];
12606 if (!validate_arg (fmt, POINTER_TYPE))
12607 return NULL_TREE;
12609 if (! host_integerp (size, 1))
12610 return NULL_TREE;
12612 len = NULL_TREE;
12614 if (!init_target_chars ())
12615 return NULL_TREE;
12617 /* Check whether the format is a literal string constant. */
12618 fmt_str = c_getstr (fmt);
12619 if (fmt_str != NULL)
12621 /* If the format doesn't contain % args or %%, we know the size. */
12622 if (strchr (fmt_str, target_percent) == 0)
12624 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12625 len = build_int_cstu (size_type_node, strlen (fmt_str));
12627 /* If the format is "%s" and first ... argument is a string literal,
12628 we know the size too. */
12629 else if (fcode == BUILT_IN_SPRINTF_CHK
12630 && strcmp (fmt_str, target_percent_s) == 0)
12632 tree arg;
12634 if (nargs == 5)
12636 arg = args[4];
12637 if (validate_arg (arg, POINTER_TYPE))
12639 len = c_strlen (arg, 1);
12640 if (! len || ! host_integerp (len, 1))
12641 len = NULL_TREE;
12647 if (! integer_all_onesp (size))
12649 if (! len || ! tree_int_cst_lt (len, size))
12650 return NULL_TREE;
12653 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12654 or if format doesn't contain % chars or is "%s". */
12655 if (! integer_zerop (flag))
12657 if (fmt_str == NULL)
12658 return NULL_TREE;
12659 if (strchr (fmt_str, target_percent) != NULL
12660 && strcmp (fmt_str, target_percent_s))
12661 return NULL_TREE;
12664 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12665 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12666 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12667 if (!fn)
12668 return NULL_TREE;
12670 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12673 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12674 a normal call should be emitted rather than expanding the function
12675 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12677 static tree
12678 fold_builtin_sprintf_chk (location_t loc, tree exp,
12679 enum built_in_function fcode)
12681 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12682 CALL_EXPR_ARGP (exp), fcode);
12685 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12686 NULL_TREE if a normal call should be emitted rather than expanding
12687 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12688 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12689 passed as second argument. */
12691 static tree
12692 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12693 tree maxlen, enum built_in_function fcode)
12695 tree dest, size, len, fn, fmt, flag;
12696 const char *fmt_str;
12698 /* Verify the required arguments in the original call. */
12699 if (nargs < 5)
12700 return NULL_TREE;
12701 dest = args[0];
12702 if (!validate_arg (dest, POINTER_TYPE))
12703 return NULL_TREE;
12704 len = args[1];
12705 if (!validate_arg (len, INTEGER_TYPE))
12706 return NULL_TREE;
12707 flag = args[2];
12708 if (!validate_arg (flag, INTEGER_TYPE))
12709 return NULL_TREE;
12710 size = args[3];
12711 if (!validate_arg (size, INTEGER_TYPE))
12712 return NULL_TREE;
12713 fmt = args[4];
12714 if (!validate_arg (fmt, POINTER_TYPE))
12715 return NULL_TREE;
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))
12728 return NULL_TREE;
12730 else
12731 maxlen = len;
12733 if (tree_int_cst_lt (size, maxlen))
12734 return NULL_TREE;
12737 if (!init_target_chars ())
12738 return NULL_TREE;
12740 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12741 or if format doesn't contain % chars or is "%s". */
12742 if (! integer_zerop (flag))
12744 fmt_str = c_getstr (fmt);
12745 if (fmt_str == NULL)
12746 return NULL_TREE;
12747 if (strchr (fmt_str, target_percent) != NULL
12748 && strcmp (fmt_str, target_percent_s))
12749 return NULL_TREE;
12752 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12753 available. */
12754 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12755 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12756 if (!fn)
12757 return NULL_TREE;
12759 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12762 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12763 a normal call should be emitted rather than expanding the function
12764 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12765 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12766 passed as second argument. */
12768 tree
12769 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12770 enum built_in_function fcode)
12772 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12773 CALL_EXPR_ARGP (exp), maxlen, fcode);
12776 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12777 FMT and ARG are the arguments to the call; we don't fold cases with
12778 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12780 Return NULL_TREE if no simplification was possible, otherwise return the
12781 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12782 code of the function to be simplified. */
12784 static tree
12785 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12786 tree arg, bool ignore,
12787 enum built_in_function fcode)
12789 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12790 const char *fmt_str = NULL;
12792 /* If the return value is used, don't do the transformation. */
12793 if (! ignore)
12794 return NULL_TREE;
12796 /* Verify the required arguments in the original call. */
12797 if (!validate_arg (fmt, POINTER_TYPE))
12798 return NULL_TREE;
12800 /* Check whether the format is a literal string constant. */
12801 fmt_str = c_getstr (fmt);
12802 if (fmt_str == NULL)
12803 return NULL_TREE;
12805 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12807 /* If we're using an unlocked function, assume the other
12808 unlocked functions exist explicitly. */
12809 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12810 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12812 else
12814 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12815 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12818 if (!init_target_chars ())
12819 return NULL_TREE;
12821 if (strcmp (fmt_str, target_percent_s) == 0
12822 || strchr (fmt_str, target_percent) == NULL)
12824 const char *str;
12826 if (strcmp (fmt_str, target_percent_s) == 0)
12828 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12829 return NULL_TREE;
12831 if (!arg || !validate_arg (arg, POINTER_TYPE))
12832 return NULL_TREE;
12834 str = c_getstr (arg);
12835 if (str == NULL)
12836 return NULL_TREE;
12838 else
12840 /* The format specifier doesn't contain any '%' characters. */
12841 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12842 && arg)
12843 return NULL_TREE;
12844 str = fmt_str;
12847 /* If the string was "", printf does nothing. */
12848 if (str[0] == '\0')
12849 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12851 /* If the string has length of 1, call putchar. */
12852 if (str[1] == '\0')
12854 /* Given printf("c"), (where c is any one character,)
12855 convert "c"[0] to an int and pass that to the replacement
12856 function. */
12857 newarg = build_int_cst (NULL_TREE, str[0]);
12858 if (fn_putchar)
12859 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12861 else
12863 /* If the string was "string\n", call puts("string"). */
12864 size_t len = strlen (str);
12865 if ((unsigned char)str[len - 1] == target_newline
12866 && (size_t) (int) len == len
12867 && (int) len > 0)
12869 char *newstr;
12870 tree offset_node, string_cst;
12872 /* Create a NUL-terminated string that's one char shorter
12873 than the original, stripping off the trailing '\n'. */
12874 newarg = build_string_literal (len, str);
12875 string_cst = string_constant (newarg, &offset_node);
12876 gcc_checking_assert (string_cst
12877 && (TREE_STRING_LENGTH (string_cst)
12878 == (int) len)
12879 && integer_zerop (offset_node)
12880 && (unsigned char)
12881 TREE_STRING_POINTER (string_cst)[len - 1]
12882 == target_newline);
12883 /* build_string_literal creates a new STRING_CST,
12884 modify it in place to avoid double copying. */
12885 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
12886 newstr[len - 1] = '\0';
12887 if (fn_puts)
12888 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12890 else
12891 /* We'd like to arrange to call fputs(string,stdout) here,
12892 but we need stdout and don't have a way to get it yet. */
12893 return NULL_TREE;
12897 /* The other optimizations can be done only on the non-va_list variants. */
12898 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12899 return NULL_TREE;
12901 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12902 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12904 if (!arg || !validate_arg (arg, POINTER_TYPE))
12905 return NULL_TREE;
12906 if (fn_puts)
12907 call = build_call_expr_loc (loc, fn_puts, 1, arg);
12910 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12911 else if (strcmp (fmt_str, target_percent_c) == 0)
12913 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12914 return NULL_TREE;
12915 if (fn_putchar)
12916 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
12919 if (!call)
12920 return NULL_TREE;
12922 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12925 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12926 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12927 more than 3 arguments, and ARG may be null in the 2-argument case.
12929 Return NULL_TREE if no simplification was possible, otherwise return the
12930 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12931 code of the function to be simplified. */
12933 static tree
12934 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
12935 tree fmt, tree arg, bool ignore,
12936 enum built_in_function fcode)
12938 tree fn_fputc, fn_fputs, call = NULL_TREE;
12939 const char *fmt_str = NULL;
12941 /* If the return value is used, don't do the transformation. */
12942 if (! ignore)
12943 return NULL_TREE;
12945 /* Verify the required arguments in the original call. */
12946 if (!validate_arg (fp, POINTER_TYPE))
12947 return NULL_TREE;
12948 if (!validate_arg (fmt, POINTER_TYPE))
12949 return NULL_TREE;
12951 /* Check whether the format is a literal string constant. */
12952 fmt_str = c_getstr (fmt);
12953 if (fmt_str == NULL)
12954 return NULL_TREE;
12956 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12958 /* If we're using an unlocked function, assume the other
12959 unlocked functions exist explicitly. */
12960 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12961 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12963 else
12965 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12966 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12969 if (!init_target_chars ())
12970 return NULL_TREE;
12972 /* If the format doesn't contain % args or %%, use strcpy. */
12973 if (strchr (fmt_str, target_percent) == NULL)
12975 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12976 && arg)
12977 return NULL_TREE;
12979 /* If the format specifier was "", fprintf does nothing. */
12980 if (fmt_str[0] == '\0')
12982 /* If FP has side-effects, just wait until gimplification is
12983 done. */
12984 if (TREE_SIDE_EFFECTS (fp))
12985 return NULL_TREE;
12987 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12990 /* When "string" doesn't contain %, replace all cases of
12991 fprintf (fp, string) with fputs (string, fp). The fputs
12992 builtin will take care of special cases like length == 1. */
12993 if (fn_fputs)
12994 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
12997 /* The other optimizations can be done only on the non-va_list variants. */
12998 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12999 return NULL_TREE;
13001 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13002 else if (strcmp (fmt_str, target_percent_s) == 0)
13004 if (!arg || !validate_arg (arg, POINTER_TYPE))
13005 return NULL_TREE;
13006 if (fn_fputs)
13007 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13010 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13011 else if (strcmp (fmt_str, target_percent_c) == 0)
13013 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13014 return NULL_TREE;
13015 if (fn_fputc)
13016 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13019 if (!call)
13020 return NULL_TREE;
13021 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13024 /* Initialize format string characters in the target charset. */
13026 static bool
13027 init_target_chars (void)
13029 static bool init;
13030 if (!init)
13032 target_newline = lang_hooks.to_target_charset ('\n');
13033 target_percent = lang_hooks.to_target_charset ('%');
13034 target_c = lang_hooks.to_target_charset ('c');
13035 target_s = lang_hooks.to_target_charset ('s');
13036 if (target_newline == 0 || target_percent == 0 || target_c == 0
13037 || target_s == 0)
13038 return false;
13040 target_percent_c[0] = target_percent;
13041 target_percent_c[1] = target_c;
13042 target_percent_c[2] = '\0';
13044 target_percent_s[0] = target_percent;
13045 target_percent_s[1] = target_s;
13046 target_percent_s[2] = '\0';
13048 target_percent_s_newline[0] = target_percent;
13049 target_percent_s_newline[1] = target_s;
13050 target_percent_s_newline[2] = target_newline;
13051 target_percent_s_newline[3] = '\0';
13053 init = true;
13055 return true;
13058 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13059 and no overflow/underflow occurred. INEXACT is true if M was not
13060 exactly calculated. TYPE is the tree type for the result. This
13061 function assumes that you cleared the MPFR flags and then
13062 calculated M to see if anything subsequently set a flag prior to
13063 entering this function. Return NULL_TREE if any checks fail. */
13065 static tree
13066 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13068 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13069 overflow/underflow occurred. If -frounding-math, proceed iff the
13070 result of calling FUNC was exact. */
13071 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13072 && (!flag_rounding_math || !inexact))
13074 REAL_VALUE_TYPE rr;
13076 real_from_mpfr (&rr, m, type, GMP_RNDN);
13077 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13078 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13079 but the mpft_t is not, then we underflowed in the
13080 conversion. */
13081 if (real_isfinite (&rr)
13082 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13084 REAL_VALUE_TYPE rmode;
13086 real_convert (&rmode, TYPE_MODE (type), &rr);
13087 /* Proceed iff the specified mode can hold the value. */
13088 if (real_identical (&rmode, &rr))
13089 return build_real (type, rmode);
13092 return NULL_TREE;
13095 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13096 number and no overflow/underflow occurred. INEXACT is true if M
13097 was not exactly calculated. TYPE is the tree type for the result.
13098 This function assumes that you cleared the MPFR flags and then
13099 calculated M to see if anything subsequently set a flag prior to
13100 entering this function. Return NULL_TREE if any checks fail, if
13101 FORCE_CONVERT is true, then bypass the checks. */
13103 static tree
13104 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13106 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13107 overflow/underflow occurred. If -frounding-math, proceed iff the
13108 result of calling FUNC was exact. */
13109 if (force_convert
13110 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13111 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13112 && (!flag_rounding_math || !inexact)))
13114 REAL_VALUE_TYPE re, im;
13116 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13117 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13118 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13119 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13120 but the mpft_t is not, then we underflowed in the
13121 conversion. */
13122 if (force_convert
13123 || (real_isfinite (&re) && real_isfinite (&im)
13124 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13125 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13127 REAL_VALUE_TYPE re_mode, im_mode;
13129 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13130 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13131 /* Proceed iff the specified mode can hold the value. */
13132 if (force_convert
13133 || (real_identical (&re_mode, &re)
13134 && real_identical (&im_mode, &im)))
13135 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13136 build_real (TREE_TYPE (type), im_mode));
13139 return NULL_TREE;
13142 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13143 FUNC on it and return the resulting value as a tree with type TYPE.
13144 If MIN and/or MAX are not NULL, then the supplied ARG must be
13145 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13146 acceptable values, otherwise they are not. The mpfr precision is
13147 set to the precision of TYPE. We assume that function FUNC returns
13148 zero if the result could be calculated exactly within the requested
13149 precision. */
13151 static tree
13152 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13153 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13154 bool inclusive)
13156 tree result = NULL_TREE;
13158 STRIP_NOPS (arg);
13160 /* To proceed, MPFR must exactly represent the target floating point
13161 format, which only happens when the target base equals two. */
13162 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13163 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13165 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13167 if (real_isfinite (ra)
13168 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13169 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13171 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13172 const int prec = fmt->p;
13173 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13174 int inexact;
13175 mpfr_t m;
13177 mpfr_init2 (m, prec);
13178 mpfr_from_real (m, ra, GMP_RNDN);
13179 mpfr_clear_flags ();
13180 inexact = func (m, m, rnd);
13181 result = do_mpfr_ckconv (m, type, inexact);
13182 mpfr_clear (m);
13186 return result;
13189 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13190 FUNC on it and return the resulting value as a tree with type TYPE.
13191 The mpfr precision is set to the precision of TYPE. We assume that
13192 function FUNC returns zero if the result could be calculated
13193 exactly within the requested precision. */
13195 static tree
13196 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13197 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13199 tree result = NULL_TREE;
13201 STRIP_NOPS (arg1);
13202 STRIP_NOPS (arg2);
13204 /* To proceed, MPFR must exactly represent the target floating point
13205 format, which only happens when the target base equals two. */
13206 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13207 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13208 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13210 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13211 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13213 if (real_isfinite (ra1) && real_isfinite (ra2))
13215 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13216 const int prec = fmt->p;
13217 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13218 int inexact;
13219 mpfr_t m1, m2;
13221 mpfr_inits2 (prec, m1, m2, NULL);
13222 mpfr_from_real (m1, ra1, GMP_RNDN);
13223 mpfr_from_real (m2, ra2, GMP_RNDN);
13224 mpfr_clear_flags ();
13225 inexact = func (m1, m1, m2, rnd);
13226 result = do_mpfr_ckconv (m1, type, inexact);
13227 mpfr_clears (m1, m2, NULL);
13231 return result;
13234 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13235 FUNC on it and return the resulting value as a tree with type TYPE.
13236 The mpfr precision is set to the precision of TYPE. We assume that
13237 function FUNC returns zero if the result could be calculated
13238 exactly within the requested precision. */
13240 static tree
13241 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13242 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13244 tree result = NULL_TREE;
13246 STRIP_NOPS (arg1);
13247 STRIP_NOPS (arg2);
13248 STRIP_NOPS (arg3);
13250 /* To proceed, MPFR must exactly represent the target floating point
13251 format, which only happens when the target base equals two. */
13252 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13253 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13254 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13255 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13257 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13258 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13259 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13261 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13263 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13264 const int prec = fmt->p;
13265 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13266 int inexact;
13267 mpfr_t m1, m2, m3;
13269 mpfr_inits2 (prec, m1, m2, m3, NULL);
13270 mpfr_from_real (m1, ra1, GMP_RNDN);
13271 mpfr_from_real (m2, ra2, GMP_RNDN);
13272 mpfr_from_real (m3, ra3, GMP_RNDN);
13273 mpfr_clear_flags ();
13274 inexact = func (m1, m1, m2, m3, rnd);
13275 result = do_mpfr_ckconv (m1, type, inexact);
13276 mpfr_clears (m1, m2, m3, NULL);
13280 return result;
13283 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13284 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13285 If ARG_SINP and ARG_COSP are NULL then the result is returned
13286 as a complex value.
13287 The type is taken from the type of ARG and is used for setting the
13288 precision of the calculation and results. */
13290 static tree
13291 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13293 tree const type = TREE_TYPE (arg);
13294 tree result = NULL_TREE;
13296 STRIP_NOPS (arg);
13298 /* To proceed, MPFR must exactly represent the target floating point
13299 format, which only happens when the target base equals two. */
13300 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13301 && TREE_CODE (arg) == REAL_CST
13302 && !TREE_OVERFLOW (arg))
13304 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13306 if (real_isfinite (ra))
13308 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13309 const int prec = fmt->p;
13310 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13311 tree result_s, result_c;
13312 int inexact;
13313 mpfr_t m, ms, mc;
13315 mpfr_inits2 (prec, m, ms, mc, NULL);
13316 mpfr_from_real (m, ra, GMP_RNDN);
13317 mpfr_clear_flags ();
13318 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13319 result_s = do_mpfr_ckconv (ms, type, inexact);
13320 result_c = do_mpfr_ckconv (mc, type, inexact);
13321 mpfr_clears (m, ms, mc, NULL);
13322 if (result_s && result_c)
13324 /* If we are to return in a complex value do so. */
13325 if (!arg_sinp && !arg_cosp)
13326 return build_complex (build_complex_type (type),
13327 result_c, result_s);
13329 /* Dereference the sin/cos pointer arguments. */
13330 arg_sinp = build_fold_indirect_ref (arg_sinp);
13331 arg_cosp = build_fold_indirect_ref (arg_cosp);
13332 /* Proceed if valid pointer type were passed in. */
13333 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13334 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13336 /* Set the values. */
13337 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13338 result_s);
13339 TREE_SIDE_EFFECTS (result_s) = 1;
13340 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13341 result_c);
13342 TREE_SIDE_EFFECTS (result_c) = 1;
13343 /* Combine the assignments into a compound expr. */
13344 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13345 result_s, result_c));
13350 return result;
13353 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13354 two-argument mpfr order N Bessel function FUNC on them and return
13355 the resulting value as a tree with type TYPE. The mpfr precision
13356 is set to the precision of TYPE. We assume that function FUNC
13357 returns zero if the result could be calculated exactly within the
13358 requested precision. */
13359 static tree
13360 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13361 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13362 const REAL_VALUE_TYPE *min, bool inclusive)
13364 tree result = NULL_TREE;
13366 STRIP_NOPS (arg1);
13367 STRIP_NOPS (arg2);
13369 /* To proceed, MPFR must exactly represent the target floating point
13370 format, which only happens when the target base equals two. */
13371 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13372 && host_integerp (arg1, 0)
13373 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13375 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13376 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13378 if (n == (long)n
13379 && real_isfinite (ra)
13380 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13382 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13383 const int prec = fmt->p;
13384 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13385 int inexact;
13386 mpfr_t m;
13388 mpfr_init2 (m, prec);
13389 mpfr_from_real (m, ra, GMP_RNDN);
13390 mpfr_clear_flags ();
13391 inexact = func (m, n, m, rnd);
13392 result = do_mpfr_ckconv (m, type, inexact);
13393 mpfr_clear (m);
13397 return result;
13400 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13401 the pointer *(ARG_QUO) and return the result. The type is taken
13402 from the type of ARG0 and is used for setting the precision of the
13403 calculation and results. */
13405 static tree
13406 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13408 tree const type = TREE_TYPE (arg0);
13409 tree result = NULL_TREE;
13411 STRIP_NOPS (arg0);
13412 STRIP_NOPS (arg1);
13414 /* To proceed, MPFR must exactly represent the target floating point
13415 format, which only happens when the target base equals two. */
13416 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13417 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13418 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13420 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13421 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13423 if (real_isfinite (ra0) && real_isfinite (ra1))
13425 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13426 const int prec = fmt->p;
13427 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13428 tree result_rem;
13429 long integer_quo;
13430 mpfr_t m0, m1;
13432 mpfr_inits2 (prec, m0, m1, NULL);
13433 mpfr_from_real (m0, ra0, GMP_RNDN);
13434 mpfr_from_real (m1, ra1, GMP_RNDN);
13435 mpfr_clear_flags ();
13436 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13437 /* Remquo is independent of the rounding mode, so pass
13438 inexact=0 to do_mpfr_ckconv(). */
13439 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13440 mpfr_clears (m0, m1, NULL);
13441 if (result_rem)
13443 /* MPFR calculates quo in the host's long so it may
13444 return more bits in quo than the target int can hold
13445 if sizeof(host long) > sizeof(target int). This can
13446 happen even for native compilers in LP64 mode. In
13447 these cases, modulo the quo value with the largest
13448 number that the target int can hold while leaving one
13449 bit for the sign. */
13450 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13451 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13453 /* Dereference the quo pointer argument. */
13454 arg_quo = build_fold_indirect_ref (arg_quo);
13455 /* Proceed iff a valid pointer type was passed in. */
13456 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13458 /* Set the value. */
13459 tree result_quo = fold_build2 (MODIFY_EXPR,
13460 TREE_TYPE (arg_quo), arg_quo,
13461 build_int_cst (NULL, integer_quo));
13462 TREE_SIDE_EFFECTS (result_quo) = 1;
13463 /* Combine the quo assignment with the rem. */
13464 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13465 result_quo, result_rem));
13470 return result;
13473 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13474 resulting value as a tree with type TYPE. The mpfr precision is
13475 set to the precision of TYPE. We assume that this mpfr function
13476 returns zero if the result could be calculated exactly within the
13477 requested precision. In addition, the integer pointer represented
13478 by ARG_SG will be dereferenced and set to the appropriate signgam
13479 (-1,1) value. */
13481 static tree
13482 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13484 tree result = NULL_TREE;
13486 STRIP_NOPS (arg);
13488 /* To proceed, MPFR must exactly represent the target floating point
13489 format, which only happens when the target base equals two. Also
13490 verify ARG is a constant and that ARG_SG is an int pointer. */
13491 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13492 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13493 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13494 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13496 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13498 /* In addition to NaN and Inf, the argument cannot be zero or a
13499 negative integer. */
13500 if (real_isfinite (ra)
13501 && ra->cl != rvc_zero
13502 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13504 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13505 const int prec = fmt->p;
13506 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13507 int inexact, sg;
13508 mpfr_t m;
13509 tree result_lg;
13511 mpfr_init2 (m, prec);
13512 mpfr_from_real (m, ra, GMP_RNDN);
13513 mpfr_clear_flags ();
13514 inexact = mpfr_lgamma (m, &sg, m, rnd);
13515 result_lg = do_mpfr_ckconv (m, type, inexact);
13516 mpfr_clear (m);
13517 if (result_lg)
13519 tree result_sg;
13521 /* Dereference the arg_sg pointer argument. */
13522 arg_sg = build_fold_indirect_ref (arg_sg);
13523 /* Assign the signgam value into *arg_sg. */
13524 result_sg = fold_build2 (MODIFY_EXPR,
13525 TREE_TYPE (arg_sg), arg_sg,
13526 build_int_cst (NULL, sg));
13527 TREE_SIDE_EFFECTS (result_sg) = 1;
13528 /* Combine the signgam assignment with the lgamma result. */
13529 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13530 result_sg, result_lg));
13535 return result;
13538 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13539 function FUNC on it and return the resulting value as a tree with
13540 type TYPE. The mpfr precision is set to the precision of TYPE. We
13541 assume that function FUNC returns zero if the result could be
13542 calculated exactly within the requested precision. */
13544 static tree
13545 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13547 tree result = NULL_TREE;
13549 STRIP_NOPS (arg);
13551 /* To proceed, MPFR must exactly represent the target floating point
13552 format, which only happens when the target base equals two. */
13553 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13554 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13555 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13557 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13558 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13560 if (real_isfinite (re) && real_isfinite (im))
13562 const struct real_format *const fmt =
13563 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13564 const int prec = fmt->p;
13565 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13566 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13567 int inexact;
13568 mpc_t m;
13570 mpc_init2 (m, prec);
13571 mpfr_from_real (mpc_realref(m), re, rnd);
13572 mpfr_from_real (mpc_imagref(m), im, rnd);
13573 mpfr_clear_flags ();
13574 inexact = func (m, m, crnd);
13575 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13576 mpc_clear (m);
13580 return result;
13583 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13584 mpc function FUNC on it and return the resulting value as a tree
13585 with type TYPE. The mpfr precision is set to the precision of
13586 TYPE. We assume that function FUNC returns zero if the result
13587 could be calculated exactly within the requested precision. If
13588 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13589 in the arguments and/or results. */
13591 tree
13592 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13593 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13595 tree result = NULL_TREE;
13597 STRIP_NOPS (arg0);
13598 STRIP_NOPS (arg1);
13600 /* To proceed, MPFR must exactly represent the target floating point
13601 format, which only happens when the target base equals two. */
13602 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13603 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13604 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13605 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13606 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13608 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13609 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13610 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13611 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13613 if (do_nonfinite
13614 || (real_isfinite (re0) && real_isfinite (im0)
13615 && real_isfinite (re1) && real_isfinite (im1)))
13617 const struct real_format *const fmt =
13618 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13619 const int prec = fmt->p;
13620 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13621 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13622 int inexact;
13623 mpc_t m0, m1;
13625 mpc_init2 (m0, prec);
13626 mpc_init2 (m1, prec);
13627 mpfr_from_real (mpc_realref(m0), re0, rnd);
13628 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13629 mpfr_from_real (mpc_realref(m1), re1, rnd);
13630 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13631 mpfr_clear_flags ();
13632 inexact = func (m0, m0, m1, crnd);
13633 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13634 mpc_clear (m0);
13635 mpc_clear (m1);
13639 return result;
13642 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13643 a normal call should be emitted rather than expanding the function
13644 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13646 static tree
13647 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13649 int nargs = gimple_call_num_args (stmt);
13651 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13652 (nargs > 0
13653 ? gimple_call_arg_ptr (stmt, 0)
13654 : &error_mark_node), fcode);
13657 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13658 a normal call should be emitted rather than expanding the function
13659 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13660 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13661 passed as second argument. */
13663 tree
13664 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13665 enum built_in_function fcode)
13667 int nargs = gimple_call_num_args (stmt);
13669 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13670 (nargs > 0
13671 ? gimple_call_arg_ptr (stmt, 0)
13672 : &error_mark_node), maxlen, fcode);
13675 /* Builtins with folding operations that operate on "..." arguments
13676 need special handling; we need to store the arguments in a convenient
13677 data structure before attempting any folding. Fortunately there are
13678 only a few builtins that fall into this category. FNDECL is the
13679 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13680 result of the function call is ignored. */
13682 static tree
13683 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13684 bool ignore ATTRIBUTE_UNUSED)
13686 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13687 tree ret = NULL_TREE;
13689 switch (fcode)
13691 case BUILT_IN_SPRINTF_CHK:
13692 case BUILT_IN_VSPRINTF_CHK:
13693 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13694 break;
13696 case BUILT_IN_SNPRINTF_CHK:
13697 case BUILT_IN_VSNPRINTF_CHK:
13698 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13700 default:
13701 break;
13703 if (ret)
13705 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13706 TREE_NO_WARNING (ret) = 1;
13707 return ret;
13709 return NULL_TREE;
13712 /* A wrapper function for builtin folding that prevents warnings for
13713 "statement without effect" and the like, caused by removing the
13714 call node earlier than the warning is generated. */
13716 tree
13717 fold_call_stmt (gimple stmt, bool ignore)
13719 tree ret = NULL_TREE;
13720 tree fndecl = gimple_call_fndecl (stmt);
13721 location_t loc = gimple_location (stmt);
13722 if (fndecl
13723 && TREE_CODE (fndecl) == FUNCTION_DECL
13724 && DECL_BUILT_IN (fndecl)
13725 && !gimple_call_va_arg_pack_p (stmt))
13727 int nargs = gimple_call_num_args (stmt);
13728 tree *args = (nargs > 0
13729 ? gimple_call_arg_ptr (stmt, 0)
13730 : &error_mark_node);
13732 if (avoid_folding_inline_builtin (fndecl))
13733 return NULL_TREE;
13734 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13736 return targetm.fold_builtin (fndecl, nargs, args, ignore);
13738 else
13740 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13741 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13742 if (!ret)
13743 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13744 if (ret)
13746 /* Propagate location information from original call to
13747 expansion of builtin. Otherwise things like
13748 maybe_emit_chk_warning, that operate on the expansion
13749 of a builtin, will use the wrong location information. */
13750 if (gimple_has_location (stmt))
13752 tree realret = ret;
13753 if (TREE_CODE (ret) == NOP_EXPR)
13754 realret = TREE_OPERAND (ret, 0);
13755 if (CAN_HAVE_LOCATION_P (realret)
13756 && !EXPR_HAS_LOCATION (realret))
13757 SET_EXPR_LOCATION (realret, loc);
13758 return realret;
13760 return ret;
13764 return NULL_TREE;
13767 /* Look up the function in built_in_decls that corresponds to DECL
13768 and set ASMSPEC as its user assembler name. DECL must be a
13769 function decl that declares a builtin. */
13771 void
13772 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13774 tree builtin;
13775 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13776 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13777 && asmspec != 0);
13779 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13780 set_user_assembler_name (builtin, asmspec);
13781 switch (DECL_FUNCTION_CODE (decl))
13783 case BUILT_IN_MEMCPY:
13784 init_block_move_fn (asmspec);
13785 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13786 break;
13787 case BUILT_IN_MEMSET:
13788 init_block_clear_fn (asmspec);
13789 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13790 break;
13791 case BUILT_IN_MEMMOVE:
13792 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13793 break;
13794 case BUILT_IN_MEMCMP:
13795 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13796 break;
13797 case BUILT_IN_ABORT:
13798 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13799 break;
13800 case BUILT_IN_FFS:
13801 if (INT_TYPE_SIZE < BITS_PER_WORD)
13803 set_user_assembler_libfunc ("ffs", asmspec);
13804 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13805 MODE_INT, 0), "ffs");
13807 break;
13808 default:
13809 break;
13813 /* Return true if DECL is a builtin that expands to a constant or similarly
13814 simple code. */
13815 bool
13816 is_simple_builtin (tree decl)
13818 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13819 switch (DECL_FUNCTION_CODE (decl))
13821 /* Builtins that expand to constants. */
13822 case BUILT_IN_CONSTANT_P:
13823 case BUILT_IN_EXPECT:
13824 case BUILT_IN_OBJECT_SIZE:
13825 case BUILT_IN_UNREACHABLE:
13826 /* Simple register moves or loads from stack. */
13827 case BUILT_IN_RETURN_ADDRESS:
13828 case BUILT_IN_EXTRACT_RETURN_ADDR:
13829 case BUILT_IN_FROB_RETURN_ADDR:
13830 case BUILT_IN_RETURN:
13831 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13832 case BUILT_IN_FRAME_ADDRESS:
13833 case BUILT_IN_VA_END:
13834 case BUILT_IN_STACK_SAVE:
13835 case BUILT_IN_STACK_RESTORE:
13836 /* Exception state returns or moves registers around. */
13837 case BUILT_IN_EH_FILTER:
13838 case BUILT_IN_EH_POINTER:
13839 case BUILT_IN_EH_COPY_VALUES:
13840 return true;
13842 default:
13843 return false;
13846 return false;
13849 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13850 most probably expanded inline into reasonably simple code. This is a
13851 superset of is_simple_builtin. */
13852 bool
13853 is_inexpensive_builtin (tree decl)
13855 if (!decl)
13856 return false;
13857 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13858 return true;
13859 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13860 switch (DECL_FUNCTION_CODE (decl))
13862 case BUILT_IN_ABS:
13863 case BUILT_IN_ALLOCA:
13864 case BUILT_IN_BSWAP32:
13865 case BUILT_IN_BSWAP64:
13866 case BUILT_IN_CLZ:
13867 case BUILT_IN_CLZIMAX:
13868 case BUILT_IN_CLZL:
13869 case BUILT_IN_CLZLL:
13870 case BUILT_IN_CTZ:
13871 case BUILT_IN_CTZIMAX:
13872 case BUILT_IN_CTZL:
13873 case BUILT_IN_CTZLL:
13874 case BUILT_IN_FFS:
13875 case BUILT_IN_FFSIMAX:
13876 case BUILT_IN_FFSL:
13877 case BUILT_IN_FFSLL:
13878 case BUILT_IN_IMAXABS:
13879 case BUILT_IN_FINITE:
13880 case BUILT_IN_FINITEF:
13881 case BUILT_IN_FINITEL:
13882 case BUILT_IN_FINITED32:
13883 case BUILT_IN_FINITED64:
13884 case BUILT_IN_FINITED128:
13885 case BUILT_IN_FPCLASSIFY:
13886 case BUILT_IN_ISFINITE:
13887 case BUILT_IN_ISINF_SIGN:
13888 case BUILT_IN_ISINF:
13889 case BUILT_IN_ISINFF:
13890 case BUILT_IN_ISINFL:
13891 case BUILT_IN_ISINFD32:
13892 case BUILT_IN_ISINFD64:
13893 case BUILT_IN_ISINFD128:
13894 case BUILT_IN_ISNAN:
13895 case BUILT_IN_ISNANF:
13896 case BUILT_IN_ISNANL:
13897 case BUILT_IN_ISNAND32:
13898 case BUILT_IN_ISNAND64:
13899 case BUILT_IN_ISNAND128:
13900 case BUILT_IN_ISNORMAL:
13901 case BUILT_IN_ISGREATER:
13902 case BUILT_IN_ISGREATEREQUAL:
13903 case BUILT_IN_ISLESS:
13904 case BUILT_IN_ISLESSEQUAL:
13905 case BUILT_IN_ISLESSGREATER:
13906 case BUILT_IN_ISUNORDERED:
13907 case BUILT_IN_VA_ARG_PACK:
13908 case BUILT_IN_VA_ARG_PACK_LEN:
13909 case BUILT_IN_VA_COPY:
13910 case BUILT_IN_TRAP:
13911 case BUILT_IN_SAVEREGS:
13912 case BUILT_IN_POPCOUNTL:
13913 case BUILT_IN_POPCOUNTLL:
13914 case BUILT_IN_POPCOUNTIMAX:
13915 case BUILT_IN_POPCOUNT:
13916 case BUILT_IN_PARITYL:
13917 case BUILT_IN_PARITYLL:
13918 case BUILT_IN_PARITYIMAX:
13919 case BUILT_IN_PARITY:
13920 case BUILT_IN_LABS:
13921 case BUILT_IN_LLABS:
13922 case BUILT_IN_PREFETCH:
13923 return true;
13925 default:
13926 return is_simple_builtin (decl);
13929 return false;