Remove unused debug_str_hash_forced from dwarf2out.
[official-gcc.git] / gcc / builtins.c
blobbd61f29a616f095e53963c2b1f0b3917b67889d3
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
52 #include "builtins.h"
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 #endif
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
60 struct target_builtins default_target_builtins;
61 #if SWITCHABLE_TARGET
62 struct target_builtins *this_target_builtins = &default_target_builtins;
63 #endif
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
72 #include "builtins.def"
74 #undef DEF_BUILTIN
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 tree built_in_decls[(int) END_BUILTINS];
79 /* Declarations used when constructing the builtin implicitly in the compiler.
80 It may be NULL_TREE when this is invalid (for instance runtime is not
81 required to implement the function call in all cases). */
82 tree implicit_built_in_decls[(int) END_BUILTINS];
84 static const char *c_getstr (tree);
85 static rtx c_readstr (const char *, enum machine_mode);
86 static int target_char_cast (tree, char *);
87 static rtx get_memory_rtx (tree, tree);
88 static int apply_args_size (void);
89 static int apply_result_size (void);
90 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
91 static rtx result_vector (int, rtx);
92 #endif
93 static void expand_builtin_update_setjmp_buf (rtx);
94 static void expand_builtin_prefetch (tree);
95 static rtx expand_builtin_apply_args (void);
96 static rtx expand_builtin_apply_args_1 (void);
97 static rtx expand_builtin_apply (rtx, rtx, rtx);
98 static void expand_builtin_return (rtx);
99 static enum type_class type_to_class (tree);
100 static rtx expand_builtin_classify_type (tree);
101 static void expand_errno_check (tree, rtx);
102 static rtx expand_builtin_mathfn (tree, rtx, rtx);
103 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
104 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
105 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
106 static rtx expand_builtin_interclass_mathfn (tree, rtx);
107 static rtx expand_builtin_sincos (tree);
108 static rtx expand_builtin_cexpi (tree, rtx);
109 static rtx expand_builtin_int_roundingfn (tree, rtx);
110 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
111 static rtx expand_builtin_next_arg (void);
112 static rtx expand_builtin_va_start (tree);
113 static rtx expand_builtin_va_end (tree);
114 static rtx expand_builtin_va_copy (tree);
115 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_strcmp (tree, rtx);
117 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
118 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_memcpy (tree, rtx);
120 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
122 enum machine_mode, int);
123 static rtx expand_builtin_strcpy (tree, rtx);
124 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
125 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strncpy (tree, rtx);
127 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
128 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
130 static rtx expand_builtin_bzero (tree);
131 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_alloca (tree, bool);
133 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
134 static rtx expand_builtin_frame_address (tree, tree);
135 static tree stabilize_va_list_loc (location_t, tree, int);
136 static rtx expand_builtin_expect (tree, rtx);
137 static tree fold_builtin_constant_p (tree);
138 static tree fold_builtin_expect (location_t, tree, tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (location_t, tree, tree);
141 static tree fold_builtin_inf (location_t, tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
144 static bool validate_arg (const_tree, enum tree_code code);
145 static bool integer_valued_real_p (tree);
146 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
147 static bool readonly_data_expr (tree);
148 static rtx expand_builtin_fabs (tree, rtx, rtx);
149 static rtx expand_builtin_signbit (tree, rtx);
150 static tree fold_builtin_sqrt (location_t, tree, tree);
151 static tree fold_builtin_cbrt (location_t, tree, tree);
152 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
153 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
154 static tree fold_builtin_cos (location_t, tree, tree, tree);
155 static tree fold_builtin_cosh (location_t, tree, tree, tree);
156 static tree fold_builtin_tan (tree, tree);
157 static tree fold_builtin_trunc (location_t, tree, tree);
158 static tree fold_builtin_floor (location_t, tree, tree);
159 static tree fold_builtin_ceil (location_t, tree, tree);
160 static tree fold_builtin_round (location_t, tree, tree);
161 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
162 static tree fold_builtin_bitop (tree, tree);
163 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
164 static tree fold_builtin_strchr (location_t, tree, tree, tree);
165 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
166 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
167 static tree fold_builtin_strcmp (location_t, tree, tree);
168 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
169 static tree fold_builtin_signbit (location_t, tree, tree);
170 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_isascii (location_t, tree);
172 static tree fold_builtin_toascii (location_t, tree);
173 static tree fold_builtin_isdigit (location_t, tree);
174 static tree fold_builtin_fabs (location_t, tree, tree);
175 static tree fold_builtin_abs (location_t, tree, tree);
176 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
177 enum tree_code);
178 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
179 static tree fold_builtin_0 (location_t, tree, bool);
180 static tree fold_builtin_1 (location_t, tree, tree, bool);
181 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
182 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
183 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
184 static tree fold_builtin_varargs (location_t, tree, tree, bool);
186 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
187 static tree fold_builtin_strstr (location_t, tree, tree, tree);
188 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
189 static tree fold_builtin_strcat (location_t, tree, tree);
190 static tree fold_builtin_strncat (location_t, tree, tree, tree);
191 static tree fold_builtin_strspn (location_t, tree, tree);
192 static tree fold_builtin_strcspn (location_t, tree, tree);
193 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
194 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
196 static rtx expand_builtin_object_size (tree);
197 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
198 enum built_in_function);
199 static void maybe_emit_chk_warning (tree, enum built_in_function);
200 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
201 static void maybe_emit_free_warning (tree);
202 static tree fold_builtin_object_size (tree, tree);
203 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
204 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
205 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
206 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
207 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
208 enum built_in_function);
209 static bool init_target_chars (void);
211 static unsigned HOST_WIDE_INT target_newline;
212 static unsigned HOST_WIDE_INT target_percent;
213 static unsigned HOST_WIDE_INT target_c;
214 static unsigned HOST_WIDE_INT target_s;
215 static char target_percent_c[3];
216 static char target_percent_s[3];
217 static char target_percent_s_newline[4];
218 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
219 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
220 static tree do_mpfr_arg2 (tree, tree, tree,
221 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
222 static tree do_mpfr_arg3 (tree, tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_sincos (tree, tree, tree);
225 static tree do_mpfr_bessel_n (tree, tree, tree,
226 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
227 const REAL_VALUE_TYPE *, bool);
228 static tree do_mpfr_remquo (tree, tree, tree);
229 static tree do_mpfr_lgamma_r (tree, tree, tree);
231 /* Return true if NAME starts with __builtin_ or __sync_. */
233 bool
234 is_builtin_name (const char *name)
236 if (strncmp (name, "__builtin_", 10) == 0)
237 return true;
238 if (strncmp (name, "__sync_", 7) == 0)
239 return true;
240 return false;
244 /* Return true if DECL is a function symbol representing a built-in. */
246 bool
247 is_builtin_fn (tree decl)
249 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
253 /* Return true if NODE should be considered for inline expansion regardless
254 of the optimization level. This means whenever a function is invoked with
255 its "internal" name, which normally contains the prefix "__builtin". */
257 static bool
258 called_as_built_in (tree node)
260 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
261 we want the name used to call the function, not the name it
262 will have. */
263 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
264 return is_builtin_name (name);
267 /* Return the alignment in bits of EXP, an object.
268 Don't return more than MAX_ALIGN no matter what. */
270 unsigned int
271 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
273 HOST_WIDE_INT bitsize, bitpos;
274 tree offset;
275 enum machine_mode mode;
276 int unsignedp, volatilep;
277 unsigned int align, inner;
279 /* Get the innermost object and the constant (bitpos) and possibly
280 variable (offset) offset of the access. */
281 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
282 &mode, &unsignedp, &volatilep, true);
284 /* Extract alignment information from the innermost object and
285 possibly adjust bitpos and offset. */
286 if (TREE_CODE (exp) == CONST_DECL)
287 exp = DECL_INITIAL (exp);
288 if (DECL_P (exp)
289 && TREE_CODE (exp) != LABEL_DECL)
290 align = DECL_ALIGN (exp);
291 else if (CONSTANT_CLASS_P (exp))
293 align = TYPE_ALIGN (TREE_TYPE (exp));
294 #ifdef CONSTANT_ALIGNMENT
295 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
296 #endif
298 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
299 align = TYPE_ALIGN (TREE_TYPE (exp));
300 else if (TREE_CODE (exp) == INDIRECT_REF)
301 align = TYPE_ALIGN (TREE_TYPE (exp));
302 else if (TREE_CODE (exp) == MEM_REF)
304 tree addr = TREE_OPERAND (exp, 0);
305 struct ptr_info_def *pi;
306 if (TREE_CODE (addr) == BIT_AND_EXPR
307 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
309 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
310 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
311 align *= BITS_PER_UNIT;
312 addr = TREE_OPERAND (addr, 0);
314 else
315 align = BITS_PER_UNIT;
316 if (TREE_CODE (addr) == SSA_NAME
317 && (pi = SSA_NAME_PTR_INFO (addr)))
319 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
320 align = MAX (pi->align * BITS_PER_UNIT, align);
322 else if (TREE_CODE (addr) == ADDR_EXPR)
323 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0), ~0U));
324 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
326 else if (TREE_CODE (exp) == TARGET_MEM_REF)
328 struct ptr_info_def *pi;
329 tree addr = TMR_BASE (exp);
330 if (TREE_CODE (addr) == BIT_AND_EXPR
331 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
333 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
334 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
335 align *= BITS_PER_UNIT;
336 addr = TREE_OPERAND (addr, 0);
338 else
339 align = BITS_PER_UNIT;
340 if (TREE_CODE (addr) == SSA_NAME
341 && (pi = SSA_NAME_PTR_INFO (addr)))
343 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
344 align = MAX (pi->align * BITS_PER_UNIT, align);
346 else if (TREE_CODE (addr) == ADDR_EXPR)
347 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0), ~0U));
348 if (TMR_OFFSET (exp))
349 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
350 if (TMR_INDEX (exp) && TMR_STEP (exp))
352 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
353 align = MIN (align, (step & -step) * BITS_PER_UNIT);
355 else if (TMR_INDEX (exp))
356 align = BITS_PER_UNIT;
357 if (TMR_INDEX2 (exp))
358 align = BITS_PER_UNIT;
360 else
361 align = BITS_PER_UNIT;
363 /* If there is a non-constant offset part extract the maximum
364 alignment that can prevail. */
365 inner = ~0U;
366 while (offset)
368 tree next_offset;
370 if (TREE_CODE (offset) == PLUS_EXPR)
372 next_offset = TREE_OPERAND (offset, 0);
373 offset = TREE_OPERAND (offset, 1);
375 else
376 next_offset = NULL;
377 if (host_integerp (offset, 1))
379 /* Any overflow in calculating offset_bits won't change
380 the alignment. */
381 unsigned offset_bits
382 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
384 if (offset_bits)
385 inner = MIN (inner, (offset_bits & -offset_bits));
387 else if (TREE_CODE (offset) == MULT_EXPR
388 && host_integerp (TREE_OPERAND (offset, 1), 1))
390 /* Any overflow in calculating offset_factor won't change
391 the alignment. */
392 unsigned offset_factor
393 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
394 * BITS_PER_UNIT);
396 if (offset_factor)
397 inner = MIN (inner, (offset_factor & -offset_factor));
399 else
401 inner = MIN (inner, BITS_PER_UNIT);
402 break;
404 offset = next_offset;
407 /* Alignment is innermost object alignment adjusted by the constant
408 and non-constant offset parts. */
409 align = MIN (align, inner);
410 bitpos = bitpos & (align - 1);
412 *bitposp = bitpos;
413 return align;
416 /* Return the alignment in bits of EXP, an object.
417 Don't return more than MAX_ALIGN no matter what. */
419 unsigned int
420 get_object_alignment (tree exp, unsigned int max_align)
422 unsigned HOST_WIDE_INT bitpos = 0;
423 unsigned int align;
425 align = get_object_alignment_1 (exp, &bitpos);
427 /* align and bitpos now specify known low bits of the pointer.
428 ptr & (align - 1) == bitpos. */
430 if (bitpos != 0)
431 align = (bitpos & -bitpos);
433 return MIN (align, max_align);
436 /* Returns true iff we can trust that alignment information has been
437 calculated properly. */
439 bool
440 can_trust_pointer_alignment (void)
442 /* We rely on TER to compute accurate alignment information. */
443 return (optimize && flag_tree_ter);
446 /* Return the alignment in bits of EXP, a pointer valued expression.
447 But don't return more than MAX_ALIGN no matter what.
448 The alignment returned is, by default, the alignment of the thing that
449 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
451 Otherwise, look at the expression to see if we can do better, i.e., if the
452 expression is actually pointing at an object whose alignment is tighter. */
454 unsigned int
455 get_pointer_alignment (tree exp, unsigned int max_align)
457 STRIP_NOPS (exp);
459 if (TREE_CODE (exp) == ADDR_EXPR)
460 return get_object_alignment (TREE_OPERAND (exp, 0), max_align);
461 else if (TREE_CODE (exp) == SSA_NAME
462 && POINTER_TYPE_P (TREE_TYPE (exp)))
464 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
465 unsigned align;
466 if (!pi)
467 return BITS_PER_UNIT;
468 if (pi->misalign != 0)
469 align = (pi->misalign & -pi->misalign);
470 else
471 align = pi->align;
472 return MIN (max_align, align * BITS_PER_UNIT);
475 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
478 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
479 way, because it could contain a zero byte in the middle.
480 TREE_STRING_LENGTH is the size of the character array, not the string.
482 ONLY_VALUE should be nonzero if the result is not going to be emitted
483 into the instruction stream and zero if it is going to be expanded.
484 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
485 is returned, otherwise NULL, since
486 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
487 evaluate the side-effects.
489 The value returned is of type `ssizetype'.
491 Unfortunately, string_constant can't access the values of const char
492 arrays with initializers, so neither can we do so here. */
494 tree
495 c_strlen (tree src, int only_value)
497 tree offset_node;
498 HOST_WIDE_INT offset;
499 int max;
500 const char *ptr;
501 location_t loc;
503 STRIP_NOPS (src);
504 if (TREE_CODE (src) == COND_EXPR
505 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
507 tree len1, len2;
509 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
510 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
511 if (tree_int_cst_equal (len1, len2))
512 return len1;
515 if (TREE_CODE (src) == COMPOUND_EXPR
516 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
517 return c_strlen (TREE_OPERAND (src, 1), only_value);
519 loc = EXPR_LOC_OR_HERE (src);
521 src = string_constant (src, &offset_node);
522 if (src == 0)
523 return NULL_TREE;
525 max = TREE_STRING_LENGTH (src) - 1;
526 ptr = TREE_STRING_POINTER (src);
528 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
530 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
531 compute the offset to the following null if we don't know where to
532 start searching for it. */
533 int i;
535 for (i = 0; i < max; i++)
536 if (ptr[i] == 0)
537 return NULL_TREE;
539 /* We don't know the starting offset, but we do know that the string
540 has no internal zero bytes. We can assume that the offset falls
541 within the bounds of the string; otherwise, the programmer deserves
542 what he gets. Subtract the offset from the length of the string,
543 and return that. This would perhaps not be valid if we were dealing
544 with named arrays in addition to literal string constants. */
546 return size_diffop_loc (loc, size_int (max), offset_node);
549 /* We have a known offset into the string. Start searching there for
550 a null character if we can represent it as a single HOST_WIDE_INT. */
551 if (offset_node == 0)
552 offset = 0;
553 else if (! host_integerp (offset_node, 0))
554 offset = -1;
555 else
556 offset = tree_low_cst (offset_node, 0);
558 /* If the offset is known to be out of bounds, warn, and call strlen at
559 runtime. */
560 if (offset < 0 || offset > max)
562 /* Suppress multiple warnings for propagated constant strings. */
563 if (! TREE_NO_WARNING (src))
565 warning_at (loc, 0, "offset outside bounds of constant string");
566 TREE_NO_WARNING (src) = 1;
568 return NULL_TREE;
571 /* Use strlen to search for the first zero byte. Since any strings
572 constructed with build_string will have nulls appended, we win even
573 if we get handed something like (char[4])"abcd".
575 Since OFFSET is our starting index into the string, no further
576 calculation is needed. */
577 return ssize_int (strlen (ptr + offset));
580 /* Return a char pointer for a C string if it is a string constant
581 or sum of string constant and integer constant. */
583 static const char *
584 c_getstr (tree src)
586 tree offset_node;
588 src = string_constant (src, &offset_node);
589 if (src == 0)
590 return 0;
592 if (offset_node == 0)
593 return TREE_STRING_POINTER (src);
594 else if (!host_integerp (offset_node, 1)
595 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
596 return 0;
598 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
601 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
602 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
604 static rtx
605 c_readstr (const char *str, enum machine_mode mode)
607 HOST_WIDE_INT c[2];
608 HOST_WIDE_INT ch;
609 unsigned int i, j;
611 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
613 c[0] = 0;
614 c[1] = 0;
615 ch = 1;
616 for (i = 0; i < GET_MODE_SIZE (mode); i++)
618 j = i;
619 if (WORDS_BIG_ENDIAN)
620 j = GET_MODE_SIZE (mode) - i - 1;
621 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
622 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
623 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
624 j *= BITS_PER_UNIT;
625 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
627 if (ch)
628 ch = (unsigned char) str[i];
629 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
631 return immed_double_const (c[0], c[1], mode);
634 /* Cast a target constant CST to target CHAR and if that value fits into
635 host char type, return zero and put that value into variable pointed to by
636 P. */
638 static int
639 target_char_cast (tree cst, char *p)
641 unsigned HOST_WIDE_INT val, hostval;
643 if (TREE_CODE (cst) != INTEGER_CST
644 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
645 return 1;
647 val = TREE_INT_CST_LOW (cst);
648 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
649 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
651 hostval = val;
652 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
653 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
655 if (val != hostval)
656 return 1;
658 *p = hostval;
659 return 0;
662 /* Similar to save_expr, but assumes that arbitrary code is not executed
663 in between the multiple evaluations. In particular, we assume that a
664 non-addressable local variable will not be modified. */
666 static tree
667 builtin_save_expr (tree exp)
669 if (TREE_CODE (exp) == SSA_NAME
670 || (TREE_ADDRESSABLE (exp) == 0
671 && (TREE_CODE (exp) == PARM_DECL
672 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
673 return exp;
675 return save_expr (exp);
678 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
679 times to get the address of either a higher stack frame, or a return
680 address located within it (depending on FNDECL_CODE). */
682 static rtx
683 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
685 int i;
687 #ifdef INITIAL_FRAME_ADDRESS_RTX
688 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
689 #else
690 rtx tem;
692 /* For a zero count with __builtin_return_address, we don't care what
693 frame address we return, because target-specific definitions will
694 override us. Therefore frame pointer elimination is OK, and using
695 the soft frame pointer is OK.
697 For a nonzero count, or a zero count with __builtin_frame_address,
698 we require a stable offset from the current frame pointer to the
699 previous one, so we must use the hard frame pointer, and
700 we must disable frame pointer elimination. */
701 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
702 tem = frame_pointer_rtx;
703 else
705 tem = hard_frame_pointer_rtx;
707 /* Tell reload not to eliminate the frame pointer. */
708 crtl->accesses_prior_frames = 1;
710 #endif
712 /* Some machines need special handling before we can access
713 arbitrary frames. For example, on the SPARC, we must first flush
714 all register windows to the stack. */
715 #ifdef SETUP_FRAME_ADDRESSES
716 if (count > 0)
717 SETUP_FRAME_ADDRESSES ();
718 #endif
720 /* On the SPARC, the return address is not in the frame, it is in a
721 register. There is no way to access it off of the current frame
722 pointer, but it can be accessed off the previous frame pointer by
723 reading the value from the register window save area. */
724 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
725 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
726 count--;
727 #endif
729 /* Scan back COUNT frames to the specified frame. */
730 for (i = 0; i < count; i++)
732 /* Assume the dynamic chain pointer is in the word that the
733 frame address points to, unless otherwise specified. */
734 #ifdef DYNAMIC_CHAIN_ADDRESS
735 tem = DYNAMIC_CHAIN_ADDRESS (tem);
736 #endif
737 tem = memory_address (Pmode, tem);
738 tem = gen_frame_mem (Pmode, tem);
739 tem = copy_to_reg (tem);
742 /* For __builtin_frame_address, return what we've got. But, on
743 the SPARC for example, we may have to add a bias. */
744 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
745 #ifdef FRAME_ADDR_RTX
746 return FRAME_ADDR_RTX (tem);
747 #else
748 return tem;
749 #endif
751 /* For __builtin_return_address, get the return address from that frame. */
752 #ifdef RETURN_ADDR_RTX
753 tem = RETURN_ADDR_RTX (count, tem);
754 #else
755 tem = memory_address (Pmode,
756 plus_constant (tem, GET_MODE_SIZE (Pmode)));
757 tem = gen_frame_mem (Pmode, tem);
758 #endif
759 return tem;
762 /* Alias set used for setjmp buffer. */
763 static alias_set_type setjmp_alias_set = -1;
765 /* Construct the leading half of a __builtin_setjmp call. Control will
766 return to RECEIVER_LABEL. This is also called directly by the SJLJ
767 exception handling code. */
769 void
770 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
772 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
773 rtx stack_save;
774 rtx mem;
776 if (setjmp_alias_set == -1)
777 setjmp_alias_set = new_alias_set ();
779 buf_addr = convert_memory_address (Pmode, buf_addr);
781 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
783 /* We store the frame pointer and the address of receiver_label in
784 the buffer and use the rest of it for the stack save area, which
785 is machine-dependent. */
787 mem = gen_rtx_MEM (Pmode, buf_addr);
788 set_mem_alias_set (mem, setjmp_alias_set);
789 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
791 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
792 set_mem_alias_set (mem, setjmp_alias_set);
794 emit_move_insn (validize_mem (mem),
795 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
797 stack_save = gen_rtx_MEM (sa_mode,
798 plus_constant (buf_addr,
799 2 * GET_MODE_SIZE (Pmode)));
800 set_mem_alias_set (stack_save, setjmp_alias_set);
801 emit_stack_save (SAVE_NONLOCAL, &stack_save);
803 /* If there is further processing to do, do it. */
804 #ifdef HAVE_builtin_setjmp_setup
805 if (HAVE_builtin_setjmp_setup)
806 emit_insn (gen_builtin_setjmp_setup (buf_addr));
807 #endif
809 /* Tell optimize_save_area_alloca that extra work is going to
810 need to go on during alloca. */
811 cfun->calls_setjmp = 1;
813 /* We have a nonlocal label. */
814 cfun->has_nonlocal_label = 1;
817 /* Construct the trailing part of a __builtin_setjmp call. This is
818 also called directly by the SJLJ exception handling code. */
820 void
821 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
823 rtx chain;
825 /* Clobber the FP when we get here, so we have to make sure it's
826 marked as used by this function. */
827 emit_use (hard_frame_pointer_rtx);
829 /* Mark the static chain as clobbered here so life information
830 doesn't get messed up for it. */
831 chain = targetm.calls.static_chain (current_function_decl, true);
832 if (chain && REG_P (chain))
833 emit_clobber (chain);
835 /* Now put in the code to restore the frame pointer, and argument
836 pointer, if needed. */
837 #ifdef HAVE_nonlocal_goto
838 if (! HAVE_nonlocal_goto)
839 #endif
841 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
842 /* This might change the hard frame pointer in ways that aren't
843 apparent to early optimization passes, so force a clobber. */
844 emit_clobber (hard_frame_pointer_rtx);
847 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
848 if (fixed_regs[ARG_POINTER_REGNUM])
850 #ifdef ELIMINABLE_REGS
851 size_t i;
852 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
854 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
855 if (elim_regs[i].from == ARG_POINTER_REGNUM
856 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
857 break;
859 if (i == ARRAY_SIZE (elim_regs))
860 #endif
862 /* Now restore our arg pointer from the address at which it
863 was saved in our stack frame. */
864 emit_move_insn (crtl->args.internal_arg_pointer,
865 copy_to_reg (get_arg_pointer_save_area ()));
868 #endif
870 #ifdef HAVE_builtin_setjmp_receiver
871 if (HAVE_builtin_setjmp_receiver)
872 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
873 else
874 #endif
875 #ifdef HAVE_nonlocal_goto_receiver
876 if (HAVE_nonlocal_goto_receiver)
877 emit_insn (gen_nonlocal_goto_receiver ());
878 else
879 #endif
880 { /* Nothing */ }
882 /* We must not allow the code we just generated to be reordered by
883 scheduling. Specifically, the update of the frame pointer must
884 happen immediately, not later. */
885 emit_insn (gen_blockage ());
888 /* __builtin_longjmp is passed a pointer to an array of five words (not
889 all will be used on all machines). It operates similarly to the C
890 library function of the same name, but is more efficient. Much of
891 the code below is copied from the handling of non-local gotos. */
893 static void
894 expand_builtin_longjmp (rtx buf_addr, rtx value)
896 rtx fp, lab, stack, insn, last;
897 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
899 /* DRAP is needed for stack realign if longjmp is expanded to current
900 function */
901 if (SUPPORTS_STACK_ALIGNMENT)
902 crtl->need_drap = true;
904 if (setjmp_alias_set == -1)
905 setjmp_alias_set = new_alias_set ();
907 buf_addr = convert_memory_address (Pmode, buf_addr);
909 buf_addr = force_reg (Pmode, buf_addr);
911 /* We require that the user must pass a second argument of 1, because
912 that is what builtin_setjmp will return. */
913 gcc_assert (value == const1_rtx);
915 last = get_last_insn ();
916 #ifdef HAVE_builtin_longjmp
917 if (HAVE_builtin_longjmp)
918 emit_insn (gen_builtin_longjmp (buf_addr));
919 else
920 #endif
922 fp = gen_rtx_MEM (Pmode, buf_addr);
923 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
924 GET_MODE_SIZE (Pmode)));
926 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
927 2 * GET_MODE_SIZE (Pmode)));
928 set_mem_alias_set (fp, setjmp_alias_set);
929 set_mem_alias_set (lab, setjmp_alias_set);
930 set_mem_alias_set (stack, setjmp_alias_set);
932 /* Pick up FP, label, and SP from the block and jump. This code is
933 from expand_goto in stmt.c; see there for detailed comments. */
934 #ifdef HAVE_nonlocal_goto
935 if (HAVE_nonlocal_goto)
936 /* We have to pass a value to the nonlocal_goto pattern that will
937 get copied into the static_chain pointer, but it does not matter
938 what that value is, because builtin_setjmp does not use it. */
939 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
940 else
941 #endif
943 lab = copy_to_reg (lab);
945 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
946 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
948 emit_move_insn (hard_frame_pointer_rtx, fp);
949 emit_stack_restore (SAVE_NONLOCAL, stack);
951 emit_use (hard_frame_pointer_rtx);
952 emit_use (stack_pointer_rtx);
953 emit_indirect_jump (lab);
957 /* Search backwards and mark the jump insn as a non-local goto.
958 Note that this precludes the use of __builtin_longjmp to a
959 __builtin_setjmp target in the same function. However, we've
960 already cautioned the user that these functions are for
961 internal exception handling use only. */
962 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
964 gcc_assert (insn != last);
966 if (JUMP_P (insn))
968 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
969 break;
971 else if (CALL_P (insn))
972 break;
976 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
977 and the address of the save area. */
979 static rtx
980 expand_builtin_nonlocal_goto (tree exp)
982 tree t_label, t_save_area;
983 rtx r_label, r_save_area, r_fp, r_sp, insn;
985 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
986 return NULL_RTX;
988 t_label = CALL_EXPR_ARG (exp, 0);
989 t_save_area = CALL_EXPR_ARG (exp, 1);
991 r_label = expand_normal (t_label);
992 r_label = convert_memory_address (Pmode, r_label);
993 r_save_area = expand_normal (t_save_area);
994 r_save_area = convert_memory_address (Pmode, r_save_area);
995 /* Copy the address of the save location to a register just in case it was based
996 on the frame pointer. */
997 r_save_area = copy_to_reg (r_save_area);
998 r_fp = gen_rtx_MEM (Pmode, r_save_area);
999 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1000 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1002 crtl->has_nonlocal_goto = 1;
1004 #ifdef HAVE_nonlocal_goto
1005 /* ??? We no longer need to pass the static chain value, afaik. */
1006 if (HAVE_nonlocal_goto)
1007 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1008 else
1009 #endif
1011 r_label = copy_to_reg (r_label);
1013 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1014 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1016 /* Restore frame pointer for containing function.
1017 This sets the actual hard register used for the frame pointer
1018 to the location of the function's incoming static chain info.
1019 The non-local goto handler will then adjust it to contain the
1020 proper value and reload the argument pointer, if needed. */
1021 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1022 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1024 /* USE of hard_frame_pointer_rtx added for consistency;
1025 not clear if really needed. */
1026 emit_use (hard_frame_pointer_rtx);
1027 emit_use (stack_pointer_rtx);
1029 /* If the architecture is using a GP register, we must
1030 conservatively assume that the target function makes use of it.
1031 The prologue of functions with nonlocal gotos must therefore
1032 initialize the GP register to the appropriate value, and we
1033 must then make sure that this value is live at the point
1034 of the jump. (Note that this doesn't necessarily apply
1035 to targets with a nonlocal_goto pattern; they are free
1036 to implement it in their own way. Note also that this is
1037 a no-op if the GP register is a global invariant.) */
1038 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1039 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1040 emit_use (pic_offset_table_rtx);
1042 emit_indirect_jump (r_label);
1045 /* Search backwards to the jump insn and mark it as a
1046 non-local goto. */
1047 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1049 if (JUMP_P (insn))
1051 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1052 break;
1054 else if (CALL_P (insn))
1055 break;
1058 return const0_rtx;
1061 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1062 (not all will be used on all machines) that was passed to __builtin_setjmp.
1063 It updates the stack pointer in that block to correspond to the current
1064 stack pointer. */
1066 static void
1067 expand_builtin_update_setjmp_buf (rtx buf_addr)
1069 enum machine_mode sa_mode = Pmode;
1070 rtx stack_save;
1073 #ifdef HAVE_save_stack_nonlocal
1074 if (HAVE_save_stack_nonlocal)
1075 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1076 #endif
1077 #ifdef STACK_SAVEAREA_MODE
1078 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1079 #endif
1081 stack_save
1082 = gen_rtx_MEM (sa_mode,
1083 memory_address
1084 (sa_mode,
1085 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1087 #ifdef HAVE_setjmp
1088 if (HAVE_setjmp)
1089 emit_insn (gen_setjmp ());
1090 #endif
1092 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1095 /* Expand a call to __builtin_prefetch. For a target that does not support
1096 data prefetch, evaluate the memory address argument in case it has side
1097 effects. */
1099 static void
1100 expand_builtin_prefetch (tree exp)
1102 tree arg0, arg1, arg2;
1103 int nargs;
1104 rtx op0, op1, op2;
1106 if (!validate_arglist (exp, POINTER_TYPE, 0))
1107 return;
1109 arg0 = CALL_EXPR_ARG (exp, 0);
1111 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1112 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1113 locality). */
1114 nargs = call_expr_nargs (exp);
1115 if (nargs > 1)
1116 arg1 = CALL_EXPR_ARG (exp, 1);
1117 else
1118 arg1 = integer_zero_node;
1119 if (nargs > 2)
1120 arg2 = CALL_EXPR_ARG (exp, 2);
1121 else
1122 arg2 = integer_three_node;
1124 /* Argument 0 is an address. */
1125 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1127 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1128 if (TREE_CODE (arg1) != INTEGER_CST)
1130 error ("second argument to %<__builtin_prefetch%> must be a constant");
1131 arg1 = integer_zero_node;
1133 op1 = expand_normal (arg1);
1134 /* Argument 1 must be either zero or one. */
1135 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1137 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1138 " using zero");
1139 op1 = const0_rtx;
1142 /* Argument 2 (locality) must be a compile-time constant int. */
1143 if (TREE_CODE (arg2) != INTEGER_CST)
1145 error ("third argument to %<__builtin_prefetch%> must be a constant");
1146 arg2 = integer_zero_node;
1148 op2 = expand_normal (arg2);
1149 /* Argument 2 must be 0, 1, 2, or 3. */
1150 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1152 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1153 op2 = const0_rtx;
1156 #ifdef HAVE_prefetch
1157 if (HAVE_prefetch)
1159 struct expand_operand ops[3];
1161 create_address_operand (&ops[0], op0);
1162 create_integer_operand (&ops[1], INTVAL (op1));
1163 create_integer_operand (&ops[2], INTVAL (op2));
1164 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1165 return;
1167 #endif
1169 /* Don't do anything with direct references to volatile memory, but
1170 generate code to handle other side effects. */
1171 if (!MEM_P (op0) && side_effects_p (op0))
1172 emit_insn (op0);
1175 /* Get a MEM rtx for expression EXP which is the address of an operand
1176 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1177 the maximum length of the block of memory that might be accessed or
1178 NULL if unknown. */
1180 static rtx
1181 get_memory_rtx (tree exp, tree len)
1183 tree orig_exp = exp;
1184 rtx addr, mem;
1185 HOST_WIDE_INT off;
1187 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1188 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1189 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1190 exp = TREE_OPERAND (exp, 0);
1192 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1193 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1195 /* Get an expression we can use to find the attributes to assign to MEM.
1196 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1197 we can. First remove any nops. */
1198 while (CONVERT_EXPR_P (exp)
1199 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1200 exp = TREE_OPERAND (exp, 0);
1202 off = 0;
1203 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1204 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1205 && host_integerp (TREE_OPERAND (exp, 1), 0)
1206 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1207 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1208 else if (TREE_CODE (exp) == ADDR_EXPR)
1209 exp = TREE_OPERAND (exp, 0);
1210 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1211 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1212 else
1213 exp = NULL;
1215 /* Honor attributes derived from exp, except for the alias set
1216 (as builtin stringops may alias with anything) and the size
1217 (as stringops may access multiple array elements). */
1218 if (exp)
1220 set_mem_attributes (mem, exp, 0);
1222 if (off)
1223 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1225 /* Allow the string and memory builtins to overflow from one
1226 field into another, see http://gcc.gnu.org/PR23561.
1227 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1228 memory accessed by the string or memory builtin will fit
1229 within the field. */
1230 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1232 tree mem_expr = MEM_EXPR (mem);
1233 HOST_WIDE_INT offset = -1, length = -1;
1234 tree inner = exp;
1236 while (TREE_CODE (inner) == ARRAY_REF
1237 || CONVERT_EXPR_P (inner)
1238 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1239 || TREE_CODE (inner) == SAVE_EXPR)
1240 inner = TREE_OPERAND (inner, 0);
1242 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1244 if (MEM_OFFSET (mem)
1245 && CONST_INT_P (MEM_OFFSET (mem)))
1246 offset = INTVAL (MEM_OFFSET (mem));
1248 if (offset >= 0 && len && host_integerp (len, 0))
1249 length = tree_low_cst (len, 0);
1251 while (TREE_CODE (inner) == COMPONENT_REF)
1253 tree field = TREE_OPERAND (inner, 1);
1254 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1255 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1257 /* Bitfields are generally not byte-addressable. */
1258 gcc_assert (!DECL_BIT_FIELD (field)
1259 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1260 % BITS_PER_UNIT) == 0
1261 && host_integerp (DECL_SIZE (field), 0)
1262 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1263 % BITS_PER_UNIT) == 0));
1265 /* If we can prove that the memory starting at XEXP (mem, 0) and
1266 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1267 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1268 fields without DECL_SIZE_UNIT like flexible array members. */
1269 if (length >= 0
1270 && DECL_SIZE_UNIT (field)
1271 && host_integerp (DECL_SIZE_UNIT (field), 0))
1273 HOST_WIDE_INT size
1274 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1275 if (offset <= size
1276 && length <= size
1277 && offset + length <= size)
1278 break;
1281 if (offset >= 0
1282 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1283 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1284 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1285 / BITS_PER_UNIT;
1286 else
1288 offset = -1;
1289 length = -1;
1292 mem_expr = TREE_OPERAND (mem_expr, 0);
1293 inner = TREE_OPERAND (inner, 0);
1296 if (mem_expr == NULL)
1297 offset = -1;
1298 if (mem_expr != MEM_EXPR (mem))
1300 set_mem_expr (mem, mem_expr);
1301 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1304 set_mem_alias_set (mem, 0);
1305 set_mem_size (mem, NULL_RTX);
1308 return mem;
1311 /* Built-in functions to perform an untyped call and return. */
1313 #define apply_args_mode \
1314 (this_target_builtins->x_apply_args_mode)
1315 #define apply_result_mode \
1316 (this_target_builtins->x_apply_result_mode)
1318 /* Return the size required for the block returned by __builtin_apply_args,
1319 and initialize apply_args_mode. */
1321 static int
1322 apply_args_size (void)
1324 static int size = -1;
1325 int align;
1326 unsigned int regno;
1327 enum machine_mode mode;
1329 /* The values computed by this function never change. */
1330 if (size < 0)
1332 /* The first value is the incoming arg-pointer. */
1333 size = GET_MODE_SIZE (Pmode);
1335 /* The second value is the structure value address unless this is
1336 passed as an "invisible" first argument. */
1337 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1338 size += GET_MODE_SIZE (Pmode);
1340 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1341 if (FUNCTION_ARG_REGNO_P (regno))
1343 mode = targetm.calls.get_raw_arg_mode (regno);
1345 gcc_assert (mode != VOIDmode);
1347 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1348 if (size % align != 0)
1349 size = CEIL (size, align) * align;
1350 size += GET_MODE_SIZE (mode);
1351 apply_args_mode[regno] = mode;
1353 else
1355 apply_args_mode[regno] = VOIDmode;
1358 return size;
1361 /* Return the size required for the block returned by __builtin_apply,
1362 and initialize apply_result_mode. */
1364 static int
1365 apply_result_size (void)
1367 static int size = -1;
1368 int align, regno;
1369 enum machine_mode mode;
1371 /* The values computed by this function never change. */
1372 if (size < 0)
1374 size = 0;
1376 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1377 if (targetm.calls.function_value_regno_p (regno))
1379 mode = targetm.calls.get_raw_result_mode (regno);
1381 gcc_assert (mode != VOIDmode);
1383 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1384 if (size % align != 0)
1385 size = CEIL (size, align) * align;
1386 size += GET_MODE_SIZE (mode);
1387 apply_result_mode[regno] = mode;
1389 else
1390 apply_result_mode[regno] = VOIDmode;
1392 /* Allow targets that use untyped_call and untyped_return to override
1393 the size so that machine-specific information can be stored here. */
1394 #ifdef APPLY_RESULT_SIZE
1395 size = APPLY_RESULT_SIZE;
1396 #endif
1398 return size;
1401 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1402 /* Create a vector describing the result block RESULT. If SAVEP is true,
1403 the result block is used to save the values; otherwise it is used to
1404 restore the values. */
1406 static rtx
1407 result_vector (int savep, rtx result)
1409 int regno, size, align, nelts;
1410 enum machine_mode mode;
1411 rtx reg, mem;
1412 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1414 size = nelts = 0;
1415 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1416 if ((mode = apply_result_mode[regno]) != VOIDmode)
1418 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1419 if (size % align != 0)
1420 size = CEIL (size, align) * align;
1421 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1422 mem = adjust_address (result, mode, size);
1423 savevec[nelts++] = (savep
1424 ? gen_rtx_SET (VOIDmode, mem, reg)
1425 : gen_rtx_SET (VOIDmode, reg, mem));
1426 size += GET_MODE_SIZE (mode);
1428 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1430 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1432 /* Save the state required to perform an untyped call with the same
1433 arguments as were passed to the current function. */
1435 static rtx
1436 expand_builtin_apply_args_1 (void)
1438 rtx registers, tem;
1439 int size, align, regno;
1440 enum machine_mode mode;
1441 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1443 /* Create a block where the arg-pointer, structure value address,
1444 and argument registers can be saved. */
1445 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1447 /* Walk past the arg-pointer and structure value address. */
1448 size = GET_MODE_SIZE (Pmode);
1449 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1450 size += GET_MODE_SIZE (Pmode);
1452 /* Save each register used in calling a function to the block. */
1453 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1454 if ((mode = apply_args_mode[regno]) != VOIDmode)
1456 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1457 if (size % align != 0)
1458 size = CEIL (size, align) * align;
1460 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1462 emit_move_insn (adjust_address (registers, mode, size), tem);
1463 size += GET_MODE_SIZE (mode);
1466 /* Save the arg pointer to the block. */
1467 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1468 #ifdef STACK_GROWS_DOWNWARD
1469 /* We need the pointer as the caller actually passed them to us, not
1470 as we might have pretended they were passed. Make sure it's a valid
1471 operand, as emit_move_insn isn't expected to handle a PLUS. */
1473 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1474 NULL_RTX);
1475 #endif
1476 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1478 size = GET_MODE_SIZE (Pmode);
1480 /* Save the structure value address unless this is passed as an
1481 "invisible" first argument. */
1482 if (struct_incoming_value)
1484 emit_move_insn (adjust_address (registers, Pmode, size),
1485 copy_to_reg (struct_incoming_value));
1486 size += GET_MODE_SIZE (Pmode);
1489 /* Return the address of the block. */
1490 return copy_addr_to_reg (XEXP (registers, 0));
1493 /* __builtin_apply_args returns block of memory allocated on
1494 the stack into which is stored the arg pointer, structure
1495 value address, static chain, and all the registers that might
1496 possibly be used in performing a function call. The code is
1497 moved to the start of the function so the incoming values are
1498 saved. */
1500 static rtx
1501 expand_builtin_apply_args (void)
1503 /* Don't do __builtin_apply_args more than once in a function.
1504 Save the result of the first call and reuse it. */
1505 if (apply_args_value != 0)
1506 return apply_args_value;
1508 /* When this function is called, it means that registers must be
1509 saved on entry to this function. So we migrate the
1510 call to the first insn of this function. */
1511 rtx temp;
1512 rtx seq;
1514 start_sequence ();
1515 temp = expand_builtin_apply_args_1 ();
1516 seq = get_insns ();
1517 end_sequence ();
1519 apply_args_value = temp;
1521 /* Put the insns after the NOTE that starts the function.
1522 If this is inside a start_sequence, make the outer-level insn
1523 chain current, so the code is placed at the start of the
1524 function. If internal_arg_pointer is a non-virtual pseudo,
1525 it needs to be placed after the function that initializes
1526 that pseudo. */
1527 push_topmost_sequence ();
1528 if (REG_P (crtl->args.internal_arg_pointer)
1529 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1530 emit_insn_before (seq, parm_birth_insn);
1531 else
1532 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1533 pop_topmost_sequence ();
1534 return temp;
1538 /* Perform an untyped call and save the state required to perform an
1539 untyped return of whatever value was returned by the given function. */
1541 static rtx
1542 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1544 int size, align, regno;
1545 enum machine_mode mode;
1546 rtx incoming_args, result, reg, dest, src, call_insn;
1547 rtx old_stack_level = 0;
1548 rtx call_fusage = 0;
1549 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1551 arguments = convert_memory_address (Pmode, arguments);
1553 /* Create a block where the return registers can be saved. */
1554 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1556 /* Fetch the arg pointer from the ARGUMENTS block. */
1557 incoming_args = gen_reg_rtx (Pmode);
1558 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1559 #ifndef STACK_GROWS_DOWNWARD
1560 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1561 incoming_args, 0, OPTAB_LIB_WIDEN);
1562 #endif
1564 /* Push a new argument block and copy the arguments. Do not allow
1565 the (potential) memcpy call below to interfere with our stack
1566 manipulations. */
1567 do_pending_stack_adjust ();
1568 NO_DEFER_POP;
1570 /* Save the stack with nonlocal if available. */
1571 #ifdef HAVE_save_stack_nonlocal
1572 if (HAVE_save_stack_nonlocal)
1573 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1574 else
1575 #endif
1576 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1578 /* Allocate a block of memory onto the stack and copy the memory
1579 arguments to the outgoing arguments address. We can pass TRUE
1580 as the 4th argument because we just saved the stack pointer
1581 and will restore it right after the call. */
1582 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1584 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1585 may have already set current_function_calls_alloca to true.
1586 current_function_calls_alloca won't be set if argsize is zero,
1587 so we have to guarantee need_drap is true here. */
1588 if (SUPPORTS_STACK_ALIGNMENT)
1589 crtl->need_drap = true;
1591 dest = virtual_outgoing_args_rtx;
1592 #ifndef STACK_GROWS_DOWNWARD
1593 if (CONST_INT_P (argsize))
1594 dest = plus_constant (dest, -INTVAL (argsize));
1595 else
1596 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1597 #endif
1598 dest = gen_rtx_MEM (BLKmode, dest);
1599 set_mem_align (dest, PARM_BOUNDARY);
1600 src = gen_rtx_MEM (BLKmode, incoming_args);
1601 set_mem_align (src, PARM_BOUNDARY);
1602 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1604 /* Refer to the argument block. */
1605 apply_args_size ();
1606 arguments = gen_rtx_MEM (BLKmode, arguments);
1607 set_mem_align (arguments, PARM_BOUNDARY);
1609 /* Walk past the arg-pointer and structure value address. */
1610 size = GET_MODE_SIZE (Pmode);
1611 if (struct_value)
1612 size += GET_MODE_SIZE (Pmode);
1614 /* Restore each of the registers previously saved. Make USE insns
1615 for each of these registers for use in making the call. */
1616 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1617 if ((mode = apply_args_mode[regno]) != VOIDmode)
1619 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1620 if (size % align != 0)
1621 size = CEIL (size, align) * align;
1622 reg = gen_rtx_REG (mode, regno);
1623 emit_move_insn (reg, adjust_address (arguments, mode, size));
1624 use_reg (&call_fusage, reg);
1625 size += GET_MODE_SIZE (mode);
1628 /* Restore the structure value address unless this is passed as an
1629 "invisible" first argument. */
1630 size = GET_MODE_SIZE (Pmode);
1631 if (struct_value)
1633 rtx value = gen_reg_rtx (Pmode);
1634 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1635 emit_move_insn (struct_value, value);
1636 if (REG_P (struct_value))
1637 use_reg (&call_fusage, struct_value);
1638 size += GET_MODE_SIZE (Pmode);
1641 /* All arguments and registers used for the call are set up by now! */
1642 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1644 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1645 and we don't want to load it into a register as an optimization,
1646 because prepare_call_address already did it if it should be done. */
1647 if (GET_CODE (function) != SYMBOL_REF)
1648 function = memory_address (FUNCTION_MODE, function);
1650 /* Generate the actual call instruction and save the return value. */
1651 #ifdef HAVE_untyped_call
1652 if (HAVE_untyped_call)
1653 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1654 result, result_vector (1, result)));
1655 else
1656 #endif
1657 #ifdef HAVE_call_value
1658 if (HAVE_call_value)
1660 rtx valreg = 0;
1662 /* Locate the unique return register. It is not possible to
1663 express a call that sets more than one return register using
1664 call_value; use untyped_call for that. In fact, untyped_call
1665 only needs to save the return registers in the given block. */
1666 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1667 if ((mode = apply_result_mode[regno]) != VOIDmode)
1669 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1671 valreg = gen_rtx_REG (mode, regno);
1674 emit_call_insn (GEN_CALL_VALUE (valreg,
1675 gen_rtx_MEM (FUNCTION_MODE, function),
1676 const0_rtx, NULL_RTX, const0_rtx));
1678 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1680 else
1681 #endif
1682 gcc_unreachable ();
1684 /* Find the CALL insn we just emitted, and attach the register usage
1685 information. */
1686 call_insn = last_call_insn ();
1687 add_function_usage_to (call_insn, call_fusage);
1689 /* Restore the stack. */
1690 #ifdef HAVE_save_stack_nonlocal
1691 if (HAVE_save_stack_nonlocal)
1692 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1693 else
1694 #endif
1695 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1697 OK_DEFER_POP;
1699 /* Return the address of the result block. */
1700 result = copy_addr_to_reg (XEXP (result, 0));
1701 return convert_memory_address (ptr_mode, result);
1704 /* Perform an untyped return. */
1706 static void
1707 expand_builtin_return (rtx result)
1709 int size, align, regno;
1710 enum machine_mode mode;
1711 rtx reg;
1712 rtx call_fusage = 0;
1714 result = convert_memory_address (Pmode, result);
1716 apply_result_size ();
1717 result = gen_rtx_MEM (BLKmode, result);
1719 #ifdef HAVE_untyped_return
1720 if (HAVE_untyped_return)
1722 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1723 emit_barrier ();
1724 return;
1726 #endif
1728 /* Restore the return value and note that each value is used. */
1729 size = 0;
1730 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1731 if ((mode = apply_result_mode[regno]) != VOIDmode)
1733 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1734 if (size % align != 0)
1735 size = CEIL (size, align) * align;
1736 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1737 emit_move_insn (reg, adjust_address (result, mode, size));
1739 push_to_sequence (call_fusage);
1740 emit_use (reg);
1741 call_fusage = get_insns ();
1742 end_sequence ();
1743 size += GET_MODE_SIZE (mode);
1746 /* Put the USE insns before the return. */
1747 emit_insn (call_fusage);
1749 /* Return whatever values was restored by jumping directly to the end
1750 of the function. */
1751 expand_naked_return ();
1754 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1756 static enum type_class
1757 type_to_class (tree type)
1759 switch (TREE_CODE (type))
1761 case VOID_TYPE: return void_type_class;
1762 case INTEGER_TYPE: return integer_type_class;
1763 case ENUMERAL_TYPE: return enumeral_type_class;
1764 case BOOLEAN_TYPE: return boolean_type_class;
1765 case POINTER_TYPE: return pointer_type_class;
1766 case REFERENCE_TYPE: return reference_type_class;
1767 case OFFSET_TYPE: return offset_type_class;
1768 case REAL_TYPE: return real_type_class;
1769 case COMPLEX_TYPE: return complex_type_class;
1770 case FUNCTION_TYPE: return function_type_class;
1771 case METHOD_TYPE: return method_type_class;
1772 case RECORD_TYPE: return record_type_class;
1773 case UNION_TYPE:
1774 case QUAL_UNION_TYPE: return union_type_class;
1775 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1776 ? string_type_class : array_type_class);
1777 case LANG_TYPE: return lang_type_class;
1778 default: return no_type_class;
1782 /* Expand a call EXP to __builtin_classify_type. */
1784 static rtx
1785 expand_builtin_classify_type (tree exp)
1787 if (call_expr_nargs (exp))
1788 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1789 return GEN_INT (no_type_class);
1792 /* This helper macro, meant to be used in mathfn_built_in below,
1793 determines which among a set of three builtin math functions is
1794 appropriate for a given type mode. The `F' and `L' cases are
1795 automatically generated from the `double' case. */
1796 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1797 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1798 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1799 fcodel = BUILT_IN_MATHFN##L ; break;
1800 /* Similar to above, but appends _R after any F/L suffix. */
1801 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1802 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1803 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1804 fcodel = BUILT_IN_MATHFN##L_R ; break;
1806 /* Return mathematic function equivalent to FN but operating directly
1807 on TYPE, if available. If IMPLICIT is true find the function in
1808 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1809 can't do the conversion, return zero. */
1811 static tree
1812 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1814 tree const *const fn_arr
1815 = implicit ? implicit_built_in_decls : built_in_decls;
1816 enum built_in_function fcode, fcodef, fcodel;
1818 switch (fn)
1820 CASE_MATHFN (BUILT_IN_ACOS)
1821 CASE_MATHFN (BUILT_IN_ACOSH)
1822 CASE_MATHFN (BUILT_IN_ASIN)
1823 CASE_MATHFN (BUILT_IN_ASINH)
1824 CASE_MATHFN (BUILT_IN_ATAN)
1825 CASE_MATHFN (BUILT_IN_ATAN2)
1826 CASE_MATHFN (BUILT_IN_ATANH)
1827 CASE_MATHFN (BUILT_IN_CBRT)
1828 CASE_MATHFN (BUILT_IN_CEIL)
1829 CASE_MATHFN (BUILT_IN_CEXPI)
1830 CASE_MATHFN (BUILT_IN_COPYSIGN)
1831 CASE_MATHFN (BUILT_IN_COS)
1832 CASE_MATHFN (BUILT_IN_COSH)
1833 CASE_MATHFN (BUILT_IN_DREM)
1834 CASE_MATHFN (BUILT_IN_ERF)
1835 CASE_MATHFN (BUILT_IN_ERFC)
1836 CASE_MATHFN (BUILT_IN_EXP)
1837 CASE_MATHFN (BUILT_IN_EXP10)
1838 CASE_MATHFN (BUILT_IN_EXP2)
1839 CASE_MATHFN (BUILT_IN_EXPM1)
1840 CASE_MATHFN (BUILT_IN_FABS)
1841 CASE_MATHFN (BUILT_IN_FDIM)
1842 CASE_MATHFN (BUILT_IN_FLOOR)
1843 CASE_MATHFN (BUILT_IN_FMA)
1844 CASE_MATHFN (BUILT_IN_FMAX)
1845 CASE_MATHFN (BUILT_IN_FMIN)
1846 CASE_MATHFN (BUILT_IN_FMOD)
1847 CASE_MATHFN (BUILT_IN_FREXP)
1848 CASE_MATHFN (BUILT_IN_GAMMA)
1849 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1850 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1851 CASE_MATHFN (BUILT_IN_HYPOT)
1852 CASE_MATHFN (BUILT_IN_ILOGB)
1853 CASE_MATHFN (BUILT_IN_INF)
1854 CASE_MATHFN (BUILT_IN_ISINF)
1855 CASE_MATHFN (BUILT_IN_J0)
1856 CASE_MATHFN (BUILT_IN_J1)
1857 CASE_MATHFN (BUILT_IN_JN)
1858 CASE_MATHFN (BUILT_IN_LCEIL)
1859 CASE_MATHFN (BUILT_IN_LDEXP)
1860 CASE_MATHFN (BUILT_IN_LFLOOR)
1861 CASE_MATHFN (BUILT_IN_LGAMMA)
1862 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1863 CASE_MATHFN (BUILT_IN_LLCEIL)
1864 CASE_MATHFN (BUILT_IN_LLFLOOR)
1865 CASE_MATHFN (BUILT_IN_LLRINT)
1866 CASE_MATHFN (BUILT_IN_LLROUND)
1867 CASE_MATHFN (BUILT_IN_LOG)
1868 CASE_MATHFN (BUILT_IN_LOG10)
1869 CASE_MATHFN (BUILT_IN_LOG1P)
1870 CASE_MATHFN (BUILT_IN_LOG2)
1871 CASE_MATHFN (BUILT_IN_LOGB)
1872 CASE_MATHFN (BUILT_IN_LRINT)
1873 CASE_MATHFN (BUILT_IN_LROUND)
1874 CASE_MATHFN (BUILT_IN_MODF)
1875 CASE_MATHFN (BUILT_IN_NAN)
1876 CASE_MATHFN (BUILT_IN_NANS)
1877 CASE_MATHFN (BUILT_IN_NEARBYINT)
1878 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1879 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1880 CASE_MATHFN (BUILT_IN_POW)
1881 CASE_MATHFN (BUILT_IN_POWI)
1882 CASE_MATHFN (BUILT_IN_POW10)
1883 CASE_MATHFN (BUILT_IN_REMAINDER)
1884 CASE_MATHFN (BUILT_IN_REMQUO)
1885 CASE_MATHFN (BUILT_IN_RINT)
1886 CASE_MATHFN (BUILT_IN_ROUND)
1887 CASE_MATHFN (BUILT_IN_SCALB)
1888 CASE_MATHFN (BUILT_IN_SCALBLN)
1889 CASE_MATHFN (BUILT_IN_SCALBN)
1890 CASE_MATHFN (BUILT_IN_SIGNBIT)
1891 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1892 CASE_MATHFN (BUILT_IN_SIN)
1893 CASE_MATHFN (BUILT_IN_SINCOS)
1894 CASE_MATHFN (BUILT_IN_SINH)
1895 CASE_MATHFN (BUILT_IN_SQRT)
1896 CASE_MATHFN (BUILT_IN_TAN)
1897 CASE_MATHFN (BUILT_IN_TANH)
1898 CASE_MATHFN (BUILT_IN_TGAMMA)
1899 CASE_MATHFN (BUILT_IN_TRUNC)
1900 CASE_MATHFN (BUILT_IN_Y0)
1901 CASE_MATHFN (BUILT_IN_Y1)
1902 CASE_MATHFN (BUILT_IN_YN)
1904 default:
1905 return NULL_TREE;
1908 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1909 return fn_arr[fcode];
1910 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1911 return fn_arr[fcodef];
1912 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1913 return fn_arr[fcodel];
1914 else
1915 return NULL_TREE;
1918 /* Like mathfn_built_in_1(), but always use the implicit array. */
1920 tree
1921 mathfn_built_in (tree type, enum built_in_function fn)
1923 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1926 /* If errno must be maintained, expand the RTL to check if the result,
1927 TARGET, of a built-in function call, EXP, is NaN, and if so set
1928 errno to EDOM. */
1930 static void
1931 expand_errno_check (tree exp, rtx target)
1933 rtx lab = gen_label_rtx ();
1935 /* Test the result; if it is NaN, set errno=EDOM because
1936 the argument was not in the domain. */
1937 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1938 NULL_RTX, NULL_RTX, lab,
1939 /* The jump is very likely. */
1940 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1942 #ifdef TARGET_EDOM
1943 /* If this built-in doesn't throw an exception, set errno directly. */
1944 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1946 #ifdef GEN_ERRNO_RTX
1947 rtx errno_rtx = GEN_ERRNO_RTX;
1948 #else
1949 rtx errno_rtx
1950 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1951 #endif
1952 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1953 emit_label (lab);
1954 return;
1956 #endif
1958 /* Make sure the library call isn't expanded as a tail call. */
1959 CALL_EXPR_TAILCALL (exp) = 0;
1961 /* We can't set errno=EDOM directly; let the library call do it.
1962 Pop the arguments right away in case the call gets deleted. */
1963 NO_DEFER_POP;
1964 expand_call (exp, target, 0);
1965 OK_DEFER_POP;
1966 emit_label (lab);
1969 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1970 Return NULL_RTX if a normal call should be emitted rather than expanding
1971 the function in-line. EXP is the expression that is a call to the builtin
1972 function; if convenient, the result should be placed in TARGET.
1973 SUBTARGET may be used as the target for computing one of EXP's operands. */
1975 static rtx
1976 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1978 optab builtin_optab;
1979 rtx op0, insns;
1980 tree fndecl = get_callee_fndecl (exp);
1981 enum machine_mode mode;
1982 bool errno_set = false;
1983 tree arg;
1985 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1986 return NULL_RTX;
1988 arg = CALL_EXPR_ARG (exp, 0);
1990 switch (DECL_FUNCTION_CODE (fndecl))
1992 CASE_FLT_FN (BUILT_IN_SQRT):
1993 errno_set = ! tree_expr_nonnegative_p (arg);
1994 builtin_optab = sqrt_optab;
1995 break;
1996 CASE_FLT_FN (BUILT_IN_EXP):
1997 errno_set = true; builtin_optab = exp_optab; break;
1998 CASE_FLT_FN (BUILT_IN_EXP10):
1999 CASE_FLT_FN (BUILT_IN_POW10):
2000 errno_set = true; builtin_optab = exp10_optab; break;
2001 CASE_FLT_FN (BUILT_IN_EXP2):
2002 errno_set = true; builtin_optab = exp2_optab; break;
2003 CASE_FLT_FN (BUILT_IN_EXPM1):
2004 errno_set = true; builtin_optab = expm1_optab; break;
2005 CASE_FLT_FN (BUILT_IN_LOGB):
2006 errno_set = true; builtin_optab = logb_optab; break;
2007 CASE_FLT_FN (BUILT_IN_LOG):
2008 errno_set = true; builtin_optab = log_optab; break;
2009 CASE_FLT_FN (BUILT_IN_LOG10):
2010 errno_set = true; builtin_optab = log10_optab; break;
2011 CASE_FLT_FN (BUILT_IN_LOG2):
2012 errno_set = true; builtin_optab = log2_optab; break;
2013 CASE_FLT_FN (BUILT_IN_LOG1P):
2014 errno_set = true; builtin_optab = log1p_optab; break;
2015 CASE_FLT_FN (BUILT_IN_ASIN):
2016 builtin_optab = asin_optab; break;
2017 CASE_FLT_FN (BUILT_IN_ACOS):
2018 builtin_optab = acos_optab; break;
2019 CASE_FLT_FN (BUILT_IN_TAN):
2020 builtin_optab = tan_optab; break;
2021 CASE_FLT_FN (BUILT_IN_ATAN):
2022 builtin_optab = atan_optab; break;
2023 CASE_FLT_FN (BUILT_IN_FLOOR):
2024 builtin_optab = floor_optab; break;
2025 CASE_FLT_FN (BUILT_IN_CEIL):
2026 builtin_optab = ceil_optab; break;
2027 CASE_FLT_FN (BUILT_IN_TRUNC):
2028 builtin_optab = btrunc_optab; break;
2029 CASE_FLT_FN (BUILT_IN_ROUND):
2030 builtin_optab = round_optab; break;
2031 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2032 builtin_optab = nearbyint_optab;
2033 if (flag_trapping_math)
2034 break;
2035 /* Else fallthrough and expand as rint. */
2036 CASE_FLT_FN (BUILT_IN_RINT):
2037 builtin_optab = rint_optab; break;
2038 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2039 builtin_optab = significand_optab; break;
2040 default:
2041 gcc_unreachable ();
2044 /* Make a suitable register to place result in. */
2045 mode = TYPE_MODE (TREE_TYPE (exp));
2047 if (! flag_errno_math || ! HONOR_NANS (mode))
2048 errno_set = false;
2050 /* Before working hard, check whether the instruction is available. */
2051 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2052 && (!errno_set || !optimize_insn_for_size_p ()))
2054 target = gen_reg_rtx (mode);
2056 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2057 need to expand the argument again. This way, we will not perform
2058 side-effects more the once. */
2059 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2061 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2063 start_sequence ();
2065 /* Compute into TARGET.
2066 Set TARGET to wherever the result comes back. */
2067 target = expand_unop (mode, builtin_optab, op0, target, 0);
2069 if (target != 0)
2071 if (errno_set)
2072 expand_errno_check (exp, target);
2074 /* Output the entire sequence. */
2075 insns = get_insns ();
2076 end_sequence ();
2077 emit_insn (insns);
2078 return target;
2081 /* If we were unable to expand via the builtin, stop the sequence
2082 (without outputting the insns) and call to the library function
2083 with the stabilized argument list. */
2084 end_sequence ();
2087 return expand_call (exp, target, target == const0_rtx);
2090 /* Expand a call to the builtin binary math functions (pow and atan2).
2091 Return NULL_RTX if a normal call should be emitted rather than expanding the
2092 function in-line. EXP is the expression that is a call to the builtin
2093 function; if convenient, the result should be placed in TARGET.
2094 SUBTARGET may be used as the target for computing one of EXP's
2095 operands. */
2097 static rtx
2098 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2100 optab builtin_optab;
2101 rtx op0, op1, insns;
2102 int op1_type = REAL_TYPE;
2103 tree fndecl = get_callee_fndecl (exp);
2104 tree arg0, arg1;
2105 enum machine_mode mode;
2106 bool errno_set = true;
2108 switch (DECL_FUNCTION_CODE (fndecl))
2110 CASE_FLT_FN (BUILT_IN_SCALBN):
2111 CASE_FLT_FN (BUILT_IN_SCALBLN):
2112 CASE_FLT_FN (BUILT_IN_LDEXP):
2113 op1_type = INTEGER_TYPE;
2114 default:
2115 break;
2118 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2119 return NULL_RTX;
2121 arg0 = CALL_EXPR_ARG (exp, 0);
2122 arg1 = CALL_EXPR_ARG (exp, 1);
2124 switch (DECL_FUNCTION_CODE (fndecl))
2126 CASE_FLT_FN (BUILT_IN_POW):
2127 builtin_optab = pow_optab; break;
2128 CASE_FLT_FN (BUILT_IN_ATAN2):
2129 builtin_optab = atan2_optab; break;
2130 CASE_FLT_FN (BUILT_IN_SCALB):
2131 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2132 return 0;
2133 builtin_optab = scalb_optab; break;
2134 CASE_FLT_FN (BUILT_IN_SCALBN):
2135 CASE_FLT_FN (BUILT_IN_SCALBLN):
2136 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2137 return 0;
2138 /* Fall through... */
2139 CASE_FLT_FN (BUILT_IN_LDEXP):
2140 builtin_optab = ldexp_optab; break;
2141 CASE_FLT_FN (BUILT_IN_FMOD):
2142 builtin_optab = fmod_optab; break;
2143 CASE_FLT_FN (BUILT_IN_REMAINDER):
2144 CASE_FLT_FN (BUILT_IN_DREM):
2145 builtin_optab = remainder_optab; break;
2146 default:
2147 gcc_unreachable ();
2150 /* Make a suitable register to place result in. */
2151 mode = TYPE_MODE (TREE_TYPE (exp));
2153 /* Before working hard, check whether the instruction is available. */
2154 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2155 return NULL_RTX;
2157 target = gen_reg_rtx (mode);
2159 if (! flag_errno_math || ! HONOR_NANS (mode))
2160 errno_set = false;
2162 if (errno_set && optimize_insn_for_size_p ())
2163 return 0;
2165 /* Always stabilize the argument list. */
2166 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2167 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2169 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2170 op1 = expand_normal (arg1);
2172 start_sequence ();
2174 /* Compute into TARGET.
2175 Set TARGET to wherever the result comes back. */
2176 target = expand_binop (mode, builtin_optab, op0, op1,
2177 target, 0, OPTAB_DIRECT);
2179 /* If we were unable to expand via the builtin, stop the sequence
2180 (without outputting the insns) and call to the library function
2181 with the stabilized argument list. */
2182 if (target == 0)
2184 end_sequence ();
2185 return expand_call (exp, target, target == const0_rtx);
2188 if (errno_set)
2189 expand_errno_check (exp, target);
2191 /* Output the entire sequence. */
2192 insns = get_insns ();
2193 end_sequence ();
2194 emit_insn (insns);
2196 return target;
2199 /* Expand a call to the builtin trinary math functions (fma).
2200 Return NULL_RTX if a normal call should be emitted rather than expanding the
2201 function in-line. EXP is the expression that is a call to the builtin
2202 function; if convenient, the result should be placed in TARGET.
2203 SUBTARGET may be used as the target for computing one of EXP's
2204 operands. */
2206 static rtx
2207 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2209 optab builtin_optab;
2210 rtx op0, op1, op2, insns;
2211 tree fndecl = get_callee_fndecl (exp);
2212 tree arg0, arg1, arg2;
2213 enum machine_mode mode;
2215 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2216 return NULL_RTX;
2218 arg0 = CALL_EXPR_ARG (exp, 0);
2219 arg1 = CALL_EXPR_ARG (exp, 1);
2220 arg2 = CALL_EXPR_ARG (exp, 2);
2222 switch (DECL_FUNCTION_CODE (fndecl))
2224 CASE_FLT_FN (BUILT_IN_FMA):
2225 builtin_optab = fma_optab; break;
2226 default:
2227 gcc_unreachable ();
2230 /* Make a suitable register to place result in. */
2231 mode = TYPE_MODE (TREE_TYPE (exp));
2233 /* Before working hard, check whether the instruction is available. */
2234 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2235 return NULL_RTX;
2237 target = gen_reg_rtx (mode);
2239 /* Always stabilize the argument list. */
2240 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2241 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2242 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2244 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2245 op1 = expand_normal (arg1);
2246 op2 = expand_normal (arg2);
2248 start_sequence ();
2250 /* Compute into TARGET.
2251 Set TARGET to wherever the result comes back. */
2252 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2253 target, 0);
2255 /* If we were unable to expand via the builtin, stop the sequence
2256 (without outputting the insns) and call to the library function
2257 with the stabilized argument list. */
2258 if (target == 0)
2260 end_sequence ();
2261 return expand_call (exp, target, target == const0_rtx);
2264 /* Output the entire sequence. */
2265 insns = get_insns ();
2266 end_sequence ();
2267 emit_insn (insns);
2269 return target;
2272 /* Expand a call to the builtin sin and cos math functions.
2273 Return NULL_RTX if a normal call should be emitted rather than expanding the
2274 function in-line. EXP is the expression that is a call to the builtin
2275 function; if convenient, the result should be placed in TARGET.
2276 SUBTARGET may be used as the target for computing one of EXP's
2277 operands. */
2279 static rtx
2280 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2282 optab builtin_optab;
2283 rtx op0, insns;
2284 tree fndecl = get_callee_fndecl (exp);
2285 enum machine_mode mode;
2286 tree arg;
2288 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2289 return NULL_RTX;
2291 arg = CALL_EXPR_ARG (exp, 0);
2293 switch (DECL_FUNCTION_CODE (fndecl))
2295 CASE_FLT_FN (BUILT_IN_SIN):
2296 CASE_FLT_FN (BUILT_IN_COS):
2297 builtin_optab = sincos_optab; break;
2298 default:
2299 gcc_unreachable ();
2302 /* Make a suitable register to place result in. */
2303 mode = TYPE_MODE (TREE_TYPE (exp));
2305 /* Check if sincos insn is available, otherwise fallback
2306 to sin or cos insn. */
2307 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2308 switch (DECL_FUNCTION_CODE (fndecl))
2310 CASE_FLT_FN (BUILT_IN_SIN):
2311 builtin_optab = sin_optab; break;
2312 CASE_FLT_FN (BUILT_IN_COS):
2313 builtin_optab = cos_optab; break;
2314 default:
2315 gcc_unreachable ();
2318 /* Before working hard, check whether the instruction is available. */
2319 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2321 target = gen_reg_rtx (mode);
2323 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2324 need to expand the argument again. This way, we will not perform
2325 side-effects more the once. */
2326 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2328 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2330 start_sequence ();
2332 /* Compute into TARGET.
2333 Set TARGET to wherever the result comes back. */
2334 if (builtin_optab == sincos_optab)
2336 int result;
2338 switch (DECL_FUNCTION_CODE (fndecl))
2340 CASE_FLT_FN (BUILT_IN_SIN):
2341 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2342 break;
2343 CASE_FLT_FN (BUILT_IN_COS):
2344 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2345 break;
2346 default:
2347 gcc_unreachable ();
2349 gcc_assert (result);
2351 else
2353 target = expand_unop (mode, builtin_optab, op0, target, 0);
2356 if (target != 0)
2358 /* Output the entire sequence. */
2359 insns = get_insns ();
2360 end_sequence ();
2361 emit_insn (insns);
2362 return target;
2365 /* If we were unable to expand via the builtin, stop the sequence
2366 (without outputting the insns) and call to the library function
2367 with the stabilized argument list. */
2368 end_sequence ();
2371 target = expand_call (exp, target, target == const0_rtx);
2373 return target;
2376 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2377 return an RTL instruction code that implements the functionality.
2378 If that isn't possible or available return CODE_FOR_nothing. */
2380 static enum insn_code
2381 interclass_mathfn_icode (tree arg, tree fndecl)
2383 bool errno_set = false;
2384 optab builtin_optab = 0;
2385 enum machine_mode mode;
2387 switch (DECL_FUNCTION_CODE (fndecl))
2389 CASE_FLT_FN (BUILT_IN_ILOGB):
2390 errno_set = true; builtin_optab = ilogb_optab; break;
2391 CASE_FLT_FN (BUILT_IN_ISINF):
2392 builtin_optab = isinf_optab; break;
2393 case BUILT_IN_ISNORMAL:
2394 case BUILT_IN_ISFINITE:
2395 CASE_FLT_FN (BUILT_IN_FINITE):
2396 case BUILT_IN_FINITED32:
2397 case BUILT_IN_FINITED64:
2398 case BUILT_IN_FINITED128:
2399 case BUILT_IN_ISINFD32:
2400 case BUILT_IN_ISINFD64:
2401 case BUILT_IN_ISINFD128:
2402 /* These builtins have no optabs (yet). */
2403 break;
2404 default:
2405 gcc_unreachable ();
2408 /* There's no easy way to detect the case we need to set EDOM. */
2409 if (flag_errno_math && errno_set)
2410 return CODE_FOR_nothing;
2412 /* Optab mode depends on the mode of the input argument. */
2413 mode = TYPE_MODE (TREE_TYPE (arg));
2415 if (builtin_optab)
2416 return optab_handler (builtin_optab, mode);
2417 return CODE_FOR_nothing;
2420 /* Expand a call to one of the builtin math functions that operate on
2421 floating point argument and output an integer result (ilogb, isinf,
2422 isnan, etc).
2423 Return 0 if a normal call should be emitted rather than expanding the
2424 function in-line. EXP is the expression that is a call to the builtin
2425 function; if convenient, the result should be placed in TARGET. */
2427 static rtx
2428 expand_builtin_interclass_mathfn (tree exp, rtx target)
2430 enum insn_code icode = CODE_FOR_nothing;
2431 rtx op0;
2432 tree fndecl = get_callee_fndecl (exp);
2433 enum machine_mode mode;
2434 tree arg;
2436 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2437 return NULL_RTX;
2439 arg = CALL_EXPR_ARG (exp, 0);
2440 icode = interclass_mathfn_icode (arg, fndecl);
2441 mode = TYPE_MODE (TREE_TYPE (arg));
2443 if (icode != CODE_FOR_nothing)
2445 struct expand_operand ops[1];
2446 rtx last = get_last_insn ();
2447 tree orig_arg = arg;
2449 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2450 need to expand the argument again. This way, we will not perform
2451 side-effects more the once. */
2452 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2454 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2456 if (mode != GET_MODE (op0))
2457 op0 = convert_to_mode (mode, op0, 0);
2459 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2460 if (maybe_legitimize_operands (icode, 0, 1, ops)
2461 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2462 return ops[0].value;
2464 delete_insns_since (last);
2465 CALL_EXPR_ARG (exp, 0) = orig_arg;
2468 return NULL_RTX;
2471 /* Expand a call to the builtin sincos math function.
2472 Return NULL_RTX if a normal call should be emitted rather than expanding the
2473 function in-line. EXP is the expression that is a call to the builtin
2474 function. */
2476 static rtx
2477 expand_builtin_sincos (tree exp)
2479 rtx op0, op1, op2, target1, target2;
2480 enum machine_mode mode;
2481 tree arg, sinp, cosp;
2482 int result;
2483 location_t loc = EXPR_LOCATION (exp);
2484 tree alias_type, alias_off;
2486 if (!validate_arglist (exp, REAL_TYPE,
2487 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2488 return NULL_RTX;
2490 arg = CALL_EXPR_ARG (exp, 0);
2491 sinp = CALL_EXPR_ARG (exp, 1);
2492 cosp = CALL_EXPR_ARG (exp, 2);
2494 /* Make a suitable register to place result in. */
2495 mode = TYPE_MODE (TREE_TYPE (arg));
2497 /* Check if sincos insn is available, otherwise emit the call. */
2498 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2499 return NULL_RTX;
2501 target1 = gen_reg_rtx (mode);
2502 target2 = gen_reg_rtx (mode);
2504 op0 = expand_normal (arg);
2505 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2506 alias_off = build_int_cst (alias_type, 0);
2507 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2508 sinp, alias_off));
2509 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2510 cosp, alias_off));
2512 /* Compute into target1 and target2.
2513 Set TARGET to wherever the result comes back. */
2514 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2515 gcc_assert (result);
2517 /* Move target1 and target2 to the memory locations indicated
2518 by op1 and op2. */
2519 emit_move_insn (op1, target1);
2520 emit_move_insn (op2, target2);
2522 return const0_rtx;
2525 /* Expand a call to the internal cexpi builtin to the sincos math function.
2526 EXP is the expression that is a call to the builtin function; if convenient,
2527 the result should be placed in TARGET. */
2529 static rtx
2530 expand_builtin_cexpi (tree exp, rtx target)
2532 tree fndecl = get_callee_fndecl (exp);
2533 tree arg, type;
2534 enum machine_mode mode;
2535 rtx op0, op1, op2;
2536 location_t loc = EXPR_LOCATION (exp);
2538 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2539 return NULL_RTX;
2541 arg = CALL_EXPR_ARG (exp, 0);
2542 type = TREE_TYPE (arg);
2543 mode = TYPE_MODE (TREE_TYPE (arg));
2545 /* Try expanding via a sincos optab, fall back to emitting a libcall
2546 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2547 is only generated from sincos, cexp or if we have either of them. */
2548 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2550 op1 = gen_reg_rtx (mode);
2551 op2 = gen_reg_rtx (mode);
2553 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2555 /* Compute into op1 and op2. */
2556 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2558 else if (TARGET_HAS_SINCOS)
2560 tree call, fn = NULL_TREE;
2561 tree top1, top2;
2562 rtx op1a, op2a;
2564 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2565 fn = built_in_decls[BUILT_IN_SINCOSF];
2566 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2567 fn = built_in_decls[BUILT_IN_SINCOS];
2568 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2569 fn = built_in_decls[BUILT_IN_SINCOSL];
2570 else
2571 gcc_unreachable ();
2573 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2574 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2575 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2576 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2577 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2578 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2580 /* Make sure not to fold the sincos call again. */
2581 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2582 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2583 call, 3, arg, top1, top2));
2585 else
2587 tree call, fn = NULL_TREE, narg;
2588 tree ctype = build_complex_type (type);
2590 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2591 fn = built_in_decls[BUILT_IN_CEXPF];
2592 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2593 fn = built_in_decls[BUILT_IN_CEXP];
2594 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2595 fn = built_in_decls[BUILT_IN_CEXPL];
2596 else
2597 gcc_unreachable ();
2599 /* If we don't have a decl for cexp create one. This is the
2600 friendliest fallback if the user calls __builtin_cexpi
2601 without full target C99 function support. */
2602 if (fn == NULL_TREE)
2604 tree fntype;
2605 const char *name = NULL;
2607 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2608 name = "cexpf";
2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2610 name = "cexp";
2611 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2612 name = "cexpl";
2614 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2615 fn = build_fn_decl (name, fntype);
2618 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2619 build_real (type, dconst0), arg);
2621 /* Make sure not to fold the cexp call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623 return expand_expr (build_call_nary (ctype, call, 1, narg),
2624 target, VOIDmode, EXPAND_NORMAL);
2627 /* Now build the proper return type. */
2628 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2629 make_tree (TREE_TYPE (arg), op2),
2630 make_tree (TREE_TYPE (arg), op1)),
2631 target, VOIDmode, EXPAND_NORMAL);
2634 /* Conveniently construct a function call expression. FNDECL names the
2635 function to be called, N is the number of arguments, and the "..."
2636 parameters are the argument expressions. Unlike build_call_exr
2637 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2639 static tree
2640 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2642 va_list ap;
2643 tree fntype = TREE_TYPE (fndecl);
2644 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2646 va_start (ap, n);
2647 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2648 va_end (ap);
2649 SET_EXPR_LOCATION (fn, loc);
2650 return fn;
2653 /* Expand a call to one of the builtin rounding functions gcc defines
2654 as an extension (lfloor and lceil). As these are gcc extensions we
2655 do not need to worry about setting errno to EDOM.
2656 If expanding via optab fails, lower expression to (int)(floor(x)).
2657 EXP is the expression that is a call to the builtin function;
2658 if convenient, the result should be placed in TARGET. */
2660 static rtx
2661 expand_builtin_int_roundingfn (tree exp, rtx target)
2663 convert_optab builtin_optab;
2664 rtx op0, insns, tmp;
2665 tree fndecl = get_callee_fndecl (exp);
2666 enum built_in_function fallback_fn;
2667 tree fallback_fndecl;
2668 enum machine_mode mode;
2669 tree arg;
2671 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2672 gcc_unreachable ();
2674 arg = CALL_EXPR_ARG (exp, 0);
2676 switch (DECL_FUNCTION_CODE (fndecl))
2678 CASE_FLT_FN (BUILT_IN_LCEIL):
2679 CASE_FLT_FN (BUILT_IN_LLCEIL):
2680 builtin_optab = lceil_optab;
2681 fallback_fn = BUILT_IN_CEIL;
2682 break;
2684 CASE_FLT_FN (BUILT_IN_LFLOOR):
2685 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2686 builtin_optab = lfloor_optab;
2687 fallback_fn = BUILT_IN_FLOOR;
2688 break;
2690 default:
2691 gcc_unreachable ();
2694 /* Make a suitable register to place result in. */
2695 mode = TYPE_MODE (TREE_TYPE (exp));
2697 target = gen_reg_rtx (mode);
2699 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2700 need to expand the argument again. This way, we will not perform
2701 side-effects more the once. */
2702 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2704 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2706 start_sequence ();
2708 /* Compute into TARGET. */
2709 if (expand_sfix_optab (target, op0, builtin_optab))
2711 /* Output the entire sequence. */
2712 insns = get_insns ();
2713 end_sequence ();
2714 emit_insn (insns);
2715 return target;
2718 /* If we were unable to expand via the builtin, stop the sequence
2719 (without outputting the insns). */
2720 end_sequence ();
2722 /* Fall back to floating point rounding optab. */
2723 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2725 /* For non-C99 targets we may end up without a fallback fndecl here
2726 if the user called __builtin_lfloor directly. In this case emit
2727 a call to the floor/ceil variants nevertheless. This should result
2728 in the best user experience for not full C99 targets. */
2729 if (fallback_fndecl == NULL_TREE)
2731 tree fntype;
2732 const char *name = NULL;
2734 switch (DECL_FUNCTION_CODE (fndecl))
2736 case BUILT_IN_LCEIL:
2737 case BUILT_IN_LLCEIL:
2738 name = "ceil";
2739 break;
2740 case BUILT_IN_LCEILF:
2741 case BUILT_IN_LLCEILF:
2742 name = "ceilf";
2743 break;
2744 case BUILT_IN_LCEILL:
2745 case BUILT_IN_LLCEILL:
2746 name = "ceill";
2747 break;
2748 case BUILT_IN_LFLOOR:
2749 case BUILT_IN_LLFLOOR:
2750 name = "floor";
2751 break;
2752 case BUILT_IN_LFLOORF:
2753 case BUILT_IN_LLFLOORF:
2754 name = "floorf";
2755 break;
2756 case BUILT_IN_LFLOORL:
2757 case BUILT_IN_LLFLOORL:
2758 name = "floorl";
2759 break;
2760 default:
2761 gcc_unreachable ();
2764 fntype = build_function_type_list (TREE_TYPE (arg),
2765 TREE_TYPE (arg), NULL_TREE);
2766 fallback_fndecl = build_fn_decl (name, fntype);
2769 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2771 tmp = expand_normal (exp);
2773 /* Truncate the result of floating point optab to integer
2774 via expand_fix (). */
2775 target = gen_reg_rtx (mode);
2776 expand_fix (target, tmp, 0);
2778 return target;
2781 /* Expand a call to one of the builtin math functions doing integer
2782 conversion (lrint).
2783 Return 0 if a normal call should be emitted rather than expanding the
2784 function in-line. EXP is the expression that is a call to the builtin
2785 function; if convenient, the result should be placed in TARGET. */
2787 static rtx
2788 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2790 convert_optab builtin_optab;
2791 rtx op0, insns;
2792 tree fndecl = get_callee_fndecl (exp);
2793 tree arg;
2794 enum machine_mode mode;
2796 /* There's no easy way to detect the case we need to set EDOM. */
2797 if (flag_errno_math)
2798 return NULL_RTX;
2800 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2801 gcc_unreachable ();
2803 arg = CALL_EXPR_ARG (exp, 0);
2805 switch (DECL_FUNCTION_CODE (fndecl))
2807 CASE_FLT_FN (BUILT_IN_LRINT):
2808 CASE_FLT_FN (BUILT_IN_LLRINT):
2809 builtin_optab = lrint_optab; break;
2810 CASE_FLT_FN (BUILT_IN_LROUND):
2811 CASE_FLT_FN (BUILT_IN_LLROUND):
2812 builtin_optab = lround_optab; break;
2813 default:
2814 gcc_unreachable ();
2817 /* Make a suitable register to place result in. */
2818 mode = TYPE_MODE (TREE_TYPE (exp));
2820 target = gen_reg_rtx (mode);
2822 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2823 need to expand the argument again. This way, we will not perform
2824 side-effects more the once. */
2825 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2827 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2829 start_sequence ();
2831 if (expand_sfix_optab (target, op0, builtin_optab))
2833 /* Output the entire sequence. */
2834 insns = get_insns ();
2835 end_sequence ();
2836 emit_insn (insns);
2837 return target;
2840 /* If we were unable to expand via the builtin, stop the sequence
2841 (without outputting the insns) and call to the library function
2842 with the stabilized argument list. */
2843 end_sequence ();
2845 target = expand_call (exp, target, target == const0_rtx);
2847 return target;
2850 /* To evaluate powi(x,n), the floating point value x raised to the
2851 constant integer exponent n, we use a hybrid algorithm that
2852 combines the "window method" with look-up tables. For an
2853 introduction to exponentiation algorithms and "addition chains",
2854 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2855 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2856 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2857 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2859 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2860 multiplications to inline before calling the system library's pow
2861 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2862 so this default never requires calling pow, powf or powl. */
2864 #ifndef POWI_MAX_MULTS
2865 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2866 #endif
2868 /* The size of the "optimal power tree" lookup table. All
2869 exponents less than this value are simply looked up in the
2870 powi_table below. This threshold is also used to size the
2871 cache of pseudo registers that hold intermediate results. */
2872 #define POWI_TABLE_SIZE 256
2874 /* The size, in bits of the window, used in the "window method"
2875 exponentiation algorithm. This is equivalent to a radix of
2876 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2877 #define POWI_WINDOW_SIZE 3
2879 /* The following table is an efficient representation of an
2880 "optimal power tree". For each value, i, the corresponding
2881 value, j, in the table states than an optimal evaluation
2882 sequence for calculating pow(x,i) can be found by evaluating
2883 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2884 100 integers is given in Knuth's "Seminumerical algorithms". */
2886 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2888 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2889 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2890 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2891 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2892 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2893 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2894 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2895 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2896 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2897 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2898 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2899 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2900 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2901 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2902 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2903 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2904 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2905 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2906 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2907 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2908 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2909 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2910 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2911 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2912 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2913 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2914 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2915 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2916 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2917 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2918 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2919 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2923 /* Return the number of multiplications required to calculate
2924 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2925 subroutine of powi_cost. CACHE is an array indicating
2926 which exponents have already been calculated. */
2928 static int
2929 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2931 /* If we've already calculated this exponent, then this evaluation
2932 doesn't require any additional multiplications. */
2933 if (cache[n])
2934 return 0;
2936 cache[n] = true;
2937 return powi_lookup_cost (n - powi_table[n], cache)
2938 + powi_lookup_cost (powi_table[n], cache) + 1;
2941 /* Return the number of multiplications required to calculate
2942 powi(x,n) for an arbitrary x, given the exponent N. This
2943 function needs to be kept in sync with expand_powi below. */
2945 static int
2946 powi_cost (HOST_WIDE_INT n)
2948 bool cache[POWI_TABLE_SIZE];
2949 unsigned HOST_WIDE_INT digit;
2950 unsigned HOST_WIDE_INT val;
2951 int result;
2953 if (n == 0)
2954 return 0;
2956 /* Ignore the reciprocal when calculating the cost. */
2957 val = (n < 0) ? -n : n;
2959 /* Initialize the exponent cache. */
2960 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2961 cache[1] = true;
2963 result = 0;
2965 while (val >= POWI_TABLE_SIZE)
2967 if (val & 1)
2969 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2970 result += powi_lookup_cost (digit, cache)
2971 + POWI_WINDOW_SIZE + 1;
2972 val >>= POWI_WINDOW_SIZE;
2974 else
2976 val >>= 1;
2977 result++;
2981 return result + powi_lookup_cost (val, cache);
2984 /* Recursive subroutine of expand_powi. This function takes the array,
2985 CACHE, of already calculated exponents and an exponent N and returns
2986 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2988 static rtx
2989 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2991 unsigned HOST_WIDE_INT digit;
2992 rtx target, result;
2993 rtx op0, op1;
2995 if (n < POWI_TABLE_SIZE)
2997 if (cache[n])
2998 return cache[n];
3000 target = gen_reg_rtx (mode);
3001 cache[n] = target;
3003 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
3004 op1 = expand_powi_1 (mode, powi_table[n], cache);
3006 else if (n & 1)
3008 target = gen_reg_rtx (mode);
3009 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3010 op0 = expand_powi_1 (mode, n - digit, cache);
3011 op1 = expand_powi_1 (mode, digit, cache);
3013 else
3015 target = gen_reg_rtx (mode);
3016 op0 = expand_powi_1 (mode, n >> 1, cache);
3017 op1 = op0;
3020 result = expand_mult (mode, op0, op1, target, 0);
3021 if (result != target)
3022 emit_move_insn (target, result);
3023 return target;
3026 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
3027 floating point operand in mode MODE, and N is the exponent. This
3028 function needs to be kept in sync with powi_cost above. */
3030 static rtx
3031 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
3033 rtx cache[POWI_TABLE_SIZE];
3034 rtx result;
3036 if (n == 0)
3037 return CONST1_RTX (mode);
3039 memset (cache, 0, sizeof (cache));
3040 cache[1] = x;
3042 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
3044 /* If the original exponent was negative, reciprocate the result. */
3045 if (n < 0)
3046 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3047 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3049 return result;
3052 /* Fold a builtin function call to pow, powf, or powl into a series of sqrts or
3053 cbrts. Return NULL_RTX if no simplification can be made or expand the tree
3054 if we can simplify it. */
3055 static rtx
3056 expand_builtin_pow_root (location_t loc, tree arg0, tree arg1, tree type,
3057 rtx subtarget)
3059 if (TREE_CODE (arg1) == REAL_CST
3060 && !TREE_OVERFLOW (arg1)
3061 && flag_unsafe_math_optimizations)
3063 enum machine_mode mode = TYPE_MODE (type);
3064 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
3065 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
3066 REAL_VALUE_TYPE c = TREE_REAL_CST (arg1);
3067 tree op = NULL_TREE;
3069 if (sqrtfn)
3071 /* Optimize pow (x, 0.5) into sqrt. */
3072 if (REAL_VALUES_EQUAL (c, dconsthalf))
3073 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3075 /* Don't do this optimization if we don't have a sqrt insn. */
3076 else if (optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
3078 REAL_VALUE_TYPE dconst1_4 = dconst1;
3079 REAL_VALUE_TYPE dconst3_4;
3080 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
3082 real_from_integer (&dconst3_4, VOIDmode, 3, 0, 0);
3083 SET_REAL_EXP (&dconst3_4, REAL_EXP (&dconst3_4) - 2);
3085 /* Optimize pow (x, 0.25) into sqrt (sqrt (x)). Assume on most
3086 machines that a builtin sqrt instruction is smaller than a
3087 call to pow with 0.25, so do this optimization even if
3088 -Os. */
3089 if (REAL_VALUES_EQUAL (c, dconst1_4))
3091 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3092 op = build_call_nofold_loc (loc, sqrtfn, 1, op);
3095 /* Optimize pow (x, 0.75) = sqrt (x) * sqrt (sqrt (x)) unless we
3096 are optimizing for space. */
3097 else if (optimize_insn_for_speed_p ()
3098 && !TREE_SIDE_EFFECTS (arg0)
3099 && REAL_VALUES_EQUAL (c, dconst3_4))
3101 tree sqrt1 = build_call_expr_loc (loc, sqrtfn, 1, arg0);
3102 tree sqrt2 = builtin_save_expr (sqrt1);
3103 tree sqrt3 = build_call_expr_loc (loc, sqrtfn, 1, sqrt1);
3104 op = fold_build2_loc (loc, MULT_EXPR, type, sqrt2, sqrt3);
3109 /* Check whether we can do cbrt insstead of pow (x, 1./3.) and
3110 cbrt/sqrts instead of pow (x, 1./6.). */
3111 if (cbrtfn && ! op
3112 && (tree_expr_nonnegative_p (arg0) || !HONOR_NANS (mode)))
3114 /* First try 1/3. */
3115 REAL_VALUE_TYPE dconst1_3
3116 = real_value_truncate (mode, dconst_third ());
3118 if (REAL_VALUES_EQUAL (c, dconst1_3))
3119 op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
3121 /* Now try 1/6. */
3122 else if (optimize_insn_for_speed_p ()
3123 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
3125 REAL_VALUE_TYPE dconst1_6 = dconst1_3;
3126 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
3128 if (REAL_VALUES_EQUAL (c, dconst1_6))
3130 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3131 op = build_call_nofold_loc (loc, cbrtfn, 1, op);
3136 if (op)
3137 return expand_expr (op, subtarget, mode, EXPAND_NORMAL);
3140 return NULL_RTX;
3143 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
3144 a normal call should be emitted rather than expanding the function
3145 in-line. EXP is the expression that is a call to the builtin
3146 function; if convenient, the result should be placed in TARGET. */
3148 static rtx
3149 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
3151 tree arg0, arg1;
3152 tree fn, narg0;
3153 tree type = TREE_TYPE (exp);
3154 REAL_VALUE_TYPE cint, c, c2;
3155 HOST_WIDE_INT n;
3156 rtx op, op2;
3157 enum machine_mode mode = TYPE_MODE (type);
3159 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
3160 return NULL_RTX;
3162 arg0 = CALL_EXPR_ARG (exp, 0);
3163 arg1 = CALL_EXPR_ARG (exp, 1);
3165 if (TREE_CODE (arg1) != REAL_CST
3166 || TREE_OVERFLOW (arg1))
3167 return expand_builtin_mathfn_2 (exp, target, subtarget);
3169 /* Handle constant exponents. */
3171 /* For integer valued exponents we can expand to an optimal multiplication
3172 sequence using expand_powi. */
3173 c = TREE_REAL_CST (arg1);
3174 n = real_to_integer (&c);
3175 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3176 if (real_identical (&c, &cint)
3177 && ((n >= -1 && n <= 2)
3178 || (flag_unsafe_math_optimizations
3179 && optimize_insn_for_speed_p ()
3180 && powi_cost (n) <= POWI_MAX_MULTS)))
3182 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3183 if (n != 1)
3185 op = force_reg (mode, op);
3186 op = expand_powi (op, mode, n);
3188 return op;
3191 narg0 = builtin_save_expr (arg0);
3193 /* If the exponent is not integer valued, check if it is half of an integer.
3194 In this case we can expand to sqrt (x) * x**(n/2). */
3195 fn = mathfn_built_in (type, BUILT_IN_SQRT);
3196 if (fn != NULL_TREE)
3198 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3199 n = real_to_integer (&c2);
3200 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3201 if (real_identical (&c2, &cint)
3202 && ((flag_unsafe_math_optimizations
3203 && optimize_insn_for_speed_p ()
3204 && powi_cost (n/2) <= POWI_MAX_MULTS)
3205 /* Even the c == 0.5 case cannot be done unconditionally
3206 when we need to preserve signed zeros, as
3207 pow (-0, 0.5) is +0, while sqrt(-0) is -0. */
3208 || (!HONOR_SIGNED_ZEROS (mode) && n == 1)
3209 /* For c == 1.5 we can assume that x * sqrt (x) is always
3210 smaller than pow (x, 1.5) if sqrt will not be expanded
3211 as a call. */
3212 || (n == 3
3213 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)))
3215 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3216 narg0);
3217 /* Use expand_expr in case the newly built call expression
3218 was folded to a non-call. */
3219 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3220 if (n != 1)
3222 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3223 op2 = force_reg (mode, op2);
3224 op2 = expand_powi (op2, mode, abs (n / 2));
3225 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3226 0, OPTAB_LIB_WIDEN);
3227 /* If the original exponent was negative, reciprocate the
3228 result. */
3229 if (n < 0)
3230 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3231 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3233 return op;
3237 /* Check whether we can do a series of sqrt or cbrt's instead of the pow
3238 call. */
3239 op = expand_builtin_pow_root (EXPR_LOCATION (exp), arg0, arg1, type,
3240 subtarget);
3241 if (op)
3242 return op;
3244 /* Try if the exponent is a third of an integer. In this case
3245 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3246 different from pow (x, 1./3.) due to rounding and behavior
3247 with negative x we need to constrain this transformation to
3248 unsafe math and positive x or finite math. */
3249 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3250 if (fn != NULL_TREE
3251 && flag_unsafe_math_optimizations
3252 && (tree_expr_nonnegative_p (arg0)
3253 || !HONOR_NANS (mode)))
3255 REAL_VALUE_TYPE dconst3;
3256 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3257 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3258 real_round (&c2, mode, &c2);
3259 n = real_to_integer (&c2);
3260 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3261 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3262 real_convert (&c2, mode, &c2);
3263 if (real_identical (&c2, &c)
3264 && ((optimize_insn_for_speed_p ()
3265 && powi_cost (n/3) <= POWI_MAX_MULTS)
3266 || n == 1))
3268 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3269 narg0);
3270 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3271 if (abs (n) % 3 == 2)
3272 op = expand_simple_binop (mode, MULT, op, op, op,
3273 0, OPTAB_LIB_WIDEN);
3274 if (n != 1)
3276 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3277 op2 = force_reg (mode, op2);
3278 op2 = expand_powi (op2, mode, abs (n / 3));
3279 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3280 0, OPTAB_LIB_WIDEN);
3281 /* If the original exponent was negative, reciprocate the
3282 result. */
3283 if (n < 0)
3284 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3285 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3287 return op;
3291 /* Fall back to optab expansion. */
3292 return expand_builtin_mathfn_2 (exp, target, subtarget);
3295 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3296 a normal call should be emitted rather than expanding the function
3297 in-line. EXP is the expression that is a call to the builtin
3298 function; if convenient, the result should be placed in TARGET. */
3300 static rtx
3301 expand_builtin_powi (tree exp, rtx target)
3303 tree arg0, arg1;
3304 rtx op0, op1;
3305 enum machine_mode mode;
3306 enum machine_mode mode2;
3308 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3309 return NULL_RTX;
3311 arg0 = CALL_EXPR_ARG (exp, 0);
3312 arg1 = CALL_EXPR_ARG (exp, 1);
3313 mode = TYPE_MODE (TREE_TYPE (exp));
3315 /* Handle constant power. */
3317 if (TREE_CODE (arg1) == INTEGER_CST
3318 && !TREE_OVERFLOW (arg1))
3320 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3322 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3323 Otherwise, check the number of multiplications required. */
3324 if ((TREE_INT_CST_HIGH (arg1) == 0
3325 || TREE_INT_CST_HIGH (arg1) == -1)
3326 && ((n >= -1 && n <= 2)
3327 || (optimize_insn_for_speed_p ()
3328 && powi_cost (n) <= POWI_MAX_MULTS)))
3330 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3331 op0 = force_reg (mode, op0);
3332 return expand_powi (op0, mode, n);
3336 /* Emit a libcall to libgcc. */
3338 /* Mode of the 2nd argument must match that of an int. */
3339 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3341 if (target == NULL_RTX)
3342 target = gen_reg_rtx (mode);
3344 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3345 if (GET_MODE (op0) != mode)
3346 op0 = convert_to_mode (mode, op0, 0);
3347 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3348 if (GET_MODE (op1) != mode2)
3349 op1 = convert_to_mode (mode2, op1, 0);
3351 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3352 target, LCT_CONST, mode, 2,
3353 op0, mode, op1, mode2);
3355 return target;
3358 /* Expand expression EXP which is a call to the strlen builtin. Return
3359 NULL_RTX if we failed the caller should emit a normal call, otherwise
3360 try to get the result in TARGET, if convenient. */
3362 static rtx
3363 expand_builtin_strlen (tree exp, rtx target,
3364 enum machine_mode target_mode)
3366 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3367 return NULL_RTX;
3368 else
3370 struct expand_operand ops[4];
3371 rtx pat;
3372 tree len;
3373 tree src = CALL_EXPR_ARG (exp, 0);
3374 rtx src_reg, before_strlen;
3375 enum machine_mode insn_mode = target_mode;
3376 enum insn_code icode = CODE_FOR_nothing;
3377 unsigned int align;
3379 /* If the length can be computed at compile-time, return it. */
3380 len = c_strlen (src, 0);
3381 if (len)
3382 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3384 /* If the length can be computed at compile-time and is constant
3385 integer, but there are side-effects in src, evaluate
3386 src for side-effects, then return len.
3387 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3388 can be optimized into: i++; x = 3; */
3389 len = c_strlen (src, 1);
3390 if (len && TREE_CODE (len) == INTEGER_CST)
3392 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3393 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3396 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3398 /* If SRC is not a pointer type, don't do this operation inline. */
3399 if (align == 0)
3400 return NULL_RTX;
3402 /* Bail out if we can't compute strlen in the right mode. */
3403 while (insn_mode != VOIDmode)
3405 icode = optab_handler (strlen_optab, insn_mode);
3406 if (icode != CODE_FOR_nothing)
3407 break;
3409 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3411 if (insn_mode == VOIDmode)
3412 return NULL_RTX;
3414 /* Make a place to hold the source address. We will not expand
3415 the actual source until we are sure that the expansion will
3416 not fail -- there are trees that cannot be expanded twice. */
3417 src_reg = gen_reg_rtx (Pmode);
3419 /* Mark the beginning of the strlen sequence so we can emit the
3420 source operand later. */
3421 before_strlen = get_last_insn ();
3423 create_output_operand (&ops[0], target, insn_mode);
3424 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3425 create_integer_operand (&ops[2], 0);
3426 create_integer_operand (&ops[3], align);
3427 if (!maybe_expand_insn (icode, 4, ops))
3428 return NULL_RTX;
3430 /* Now that we are assured of success, expand the source. */
3431 start_sequence ();
3432 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3433 if (pat != src_reg)
3434 emit_move_insn (src_reg, pat);
3435 pat = get_insns ();
3436 end_sequence ();
3438 if (before_strlen)
3439 emit_insn_after (pat, before_strlen);
3440 else
3441 emit_insn_before (pat, get_insns ());
3443 /* Return the value in the proper mode for this function. */
3444 if (GET_MODE (ops[0].value) == target_mode)
3445 target = ops[0].value;
3446 else if (target != 0)
3447 convert_move (target, ops[0].value, 0);
3448 else
3449 target = convert_to_mode (target_mode, ops[0].value, 0);
3451 return target;
3455 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3456 bytes from constant string DATA + OFFSET and return it as target
3457 constant. */
3459 static rtx
3460 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3461 enum machine_mode mode)
3463 const char *str = (const char *) data;
3465 gcc_assert (offset >= 0
3466 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3467 <= strlen (str) + 1));
3469 return c_readstr (str + offset, mode);
3472 /* Expand a call EXP to the memcpy builtin.
3473 Return NULL_RTX if we failed, the caller should emit a normal call,
3474 otherwise try to get the result in TARGET, if convenient (and in
3475 mode MODE if that's convenient). */
3477 static rtx
3478 expand_builtin_memcpy (tree exp, rtx target)
3480 if (!validate_arglist (exp,
3481 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3482 return NULL_RTX;
3483 else
3485 tree dest = CALL_EXPR_ARG (exp, 0);
3486 tree src = CALL_EXPR_ARG (exp, 1);
3487 tree len = CALL_EXPR_ARG (exp, 2);
3488 const char *src_str;
3489 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3490 unsigned int dest_align
3491 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3492 rtx dest_mem, src_mem, dest_addr, len_rtx;
3493 HOST_WIDE_INT expected_size = -1;
3494 unsigned int expected_align = 0;
3496 /* If DEST is not a pointer type, call the normal function. */
3497 if (dest_align == 0)
3498 return NULL_RTX;
3500 /* If either SRC is not a pointer type, don't do this
3501 operation in-line. */
3502 if (src_align == 0)
3503 return NULL_RTX;
3505 if (currently_expanding_gimple_stmt)
3506 stringop_block_profile (currently_expanding_gimple_stmt,
3507 &expected_align, &expected_size);
3509 if (expected_align < dest_align)
3510 expected_align = dest_align;
3511 dest_mem = get_memory_rtx (dest, len);
3512 set_mem_align (dest_mem, dest_align);
3513 len_rtx = expand_normal (len);
3514 src_str = c_getstr (src);
3516 /* If SRC is a string constant and block move would be done
3517 by pieces, we can avoid loading the string from memory
3518 and only stored the computed constants. */
3519 if (src_str
3520 && CONST_INT_P (len_rtx)
3521 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3522 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3523 CONST_CAST (char *, src_str),
3524 dest_align, false))
3526 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3527 builtin_memcpy_read_str,
3528 CONST_CAST (char *, src_str),
3529 dest_align, false, 0);
3530 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3531 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3532 return dest_mem;
3535 src_mem = get_memory_rtx (src, len);
3536 set_mem_align (src_mem, src_align);
3538 /* Copy word part most expediently. */
3539 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3540 CALL_EXPR_TAILCALL (exp)
3541 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3542 expected_align, expected_size);
3544 if (dest_addr == 0)
3546 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3547 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3549 return dest_addr;
3553 /* Expand a call EXP to the mempcpy builtin.
3554 Return NULL_RTX if we failed; the caller should emit a normal call,
3555 otherwise try to get the result in TARGET, if convenient (and in
3556 mode MODE if that's convenient). If ENDP is 0 return the
3557 destination pointer, if ENDP is 1 return the end pointer ala
3558 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3559 stpcpy. */
3561 static rtx
3562 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3564 if (!validate_arglist (exp,
3565 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3566 return NULL_RTX;
3567 else
3569 tree dest = CALL_EXPR_ARG (exp, 0);
3570 tree src = CALL_EXPR_ARG (exp, 1);
3571 tree len = CALL_EXPR_ARG (exp, 2);
3572 return expand_builtin_mempcpy_args (dest, src, len,
3573 target, mode, /*endp=*/ 1);
3577 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3578 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3579 so that this can also be called without constructing an actual CALL_EXPR.
3580 The other arguments and return value are the same as for
3581 expand_builtin_mempcpy. */
3583 static rtx
3584 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3585 rtx target, enum machine_mode mode, int endp)
3587 /* If return value is ignored, transform mempcpy into memcpy. */
3588 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3590 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3591 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3592 dest, src, len);
3593 return expand_expr (result, target, mode, EXPAND_NORMAL);
3595 else
3597 const char *src_str;
3598 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3599 unsigned int dest_align
3600 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3601 rtx dest_mem, src_mem, len_rtx;
3603 /* If either SRC or DEST is not a pointer type, don't do this
3604 operation in-line. */
3605 if (dest_align == 0 || src_align == 0)
3606 return NULL_RTX;
3608 /* If LEN is not constant, call the normal function. */
3609 if (! host_integerp (len, 1))
3610 return NULL_RTX;
3612 len_rtx = expand_normal (len);
3613 src_str = c_getstr (src);
3615 /* If SRC is a string constant and block move would be done
3616 by pieces, we can avoid loading the string from memory
3617 and only stored the computed constants. */
3618 if (src_str
3619 && CONST_INT_P (len_rtx)
3620 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3621 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3622 CONST_CAST (char *, src_str),
3623 dest_align, false))
3625 dest_mem = get_memory_rtx (dest, len);
3626 set_mem_align (dest_mem, dest_align);
3627 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3628 builtin_memcpy_read_str,
3629 CONST_CAST (char *, src_str),
3630 dest_align, false, endp);
3631 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3632 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3633 return dest_mem;
3636 if (CONST_INT_P (len_rtx)
3637 && can_move_by_pieces (INTVAL (len_rtx),
3638 MIN (dest_align, src_align)))
3640 dest_mem = get_memory_rtx (dest, len);
3641 set_mem_align (dest_mem, dest_align);
3642 src_mem = get_memory_rtx (src, len);
3643 set_mem_align (src_mem, src_align);
3644 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3645 MIN (dest_align, src_align), endp);
3646 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3647 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3648 return dest_mem;
3651 return NULL_RTX;
3655 #ifndef HAVE_movstr
3656 # define HAVE_movstr 0
3657 # define CODE_FOR_movstr CODE_FOR_nothing
3658 #endif
3660 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3661 we failed, the caller should emit a normal call, otherwise try to
3662 get the result in TARGET, if convenient. If ENDP is 0 return the
3663 destination pointer, if ENDP is 1 return the end pointer ala
3664 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3665 stpcpy. */
3667 static rtx
3668 expand_movstr (tree dest, tree src, rtx target, int endp)
3670 struct expand_operand ops[3];
3671 rtx dest_mem;
3672 rtx src_mem;
3674 if (!HAVE_movstr)
3675 return NULL_RTX;
3677 dest_mem = get_memory_rtx (dest, NULL);
3678 src_mem = get_memory_rtx (src, NULL);
3679 if (!endp)
3681 target = force_reg (Pmode, XEXP (dest_mem, 0));
3682 dest_mem = replace_equiv_address (dest_mem, target);
3685 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3686 create_fixed_operand (&ops[1], dest_mem);
3687 create_fixed_operand (&ops[2], src_mem);
3688 expand_insn (CODE_FOR_movstr, 3, ops);
3690 if (endp && target != const0_rtx)
3692 target = ops[0].value;
3693 /* movstr is supposed to set end to the address of the NUL
3694 terminator. If the caller requested a mempcpy-like return value,
3695 adjust it. */
3696 if (endp == 1)
3698 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3699 emit_move_insn (target, force_operand (tem, NULL_RTX));
3702 return target;
3705 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3706 NULL_RTX if we failed the caller should emit a normal call, otherwise
3707 try to get the result in TARGET, if convenient (and in mode MODE if that's
3708 convenient). */
3710 static rtx
3711 expand_builtin_strcpy (tree exp, rtx target)
3713 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3715 tree dest = CALL_EXPR_ARG (exp, 0);
3716 tree src = CALL_EXPR_ARG (exp, 1);
3717 return expand_builtin_strcpy_args (dest, src, target);
3719 return NULL_RTX;
3722 /* Helper function to do the actual work for expand_builtin_strcpy. The
3723 arguments to the builtin_strcpy call DEST and SRC are broken out
3724 so that this can also be called without constructing an actual CALL_EXPR.
3725 The other arguments and return value are the same as for
3726 expand_builtin_strcpy. */
3728 static rtx
3729 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3731 return expand_movstr (dest, src, target, /*endp=*/0);
3734 /* Expand a call EXP to the stpcpy builtin.
3735 Return NULL_RTX if we failed the caller should emit a normal call,
3736 otherwise try to get the result in TARGET, if convenient (and in
3737 mode MODE if that's convenient). */
3739 static rtx
3740 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3742 tree dst, src;
3743 location_t loc = EXPR_LOCATION (exp);
3745 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3746 return NULL_RTX;
3748 dst = CALL_EXPR_ARG (exp, 0);
3749 src = CALL_EXPR_ARG (exp, 1);
3751 /* If return value is ignored, transform stpcpy into strcpy. */
3752 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3754 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3755 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3756 return expand_expr (result, target, mode, EXPAND_NORMAL);
3758 else
3760 tree len, lenp1;
3761 rtx ret;
3763 /* Ensure we get an actual string whose length can be evaluated at
3764 compile-time, not an expression containing a string. This is
3765 because the latter will potentially produce pessimized code
3766 when used to produce the return value. */
3767 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3768 return expand_movstr (dst, src, target, /*endp=*/2);
3770 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3771 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3772 target, mode, /*endp=*/2);
3774 if (ret)
3775 return ret;
3777 if (TREE_CODE (len) == INTEGER_CST)
3779 rtx len_rtx = expand_normal (len);
3781 if (CONST_INT_P (len_rtx))
3783 ret = expand_builtin_strcpy_args (dst, src, target);
3785 if (ret)
3787 if (! target)
3789 if (mode != VOIDmode)
3790 target = gen_reg_rtx (mode);
3791 else
3792 target = gen_reg_rtx (GET_MODE (ret));
3794 if (GET_MODE (target) != GET_MODE (ret))
3795 ret = gen_lowpart (GET_MODE (target), ret);
3797 ret = plus_constant (ret, INTVAL (len_rtx));
3798 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3799 gcc_assert (ret);
3801 return target;
3806 return expand_movstr (dst, src, target, /*endp=*/2);
3810 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3811 bytes from constant string DATA + OFFSET and return it as target
3812 constant. */
3815 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3816 enum machine_mode mode)
3818 const char *str = (const char *) data;
3820 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3821 return const0_rtx;
3823 return c_readstr (str + offset, mode);
3826 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3827 NULL_RTX if we failed the caller should emit a normal call. */
3829 static rtx
3830 expand_builtin_strncpy (tree exp, rtx target)
3832 location_t loc = EXPR_LOCATION (exp);
3834 if (validate_arglist (exp,
3835 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3837 tree dest = CALL_EXPR_ARG (exp, 0);
3838 tree src = CALL_EXPR_ARG (exp, 1);
3839 tree len = CALL_EXPR_ARG (exp, 2);
3840 tree slen = c_strlen (src, 1);
3842 /* We must be passed a constant len and src parameter. */
3843 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3844 return NULL_RTX;
3846 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3848 /* We're required to pad with trailing zeros if the requested
3849 len is greater than strlen(s2)+1. In that case try to
3850 use store_by_pieces, if it fails, punt. */
3851 if (tree_int_cst_lt (slen, len))
3853 unsigned int dest_align
3854 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3855 const char *p = c_getstr (src);
3856 rtx dest_mem;
3858 if (!p || dest_align == 0 || !host_integerp (len, 1)
3859 || !can_store_by_pieces (tree_low_cst (len, 1),
3860 builtin_strncpy_read_str,
3861 CONST_CAST (char *, p),
3862 dest_align, false))
3863 return NULL_RTX;
3865 dest_mem = get_memory_rtx (dest, len);
3866 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3867 builtin_strncpy_read_str,
3868 CONST_CAST (char *, p), dest_align, false, 0);
3869 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3870 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3871 return dest_mem;
3874 return NULL_RTX;
3877 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3878 bytes from constant string DATA + OFFSET and return it as target
3879 constant. */
3882 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3883 enum machine_mode mode)
3885 const char *c = (const char *) data;
3886 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3888 memset (p, *c, GET_MODE_SIZE (mode));
3890 return c_readstr (p, mode);
3893 /* Callback routine for store_by_pieces. Return the RTL of a register
3894 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3895 char value given in the RTL register data. For example, if mode is
3896 4 bytes wide, return the RTL for 0x01010101*data. */
3898 static rtx
3899 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3900 enum machine_mode mode)
3902 rtx target, coeff;
3903 size_t size;
3904 char *p;
3906 size = GET_MODE_SIZE (mode);
3907 if (size == 1)
3908 return (rtx) data;
3910 p = XALLOCAVEC (char, size);
3911 memset (p, 1, size);
3912 coeff = c_readstr (p, mode);
3914 target = convert_to_mode (mode, (rtx) data, 1);
3915 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3916 return force_reg (mode, target);
3919 /* Expand expression EXP, which is a call to the memset builtin. Return
3920 NULL_RTX if we failed the caller should emit a normal call, otherwise
3921 try to get the result in TARGET, if convenient (and in mode MODE if that's
3922 convenient). */
3924 static rtx
3925 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3927 if (!validate_arglist (exp,
3928 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3929 return NULL_RTX;
3930 else
3932 tree dest = CALL_EXPR_ARG (exp, 0);
3933 tree val = CALL_EXPR_ARG (exp, 1);
3934 tree len = CALL_EXPR_ARG (exp, 2);
3935 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3939 /* Helper function to do the actual work for expand_builtin_memset. The
3940 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3941 so that this can also be called without constructing an actual CALL_EXPR.
3942 The other arguments and return value are the same as for
3943 expand_builtin_memset. */
3945 static rtx
3946 expand_builtin_memset_args (tree dest, tree val, tree len,
3947 rtx target, enum machine_mode mode, tree orig_exp)
3949 tree fndecl, fn;
3950 enum built_in_function fcode;
3951 enum machine_mode val_mode;
3952 char c;
3953 unsigned int dest_align;
3954 rtx dest_mem, dest_addr, len_rtx;
3955 HOST_WIDE_INT expected_size = -1;
3956 unsigned int expected_align = 0;
3958 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3960 /* If DEST is not a pointer type, don't do this operation in-line. */
3961 if (dest_align == 0)
3962 return NULL_RTX;
3964 if (currently_expanding_gimple_stmt)
3965 stringop_block_profile (currently_expanding_gimple_stmt,
3966 &expected_align, &expected_size);
3968 if (expected_align < dest_align)
3969 expected_align = dest_align;
3971 /* If the LEN parameter is zero, return DEST. */
3972 if (integer_zerop (len))
3974 /* Evaluate and ignore VAL in case it has side-effects. */
3975 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3976 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3979 /* Stabilize the arguments in case we fail. */
3980 dest = builtin_save_expr (dest);
3981 val = builtin_save_expr (val);
3982 len = builtin_save_expr (len);
3984 len_rtx = expand_normal (len);
3985 dest_mem = get_memory_rtx (dest, len);
3986 val_mode = TYPE_MODE (unsigned_char_type_node);
3988 if (TREE_CODE (val) != INTEGER_CST)
3990 rtx val_rtx;
3992 val_rtx = expand_normal (val);
3993 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3995 /* Assume that we can memset by pieces if we can store
3996 * the coefficients by pieces (in the required modes).
3997 * We can't pass builtin_memset_gen_str as that emits RTL. */
3998 c = 1;
3999 if (host_integerp (len, 1)
4000 && can_store_by_pieces (tree_low_cst (len, 1),
4001 builtin_memset_read_str, &c, dest_align,
4002 true))
4004 val_rtx = force_reg (val_mode, val_rtx);
4005 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4006 builtin_memset_gen_str, val_rtx, dest_align,
4007 true, 0);
4009 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4010 dest_align, expected_align,
4011 expected_size))
4012 goto do_libcall;
4014 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4015 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4016 return dest_mem;
4019 if (target_char_cast (val, &c))
4020 goto do_libcall;
4022 if (c)
4024 if (host_integerp (len, 1)
4025 && can_store_by_pieces (tree_low_cst (len, 1),
4026 builtin_memset_read_str, &c, dest_align,
4027 true))
4028 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4029 builtin_memset_read_str, &c, dest_align, true, 0);
4030 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4031 gen_int_mode (c, val_mode),
4032 dest_align, expected_align,
4033 expected_size))
4034 goto do_libcall;
4036 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4037 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4038 return dest_mem;
4041 set_mem_align (dest_mem, dest_align);
4042 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4043 CALL_EXPR_TAILCALL (orig_exp)
4044 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4045 expected_align, expected_size);
4047 if (dest_addr == 0)
4049 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4050 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4053 return dest_addr;
4055 do_libcall:
4056 fndecl = get_callee_fndecl (orig_exp);
4057 fcode = DECL_FUNCTION_CODE (fndecl);
4058 if (fcode == BUILT_IN_MEMSET)
4059 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4060 dest, val, len);
4061 else if (fcode == BUILT_IN_BZERO)
4062 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4063 dest, len);
4064 else
4065 gcc_unreachable ();
4066 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4067 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4068 return expand_call (fn, target, target == const0_rtx);
4071 /* Expand expression EXP, which is a call to the bzero builtin. Return
4072 NULL_RTX if we failed the caller should emit a normal call. */
4074 static rtx
4075 expand_builtin_bzero (tree exp)
4077 tree dest, size;
4078 location_t loc = EXPR_LOCATION (exp);
4080 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4081 return NULL_RTX;
4083 dest = CALL_EXPR_ARG (exp, 0);
4084 size = CALL_EXPR_ARG (exp, 1);
4086 /* New argument list transforming bzero(ptr x, int y) to
4087 memset(ptr x, int 0, size_t y). This is done this way
4088 so that if it isn't expanded inline, we fallback to
4089 calling bzero instead of memset. */
4091 return expand_builtin_memset_args (dest, integer_zero_node,
4092 fold_convert_loc (loc, sizetype, size),
4093 const0_rtx, VOIDmode, exp);
4096 /* Expand expression EXP, which is a call to the memcmp built-in function.
4097 Return NULL_RTX if we failed and the
4098 caller should emit a normal call, otherwise try to get the result in
4099 TARGET, if convenient (and in mode MODE, if that's convenient). */
4101 static rtx
4102 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4103 ATTRIBUTE_UNUSED enum machine_mode mode)
4105 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4107 if (!validate_arglist (exp,
4108 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4109 return NULL_RTX;
4111 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4113 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4114 rtx result;
4115 rtx insn;
4116 tree arg1 = CALL_EXPR_ARG (exp, 0);
4117 tree arg2 = CALL_EXPR_ARG (exp, 1);
4118 tree len = CALL_EXPR_ARG (exp, 2);
4120 unsigned int arg1_align
4121 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4122 unsigned int arg2_align
4123 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4124 enum machine_mode insn_mode;
4126 #ifdef HAVE_cmpmemsi
4127 if (HAVE_cmpmemsi)
4128 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4129 else
4130 #endif
4131 #ifdef HAVE_cmpstrnsi
4132 if (HAVE_cmpstrnsi)
4133 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4134 else
4135 #endif
4136 return NULL_RTX;
4138 /* If we don't have POINTER_TYPE, call the function. */
4139 if (arg1_align == 0 || arg2_align == 0)
4140 return NULL_RTX;
4142 /* Make a place to write the result of the instruction. */
4143 result = target;
4144 if (! (result != 0
4145 && REG_P (result) && GET_MODE (result) == insn_mode
4146 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4147 result = gen_reg_rtx (insn_mode);
4149 arg1_rtx = get_memory_rtx (arg1, len);
4150 arg2_rtx = get_memory_rtx (arg2, len);
4151 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4153 /* Set MEM_SIZE as appropriate. */
4154 if (CONST_INT_P (arg3_rtx))
4156 set_mem_size (arg1_rtx, arg3_rtx);
4157 set_mem_size (arg2_rtx, arg3_rtx);
4160 #ifdef HAVE_cmpmemsi
4161 if (HAVE_cmpmemsi)
4162 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4163 GEN_INT (MIN (arg1_align, arg2_align)));
4164 else
4165 #endif
4166 #ifdef HAVE_cmpstrnsi
4167 if (HAVE_cmpstrnsi)
4168 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4169 GEN_INT (MIN (arg1_align, arg2_align)));
4170 else
4171 #endif
4172 gcc_unreachable ();
4174 if (insn)
4175 emit_insn (insn);
4176 else
4177 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4178 TYPE_MODE (integer_type_node), 3,
4179 XEXP (arg1_rtx, 0), Pmode,
4180 XEXP (arg2_rtx, 0), Pmode,
4181 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4182 TYPE_UNSIGNED (sizetype)),
4183 TYPE_MODE (sizetype));
4185 /* Return the value in the proper mode for this function. */
4186 mode = TYPE_MODE (TREE_TYPE (exp));
4187 if (GET_MODE (result) == mode)
4188 return result;
4189 else if (target != 0)
4191 convert_move (target, result, 0);
4192 return target;
4194 else
4195 return convert_to_mode (mode, result, 0);
4197 #endif
4199 return NULL_RTX;
4202 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4203 if we failed the caller should emit a normal call, otherwise try to get
4204 the result in TARGET, if convenient. */
4206 static rtx
4207 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4209 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4210 return NULL_RTX;
4212 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4213 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4214 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4216 rtx arg1_rtx, arg2_rtx;
4217 rtx result, insn = NULL_RTX;
4218 tree fndecl, fn;
4219 tree arg1 = CALL_EXPR_ARG (exp, 0);
4220 tree arg2 = CALL_EXPR_ARG (exp, 1);
4222 unsigned int arg1_align
4223 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4224 unsigned int arg2_align
4225 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4227 /* If we don't have POINTER_TYPE, call the function. */
4228 if (arg1_align == 0 || arg2_align == 0)
4229 return NULL_RTX;
4231 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4232 arg1 = builtin_save_expr (arg1);
4233 arg2 = builtin_save_expr (arg2);
4235 arg1_rtx = get_memory_rtx (arg1, NULL);
4236 arg2_rtx = get_memory_rtx (arg2, NULL);
4238 #ifdef HAVE_cmpstrsi
4239 /* Try to call cmpstrsi. */
4240 if (HAVE_cmpstrsi)
4242 enum machine_mode insn_mode
4243 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4245 /* Make a place to write the result of the instruction. */
4246 result = target;
4247 if (! (result != 0
4248 && REG_P (result) && GET_MODE (result) == insn_mode
4249 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4250 result = gen_reg_rtx (insn_mode);
4252 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4253 GEN_INT (MIN (arg1_align, arg2_align)));
4255 #endif
4256 #ifdef HAVE_cmpstrnsi
4257 /* Try to determine at least one length and call cmpstrnsi. */
4258 if (!insn && HAVE_cmpstrnsi)
4260 tree len;
4261 rtx arg3_rtx;
4263 enum machine_mode insn_mode
4264 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4265 tree len1 = c_strlen (arg1, 1);
4266 tree len2 = c_strlen (arg2, 1);
4268 if (len1)
4269 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4270 if (len2)
4271 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4273 /* If we don't have a constant length for the first, use the length
4274 of the second, if we know it. We don't require a constant for
4275 this case; some cost analysis could be done if both are available
4276 but neither is constant. For now, assume they're equally cheap,
4277 unless one has side effects. If both strings have constant lengths,
4278 use the smaller. */
4280 if (!len1)
4281 len = len2;
4282 else if (!len2)
4283 len = len1;
4284 else if (TREE_SIDE_EFFECTS (len1))
4285 len = len2;
4286 else if (TREE_SIDE_EFFECTS (len2))
4287 len = len1;
4288 else if (TREE_CODE (len1) != INTEGER_CST)
4289 len = len2;
4290 else if (TREE_CODE (len2) != INTEGER_CST)
4291 len = len1;
4292 else if (tree_int_cst_lt (len1, len2))
4293 len = len1;
4294 else
4295 len = len2;
4297 /* If both arguments have side effects, we cannot optimize. */
4298 if (!len || TREE_SIDE_EFFECTS (len))
4299 goto do_libcall;
4301 arg3_rtx = expand_normal (len);
4303 /* Make a place to write the result of the instruction. */
4304 result = target;
4305 if (! (result != 0
4306 && REG_P (result) && GET_MODE (result) == insn_mode
4307 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4308 result = gen_reg_rtx (insn_mode);
4310 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4311 GEN_INT (MIN (arg1_align, arg2_align)));
4313 #endif
4315 if (insn)
4317 enum machine_mode mode;
4318 emit_insn (insn);
4320 /* Return the value in the proper mode for this function. */
4321 mode = TYPE_MODE (TREE_TYPE (exp));
4322 if (GET_MODE (result) == mode)
4323 return result;
4324 if (target == 0)
4325 return convert_to_mode (mode, result, 0);
4326 convert_move (target, result, 0);
4327 return target;
4330 /* Expand the library call ourselves using a stabilized argument
4331 list to avoid re-evaluating the function's arguments twice. */
4332 #ifdef HAVE_cmpstrnsi
4333 do_libcall:
4334 #endif
4335 fndecl = get_callee_fndecl (exp);
4336 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4337 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4338 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4339 return expand_call (fn, target, target == const0_rtx);
4341 #endif
4342 return NULL_RTX;
4345 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4346 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4347 the result in TARGET, if convenient. */
4349 static rtx
4350 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4351 ATTRIBUTE_UNUSED enum machine_mode mode)
4353 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4355 if (!validate_arglist (exp,
4356 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4357 return NULL_RTX;
4359 /* If c_strlen can determine an expression for one of the string
4360 lengths, and it doesn't have side effects, then emit cmpstrnsi
4361 using length MIN(strlen(string)+1, arg3). */
4362 #ifdef HAVE_cmpstrnsi
4363 if (HAVE_cmpstrnsi)
4365 tree len, len1, len2;
4366 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4367 rtx result, insn;
4368 tree fndecl, fn;
4369 tree arg1 = CALL_EXPR_ARG (exp, 0);
4370 tree arg2 = CALL_EXPR_ARG (exp, 1);
4371 tree arg3 = CALL_EXPR_ARG (exp, 2);
4373 unsigned int arg1_align
4374 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4375 unsigned int arg2_align
4376 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4377 enum machine_mode insn_mode
4378 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4380 len1 = c_strlen (arg1, 1);
4381 len2 = c_strlen (arg2, 1);
4383 if (len1)
4384 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4385 if (len2)
4386 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4388 /* If we don't have a constant length for the first, use the length
4389 of the second, if we know it. We don't require a constant for
4390 this case; some cost analysis could be done if both are available
4391 but neither is constant. For now, assume they're equally cheap,
4392 unless one has side effects. If both strings have constant lengths,
4393 use the smaller. */
4395 if (!len1)
4396 len = len2;
4397 else if (!len2)
4398 len = len1;
4399 else if (TREE_SIDE_EFFECTS (len1))
4400 len = len2;
4401 else if (TREE_SIDE_EFFECTS (len2))
4402 len = len1;
4403 else if (TREE_CODE (len1) != INTEGER_CST)
4404 len = len2;
4405 else if (TREE_CODE (len2) != INTEGER_CST)
4406 len = len1;
4407 else if (tree_int_cst_lt (len1, len2))
4408 len = len1;
4409 else
4410 len = len2;
4412 /* If both arguments have side effects, we cannot optimize. */
4413 if (!len || TREE_SIDE_EFFECTS (len))
4414 return NULL_RTX;
4416 /* The actual new length parameter is MIN(len,arg3). */
4417 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4418 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4420 /* If we don't have POINTER_TYPE, call the function. */
4421 if (arg1_align == 0 || arg2_align == 0)
4422 return NULL_RTX;
4424 /* Make a place to write the result of the instruction. */
4425 result = target;
4426 if (! (result != 0
4427 && REG_P (result) && GET_MODE (result) == insn_mode
4428 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4429 result = gen_reg_rtx (insn_mode);
4431 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4432 arg1 = builtin_save_expr (arg1);
4433 arg2 = builtin_save_expr (arg2);
4434 len = builtin_save_expr (len);
4436 arg1_rtx = get_memory_rtx (arg1, len);
4437 arg2_rtx = get_memory_rtx (arg2, len);
4438 arg3_rtx = expand_normal (len);
4439 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4440 GEN_INT (MIN (arg1_align, arg2_align)));
4441 if (insn)
4443 emit_insn (insn);
4445 /* Return the value in the proper mode for this function. */
4446 mode = TYPE_MODE (TREE_TYPE (exp));
4447 if (GET_MODE (result) == mode)
4448 return result;
4449 if (target == 0)
4450 return convert_to_mode (mode, result, 0);
4451 convert_move (target, result, 0);
4452 return target;
4455 /* Expand the library call ourselves using a stabilized argument
4456 list to avoid re-evaluating the function's arguments twice. */
4457 fndecl = get_callee_fndecl (exp);
4458 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4459 arg1, arg2, len);
4460 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4461 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4462 return expand_call (fn, target, target == const0_rtx);
4464 #endif
4465 return NULL_RTX;
4468 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4469 if that's convenient. */
4472 expand_builtin_saveregs (void)
4474 rtx val, seq;
4476 /* Don't do __builtin_saveregs more than once in a function.
4477 Save the result of the first call and reuse it. */
4478 if (saveregs_value != 0)
4479 return saveregs_value;
4481 /* When this function is called, it means that registers must be
4482 saved on entry to this function. So we migrate the call to the
4483 first insn of this function. */
4485 start_sequence ();
4487 /* Do whatever the machine needs done in this case. */
4488 val = targetm.calls.expand_builtin_saveregs ();
4490 seq = get_insns ();
4491 end_sequence ();
4493 saveregs_value = val;
4495 /* Put the insns after the NOTE that starts the function. If this
4496 is inside a start_sequence, make the outer-level insn chain current, so
4497 the code is placed at the start of the function. */
4498 push_topmost_sequence ();
4499 emit_insn_after (seq, entry_of_function ());
4500 pop_topmost_sequence ();
4502 return val;
4505 /* Expand a call to __builtin_next_arg. */
4507 static rtx
4508 expand_builtin_next_arg (void)
4510 /* Checking arguments is already done in fold_builtin_next_arg
4511 that must be called before this function. */
4512 return expand_binop (ptr_mode, add_optab,
4513 crtl->args.internal_arg_pointer,
4514 crtl->args.arg_offset_rtx,
4515 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4518 /* Make it easier for the backends by protecting the valist argument
4519 from multiple evaluations. */
4521 static tree
4522 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4524 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4526 /* The current way of determining the type of valist is completely
4527 bogus. We should have the information on the va builtin instead. */
4528 if (!vatype)
4529 vatype = targetm.fn_abi_va_list (cfun->decl);
4531 if (TREE_CODE (vatype) == ARRAY_TYPE)
4533 if (TREE_SIDE_EFFECTS (valist))
4534 valist = save_expr (valist);
4536 /* For this case, the backends will be expecting a pointer to
4537 vatype, but it's possible we've actually been given an array
4538 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4539 So fix it. */
4540 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4542 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4543 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4546 else
4548 tree pt = build_pointer_type (vatype);
4550 if (! needs_lvalue)
4552 if (! TREE_SIDE_EFFECTS (valist))
4553 return valist;
4555 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4556 TREE_SIDE_EFFECTS (valist) = 1;
4559 if (TREE_SIDE_EFFECTS (valist))
4560 valist = save_expr (valist);
4561 valist = fold_build2_loc (loc, MEM_REF,
4562 vatype, valist, build_int_cst (pt, 0));
4565 return valist;
4568 /* The "standard" definition of va_list is void*. */
4570 tree
4571 std_build_builtin_va_list (void)
4573 return ptr_type_node;
4576 /* The "standard" abi va_list is va_list_type_node. */
4578 tree
4579 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4581 return va_list_type_node;
4584 /* The "standard" type of va_list is va_list_type_node. */
4586 tree
4587 std_canonical_va_list_type (tree type)
4589 tree wtype, htype;
4591 if (INDIRECT_REF_P (type))
4592 type = TREE_TYPE (type);
4593 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4594 type = TREE_TYPE (type);
4595 wtype = va_list_type_node;
4596 htype = type;
4597 /* Treat structure va_list types. */
4598 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4599 htype = TREE_TYPE (htype);
4600 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4602 /* If va_list is an array type, the argument may have decayed
4603 to a pointer type, e.g. by being passed to another function.
4604 In that case, unwrap both types so that we can compare the
4605 underlying records. */
4606 if (TREE_CODE (htype) == ARRAY_TYPE
4607 || POINTER_TYPE_P (htype))
4609 wtype = TREE_TYPE (wtype);
4610 htype = TREE_TYPE (htype);
4613 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4614 return va_list_type_node;
4616 return NULL_TREE;
4619 /* The "standard" implementation of va_start: just assign `nextarg' to
4620 the variable. */
4622 void
4623 std_expand_builtin_va_start (tree valist, rtx nextarg)
4625 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4626 convert_move (va_r, nextarg, 0);
4629 /* Expand EXP, a call to __builtin_va_start. */
4631 static rtx
4632 expand_builtin_va_start (tree exp)
4634 rtx nextarg;
4635 tree valist;
4636 location_t loc = EXPR_LOCATION (exp);
4638 if (call_expr_nargs (exp) < 2)
4640 error_at (loc, "too few arguments to function %<va_start%>");
4641 return const0_rtx;
4644 if (fold_builtin_next_arg (exp, true))
4645 return const0_rtx;
4647 nextarg = expand_builtin_next_arg ();
4648 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4650 if (targetm.expand_builtin_va_start)
4651 targetm.expand_builtin_va_start (valist, nextarg);
4652 else
4653 std_expand_builtin_va_start (valist, nextarg);
4655 return const0_rtx;
4658 /* The "standard" implementation of va_arg: read the value from the
4659 current (padded) address and increment by the (padded) size. */
4661 tree
4662 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4663 gimple_seq *post_p)
4665 tree addr, t, type_size, rounded_size, valist_tmp;
4666 unsigned HOST_WIDE_INT align, boundary;
4667 bool indirect;
4669 #ifdef ARGS_GROW_DOWNWARD
4670 /* All of the alignment and movement below is for args-grow-up machines.
4671 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4672 implement their own specialized gimplify_va_arg_expr routines. */
4673 gcc_unreachable ();
4674 #endif
4676 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4677 if (indirect)
4678 type = build_pointer_type (type);
4680 align = PARM_BOUNDARY / BITS_PER_UNIT;
4681 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4683 /* When we align parameter on stack for caller, if the parameter
4684 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4685 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4686 here with caller. */
4687 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4688 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4690 boundary /= BITS_PER_UNIT;
4692 /* Hoist the valist value into a temporary for the moment. */
4693 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4695 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4696 requires greater alignment, we must perform dynamic alignment. */
4697 if (boundary > align
4698 && !integer_zerop (TYPE_SIZE (type)))
4700 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4701 fold_build2 (POINTER_PLUS_EXPR,
4702 TREE_TYPE (valist),
4703 valist_tmp, size_int (boundary - 1)));
4704 gimplify_and_add (t, pre_p);
4706 t = fold_convert (sizetype, valist_tmp);
4707 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4708 fold_convert (TREE_TYPE (valist),
4709 fold_build2 (BIT_AND_EXPR, sizetype, t,
4710 size_int (-boundary))));
4711 gimplify_and_add (t, pre_p);
4713 else
4714 boundary = align;
4716 /* If the actual alignment is less than the alignment of the type,
4717 adjust the type accordingly so that we don't assume strict alignment
4718 when dereferencing the pointer. */
4719 boundary *= BITS_PER_UNIT;
4720 if (boundary < TYPE_ALIGN (type))
4722 type = build_variant_type_copy (type);
4723 TYPE_ALIGN (type) = boundary;
4726 /* Compute the rounded size of the type. */
4727 type_size = size_in_bytes (type);
4728 rounded_size = round_up (type_size, align);
4730 /* Reduce rounded_size so it's sharable with the postqueue. */
4731 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4733 /* Get AP. */
4734 addr = valist_tmp;
4735 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4737 /* Small args are padded downward. */
4738 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4739 rounded_size, size_int (align));
4740 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4741 size_binop (MINUS_EXPR, rounded_size, type_size));
4742 addr = fold_build2 (POINTER_PLUS_EXPR,
4743 TREE_TYPE (addr), addr, t);
4746 /* Compute new value for AP. */
4747 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4748 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4749 gimplify_and_add (t, pre_p);
4751 addr = fold_convert (build_pointer_type (type), addr);
4753 if (indirect)
4754 addr = build_va_arg_indirect_ref (addr);
4756 return build_va_arg_indirect_ref (addr);
4759 /* Build an indirect-ref expression over the given TREE, which represents a
4760 piece of a va_arg() expansion. */
4761 tree
4762 build_va_arg_indirect_ref (tree addr)
4764 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4766 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4767 mf_mark (addr);
4769 return addr;
4772 /* Return a dummy expression of type TYPE in order to keep going after an
4773 error. */
4775 static tree
4776 dummy_object (tree type)
4778 tree t = build_int_cst (build_pointer_type (type), 0);
4779 return build2 (MEM_REF, type, t, t);
4782 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4783 builtin function, but a very special sort of operator. */
4785 enum gimplify_status
4786 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4788 tree promoted_type, have_va_type;
4789 tree valist = TREE_OPERAND (*expr_p, 0);
4790 tree type = TREE_TYPE (*expr_p);
4791 tree t;
4792 location_t loc = EXPR_LOCATION (*expr_p);
4794 /* Verify that valist is of the proper type. */
4795 have_va_type = TREE_TYPE (valist);
4796 if (have_va_type == error_mark_node)
4797 return GS_ERROR;
4798 have_va_type = targetm.canonical_va_list_type (have_va_type);
4800 if (have_va_type == NULL_TREE)
4802 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4803 return GS_ERROR;
4806 /* Generate a diagnostic for requesting data of a type that cannot
4807 be passed through `...' due to type promotion at the call site. */
4808 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4809 != type)
4811 static bool gave_help;
4812 bool warned;
4814 /* Unfortunately, this is merely undefined, rather than a constraint
4815 violation, so we cannot make this an error. If this call is never
4816 executed, the program is still strictly conforming. */
4817 warned = warning_at (loc, 0,
4818 "%qT is promoted to %qT when passed through %<...%>",
4819 type, promoted_type);
4820 if (!gave_help && warned)
4822 gave_help = true;
4823 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4824 promoted_type, type);
4827 /* We can, however, treat "undefined" any way we please.
4828 Call abort to encourage the user to fix the program. */
4829 if (warned)
4830 inform (loc, "if this code is reached, the program will abort");
4831 /* Before the abort, allow the evaluation of the va_list
4832 expression to exit or longjmp. */
4833 gimplify_and_add (valist, pre_p);
4834 t = build_call_expr_loc (loc,
4835 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4836 gimplify_and_add (t, pre_p);
4838 /* This is dead code, but go ahead and finish so that the
4839 mode of the result comes out right. */
4840 *expr_p = dummy_object (type);
4841 return GS_ALL_DONE;
4843 else
4845 /* Make it easier for the backends by protecting the valist argument
4846 from multiple evaluations. */
4847 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4849 /* For this case, the backends will be expecting a pointer to
4850 TREE_TYPE (abi), but it's possible we've
4851 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4852 So fix it. */
4853 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4855 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4856 valist = fold_convert_loc (loc, p1,
4857 build_fold_addr_expr_loc (loc, valist));
4860 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4862 else
4863 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4865 if (!targetm.gimplify_va_arg_expr)
4866 /* FIXME: Once most targets are converted we should merely
4867 assert this is non-null. */
4868 return GS_ALL_DONE;
4870 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4871 return GS_OK;
4875 /* Expand EXP, a call to __builtin_va_end. */
4877 static rtx
4878 expand_builtin_va_end (tree exp)
4880 tree valist = CALL_EXPR_ARG (exp, 0);
4882 /* Evaluate for side effects, if needed. I hate macros that don't
4883 do that. */
4884 if (TREE_SIDE_EFFECTS (valist))
4885 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4887 return const0_rtx;
4890 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4891 builtin rather than just as an assignment in stdarg.h because of the
4892 nastiness of array-type va_list types. */
4894 static rtx
4895 expand_builtin_va_copy (tree exp)
4897 tree dst, src, t;
4898 location_t loc = EXPR_LOCATION (exp);
4900 dst = CALL_EXPR_ARG (exp, 0);
4901 src = CALL_EXPR_ARG (exp, 1);
4903 dst = stabilize_va_list_loc (loc, dst, 1);
4904 src = stabilize_va_list_loc (loc, src, 0);
4906 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4908 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4910 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4911 TREE_SIDE_EFFECTS (t) = 1;
4912 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4914 else
4916 rtx dstb, srcb, size;
4918 /* Evaluate to pointers. */
4919 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4920 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4921 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4922 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4924 dstb = convert_memory_address (Pmode, dstb);
4925 srcb = convert_memory_address (Pmode, srcb);
4927 /* "Dereference" to BLKmode memories. */
4928 dstb = gen_rtx_MEM (BLKmode, dstb);
4929 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4930 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4931 srcb = gen_rtx_MEM (BLKmode, srcb);
4932 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4933 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4935 /* Copy. */
4936 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4939 return const0_rtx;
4942 /* Expand a call to one of the builtin functions __builtin_frame_address or
4943 __builtin_return_address. */
4945 static rtx
4946 expand_builtin_frame_address (tree fndecl, tree exp)
4948 /* The argument must be a nonnegative integer constant.
4949 It counts the number of frames to scan up the stack.
4950 The value is the return address saved in that frame. */
4951 if (call_expr_nargs (exp) == 0)
4952 /* Warning about missing arg was already issued. */
4953 return const0_rtx;
4954 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4956 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4957 error ("invalid argument to %<__builtin_frame_address%>");
4958 else
4959 error ("invalid argument to %<__builtin_return_address%>");
4960 return const0_rtx;
4962 else
4964 rtx tem
4965 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4966 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4968 /* Some ports cannot access arbitrary stack frames. */
4969 if (tem == NULL)
4971 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4972 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4973 else
4974 warning (0, "unsupported argument to %<__builtin_return_address%>");
4975 return const0_rtx;
4978 /* For __builtin_frame_address, return what we've got. */
4979 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4980 return tem;
4982 if (!REG_P (tem)
4983 && ! CONSTANT_P (tem))
4984 tem = copy_to_mode_reg (Pmode, tem);
4985 return tem;
4989 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4990 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4991 is the same as for allocate_dynamic_stack_space. */
4993 static rtx
4994 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4996 rtx op0;
4997 rtx result;
4999 /* Emit normal call if marked not-inlineable. */
5000 if (CALL_CANNOT_INLINE_P (exp))
5001 return NULL_RTX;
5003 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5004 return NULL_RTX;
5006 /* Compute the argument. */
5007 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5009 /* Allocate the desired space. */
5010 result = allocate_dynamic_stack_space (op0, 0, BIGGEST_ALIGNMENT,
5011 cannot_accumulate);
5012 result = convert_memory_address (ptr_mode, result);
5014 return result;
5017 /* Expand a call to a bswap builtin with argument ARG0. MODE
5018 is the mode to expand with. */
5020 static rtx
5021 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5023 enum machine_mode mode;
5024 tree arg;
5025 rtx op0;
5027 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5028 return NULL_RTX;
5030 arg = CALL_EXPR_ARG (exp, 0);
5031 mode = TYPE_MODE (TREE_TYPE (arg));
5032 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5034 target = expand_unop (mode, bswap_optab, op0, target, 1);
5036 gcc_assert (target);
5038 return convert_to_mode (mode, target, 0);
5041 /* Expand a call to a unary builtin in EXP.
5042 Return NULL_RTX if a normal call should be emitted rather than expanding the
5043 function in-line. If convenient, the result should be placed in TARGET.
5044 SUBTARGET may be used as the target for computing one of EXP's operands. */
5046 static rtx
5047 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5048 rtx subtarget, optab op_optab)
5050 rtx op0;
5052 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5053 return NULL_RTX;
5055 /* Compute the argument. */
5056 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5057 (subtarget
5058 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5059 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5060 VOIDmode, EXPAND_NORMAL);
5061 /* Compute op, into TARGET if possible.
5062 Set TARGET to wherever the result comes back. */
5063 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5064 op_optab, op0, target, 1);
5065 gcc_assert (target);
5067 return convert_to_mode (target_mode, target, 0);
5070 /* Expand a call to __builtin_expect. We just return our argument
5071 as the builtin_expect semantic should've been already executed by
5072 tree branch prediction pass. */
5074 static rtx
5075 expand_builtin_expect (tree exp, rtx target)
5077 tree arg;
5079 if (call_expr_nargs (exp) < 2)
5080 return const0_rtx;
5081 arg = CALL_EXPR_ARG (exp, 0);
5083 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5084 /* When guessing was done, the hints should be already stripped away. */
5085 gcc_assert (!flag_guess_branch_prob
5086 || optimize == 0 || seen_error ());
5087 return target;
5090 void
5091 expand_builtin_trap (void)
5093 #ifdef HAVE_trap
5094 if (HAVE_trap)
5095 emit_insn (gen_trap ());
5096 else
5097 #endif
5098 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5099 emit_barrier ();
5102 /* Expand a call to __builtin_unreachable. We do nothing except emit
5103 a barrier saying that control flow will not pass here.
5105 It is the responsibility of the program being compiled to ensure
5106 that control flow does never reach __builtin_unreachable. */
5107 static void
5108 expand_builtin_unreachable (void)
5110 emit_barrier ();
5113 /* Expand EXP, a call to fabs, fabsf or fabsl.
5114 Return NULL_RTX if a normal call should be emitted rather than expanding
5115 the function inline. If convenient, the result should be placed
5116 in TARGET. SUBTARGET may be used as the target for computing
5117 the operand. */
5119 static rtx
5120 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5122 enum machine_mode mode;
5123 tree arg;
5124 rtx op0;
5126 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5127 return NULL_RTX;
5129 arg = CALL_EXPR_ARG (exp, 0);
5130 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5131 mode = TYPE_MODE (TREE_TYPE (arg));
5132 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5133 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5136 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5137 Return NULL is a normal call should be emitted rather than expanding the
5138 function inline. If convenient, the result should be placed in TARGET.
5139 SUBTARGET may be used as the target for computing the operand. */
5141 static rtx
5142 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5144 rtx op0, op1;
5145 tree arg;
5147 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5148 return NULL_RTX;
5150 arg = CALL_EXPR_ARG (exp, 0);
5151 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5153 arg = CALL_EXPR_ARG (exp, 1);
5154 op1 = expand_normal (arg);
5156 return expand_copysign (op0, op1, target);
5159 /* Create a new constant string literal and return a char* pointer to it.
5160 The STRING_CST value is the LEN characters at STR. */
5161 tree
5162 build_string_literal (int len, const char *str)
5164 tree t, elem, index, type;
5166 t = build_string (len, str);
5167 elem = build_type_variant (char_type_node, 1, 0);
5168 index = build_index_type (size_int (len - 1));
5169 type = build_array_type (elem, index);
5170 TREE_TYPE (t) = type;
5171 TREE_CONSTANT (t) = 1;
5172 TREE_READONLY (t) = 1;
5173 TREE_STATIC (t) = 1;
5175 type = build_pointer_type (elem);
5176 t = build1 (ADDR_EXPR, type,
5177 build4 (ARRAY_REF, elem,
5178 t, integer_zero_node, NULL_TREE, NULL_TREE));
5179 return t;
5182 /* Expand a call to __builtin___clear_cache. */
5184 static rtx
5185 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5187 #ifndef HAVE_clear_cache
5188 #ifdef CLEAR_INSN_CACHE
5189 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5190 does something. Just do the default expansion to a call to
5191 __clear_cache(). */
5192 return NULL_RTX;
5193 #else
5194 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5195 does nothing. There is no need to call it. Do nothing. */
5196 return const0_rtx;
5197 #endif /* CLEAR_INSN_CACHE */
5198 #else
5199 /* We have a "clear_cache" insn, and it will handle everything. */
5200 tree begin, end;
5201 rtx begin_rtx, end_rtx;
5203 /* We must not expand to a library call. If we did, any
5204 fallback library function in libgcc that might contain a call to
5205 __builtin___clear_cache() would recurse infinitely. */
5206 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5208 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5209 return const0_rtx;
5212 if (HAVE_clear_cache)
5214 struct expand_operand ops[2];
5216 begin = CALL_EXPR_ARG (exp, 0);
5217 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5219 end = CALL_EXPR_ARG (exp, 1);
5220 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5222 create_address_operand (&ops[0], begin_rtx);
5223 create_address_operand (&ops[1], end_rtx);
5224 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
5225 return const0_rtx;
5227 return const0_rtx;
5228 #endif /* HAVE_clear_cache */
5231 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5233 static rtx
5234 round_trampoline_addr (rtx tramp)
5236 rtx temp, addend, mask;
5238 /* If we don't need too much alignment, we'll have been guaranteed
5239 proper alignment by get_trampoline_type. */
5240 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5241 return tramp;
5243 /* Round address up to desired boundary. */
5244 temp = gen_reg_rtx (Pmode);
5245 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5246 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5248 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5249 temp, 0, OPTAB_LIB_WIDEN);
5250 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5251 temp, 0, OPTAB_LIB_WIDEN);
5253 return tramp;
5256 static rtx
5257 expand_builtin_init_trampoline (tree exp)
5259 tree t_tramp, t_func, t_chain;
5260 rtx m_tramp, r_tramp, r_chain, tmp;
5262 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5263 POINTER_TYPE, VOID_TYPE))
5264 return NULL_RTX;
5266 t_tramp = CALL_EXPR_ARG (exp, 0);
5267 t_func = CALL_EXPR_ARG (exp, 1);
5268 t_chain = CALL_EXPR_ARG (exp, 2);
5270 r_tramp = expand_normal (t_tramp);
5271 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5272 MEM_NOTRAP_P (m_tramp) = 1;
5274 /* The TRAMP argument should be the address of a field within the
5275 local function's FRAME decl. Let's see if we can fill in the
5276 to fill in the MEM_ATTRs for this memory. */
5277 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5278 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5279 true, 0);
5281 tmp = round_trampoline_addr (r_tramp);
5282 if (tmp != r_tramp)
5284 m_tramp = change_address (m_tramp, BLKmode, tmp);
5285 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5286 set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5289 /* The FUNC argument should be the address of the nested function.
5290 Extract the actual function decl to pass to the hook. */
5291 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5292 t_func = TREE_OPERAND (t_func, 0);
5293 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5295 r_chain = expand_normal (t_chain);
5297 /* Generate insns to initialize the trampoline. */
5298 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5300 trampolines_created = 1;
5302 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5303 "trampoline generated for nested function %qD", t_func);
5305 return const0_rtx;
5308 static rtx
5309 expand_builtin_adjust_trampoline (tree exp)
5311 rtx tramp;
5313 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5314 return NULL_RTX;
5316 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5317 tramp = round_trampoline_addr (tramp);
5318 if (targetm.calls.trampoline_adjust_address)
5319 tramp = targetm.calls.trampoline_adjust_address (tramp);
5321 return tramp;
5324 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5325 function. The function first checks whether the back end provides
5326 an insn to implement signbit for the respective mode. If not, it
5327 checks whether the floating point format of the value is such that
5328 the sign bit can be extracted. If that is not the case, the
5329 function returns NULL_RTX to indicate that a normal call should be
5330 emitted rather than expanding the function in-line. EXP is the
5331 expression that is a call to the builtin function; if convenient,
5332 the result should be placed in TARGET. */
5333 static rtx
5334 expand_builtin_signbit (tree exp, rtx target)
5336 const struct real_format *fmt;
5337 enum machine_mode fmode, imode, rmode;
5338 tree arg;
5339 int word, bitpos;
5340 enum insn_code icode;
5341 rtx temp;
5342 location_t loc = EXPR_LOCATION (exp);
5344 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5345 return NULL_RTX;
5347 arg = CALL_EXPR_ARG (exp, 0);
5348 fmode = TYPE_MODE (TREE_TYPE (arg));
5349 rmode = TYPE_MODE (TREE_TYPE (exp));
5350 fmt = REAL_MODE_FORMAT (fmode);
5352 arg = builtin_save_expr (arg);
5354 /* Expand the argument yielding a RTX expression. */
5355 temp = expand_normal (arg);
5357 /* Check if the back end provides an insn that handles signbit for the
5358 argument's mode. */
5359 icode = optab_handler (signbit_optab, fmode);
5360 if (icode != CODE_FOR_nothing)
5362 rtx last = get_last_insn ();
5363 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5364 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5365 return target;
5366 delete_insns_since (last);
5369 /* For floating point formats without a sign bit, implement signbit
5370 as "ARG < 0.0". */
5371 bitpos = fmt->signbit_ro;
5372 if (bitpos < 0)
5374 /* But we can't do this if the format supports signed zero. */
5375 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5376 return NULL_RTX;
5378 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5379 build_real (TREE_TYPE (arg), dconst0));
5380 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5383 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5385 imode = int_mode_for_mode (fmode);
5386 if (imode == BLKmode)
5387 return NULL_RTX;
5388 temp = gen_lowpart (imode, temp);
5390 else
5392 imode = word_mode;
5393 /* Handle targets with different FP word orders. */
5394 if (FLOAT_WORDS_BIG_ENDIAN)
5395 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5396 else
5397 word = bitpos / BITS_PER_WORD;
5398 temp = operand_subword_force (temp, word, fmode);
5399 bitpos = bitpos % BITS_PER_WORD;
5402 /* Force the intermediate word_mode (or narrower) result into a
5403 register. This avoids attempting to create paradoxical SUBREGs
5404 of floating point modes below. */
5405 temp = force_reg (imode, temp);
5407 /* If the bitpos is within the "result mode" lowpart, the operation
5408 can be implement with a single bitwise AND. Otherwise, we need
5409 a right shift and an AND. */
5411 if (bitpos < GET_MODE_BITSIZE (rmode))
5413 double_int mask = double_int_setbit (double_int_zero, bitpos);
5415 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5416 temp = gen_lowpart (rmode, temp);
5417 temp = expand_binop (rmode, and_optab, temp,
5418 immed_double_int_const (mask, rmode),
5419 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5421 else
5423 /* Perform a logical right shift to place the signbit in the least
5424 significant bit, then truncate the result to the desired mode
5425 and mask just this bit. */
5426 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5427 temp = gen_lowpart (rmode, temp);
5428 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5429 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5432 return temp;
5435 /* Expand fork or exec calls. TARGET is the desired target of the
5436 call. EXP is the call. FN is the
5437 identificator of the actual function. IGNORE is nonzero if the
5438 value is to be ignored. */
5440 static rtx
5441 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5443 tree id, decl;
5444 tree call;
5446 /* If we are not profiling, just call the function. */
5447 if (!profile_arc_flag)
5448 return NULL_RTX;
5450 /* Otherwise call the wrapper. This should be equivalent for the rest of
5451 compiler, so the code does not diverge, and the wrapper may run the
5452 code necessary for keeping the profiling sane. */
5454 switch (DECL_FUNCTION_CODE (fn))
5456 case BUILT_IN_FORK:
5457 id = get_identifier ("__gcov_fork");
5458 break;
5460 case BUILT_IN_EXECL:
5461 id = get_identifier ("__gcov_execl");
5462 break;
5464 case BUILT_IN_EXECV:
5465 id = get_identifier ("__gcov_execv");
5466 break;
5468 case BUILT_IN_EXECLP:
5469 id = get_identifier ("__gcov_execlp");
5470 break;
5472 case BUILT_IN_EXECLE:
5473 id = get_identifier ("__gcov_execle");
5474 break;
5476 case BUILT_IN_EXECVP:
5477 id = get_identifier ("__gcov_execvp");
5478 break;
5480 case BUILT_IN_EXECVE:
5481 id = get_identifier ("__gcov_execve");
5482 break;
5484 default:
5485 gcc_unreachable ();
5488 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5489 FUNCTION_DECL, id, TREE_TYPE (fn));
5490 DECL_EXTERNAL (decl) = 1;
5491 TREE_PUBLIC (decl) = 1;
5492 DECL_ARTIFICIAL (decl) = 1;
5493 TREE_NOTHROW (decl) = 1;
5494 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5495 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5496 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5497 return expand_call (call, target, ignore);
5502 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5503 the pointer in these functions is void*, the tree optimizers may remove
5504 casts. The mode computed in expand_builtin isn't reliable either, due
5505 to __sync_bool_compare_and_swap.
5507 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5508 group of builtins. This gives us log2 of the mode size. */
5510 static inline enum machine_mode
5511 get_builtin_sync_mode (int fcode_diff)
5513 /* The size is not negotiable, so ask not to get BLKmode in return
5514 if the target indicates that a smaller size would be better. */
5515 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5518 /* Expand the memory expression LOC and return the appropriate memory operand
5519 for the builtin_sync operations. */
5521 static rtx
5522 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5524 rtx addr, mem;
5526 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5527 addr = convert_memory_address (Pmode, addr);
5529 /* Note that we explicitly do not want any alias information for this
5530 memory, so that we kill all other live memories. Otherwise we don't
5531 satisfy the full barrier semantics of the intrinsic. */
5532 mem = validize_mem (gen_rtx_MEM (mode, addr));
5534 /* The alignment needs to be at least according to that of the mode. */
5535 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5536 get_pointer_alignment (loc, BIGGEST_ALIGNMENT)));
5537 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5538 MEM_VOLATILE_P (mem) = 1;
5540 return mem;
5543 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5544 EXP is the CALL_EXPR. CODE is the rtx code
5545 that corresponds to the arithmetic or logical operation from the name;
5546 an exception here is that NOT actually means NAND. TARGET is an optional
5547 place for us to store the results; AFTER is true if this is the
5548 fetch_and_xxx form. IGNORE is true if we don't actually care about
5549 the result of the operation at all. */
5551 static rtx
5552 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5553 enum rtx_code code, bool after,
5554 rtx target, bool ignore)
5556 rtx val, mem;
5557 enum machine_mode old_mode;
5558 location_t loc = EXPR_LOCATION (exp);
5560 if (code == NOT && warn_sync_nand)
5562 tree fndecl = get_callee_fndecl (exp);
5563 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5565 static bool warned_f_a_n, warned_n_a_f;
5567 switch (fcode)
5569 case BUILT_IN_FETCH_AND_NAND_1:
5570 case BUILT_IN_FETCH_AND_NAND_2:
5571 case BUILT_IN_FETCH_AND_NAND_4:
5572 case BUILT_IN_FETCH_AND_NAND_8:
5573 case BUILT_IN_FETCH_AND_NAND_16:
5575 if (warned_f_a_n)
5576 break;
5578 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5579 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5580 warned_f_a_n = true;
5581 break;
5583 case BUILT_IN_NAND_AND_FETCH_1:
5584 case BUILT_IN_NAND_AND_FETCH_2:
5585 case BUILT_IN_NAND_AND_FETCH_4:
5586 case BUILT_IN_NAND_AND_FETCH_8:
5587 case BUILT_IN_NAND_AND_FETCH_16:
5589 if (warned_n_a_f)
5590 break;
5592 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5593 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5594 warned_n_a_f = true;
5595 break;
5597 default:
5598 gcc_unreachable ();
5602 /* Expand the operands. */
5603 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5605 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5606 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5607 of CONST_INTs, where we know the old_mode only from the call argument. */
5608 old_mode = GET_MODE (val);
5609 if (old_mode == VOIDmode)
5610 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5611 val = convert_modes (mode, old_mode, val, 1);
5613 if (ignore)
5614 return expand_sync_operation (mem, val, code);
5615 else
5616 return expand_sync_fetch_operation (mem, val, code, after, target);
5619 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5620 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5621 true if this is the boolean form. TARGET is a place for us to store the
5622 results; this is NOT optional if IS_BOOL is true. */
5624 static rtx
5625 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5626 bool is_bool, rtx target)
5628 rtx old_val, new_val, mem;
5629 enum machine_mode old_mode;
5631 /* Expand the operands. */
5632 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5635 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5636 mode, EXPAND_NORMAL);
5637 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5638 of CONST_INTs, where we know the old_mode only from the call argument. */
5639 old_mode = GET_MODE (old_val);
5640 if (old_mode == VOIDmode)
5641 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5642 old_val = convert_modes (mode, old_mode, old_val, 1);
5644 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5645 mode, EXPAND_NORMAL);
5646 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5647 of CONST_INTs, where we know the old_mode only from the call argument. */
5648 old_mode = GET_MODE (new_val);
5649 if (old_mode == VOIDmode)
5650 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5651 new_val = convert_modes (mode, old_mode, new_val, 1);
5653 if (is_bool)
5654 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5655 else
5656 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5659 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5660 general form is actually an atomic exchange, and some targets only
5661 support a reduced form with the second argument being a constant 1.
5662 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5663 the results. */
5665 static rtx
5666 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5667 rtx target)
5669 rtx val, mem;
5670 enum machine_mode old_mode;
5672 /* Expand the operands. */
5673 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5674 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5675 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5676 of CONST_INTs, where we know the old_mode only from the call argument. */
5677 old_mode = GET_MODE (val);
5678 if (old_mode == VOIDmode)
5679 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5680 val = convert_modes (mode, old_mode, val, 1);
5682 return expand_sync_lock_test_and_set (mem, val, target);
5685 /* Expand the __sync_synchronize intrinsic. */
5687 static void
5688 expand_builtin_synchronize (void)
5690 gimple x;
5691 VEC (tree, gc) *v_clobbers;
5693 #ifdef HAVE_memory_barrier
5694 if (HAVE_memory_barrier)
5696 emit_insn (gen_memory_barrier ());
5697 return;
5699 #endif
5701 if (synchronize_libfunc != NULL_RTX)
5703 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5704 return;
5707 /* If no explicit memory barrier instruction is available, create an
5708 empty asm stmt with a memory clobber. */
5709 v_clobbers = VEC_alloc (tree, gc, 1);
5710 VEC_quick_push (tree, v_clobbers,
5711 tree_cons (NULL, build_string (6, "memory"), NULL));
5712 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5713 gimple_asm_set_volatile (x, true);
5714 expand_asm_stmt (x);
5717 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5719 static void
5720 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5722 struct expand_operand ops[2];
5723 enum insn_code icode;
5724 rtx mem;
5726 /* Expand the operands. */
5727 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5729 /* If there is an explicit operation in the md file, use it. */
5730 icode = direct_optab_handler (sync_lock_release_optab, mode);
5731 if (icode != CODE_FOR_nothing)
5733 create_fixed_operand (&ops[0], mem);
5734 create_input_operand (&ops[1], const0_rtx, mode);
5735 if (maybe_expand_insn (icode, 2, ops))
5736 return;
5739 /* Otherwise we can implement this operation by emitting a barrier
5740 followed by a store of zero. */
5741 expand_builtin_synchronize ();
5742 emit_move_insn (mem, const0_rtx);
5745 /* Expand an expression EXP that calls a built-in function,
5746 with result going to TARGET if that's convenient
5747 (and in mode MODE if that's convenient).
5748 SUBTARGET may be used as the target for computing one of EXP's operands.
5749 IGNORE is nonzero if the value is to be ignored. */
5752 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5753 int ignore)
5755 tree fndecl = get_callee_fndecl (exp);
5756 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5757 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5758 int flags;
5760 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5761 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5763 /* When not optimizing, generate calls to library functions for a certain
5764 set of builtins. */
5765 if (!optimize
5766 && !called_as_built_in (fndecl)
5767 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5768 && fcode != BUILT_IN_ALLOCA
5769 && fcode != BUILT_IN_FREE)
5770 return expand_call (exp, target, ignore);
5772 /* The built-in function expanders test for target == const0_rtx
5773 to determine whether the function's result will be ignored. */
5774 if (ignore)
5775 target = const0_rtx;
5777 /* If the result of a pure or const built-in function is ignored, and
5778 none of its arguments are volatile, we can avoid expanding the
5779 built-in call and just evaluate the arguments for side-effects. */
5780 if (target == const0_rtx
5781 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5782 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5784 bool volatilep = false;
5785 tree arg;
5786 call_expr_arg_iterator iter;
5788 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5789 if (TREE_THIS_VOLATILE (arg))
5791 volatilep = true;
5792 break;
5795 if (! volatilep)
5797 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5798 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5799 return const0_rtx;
5803 switch (fcode)
5805 CASE_FLT_FN (BUILT_IN_FABS):
5806 target = expand_builtin_fabs (exp, target, subtarget);
5807 if (target)
5808 return target;
5809 break;
5811 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5812 target = expand_builtin_copysign (exp, target, subtarget);
5813 if (target)
5814 return target;
5815 break;
5817 /* Just do a normal library call if we were unable to fold
5818 the values. */
5819 CASE_FLT_FN (BUILT_IN_CABS):
5820 break;
5822 CASE_FLT_FN (BUILT_IN_EXP):
5823 CASE_FLT_FN (BUILT_IN_EXP10):
5824 CASE_FLT_FN (BUILT_IN_POW10):
5825 CASE_FLT_FN (BUILT_IN_EXP2):
5826 CASE_FLT_FN (BUILT_IN_EXPM1):
5827 CASE_FLT_FN (BUILT_IN_LOGB):
5828 CASE_FLT_FN (BUILT_IN_LOG):
5829 CASE_FLT_FN (BUILT_IN_LOG10):
5830 CASE_FLT_FN (BUILT_IN_LOG2):
5831 CASE_FLT_FN (BUILT_IN_LOG1P):
5832 CASE_FLT_FN (BUILT_IN_TAN):
5833 CASE_FLT_FN (BUILT_IN_ASIN):
5834 CASE_FLT_FN (BUILT_IN_ACOS):
5835 CASE_FLT_FN (BUILT_IN_ATAN):
5836 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5837 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5838 because of possible accuracy problems. */
5839 if (! flag_unsafe_math_optimizations)
5840 break;
5841 CASE_FLT_FN (BUILT_IN_SQRT):
5842 CASE_FLT_FN (BUILT_IN_FLOOR):
5843 CASE_FLT_FN (BUILT_IN_CEIL):
5844 CASE_FLT_FN (BUILT_IN_TRUNC):
5845 CASE_FLT_FN (BUILT_IN_ROUND):
5846 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5847 CASE_FLT_FN (BUILT_IN_RINT):
5848 target = expand_builtin_mathfn (exp, target, subtarget);
5849 if (target)
5850 return target;
5851 break;
5853 CASE_FLT_FN (BUILT_IN_FMA):
5854 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5855 if (target)
5856 return target;
5857 break;
5859 CASE_FLT_FN (BUILT_IN_ILOGB):
5860 if (! flag_unsafe_math_optimizations)
5861 break;
5862 CASE_FLT_FN (BUILT_IN_ISINF):
5863 CASE_FLT_FN (BUILT_IN_FINITE):
5864 case BUILT_IN_ISFINITE:
5865 case BUILT_IN_ISNORMAL:
5866 target = expand_builtin_interclass_mathfn (exp, target);
5867 if (target)
5868 return target;
5869 break;
5871 CASE_FLT_FN (BUILT_IN_LCEIL):
5872 CASE_FLT_FN (BUILT_IN_LLCEIL):
5873 CASE_FLT_FN (BUILT_IN_LFLOOR):
5874 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5875 target = expand_builtin_int_roundingfn (exp, target);
5876 if (target)
5877 return target;
5878 break;
5880 CASE_FLT_FN (BUILT_IN_LRINT):
5881 CASE_FLT_FN (BUILT_IN_LLRINT):
5882 CASE_FLT_FN (BUILT_IN_LROUND):
5883 CASE_FLT_FN (BUILT_IN_LLROUND):
5884 target = expand_builtin_int_roundingfn_2 (exp, target);
5885 if (target)
5886 return target;
5887 break;
5889 CASE_FLT_FN (BUILT_IN_POW):
5890 target = expand_builtin_pow (exp, target, subtarget);
5891 if (target)
5892 return target;
5893 break;
5895 CASE_FLT_FN (BUILT_IN_POWI):
5896 target = expand_builtin_powi (exp, target);
5897 if (target)
5898 return target;
5899 break;
5901 CASE_FLT_FN (BUILT_IN_ATAN2):
5902 CASE_FLT_FN (BUILT_IN_LDEXP):
5903 CASE_FLT_FN (BUILT_IN_SCALB):
5904 CASE_FLT_FN (BUILT_IN_SCALBN):
5905 CASE_FLT_FN (BUILT_IN_SCALBLN):
5906 if (! flag_unsafe_math_optimizations)
5907 break;
5909 CASE_FLT_FN (BUILT_IN_FMOD):
5910 CASE_FLT_FN (BUILT_IN_REMAINDER):
5911 CASE_FLT_FN (BUILT_IN_DREM):
5912 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5913 if (target)
5914 return target;
5915 break;
5917 CASE_FLT_FN (BUILT_IN_CEXPI):
5918 target = expand_builtin_cexpi (exp, target);
5919 gcc_assert (target);
5920 return target;
5922 CASE_FLT_FN (BUILT_IN_SIN):
5923 CASE_FLT_FN (BUILT_IN_COS):
5924 if (! flag_unsafe_math_optimizations)
5925 break;
5926 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5927 if (target)
5928 return target;
5929 break;
5931 CASE_FLT_FN (BUILT_IN_SINCOS):
5932 if (! flag_unsafe_math_optimizations)
5933 break;
5934 target = expand_builtin_sincos (exp);
5935 if (target)
5936 return target;
5937 break;
5939 case BUILT_IN_APPLY_ARGS:
5940 return expand_builtin_apply_args ();
5942 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5943 FUNCTION with a copy of the parameters described by
5944 ARGUMENTS, and ARGSIZE. It returns a block of memory
5945 allocated on the stack into which is stored all the registers
5946 that might possibly be used for returning the result of a
5947 function. ARGUMENTS is the value returned by
5948 __builtin_apply_args. ARGSIZE is the number of bytes of
5949 arguments that must be copied. ??? How should this value be
5950 computed? We'll also need a safe worst case value for varargs
5951 functions. */
5952 case BUILT_IN_APPLY:
5953 if (!validate_arglist (exp, POINTER_TYPE,
5954 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5955 && !validate_arglist (exp, REFERENCE_TYPE,
5956 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5957 return const0_rtx;
5958 else
5960 rtx ops[3];
5962 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5963 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5964 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5966 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5969 /* __builtin_return (RESULT) causes the function to return the
5970 value described by RESULT. RESULT is address of the block of
5971 memory returned by __builtin_apply. */
5972 case BUILT_IN_RETURN:
5973 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5974 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5975 return const0_rtx;
5977 case BUILT_IN_SAVEREGS:
5978 return expand_builtin_saveregs ();
5980 case BUILT_IN_VA_ARG_PACK:
5981 /* All valid uses of __builtin_va_arg_pack () are removed during
5982 inlining. */
5983 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5984 return const0_rtx;
5986 case BUILT_IN_VA_ARG_PACK_LEN:
5987 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5988 inlining. */
5989 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5990 return const0_rtx;
5992 /* Return the address of the first anonymous stack arg. */
5993 case BUILT_IN_NEXT_ARG:
5994 if (fold_builtin_next_arg (exp, false))
5995 return const0_rtx;
5996 return expand_builtin_next_arg ();
5998 case BUILT_IN_CLEAR_CACHE:
5999 target = expand_builtin___clear_cache (exp);
6000 if (target)
6001 return target;
6002 break;
6004 case BUILT_IN_CLASSIFY_TYPE:
6005 return expand_builtin_classify_type (exp);
6007 case BUILT_IN_CONSTANT_P:
6008 return const0_rtx;
6010 case BUILT_IN_FRAME_ADDRESS:
6011 case BUILT_IN_RETURN_ADDRESS:
6012 return expand_builtin_frame_address (fndecl, exp);
6014 /* Returns the address of the area where the structure is returned.
6015 0 otherwise. */
6016 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6017 if (call_expr_nargs (exp) != 0
6018 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6019 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6020 return const0_rtx;
6021 else
6022 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6024 case BUILT_IN_ALLOCA:
6025 /* If the allocation stems from the declaration of a variable-sized
6026 object, it cannot accumulate. */
6027 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6028 if (target)
6029 return target;
6030 break;
6032 case BUILT_IN_STACK_SAVE:
6033 return expand_stack_save ();
6035 case BUILT_IN_STACK_RESTORE:
6036 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6037 return const0_rtx;
6039 case BUILT_IN_BSWAP32:
6040 case BUILT_IN_BSWAP64:
6041 target = expand_builtin_bswap (exp, target, subtarget);
6043 if (target)
6044 return target;
6045 break;
6047 CASE_INT_FN (BUILT_IN_FFS):
6048 case BUILT_IN_FFSIMAX:
6049 target = expand_builtin_unop (target_mode, exp, target,
6050 subtarget, ffs_optab);
6051 if (target)
6052 return target;
6053 break;
6055 CASE_INT_FN (BUILT_IN_CLZ):
6056 case BUILT_IN_CLZIMAX:
6057 target = expand_builtin_unop (target_mode, exp, target,
6058 subtarget, clz_optab);
6059 if (target)
6060 return target;
6061 break;
6063 CASE_INT_FN (BUILT_IN_CTZ):
6064 case BUILT_IN_CTZIMAX:
6065 target = expand_builtin_unop (target_mode, exp, target,
6066 subtarget, ctz_optab);
6067 if (target)
6068 return target;
6069 break;
6071 CASE_INT_FN (BUILT_IN_POPCOUNT):
6072 case BUILT_IN_POPCOUNTIMAX:
6073 target = expand_builtin_unop (target_mode, exp, target,
6074 subtarget, popcount_optab);
6075 if (target)
6076 return target;
6077 break;
6079 CASE_INT_FN (BUILT_IN_PARITY):
6080 case BUILT_IN_PARITYIMAX:
6081 target = expand_builtin_unop (target_mode, exp, target,
6082 subtarget, parity_optab);
6083 if (target)
6084 return target;
6085 break;
6087 case BUILT_IN_STRLEN:
6088 target = expand_builtin_strlen (exp, target, target_mode);
6089 if (target)
6090 return target;
6091 break;
6093 case BUILT_IN_STRCPY:
6094 target = expand_builtin_strcpy (exp, target);
6095 if (target)
6096 return target;
6097 break;
6099 case BUILT_IN_STRNCPY:
6100 target = expand_builtin_strncpy (exp, target);
6101 if (target)
6102 return target;
6103 break;
6105 case BUILT_IN_STPCPY:
6106 target = expand_builtin_stpcpy (exp, target, mode);
6107 if (target)
6108 return target;
6109 break;
6111 case BUILT_IN_MEMCPY:
6112 target = expand_builtin_memcpy (exp, target);
6113 if (target)
6114 return target;
6115 break;
6117 case BUILT_IN_MEMPCPY:
6118 target = expand_builtin_mempcpy (exp, target, mode);
6119 if (target)
6120 return target;
6121 break;
6123 case BUILT_IN_MEMSET:
6124 target = expand_builtin_memset (exp, target, mode);
6125 if (target)
6126 return target;
6127 break;
6129 case BUILT_IN_BZERO:
6130 target = expand_builtin_bzero (exp);
6131 if (target)
6132 return target;
6133 break;
6135 case BUILT_IN_STRCMP:
6136 target = expand_builtin_strcmp (exp, target);
6137 if (target)
6138 return target;
6139 break;
6141 case BUILT_IN_STRNCMP:
6142 target = expand_builtin_strncmp (exp, target, mode);
6143 if (target)
6144 return target;
6145 break;
6147 case BUILT_IN_BCMP:
6148 case BUILT_IN_MEMCMP:
6149 target = expand_builtin_memcmp (exp, target, mode);
6150 if (target)
6151 return target;
6152 break;
6154 case BUILT_IN_SETJMP:
6155 /* This should have been lowered to the builtins below. */
6156 gcc_unreachable ();
6158 case BUILT_IN_SETJMP_SETUP:
6159 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6160 and the receiver label. */
6161 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6163 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6164 VOIDmode, EXPAND_NORMAL);
6165 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6166 rtx label_r = label_rtx (label);
6168 /* This is copied from the handling of non-local gotos. */
6169 expand_builtin_setjmp_setup (buf_addr, label_r);
6170 nonlocal_goto_handler_labels
6171 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6172 nonlocal_goto_handler_labels);
6173 /* ??? Do not let expand_label treat us as such since we would
6174 not want to be both on the list of non-local labels and on
6175 the list of forced labels. */
6176 FORCED_LABEL (label) = 0;
6177 return const0_rtx;
6179 break;
6181 case BUILT_IN_SETJMP_DISPATCHER:
6182 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6183 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6185 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6186 rtx label_r = label_rtx (label);
6188 /* Remove the dispatcher label from the list of non-local labels
6189 since the receiver labels have been added to it above. */
6190 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6191 return const0_rtx;
6193 break;
6195 case BUILT_IN_SETJMP_RECEIVER:
6196 /* __builtin_setjmp_receiver is passed the receiver label. */
6197 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6199 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6200 rtx label_r = label_rtx (label);
6202 expand_builtin_setjmp_receiver (label_r);
6203 return const0_rtx;
6205 break;
6207 /* __builtin_longjmp is passed a pointer to an array of five words.
6208 It's similar to the C library longjmp function but works with
6209 __builtin_setjmp above. */
6210 case BUILT_IN_LONGJMP:
6211 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6213 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6214 VOIDmode, EXPAND_NORMAL);
6215 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6217 if (value != const1_rtx)
6219 error ("%<__builtin_longjmp%> second argument must be 1");
6220 return const0_rtx;
6223 expand_builtin_longjmp (buf_addr, value);
6224 return const0_rtx;
6226 break;
6228 case BUILT_IN_NONLOCAL_GOTO:
6229 target = expand_builtin_nonlocal_goto (exp);
6230 if (target)
6231 return target;
6232 break;
6234 /* This updates the setjmp buffer that is its argument with the value
6235 of the current stack pointer. */
6236 case BUILT_IN_UPDATE_SETJMP_BUF:
6237 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6239 rtx buf_addr
6240 = expand_normal (CALL_EXPR_ARG (exp, 0));
6242 expand_builtin_update_setjmp_buf (buf_addr);
6243 return const0_rtx;
6245 break;
6247 case BUILT_IN_TRAP:
6248 expand_builtin_trap ();
6249 return const0_rtx;
6251 case BUILT_IN_UNREACHABLE:
6252 expand_builtin_unreachable ();
6253 return const0_rtx;
6255 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6256 case BUILT_IN_SIGNBITD32:
6257 case BUILT_IN_SIGNBITD64:
6258 case BUILT_IN_SIGNBITD128:
6259 target = expand_builtin_signbit (exp, target);
6260 if (target)
6261 return target;
6262 break;
6264 /* Various hooks for the DWARF 2 __throw routine. */
6265 case BUILT_IN_UNWIND_INIT:
6266 expand_builtin_unwind_init ();
6267 return const0_rtx;
6268 case BUILT_IN_DWARF_CFA:
6269 return virtual_cfa_rtx;
6270 #ifdef DWARF2_UNWIND_INFO
6271 case BUILT_IN_DWARF_SP_COLUMN:
6272 return expand_builtin_dwarf_sp_column ();
6273 case BUILT_IN_INIT_DWARF_REG_SIZES:
6274 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6275 return const0_rtx;
6276 #endif
6277 case BUILT_IN_FROB_RETURN_ADDR:
6278 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6279 case BUILT_IN_EXTRACT_RETURN_ADDR:
6280 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6281 case BUILT_IN_EH_RETURN:
6282 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6283 CALL_EXPR_ARG (exp, 1));
6284 return const0_rtx;
6285 #ifdef EH_RETURN_DATA_REGNO
6286 case BUILT_IN_EH_RETURN_DATA_REGNO:
6287 return expand_builtin_eh_return_data_regno (exp);
6288 #endif
6289 case BUILT_IN_EXTEND_POINTER:
6290 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6291 case BUILT_IN_EH_POINTER:
6292 return expand_builtin_eh_pointer (exp);
6293 case BUILT_IN_EH_FILTER:
6294 return expand_builtin_eh_filter (exp);
6295 case BUILT_IN_EH_COPY_VALUES:
6296 return expand_builtin_eh_copy_values (exp);
6298 case BUILT_IN_VA_START:
6299 return expand_builtin_va_start (exp);
6300 case BUILT_IN_VA_END:
6301 return expand_builtin_va_end (exp);
6302 case BUILT_IN_VA_COPY:
6303 return expand_builtin_va_copy (exp);
6304 case BUILT_IN_EXPECT:
6305 return expand_builtin_expect (exp, target);
6306 case BUILT_IN_PREFETCH:
6307 expand_builtin_prefetch (exp);
6308 return const0_rtx;
6310 case BUILT_IN_INIT_TRAMPOLINE:
6311 return expand_builtin_init_trampoline (exp);
6312 case BUILT_IN_ADJUST_TRAMPOLINE:
6313 return expand_builtin_adjust_trampoline (exp);
6315 case BUILT_IN_FORK:
6316 case BUILT_IN_EXECL:
6317 case BUILT_IN_EXECV:
6318 case BUILT_IN_EXECLP:
6319 case BUILT_IN_EXECLE:
6320 case BUILT_IN_EXECVP:
6321 case BUILT_IN_EXECVE:
6322 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6323 if (target)
6324 return target;
6325 break;
6327 case BUILT_IN_FETCH_AND_ADD_1:
6328 case BUILT_IN_FETCH_AND_ADD_2:
6329 case BUILT_IN_FETCH_AND_ADD_4:
6330 case BUILT_IN_FETCH_AND_ADD_8:
6331 case BUILT_IN_FETCH_AND_ADD_16:
6332 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6333 target = expand_builtin_sync_operation (mode, exp, PLUS,
6334 false, target, ignore);
6335 if (target)
6336 return target;
6337 break;
6339 case BUILT_IN_FETCH_AND_SUB_1:
6340 case BUILT_IN_FETCH_AND_SUB_2:
6341 case BUILT_IN_FETCH_AND_SUB_4:
6342 case BUILT_IN_FETCH_AND_SUB_8:
6343 case BUILT_IN_FETCH_AND_SUB_16:
6344 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6345 target = expand_builtin_sync_operation (mode, exp, MINUS,
6346 false, target, ignore);
6347 if (target)
6348 return target;
6349 break;
6351 case BUILT_IN_FETCH_AND_OR_1:
6352 case BUILT_IN_FETCH_AND_OR_2:
6353 case BUILT_IN_FETCH_AND_OR_4:
6354 case BUILT_IN_FETCH_AND_OR_8:
6355 case BUILT_IN_FETCH_AND_OR_16:
6356 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6357 target = expand_builtin_sync_operation (mode, exp, IOR,
6358 false, target, ignore);
6359 if (target)
6360 return target;
6361 break;
6363 case BUILT_IN_FETCH_AND_AND_1:
6364 case BUILT_IN_FETCH_AND_AND_2:
6365 case BUILT_IN_FETCH_AND_AND_4:
6366 case BUILT_IN_FETCH_AND_AND_8:
6367 case BUILT_IN_FETCH_AND_AND_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6369 target = expand_builtin_sync_operation (mode, exp, AND,
6370 false, target, ignore);
6371 if (target)
6372 return target;
6373 break;
6375 case BUILT_IN_FETCH_AND_XOR_1:
6376 case BUILT_IN_FETCH_AND_XOR_2:
6377 case BUILT_IN_FETCH_AND_XOR_4:
6378 case BUILT_IN_FETCH_AND_XOR_8:
6379 case BUILT_IN_FETCH_AND_XOR_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6381 target = expand_builtin_sync_operation (mode, exp, XOR,
6382 false, target, ignore);
6383 if (target)
6384 return target;
6385 break;
6387 case BUILT_IN_FETCH_AND_NAND_1:
6388 case BUILT_IN_FETCH_AND_NAND_2:
6389 case BUILT_IN_FETCH_AND_NAND_4:
6390 case BUILT_IN_FETCH_AND_NAND_8:
6391 case BUILT_IN_FETCH_AND_NAND_16:
6392 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6393 target = expand_builtin_sync_operation (mode, exp, NOT,
6394 false, target, ignore);
6395 if (target)
6396 return target;
6397 break;
6399 case BUILT_IN_ADD_AND_FETCH_1:
6400 case BUILT_IN_ADD_AND_FETCH_2:
6401 case BUILT_IN_ADD_AND_FETCH_4:
6402 case BUILT_IN_ADD_AND_FETCH_8:
6403 case BUILT_IN_ADD_AND_FETCH_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6405 target = expand_builtin_sync_operation (mode, exp, PLUS,
6406 true, target, ignore);
6407 if (target)
6408 return target;
6409 break;
6411 case BUILT_IN_SUB_AND_FETCH_1:
6412 case BUILT_IN_SUB_AND_FETCH_2:
6413 case BUILT_IN_SUB_AND_FETCH_4:
6414 case BUILT_IN_SUB_AND_FETCH_8:
6415 case BUILT_IN_SUB_AND_FETCH_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6417 target = expand_builtin_sync_operation (mode, exp, MINUS,
6418 true, target, ignore);
6419 if (target)
6420 return target;
6421 break;
6423 case BUILT_IN_OR_AND_FETCH_1:
6424 case BUILT_IN_OR_AND_FETCH_2:
6425 case BUILT_IN_OR_AND_FETCH_4:
6426 case BUILT_IN_OR_AND_FETCH_8:
6427 case BUILT_IN_OR_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, exp, IOR,
6430 true, target, ignore);
6431 if (target)
6432 return target;
6433 break;
6435 case BUILT_IN_AND_AND_FETCH_1:
6436 case BUILT_IN_AND_AND_FETCH_2:
6437 case BUILT_IN_AND_AND_FETCH_4:
6438 case BUILT_IN_AND_AND_FETCH_8:
6439 case BUILT_IN_AND_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, exp, AND,
6442 true, target, ignore);
6443 if (target)
6444 return target;
6445 break;
6447 case BUILT_IN_XOR_AND_FETCH_1:
6448 case BUILT_IN_XOR_AND_FETCH_2:
6449 case BUILT_IN_XOR_AND_FETCH_4:
6450 case BUILT_IN_XOR_AND_FETCH_8:
6451 case BUILT_IN_XOR_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, exp, XOR,
6454 true, target, ignore);
6455 if (target)
6456 return target;
6457 break;
6459 case BUILT_IN_NAND_AND_FETCH_1:
6460 case BUILT_IN_NAND_AND_FETCH_2:
6461 case BUILT_IN_NAND_AND_FETCH_4:
6462 case BUILT_IN_NAND_AND_FETCH_8:
6463 case BUILT_IN_NAND_AND_FETCH_16:
6464 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6465 target = expand_builtin_sync_operation (mode, exp, NOT,
6466 true, target, ignore);
6467 if (target)
6468 return target;
6469 break;
6471 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6472 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6473 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6474 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6475 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6476 if (mode == VOIDmode)
6477 mode = TYPE_MODE (boolean_type_node);
6478 if (!target || !register_operand (target, mode))
6479 target = gen_reg_rtx (mode);
6481 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6482 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6483 if (target)
6484 return target;
6485 break;
6487 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6488 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6489 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6490 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6491 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6492 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6493 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6494 if (target)
6495 return target;
6496 break;
6498 case BUILT_IN_LOCK_TEST_AND_SET_1:
6499 case BUILT_IN_LOCK_TEST_AND_SET_2:
6500 case BUILT_IN_LOCK_TEST_AND_SET_4:
6501 case BUILT_IN_LOCK_TEST_AND_SET_8:
6502 case BUILT_IN_LOCK_TEST_AND_SET_16:
6503 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6504 target = expand_builtin_lock_test_and_set (mode, exp, target);
6505 if (target)
6506 return target;
6507 break;
6509 case BUILT_IN_LOCK_RELEASE_1:
6510 case BUILT_IN_LOCK_RELEASE_2:
6511 case BUILT_IN_LOCK_RELEASE_4:
6512 case BUILT_IN_LOCK_RELEASE_8:
6513 case BUILT_IN_LOCK_RELEASE_16:
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6515 expand_builtin_lock_release (mode, exp);
6516 return const0_rtx;
6518 case BUILT_IN_SYNCHRONIZE:
6519 expand_builtin_synchronize ();
6520 return const0_rtx;
6522 case BUILT_IN_OBJECT_SIZE:
6523 return expand_builtin_object_size (exp);
6525 case BUILT_IN_MEMCPY_CHK:
6526 case BUILT_IN_MEMPCPY_CHK:
6527 case BUILT_IN_MEMMOVE_CHK:
6528 case BUILT_IN_MEMSET_CHK:
6529 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6530 if (target)
6531 return target;
6532 break;
6534 case BUILT_IN_STRCPY_CHK:
6535 case BUILT_IN_STPCPY_CHK:
6536 case BUILT_IN_STRNCPY_CHK:
6537 case BUILT_IN_STRCAT_CHK:
6538 case BUILT_IN_STRNCAT_CHK:
6539 case BUILT_IN_SNPRINTF_CHK:
6540 case BUILT_IN_VSNPRINTF_CHK:
6541 maybe_emit_chk_warning (exp, fcode);
6542 break;
6544 case BUILT_IN_SPRINTF_CHK:
6545 case BUILT_IN_VSPRINTF_CHK:
6546 maybe_emit_sprintf_chk_warning (exp, fcode);
6547 break;
6549 case BUILT_IN_FREE:
6550 maybe_emit_free_warning (exp);
6551 break;
6553 default: /* just do library call, if unknown builtin */
6554 break;
6557 /* The switch statement above can drop through to cause the function
6558 to be called normally. */
6559 return expand_call (exp, target, ignore);
6562 /* Determine whether a tree node represents a call to a built-in
6563 function. If the tree T is a call to a built-in function with
6564 the right number of arguments of the appropriate types, return
6565 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6566 Otherwise the return value is END_BUILTINS. */
6568 enum built_in_function
6569 builtin_mathfn_code (const_tree t)
6571 const_tree fndecl, arg, parmlist;
6572 const_tree argtype, parmtype;
6573 const_call_expr_arg_iterator iter;
6575 if (TREE_CODE (t) != CALL_EXPR
6576 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6577 return END_BUILTINS;
6579 fndecl = get_callee_fndecl (t);
6580 if (fndecl == NULL_TREE
6581 || TREE_CODE (fndecl) != FUNCTION_DECL
6582 || ! DECL_BUILT_IN (fndecl)
6583 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6584 return END_BUILTINS;
6586 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6587 init_const_call_expr_arg_iterator (t, &iter);
6588 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6590 /* If a function doesn't take a variable number of arguments,
6591 the last element in the list will have type `void'. */
6592 parmtype = TREE_VALUE (parmlist);
6593 if (VOID_TYPE_P (parmtype))
6595 if (more_const_call_expr_args_p (&iter))
6596 return END_BUILTINS;
6597 return DECL_FUNCTION_CODE (fndecl);
6600 if (! more_const_call_expr_args_p (&iter))
6601 return END_BUILTINS;
6603 arg = next_const_call_expr_arg (&iter);
6604 argtype = TREE_TYPE (arg);
6606 if (SCALAR_FLOAT_TYPE_P (parmtype))
6608 if (! SCALAR_FLOAT_TYPE_P (argtype))
6609 return END_BUILTINS;
6611 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6613 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6614 return END_BUILTINS;
6616 else if (POINTER_TYPE_P (parmtype))
6618 if (! POINTER_TYPE_P (argtype))
6619 return END_BUILTINS;
6621 else if (INTEGRAL_TYPE_P (parmtype))
6623 if (! INTEGRAL_TYPE_P (argtype))
6624 return END_BUILTINS;
6626 else
6627 return END_BUILTINS;
6630 /* Variable-length argument list. */
6631 return DECL_FUNCTION_CODE (fndecl);
6634 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6635 evaluate to a constant. */
6637 static tree
6638 fold_builtin_constant_p (tree arg)
6640 /* We return 1 for a numeric type that's known to be a constant
6641 value at compile-time or for an aggregate type that's a
6642 literal constant. */
6643 STRIP_NOPS (arg);
6645 /* If we know this is a constant, emit the constant of one. */
6646 if (CONSTANT_CLASS_P (arg)
6647 || (TREE_CODE (arg) == CONSTRUCTOR
6648 && TREE_CONSTANT (arg)))
6649 return integer_one_node;
6650 if (TREE_CODE (arg) == ADDR_EXPR)
6652 tree op = TREE_OPERAND (arg, 0);
6653 if (TREE_CODE (op) == STRING_CST
6654 || (TREE_CODE (op) == ARRAY_REF
6655 && integer_zerop (TREE_OPERAND (op, 1))
6656 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6657 return integer_one_node;
6660 /* If this expression has side effects, show we don't know it to be a
6661 constant. Likewise if it's a pointer or aggregate type since in
6662 those case we only want literals, since those are only optimized
6663 when generating RTL, not later.
6664 And finally, if we are compiling an initializer, not code, we
6665 need to return a definite result now; there's not going to be any
6666 more optimization done. */
6667 if (TREE_SIDE_EFFECTS (arg)
6668 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6669 || POINTER_TYPE_P (TREE_TYPE (arg))
6670 || cfun == 0
6671 || folding_initializer)
6672 return integer_zero_node;
6674 return NULL_TREE;
6677 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6678 return it as a truthvalue. */
6680 static tree
6681 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6683 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6685 fn = built_in_decls[BUILT_IN_EXPECT];
6686 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6687 ret_type = TREE_TYPE (TREE_TYPE (fn));
6688 pred_type = TREE_VALUE (arg_types);
6689 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6691 pred = fold_convert_loc (loc, pred_type, pred);
6692 expected = fold_convert_loc (loc, expected_type, expected);
6693 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6695 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6696 build_int_cst (ret_type, 0));
6699 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6700 NULL_TREE if no simplification is possible. */
6702 static tree
6703 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6705 tree inner, fndecl;
6706 enum tree_code code;
6708 /* If this is a builtin_expect within a builtin_expect keep the
6709 inner one. See through a comparison against a constant. It
6710 might have been added to create a thruthvalue. */
6711 inner = arg0;
6712 if (COMPARISON_CLASS_P (inner)
6713 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6714 inner = TREE_OPERAND (inner, 0);
6716 if (TREE_CODE (inner) == CALL_EXPR
6717 && (fndecl = get_callee_fndecl (inner))
6718 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6719 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6720 return arg0;
6722 /* Distribute the expected value over short-circuiting operators.
6723 See through the cast from truthvalue_type_node to long. */
6724 inner = arg0;
6725 while (TREE_CODE (inner) == NOP_EXPR
6726 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6727 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6728 inner = TREE_OPERAND (inner, 0);
6730 code = TREE_CODE (inner);
6731 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6733 tree op0 = TREE_OPERAND (inner, 0);
6734 tree op1 = TREE_OPERAND (inner, 1);
6736 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6737 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6738 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6740 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6743 /* If the argument isn't invariant then there's nothing else we can do. */
6744 if (!TREE_CONSTANT (arg0))
6745 return NULL_TREE;
6747 /* If we expect that a comparison against the argument will fold to
6748 a constant return the constant. In practice, this means a true
6749 constant or the address of a non-weak symbol. */
6750 inner = arg0;
6751 STRIP_NOPS (inner);
6752 if (TREE_CODE (inner) == ADDR_EXPR)
6756 inner = TREE_OPERAND (inner, 0);
6758 while (TREE_CODE (inner) == COMPONENT_REF
6759 || TREE_CODE (inner) == ARRAY_REF);
6760 if ((TREE_CODE (inner) == VAR_DECL
6761 || TREE_CODE (inner) == FUNCTION_DECL)
6762 && DECL_WEAK (inner))
6763 return NULL_TREE;
6766 /* Otherwise, ARG0 already has the proper type for the return value. */
6767 return arg0;
6770 /* Fold a call to __builtin_classify_type with argument ARG. */
6772 static tree
6773 fold_builtin_classify_type (tree arg)
6775 if (arg == 0)
6776 return build_int_cst (integer_type_node, no_type_class);
6778 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
6781 /* Fold a call to __builtin_strlen with argument ARG. */
6783 static tree
6784 fold_builtin_strlen (location_t loc, tree type, tree arg)
6786 if (!validate_arg (arg, POINTER_TYPE))
6787 return NULL_TREE;
6788 else
6790 tree len = c_strlen (arg, 0);
6792 if (len)
6793 return fold_convert_loc (loc, type, len);
6795 return NULL_TREE;
6799 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6801 static tree
6802 fold_builtin_inf (location_t loc, tree type, int warn)
6804 REAL_VALUE_TYPE real;
6806 /* __builtin_inff is intended to be usable to define INFINITY on all
6807 targets. If an infinity is not available, INFINITY expands "to a
6808 positive constant of type float that overflows at translation
6809 time", footnote "In this case, using INFINITY will violate the
6810 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6811 Thus we pedwarn to ensure this constraint violation is
6812 diagnosed. */
6813 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6814 pedwarn (loc, 0, "target format does not support infinity");
6816 real_inf (&real);
6817 return build_real (type, real);
6820 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6822 static tree
6823 fold_builtin_nan (tree arg, tree type, int quiet)
6825 REAL_VALUE_TYPE real;
6826 const char *str;
6828 if (!validate_arg (arg, POINTER_TYPE))
6829 return NULL_TREE;
6830 str = c_getstr (arg);
6831 if (!str)
6832 return NULL_TREE;
6834 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6835 return NULL_TREE;
6837 return build_real (type, real);
6840 /* Return true if the floating point expression T has an integer value.
6841 We also allow +Inf, -Inf and NaN to be considered integer values. */
6843 static bool
6844 integer_valued_real_p (tree t)
6846 switch (TREE_CODE (t))
6848 case FLOAT_EXPR:
6849 return true;
6851 case ABS_EXPR:
6852 case SAVE_EXPR:
6853 return integer_valued_real_p (TREE_OPERAND (t, 0));
6855 case COMPOUND_EXPR:
6856 case MODIFY_EXPR:
6857 case BIND_EXPR:
6858 return integer_valued_real_p (TREE_OPERAND (t, 1));
6860 case PLUS_EXPR:
6861 case MINUS_EXPR:
6862 case MULT_EXPR:
6863 case MIN_EXPR:
6864 case MAX_EXPR:
6865 return integer_valued_real_p (TREE_OPERAND (t, 0))
6866 && integer_valued_real_p (TREE_OPERAND (t, 1));
6868 case COND_EXPR:
6869 return integer_valued_real_p (TREE_OPERAND (t, 1))
6870 && integer_valued_real_p (TREE_OPERAND (t, 2));
6872 case REAL_CST:
6873 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6875 case NOP_EXPR:
6877 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6878 if (TREE_CODE (type) == INTEGER_TYPE)
6879 return true;
6880 if (TREE_CODE (type) == REAL_TYPE)
6881 return integer_valued_real_p (TREE_OPERAND (t, 0));
6882 break;
6885 case CALL_EXPR:
6886 switch (builtin_mathfn_code (t))
6888 CASE_FLT_FN (BUILT_IN_CEIL):
6889 CASE_FLT_FN (BUILT_IN_FLOOR):
6890 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6891 CASE_FLT_FN (BUILT_IN_RINT):
6892 CASE_FLT_FN (BUILT_IN_ROUND):
6893 CASE_FLT_FN (BUILT_IN_TRUNC):
6894 return true;
6896 CASE_FLT_FN (BUILT_IN_FMIN):
6897 CASE_FLT_FN (BUILT_IN_FMAX):
6898 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6899 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6901 default:
6902 break;
6904 break;
6906 default:
6907 break;
6909 return false;
6912 /* FNDECL is assumed to be a builtin where truncation can be propagated
6913 across (for instance floor((double)f) == (double)floorf (f).
6914 Do the transformation for a call with argument ARG. */
6916 static tree
6917 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6919 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6921 if (!validate_arg (arg, REAL_TYPE))
6922 return NULL_TREE;
6924 /* Integer rounding functions are idempotent. */
6925 if (fcode == builtin_mathfn_code (arg))
6926 return arg;
6928 /* If argument is already integer valued, and we don't need to worry
6929 about setting errno, there's no need to perform rounding. */
6930 if (! flag_errno_math && integer_valued_real_p (arg))
6931 return arg;
6933 if (optimize)
6935 tree arg0 = strip_float_extensions (arg);
6936 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6937 tree newtype = TREE_TYPE (arg0);
6938 tree decl;
6940 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6941 && (decl = mathfn_built_in (newtype, fcode)))
6942 return fold_convert_loc (loc, ftype,
6943 build_call_expr_loc (loc, decl, 1,
6944 fold_convert_loc (loc,
6945 newtype,
6946 arg0)));
6948 return NULL_TREE;
6951 /* FNDECL is assumed to be builtin which can narrow the FP type of
6952 the argument, for instance lround((double)f) -> lroundf (f).
6953 Do the transformation for a call with argument ARG. */
6955 static tree
6956 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
6958 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6960 if (!validate_arg (arg, REAL_TYPE))
6961 return NULL_TREE;
6963 /* If argument is already integer valued, and we don't need to worry
6964 about setting errno, there's no need to perform rounding. */
6965 if (! flag_errno_math && integer_valued_real_p (arg))
6966 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
6967 TREE_TYPE (TREE_TYPE (fndecl)), arg);
6969 if (optimize)
6971 tree ftype = TREE_TYPE (arg);
6972 tree arg0 = strip_float_extensions (arg);
6973 tree newtype = TREE_TYPE (arg0);
6974 tree decl;
6976 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6977 && (decl = mathfn_built_in (newtype, fcode)))
6978 return build_call_expr_loc (loc, decl, 1,
6979 fold_convert_loc (loc, newtype, arg0));
6982 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6983 sizeof (long long) == sizeof (long). */
6984 if (TYPE_PRECISION (long_long_integer_type_node)
6985 == TYPE_PRECISION (long_integer_type_node))
6987 tree newfn = NULL_TREE;
6988 switch (fcode)
6990 CASE_FLT_FN (BUILT_IN_LLCEIL):
6991 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6992 break;
6994 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6995 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6996 break;
6998 CASE_FLT_FN (BUILT_IN_LLROUND):
6999 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7000 break;
7002 CASE_FLT_FN (BUILT_IN_LLRINT):
7003 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7004 break;
7006 default:
7007 break;
7010 if (newfn)
7012 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7013 return fold_convert_loc (loc,
7014 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7018 return NULL_TREE;
7021 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7022 return type. Return NULL_TREE if no simplification can be made. */
7024 static tree
7025 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7027 tree res;
7029 if (!validate_arg (arg, COMPLEX_TYPE)
7030 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7031 return NULL_TREE;
7033 /* Calculate the result when the argument is a constant. */
7034 if (TREE_CODE (arg) == COMPLEX_CST
7035 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7036 type, mpfr_hypot)))
7037 return res;
7039 if (TREE_CODE (arg) == COMPLEX_EXPR)
7041 tree real = TREE_OPERAND (arg, 0);
7042 tree imag = TREE_OPERAND (arg, 1);
7044 /* If either part is zero, cabs is fabs of the other. */
7045 if (real_zerop (real))
7046 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7047 if (real_zerop (imag))
7048 return fold_build1_loc (loc, ABS_EXPR, type, real);
7050 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7051 if (flag_unsafe_math_optimizations
7052 && operand_equal_p (real, imag, OEP_PURE_SAME))
7054 const REAL_VALUE_TYPE sqrt2_trunc
7055 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7056 STRIP_NOPS (real);
7057 return fold_build2_loc (loc, MULT_EXPR, type,
7058 fold_build1_loc (loc, ABS_EXPR, type, real),
7059 build_real (type, sqrt2_trunc));
7063 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7064 if (TREE_CODE (arg) == NEGATE_EXPR
7065 || TREE_CODE (arg) == CONJ_EXPR)
7066 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7068 /* Don't do this when optimizing for size. */
7069 if (flag_unsafe_math_optimizations
7070 && optimize && optimize_function_for_speed_p (cfun))
7072 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7074 if (sqrtfn != NULL_TREE)
7076 tree rpart, ipart, result;
7078 arg = builtin_save_expr (arg);
7080 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7081 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7083 rpart = builtin_save_expr (rpart);
7084 ipart = builtin_save_expr (ipart);
7086 result = fold_build2_loc (loc, PLUS_EXPR, type,
7087 fold_build2_loc (loc, MULT_EXPR, type,
7088 rpart, rpart),
7089 fold_build2_loc (loc, MULT_EXPR, type,
7090 ipart, ipart));
7092 return build_call_expr_loc (loc, sqrtfn, 1, result);
7096 return NULL_TREE;
7099 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7100 complex tree type of the result. If NEG is true, the imaginary
7101 zero is negative. */
7103 static tree
7104 build_complex_cproj (tree type, bool neg)
7106 REAL_VALUE_TYPE rinf, rzero = dconst0;
7108 real_inf (&rinf);
7109 rzero.sign = neg;
7110 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7111 build_real (TREE_TYPE (type), rzero));
7114 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7115 return type. Return NULL_TREE if no simplification can be made. */
7117 static tree
7118 fold_builtin_cproj (location_t loc, tree arg, tree type)
7120 if (!validate_arg (arg, COMPLEX_TYPE)
7121 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7122 return NULL_TREE;
7124 /* If there are no infinities, return arg. */
7125 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7126 return non_lvalue_loc (loc, arg);
7128 /* Calculate the result when the argument is a constant. */
7129 if (TREE_CODE (arg) == COMPLEX_CST)
7131 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7132 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7134 if (real_isinf (real) || real_isinf (imag))
7135 return build_complex_cproj (type, imag->sign);
7136 else
7137 return arg;
7139 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7141 tree real = TREE_OPERAND (arg, 0);
7142 tree imag = TREE_OPERAND (arg, 1);
7144 STRIP_NOPS (real);
7145 STRIP_NOPS (imag);
7147 /* If the real part is inf and the imag part is known to be
7148 nonnegative, return (inf + 0i). Remember side-effects are
7149 possible in the imag part. */
7150 if (TREE_CODE (real) == REAL_CST
7151 && real_isinf (TREE_REAL_CST_PTR (real))
7152 && tree_expr_nonnegative_p (imag))
7153 return omit_one_operand_loc (loc, type,
7154 build_complex_cproj (type, false),
7155 arg);
7157 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7158 Remember side-effects are possible in the real part. */
7159 if (TREE_CODE (imag) == REAL_CST
7160 && real_isinf (TREE_REAL_CST_PTR (imag)))
7161 return
7162 omit_one_operand_loc (loc, type,
7163 build_complex_cproj (type, TREE_REAL_CST_PTR
7164 (imag)->sign), arg);
7167 return NULL_TREE;
7170 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7171 Return NULL_TREE if no simplification can be made. */
7173 static tree
7174 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7177 enum built_in_function fcode;
7178 tree res;
7180 if (!validate_arg (arg, REAL_TYPE))
7181 return NULL_TREE;
7183 /* Calculate the result when the argument is a constant. */
7184 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7185 return res;
7187 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7188 fcode = builtin_mathfn_code (arg);
7189 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7191 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7192 arg = fold_build2_loc (loc, MULT_EXPR, type,
7193 CALL_EXPR_ARG (arg, 0),
7194 build_real (type, dconsthalf));
7195 return build_call_expr_loc (loc, expfn, 1, arg);
7198 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7199 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7201 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7203 if (powfn)
7205 tree arg0 = CALL_EXPR_ARG (arg, 0);
7206 tree tree_root;
7207 /* The inner root was either sqrt or cbrt. */
7208 /* This was a conditional expression but it triggered a bug
7209 in Sun C 5.5. */
7210 REAL_VALUE_TYPE dconstroot;
7211 if (BUILTIN_SQRT_P (fcode))
7212 dconstroot = dconsthalf;
7213 else
7214 dconstroot = dconst_third ();
7216 /* Adjust for the outer root. */
7217 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7218 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7219 tree_root = build_real (type, dconstroot);
7220 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7224 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7225 if (flag_unsafe_math_optimizations
7226 && (fcode == BUILT_IN_POW
7227 || fcode == BUILT_IN_POWF
7228 || fcode == BUILT_IN_POWL))
7230 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7231 tree arg0 = CALL_EXPR_ARG (arg, 0);
7232 tree arg1 = CALL_EXPR_ARG (arg, 1);
7233 tree narg1;
7234 if (!tree_expr_nonnegative_p (arg0))
7235 arg0 = build1 (ABS_EXPR, type, arg0);
7236 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7237 build_real (type, dconsthalf));
7238 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7241 return NULL_TREE;
7244 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7245 Return NULL_TREE if no simplification can be made. */
7247 static tree
7248 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7250 const enum built_in_function fcode = builtin_mathfn_code (arg);
7251 tree res;
7253 if (!validate_arg (arg, REAL_TYPE))
7254 return NULL_TREE;
7256 /* Calculate the result when the argument is a constant. */
7257 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7258 return res;
7260 if (flag_unsafe_math_optimizations)
7262 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7263 if (BUILTIN_EXPONENT_P (fcode))
7265 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7266 const REAL_VALUE_TYPE third_trunc =
7267 real_value_truncate (TYPE_MODE (type), dconst_third ());
7268 arg = fold_build2_loc (loc, MULT_EXPR, type,
7269 CALL_EXPR_ARG (arg, 0),
7270 build_real (type, third_trunc));
7271 return build_call_expr_loc (loc, expfn, 1, arg);
7274 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7275 if (BUILTIN_SQRT_P (fcode))
7277 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7279 if (powfn)
7281 tree arg0 = CALL_EXPR_ARG (arg, 0);
7282 tree tree_root;
7283 REAL_VALUE_TYPE dconstroot = dconst_third ();
7285 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7286 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7287 tree_root = build_real (type, dconstroot);
7288 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7292 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7293 if (BUILTIN_CBRT_P (fcode))
7295 tree arg0 = CALL_EXPR_ARG (arg, 0);
7296 if (tree_expr_nonnegative_p (arg0))
7298 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7300 if (powfn)
7302 tree tree_root;
7303 REAL_VALUE_TYPE dconstroot;
7305 real_arithmetic (&dconstroot, MULT_EXPR,
7306 dconst_third_ptr (), dconst_third_ptr ());
7307 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7308 tree_root = build_real (type, dconstroot);
7309 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7314 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7315 if (fcode == BUILT_IN_POW
7316 || fcode == BUILT_IN_POWF
7317 || fcode == BUILT_IN_POWL)
7319 tree arg00 = CALL_EXPR_ARG (arg, 0);
7320 tree arg01 = CALL_EXPR_ARG (arg, 1);
7321 if (tree_expr_nonnegative_p (arg00))
7323 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7324 const REAL_VALUE_TYPE dconstroot
7325 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7326 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7327 build_real (type, dconstroot));
7328 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7332 return NULL_TREE;
7335 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7336 TYPE is the type of the return value. Return NULL_TREE if no
7337 simplification can be made. */
7339 static tree
7340 fold_builtin_cos (location_t loc,
7341 tree arg, tree type, tree fndecl)
7343 tree res, narg;
7345 if (!validate_arg (arg, REAL_TYPE))
7346 return NULL_TREE;
7348 /* Calculate the result when the argument is a constant. */
7349 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7350 return res;
7352 /* Optimize cos(-x) into cos (x). */
7353 if ((narg = fold_strip_sign_ops (arg)))
7354 return build_call_expr_loc (loc, fndecl, 1, narg);
7356 return NULL_TREE;
7359 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7360 Return NULL_TREE if no simplification can be made. */
7362 static tree
7363 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7365 if (validate_arg (arg, REAL_TYPE))
7367 tree res, narg;
7369 /* Calculate the result when the argument is a constant. */
7370 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7371 return res;
7373 /* Optimize cosh(-x) into cosh (x). */
7374 if ((narg = fold_strip_sign_ops (arg)))
7375 return build_call_expr_loc (loc, fndecl, 1, narg);
7378 return NULL_TREE;
7381 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7382 argument ARG. TYPE is the type of the return value. Return
7383 NULL_TREE if no simplification can be made. */
7385 static tree
7386 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7387 bool hyper)
7389 if (validate_arg (arg, COMPLEX_TYPE)
7390 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7392 tree tmp;
7394 /* Calculate the result when the argument is a constant. */
7395 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7396 return tmp;
7398 /* Optimize fn(-x) into fn(x). */
7399 if ((tmp = fold_strip_sign_ops (arg)))
7400 return build_call_expr_loc (loc, fndecl, 1, tmp);
7403 return NULL_TREE;
7406 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7407 Return NULL_TREE if no simplification can be made. */
7409 static tree
7410 fold_builtin_tan (tree arg, tree type)
7412 enum built_in_function fcode;
7413 tree res;
7415 if (!validate_arg (arg, REAL_TYPE))
7416 return NULL_TREE;
7418 /* Calculate the result when the argument is a constant. */
7419 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7420 return res;
7422 /* Optimize tan(atan(x)) = x. */
7423 fcode = builtin_mathfn_code (arg);
7424 if (flag_unsafe_math_optimizations
7425 && (fcode == BUILT_IN_ATAN
7426 || fcode == BUILT_IN_ATANF
7427 || fcode == BUILT_IN_ATANL))
7428 return CALL_EXPR_ARG (arg, 0);
7430 return NULL_TREE;
7433 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7434 NULL_TREE if no simplification can be made. */
7436 static tree
7437 fold_builtin_sincos (location_t loc,
7438 tree arg0, tree arg1, tree arg2)
7440 tree type;
7441 tree res, fn, call;
7443 if (!validate_arg (arg0, REAL_TYPE)
7444 || !validate_arg (arg1, POINTER_TYPE)
7445 || !validate_arg (arg2, POINTER_TYPE))
7446 return NULL_TREE;
7448 type = TREE_TYPE (arg0);
7450 /* Calculate the result when the argument is a constant. */
7451 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7452 return res;
7454 /* Canonicalize sincos to cexpi. */
7455 if (!TARGET_C99_FUNCTIONS)
7456 return NULL_TREE;
7457 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7458 if (!fn)
7459 return NULL_TREE;
7461 call = build_call_expr_loc (loc, fn, 1, arg0);
7462 call = builtin_save_expr (call);
7464 return build2 (COMPOUND_EXPR, void_type_node,
7465 build2 (MODIFY_EXPR, void_type_node,
7466 build_fold_indirect_ref_loc (loc, arg1),
7467 build1 (IMAGPART_EXPR, type, call)),
7468 build2 (MODIFY_EXPR, void_type_node,
7469 build_fold_indirect_ref_loc (loc, arg2),
7470 build1 (REALPART_EXPR, type, call)));
7473 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7474 NULL_TREE if no simplification can be made. */
7476 static tree
7477 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7479 tree rtype;
7480 tree realp, imagp, ifn;
7481 tree res;
7483 if (!validate_arg (arg0, COMPLEX_TYPE)
7484 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7485 return NULL_TREE;
7487 /* Calculate the result when the argument is a constant. */
7488 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7489 return res;
7491 rtype = TREE_TYPE (TREE_TYPE (arg0));
7493 /* In case we can figure out the real part of arg0 and it is constant zero
7494 fold to cexpi. */
7495 if (!TARGET_C99_FUNCTIONS)
7496 return NULL_TREE;
7497 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7498 if (!ifn)
7499 return NULL_TREE;
7501 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7502 && real_zerop (realp))
7504 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7505 return build_call_expr_loc (loc, ifn, 1, narg);
7508 /* In case we can easily decompose real and imaginary parts split cexp
7509 to exp (r) * cexpi (i). */
7510 if (flag_unsafe_math_optimizations
7511 && realp)
7513 tree rfn, rcall, icall;
7515 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7516 if (!rfn)
7517 return NULL_TREE;
7519 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7520 if (!imagp)
7521 return NULL_TREE;
7523 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7524 icall = builtin_save_expr (icall);
7525 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7526 rcall = builtin_save_expr (rcall);
7527 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7528 fold_build2_loc (loc, MULT_EXPR, rtype,
7529 rcall,
7530 fold_build1_loc (loc, REALPART_EXPR,
7531 rtype, icall)),
7532 fold_build2_loc (loc, MULT_EXPR, rtype,
7533 rcall,
7534 fold_build1_loc (loc, IMAGPART_EXPR,
7535 rtype, icall)));
7538 return NULL_TREE;
7541 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7542 Return NULL_TREE if no simplification can be made. */
7544 static tree
7545 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7547 if (!validate_arg (arg, REAL_TYPE))
7548 return NULL_TREE;
7550 /* Optimize trunc of constant value. */
7551 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7553 REAL_VALUE_TYPE r, x;
7554 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7556 x = TREE_REAL_CST (arg);
7557 real_trunc (&r, TYPE_MODE (type), &x);
7558 return build_real (type, r);
7561 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7564 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7565 Return NULL_TREE if no simplification can be made. */
7567 static tree
7568 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7570 if (!validate_arg (arg, REAL_TYPE))
7571 return NULL_TREE;
7573 /* Optimize floor of constant value. */
7574 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7576 REAL_VALUE_TYPE x;
7578 x = TREE_REAL_CST (arg);
7579 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7581 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7582 REAL_VALUE_TYPE r;
7584 real_floor (&r, TYPE_MODE (type), &x);
7585 return build_real (type, r);
7589 /* Fold floor (x) where x is nonnegative to trunc (x). */
7590 if (tree_expr_nonnegative_p (arg))
7592 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7593 if (truncfn)
7594 return build_call_expr_loc (loc, truncfn, 1, arg);
7597 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7600 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7601 Return NULL_TREE if no simplification can be made. */
7603 static tree
7604 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7606 if (!validate_arg (arg, REAL_TYPE))
7607 return NULL_TREE;
7609 /* Optimize ceil of constant value. */
7610 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7612 REAL_VALUE_TYPE x;
7614 x = TREE_REAL_CST (arg);
7615 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7617 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7618 REAL_VALUE_TYPE r;
7620 real_ceil (&r, TYPE_MODE (type), &x);
7621 return build_real (type, r);
7625 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7628 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7629 Return NULL_TREE if no simplification can be made. */
7631 static tree
7632 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7634 if (!validate_arg (arg, REAL_TYPE))
7635 return NULL_TREE;
7637 /* Optimize round of constant value. */
7638 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7640 REAL_VALUE_TYPE x;
7642 x = TREE_REAL_CST (arg);
7643 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7645 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7646 REAL_VALUE_TYPE r;
7648 real_round (&r, TYPE_MODE (type), &x);
7649 return build_real (type, r);
7653 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7656 /* Fold function call to builtin lround, lroundf or lroundl (or the
7657 corresponding long long versions) and other rounding functions. ARG
7658 is the argument to the call. Return NULL_TREE if no simplification
7659 can be made. */
7661 static tree
7662 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7664 if (!validate_arg (arg, REAL_TYPE))
7665 return NULL_TREE;
7667 /* Optimize lround of constant value. */
7668 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7670 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7672 if (real_isfinite (&x))
7674 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7675 tree ftype = TREE_TYPE (arg);
7676 double_int val;
7677 REAL_VALUE_TYPE r;
7679 switch (DECL_FUNCTION_CODE (fndecl))
7681 CASE_FLT_FN (BUILT_IN_LFLOOR):
7682 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7683 real_floor (&r, TYPE_MODE (ftype), &x);
7684 break;
7686 CASE_FLT_FN (BUILT_IN_LCEIL):
7687 CASE_FLT_FN (BUILT_IN_LLCEIL):
7688 real_ceil (&r, TYPE_MODE (ftype), &x);
7689 break;
7691 CASE_FLT_FN (BUILT_IN_LROUND):
7692 CASE_FLT_FN (BUILT_IN_LLROUND):
7693 real_round (&r, TYPE_MODE (ftype), &x);
7694 break;
7696 default:
7697 gcc_unreachable ();
7700 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7701 if (double_int_fits_to_tree_p (itype, val))
7702 return double_int_to_tree (itype, val);
7706 switch (DECL_FUNCTION_CODE (fndecl))
7708 CASE_FLT_FN (BUILT_IN_LFLOOR):
7709 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7710 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7711 if (tree_expr_nonnegative_p (arg))
7712 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7713 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7714 break;
7715 default:;
7718 return fold_fixed_mathfn (loc, fndecl, arg);
7721 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7722 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7723 the argument to the call. Return NULL_TREE if no simplification can
7724 be made. */
7726 static tree
7727 fold_builtin_bitop (tree fndecl, tree arg)
7729 if (!validate_arg (arg, INTEGER_TYPE))
7730 return NULL_TREE;
7732 /* Optimize for constant argument. */
7733 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7735 HOST_WIDE_INT hi, width, result;
7736 unsigned HOST_WIDE_INT lo;
7737 tree type;
7739 type = TREE_TYPE (arg);
7740 width = TYPE_PRECISION (type);
7741 lo = TREE_INT_CST_LOW (arg);
7743 /* Clear all the bits that are beyond the type's precision. */
7744 if (width > HOST_BITS_PER_WIDE_INT)
7746 hi = TREE_INT_CST_HIGH (arg);
7747 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7748 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7750 else
7752 hi = 0;
7753 if (width < HOST_BITS_PER_WIDE_INT)
7754 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7757 switch (DECL_FUNCTION_CODE (fndecl))
7759 CASE_INT_FN (BUILT_IN_FFS):
7760 if (lo != 0)
7761 result = ffs_hwi (lo);
7762 else if (hi != 0)
7763 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
7764 else
7765 result = 0;
7766 break;
7768 CASE_INT_FN (BUILT_IN_CLZ):
7769 if (hi != 0)
7770 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7771 else if (lo != 0)
7772 result = width - floor_log2 (lo) - 1;
7773 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7774 result = width;
7775 break;
7777 CASE_INT_FN (BUILT_IN_CTZ):
7778 if (lo != 0)
7779 result = ctz_hwi (lo);
7780 else if (hi != 0)
7781 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
7782 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7783 result = width;
7784 break;
7786 CASE_INT_FN (BUILT_IN_POPCOUNT):
7787 result = 0;
7788 while (lo)
7789 result++, lo &= lo - 1;
7790 while (hi)
7791 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7792 break;
7794 CASE_INT_FN (BUILT_IN_PARITY):
7795 result = 0;
7796 while (lo)
7797 result++, lo &= lo - 1;
7798 while (hi)
7799 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7800 result &= 1;
7801 break;
7803 default:
7804 gcc_unreachable ();
7807 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7810 return NULL_TREE;
7813 /* Fold function call to builtin_bswap and the long and long long
7814 variants. Return NULL_TREE if no simplification can be made. */
7815 static tree
7816 fold_builtin_bswap (tree fndecl, tree arg)
7818 if (! validate_arg (arg, INTEGER_TYPE))
7819 return NULL_TREE;
7821 /* Optimize constant value. */
7822 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7824 HOST_WIDE_INT hi, width, r_hi = 0;
7825 unsigned HOST_WIDE_INT lo, r_lo = 0;
7826 tree type;
7828 type = TREE_TYPE (arg);
7829 width = TYPE_PRECISION (type);
7830 lo = TREE_INT_CST_LOW (arg);
7831 hi = TREE_INT_CST_HIGH (arg);
7833 switch (DECL_FUNCTION_CODE (fndecl))
7835 case BUILT_IN_BSWAP32:
7836 case BUILT_IN_BSWAP64:
7838 int s;
7840 for (s = 0; s < width; s += 8)
7842 int d = width - s - 8;
7843 unsigned HOST_WIDE_INT byte;
7845 if (s < HOST_BITS_PER_WIDE_INT)
7846 byte = (lo >> s) & 0xff;
7847 else
7848 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7850 if (d < HOST_BITS_PER_WIDE_INT)
7851 r_lo |= byte << d;
7852 else
7853 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7857 break;
7859 default:
7860 gcc_unreachable ();
7863 if (width < HOST_BITS_PER_WIDE_INT)
7864 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7865 else
7866 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7869 return NULL_TREE;
7872 /* A subroutine of fold_builtin to fold the various logarithmic
7873 functions. Return NULL_TREE if no simplification can me made.
7874 FUNC is the corresponding MPFR logarithm function. */
7876 static tree
7877 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7878 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7880 if (validate_arg (arg, REAL_TYPE))
7882 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7883 tree res;
7884 const enum built_in_function fcode = builtin_mathfn_code (arg);
7886 /* Calculate the result when the argument is a constant. */
7887 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7888 return res;
7890 /* Special case, optimize logN(expN(x)) = x. */
7891 if (flag_unsafe_math_optimizations
7892 && ((func == mpfr_log
7893 && (fcode == BUILT_IN_EXP
7894 || fcode == BUILT_IN_EXPF
7895 || fcode == BUILT_IN_EXPL))
7896 || (func == mpfr_log2
7897 && (fcode == BUILT_IN_EXP2
7898 || fcode == BUILT_IN_EXP2F
7899 || fcode == BUILT_IN_EXP2L))
7900 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7901 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7903 /* Optimize logN(func()) for various exponential functions. We
7904 want to determine the value "x" and the power "exponent" in
7905 order to transform logN(x**exponent) into exponent*logN(x). */
7906 if (flag_unsafe_math_optimizations)
7908 tree exponent = 0, x = 0;
7910 switch (fcode)
7912 CASE_FLT_FN (BUILT_IN_EXP):
7913 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7914 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7915 dconst_e ()));
7916 exponent = CALL_EXPR_ARG (arg, 0);
7917 break;
7918 CASE_FLT_FN (BUILT_IN_EXP2):
7919 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7920 x = build_real (type, dconst2);
7921 exponent = CALL_EXPR_ARG (arg, 0);
7922 break;
7923 CASE_FLT_FN (BUILT_IN_EXP10):
7924 CASE_FLT_FN (BUILT_IN_POW10):
7925 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7927 REAL_VALUE_TYPE dconst10;
7928 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7929 x = build_real (type, dconst10);
7931 exponent = CALL_EXPR_ARG (arg, 0);
7932 break;
7933 CASE_FLT_FN (BUILT_IN_SQRT):
7934 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7935 x = CALL_EXPR_ARG (arg, 0);
7936 exponent = build_real (type, dconsthalf);
7937 break;
7938 CASE_FLT_FN (BUILT_IN_CBRT):
7939 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7940 x = CALL_EXPR_ARG (arg, 0);
7941 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7942 dconst_third ()));
7943 break;
7944 CASE_FLT_FN (BUILT_IN_POW):
7945 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7946 x = CALL_EXPR_ARG (arg, 0);
7947 exponent = CALL_EXPR_ARG (arg, 1);
7948 break;
7949 default:
7950 break;
7953 /* Now perform the optimization. */
7954 if (x && exponent)
7956 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
7957 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
7962 return NULL_TREE;
7965 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7966 NULL_TREE if no simplification can be made. */
7968 static tree
7969 fold_builtin_hypot (location_t loc, tree fndecl,
7970 tree arg0, tree arg1, tree type)
7972 tree res, narg0, narg1;
7974 if (!validate_arg (arg0, REAL_TYPE)
7975 || !validate_arg (arg1, REAL_TYPE))
7976 return NULL_TREE;
7978 /* Calculate the result when the argument is a constant. */
7979 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7980 return res;
7982 /* If either argument to hypot has a negate or abs, strip that off.
7983 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7984 narg0 = fold_strip_sign_ops (arg0);
7985 narg1 = fold_strip_sign_ops (arg1);
7986 if (narg0 || narg1)
7988 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
7989 narg1 ? narg1 : arg1);
7992 /* If either argument is zero, hypot is fabs of the other. */
7993 if (real_zerop (arg0))
7994 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
7995 else if (real_zerop (arg1))
7996 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
7998 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7999 if (flag_unsafe_math_optimizations
8000 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8002 const REAL_VALUE_TYPE sqrt2_trunc
8003 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8004 return fold_build2_loc (loc, MULT_EXPR, type,
8005 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8006 build_real (type, sqrt2_trunc));
8009 return NULL_TREE;
8013 /* Fold a builtin function call to pow, powf, or powl. Return
8014 NULL_TREE if no simplification can be made. */
8015 static tree
8016 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8018 tree res;
8020 if (!validate_arg (arg0, REAL_TYPE)
8021 || !validate_arg (arg1, REAL_TYPE))
8022 return NULL_TREE;
8024 /* Calculate the result when the argument is a constant. */
8025 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8026 return res;
8028 /* Optimize pow(1.0,y) = 1.0. */
8029 if (real_onep (arg0))
8030 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8032 if (TREE_CODE (arg1) == REAL_CST
8033 && !TREE_OVERFLOW (arg1))
8035 REAL_VALUE_TYPE cint;
8036 REAL_VALUE_TYPE c;
8037 HOST_WIDE_INT n;
8039 c = TREE_REAL_CST (arg1);
8041 /* Optimize pow(x,0.0) = 1.0. */
8042 if (REAL_VALUES_EQUAL (c, dconst0))
8043 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8044 arg0);
8046 /* Optimize pow(x,1.0) = x. */
8047 if (REAL_VALUES_EQUAL (c, dconst1))
8048 return arg0;
8050 /* Optimize pow(x,-1.0) = 1.0/x. */
8051 if (REAL_VALUES_EQUAL (c, dconstm1))
8052 return fold_build2_loc (loc, RDIV_EXPR, type,
8053 build_real (type, dconst1), arg0);
8055 /* Optimize pow(x,0.5) = sqrt(x). */
8056 if (flag_unsafe_math_optimizations
8057 && REAL_VALUES_EQUAL (c, dconsthalf))
8059 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8061 if (sqrtfn != NULL_TREE)
8062 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8065 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8066 if (flag_unsafe_math_optimizations)
8068 const REAL_VALUE_TYPE dconstroot
8069 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8071 if (REAL_VALUES_EQUAL (c, dconstroot))
8073 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8074 if (cbrtfn != NULL_TREE)
8075 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8079 /* Check for an integer exponent. */
8080 n = real_to_integer (&c);
8081 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8082 if (real_identical (&c, &cint))
8084 /* Attempt to evaluate pow at compile-time, unless this should
8085 raise an exception. */
8086 if (TREE_CODE (arg0) == REAL_CST
8087 && !TREE_OVERFLOW (arg0)
8088 && (n > 0
8089 || (!flag_trapping_math && !flag_errno_math)
8090 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8092 REAL_VALUE_TYPE x;
8093 bool inexact;
8095 x = TREE_REAL_CST (arg0);
8096 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8097 if (flag_unsafe_math_optimizations || !inexact)
8098 return build_real (type, x);
8101 /* Strip sign ops from even integer powers. */
8102 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8104 tree narg0 = fold_strip_sign_ops (arg0);
8105 if (narg0)
8106 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8111 if (flag_unsafe_math_optimizations)
8113 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8115 /* Optimize pow(expN(x),y) = expN(x*y). */
8116 if (BUILTIN_EXPONENT_P (fcode))
8118 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8119 tree arg = CALL_EXPR_ARG (arg0, 0);
8120 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8121 return build_call_expr_loc (loc, expfn, 1, arg);
8124 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8125 if (BUILTIN_SQRT_P (fcode))
8127 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8128 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8129 build_real (type, dconsthalf));
8130 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8133 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8134 if (BUILTIN_CBRT_P (fcode))
8136 tree arg = CALL_EXPR_ARG (arg0, 0);
8137 if (tree_expr_nonnegative_p (arg))
8139 const REAL_VALUE_TYPE dconstroot
8140 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8141 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8142 build_real (type, dconstroot));
8143 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8147 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8148 if (fcode == BUILT_IN_POW
8149 || fcode == BUILT_IN_POWF
8150 || fcode == BUILT_IN_POWL)
8152 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8153 if (tree_expr_nonnegative_p (arg00))
8155 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8156 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8157 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8162 return NULL_TREE;
8165 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8166 Return NULL_TREE if no simplification can be made. */
8167 static tree
8168 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8169 tree arg0, tree arg1, tree type)
8171 if (!validate_arg (arg0, REAL_TYPE)
8172 || !validate_arg (arg1, INTEGER_TYPE))
8173 return NULL_TREE;
8175 /* Optimize pow(1.0,y) = 1.0. */
8176 if (real_onep (arg0))
8177 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8179 if (host_integerp (arg1, 0))
8181 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8183 /* Evaluate powi at compile-time. */
8184 if (TREE_CODE (arg0) == REAL_CST
8185 && !TREE_OVERFLOW (arg0))
8187 REAL_VALUE_TYPE x;
8188 x = TREE_REAL_CST (arg0);
8189 real_powi (&x, TYPE_MODE (type), &x, c);
8190 return build_real (type, x);
8193 /* Optimize pow(x,0) = 1.0. */
8194 if (c == 0)
8195 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8196 arg0);
8198 /* Optimize pow(x,1) = x. */
8199 if (c == 1)
8200 return arg0;
8202 /* Optimize pow(x,-1) = 1.0/x. */
8203 if (c == -1)
8204 return fold_build2_loc (loc, RDIV_EXPR, type,
8205 build_real (type, dconst1), arg0);
8208 return NULL_TREE;
8211 /* A subroutine of fold_builtin to fold the various exponent
8212 functions. Return NULL_TREE if no simplification can be made.
8213 FUNC is the corresponding MPFR exponent function. */
8215 static tree
8216 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8217 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8219 if (validate_arg (arg, REAL_TYPE))
8221 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8222 tree res;
8224 /* Calculate the result when the argument is a constant. */
8225 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8226 return res;
8228 /* Optimize expN(logN(x)) = x. */
8229 if (flag_unsafe_math_optimizations)
8231 const enum built_in_function fcode = builtin_mathfn_code (arg);
8233 if ((func == mpfr_exp
8234 && (fcode == BUILT_IN_LOG
8235 || fcode == BUILT_IN_LOGF
8236 || fcode == BUILT_IN_LOGL))
8237 || (func == mpfr_exp2
8238 && (fcode == BUILT_IN_LOG2
8239 || fcode == BUILT_IN_LOG2F
8240 || fcode == BUILT_IN_LOG2L))
8241 || (func == mpfr_exp10
8242 && (fcode == BUILT_IN_LOG10
8243 || fcode == BUILT_IN_LOG10F
8244 || fcode == BUILT_IN_LOG10L)))
8245 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8249 return NULL_TREE;
8252 /* Return true if VAR is a VAR_DECL or a component thereof. */
8254 static bool
8255 var_decl_component_p (tree var)
8257 tree inner = var;
8258 while (handled_component_p (inner))
8259 inner = TREE_OPERAND (inner, 0);
8260 return SSA_VAR_P (inner);
8263 /* Fold function call to builtin memset. Return
8264 NULL_TREE if no simplification can be made. */
8266 static tree
8267 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8268 tree type, bool ignore)
8270 tree var, ret, etype;
8271 unsigned HOST_WIDE_INT length, cval;
8273 if (! validate_arg (dest, POINTER_TYPE)
8274 || ! validate_arg (c, INTEGER_TYPE)
8275 || ! validate_arg (len, INTEGER_TYPE))
8276 return NULL_TREE;
8278 if (! host_integerp (len, 1))
8279 return NULL_TREE;
8281 /* If the LEN parameter is zero, return DEST. */
8282 if (integer_zerop (len))
8283 return omit_one_operand_loc (loc, type, dest, c);
8285 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8286 return NULL_TREE;
8288 var = dest;
8289 STRIP_NOPS (var);
8290 if (TREE_CODE (var) != ADDR_EXPR)
8291 return NULL_TREE;
8293 var = TREE_OPERAND (var, 0);
8294 if (TREE_THIS_VOLATILE (var))
8295 return NULL_TREE;
8297 etype = TREE_TYPE (var);
8298 if (TREE_CODE (etype) == ARRAY_TYPE)
8299 etype = TREE_TYPE (etype);
8301 if (!INTEGRAL_TYPE_P (etype)
8302 && !POINTER_TYPE_P (etype))
8303 return NULL_TREE;
8305 if (! var_decl_component_p (var))
8306 return NULL_TREE;
8308 length = tree_low_cst (len, 1);
8309 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8310 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8311 < length)
8312 return NULL_TREE;
8314 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8315 return NULL_TREE;
8317 if (integer_zerop (c))
8318 cval = 0;
8319 else
8321 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8322 return NULL_TREE;
8324 cval = TREE_INT_CST_LOW (c);
8325 cval &= 0xff;
8326 cval |= cval << 8;
8327 cval |= cval << 16;
8328 cval |= (cval << 31) << 1;
8331 ret = build_int_cst_type (etype, cval);
8332 var = build_fold_indirect_ref_loc (loc,
8333 fold_convert_loc (loc,
8334 build_pointer_type (etype),
8335 dest));
8336 ret = build2 (MODIFY_EXPR, etype, var, ret);
8337 if (ignore)
8338 return ret;
8340 return omit_one_operand_loc (loc, type, dest, ret);
8343 /* Fold function call to builtin memset. Return
8344 NULL_TREE if no simplification can be made. */
8346 static tree
8347 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8349 if (! validate_arg (dest, POINTER_TYPE)
8350 || ! validate_arg (size, INTEGER_TYPE))
8351 return NULL_TREE;
8353 if (!ignore)
8354 return NULL_TREE;
8356 /* New argument list transforming bzero(ptr x, int y) to
8357 memset(ptr x, int 0, size_t y). This is done this way
8358 so that if it isn't expanded inline, we fallback to
8359 calling bzero instead of memset. */
8361 return fold_builtin_memset (loc, dest, integer_zero_node,
8362 fold_convert_loc (loc, sizetype, size),
8363 void_type_node, ignore);
8366 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8367 NULL_TREE if no simplification can be made.
8368 If ENDP is 0, return DEST (like memcpy).
8369 If ENDP is 1, return DEST+LEN (like mempcpy).
8370 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8371 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8372 (memmove). */
8374 static tree
8375 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8376 tree len, tree type, bool ignore, int endp)
8378 tree destvar, srcvar, expr;
8380 if (! validate_arg (dest, POINTER_TYPE)
8381 || ! validate_arg (src, POINTER_TYPE)
8382 || ! validate_arg (len, INTEGER_TYPE))
8383 return NULL_TREE;
8385 /* If the LEN parameter is zero, return DEST. */
8386 if (integer_zerop (len))
8387 return omit_one_operand_loc (loc, type, dest, src);
8389 /* If SRC and DEST are the same (and not volatile), return
8390 DEST{,+LEN,+LEN-1}. */
8391 if (operand_equal_p (src, dest, 0))
8392 expr = len;
8393 else
8395 tree srctype, desttype;
8396 unsigned int src_align, dest_align;
8397 tree off0;
8399 if (endp == 3)
8401 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8402 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8404 /* Both DEST and SRC must be pointer types.
8405 ??? This is what old code did. Is the testing for pointer types
8406 really mandatory?
8408 If either SRC is readonly or length is 1, we can use memcpy. */
8409 if (!dest_align || !src_align)
8410 return NULL_TREE;
8411 if (readonly_data_expr (src)
8412 || (host_integerp (len, 1)
8413 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8414 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8416 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8417 if (!fn)
8418 return NULL_TREE;
8419 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8422 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8423 if (TREE_CODE (src) == ADDR_EXPR
8424 && TREE_CODE (dest) == ADDR_EXPR)
8426 tree src_base, dest_base, fn;
8427 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8428 HOST_WIDE_INT size = -1;
8429 HOST_WIDE_INT maxsize = -1;
8431 srcvar = TREE_OPERAND (src, 0);
8432 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8433 &size, &maxsize);
8434 destvar = TREE_OPERAND (dest, 0);
8435 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8436 &size, &maxsize);
8437 if (host_integerp (len, 1))
8438 maxsize = tree_low_cst (len, 1);
8439 else
8440 maxsize = -1;
8441 src_offset /= BITS_PER_UNIT;
8442 dest_offset /= BITS_PER_UNIT;
8443 if (SSA_VAR_P (src_base)
8444 && SSA_VAR_P (dest_base))
8446 if (operand_equal_p (src_base, dest_base, 0)
8447 && ranges_overlap_p (src_offset, maxsize,
8448 dest_offset, maxsize))
8449 return NULL_TREE;
8451 else if (TREE_CODE (src_base) == MEM_REF
8452 && TREE_CODE (dest_base) == MEM_REF)
8454 double_int off;
8455 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8456 TREE_OPERAND (dest_base, 0), 0))
8457 return NULL_TREE;
8458 off = double_int_add (mem_ref_offset (src_base),
8459 shwi_to_double_int (src_offset));
8460 if (!double_int_fits_in_shwi_p (off))
8461 return NULL_TREE;
8462 src_offset = off.low;
8463 off = double_int_add (mem_ref_offset (dest_base),
8464 shwi_to_double_int (dest_offset));
8465 if (!double_int_fits_in_shwi_p (off))
8466 return NULL_TREE;
8467 dest_offset = off.low;
8468 if (ranges_overlap_p (src_offset, maxsize,
8469 dest_offset, maxsize))
8470 return NULL_TREE;
8472 else
8473 return NULL_TREE;
8475 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8476 if (!fn)
8477 return NULL_TREE;
8478 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8481 /* If the destination and source do not alias optimize into
8482 memcpy as well. */
8483 if ((is_gimple_min_invariant (dest)
8484 || TREE_CODE (dest) == SSA_NAME)
8485 && (is_gimple_min_invariant (src)
8486 || TREE_CODE (src) == SSA_NAME))
8488 ao_ref destr, srcr;
8489 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8490 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8491 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8493 tree fn;
8494 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8495 if (!fn)
8496 return NULL_TREE;
8497 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8501 return NULL_TREE;
8504 if (!host_integerp (len, 0))
8505 return NULL_TREE;
8506 /* FIXME:
8507 This logic lose for arguments like (type *)malloc (sizeof (type)),
8508 since we strip the casts of up to VOID return value from malloc.
8509 Perhaps we ought to inherit type from non-VOID argument here? */
8510 STRIP_NOPS (src);
8511 STRIP_NOPS (dest);
8512 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8513 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8515 tree tem = TREE_OPERAND (src, 0);
8516 STRIP_NOPS (tem);
8517 if (tem != TREE_OPERAND (src, 0))
8518 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8520 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8522 tree tem = TREE_OPERAND (dest, 0);
8523 STRIP_NOPS (tem);
8524 if (tem != TREE_OPERAND (dest, 0))
8525 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8527 srctype = TREE_TYPE (TREE_TYPE (src));
8528 if (srctype
8529 && TREE_CODE (srctype) == ARRAY_TYPE
8530 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8532 srctype = TREE_TYPE (srctype);
8533 STRIP_NOPS (src);
8534 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8536 desttype = TREE_TYPE (TREE_TYPE (dest));
8537 if (desttype
8538 && TREE_CODE (desttype) == ARRAY_TYPE
8539 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8541 desttype = TREE_TYPE (desttype);
8542 STRIP_NOPS (dest);
8543 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8545 if (!srctype || !desttype
8546 || TREE_ADDRESSABLE (srctype)
8547 || TREE_ADDRESSABLE (desttype)
8548 || !TYPE_SIZE_UNIT (srctype)
8549 || !TYPE_SIZE_UNIT (desttype)
8550 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8551 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8552 return NULL_TREE;
8554 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8555 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8556 if (dest_align < TYPE_ALIGN (desttype)
8557 || src_align < TYPE_ALIGN (srctype))
8558 return NULL_TREE;
8560 if (!ignore)
8561 dest = builtin_save_expr (dest);
8563 /* Build accesses at offset zero with a ref-all character type. */
8564 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8565 ptr_mode, true), 0);
8567 destvar = dest;
8568 STRIP_NOPS (destvar);
8569 if (TREE_CODE (destvar) == ADDR_EXPR
8570 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8571 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8572 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8573 else
8574 destvar = NULL_TREE;
8576 srcvar = src;
8577 STRIP_NOPS (srcvar);
8578 if (TREE_CODE (srcvar) == ADDR_EXPR
8579 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8580 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8582 if (!destvar
8583 || src_align >= TYPE_ALIGN (desttype))
8584 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8585 srcvar, off0);
8586 else if (!STRICT_ALIGNMENT)
8588 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8589 src_align);
8590 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8592 else
8593 srcvar = NULL_TREE;
8595 else
8596 srcvar = NULL_TREE;
8598 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8599 return NULL_TREE;
8601 if (srcvar == NULL_TREE)
8603 STRIP_NOPS (src);
8604 if (src_align >= TYPE_ALIGN (desttype))
8605 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8606 else
8608 if (STRICT_ALIGNMENT)
8609 return NULL_TREE;
8610 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8611 src_align);
8612 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8615 else if (destvar == NULL_TREE)
8617 STRIP_NOPS (dest);
8618 if (dest_align >= TYPE_ALIGN (srctype))
8619 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8620 else
8622 if (STRICT_ALIGNMENT)
8623 return NULL_TREE;
8624 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8625 dest_align);
8626 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8630 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8633 if (ignore)
8634 return expr;
8636 if (endp == 0 || endp == 3)
8637 return omit_one_operand_loc (loc, type, dest, expr);
8639 if (expr == len)
8640 expr = NULL_TREE;
8642 if (endp == 2)
8643 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8644 ssize_int (1));
8646 len = fold_convert_loc (loc, sizetype, len);
8647 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8648 dest = fold_convert_loc (loc, type, dest);
8649 if (expr)
8650 dest = omit_one_operand_loc (loc, type, dest, expr);
8651 return dest;
8654 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8655 If LEN is not NULL, it represents the length of the string to be
8656 copied. Return NULL_TREE if no simplification can be made. */
8658 tree
8659 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8661 tree fn;
8663 if (!validate_arg (dest, POINTER_TYPE)
8664 || !validate_arg (src, POINTER_TYPE))
8665 return NULL_TREE;
8667 /* If SRC and DEST are the same (and not volatile), return DEST. */
8668 if (operand_equal_p (src, dest, 0))
8669 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8671 if (optimize_function_for_size_p (cfun))
8672 return NULL_TREE;
8674 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8675 if (!fn)
8676 return NULL_TREE;
8678 if (!len)
8680 len = c_strlen (src, 1);
8681 if (! len || TREE_SIDE_EFFECTS (len))
8682 return NULL_TREE;
8685 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8686 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8687 build_call_expr_loc (loc, fn, 3, dest, src, len));
8690 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8691 Return NULL_TREE if no simplification can be made. */
8693 static tree
8694 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8696 tree fn, len, lenp1, call, type;
8698 if (!validate_arg (dest, POINTER_TYPE)
8699 || !validate_arg (src, POINTER_TYPE))
8700 return NULL_TREE;
8702 len = c_strlen (src, 1);
8703 if (!len
8704 || TREE_CODE (len) != INTEGER_CST)
8705 return NULL_TREE;
8707 if (optimize_function_for_size_p (cfun)
8708 /* If length is zero it's small enough. */
8709 && !integer_zerop (len))
8710 return NULL_TREE;
8712 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8713 if (!fn)
8714 return NULL_TREE;
8716 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8717 /* We use dest twice in building our expression. Save it from
8718 multiple expansions. */
8719 dest = builtin_save_expr (dest);
8720 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8722 type = TREE_TYPE (TREE_TYPE (fndecl));
8723 len = fold_convert_loc (loc, sizetype, len);
8724 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8725 dest = fold_convert_loc (loc, type, dest);
8726 dest = omit_one_operand_loc (loc, type, dest, call);
8727 return dest;
8730 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8731 If SLEN is not NULL, it represents the length of the source string.
8732 Return NULL_TREE if no simplification can be made. */
8734 tree
8735 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8736 tree src, tree len, tree slen)
8738 tree fn;
8740 if (!validate_arg (dest, POINTER_TYPE)
8741 || !validate_arg (src, POINTER_TYPE)
8742 || !validate_arg (len, INTEGER_TYPE))
8743 return NULL_TREE;
8745 /* If the LEN parameter is zero, return DEST. */
8746 if (integer_zerop (len))
8747 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8749 /* We can't compare slen with len as constants below if len is not a
8750 constant. */
8751 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8752 return NULL_TREE;
8754 if (!slen)
8755 slen = c_strlen (src, 1);
8757 /* Now, we must be passed a constant src ptr parameter. */
8758 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8759 return NULL_TREE;
8761 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8763 /* We do not support simplification of this case, though we do
8764 support it when expanding trees into RTL. */
8765 /* FIXME: generate a call to __builtin_memset. */
8766 if (tree_int_cst_lt (slen, len))
8767 return NULL_TREE;
8769 /* OK transform into builtin memcpy. */
8770 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8771 if (!fn)
8772 return NULL_TREE;
8773 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8774 build_call_expr_loc (loc, fn, 3, dest, src, len));
8777 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8778 arguments to the call, and TYPE is its return type.
8779 Return NULL_TREE if no simplification can be made. */
8781 static tree
8782 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8784 if (!validate_arg (arg1, POINTER_TYPE)
8785 || !validate_arg (arg2, INTEGER_TYPE)
8786 || !validate_arg (len, INTEGER_TYPE))
8787 return NULL_TREE;
8788 else
8790 const char *p1;
8792 if (TREE_CODE (arg2) != INTEGER_CST
8793 || !host_integerp (len, 1))
8794 return NULL_TREE;
8796 p1 = c_getstr (arg1);
8797 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8799 char c;
8800 const char *r;
8801 tree tem;
8803 if (target_char_cast (arg2, &c))
8804 return NULL_TREE;
8806 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8808 if (r == NULL)
8809 return build_int_cst (TREE_TYPE (arg1), 0);
8811 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8812 size_int (r - p1));
8813 return fold_convert_loc (loc, type, tem);
8815 return NULL_TREE;
8819 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8820 Return NULL_TREE if no simplification can be made. */
8822 static tree
8823 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8825 const char *p1, *p2;
8827 if (!validate_arg (arg1, POINTER_TYPE)
8828 || !validate_arg (arg2, POINTER_TYPE)
8829 || !validate_arg (len, INTEGER_TYPE))
8830 return NULL_TREE;
8832 /* If the LEN parameter is zero, return zero. */
8833 if (integer_zerop (len))
8834 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8835 arg1, arg2);
8837 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8838 if (operand_equal_p (arg1, arg2, 0))
8839 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8841 p1 = c_getstr (arg1);
8842 p2 = c_getstr (arg2);
8844 /* If all arguments are constant, and the value of len is not greater
8845 than the lengths of arg1 and arg2, evaluate at compile-time. */
8846 if (host_integerp (len, 1) && p1 && p2
8847 && compare_tree_int (len, strlen (p1) + 1) <= 0
8848 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8850 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8852 if (r > 0)
8853 return integer_one_node;
8854 else if (r < 0)
8855 return integer_minus_one_node;
8856 else
8857 return integer_zero_node;
8860 /* If len parameter is one, return an expression corresponding to
8861 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8862 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8864 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8865 tree cst_uchar_ptr_node
8866 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8868 tree ind1
8869 = fold_convert_loc (loc, integer_type_node,
8870 build1 (INDIRECT_REF, cst_uchar_node,
8871 fold_convert_loc (loc,
8872 cst_uchar_ptr_node,
8873 arg1)));
8874 tree ind2
8875 = fold_convert_loc (loc, integer_type_node,
8876 build1 (INDIRECT_REF, cst_uchar_node,
8877 fold_convert_loc (loc,
8878 cst_uchar_ptr_node,
8879 arg2)));
8880 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8883 return NULL_TREE;
8886 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8887 Return NULL_TREE if no simplification can be made. */
8889 static tree
8890 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8892 const char *p1, *p2;
8894 if (!validate_arg (arg1, POINTER_TYPE)
8895 || !validate_arg (arg2, POINTER_TYPE))
8896 return NULL_TREE;
8898 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8899 if (operand_equal_p (arg1, arg2, 0))
8900 return integer_zero_node;
8902 p1 = c_getstr (arg1);
8903 p2 = c_getstr (arg2);
8905 if (p1 && p2)
8907 const int i = strcmp (p1, p2);
8908 if (i < 0)
8909 return integer_minus_one_node;
8910 else if (i > 0)
8911 return integer_one_node;
8912 else
8913 return integer_zero_node;
8916 /* If the second arg is "", return *(const unsigned char*)arg1. */
8917 if (p2 && *p2 == '\0')
8919 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8920 tree cst_uchar_ptr_node
8921 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8923 return fold_convert_loc (loc, integer_type_node,
8924 build1 (INDIRECT_REF, cst_uchar_node,
8925 fold_convert_loc (loc,
8926 cst_uchar_ptr_node,
8927 arg1)));
8930 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8931 if (p1 && *p1 == '\0')
8933 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8934 tree cst_uchar_ptr_node
8935 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8937 tree temp
8938 = fold_convert_loc (loc, integer_type_node,
8939 build1 (INDIRECT_REF, cst_uchar_node,
8940 fold_convert_loc (loc,
8941 cst_uchar_ptr_node,
8942 arg2)));
8943 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8946 return NULL_TREE;
8949 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8950 Return NULL_TREE if no simplification can be made. */
8952 static tree
8953 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8955 const char *p1, *p2;
8957 if (!validate_arg (arg1, POINTER_TYPE)
8958 || !validate_arg (arg2, POINTER_TYPE)
8959 || !validate_arg (len, INTEGER_TYPE))
8960 return NULL_TREE;
8962 /* If the LEN parameter is zero, return zero. */
8963 if (integer_zerop (len))
8964 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8965 arg1, arg2);
8967 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8968 if (operand_equal_p (arg1, arg2, 0))
8969 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8971 p1 = c_getstr (arg1);
8972 p2 = c_getstr (arg2);
8974 if (host_integerp (len, 1) && p1 && p2)
8976 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8977 if (i > 0)
8978 return integer_one_node;
8979 else if (i < 0)
8980 return integer_minus_one_node;
8981 else
8982 return integer_zero_node;
8985 /* If the second arg is "", and the length is greater than zero,
8986 return *(const unsigned char*)arg1. */
8987 if (p2 && *p2 == '\0'
8988 && TREE_CODE (len) == INTEGER_CST
8989 && tree_int_cst_sgn (len) == 1)
8991 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8992 tree cst_uchar_ptr_node
8993 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8995 return fold_convert_loc (loc, integer_type_node,
8996 build1 (INDIRECT_REF, cst_uchar_node,
8997 fold_convert_loc (loc,
8998 cst_uchar_ptr_node,
8999 arg1)));
9002 /* If the first arg is "", and the length is greater than zero,
9003 return -*(const unsigned char*)arg2. */
9004 if (p1 && *p1 == '\0'
9005 && TREE_CODE (len) == INTEGER_CST
9006 && tree_int_cst_sgn (len) == 1)
9008 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9009 tree cst_uchar_ptr_node
9010 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9012 tree temp = fold_convert_loc (loc, integer_type_node,
9013 build1 (INDIRECT_REF, cst_uchar_node,
9014 fold_convert_loc (loc,
9015 cst_uchar_ptr_node,
9016 arg2)));
9017 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9020 /* If len parameter is one, return an expression corresponding to
9021 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9022 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 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 tree ind1 = 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)));
9033 tree ind2 = fold_convert_loc (loc, integer_type_node,
9034 build1 (INDIRECT_REF, cst_uchar_node,
9035 fold_convert_loc (loc,
9036 cst_uchar_ptr_node,
9037 arg2)));
9038 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9041 return NULL_TREE;
9044 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9045 ARG. Return NULL_TREE if no simplification can be made. */
9047 static tree
9048 fold_builtin_signbit (location_t loc, tree arg, tree type)
9050 if (!validate_arg (arg, REAL_TYPE))
9051 return NULL_TREE;
9053 /* If ARG is a compile-time constant, determine the result. */
9054 if (TREE_CODE (arg) == REAL_CST
9055 && !TREE_OVERFLOW (arg))
9057 REAL_VALUE_TYPE c;
9059 c = TREE_REAL_CST (arg);
9060 return (REAL_VALUE_NEGATIVE (c)
9061 ? build_one_cst (type)
9062 : build_zero_cst (type));
9065 /* If ARG is non-negative, the result is always zero. */
9066 if (tree_expr_nonnegative_p (arg))
9067 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9069 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9070 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9071 return fold_build2_loc (loc, LT_EXPR, type, arg,
9072 build_real (TREE_TYPE (arg), dconst0));
9074 return NULL_TREE;
9077 /* Fold function call to builtin copysign, copysignf or copysignl with
9078 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9079 be made. */
9081 static tree
9082 fold_builtin_copysign (location_t loc, tree fndecl,
9083 tree arg1, tree arg2, tree type)
9085 tree tem;
9087 if (!validate_arg (arg1, REAL_TYPE)
9088 || !validate_arg (arg2, REAL_TYPE))
9089 return NULL_TREE;
9091 /* copysign(X,X) is X. */
9092 if (operand_equal_p (arg1, arg2, 0))
9093 return fold_convert_loc (loc, type, arg1);
9095 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9096 if (TREE_CODE (arg1) == REAL_CST
9097 && TREE_CODE (arg2) == REAL_CST
9098 && !TREE_OVERFLOW (arg1)
9099 && !TREE_OVERFLOW (arg2))
9101 REAL_VALUE_TYPE c1, c2;
9103 c1 = TREE_REAL_CST (arg1);
9104 c2 = TREE_REAL_CST (arg2);
9105 /* c1.sign := c2.sign. */
9106 real_copysign (&c1, &c2);
9107 return build_real (type, c1);
9110 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9111 Remember to evaluate Y for side-effects. */
9112 if (tree_expr_nonnegative_p (arg2))
9113 return omit_one_operand_loc (loc, type,
9114 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9115 arg2);
9117 /* Strip sign changing operations for the first argument. */
9118 tem = fold_strip_sign_ops (arg1);
9119 if (tem)
9120 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9122 return NULL_TREE;
9125 /* Fold a call to builtin isascii with argument ARG. */
9127 static tree
9128 fold_builtin_isascii (location_t loc, tree arg)
9130 if (!validate_arg (arg, INTEGER_TYPE))
9131 return NULL_TREE;
9132 else
9134 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9135 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9136 build_int_cst (integer_type_node,
9137 ~ (unsigned HOST_WIDE_INT) 0x7f));
9138 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9139 arg, integer_zero_node);
9143 /* Fold a call to builtin toascii with argument ARG. */
9145 static tree
9146 fold_builtin_toascii (location_t loc, tree arg)
9148 if (!validate_arg (arg, INTEGER_TYPE))
9149 return NULL_TREE;
9151 /* Transform toascii(c) -> (c & 0x7f). */
9152 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9153 build_int_cst (integer_type_node, 0x7f));
9156 /* Fold a call to builtin isdigit with argument ARG. */
9158 static tree
9159 fold_builtin_isdigit (location_t loc, tree arg)
9161 if (!validate_arg (arg, INTEGER_TYPE))
9162 return NULL_TREE;
9163 else
9165 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9166 /* According to the C standard, isdigit is unaffected by locale.
9167 However, it definitely is affected by the target character set. */
9168 unsigned HOST_WIDE_INT target_digit0
9169 = lang_hooks.to_target_charset ('0');
9171 if (target_digit0 == 0)
9172 return NULL_TREE;
9174 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9175 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9176 build_int_cst (unsigned_type_node, target_digit0));
9177 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9178 build_int_cst (unsigned_type_node, 9));
9182 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9184 static tree
9185 fold_builtin_fabs (location_t loc, tree arg, tree type)
9187 if (!validate_arg (arg, REAL_TYPE))
9188 return NULL_TREE;
9190 arg = fold_convert_loc (loc, type, arg);
9191 if (TREE_CODE (arg) == REAL_CST)
9192 return fold_abs_const (arg, type);
9193 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9196 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9198 static tree
9199 fold_builtin_abs (location_t loc, tree arg, tree type)
9201 if (!validate_arg (arg, INTEGER_TYPE))
9202 return NULL_TREE;
9204 arg = fold_convert_loc (loc, type, arg);
9205 if (TREE_CODE (arg) == INTEGER_CST)
9206 return fold_abs_const (arg, type);
9207 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9210 /* Fold a fma operation with arguments ARG[012]. */
9212 tree
9213 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9214 tree type, tree arg0, tree arg1, tree arg2)
9216 if (TREE_CODE (arg0) == REAL_CST
9217 && TREE_CODE (arg1) == REAL_CST
9218 && TREE_CODE (arg2) == REAL_CST)
9219 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9221 return NULL_TREE;
9224 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9226 static tree
9227 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9229 if (validate_arg (arg0, REAL_TYPE)
9230 && validate_arg(arg1, REAL_TYPE)
9231 && validate_arg(arg2, REAL_TYPE))
9233 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9234 if (tem)
9235 return tem;
9237 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9238 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9239 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9241 return NULL_TREE;
9244 /* Fold a call to builtin fmin or fmax. */
9246 static tree
9247 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9248 tree type, bool max)
9250 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9252 /* Calculate the result when the argument is a constant. */
9253 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9255 if (res)
9256 return res;
9258 /* If either argument is NaN, return the other one. Avoid the
9259 transformation if we get (and honor) a signalling NaN. Using
9260 omit_one_operand() ensures we create a non-lvalue. */
9261 if (TREE_CODE (arg0) == REAL_CST
9262 && real_isnan (&TREE_REAL_CST (arg0))
9263 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9264 || ! TREE_REAL_CST (arg0).signalling))
9265 return omit_one_operand_loc (loc, type, arg1, arg0);
9266 if (TREE_CODE (arg1) == REAL_CST
9267 && real_isnan (&TREE_REAL_CST (arg1))
9268 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9269 || ! TREE_REAL_CST (arg1).signalling))
9270 return omit_one_operand_loc (loc, type, arg0, arg1);
9272 /* Transform fmin/fmax(x,x) -> x. */
9273 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9274 return omit_one_operand_loc (loc, type, arg0, arg1);
9276 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9277 functions to return the numeric arg if the other one is NaN.
9278 These tree codes don't honor that, so only transform if
9279 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9280 handled, so we don't have to worry about it either. */
9281 if (flag_finite_math_only)
9282 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9283 fold_convert_loc (loc, type, arg0),
9284 fold_convert_loc (loc, type, arg1));
9286 return NULL_TREE;
9289 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9291 static tree
9292 fold_builtin_carg (location_t loc, tree arg, tree type)
9294 if (validate_arg (arg, COMPLEX_TYPE)
9295 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9297 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9299 if (atan2_fn)
9301 tree new_arg = builtin_save_expr (arg);
9302 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9303 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9304 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9308 return NULL_TREE;
9311 /* Fold a call to builtin logb/ilogb. */
9313 static tree
9314 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9316 if (! validate_arg (arg, REAL_TYPE))
9317 return NULL_TREE;
9319 STRIP_NOPS (arg);
9321 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9323 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9325 switch (value->cl)
9327 case rvc_nan:
9328 case rvc_inf:
9329 /* If arg is Inf or NaN and we're logb, return it. */
9330 if (TREE_CODE (rettype) == REAL_TYPE)
9331 return fold_convert_loc (loc, rettype, arg);
9332 /* Fall through... */
9333 case rvc_zero:
9334 /* Zero may set errno and/or raise an exception for logb, also
9335 for ilogb we don't know FP_ILOGB0. */
9336 return NULL_TREE;
9337 case rvc_normal:
9338 /* For normal numbers, proceed iff radix == 2. In GCC,
9339 normalized significands are in the range [0.5, 1.0). We
9340 want the exponent as if they were [1.0, 2.0) so get the
9341 exponent and subtract 1. */
9342 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9343 return fold_convert_loc (loc, rettype,
9344 build_int_cst (integer_type_node,
9345 REAL_EXP (value)-1));
9346 break;
9350 return NULL_TREE;
9353 /* Fold a call to builtin significand, if radix == 2. */
9355 static tree
9356 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9358 if (! validate_arg (arg, REAL_TYPE))
9359 return NULL_TREE;
9361 STRIP_NOPS (arg);
9363 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9365 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9367 switch (value->cl)
9369 case rvc_zero:
9370 case rvc_nan:
9371 case rvc_inf:
9372 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9373 return fold_convert_loc (loc, rettype, arg);
9374 case rvc_normal:
9375 /* For normal numbers, proceed iff radix == 2. */
9376 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9378 REAL_VALUE_TYPE result = *value;
9379 /* In GCC, normalized significands are in the range [0.5,
9380 1.0). We want them to be [1.0, 2.0) so set the
9381 exponent to 1. */
9382 SET_REAL_EXP (&result, 1);
9383 return build_real (rettype, result);
9385 break;
9389 return NULL_TREE;
9392 /* Fold a call to builtin frexp, we can assume the base is 2. */
9394 static tree
9395 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9397 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9398 return NULL_TREE;
9400 STRIP_NOPS (arg0);
9402 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9403 return NULL_TREE;
9405 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9407 /* Proceed if a valid pointer type was passed in. */
9408 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9410 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9411 tree frac, exp;
9413 switch (value->cl)
9415 case rvc_zero:
9416 /* For +-0, return (*exp = 0, +-0). */
9417 exp = integer_zero_node;
9418 frac = arg0;
9419 break;
9420 case rvc_nan:
9421 case rvc_inf:
9422 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9423 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9424 case rvc_normal:
9426 /* Since the frexp function always expects base 2, and in
9427 GCC normalized significands are already in the range
9428 [0.5, 1.0), we have exactly what frexp wants. */
9429 REAL_VALUE_TYPE frac_rvt = *value;
9430 SET_REAL_EXP (&frac_rvt, 0);
9431 frac = build_real (rettype, frac_rvt);
9432 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9434 break;
9435 default:
9436 gcc_unreachable ();
9439 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9440 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9441 TREE_SIDE_EFFECTS (arg1) = 1;
9442 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9445 return NULL_TREE;
9448 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9449 then we can assume the base is two. If it's false, then we have to
9450 check the mode of the TYPE parameter in certain cases. */
9452 static tree
9453 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9454 tree type, bool ldexp)
9456 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9458 STRIP_NOPS (arg0);
9459 STRIP_NOPS (arg1);
9461 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9462 if (real_zerop (arg0) || integer_zerop (arg1)
9463 || (TREE_CODE (arg0) == REAL_CST
9464 && !real_isfinite (&TREE_REAL_CST (arg0))))
9465 return omit_one_operand_loc (loc, type, arg0, arg1);
9467 /* If both arguments are constant, then try to evaluate it. */
9468 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9469 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9470 && host_integerp (arg1, 0))
9472 /* Bound the maximum adjustment to twice the range of the
9473 mode's valid exponents. Use abs to ensure the range is
9474 positive as a sanity check. */
9475 const long max_exp_adj = 2 *
9476 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9477 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9479 /* Get the user-requested adjustment. */
9480 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9482 /* The requested adjustment must be inside this range. This
9483 is a preliminary cap to avoid things like overflow, we
9484 may still fail to compute the result for other reasons. */
9485 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9487 REAL_VALUE_TYPE initial_result;
9489 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9491 /* Ensure we didn't overflow. */
9492 if (! real_isinf (&initial_result))
9494 const REAL_VALUE_TYPE trunc_result
9495 = real_value_truncate (TYPE_MODE (type), initial_result);
9497 /* Only proceed if the target mode can hold the
9498 resulting value. */
9499 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9500 return build_real (type, trunc_result);
9506 return NULL_TREE;
9509 /* Fold a call to builtin modf. */
9511 static tree
9512 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9514 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9515 return NULL_TREE;
9517 STRIP_NOPS (arg0);
9519 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9520 return NULL_TREE;
9522 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9524 /* Proceed if a valid pointer type was passed in. */
9525 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9527 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9528 REAL_VALUE_TYPE trunc, frac;
9530 switch (value->cl)
9532 case rvc_nan:
9533 case rvc_zero:
9534 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9535 trunc = frac = *value;
9536 break;
9537 case rvc_inf:
9538 /* For +-Inf, return (*arg1 = arg0, +-0). */
9539 frac = dconst0;
9540 frac.sign = value->sign;
9541 trunc = *value;
9542 break;
9543 case rvc_normal:
9544 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9545 real_trunc (&trunc, VOIDmode, value);
9546 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9547 /* If the original number was negative and already
9548 integral, then the fractional part is -0.0. */
9549 if (value->sign && frac.cl == rvc_zero)
9550 frac.sign = value->sign;
9551 break;
9554 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9555 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9556 build_real (rettype, trunc));
9557 TREE_SIDE_EFFECTS (arg1) = 1;
9558 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9559 build_real (rettype, frac));
9562 return NULL_TREE;
9565 /* Given a location LOC, an interclass builtin function decl FNDECL
9566 and its single argument ARG, return an folded expression computing
9567 the same, or NULL_TREE if we either couldn't or didn't want to fold
9568 (the latter happen if there's an RTL instruction available). */
9570 static tree
9571 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9573 enum machine_mode mode;
9575 if (!validate_arg (arg, REAL_TYPE))
9576 return NULL_TREE;
9578 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9579 return NULL_TREE;
9581 mode = TYPE_MODE (TREE_TYPE (arg));
9583 /* If there is no optab, try generic code. */
9584 switch (DECL_FUNCTION_CODE (fndecl))
9586 tree result;
9588 CASE_FLT_FN (BUILT_IN_ISINF):
9590 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9591 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9592 tree const type = TREE_TYPE (arg);
9593 REAL_VALUE_TYPE r;
9594 char buf[128];
9596 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9597 real_from_string (&r, buf);
9598 result = build_call_expr (isgr_fn, 2,
9599 fold_build1_loc (loc, ABS_EXPR, type, arg),
9600 build_real (type, r));
9601 return result;
9603 CASE_FLT_FN (BUILT_IN_FINITE):
9604 case BUILT_IN_ISFINITE:
9606 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9607 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9608 tree const type = TREE_TYPE (arg);
9609 REAL_VALUE_TYPE r;
9610 char buf[128];
9612 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9613 real_from_string (&r, buf);
9614 result = build_call_expr (isle_fn, 2,
9615 fold_build1_loc (loc, ABS_EXPR, type, arg),
9616 build_real (type, r));
9617 /*result = fold_build2_loc (loc, UNGT_EXPR,
9618 TREE_TYPE (TREE_TYPE (fndecl)),
9619 fold_build1_loc (loc, ABS_EXPR, type, arg),
9620 build_real (type, r));
9621 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9622 TREE_TYPE (TREE_TYPE (fndecl)),
9623 result);*/
9624 return result;
9626 case BUILT_IN_ISNORMAL:
9628 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9629 islessequal(fabs(x),DBL_MAX). */
9630 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9631 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9632 tree const type = TREE_TYPE (arg);
9633 REAL_VALUE_TYPE rmax, rmin;
9634 char buf[128];
9636 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9637 real_from_string (&rmax, buf);
9638 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9639 real_from_string (&rmin, buf);
9640 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9641 result = build_call_expr (isle_fn, 2, arg,
9642 build_real (type, rmax));
9643 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9644 build_call_expr (isge_fn, 2, arg,
9645 build_real (type, rmin)));
9646 return result;
9648 default:
9649 break;
9652 return NULL_TREE;
9655 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9656 ARG is the argument for the call. */
9658 static tree
9659 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9661 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9662 REAL_VALUE_TYPE r;
9664 if (!validate_arg (arg, REAL_TYPE))
9665 return NULL_TREE;
9667 switch (builtin_index)
9669 case BUILT_IN_ISINF:
9670 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9671 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9673 if (TREE_CODE (arg) == REAL_CST)
9675 r = TREE_REAL_CST (arg);
9676 if (real_isinf (&r))
9677 return real_compare (GT_EXPR, &r, &dconst0)
9678 ? integer_one_node : integer_minus_one_node;
9679 else
9680 return integer_zero_node;
9683 return NULL_TREE;
9685 case BUILT_IN_ISINF_SIGN:
9687 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9688 /* In a boolean context, GCC will fold the inner COND_EXPR to
9689 1. So e.g. "if (isinf_sign(x))" would be folded to just
9690 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9691 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9692 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9693 tree tmp = NULL_TREE;
9695 arg = builtin_save_expr (arg);
9697 if (signbit_fn && isinf_fn)
9699 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9700 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9702 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9703 signbit_call, integer_zero_node);
9704 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9705 isinf_call, integer_zero_node);
9707 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9708 integer_minus_one_node, integer_one_node);
9709 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9710 isinf_call, tmp,
9711 integer_zero_node);
9714 return tmp;
9717 case BUILT_IN_ISFINITE:
9718 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9719 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9720 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9722 if (TREE_CODE (arg) == REAL_CST)
9724 r = TREE_REAL_CST (arg);
9725 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9728 return NULL_TREE;
9730 case BUILT_IN_ISNAN:
9731 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9732 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9734 if (TREE_CODE (arg) == REAL_CST)
9736 r = TREE_REAL_CST (arg);
9737 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9740 arg = builtin_save_expr (arg);
9741 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9743 default:
9744 gcc_unreachable ();
9748 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9749 This builtin will generate code to return the appropriate floating
9750 point classification depending on the value of the floating point
9751 number passed in. The possible return values must be supplied as
9752 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9753 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9754 one floating point argument which is "type generic". */
9756 static tree
9757 fold_builtin_fpclassify (location_t loc, tree exp)
9759 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9760 arg, type, res, tmp;
9761 enum machine_mode mode;
9762 REAL_VALUE_TYPE r;
9763 char buf[128];
9765 /* Verify the required arguments in the original call. */
9766 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9767 INTEGER_TYPE, INTEGER_TYPE,
9768 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9769 return NULL_TREE;
9771 fp_nan = CALL_EXPR_ARG (exp, 0);
9772 fp_infinite = CALL_EXPR_ARG (exp, 1);
9773 fp_normal = CALL_EXPR_ARG (exp, 2);
9774 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9775 fp_zero = CALL_EXPR_ARG (exp, 4);
9776 arg = CALL_EXPR_ARG (exp, 5);
9777 type = TREE_TYPE (arg);
9778 mode = TYPE_MODE (type);
9779 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9781 /* fpclassify(x) ->
9782 isnan(x) ? FP_NAN :
9783 (fabs(x) == Inf ? FP_INFINITE :
9784 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9785 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9787 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9788 build_real (type, dconst0));
9789 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9790 tmp, fp_zero, fp_subnormal);
9792 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9793 real_from_string (&r, buf);
9794 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9795 arg, build_real (type, r));
9796 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9798 if (HONOR_INFINITIES (mode))
9800 real_inf (&r);
9801 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9802 build_real (type, r));
9803 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9804 fp_infinite, res);
9807 if (HONOR_NANS (mode))
9809 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9810 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9813 return res;
9816 /* Fold a call to an unordered comparison function such as
9817 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9818 being called and ARG0 and ARG1 are the arguments for the call.
9819 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9820 the opposite of the desired result. UNORDERED_CODE is used
9821 for modes that can hold NaNs and ORDERED_CODE is used for
9822 the rest. */
9824 static tree
9825 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9826 enum tree_code unordered_code,
9827 enum tree_code ordered_code)
9829 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9830 enum tree_code code;
9831 tree type0, type1;
9832 enum tree_code code0, code1;
9833 tree cmp_type = NULL_TREE;
9835 type0 = TREE_TYPE (arg0);
9836 type1 = TREE_TYPE (arg1);
9838 code0 = TREE_CODE (type0);
9839 code1 = TREE_CODE (type1);
9841 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9842 /* Choose the wider of two real types. */
9843 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9844 ? type0 : type1;
9845 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9846 cmp_type = type0;
9847 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9848 cmp_type = type1;
9850 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9851 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9853 if (unordered_code == UNORDERED_EXPR)
9855 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9856 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9857 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9860 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9861 : ordered_code;
9862 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9863 fold_build2_loc (loc, code, type, arg0, arg1));
9866 /* Fold a call to built-in function FNDECL with 0 arguments.
9867 IGNORE is true if the result of the function call is ignored. This
9868 function returns NULL_TREE if no simplification was possible. */
9870 static tree
9871 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9873 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9874 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9875 switch (fcode)
9877 CASE_FLT_FN (BUILT_IN_INF):
9878 case BUILT_IN_INFD32:
9879 case BUILT_IN_INFD64:
9880 case BUILT_IN_INFD128:
9881 return fold_builtin_inf (loc, type, true);
9883 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9884 return fold_builtin_inf (loc, type, false);
9886 case BUILT_IN_CLASSIFY_TYPE:
9887 return fold_builtin_classify_type (NULL_TREE);
9889 default:
9890 break;
9892 return NULL_TREE;
9895 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9896 IGNORE is true if the result of the function call is ignored. This
9897 function returns NULL_TREE if no simplification was possible. */
9899 static tree
9900 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9902 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9903 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9904 switch (fcode)
9906 case BUILT_IN_CONSTANT_P:
9908 tree val = fold_builtin_constant_p (arg0);
9910 /* Gimplification will pull the CALL_EXPR for the builtin out of
9911 an if condition. When not optimizing, we'll not CSE it back.
9912 To avoid link error types of regressions, return false now. */
9913 if (!val && !optimize)
9914 val = integer_zero_node;
9916 return val;
9919 case BUILT_IN_CLASSIFY_TYPE:
9920 return fold_builtin_classify_type (arg0);
9922 case BUILT_IN_STRLEN:
9923 return fold_builtin_strlen (loc, type, arg0);
9925 CASE_FLT_FN (BUILT_IN_FABS):
9926 return fold_builtin_fabs (loc, arg0, type);
9928 case BUILT_IN_ABS:
9929 case BUILT_IN_LABS:
9930 case BUILT_IN_LLABS:
9931 case BUILT_IN_IMAXABS:
9932 return fold_builtin_abs (loc, arg0, type);
9934 CASE_FLT_FN (BUILT_IN_CONJ):
9935 if (validate_arg (arg0, COMPLEX_TYPE)
9936 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9937 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9938 break;
9940 CASE_FLT_FN (BUILT_IN_CREAL):
9941 if (validate_arg (arg0, COMPLEX_TYPE)
9942 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9943 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9944 break;
9946 CASE_FLT_FN (BUILT_IN_CIMAG):
9947 if (validate_arg (arg0, COMPLEX_TYPE)
9948 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9949 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9950 break;
9952 CASE_FLT_FN (BUILT_IN_CCOS):
9953 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9955 CASE_FLT_FN (BUILT_IN_CCOSH):
9956 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9958 CASE_FLT_FN (BUILT_IN_CPROJ):
9959 return fold_builtin_cproj(loc, arg0, type);
9961 CASE_FLT_FN (BUILT_IN_CSIN):
9962 if (validate_arg (arg0, COMPLEX_TYPE)
9963 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9964 return do_mpc_arg1 (arg0, type, mpc_sin);
9965 break;
9967 CASE_FLT_FN (BUILT_IN_CSINH):
9968 if (validate_arg (arg0, COMPLEX_TYPE)
9969 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9970 return do_mpc_arg1 (arg0, type, mpc_sinh);
9971 break;
9973 CASE_FLT_FN (BUILT_IN_CTAN):
9974 if (validate_arg (arg0, COMPLEX_TYPE)
9975 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9976 return do_mpc_arg1 (arg0, type, mpc_tan);
9977 break;
9979 CASE_FLT_FN (BUILT_IN_CTANH):
9980 if (validate_arg (arg0, COMPLEX_TYPE)
9981 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9982 return do_mpc_arg1 (arg0, type, mpc_tanh);
9983 break;
9985 CASE_FLT_FN (BUILT_IN_CLOG):
9986 if (validate_arg (arg0, COMPLEX_TYPE)
9987 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9988 return do_mpc_arg1 (arg0, type, mpc_log);
9989 break;
9991 CASE_FLT_FN (BUILT_IN_CSQRT):
9992 if (validate_arg (arg0, COMPLEX_TYPE)
9993 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9994 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9995 break;
9997 CASE_FLT_FN (BUILT_IN_CASIN):
9998 if (validate_arg (arg0, COMPLEX_TYPE)
9999 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10000 return do_mpc_arg1 (arg0, type, mpc_asin);
10001 break;
10003 CASE_FLT_FN (BUILT_IN_CACOS):
10004 if (validate_arg (arg0, COMPLEX_TYPE)
10005 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10006 return do_mpc_arg1 (arg0, type, mpc_acos);
10007 break;
10009 CASE_FLT_FN (BUILT_IN_CATAN):
10010 if (validate_arg (arg0, COMPLEX_TYPE)
10011 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10012 return do_mpc_arg1 (arg0, type, mpc_atan);
10013 break;
10015 CASE_FLT_FN (BUILT_IN_CASINH):
10016 if (validate_arg (arg0, COMPLEX_TYPE)
10017 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10018 return do_mpc_arg1 (arg0, type, mpc_asinh);
10019 break;
10021 CASE_FLT_FN (BUILT_IN_CACOSH):
10022 if (validate_arg (arg0, COMPLEX_TYPE)
10023 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10024 return do_mpc_arg1 (arg0, type, mpc_acosh);
10025 break;
10027 CASE_FLT_FN (BUILT_IN_CATANH):
10028 if (validate_arg (arg0, COMPLEX_TYPE)
10029 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10030 return do_mpc_arg1 (arg0, type, mpc_atanh);
10031 break;
10033 CASE_FLT_FN (BUILT_IN_CABS):
10034 return fold_builtin_cabs (loc, arg0, type, fndecl);
10036 CASE_FLT_FN (BUILT_IN_CARG):
10037 return fold_builtin_carg (loc, arg0, type);
10039 CASE_FLT_FN (BUILT_IN_SQRT):
10040 return fold_builtin_sqrt (loc, arg0, type);
10042 CASE_FLT_FN (BUILT_IN_CBRT):
10043 return fold_builtin_cbrt (loc, arg0, type);
10045 CASE_FLT_FN (BUILT_IN_ASIN):
10046 if (validate_arg (arg0, REAL_TYPE))
10047 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10048 &dconstm1, &dconst1, true);
10049 break;
10051 CASE_FLT_FN (BUILT_IN_ACOS):
10052 if (validate_arg (arg0, REAL_TYPE))
10053 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10054 &dconstm1, &dconst1, true);
10055 break;
10057 CASE_FLT_FN (BUILT_IN_ATAN):
10058 if (validate_arg (arg0, REAL_TYPE))
10059 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10060 break;
10062 CASE_FLT_FN (BUILT_IN_ASINH):
10063 if (validate_arg (arg0, REAL_TYPE))
10064 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10065 break;
10067 CASE_FLT_FN (BUILT_IN_ACOSH):
10068 if (validate_arg (arg0, REAL_TYPE))
10069 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10070 &dconst1, NULL, true);
10071 break;
10073 CASE_FLT_FN (BUILT_IN_ATANH):
10074 if (validate_arg (arg0, REAL_TYPE))
10075 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10076 &dconstm1, &dconst1, false);
10077 break;
10079 CASE_FLT_FN (BUILT_IN_SIN):
10080 if (validate_arg (arg0, REAL_TYPE))
10081 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10082 break;
10084 CASE_FLT_FN (BUILT_IN_COS):
10085 return fold_builtin_cos (loc, arg0, type, fndecl);
10087 CASE_FLT_FN (BUILT_IN_TAN):
10088 return fold_builtin_tan (arg0, type);
10090 CASE_FLT_FN (BUILT_IN_CEXP):
10091 return fold_builtin_cexp (loc, arg0, type);
10093 CASE_FLT_FN (BUILT_IN_CEXPI):
10094 if (validate_arg (arg0, REAL_TYPE))
10095 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10096 break;
10098 CASE_FLT_FN (BUILT_IN_SINH):
10099 if (validate_arg (arg0, REAL_TYPE))
10100 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10101 break;
10103 CASE_FLT_FN (BUILT_IN_COSH):
10104 return fold_builtin_cosh (loc, arg0, type, fndecl);
10106 CASE_FLT_FN (BUILT_IN_TANH):
10107 if (validate_arg (arg0, REAL_TYPE))
10108 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10109 break;
10111 CASE_FLT_FN (BUILT_IN_ERF):
10112 if (validate_arg (arg0, REAL_TYPE))
10113 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10114 break;
10116 CASE_FLT_FN (BUILT_IN_ERFC):
10117 if (validate_arg (arg0, REAL_TYPE))
10118 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10119 break;
10121 CASE_FLT_FN (BUILT_IN_TGAMMA):
10122 if (validate_arg (arg0, REAL_TYPE))
10123 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10124 break;
10126 CASE_FLT_FN (BUILT_IN_EXP):
10127 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10129 CASE_FLT_FN (BUILT_IN_EXP2):
10130 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10132 CASE_FLT_FN (BUILT_IN_EXP10):
10133 CASE_FLT_FN (BUILT_IN_POW10):
10134 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10136 CASE_FLT_FN (BUILT_IN_EXPM1):
10137 if (validate_arg (arg0, REAL_TYPE))
10138 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10139 break;
10141 CASE_FLT_FN (BUILT_IN_LOG):
10142 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10144 CASE_FLT_FN (BUILT_IN_LOG2):
10145 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10147 CASE_FLT_FN (BUILT_IN_LOG10):
10148 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10150 CASE_FLT_FN (BUILT_IN_LOG1P):
10151 if (validate_arg (arg0, REAL_TYPE))
10152 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10153 &dconstm1, NULL, false);
10154 break;
10156 CASE_FLT_FN (BUILT_IN_J0):
10157 if (validate_arg (arg0, REAL_TYPE))
10158 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10159 NULL, NULL, 0);
10160 break;
10162 CASE_FLT_FN (BUILT_IN_J1):
10163 if (validate_arg (arg0, REAL_TYPE))
10164 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10165 NULL, NULL, 0);
10166 break;
10168 CASE_FLT_FN (BUILT_IN_Y0):
10169 if (validate_arg (arg0, REAL_TYPE))
10170 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10171 &dconst0, NULL, false);
10172 break;
10174 CASE_FLT_FN (BUILT_IN_Y1):
10175 if (validate_arg (arg0, REAL_TYPE))
10176 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10177 &dconst0, NULL, false);
10178 break;
10180 CASE_FLT_FN (BUILT_IN_NAN):
10181 case BUILT_IN_NAND32:
10182 case BUILT_IN_NAND64:
10183 case BUILT_IN_NAND128:
10184 return fold_builtin_nan (arg0, type, true);
10186 CASE_FLT_FN (BUILT_IN_NANS):
10187 return fold_builtin_nan (arg0, type, false);
10189 CASE_FLT_FN (BUILT_IN_FLOOR):
10190 return fold_builtin_floor (loc, fndecl, arg0);
10192 CASE_FLT_FN (BUILT_IN_CEIL):
10193 return fold_builtin_ceil (loc, fndecl, arg0);
10195 CASE_FLT_FN (BUILT_IN_TRUNC):
10196 return fold_builtin_trunc (loc, fndecl, arg0);
10198 CASE_FLT_FN (BUILT_IN_ROUND):
10199 return fold_builtin_round (loc, fndecl, arg0);
10201 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10202 CASE_FLT_FN (BUILT_IN_RINT):
10203 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10205 CASE_FLT_FN (BUILT_IN_LCEIL):
10206 CASE_FLT_FN (BUILT_IN_LLCEIL):
10207 CASE_FLT_FN (BUILT_IN_LFLOOR):
10208 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10209 CASE_FLT_FN (BUILT_IN_LROUND):
10210 CASE_FLT_FN (BUILT_IN_LLROUND):
10211 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10213 CASE_FLT_FN (BUILT_IN_LRINT):
10214 CASE_FLT_FN (BUILT_IN_LLRINT):
10215 return fold_fixed_mathfn (loc, fndecl, arg0);
10217 case BUILT_IN_BSWAP32:
10218 case BUILT_IN_BSWAP64:
10219 return fold_builtin_bswap (fndecl, arg0);
10221 CASE_INT_FN (BUILT_IN_FFS):
10222 CASE_INT_FN (BUILT_IN_CLZ):
10223 CASE_INT_FN (BUILT_IN_CTZ):
10224 CASE_INT_FN (BUILT_IN_POPCOUNT):
10225 CASE_INT_FN (BUILT_IN_PARITY):
10226 return fold_builtin_bitop (fndecl, arg0);
10228 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10229 return fold_builtin_signbit (loc, arg0, type);
10231 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10232 return fold_builtin_significand (loc, arg0, type);
10234 CASE_FLT_FN (BUILT_IN_ILOGB):
10235 CASE_FLT_FN (BUILT_IN_LOGB):
10236 return fold_builtin_logb (loc, arg0, type);
10238 case BUILT_IN_ISASCII:
10239 return fold_builtin_isascii (loc, arg0);
10241 case BUILT_IN_TOASCII:
10242 return fold_builtin_toascii (loc, arg0);
10244 case BUILT_IN_ISDIGIT:
10245 return fold_builtin_isdigit (loc, arg0);
10247 CASE_FLT_FN (BUILT_IN_FINITE):
10248 case BUILT_IN_FINITED32:
10249 case BUILT_IN_FINITED64:
10250 case BUILT_IN_FINITED128:
10251 case BUILT_IN_ISFINITE:
10253 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10254 if (ret)
10255 return ret;
10256 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10259 CASE_FLT_FN (BUILT_IN_ISINF):
10260 case BUILT_IN_ISINFD32:
10261 case BUILT_IN_ISINFD64:
10262 case BUILT_IN_ISINFD128:
10264 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10265 if (ret)
10266 return ret;
10267 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10270 case BUILT_IN_ISNORMAL:
10271 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10273 case BUILT_IN_ISINF_SIGN:
10274 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10276 CASE_FLT_FN (BUILT_IN_ISNAN):
10277 case BUILT_IN_ISNAND32:
10278 case BUILT_IN_ISNAND64:
10279 case BUILT_IN_ISNAND128:
10280 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10282 case BUILT_IN_PRINTF:
10283 case BUILT_IN_PRINTF_UNLOCKED:
10284 case BUILT_IN_VPRINTF:
10285 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10287 case BUILT_IN_FREE:
10288 if (integer_zerop (arg0))
10289 return build_empty_stmt (loc);
10290 break;
10292 default:
10293 break;
10296 return NULL_TREE;
10300 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10301 IGNORE is true if the result of the function call is ignored. This
10302 function returns NULL_TREE if no simplification was possible. */
10304 static tree
10305 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10307 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10308 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10310 switch (fcode)
10312 CASE_FLT_FN (BUILT_IN_JN):
10313 if (validate_arg (arg0, INTEGER_TYPE)
10314 && validate_arg (arg1, REAL_TYPE))
10315 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10316 break;
10318 CASE_FLT_FN (BUILT_IN_YN):
10319 if (validate_arg (arg0, INTEGER_TYPE)
10320 && validate_arg (arg1, REAL_TYPE))
10321 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10322 &dconst0, false);
10323 break;
10325 CASE_FLT_FN (BUILT_IN_DREM):
10326 CASE_FLT_FN (BUILT_IN_REMAINDER):
10327 if (validate_arg (arg0, REAL_TYPE)
10328 && validate_arg(arg1, REAL_TYPE))
10329 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10330 break;
10332 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10333 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10334 if (validate_arg (arg0, REAL_TYPE)
10335 && validate_arg(arg1, POINTER_TYPE))
10336 return do_mpfr_lgamma_r (arg0, arg1, type);
10337 break;
10339 CASE_FLT_FN (BUILT_IN_ATAN2):
10340 if (validate_arg (arg0, REAL_TYPE)
10341 && validate_arg(arg1, REAL_TYPE))
10342 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10343 break;
10345 CASE_FLT_FN (BUILT_IN_FDIM):
10346 if (validate_arg (arg0, REAL_TYPE)
10347 && validate_arg(arg1, REAL_TYPE))
10348 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10349 break;
10351 CASE_FLT_FN (BUILT_IN_HYPOT):
10352 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10354 CASE_FLT_FN (BUILT_IN_CPOW):
10355 if (validate_arg (arg0, COMPLEX_TYPE)
10356 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10357 && validate_arg (arg1, COMPLEX_TYPE)
10358 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10359 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10360 break;
10362 CASE_FLT_FN (BUILT_IN_LDEXP):
10363 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10364 CASE_FLT_FN (BUILT_IN_SCALBN):
10365 CASE_FLT_FN (BUILT_IN_SCALBLN):
10366 return fold_builtin_load_exponent (loc, arg0, arg1,
10367 type, /*ldexp=*/false);
10369 CASE_FLT_FN (BUILT_IN_FREXP):
10370 return fold_builtin_frexp (loc, arg0, arg1, type);
10372 CASE_FLT_FN (BUILT_IN_MODF):
10373 return fold_builtin_modf (loc, arg0, arg1, type);
10375 case BUILT_IN_BZERO:
10376 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10378 case BUILT_IN_FPUTS:
10379 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10381 case BUILT_IN_FPUTS_UNLOCKED:
10382 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10384 case BUILT_IN_STRSTR:
10385 return fold_builtin_strstr (loc, arg0, arg1, type);
10387 case BUILT_IN_STRCAT:
10388 return fold_builtin_strcat (loc, arg0, arg1);
10390 case BUILT_IN_STRSPN:
10391 return fold_builtin_strspn (loc, arg0, arg1);
10393 case BUILT_IN_STRCSPN:
10394 return fold_builtin_strcspn (loc, arg0, arg1);
10396 case BUILT_IN_STRCHR:
10397 case BUILT_IN_INDEX:
10398 return fold_builtin_strchr (loc, arg0, arg1, type);
10400 case BUILT_IN_STRRCHR:
10401 case BUILT_IN_RINDEX:
10402 return fold_builtin_strrchr (loc, arg0, arg1, type);
10404 case BUILT_IN_STRCPY:
10405 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10407 case BUILT_IN_STPCPY:
10408 if (ignore)
10410 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10411 if (!fn)
10412 break;
10414 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10416 else
10417 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10418 break;
10420 case BUILT_IN_STRCMP:
10421 return fold_builtin_strcmp (loc, arg0, arg1);
10423 case BUILT_IN_STRPBRK:
10424 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10426 case BUILT_IN_EXPECT:
10427 return fold_builtin_expect (loc, arg0, arg1);
10429 CASE_FLT_FN (BUILT_IN_POW):
10430 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10432 CASE_FLT_FN (BUILT_IN_POWI):
10433 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10435 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10436 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10438 CASE_FLT_FN (BUILT_IN_FMIN):
10439 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10441 CASE_FLT_FN (BUILT_IN_FMAX):
10442 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10444 case BUILT_IN_ISGREATER:
10445 return fold_builtin_unordered_cmp (loc, fndecl,
10446 arg0, arg1, UNLE_EXPR, LE_EXPR);
10447 case BUILT_IN_ISGREATEREQUAL:
10448 return fold_builtin_unordered_cmp (loc, fndecl,
10449 arg0, arg1, UNLT_EXPR, LT_EXPR);
10450 case BUILT_IN_ISLESS:
10451 return fold_builtin_unordered_cmp (loc, fndecl,
10452 arg0, arg1, UNGE_EXPR, GE_EXPR);
10453 case BUILT_IN_ISLESSEQUAL:
10454 return fold_builtin_unordered_cmp (loc, fndecl,
10455 arg0, arg1, UNGT_EXPR, GT_EXPR);
10456 case BUILT_IN_ISLESSGREATER:
10457 return fold_builtin_unordered_cmp (loc, fndecl,
10458 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10459 case BUILT_IN_ISUNORDERED:
10460 return fold_builtin_unordered_cmp (loc, fndecl,
10461 arg0, arg1, UNORDERED_EXPR,
10462 NOP_EXPR);
10464 /* We do the folding for va_start in the expander. */
10465 case BUILT_IN_VA_START:
10466 break;
10468 case BUILT_IN_SPRINTF:
10469 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10471 case BUILT_IN_OBJECT_SIZE:
10472 return fold_builtin_object_size (arg0, arg1);
10474 case BUILT_IN_PRINTF:
10475 case BUILT_IN_PRINTF_UNLOCKED:
10476 case BUILT_IN_VPRINTF:
10477 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10479 case BUILT_IN_PRINTF_CHK:
10480 case BUILT_IN_VPRINTF_CHK:
10481 if (!validate_arg (arg0, INTEGER_TYPE)
10482 || TREE_SIDE_EFFECTS (arg0))
10483 return NULL_TREE;
10484 else
10485 return fold_builtin_printf (loc, fndecl,
10486 arg1, NULL_TREE, ignore, fcode);
10487 break;
10489 case BUILT_IN_FPRINTF:
10490 case BUILT_IN_FPRINTF_UNLOCKED:
10491 case BUILT_IN_VFPRINTF:
10492 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10493 ignore, fcode);
10495 default:
10496 break;
10498 return NULL_TREE;
10501 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10502 and ARG2. IGNORE is true if the result of the function call is ignored.
10503 This function returns NULL_TREE if no simplification was possible. */
10505 static tree
10506 fold_builtin_3 (location_t loc, tree fndecl,
10507 tree arg0, tree arg1, tree arg2, bool ignore)
10509 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10510 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10511 switch (fcode)
10514 CASE_FLT_FN (BUILT_IN_SINCOS):
10515 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10517 CASE_FLT_FN (BUILT_IN_FMA):
10518 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10519 break;
10521 CASE_FLT_FN (BUILT_IN_REMQUO):
10522 if (validate_arg (arg0, REAL_TYPE)
10523 && validate_arg(arg1, REAL_TYPE)
10524 && validate_arg(arg2, POINTER_TYPE))
10525 return do_mpfr_remquo (arg0, arg1, arg2);
10526 break;
10528 case BUILT_IN_MEMSET:
10529 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10531 case BUILT_IN_BCOPY:
10532 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10533 void_type_node, true, /*endp=*/3);
10535 case BUILT_IN_MEMCPY:
10536 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10537 type, ignore, /*endp=*/0);
10539 case BUILT_IN_MEMPCPY:
10540 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10541 type, ignore, /*endp=*/1);
10543 case BUILT_IN_MEMMOVE:
10544 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10545 type, ignore, /*endp=*/3);
10547 case BUILT_IN_STRNCAT:
10548 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10550 case BUILT_IN_STRNCPY:
10551 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10553 case BUILT_IN_STRNCMP:
10554 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10556 case BUILT_IN_MEMCHR:
10557 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10559 case BUILT_IN_BCMP:
10560 case BUILT_IN_MEMCMP:
10561 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10563 case BUILT_IN_SPRINTF:
10564 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10566 case BUILT_IN_SNPRINTF:
10567 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10569 case BUILT_IN_STRCPY_CHK:
10570 case BUILT_IN_STPCPY_CHK:
10571 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10572 ignore, fcode);
10574 case BUILT_IN_STRCAT_CHK:
10575 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10577 case BUILT_IN_PRINTF_CHK:
10578 case BUILT_IN_VPRINTF_CHK:
10579 if (!validate_arg (arg0, INTEGER_TYPE)
10580 || TREE_SIDE_EFFECTS (arg0))
10581 return NULL_TREE;
10582 else
10583 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10584 break;
10586 case BUILT_IN_FPRINTF:
10587 case BUILT_IN_FPRINTF_UNLOCKED:
10588 case BUILT_IN_VFPRINTF:
10589 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10590 ignore, fcode);
10592 case BUILT_IN_FPRINTF_CHK:
10593 case BUILT_IN_VFPRINTF_CHK:
10594 if (!validate_arg (arg1, INTEGER_TYPE)
10595 || TREE_SIDE_EFFECTS (arg1))
10596 return NULL_TREE;
10597 else
10598 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10599 ignore, fcode);
10601 default:
10602 break;
10604 return NULL_TREE;
10607 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10608 ARG2, and ARG3. IGNORE is true if the result of the function call is
10609 ignored. This function returns NULL_TREE if no simplification was
10610 possible. */
10612 static tree
10613 fold_builtin_4 (location_t loc, tree fndecl,
10614 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10616 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10618 switch (fcode)
10620 case BUILT_IN_MEMCPY_CHK:
10621 case BUILT_IN_MEMPCPY_CHK:
10622 case BUILT_IN_MEMMOVE_CHK:
10623 case BUILT_IN_MEMSET_CHK:
10624 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10625 NULL_TREE, ignore,
10626 DECL_FUNCTION_CODE (fndecl));
10628 case BUILT_IN_STRNCPY_CHK:
10629 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10631 case BUILT_IN_STRNCAT_CHK:
10632 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10634 case BUILT_IN_SNPRINTF:
10635 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10637 case BUILT_IN_FPRINTF_CHK:
10638 case BUILT_IN_VFPRINTF_CHK:
10639 if (!validate_arg (arg1, INTEGER_TYPE)
10640 || TREE_SIDE_EFFECTS (arg1))
10641 return NULL_TREE;
10642 else
10643 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10644 ignore, fcode);
10645 break;
10647 default:
10648 break;
10650 return NULL_TREE;
10653 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10654 arguments, where NARGS <= 4. IGNORE is true if the result of the
10655 function call is ignored. This function returns NULL_TREE if no
10656 simplification was possible. Note that this only folds builtins with
10657 fixed argument patterns. Foldings that do varargs-to-varargs
10658 transformations, or that match calls with more than 4 arguments,
10659 need to be handled with fold_builtin_varargs instead. */
10661 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10663 static tree
10664 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10666 tree ret = NULL_TREE;
10668 switch (nargs)
10670 case 0:
10671 ret = fold_builtin_0 (loc, fndecl, ignore);
10672 break;
10673 case 1:
10674 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10675 break;
10676 case 2:
10677 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10678 break;
10679 case 3:
10680 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10681 break;
10682 case 4:
10683 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10684 ignore);
10685 break;
10686 default:
10687 break;
10689 if (ret)
10691 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10692 SET_EXPR_LOCATION (ret, loc);
10693 TREE_NO_WARNING (ret) = 1;
10694 return ret;
10696 return NULL_TREE;
10699 /* Builtins with folding operations that operate on "..." arguments
10700 need special handling; we need to store the arguments in a convenient
10701 data structure before attempting any folding. Fortunately there are
10702 only a few builtins that fall into this category. FNDECL is the
10703 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10704 result of the function call is ignored. */
10706 static tree
10707 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10708 bool ignore ATTRIBUTE_UNUSED)
10710 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10711 tree ret = NULL_TREE;
10713 switch (fcode)
10715 case BUILT_IN_SPRINTF_CHK:
10716 case BUILT_IN_VSPRINTF_CHK:
10717 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10718 break;
10720 case BUILT_IN_SNPRINTF_CHK:
10721 case BUILT_IN_VSNPRINTF_CHK:
10722 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10723 break;
10725 case BUILT_IN_FPCLASSIFY:
10726 ret = fold_builtin_fpclassify (loc, exp);
10727 break;
10729 default:
10730 break;
10732 if (ret)
10734 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10735 SET_EXPR_LOCATION (ret, loc);
10736 TREE_NO_WARNING (ret) = 1;
10737 return ret;
10739 return NULL_TREE;
10742 /* Return true if FNDECL shouldn't be folded right now.
10743 If a built-in function has an inline attribute always_inline
10744 wrapper, defer folding it after always_inline functions have
10745 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10746 might not be performed. */
10748 static bool
10749 avoid_folding_inline_builtin (tree fndecl)
10751 return (DECL_DECLARED_INLINE_P (fndecl)
10752 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10753 && cfun
10754 && !cfun->always_inline_functions_inlined
10755 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10758 /* A wrapper function for builtin folding that prevents warnings for
10759 "statement without effect" and the like, caused by removing the
10760 call node earlier than the warning is generated. */
10762 tree
10763 fold_call_expr (location_t loc, tree exp, bool ignore)
10765 tree ret = NULL_TREE;
10766 tree fndecl = get_callee_fndecl (exp);
10767 if (fndecl
10768 && TREE_CODE (fndecl) == FUNCTION_DECL
10769 && DECL_BUILT_IN (fndecl)
10770 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10771 yet. Defer folding until we see all the arguments
10772 (after inlining). */
10773 && !CALL_EXPR_VA_ARG_PACK (exp))
10775 int nargs = call_expr_nargs (exp);
10777 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10778 instead last argument is __builtin_va_arg_pack (). Defer folding
10779 even in that case, until arguments are finalized. */
10780 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10782 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10783 if (fndecl2
10784 && TREE_CODE (fndecl2) == FUNCTION_DECL
10785 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10786 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10787 return NULL_TREE;
10790 if (avoid_folding_inline_builtin (fndecl))
10791 return NULL_TREE;
10793 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10794 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10795 CALL_EXPR_ARGP (exp), ignore);
10796 else
10798 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10800 tree *args = CALL_EXPR_ARGP (exp);
10801 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10803 if (!ret)
10804 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10805 if (ret)
10806 return ret;
10809 return NULL_TREE;
10812 /* Conveniently construct a function call expression. FNDECL names the
10813 function to be called and N arguments are passed in the array
10814 ARGARRAY. */
10816 tree
10817 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10819 tree fntype = TREE_TYPE (fndecl);
10820 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10822 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10825 /* Conveniently construct a function call expression. FNDECL names the
10826 function to be called and the arguments are passed in the vector
10827 VEC. */
10829 tree
10830 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10832 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10833 VEC_address (tree, vec));
10837 /* Conveniently construct a function call expression. FNDECL names the
10838 function to be called, N is the number of arguments, and the "..."
10839 parameters are the argument expressions. */
10841 tree
10842 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10844 va_list ap;
10845 tree *argarray = XALLOCAVEC (tree, n);
10846 int i;
10848 va_start (ap, n);
10849 for (i = 0; i < n; i++)
10850 argarray[i] = va_arg (ap, tree);
10851 va_end (ap);
10852 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10855 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10856 varargs macros aren't supported by all bootstrap compilers. */
10858 tree
10859 build_call_expr (tree fndecl, int n, ...)
10861 va_list ap;
10862 tree *argarray = XALLOCAVEC (tree, n);
10863 int i;
10865 va_start (ap, n);
10866 for (i = 0; i < n; i++)
10867 argarray[i] = va_arg (ap, tree);
10868 va_end (ap);
10869 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10872 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10873 N arguments are passed in the array ARGARRAY. */
10875 tree
10876 fold_builtin_call_array (location_t loc, tree type,
10877 tree fn,
10878 int n,
10879 tree *argarray)
10881 tree ret = NULL_TREE;
10882 tree exp;
10884 if (TREE_CODE (fn) == ADDR_EXPR)
10886 tree fndecl = TREE_OPERAND (fn, 0);
10887 if (TREE_CODE (fndecl) == FUNCTION_DECL
10888 && DECL_BUILT_IN (fndecl))
10890 /* If last argument is __builtin_va_arg_pack (), arguments to this
10891 function are not finalized yet. Defer folding until they are. */
10892 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10894 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10895 if (fndecl2
10896 && TREE_CODE (fndecl2) == FUNCTION_DECL
10897 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10898 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10899 return build_call_array_loc (loc, type, fn, n, argarray);
10901 if (avoid_folding_inline_builtin (fndecl))
10902 return build_call_array_loc (loc, type, fn, n, argarray);
10903 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10905 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10906 if (ret)
10907 return ret;
10909 return build_call_array_loc (loc, type, fn, n, argarray);
10911 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10913 /* First try the transformations that don't require consing up
10914 an exp. */
10915 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10916 if (ret)
10917 return ret;
10920 /* If we got this far, we need to build an exp. */
10921 exp = build_call_array_loc (loc, type, fn, n, argarray);
10922 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10923 return ret ? ret : exp;
10927 return build_call_array_loc (loc, type, fn, n, argarray);
10930 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10931 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10932 of arguments in ARGS to be omitted. OLDNARGS is the number of
10933 elements in ARGS. */
10935 static tree
10936 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10937 int skip, tree fndecl, int n, va_list newargs)
10939 int nargs = oldnargs - skip + n;
10940 tree *buffer;
10942 if (n > 0)
10944 int i, j;
10946 buffer = XALLOCAVEC (tree, nargs);
10947 for (i = 0; i < n; i++)
10948 buffer[i] = va_arg (newargs, tree);
10949 for (j = skip; j < oldnargs; j++, i++)
10950 buffer[i] = args[j];
10952 else
10953 buffer = args + skip;
10955 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10958 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10959 list ARGS along with N new arguments specified as the "..."
10960 parameters. SKIP is the number of arguments in ARGS to be omitted.
10961 OLDNARGS is the number of elements in ARGS. */
10963 static tree
10964 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10965 int skip, tree fndecl, int n, ...)
10967 va_list ap;
10968 tree t;
10970 va_start (ap, n);
10971 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10972 va_end (ap);
10974 return t;
10977 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10978 along with N new arguments specified as the "..." parameters. SKIP
10979 is the number of arguments in EXP to be omitted. This function is used
10980 to do varargs-to-varargs transformations. */
10982 static tree
10983 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10985 va_list ap;
10986 tree t;
10988 va_start (ap, n);
10989 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10990 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10991 va_end (ap);
10993 return t;
10996 /* Validate a single argument ARG against a tree code CODE representing
10997 a type. */
10999 static bool
11000 validate_arg (const_tree arg, enum tree_code code)
11002 if (!arg)
11003 return false;
11004 else if (code == POINTER_TYPE)
11005 return POINTER_TYPE_P (TREE_TYPE (arg));
11006 else if (code == INTEGER_TYPE)
11007 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11008 return code == TREE_CODE (TREE_TYPE (arg));
11011 /* This function validates the types of a function call argument list
11012 against a specified list of tree_codes. If the last specifier is a 0,
11013 that represents an ellipses, otherwise the last specifier must be a
11014 VOID_TYPE.
11016 This is the GIMPLE version of validate_arglist. Eventually we want to
11017 completely convert builtins.c to work from GIMPLEs and the tree based
11018 validate_arglist will then be removed. */
11020 bool
11021 validate_gimple_arglist (const_gimple call, ...)
11023 enum tree_code code;
11024 bool res = 0;
11025 va_list ap;
11026 const_tree arg;
11027 size_t i;
11029 va_start (ap, call);
11030 i = 0;
11034 code = (enum tree_code) va_arg (ap, int);
11035 switch (code)
11037 case 0:
11038 /* This signifies an ellipses, any further arguments are all ok. */
11039 res = true;
11040 goto end;
11041 case VOID_TYPE:
11042 /* This signifies an endlink, if no arguments remain, return
11043 true, otherwise return false. */
11044 res = (i == gimple_call_num_args (call));
11045 goto end;
11046 default:
11047 /* If no parameters remain or the parameter's code does not
11048 match the specified code, return false. Otherwise continue
11049 checking any remaining arguments. */
11050 arg = gimple_call_arg (call, i++);
11051 if (!validate_arg (arg, code))
11052 goto end;
11053 break;
11056 while (1);
11058 /* We need gotos here since we can only have one VA_CLOSE in a
11059 function. */
11060 end: ;
11061 va_end (ap);
11063 return res;
11066 /* This function validates the types of a function call argument list
11067 against a specified list of tree_codes. If the last specifier is a 0,
11068 that represents an ellipses, otherwise the last specifier must be a
11069 VOID_TYPE. */
11071 bool
11072 validate_arglist (const_tree callexpr, ...)
11074 enum tree_code code;
11075 bool res = 0;
11076 va_list ap;
11077 const_call_expr_arg_iterator iter;
11078 const_tree arg;
11080 va_start (ap, callexpr);
11081 init_const_call_expr_arg_iterator (callexpr, &iter);
11085 code = (enum tree_code) va_arg (ap, int);
11086 switch (code)
11088 case 0:
11089 /* This signifies an ellipses, any further arguments are all ok. */
11090 res = true;
11091 goto end;
11092 case VOID_TYPE:
11093 /* This signifies an endlink, if no arguments remain, return
11094 true, otherwise return false. */
11095 res = !more_const_call_expr_args_p (&iter);
11096 goto end;
11097 default:
11098 /* If no parameters remain or the parameter's code does not
11099 match the specified code, return false. Otherwise continue
11100 checking any remaining arguments. */
11101 arg = next_const_call_expr_arg (&iter);
11102 if (!validate_arg (arg, code))
11103 goto end;
11104 break;
11107 while (1);
11109 /* We need gotos here since we can only have one VA_CLOSE in a
11110 function. */
11111 end: ;
11112 va_end (ap);
11114 return res;
11117 /* Default target-specific builtin expander that does nothing. */
11120 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11121 rtx target ATTRIBUTE_UNUSED,
11122 rtx subtarget ATTRIBUTE_UNUSED,
11123 enum machine_mode mode ATTRIBUTE_UNUSED,
11124 int ignore ATTRIBUTE_UNUSED)
11126 return NULL_RTX;
11129 /* Returns true is EXP represents data that would potentially reside
11130 in a readonly section. */
11132 static bool
11133 readonly_data_expr (tree exp)
11135 STRIP_NOPS (exp);
11137 if (TREE_CODE (exp) != ADDR_EXPR)
11138 return false;
11140 exp = get_base_address (TREE_OPERAND (exp, 0));
11141 if (!exp)
11142 return false;
11144 /* Make sure we call decl_readonly_section only for trees it
11145 can handle (since it returns true for everything it doesn't
11146 understand). */
11147 if (TREE_CODE (exp) == STRING_CST
11148 || TREE_CODE (exp) == CONSTRUCTOR
11149 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11150 return decl_readonly_section (exp, 0);
11151 else
11152 return false;
11155 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11156 to the call, and TYPE is its return type.
11158 Return NULL_TREE if no simplification was possible, otherwise return the
11159 simplified form of the call as a tree.
11161 The simplified form may be a constant or other expression which
11162 computes the same value, but in a more efficient manner (including
11163 calls to other builtin functions).
11165 The call may contain arguments which need to be evaluated, but
11166 which are not useful to determine the result of the call. In
11167 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11168 COMPOUND_EXPR will be an argument which must be evaluated.
11169 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11170 COMPOUND_EXPR in the chain will contain the tree for the simplified
11171 form of the builtin function call. */
11173 static tree
11174 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11176 if (!validate_arg (s1, POINTER_TYPE)
11177 || !validate_arg (s2, POINTER_TYPE))
11178 return NULL_TREE;
11179 else
11181 tree fn;
11182 const char *p1, *p2;
11184 p2 = c_getstr (s2);
11185 if (p2 == NULL)
11186 return NULL_TREE;
11188 p1 = c_getstr (s1);
11189 if (p1 != NULL)
11191 const char *r = strstr (p1, p2);
11192 tree tem;
11194 if (r == NULL)
11195 return build_int_cst (TREE_TYPE (s1), 0);
11197 /* Return an offset into the constant string argument. */
11198 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11199 s1, size_int (r - p1));
11200 return fold_convert_loc (loc, type, tem);
11203 /* The argument is const char *, and the result is char *, so we need
11204 a type conversion here to avoid a warning. */
11205 if (p2[0] == '\0')
11206 return fold_convert_loc (loc, type, s1);
11208 if (p2[1] != '\0')
11209 return NULL_TREE;
11211 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11212 if (!fn)
11213 return NULL_TREE;
11215 /* New argument list transforming strstr(s1, s2) to
11216 strchr(s1, s2[0]). */
11217 return build_call_expr_loc (loc, fn, 2, s1,
11218 build_int_cst (integer_type_node, p2[0]));
11222 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11223 the call, and TYPE is its return type.
11225 Return NULL_TREE if no simplification was possible, otherwise return the
11226 simplified form of the call as a tree.
11228 The simplified form may be a constant or other expression which
11229 computes the same value, but in a more efficient manner (including
11230 calls to other builtin functions).
11232 The call may contain arguments which need to be evaluated, but
11233 which are not useful to determine the result of the call. In
11234 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11235 COMPOUND_EXPR will be an argument which must be evaluated.
11236 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11237 COMPOUND_EXPR in the chain will contain the tree for the simplified
11238 form of the builtin function call. */
11240 static tree
11241 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11243 if (!validate_arg (s1, POINTER_TYPE)
11244 || !validate_arg (s2, INTEGER_TYPE))
11245 return NULL_TREE;
11246 else
11248 const char *p1;
11250 if (TREE_CODE (s2) != INTEGER_CST)
11251 return NULL_TREE;
11253 p1 = c_getstr (s1);
11254 if (p1 != NULL)
11256 char c;
11257 const char *r;
11258 tree tem;
11260 if (target_char_cast (s2, &c))
11261 return NULL_TREE;
11263 r = strchr (p1, c);
11265 if (r == NULL)
11266 return build_int_cst (TREE_TYPE (s1), 0);
11268 /* Return an offset into the constant string argument. */
11269 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11270 s1, size_int (r - p1));
11271 return fold_convert_loc (loc, type, tem);
11273 return NULL_TREE;
11277 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11278 the call, and TYPE is its return type.
11280 Return NULL_TREE if no simplification was possible, otherwise return the
11281 simplified form of the call as a tree.
11283 The simplified form may be a constant or other expression which
11284 computes the same value, but in a more efficient manner (including
11285 calls to other builtin functions).
11287 The call may contain arguments which need to be evaluated, but
11288 which are not useful to determine the result of the call. In
11289 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11290 COMPOUND_EXPR will be an argument which must be evaluated.
11291 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11292 COMPOUND_EXPR in the chain will contain the tree for the simplified
11293 form of the builtin function call. */
11295 static tree
11296 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11298 if (!validate_arg (s1, POINTER_TYPE)
11299 || !validate_arg (s2, INTEGER_TYPE))
11300 return NULL_TREE;
11301 else
11303 tree fn;
11304 const char *p1;
11306 if (TREE_CODE (s2) != INTEGER_CST)
11307 return NULL_TREE;
11309 p1 = c_getstr (s1);
11310 if (p1 != NULL)
11312 char c;
11313 const char *r;
11314 tree tem;
11316 if (target_char_cast (s2, &c))
11317 return NULL_TREE;
11319 r = strrchr (p1, c);
11321 if (r == NULL)
11322 return build_int_cst (TREE_TYPE (s1), 0);
11324 /* Return an offset into the constant string argument. */
11325 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11326 s1, size_int (r - p1));
11327 return fold_convert_loc (loc, type, tem);
11330 if (! integer_zerop (s2))
11331 return NULL_TREE;
11333 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11334 if (!fn)
11335 return NULL_TREE;
11337 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11338 return build_call_expr_loc (loc, fn, 2, s1, s2);
11342 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11343 to the call, and TYPE is its return type.
11345 Return NULL_TREE if no simplification was possible, otherwise return the
11346 simplified form of the call as a tree.
11348 The simplified form may be a constant or other expression which
11349 computes the same value, but in a more efficient manner (including
11350 calls to other builtin functions).
11352 The call may contain arguments which need to be evaluated, but
11353 which are not useful to determine the result of the call. In
11354 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11355 COMPOUND_EXPR will be an argument which must be evaluated.
11356 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11357 COMPOUND_EXPR in the chain will contain the tree for the simplified
11358 form of the builtin function call. */
11360 static tree
11361 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11363 if (!validate_arg (s1, POINTER_TYPE)
11364 || !validate_arg (s2, POINTER_TYPE))
11365 return NULL_TREE;
11366 else
11368 tree fn;
11369 const char *p1, *p2;
11371 p2 = c_getstr (s2);
11372 if (p2 == NULL)
11373 return NULL_TREE;
11375 p1 = c_getstr (s1);
11376 if (p1 != NULL)
11378 const char *r = strpbrk (p1, p2);
11379 tree tem;
11381 if (r == NULL)
11382 return build_int_cst (TREE_TYPE (s1), 0);
11384 /* Return an offset into the constant string argument. */
11385 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11386 s1, size_int (r - p1));
11387 return fold_convert_loc (loc, type, tem);
11390 if (p2[0] == '\0')
11391 /* strpbrk(x, "") == NULL.
11392 Evaluate and ignore s1 in case it had side-effects. */
11393 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11395 if (p2[1] != '\0')
11396 return NULL_TREE; /* Really call strpbrk. */
11398 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11399 if (!fn)
11400 return NULL_TREE;
11402 /* New argument list transforming strpbrk(s1, s2) to
11403 strchr(s1, s2[0]). */
11404 return build_call_expr_loc (loc, fn, 2, s1,
11405 build_int_cst (integer_type_node, p2[0]));
11409 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11410 to the call.
11412 Return NULL_TREE if no simplification was possible, otherwise return the
11413 simplified form of the call as a tree.
11415 The simplified form may be a constant or other expression which
11416 computes the same value, but in a more efficient manner (including
11417 calls to other builtin functions).
11419 The call may contain arguments which need to be evaluated, but
11420 which are not useful to determine the result of the call. In
11421 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11422 COMPOUND_EXPR will be an argument which must be evaluated.
11423 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11424 COMPOUND_EXPR in the chain will contain the tree for the simplified
11425 form of the builtin function call. */
11427 static tree
11428 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11430 if (!validate_arg (dst, POINTER_TYPE)
11431 || !validate_arg (src, POINTER_TYPE))
11432 return NULL_TREE;
11433 else
11435 const char *p = c_getstr (src);
11437 /* If the string length is zero, return the dst parameter. */
11438 if (p && *p == '\0')
11439 return dst;
11441 if (optimize_insn_for_speed_p ())
11443 /* See if we can store by pieces into (dst + strlen(dst)). */
11444 tree newdst, call;
11445 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11446 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11448 if (!strlen_fn || !strcpy_fn)
11449 return NULL_TREE;
11451 /* If we don't have a movstr we don't want to emit an strcpy
11452 call. We have to do that if the length of the source string
11453 isn't computable (in that case we can use memcpy probably
11454 later expanding to a sequence of mov instructions). If we
11455 have movstr instructions we can emit strcpy calls. */
11456 if (!HAVE_movstr)
11458 tree len = c_strlen (src, 1);
11459 if (! len || TREE_SIDE_EFFECTS (len))
11460 return NULL_TREE;
11463 /* Stabilize the argument list. */
11464 dst = builtin_save_expr (dst);
11466 /* Create strlen (dst). */
11467 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11468 /* Create (dst p+ strlen (dst)). */
11470 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11471 TREE_TYPE (dst), dst, newdst);
11472 newdst = builtin_save_expr (newdst);
11474 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11475 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11477 return NULL_TREE;
11481 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11482 arguments to the call.
11484 Return NULL_TREE if no simplification was possible, otherwise return the
11485 simplified form of the call as a tree.
11487 The simplified form may be a constant or other expression which
11488 computes the same value, but in a more efficient manner (including
11489 calls to other builtin functions).
11491 The call may contain arguments which need to be evaluated, but
11492 which are not useful to determine the result of the call. In
11493 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11494 COMPOUND_EXPR will be an argument which must be evaluated.
11495 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11496 COMPOUND_EXPR in the chain will contain the tree for the simplified
11497 form of the builtin function call. */
11499 static tree
11500 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11502 if (!validate_arg (dst, POINTER_TYPE)
11503 || !validate_arg (src, POINTER_TYPE)
11504 || !validate_arg (len, INTEGER_TYPE))
11505 return NULL_TREE;
11506 else
11508 const char *p = c_getstr (src);
11510 /* If the requested length is zero, or the src parameter string
11511 length is zero, return the dst parameter. */
11512 if (integer_zerop (len) || (p && *p == '\0'))
11513 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11515 /* If the requested len is greater than or equal to the string
11516 length, call strcat. */
11517 if (TREE_CODE (len) == INTEGER_CST && p
11518 && compare_tree_int (len, strlen (p)) >= 0)
11520 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11522 /* If the replacement _DECL isn't initialized, don't do the
11523 transformation. */
11524 if (!fn)
11525 return NULL_TREE;
11527 return build_call_expr_loc (loc, fn, 2, dst, src);
11529 return NULL_TREE;
11533 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11534 to the call.
11536 Return NULL_TREE if no simplification was possible, otherwise return the
11537 simplified form of the call as a tree.
11539 The simplified form may be a constant or other expression which
11540 computes the same value, but in a more efficient manner (including
11541 calls to other builtin functions).
11543 The call may contain arguments which need to be evaluated, but
11544 which are not useful to determine the result of the call. In
11545 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11546 COMPOUND_EXPR will be an argument which must be evaluated.
11547 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11548 COMPOUND_EXPR in the chain will contain the tree for the simplified
11549 form of the builtin function call. */
11551 static tree
11552 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11554 if (!validate_arg (s1, POINTER_TYPE)
11555 || !validate_arg (s2, POINTER_TYPE))
11556 return NULL_TREE;
11557 else
11559 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11561 /* If both arguments are constants, evaluate at compile-time. */
11562 if (p1 && p2)
11564 const size_t r = strspn (p1, p2);
11565 return size_int (r);
11568 /* If either argument is "", return NULL_TREE. */
11569 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11570 /* Evaluate and ignore both arguments in case either one has
11571 side-effects. */
11572 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11573 s1, s2);
11574 return NULL_TREE;
11578 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11579 to the call.
11581 Return NULL_TREE if no simplification was possible, otherwise return the
11582 simplified form of the call as a tree.
11584 The simplified form may be a constant or other expression which
11585 computes the same value, but in a more efficient manner (including
11586 calls to other builtin functions).
11588 The call may contain arguments which need to be evaluated, but
11589 which are not useful to determine the result of the call. In
11590 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11591 COMPOUND_EXPR will be an argument which must be evaluated.
11592 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11593 COMPOUND_EXPR in the chain will contain the tree for the simplified
11594 form of the builtin function call. */
11596 static tree
11597 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11599 if (!validate_arg (s1, POINTER_TYPE)
11600 || !validate_arg (s2, POINTER_TYPE))
11601 return NULL_TREE;
11602 else
11604 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11606 /* If both arguments are constants, evaluate at compile-time. */
11607 if (p1 && p2)
11609 const size_t r = strcspn (p1, p2);
11610 return size_int (r);
11613 /* If the first argument is "", return NULL_TREE. */
11614 if (p1 && *p1 == '\0')
11616 /* Evaluate and ignore argument s2 in case it has
11617 side-effects. */
11618 return omit_one_operand_loc (loc, size_type_node,
11619 size_zero_node, s2);
11622 /* If the second argument is "", return __builtin_strlen(s1). */
11623 if (p2 && *p2 == '\0')
11625 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11627 /* If the replacement _DECL isn't initialized, don't do the
11628 transformation. */
11629 if (!fn)
11630 return NULL_TREE;
11632 return build_call_expr_loc (loc, fn, 1, s1);
11634 return NULL_TREE;
11638 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11639 to the call. IGNORE is true if the value returned
11640 by the builtin will be ignored. UNLOCKED is true is true if this
11641 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11642 the known length of the string. Return NULL_TREE if no simplification
11643 was possible. */
11645 tree
11646 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11647 bool ignore, bool unlocked, tree len)
11649 /* If we're using an unlocked function, assume the other unlocked
11650 functions exist explicitly. */
11651 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11652 : implicit_built_in_decls[BUILT_IN_FPUTC];
11653 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11654 : implicit_built_in_decls[BUILT_IN_FWRITE];
11656 /* If the return value is used, don't do the transformation. */
11657 if (!ignore)
11658 return NULL_TREE;
11660 /* Verify the arguments in the original call. */
11661 if (!validate_arg (arg0, POINTER_TYPE)
11662 || !validate_arg (arg1, POINTER_TYPE))
11663 return NULL_TREE;
11665 if (! len)
11666 len = c_strlen (arg0, 0);
11668 /* Get the length of the string passed to fputs. If the length
11669 can't be determined, punt. */
11670 if (!len
11671 || TREE_CODE (len) != INTEGER_CST)
11672 return NULL_TREE;
11674 switch (compare_tree_int (len, 1))
11676 case -1: /* length is 0, delete the call entirely . */
11677 return omit_one_operand_loc (loc, integer_type_node,
11678 integer_zero_node, arg1);;
11680 case 0: /* length is 1, call fputc. */
11682 const char *p = c_getstr (arg0);
11684 if (p != NULL)
11686 if (fn_fputc)
11687 return build_call_expr_loc (loc, fn_fputc, 2,
11688 build_int_cst
11689 (integer_type_node, p[0]), arg1);
11690 else
11691 return NULL_TREE;
11694 /* FALLTHROUGH */
11695 case 1: /* length is greater than 1, call fwrite. */
11697 /* If optimizing for size keep fputs. */
11698 if (optimize_function_for_size_p (cfun))
11699 return NULL_TREE;
11700 /* New argument list transforming fputs(string, stream) to
11701 fwrite(string, 1, len, stream). */
11702 if (fn_fwrite)
11703 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11704 size_one_node, len, arg1);
11705 else
11706 return NULL_TREE;
11708 default:
11709 gcc_unreachable ();
11711 return NULL_TREE;
11714 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11715 produced. False otherwise. This is done so that we don't output the error
11716 or warning twice or three times. */
11718 bool
11719 fold_builtin_next_arg (tree exp, bool va_start_p)
11721 tree fntype = TREE_TYPE (current_function_decl);
11722 int nargs = call_expr_nargs (exp);
11723 tree arg;
11725 if (!stdarg_p (fntype))
11727 error ("%<va_start%> used in function with fixed args");
11728 return true;
11731 if (va_start_p)
11733 if (va_start_p && (nargs != 2))
11735 error ("wrong number of arguments to function %<va_start%>");
11736 return true;
11738 arg = CALL_EXPR_ARG (exp, 1);
11740 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11741 when we checked the arguments and if needed issued a warning. */
11742 else
11744 if (nargs == 0)
11746 /* Evidently an out of date version of <stdarg.h>; can't validate
11747 va_start's second argument, but can still work as intended. */
11748 warning (0, "%<__builtin_next_arg%> called without an argument");
11749 return true;
11751 else if (nargs > 1)
11753 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11754 return true;
11756 arg = CALL_EXPR_ARG (exp, 0);
11759 if (TREE_CODE (arg) == SSA_NAME)
11760 arg = SSA_NAME_VAR (arg);
11762 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11763 or __builtin_next_arg (0) the first time we see it, after checking
11764 the arguments and if needed issuing a warning. */
11765 if (!integer_zerop (arg))
11767 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11769 /* Strip off all nops for the sake of the comparison. This
11770 is not quite the same as STRIP_NOPS. It does more.
11771 We must also strip off INDIRECT_EXPR for C++ reference
11772 parameters. */
11773 while (CONVERT_EXPR_P (arg)
11774 || TREE_CODE (arg) == INDIRECT_REF)
11775 arg = TREE_OPERAND (arg, 0);
11776 if (arg != last_parm)
11778 /* FIXME: Sometimes with the tree optimizers we can get the
11779 not the last argument even though the user used the last
11780 argument. We just warn and set the arg to be the last
11781 argument so that we will get wrong-code because of
11782 it. */
11783 warning (0, "second parameter of %<va_start%> not last named argument");
11786 /* Undefined by C99 7.15.1.4p4 (va_start):
11787 "If the parameter parmN is declared with the register storage
11788 class, with a function or array type, or with a type that is
11789 not compatible with the type that results after application of
11790 the default argument promotions, the behavior is undefined."
11792 else if (DECL_REGISTER (arg))
11793 warning (0, "undefined behaviour when second parameter of "
11794 "%<va_start%> is declared with %<register%> storage");
11796 /* We want to verify the second parameter just once before the tree
11797 optimizers are run and then avoid keeping it in the tree,
11798 as otherwise we could warn even for correct code like:
11799 void foo (int i, ...)
11800 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11801 if (va_start_p)
11802 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11803 else
11804 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11806 return false;
11810 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11811 ORIG may be null if this is a 2-argument call. We don't attempt to
11812 simplify calls with more than 3 arguments.
11814 Return NULL_TREE if no simplification was possible, otherwise return the
11815 simplified form of the call as a tree. If IGNORED is true, it means that
11816 the caller does not use the returned value of the function. */
11818 static tree
11819 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11820 tree orig, int ignored)
11822 tree call, retval;
11823 const char *fmt_str = NULL;
11825 /* Verify the required arguments in the original call. We deal with two
11826 types of sprintf() calls: 'sprintf (str, fmt)' and
11827 'sprintf (dest, "%s", orig)'. */
11828 if (!validate_arg (dest, POINTER_TYPE)
11829 || !validate_arg (fmt, POINTER_TYPE))
11830 return NULL_TREE;
11831 if (orig && !validate_arg (orig, POINTER_TYPE))
11832 return NULL_TREE;
11834 /* Check whether the format is a literal string constant. */
11835 fmt_str = c_getstr (fmt);
11836 if (fmt_str == NULL)
11837 return NULL_TREE;
11839 call = NULL_TREE;
11840 retval = NULL_TREE;
11842 if (!init_target_chars ())
11843 return NULL_TREE;
11845 /* If the format doesn't contain % args or %%, use strcpy. */
11846 if (strchr (fmt_str, target_percent) == NULL)
11848 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11850 if (!fn)
11851 return NULL_TREE;
11853 /* Don't optimize sprintf (buf, "abc", ptr++). */
11854 if (orig)
11855 return NULL_TREE;
11857 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11858 'format' is known to contain no % formats. */
11859 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11860 if (!ignored)
11861 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11864 /* If the format is "%s", use strcpy if the result isn't used. */
11865 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11867 tree fn;
11868 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11870 if (!fn)
11871 return NULL_TREE;
11873 /* Don't crash on sprintf (str1, "%s"). */
11874 if (!orig)
11875 return NULL_TREE;
11877 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11878 if (!ignored)
11880 retval = c_strlen (orig, 1);
11881 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11882 return NULL_TREE;
11884 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11887 if (call && retval)
11889 retval = fold_convert_loc
11890 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11891 retval);
11892 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11894 else
11895 return call;
11898 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
11899 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
11900 attempt to simplify calls with more than 4 arguments.
11902 Return NULL_TREE if no simplification was possible, otherwise return the
11903 simplified form of the call as a tree. If IGNORED is true, it means that
11904 the caller does not use the returned value of the function. */
11906 static tree
11907 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
11908 tree orig, int ignored)
11910 tree call, retval;
11911 const char *fmt_str = NULL;
11912 unsigned HOST_WIDE_INT destlen;
11914 /* Verify the required arguments in the original call. We deal with two
11915 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
11916 'snprintf (dest, cst, "%s", orig)'. */
11917 if (!validate_arg (dest, POINTER_TYPE)
11918 || !validate_arg (destsize, INTEGER_TYPE)
11919 || !validate_arg (fmt, POINTER_TYPE))
11920 return NULL_TREE;
11921 if (orig && !validate_arg (orig, POINTER_TYPE))
11922 return NULL_TREE;
11924 if (!host_integerp (destsize, 1))
11925 return NULL_TREE;
11927 /* Check whether the format is a literal string constant. */
11928 fmt_str = c_getstr (fmt);
11929 if (fmt_str == NULL)
11930 return NULL_TREE;
11932 call = NULL_TREE;
11933 retval = NULL_TREE;
11935 if (!init_target_chars ())
11936 return NULL_TREE;
11938 destlen = tree_low_cst (destsize, 1);
11940 /* If the format doesn't contain % args or %%, use strcpy. */
11941 if (strchr (fmt_str, target_percent) == NULL)
11943 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11944 size_t len = strlen (fmt_str);
11946 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
11947 if (orig)
11948 return NULL_TREE;
11950 /* We could expand this as
11951 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
11952 or to
11953 memcpy (str, fmt_with_nul_at_cstm1, cst);
11954 but in the former case that might increase code size
11955 and in the latter case grow .rodata section too much.
11956 So punt for now. */
11957 if (len >= destlen)
11958 return NULL_TREE;
11960 if (!fn)
11961 return NULL_TREE;
11963 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
11964 'format' is known to contain no % formats and
11965 strlen (fmt) < cst. */
11966 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11968 if (!ignored)
11969 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11972 /* If the format is "%s", use strcpy if the result isn't used. */
11973 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11975 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11976 unsigned HOST_WIDE_INT origlen;
11978 /* Don't crash on snprintf (str1, cst, "%s"). */
11979 if (!orig)
11980 return NULL_TREE;
11982 retval = c_strlen (orig, 1);
11983 if (!retval || !host_integerp (retval, 1))
11984 return NULL_TREE;
11986 origlen = tree_low_cst (retval, 1);
11987 /* We could expand this as
11988 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
11989 or to
11990 memcpy (str1, str2_with_nul_at_cstm1, cst);
11991 but in the former case that might increase code size
11992 and in the latter case grow .rodata section too much.
11993 So punt for now. */
11994 if (origlen >= destlen)
11995 return NULL_TREE;
11997 /* Convert snprintf (str1, cst, "%s", str2) into
11998 strcpy (str1, str2) if strlen (str2) < cst. */
11999 if (!fn)
12000 return NULL_TREE;
12002 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12004 if (ignored)
12005 retval = NULL_TREE;
12008 if (call && retval)
12010 tree fn = built_in_decls[BUILT_IN_SNPRINTF];
12011 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12012 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12014 else
12015 return call;
12018 /* Expand a call EXP to __builtin_object_size. */
12021 expand_builtin_object_size (tree exp)
12023 tree ost;
12024 int object_size_type;
12025 tree fndecl = get_callee_fndecl (exp);
12027 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12029 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12030 exp, fndecl);
12031 expand_builtin_trap ();
12032 return const0_rtx;
12035 ost = CALL_EXPR_ARG (exp, 1);
12036 STRIP_NOPS (ost);
12038 if (TREE_CODE (ost) != INTEGER_CST
12039 || tree_int_cst_sgn (ost) < 0
12040 || compare_tree_int (ost, 3) > 0)
12042 error ("%Klast argument of %D is not integer constant between 0 and 3",
12043 exp, fndecl);
12044 expand_builtin_trap ();
12045 return const0_rtx;
12048 object_size_type = tree_low_cst (ost, 0);
12050 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12053 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12054 FCODE is the BUILT_IN_* to use.
12055 Return NULL_RTX if we failed; the caller should emit a normal call,
12056 otherwise try to get the result in TARGET, if convenient (and in
12057 mode MODE if that's convenient). */
12059 static rtx
12060 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12061 enum built_in_function fcode)
12063 tree dest, src, len, size;
12065 if (!validate_arglist (exp,
12066 POINTER_TYPE,
12067 fcode == BUILT_IN_MEMSET_CHK
12068 ? INTEGER_TYPE : POINTER_TYPE,
12069 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12070 return NULL_RTX;
12072 dest = CALL_EXPR_ARG (exp, 0);
12073 src = CALL_EXPR_ARG (exp, 1);
12074 len = CALL_EXPR_ARG (exp, 2);
12075 size = CALL_EXPR_ARG (exp, 3);
12077 if (! host_integerp (size, 1))
12078 return NULL_RTX;
12080 if (host_integerp (len, 1) || integer_all_onesp (size))
12082 tree fn;
12084 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12086 warning_at (tree_nonartificial_location (exp),
12087 0, "%Kcall to %D will always overflow destination buffer",
12088 exp, get_callee_fndecl (exp));
12089 return NULL_RTX;
12092 fn = NULL_TREE;
12093 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12094 mem{cpy,pcpy,move,set} is available. */
12095 switch (fcode)
12097 case BUILT_IN_MEMCPY_CHK:
12098 fn = built_in_decls[BUILT_IN_MEMCPY];
12099 break;
12100 case BUILT_IN_MEMPCPY_CHK:
12101 fn = built_in_decls[BUILT_IN_MEMPCPY];
12102 break;
12103 case BUILT_IN_MEMMOVE_CHK:
12104 fn = built_in_decls[BUILT_IN_MEMMOVE];
12105 break;
12106 case BUILT_IN_MEMSET_CHK:
12107 fn = built_in_decls[BUILT_IN_MEMSET];
12108 break;
12109 default:
12110 break;
12113 if (! fn)
12114 return NULL_RTX;
12116 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12117 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12118 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12119 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12121 else if (fcode == BUILT_IN_MEMSET_CHK)
12122 return NULL_RTX;
12123 else
12125 unsigned int dest_align
12126 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12128 /* If DEST is not a pointer type, call the normal function. */
12129 if (dest_align == 0)
12130 return NULL_RTX;
12132 /* If SRC and DEST are the same (and not volatile), do nothing. */
12133 if (operand_equal_p (src, dest, 0))
12135 tree expr;
12137 if (fcode != BUILT_IN_MEMPCPY_CHK)
12139 /* Evaluate and ignore LEN in case it has side-effects. */
12140 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12141 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12144 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12145 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12148 /* __memmove_chk special case. */
12149 if (fcode == BUILT_IN_MEMMOVE_CHK)
12151 unsigned int src_align
12152 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12154 if (src_align == 0)
12155 return NULL_RTX;
12157 /* If src is categorized for a readonly section we can use
12158 normal __memcpy_chk. */
12159 if (readonly_data_expr (src))
12161 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12162 if (!fn)
12163 return NULL_RTX;
12164 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12165 dest, src, len, size);
12166 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12167 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12168 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12171 return NULL_RTX;
12175 /* Emit warning if a buffer overflow is detected at compile time. */
12177 static void
12178 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12180 int is_strlen = 0;
12181 tree len, size;
12182 location_t loc = tree_nonartificial_location (exp);
12184 switch (fcode)
12186 case BUILT_IN_STRCPY_CHK:
12187 case BUILT_IN_STPCPY_CHK:
12188 /* For __strcat_chk the warning will be emitted only if overflowing
12189 by at least strlen (dest) + 1 bytes. */
12190 case BUILT_IN_STRCAT_CHK:
12191 len = CALL_EXPR_ARG (exp, 1);
12192 size = CALL_EXPR_ARG (exp, 2);
12193 is_strlen = 1;
12194 break;
12195 case BUILT_IN_STRNCAT_CHK:
12196 case BUILT_IN_STRNCPY_CHK:
12197 len = CALL_EXPR_ARG (exp, 2);
12198 size = CALL_EXPR_ARG (exp, 3);
12199 break;
12200 case BUILT_IN_SNPRINTF_CHK:
12201 case BUILT_IN_VSNPRINTF_CHK:
12202 len = CALL_EXPR_ARG (exp, 1);
12203 size = CALL_EXPR_ARG (exp, 3);
12204 break;
12205 default:
12206 gcc_unreachable ();
12209 if (!len || !size)
12210 return;
12212 if (! host_integerp (size, 1) || integer_all_onesp (size))
12213 return;
12215 if (is_strlen)
12217 len = c_strlen (len, 1);
12218 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12219 return;
12221 else if (fcode == BUILT_IN_STRNCAT_CHK)
12223 tree src = CALL_EXPR_ARG (exp, 1);
12224 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12225 return;
12226 src = c_strlen (src, 1);
12227 if (! src || ! host_integerp (src, 1))
12229 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12230 exp, get_callee_fndecl (exp));
12231 return;
12233 else if (tree_int_cst_lt (src, size))
12234 return;
12236 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12237 return;
12239 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12240 exp, get_callee_fndecl (exp));
12243 /* Emit warning if a buffer overflow is detected at compile time
12244 in __sprintf_chk/__vsprintf_chk calls. */
12246 static void
12247 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12249 tree size, len, fmt;
12250 const char *fmt_str;
12251 int nargs = call_expr_nargs (exp);
12253 /* Verify the required arguments in the original call. */
12255 if (nargs < 4)
12256 return;
12257 size = CALL_EXPR_ARG (exp, 2);
12258 fmt = CALL_EXPR_ARG (exp, 3);
12260 if (! host_integerp (size, 1) || integer_all_onesp (size))
12261 return;
12263 /* Check whether the format is a literal string constant. */
12264 fmt_str = c_getstr (fmt);
12265 if (fmt_str == NULL)
12266 return;
12268 if (!init_target_chars ())
12269 return;
12271 /* If the format doesn't contain % args or %%, we know its size. */
12272 if (strchr (fmt_str, target_percent) == 0)
12273 len = build_int_cstu (size_type_node, strlen (fmt_str));
12274 /* If the format is "%s" and first ... argument is a string literal,
12275 we know it too. */
12276 else if (fcode == BUILT_IN_SPRINTF_CHK
12277 && strcmp (fmt_str, target_percent_s) == 0)
12279 tree arg;
12281 if (nargs < 5)
12282 return;
12283 arg = CALL_EXPR_ARG (exp, 4);
12284 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12285 return;
12287 len = c_strlen (arg, 1);
12288 if (!len || ! host_integerp (len, 1))
12289 return;
12291 else
12292 return;
12294 if (! tree_int_cst_lt (len, size))
12295 warning_at (tree_nonartificial_location (exp),
12296 0, "%Kcall to %D will always overflow destination buffer",
12297 exp, get_callee_fndecl (exp));
12300 /* Emit warning if a free is called with address of a variable. */
12302 static void
12303 maybe_emit_free_warning (tree exp)
12305 tree arg = CALL_EXPR_ARG (exp, 0);
12307 STRIP_NOPS (arg);
12308 if (TREE_CODE (arg) != ADDR_EXPR)
12309 return;
12311 arg = get_base_address (TREE_OPERAND (arg, 0));
12312 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12313 return;
12315 if (SSA_VAR_P (arg))
12316 warning_at (tree_nonartificial_location (exp),
12317 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12318 else
12319 warning_at (tree_nonartificial_location (exp),
12320 0, "%Kattempt to free a non-heap object", exp);
12323 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12324 if possible. */
12326 tree
12327 fold_builtin_object_size (tree ptr, tree ost)
12329 unsigned HOST_WIDE_INT bytes;
12330 int object_size_type;
12332 if (!validate_arg (ptr, POINTER_TYPE)
12333 || !validate_arg (ost, INTEGER_TYPE))
12334 return NULL_TREE;
12336 STRIP_NOPS (ost);
12338 if (TREE_CODE (ost) != INTEGER_CST
12339 || tree_int_cst_sgn (ost) < 0
12340 || compare_tree_int (ost, 3) > 0)
12341 return NULL_TREE;
12343 object_size_type = tree_low_cst (ost, 0);
12345 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12346 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12347 and (size_t) 0 for types 2 and 3. */
12348 if (TREE_SIDE_EFFECTS (ptr))
12349 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12351 if (TREE_CODE (ptr) == ADDR_EXPR)
12353 bytes = compute_builtin_object_size (ptr, object_size_type);
12354 if (double_int_fits_to_tree_p (size_type_node,
12355 uhwi_to_double_int (bytes)))
12356 return build_int_cstu (size_type_node, bytes);
12358 else if (TREE_CODE (ptr) == SSA_NAME)
12360 /* If object size is not known yet, delay folding until
12361 later. Maybe subsequent passes will help determining
12362 it. */
12363 bytes = compute_builtin_object_size (ptr, object_size_type);
12364 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12365 && double_int_fits_to_tree_p (size_type_node,
12366 uhwi_to_double_int (bytes)))
12367 return build_int_cstu (size_type_node, bytes);
12370 return NULL_TREE;
12373 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12374 DEST, SRC, LEN, 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
12377 passed as third argument. */
12379 tree
12380 fold_builtin_memory_chk (location_t loc, tree fndecl,
12381 tree dest, tree src, tree len, tree size,
12382 tree maxlen, bool ignore,
12383 enum built_in_function fcode)
12385 tree fn;
12387 if (!validate_arg (dest, POINTER_TYPE)
12388 || !validate_arg (src,
12389 (fcode == BUILT_IN_MEMSET_CHK
12390 ? INTEGER_TYPE : POINTER_TYPE))
12391 || !validate_arg (len, INTEGER_TYPE)
12392 || !validate_arg (size, INTEGER_TYPE))
12393 return NULL_TREE;
12395 /* If SRC and DEST are the same (and not volatile), return DEST
12396 (resp. DEST+LEN for __mempcpy_chk). */
12397 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12399 if (fcode != BUILT_IN_MEMPCPY_CHK)
12400 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12401 dest, len);
12402 else
12404 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12405 dest, len);
12406 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12410 if (! host_integerp (size, 1))
12411 return NULL_TREE;
12413 if (! integer_all_onesp (size))
12415 if (! host_integerp (len, 1))
12417 /* If LEN is not constant, try MAXLEN too.
12418 For MAXLEN only allow optimizing into non-_ocs function
12419 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12420 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12422 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12424 /* (void) __mempcpy_chk () can be optimized into
12425 (void) __memcpy_chk (). */
12426 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12427 if (!fn)
12428 return NULL_TREE;
12430 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12432 return NULL_TREE;
12435 else
12436 maxlen = len;
12438 if (tree_int_cst_lt (size, maxlen))
12439 return NULL_TREE;
12442 fn = NULL_TREE;
12443 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12444 mem{cpy,pcpy,move,set} is available. */
12445 switch (fcode)
12447 case BUILT_IN_MEMCPY_CHK:
12448 fn = built_in_decls[BUILT_IN_MEMCPY];
12449 break;
12450 case BUILT_IN_MEMPCPY_CHK:
12451 fn = built_in_decls[BUILT_IN_MEMPCPY];
12452 break;
12453 case BUILT_IN_MEMMOVE_CHK:
12454 fn = built_in_decls[BUILT_IN_MEMMOVE];
12455 break;
12456 case BUILT_IN_MEMSET_CHK:
12457 fn = built_in_decls[BUILT_IN_MEMSET];
12458 break;
12459 default:
12460 break;
12463 if (!fn)
12464 return NULL_TREE;
12466 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12469 /* Fold a call to the __st[rp]cpy_chk builtin.
12470 DEST, SRC, and SIZE are the arguments to the call.
12471 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12472 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12473 strings passed as second argument. */
12475 tree
12476 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12477 tree src, tree size,
12478 tree maxlen, bool ignore,
12479 enum built_in_function fcode)
12481 tree len, fn;
12483 if (!validate_arg (dest, POINTER_TYPE)
12484 || !validate_arg (src, POINTER_TYPE)
12485 || !validate_arg (size, INTEGER_TYPE))
12486 return NULL_TREE;
12488 /* If SRC and DEST are the same (and not volatile), return DEST. */
12489 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12490 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12492 if (! host_integerp (size, 1))
12493 return NULL_TREE;
12495 if (! integer_all_onesp (size))
12497 len = c_strlen (src, 1);
12498 if (! len || ! host_integerp (len, 1))
12500 /* If LEN is not constant, try MAXLEN too.
12501 For MAXLEN only allow optimizing into non-_ocs function
12502 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12503 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12505 if (fcode == BUILT_IN_STPCPY_CHK)
12507 if (! ignore)
12508 return NULL_TREE;
12510 /* If return value of __stpcpy_chk is ignored,
12511 optimize into __strcpy_chk. */
12512 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12513 if (!fn)
12514 return NULL_TREE;
12516 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12519 if (! len || TREE_SIDE_EFFECTS (len))
12520 return NULL_TREE;
12522 /* If c_strlen returned something, but not a constant,
12523 transform __strcpy_chk into __memcpy_chk. */
12524 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12525 if (!fn)
12526 return NULL_TREE;
12528 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12529 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12530 build_call_expr_loc (loc, fn, 4,
12531 dest, src, len, size));
12534 else
12535 maxlen = len;
12537 if (! tree_int_cst_lt (maxlen, size))
12538 return NULL_TREE;
12541 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12542 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12543 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12544 if (!fn)
12545 return NULL_TREE;
12547 return build_call_expr_loc (loc, fn, 2, dest, src);
12550 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12551 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12552 length passed as third argument. */
12554 tree
12555 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12556 tree len, tree size, tree maxlen)
12558 tree fn;
12560 if (!validate_arg (dest, POINTER_TYPE)
12561 || !validate_arg (src, POINTER_TYPE)
12562 || !validate_arg (len, INTEGER_TYPE)
12563 || !validate_arg (size, INTEGER_TYPE))
12564 return NULL_TREE;
12566 if (! host_integerp (size, 1))
12567 return NULL_TREE;
12569 if (! integer_all_onesp (size))
12571 if (! host_integerp (len, 1))
12573 /* If LEN is not constant, try MAXLEN too.
12574 For MAXLEN only allow optimizing into non-_ocs function
12575 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12576 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12577 return NULL_TREE;
12579 else
12580 maxlen = len;
12582 if (tree_int_cst_lt (size, maxlen))
12583 return NULL_TREE;
12586 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12587 fn = built_in_decls[BUILT_IN_STRNCPY];
12588 if (!fn)
12589 return NULL_TREE;
12591 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12594 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12595 are the arguments to the call. */
12597 static tree
12598 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12599 tree src, tree size)
12601 tree fn;
12602 const char *p;
12604 if (!validate_arg (dest, POINTER_TYPE)
12605 || !validate_arg (src, POINTER_TYPE)
12606 || !validate_arg (size, INTEGER_TYPE))
12607 return NULL_TREE;
12609 p = c_getstr (src);
12610 /* If the SRC parameter is "", return DEST. */
12611 if (p && *p == '\0')
12612 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12614 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12615 return NULL_TREE;
12617 /* If __builtin_strcat_chk is used, assume strcat is available. */
12618 fn = built_in_decls[BUILT_IN_STRCAT];
12619 if (!fn)
12620 return NULL_TREE;
12622 return build_call_expr_loc (loc, fn, 2, dest, src);
12625 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12626 LEN, and SIZE. */
12628 static tree
12629 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12630 tree dest, tree src, tree len, tree size)
12632 tree fn;
12633 const char *p;
12635 if (!validate_arg (dest, POINTER_TYPE)
12636 || !validate_arg (src, POINTER_TYPE)
12637 || !validate_arg (size, INTEGER_TYPE)
12638 || !validate_arg (size, INTEGER_TYPE))
12639 return NULL_TREE;
12641 p = c_getstr (src);
12642 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12643 if (p && *p == '\0')
12644 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12645 else if (integer_zerop (len))
12646 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12648 if (! host_integerp (size, 1))
12649 return NULL_TREE;
12651 if (! integer_all_onesp (size))
12653 tree src_len = c_strlen (src, 1);
12654 if (src_len
12655 && host_integerp (src_len, 1)
12656 && host_integerp (len, 1)
12657 && ! tree_int_cst_lt (len, src_len))
12659 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12660 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12661 if (!fn)
12662 return NULL_TREE;
12664 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12666 return NULL_TREE;
12669 /* If __builtin_strncat_chk is used, assume strncat is available. */
12670 fn = built_in_decls[BUILT_IN_STRNCAT];
12671 if (!fn)
12672 return NULL_TREE;
12674 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12677 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12678 Return NULL_TREE if a normal call should be emitted rather than
12679 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12680 or BUILT_IN_VSPRINTF_CHK. */
12682 static tree
12683 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12684 enum built_in_function fcode)
12686 tree dest, size, len, fn, fmt, flag;
12687 const char *fmt_str;
12689 /* Verify the required arguments in the original call. */
12690 if (nargs < 4)
12691 return NULL_TREE;
12692 dest = args[0];
12693 if (!validate_arg (dest, POINTER_TYPE))
12694 return NULL_TREE;
12695 flag = args[1];
12696 if (!validate_arg (flag, INTEGER_TYPE))
12697 return NULL_TREE;
12698 size = args[2];
12699 if (!validate_arg (size, INTEGER_TYPE))
12700 return NULL_TREE;
12701 fmt = args[3];
12702 if (!validate_arg (fmt, POINTER_TYPE))
12703 return NULL_TREE;
12705 if (! host_integerp (size, 1))
12706 return NULL_TREE;
12708 len = NULL_TREE;
12710 if (!init_target_chars ())
12711 return NULL_TREE;
12713 /* Check whether the format is a literal string constant. */
12714 fmt_str = c_getstr (fmt);
12715 if (fmt_str != NULL)
12717 /* If the format doesn't contain % args or %%, we know the size. */
12718 if (strchr (fmt_str, target_percent) == 0)
12720 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12721 len = build_int_cstu (size_type_node, strlen (fmt_str));
12723 /* If the format is "%s" and first ... argument is a string literal,
12724 we know the size too. */
12725 else if (fcode == BUILT_IN_SPRINTF_CHK
12726 && strcmp (fmt_str, target_percent_s) == 0)
12728 tree arg;
12730 if (nargs == 5)
12732 arg = args[4];
12733 if (validate_arg (arg, POINTER_TYPE))
12735 len = c_strlen (arg, 1);
12736 if (! len || ! host_integerp (len, 1))
12737 len = NULL_TREE;
12743 if (! integer_all_onesp (size))
12745 if (! len || ! tree_int_cst_lt (len, size))
12746 return NULL_TREE;
12749 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12750 or if format doesn't contain % chars or is "%s". */
12751 if (! integer_zerop (flag))
12753 if (fmt_str == NULL)
12754 return NULL_TREE;
12755 if (strchr (fmt_str, target_percent) != NULL
12756 && strcmp (fmt_str, target_percent_s))
12757 return NULL_TREE;
12760 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12761 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12762 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12763 if (!fn)
12764 return NULL_TREE;
12766 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12769 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12770 a normal call should be emitted rather than expanding the function
12771 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12773 static tree
12774 fold_builtin_sprintf_chk (location_t loc, tree exp,
12775 enum built_in_function fcode)
12777 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12778 CALL_EXPR_ARGP (exp), fcode);
12781 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12782 NULL_TREE if a normal call should be emitted rather than expanding
12783 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12784 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12785 passed as second argument. */
12787 static tree
12788 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12789 tree maxlen, enum built_in_function fcode)
12791 tree dest, size, len, fn, fmt, flag;
12792 const char *fmt_str;
12794 /* Verify the required arguments in the original call. */
12795 if (nargs < 5)
12796 return NULL_TREE;
12797 dest = args[0];
12798 if (!validate_arg (dest, POINTER_TYPE))
12799 return NULL_TREE;
12800 len = args[1];
12801 if (!validate_arg (len, INTEGER_TYPE))
12802 return NULL_TREE;
12803 flag = args[2];
12804 if (!validate_arg (flag, INTEGER_TYPE))
12805 return NULL_TREE;
12806 size = args[3];
12807 if (!validate_arg (size, INTEGER_TYPE))
12808 return NULL_TREE;
12809 fmt = args[4];
12810 if (!validate_arg (fmt, POINTER_TYPE))
12811 return NULL_TREE;
12813 if (! host_integerp (size, 1))
12814 return NULL_TREE;
12816 if (! integer_all_onesp (size))
12818 if (! host_integerp (len, 1))
12820 /* If LEN is not constant, try MAXLEN too.
12821 For MAXLEN only allow optimizing into non-_ocs function
12822 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12823 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12824 return NULL_TREE;
12826 else
12827 maxlen = len;
12829 if (tree_int_cst_lt (size, maxlen))
12830 return NULL_TREE;
12833 if (!init_target_chars ())
12834 return NULL_TREE;
12836 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12837 or if format doesn't contain % chars or is "%s". */
12838 if (! integer_zerop (flag))
12840 fmt_str = c_getstr (fmt);
12841 if (fmt_str == NULL)
12842 return NULL_TREE;
12843 if (strchr (fmt_str, target_percent) != NULL
12844 && strcmp (fmt_str, target_percent_s))
12845 return NULL_TREE;
12848 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12849 available. */
12850 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12851 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12852 if (!fn)
12853 return NULL_TREE;
12855 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12858 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12859 a normal call should be emitted rather than expanding the function
12860 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12861 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12862 passed as second argument. */
12864 tree
12865 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12866 enum built_in_function fcode)
12868 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12869 CALL_EXPR_ARGP (exp), maxlen, fcode);
12872 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12873 FMT and ARG are the arguments to the call; we don't fold cases with
12874 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12876 Return NULL_TREE if no simplification was possible, otherwise return the
12877 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12878 code of the function to be simplified. */
12880 static tree
12881 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12882 tree arg, bool ignore,
12883 enum built_in_function fcode)
12885 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12886 const char *fmt_str = NULL;
12888 /* If the return value is used, don't do the transformation. */
12889 if (! ignore)
12890 return NULL_TREE;
12892 /* Verify the required arguments in the original call. */
12893 if (!validate_arg (fmt, POINTER_TYPE))
12894 return NULL_TREE;
12896 /* Check whether the format is a literal string constant. */
12897 fmt_str = c_getstr (fmt);
12898 if (fmt_str == NULL)
12899 return NULL_TREE;
12901 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12903 /* If we're using an unlocked function, assume the other
12904 unlocked functions exist explicitly. */
12905 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12906 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12908 else
12910 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12911 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12914 if (!init_target_chars ())
12915 return NULL_TREE;
12917 if (strcmp (fmt_str, target_percent_s) == 0
12918 || strchr (fmt_str, target_percent) == NULL)
12920 const char *str;
12922 if (strcmp (fmt_str, target_percent_s) == 0)
12924 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12925 return NULL_TREE;
12927 if (!arg || !validate_arg (arg, POINTER_TYPE))
12928 return NULL_TREE;
12930 str = c_getstr (arg);
12931 if (str == NULL)
12932 return NULL_TREE;
12934 else
12936 /* The format specifier doesn't contain any '%' characters. */
12937 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12938 && arg)
12939 return NULL_TREE;
12940 str = fmt_str;
12943 /* If the string was "", printf does nothing. */
12944 if (str[0] == '\0')
12945 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12947 /* If the string has length of 1, call putchar. */
12948 if (str[1] == '\0')
12950 /* Given printf("c"), (where c is any one character,)
12951 convert "c"[0] to an int and pass that to the replacement
12952 function. */
12953 newarg = build_int_cst (integer_type_node, str[0]);
12954 if (fn_putchar)
12955 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12957 else
12959 /* If the string was "string\n", call puts("string"). */
12960 size_t len = strlen (str);
12961 if ((unsigned char)str[len - 1] == target_newline
12962 && (size_t) (int) len == len
12963 && (int) len > 0)
12965 char *newstr;
12966 tree offset_node, string_cst;
12968 /* Create a NUL-terminated string that's one char shorter
12969 than the original, stripping off the trailing '\n'. */
12970 newarg = build_string_literal (len, str);
12971 string_cst = string_constant (newarg, &offset_node);
12972 gcc_checking_assert (string_cst
12973 && (TREE_STRING_LENGTH (string_cst)
12974 == (int) len)
12975 && integer_zerop (offset_node)
12976 && (unsigned char)
12977 TREE_STRING_POINTER (string_cst)[len - 1]
12978 == target_newline);
12979 /* build_string_literal creates a new STRING_CST,
12980 modify it in place to avoid double copying. */
12981 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
12982 newstr[len - 1] = '\0';
12983 if (fn_puts)
12984 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12986 else
12987 /* We'd like to arrange to call fputs(string,stdout) here,
12988 but we need stdout and don't have a way to get it yet. */
12989 return NULL_TREE;
12993 /* The other optimizations can be done only on the non-va_list variants. */
12994 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12995 return NULL_TREE;
12997 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12998 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13000 if (!arg || !validate_arg (arg, POINTER_TYPE))
13001 return NULL_TREE;
13002 if (fn_puts)
13003 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13006 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13007 else if (strcmp (fmt_str, target_percent_c) == 0)
13009 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13010 return NULL_TREE;
13011 if (fn_putchar)
13012 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13015 if (!call)
13016 return NULL_TREE;
13018 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13021 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13022 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13023 more than 3 arguments, and ARG may be null in the 2-argument case.
13025 Return NULL_TREE if no simplification was possible, otherwise return the
13026 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13027 code of the function to be simplified. */
13029 static tree
13030 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13031 tree fmt, tree arg, bool ignore,
13032 enum built_in_function fcode)
13034 tree fn_fputc, fn_fputs, call = NULL_TREE;
13035 const char *fmt_str = NULL;
13037 /* If the return value is used, don't do the transformation. */
13038 if (! ignore)
13039 return NULL_TREE;
13041 /* Verify the required arguments in the original call. */
13042 if (!validate_arg (fp, POINTER_TYPE))
13043 return NULL_TREE;
13044 if (!validate_arg (fmt, POINTER_TYPE))
13045 return NULL_TREE;
13047 /* Check whether the format is a literal string constant. */
13048 fmt_str = c_getstr (fmt);
13049 if (fmt_str == NULL)
13050 return NULL_TREE;
13052 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13054 /* If we're using an unlocked function, assume the other
13055 unlocked functions exist explicitly. */
13056 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
13057 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
13059 else
13061 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
13062 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
13065 if (!init_target_chars ())
13066 return NULL_TREE;
13068 /* If the format doesn't contain % args or %%, use strcpy. */
13069 if (strchr (fmt_str, target_percent) == NULL)
13071 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13072 && arg)
13073 return NULL_TREE;
13075 /* If the format specifier was "", fprintf does nothing. */
13076 if (fmt_str[0] == '\0')
13078 /* If FP has side-effects, just wait until gimplification is
13079 done. */
13080 if (TREE_SIDE_EFFECTS (fp))
13081 return NULL_TREE;
13083 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13086 /* When "string" doesn't contain %, replace all cases of
13087 fprintf (fp, string) with fputs (string, fp). The fputs
13088 builtin will take care of special cases like length == 1. */
13089 if (fn_fputs)
13090 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13093 /* The other optimizations can be done only on the non-va_list variants. */
13094 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13095 return NULL_TREE;
13097 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13098 else if (strcmp (fmt_str, target_percent_s) == 0)
13100 if (!arg || !validate_arg (arg, POINTER_TYPE))
13101 return NULL_TREE;
13102 if (fn_fputs)
13103 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13106 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13107 else if (strcmp (fmt_str, target_percent_c) == 0)
13109 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13110 return NULL_TREE;
13111 if (fn_fputc)
13112 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13115 if (!call)
13116 return NULL_TREE;
13117 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13120 /* Initialize format string characters in the target charset. */
13122 static bool
13123 init_target_chars (void)
13125 static bool init;
13126 if (!init)
13128 target_newline = lang_hooks.to_target_charset ('\n');
13129 target_percent = lang_hooks.to_target_charset ('%');
13130 target_c = lang_hooks.to_target_charset ('c');
13131 target_s = lang_hooks.to_target_charset ('s');
13132 if (target_newline == 0 || target_percent == 0 || target_c == 0
13133 || target_s == 0)
13134 return false;
13136 target_percent_c[0] = target_percent;
13137 target_percent_c[1] = target_c;
13138 target_percent_c[2] = '\0';
13140 target_percent_s[0] = target_percent;
13141 target_percent_s[1] = target_s;
13142 target_percent_s[2] = '\0';
13144 target_percent_s_newline[0] = target_percent;
13145 target_percent_s_newline[1] = target_s;
13146 target_percent_s_newline[2] = target_newline;
13147 target_percent_s_newline[3] = '\0';
13149 init = true;
13151 return true;
13154 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13155 and no overflow/underflow occurred. INEXACT is true if M was not
13156 exactly calculated. TYPE is the tree type for the result. This
13157 function assumes that you cleared the MPFR flags and then
13158 calculated M to see if anything subsequently set a flag prior to
13159 entering this function. Return NULL_TREE if any checks fail. */
13161 static tree
13162 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13164 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13165 overflow/underflow occurred. If -frounding-math, proceed iff the
13166 result of calling FUNC was exact. */
13167 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13168 && (!flag_rounding_math || !inexact))
13170 REAL_VALUE_TYPE rr;
13172 real_from_mpfr (&rr, m, type, GMP_RNDN);
13173 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13174 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13175 but the mpft_t is not, then we underflowed in the
13176 conversion. */
13177 if (real_isfinite (&rr)
13178 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13180 REAL_VALUE_TYPE rmode;
13182 real_convert (&rmode, TYPE_MODE (type), &rr);
13183 /* Proceed iff the specified mode can hold the value. */
13184 if (real_identical (&rmode, &rr))
13185 return build_real (type, rmode);
13188 return NULL_TREE;
13191 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13192 number and no overflow/underflow occurred. INEXACT is true if M
13193 was not exactly calculated. TYPE is the tree type for the result.
13194 This function assumes that you cleared the MPFR flags and then
13195 calculated M to see if anything subsequently set a flag prior to
13196 entering this function. Return NULL_TREE if any checks fail, if
13197 FORCE_CONVERT is true, then bypass the checks. */
13199 static tree
13200 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13202 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13203 overflow/underflow occurred. If -frounding-math, proceed iff the
13204 result of calling FUNC was exact. */
13205 if (force_convert
13206 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13207 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13208 && (!flag_rounding_math || !inexact)))
13210 REAL_VALUE_TYPE re, im;
13212 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13213 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13214 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13215 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13216 but the mpft_t is not, then we underflowed in the
13217 conversion. */
13218 if (force_convert
13219 || (real_isfinite (&re) && real_isfinite (&im)
13220 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13221 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13223 REAL_VALUE_TYPE re_mode, im_mode;
13225 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13226 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13227 /* Proceed iff the specified mode can hold the value. */
13228 if (force_convert
13229 || (real_identical (&re_mode, &re)
13230 && real_identical (&im_mode, &im)))
13231 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13232 build_real (TREE_TYPE (type), im_mode));
13235 return NULL_TREE;
13238 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13239 FUNC on it and return the resulting value as a tree with type TYPE.
13240 If MIN and/or MAX are not NULL, then the supplied ARG must be
13241 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13242 acceptable values, otherwise they are not. The mpfr precision is
13243 set to the precision of TYPE. We assume that function FUNC returns
13244 zero if the result could be calculated exactly within the requested
13245 precision. */
13247 static tree
13248 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13249 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13250 bool inclusive)
13252 tree result = NULL_TREE;
13254 STRIP_NOPS (arg);
13256 /* To proceed, MPFR must exactly represent the target floating point
13257 format, which only happens when the target base equals two. */
13258 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13259 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13261 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13263 if (real_isfinite (ra)
13264 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13265 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13267 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13268 const int prec = fmt->p;
13269 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13270 int inexact;
13271 mpfr_t m;
13273 mpfr_init2 (m, prec);
13274 mpfr_from_real (m, ra, GMP_RNDN);
13275 mpfr_clear_flags ();
13276 inexact = func (m, m, rnd);
13277 result = do_mpfr_ckconv (m, type, inexact);
13278 mpfr_clear (m);
13282 return result;
13285 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13286 FUNC on it and return the resulting value as a tree with type TYPE.
13287 The mpfr precision is set to the precision of TYPE. We assume that
13288 function FUNC returns zero if the result could be calculated
13289 exactly within the requested precision. */
13291 static tree
13292 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13293 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13295 tree result = NULL_TREE;
13297 STRIP_NOPS (arg1);
13298 STRIP_NOPS (arg2);
13300 /* To proceed, MPFR must exactly represent the target floating point
13301 format, which only happens when the target base equals two. */
13302 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13303 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13304 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13306 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13307 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13309 if (real_isfinite (ra1) && real_isfinite (ra2))
13311 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13312 const int prec = fmt->p;
13313 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13314 int inexact;
13315 mpfr_t m1, m2;
13317 mpfr_inits2 (prec, m1, m2, NULL);
13318 mpfr_from_real (m1, ra1, GMP_RNDN);
13319 mpfr_from_real (m2, ra2, GMP_RNDN);
13320 mpfr_clear_flags ();
13321 inexact = func (m1, m1, m2, rnd);
13322 result = do_mpfr_ckconv (m1, type, inexact);
13323 mpfr_clears (m1, m2, NULL);
13327 return result;
13330 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13331 FUNC on it and return the resulting value as a tree with type TYPE.
13332 The mpfr precision is set to the precision of TYPE. We assume that
13333 function FUNC returns zero if the result could be calculated
13334 exactly within the requested precision. */
13336 static tree
13337 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13338 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13340 tree result = NULL_TREE;
13342 STRIP_NOPS (arg1);
13343 STRIP_NOPS (arg2);
13344 STRIP_NOPS (arg3);
13346 /* To proceed, MPFR must exactly represent the target floating point
13347 format, which only happens when the target base equals two. */
13348 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13349 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13350 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13351 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13353 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13354 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13355 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13357 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13359 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13360 const int prec = fmt->p;
13361 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13362 int inexact;
13363 mpfr_t m1, m2, m3;
13365 mpfr_inits2 (prec, m1, m2, m3, NULL);
13366 mpfr_from_real (m1, ra1, GMP_RNDN);
13367 mpfr_from_real (m2, ra2, GMP_RNDN);
13368 mpfr_from_real (m3, ra3, GMP_RNDN);
13369 mpfr_clear_flags ();
13370 inexact = func (m1, m1, m2, m3, rnd);
13371 result = do_mpfr_ckconv (m1, type, inexact);
13372 mpfr_clears (m1, m2, m3, NULL);
13376 return result;
13379 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13380 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13381 If ARG_SINP and ARG_COSP are NULL then the result is returned
13382 as a complex value.
13383 The type is taken from the type of ARG and is used for setting the
13384 precision of the calculation and results. */
13386 static tree
13387 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13389 tree const type = TREE_TYPE (arg);
13390 tree result = NULL_TREE;
13392 STRIP_NOPS (arg);
13394 /* To proceed, MPFR must exactly represent the target floating point
13395 format, which only happens when the target base equals two. */
13396 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13397 && TREE_CODE (arg) == REAL_CST
13398 && !TREE_OVERFLOW (arg))
13400 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13402 if (real_isfinite (ra))
13404 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13405 const int prec = fmt->p;
13406 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13407 tree result_s, result_c;
13408 int inexact;
13409 mpfr_t m, ms, mc;
13411 mpfr_inits2 (prec, m, ms, mc, NULL);
13412 mpfr_from_real (m, ra, GMP_RNDN);
13413 mpfr_clear_flags ();
13414 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13415 result_s = do_mpfr_ckconv (ms, type, inexact);
13416 result_c = do_mpfr_ckconv (mc, type, inexact);
13417 mpfr_clears (m, ms, mc, NULL);
13418 if (result_s && result_c)
13420 /* If we are to return in a complex value do so. */
13421 if (!arg_sinp && !arg_cosp)
13422 return build_complex (build_complex_type (type),
13423 result_c, result_s);
13425 /* Dereference the sin/cos pointer arguments. */
13426 arg_sinp = build_fold_indirect_ref (arg_sinp);
13427 arg_cosp = build_fold_indirect_ref (arg_cosp);
13428 /* Proceed if valid pointer type were passed in. */
13429 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13430 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13432 /* Set the values. */
13433 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13434 result_s);
13435 TREE_SIDE_EFFECTS (result_s) = 1;
13436 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13437 result_c);
13438 TREE_SIDE_EFFECTS (result_c) = 1;
13439 /* Combine the assignments into a compound expr. */
13440 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13441 result_s, result_c));
13446 return result;
13449 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13450 two-argument mpfr order N Bessel function FUNC on them and return
13451 the resulting value as a tree with type TYPE. The mpfr precision
13452 is set to the precision of TYPE. We assume that function FUNC
13453 returns zero if the result could be calculated exactly within the
13454 requested precision. */
13455 static tree
13456 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13457 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13458 const REAL_VALUE_TYPE *min, bool inclusive)
13460 tree result = NULL_TREE;
13462 STRIP_NOPS (arg1);
13463 STRIP_NOPS (arg2);
13465 /* To proceed, MPFR must exactly represent the target floating point
13466 format, which only happens when the target base equals two. */
13467 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13468 && host_integerp (arg1, 0)
13469 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13471 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13472 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13474 if (n == (long)n
13475 && real_isfinite (ra)
13476 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13478 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13479 const int prec = fmt->p;
13480 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13481 int inexact;
13482 mpfr_t m;
13484 mpfr_init2 (m, prec);
13485 mpfr_from_real (m, ra, GMP_RNDN);
13486 mpfr_clear_flags ();
13487 inexact = func (m, n, m, rnd);
13488 result = do_mpfr_ckconv (m, type, inexact);
13489 mpfr_clear (m);
13493 return result;
13496 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13497 the pointer *(ARG_QUO) and return the result. The type is taken
13498 from the type of ARG0 and is used for setting the precision of the
13499 calculation and results. */
13501 static tree
13502 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13504 tree const type = TREE_TYPE (arg0);
13505 tree result = NULL_TREE;
13507 STRIP_NOPS (arg0);
13508 STRIP_NOPS (arg1);
13510 /* To proceed, MPFR must exactly represent the target floating point
13511 format, which only happens when the target base equals two. */
13512 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13513 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13514 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13516 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13517 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13519 if (real_isfinite (ra0) && real_isfinite (ra1))
13521 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13522 const int prec = fmt->p;
13523 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13524 tree result_rem;
13525 long integer_quo;
13526 mpfr_t m0, m1;
13528 mpfr_inits2 (prec, m0, m1, NULL);
13529 mpfr_from_real (m0, ra0, GMP_RNDN);
13530 mpfr_from_real (m1, ra1, GMP_RNDN);
13531 mpfr_clear_flags ();
13532 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13533 /* Remquo is independent of the rounding mode, so pass
13534 inexact=0 to do_mpfr_ckconv(). */
13535 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13536 mpfr_clears (m0, m1, NULL);
13537 if (result_rem)
13539 /* MPFR calculates quo in the host's long so it may
13540 return more bits in quo than the target int can hold
13541 if sizeof(host long) > sizeof(target int). This can
13542 happen even for native compilers in LP64 mode. In
13543 these cases, modulo the quo value with the largest
13544 number that the target int can hold while leaving one
13545 bit for the sign. */
13546 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13547 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13549 /* Dereference the quo pointer argument. */
13550 arg_quo = build_fold_indirect_ref (arg_quo);
13551 /* Proceed iff a valid pointer type was passed in. */
13552 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13554 /* Set the value. */
13555 tree result_quo
13556 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13557 build_int_cst (TREE_TYPE (arg_quo),
13558 integer_quo));
13559 TREE_SIDE_EFFECTS (result_quo) = 1;
13560 /* Combine the quo assignment with the rem. */
13561 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13562 result_quo, result_rem));
13567 return result;
13570 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13571 resulting value as a tree with type TYPE. The mpfr precision is
13572 set to the precision of TYPE. We assume that this mpfr function
13573 returns zero if the result could be calculated exactly within the
13574 requested precision. In addition, the integer pointer represented
13575 by ARG_SG will be dereferenced and set to the appropriate signgam
13576 (-1,1) value. */
13578 static tree
13579 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13581 tree result = NULL_TREE;
13583 STRIP_NOPS (arg);
13585 /* To proceed, MPFR must exactly represent the target floating point
13586 format, which only happens when the target base equals two. Also
13587 verify ARG is a constant and that ARG_SG is an int pointer. */
13588 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13589 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13590 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13591 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13593 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13595 /* In addition to NaN and Inf, the argument cannot be zero or a
13596 negative integer. */
13597 if (real_isfinite (ra)
13598 && ra->cl != rvc_zero
13599 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13601 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13602 const int prec = fmt->p;
13603 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13604 int inexact, sg;
13605 mpfr_t m;
13606 tree result_lg;
13608 mpfr_init2 (m, prec);
13609 mpfr_from_real (m, ra, GMP_RNDN);
13610 mpfr_clear_flags ();
13611 inexact = mpfr_lgamma (m, &sg, m, rnd);
13612 result_lg = do_mpfr_ckconv (m, type, inexact);
13613 mpfr_clear (m);
13614 if (result_lg)
13616 tree result_sg;
13618 /* Dereference the arg_sg pointer argument. */
13619 arg_sg = build_fold_indirect_ref (arg_sg);
13620 /* Assign the signgam value into *arg_sg. */
13621 result_sg = fold_build2 (MODIFY_EXPR,
13622 TREE_TYPE (arg_sg), arg_sg,
13623 build_int_cst (TREE_TYPE (arg_sg), sg));
13624 TREE_SIDE_EFFECTS (result_sg) = 1;
13625 /* Combine the signgam assignment with the lgamma result. */
13626 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13627 result_sg, result_lg));
13632 return result;
13635 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13636 function FUNC on it and return the resulting value as a tree with
13637 type TYPE. The mpfr precision is set to the precision of TYPE. We
13638 assume that function FUNC returns zero if the result could be
13639 calculated exactly within the requested precision. */
13641 static tree
13642 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13644 tree result = NULL_TREE;
13646 STRIP_NOPS (arg);
13648 /* To proceed, MPFR must exactly represent the target floating point
13649 format, which only happens when the target base equals two. */
13650 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13651 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13652 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13654 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13655 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13657 if (real_isfinite (re) && real_isfinite (im))
13659 const struct real_format *const fmt =
13660 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13661 const int prec = fmt->p;
13662 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13663 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13664 int inexact;
13665 mpc_t m;
13667 mpc_init2 (m, prec);
13668 mpfr_from_real (mpc_realref(m), re, rnd);
13669 mpfr_from_real (mpc_imagref(m), im, rnd);
13670 mpfr_clear_flags ();
13671 inexact = func (m, m, crnd);
13672 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13673 mpc_clear (m);
13677 return result;
13680 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13681 mpc function FUNC on it and return the resulting value as a tree
13682 with type TYPE. The mpfr precision is set to the precision of
13683 TYPE. We assume that function FUNC returns zero if the result
13684 could be calculated exactly within the requested precision. If
13685 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13686 in the arguments and/or results. */
13688 tree
13689 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13690 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13692 tree result = NULL_TREE;
13694 STRIP_NOPS (arg0);
13695 STRIP_NOPS (arg1);
13697 /* To proceed, MPFR must exactly represent the target floating point
13698 format, which only happens when the target base equals two. */
13699 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13700 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13701 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13702 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13703 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13705 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13706 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13707 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13708 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13710 if (do_nonfinite
13711 || (real_isfinite (re0) && real_isfinite (im0)
13712 && real_isfinite (re1) && real_isfinite (im1)))
13714 const struct real_format *const fmt =
13715 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13716 const int prec = fmt->p;
13717 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13718 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13719 int inexact;
13720 mpc_t m0, m1;
13722 mpc_init2 (m0, prec);
13723 mpc_init2 (m1, prec);
13724 mpfr_from_real (mpc_realref(m0), re0, rnd);
13725 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13726 mpfr_from_real (mpc_realref(m1), re1, rnd);
13727 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13728 mpfr_clear_flags ();
13729 inexact = func (m0, m0, m1, crnd);
13730 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13731 mpc_clear (m0);
13732 mpc_clear (m1);
13736 return result;
13739 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13740 a normal call should be emitted rather than expanding the function
13741 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13743 static tree
13744 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13746 int nargs = gimple_call_num_args (stmt);
13748 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13749 (nargs > 0
13750 ? gimple_call_arg_ptr (stmt, 0)
13751 : &error_mark_node), fcode);
13754 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13755 a normal call should be emitted rather than expanding the function
13756 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13757 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13758 passed as second argument. */
13760 tree
13761 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13762 enum built_in_function fcode)
13764 int nargs = gimple_call_num_args (stmt);
13766 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13767 (nargs > 0
13768 ? gimple_call_arg_ptr (stmt, 0)
13769 : &error_mark_node), maxlen, fcode);
13772 /* Builtins with folding operations that operate on "..." arguments
13773 need special handling; we need to store the arguments in a convenient
13774 data structure before attempting any folding. Fortunately there are
13775 only a few builtins that fall into this category. FNDECL is the
13776 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13777 result of the function call is ignored. */
13779 static tree
13780 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13781 bool ignore ATTRIBUTE_UNUSED)
13783 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13784 tree ret = NULL_TREE;
13786 switch (fcode)
13788 case BUILT_IN_SPRINTF_CHK:
13789 case BUILT_IN_VSPRINTF_CHK:
13790 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13791 break;
13793 case BUILT_IN_SNPRINTF_CHK:
13794 case BUILT_IN_VSNPRINTF_CHK:
13795 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13797 default:
13798 break;
13800 if (ret)
13802 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13803 TREE_NO_WARNING (ret) = 1;
13804 return ret;
13806 return NULL_TREE;
13809 /* A wrapper function for builtin folding that prevents warnings for
13810 "statement without effect" and the like, caused by removing the
13811 call node earlier than the warning is generated. */
13813 tree
13814 fold_call_stmt (gimple stmt, bool ignore)
13816 tree ret = NULL_TREE;
13817 tree fndecl = gimple_call_fndecl (stmt);
13818 location_t loc = gimple_location (stmt);
13819 if (fndecl
13820 && TREE_CODE (fndecl) == FUNCTION_DECL
13821 && DECL_BUILT_IN (fndecl)
13822 && !gimple_call_va_arg_pack_p (stmt))
13824 int nargs = gimple_call_num_args (stmt);
13825 tree *args = (nargs > 0
13826 ? gimple_call_arg_ptr (stmt, 0)
13827 : &error_mark_node);
13829 if (avoid_folding_inline_builtin (fndecl))
13830 return NULL_TREE;
13831 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13833 return targetm.fold_builtin (fndecl, nargs, args, ignore);
13835 else
13837 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13838 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13839 if (!ret)
13840 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13841 if (ret)
13843 /* Propagate location information from original call to
13844 expansion of builtin. Otherwise things like
13845 maybe_emit_chk_warning, that operate on the expansion
13846 of a builtin, will use the wrong location information. */
13847 if (gimple_has_location (stmt))
13849 tree realret = ret;
13850 if (TREE_CODE (ret) == NOP_EXPR)
13851 realret = TREE_OPERAND (ret, 0);
13852 if (CAN_HAVE_LOCATION_P (realret)
13853 && !EXPR_HAS_LOCATION (realret))
13854 SET_EXPR_LOCATION (realret, loc);
13855 return realret;
13857 return ret;
13861 return NULL_TREE;
13864 /* Look up the function in built_in_decls that corresponds to DECL
13865 and set ASMSPEC as its user assembler name. DECL must be a
13866 function decl that declares a builtin. */
13868 void
13869 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13871 tree builtin;
13872 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13873 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13874 && asmspec != 0);
13876 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13877 set_user_assembler_name (builtin, asmspec);
13878 switch (DECL_FUNCTION_CODE (decl))
13880 case BUILT_IN_MEMCPY:
13881 init_block_move_fn (asmspec);
13882 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13883 break;
13884 case BUILT_IN_MEMSET:
13885 init_block_clear_fn (asmspec);
13886 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13887 break;
13888 case BUILT_IN_MEMMOVE:
13889 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13890 break;
13891 case BUILT_IN_MEMCMP:
13892 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13893 break;
13894 case BUILT_IN_ABORT:
13895 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13896 break;
13897 case BUILT_IN_FFS:
13898 if (INT_TYPE_SIZE < BITS_PER_WORD)
13900 set_user_assembler_libfunc ("ffs", asmspec);
13901 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13902 MODE_INT, 0), "ffs");
13904 break;
13905 default:
13906 break;
13910 /* Return true if DECL is a builtin that expands to a constant or similarly
13911 simple code. */
13912 bool
13913 is_simple_builtin (tree decl)
13915 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13916 switch (DECL_FUNCTION_CODE (decl))
13918 /* Builtins that expand to constants. */
13919 case BUILT_IN_CONSTANT_P:
13920 case BUILT_IN_EXPECT:
13921 case BUILT_IN_OBJECT_SIZE:
13922 case BUILT_IN_UNREACHABLE:
13923 /* Simple register moves or loads from stack. */
13924 case BUILT_IN_RETURN_ADDRESS:
13925 case BUILT_IN_EXTRACT_RETURN_ADDR:
13926 case BUILT_IN_FROB_RETURN_ADDR:
13927 case BUILT_IN_RETURN:
13928 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13929 case BUILT_IN_FRAME_ADDRESS:
13930 case BUILT_IN_VA_END:
13931 case BUILT_IN_STACK_SAVE:
13932 case BUILT_IN_STACK_RESTORE:
13933 /* Exception state returns or moves registers around. */
13934 case BUILT_IN_EH_FILTER:
13935 case BUILT_IN_EH_POINTER:
13936 case BUILT_IN_EH_COPY_VALUES:
13937 return true;
13939 default:
13940 return false;
13943 return false;
13946 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13947 most probably expanded inline into reasonably simple code. This is a
13948 superset of is_simple_builtin. */
13949 bool
13950 is_inexpensive_builtin (tree decl)
13952 if (!decl)
13953 return false;
13954 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13955 return true;
13956 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13957 switch (DECL_FUNCTION_CODE (decl))
13959 case BUILT_IN_ABS:
13960 case BUILT_IN_ALLOCA:
13961 case BUILT_IN_BSWAP32:
13962 case BUILT_IN_BSWAP64:
13963 case BUILT_IN_CLZ:
13964 case BUILT_IN_CLZIMAX:
13965 case BUILT_IN_CLZL:
13966 case BUILT_IN_CLZLL:
13967 case BUILT_IN_CTZ:
13968 case BUILT_IN_CTZIMAX:
13969 case BUILT_IN_CTZL:
13970 case BUILT_IN_CTZLL:
13971 case BUILT_IN_FFS:
13972 case BUILT_IN_FFSIMAX:
13973 case BUILT_IN_FFSL:
13974 case BUILT_IN_FFSLL:
13975 case BUILT_IN_IMAXABS:
13976 case BUILT_IN_FINITE:
13977 case BUILT_IN_FINITEF:
13978 case BUILT_IN_FINITEL:
13979 case BUILT_IN_FINITED32:
13980 case BUILT_IN_FINITED64:
13981 case BUILT_IN_FINITED128:
13982 case BUILT_IN_FPCLASSIFY:
13983 case BUILT_IN_ISFINITE:
13984 case BUILT_IN_ISINF_SIGN:
13985 case BUILT_IN_ISINF:
13986 case BUILT_IN_ISINFF:
13987 case BUILT_IN_ISINFL:
13988 case BUILT_IN_ISINFD32:
13989 case BUILT_IN_ISINFD64:
13990 case BUILT_IN_ISINFD128:
13991 case BUILT_IN_ISNAN:
13992 case BUILT_IN_ISNANF:
13993 case BUILT_IN_ISNANL:
13994 case BUILT_IN_ISNAND32:
13995 case BUILT_IN_ISNAND64:
13996 case BUILT_IN_ISNAND128:
13997 case BUILT_IN_ISNORMAL:
13998 case BUILT_IN_ISGREATER:
13999 case BUILT_IN_ISGREATEREQUAL:
14000 case BUILT_IN_ISLESS:
14001 case BUILT_IN_ISLESSEQUAL:
14002 case BUILT_IN_ISLESSGREATER:
14003 case BUILT_IN_ISUNORDERED:
14004 case BUILT_IN_VA_ARG_PACK:
14005 case BUILT_IN_VA_ARG_PACK_LEN:
14006 case BUILT_IN_VA_COPY:
14007 case BUILT_IN_TRAP:
14008 case BUILT_IN_SAVEREGS:
14009 case BUILT_IN_POPCOUNTL:
14010 case BUILT_IN_POPCOUNTLL:
14011 case BUILT_IN_POPCOUNTIMAX:
14012 case BUILT_IN_POPCOUNT:
14013 case BUILT_IN_PARITYL:
14014 case BUILT_IN_PARITYLL:
14015 case BUILT_IN_PARITYIMAX:
14016 case BUILT_IN_PARITY:
14017 case BUILT_IN_LABS:
14018 case BUILT_IN_LLABS:
14019 case BUILT_IN_PREFETCH:
14020 return true;
14022 default:
14023 return is_simple_builtin (decl);
14026 return false;