Revert wrong checkin
[official-gcc.git] / gcc / builtins.c
bloba23237dd94ad30b4516437c65e4deef28483e1bf
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 if (!POINTER_TYPE_P (TREE_TYPE (src))
8513 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8514 return NULL_TREE;
8515 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8516 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8518 tree tem = TREE_OPERAND (src, 0);
8519 STRIP_NOPS (tem);
8520 if (tem != TREE_OPERAND (src, 0))
8521 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8523 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8525 tree tem = TREE_OPERAND (dest, 0);
8526 STRIP_NOPS (tem);
8527 if (tem != TREE_OPERAND (dest, 0))
8528 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8530 srctype = TREE_TYPE (TREE_TYPE (src));
8531 if (TREE_CODE (srctype) == ARRAY_TYPE
8532 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8534 srctype = TREE_TYPE (srctype);
8535 STRIP_NOPS (src);
8536 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8538 desttype = TREE_TYPE (TREE_TYPE (dest));
8539 if (TREE_CODE (desttype) == ARRAY_TYPE
8540 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8542 desttype = TREE_TYPE (desttype);
8543 STRIP_NOPS (dest);
8544 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8546 if (TREE_ADDRESSABLE (srctype)
8547 || TREE_ADDRESSABLE (desttype))
8548 return NULL_TREE;
8550 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8551 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8552 if (dest_align < TYPE_ALIGN (desttype)
8553 || src_align < TYPE_ALIGN (srctype))
8554 return NULL_TREE;
8556 if (!ignore)
8557 dest = builtin_save_expr (dest);
8559 /* Build accesses at offset zero with a ref-all character type. */
8560 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8561 ptr_mode, true), 0);
8563 destvar = dest;
8564 STRIP_NOPS (destvar);
8565 if (TREE_CODE (destvar) == ADDR_EXPR
8566 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8567 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8568 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8569 else
8570 destvar = NULL_TREE;
8572 srcvar = src;
8573 STRIP_NOPS (srcvar);
8574 if (TREE_CODE (srcvar) == ADDR_EXPR
8575 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8576 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8578 if (!destvar
8579 || src_align >= TYPE_ALIGN (desttype))
8580 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8581 srcvar, off0);
8582 else if (!STRICT_ALIGNMENT)
8584 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8585 src_align);
8586 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8588 else
8589 srcvar = NULL_TREE;
8591 else
8592 srcvar = NULL_TREE;
8594 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8595 return NULL_TREE;
8597 if (srcvar == NULL_TREE)
8599 STRIP_NOPS (src);
8600 if (src_align >= TYPE_ALIGN (desttype))
8601 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8602 else
8604 if (STRICT_ALIGNMENT)
8605 return NULL_TREE;
8606 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8607 src_align);
8608 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8611 else if (destvar == NULL_TREE)
8613 STRIP_NOPS (dest);
8614 if (dest_align >= TYPE_ALIGN (srctype))
8615 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8616 else
8618 if (STRICT_ALIGNMENT)
8619 return NULL_TREE;
8620 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8621 dest_align);
8622 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8626 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8629 if (ignore)
8630 return expr;
8632 if (endp == 0 || endp == 3)
8633 return omit_one_operand_loc (loc, type, dest, expr);
8635 if (expr == len)
8636 expr = NULL_TREE;
8638 if (endp == 2)
8639 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8640 ssize_int (1));
8642 len = fold_convert_loc (loc, sizetype, len);
8643 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8644 dest = fold_convert_loc (loc, type, dest);
8645 if (expr)
8646 dest = omit_one_operand_loc (loc, type, dest, expr);
8647 return dest;
8650 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8651 If LEN is not NULL, it represents the length of the string to be
8652 copied. Return NULL_TREE if no simplification can be made. */
8654 tree
8655 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8657 tree fn;
8659 if (!validate_arg (dest, POINTER_TYPE)
8660 || !validate_arg (src, POINTER_TYPE))
8661 return NULL_TREE;
8663 /* If SRC and DEST are the same (and not volatile), return DEST. */
8664 if (operand_equal_p (src, dest, 0))
8665 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8667 if (optimize_function_for_size_p (cfun))
8668 return NULL_TREE;
8670 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8671 if (!fn)
8672 return NULL_TREE;
8674 if (!len)
8676 len = c_strlen (src, 1);
8677 if (! len || TREE_SIDE_EFFECTS (len))
8678 return NULL_TREE;
8681 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8682 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8683 build_call_expr_loc (loc, fn, 3, dest, src, len));
8686 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8687 Return NULL_TREE if no simplification can be made. */
8689 static tree
8690 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8692 tree fn, len, lenp1, call, type;
8694 if (!validate_arg (dest, POINTER_TYPE)
8695 || !validate_arg (src, POINTER_TYPE))
8696 return NULL_TREE;
8698 len = c_strlen (src, 1);
8699 if (!len
8700 || TREE_CODE (len) != INTEGER_CST)
8701 return NULL_TREE;
8703 if (optimize_function_for_size_p (cfun)
8704 /* If length is zero it's small enough. */
8705 && !integer_zerop (len))
8706 return NULL_TREE;
8708 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8709 if (!fn)
8710 return NULL_TREE;
8712 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8713 /* We use dest twice in building our expression. Save it from
8714 multiple expansions. */
8715 dest = builtin_save_expr (dest);
8716 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8718 type = TREE_TYPE (TREE_TYPE (fndecl));
8719 len = fold_convert_loc (loc, sizetype, len);
8720 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8721 dest = fold_convert_loc (loc, type, dest);
8722 dest = omit_one_operand_loc (loc, type, dest, call);
8723 return dest;
8726 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8727 If SLEN is not NULL, it represents the length of the source string.
8728 Return NULL_TREE if no simplification can be made. */
8730 tree
8731 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8732 tree src, tree len, tree slen)
8734 tree fn;
8736 if (!validate_arg (dest, POINTER_TYPE)
8737 || !validate_arg (src, POINTER_TYPE)
8738 || !validate_arg (len, INTEGER_TYPE))
8739 return NULL_TREE;
8741 /* If the LEN parameter is zero, return DEST. */
8742 if (integer_zerop (len))
8743 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8745 /* We can't compare slen with len as constants below if len is not a
8746 constant. */
8747 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8748 return NULL_TREE;
8750 if (!slen)
8751 slen = c_strlen (src, 1);
8753 /* Now, we must be passed a constant src ptr parameter. */
8754 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8755 return NULL_TREE;
8757 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8759 /* We do not support simplification of this case, though we do
8760 support it when expanding trees into RTL. */
8761 /* FIXME: generate a call to __builtin_memset. */
8762 if (tree_int_cst_lt (slen, len))
8763 return NULL_TREE;
8765 /* OK transform into builtin memcpy. */
8766 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8767 if (!fn)
8768 return NULL_TREE;
8769 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8770 build_call_expr_loc (loc, fn, 3, dest, src, len));
8773 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8774 arguments to the call, and TYPE is its return type.
8775 Return NULL_TREE if no simplification can be made. */
8777 static tree
8778 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8780 if (!validate_arg (arg1, POINTER_TYPE)
8781 || !validate_arg (arg2, INTEGER_TYPE)
8782 || !validate_arg (len, INTEGER_TYPE))
8783 return NULL_TREE;
8784 else
8786 const char *p1;
8788 if (TREE_CODE (arg2) != INTEGER_CST
8789 || !host_integerp (len, 1))
8790 return NULL_TREE;
8792 p1 = c_getstr (arg1);
8793 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8795 char c;
8796 const char *r;
8797 tree tem;
8799 if (target_char_cast (arg2, &c))
8800 return NULL_TREE;
8802 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8804 if (r == NULL)
8805 return build_int_cst (TREE_TYPE (arg1), 0);
8807 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8808 size_int (r - p1));
8809 return fold_convert_loc (loc, type, tem);
8811 return NULL_TREE;
8815 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8816 Return NULL_TREE if no simplification can be made. */
8818 static tree
8819 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8821 const char *p1, *p2;
8823 if (!validate_arg (arg1, POINTER_TYPE)
8824 || !validate_arg (arg2, POINTER_TYPE)
8825 || !validate_arg (len, INTEGER_TYPE))
8826 return NULL_TREE;
8828 /* If the LEN parameter is zero, return zero. */
8829 if (integer_zerop (len))
8830 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8831 arg1, arg2);
8833 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8834 if (operand_equal_p (arg1, arg2, 0))
8835 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8837 p1 = c_getstr (arg1);
8838 p2 = c_getstr (arg2);
8840 /* If all arguments are constant, and the value of len is not greater
8841 than the lengths of arg1 and arg2, evaluate at compile-time. */
8842 if (host_integerp (len, 1) && p1 && p2
8843 && compare_tree_int (len, strlen (p1) + 1) <= 0
8844 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8846 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8848 if (r > 0)
8849 return integer_one_node;
8850 else if (r < 0)
8851 return integer_minus_one_node;
8852 else
8853 return integer_zero_node;
8856 /* If len parameter is one, return an expression corresponding to
8857 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8858 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8860 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8861 tree cst_uchar_ptr_node
8862 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8864 tree ind1
8865 = fold_convert_loc (loc, integer_type_node,
8866 build1 (INDIRECT_REF, cst_uchar_node,
8867 fold_convert_loc (loc,
8868 cst_uchar_ptr_node,
8869 arg1)));
8870 tree ind2
8871 = fold_convert_loc (loc, integer_type_node,
8872 build1 (INDIRECT_REF, cst_uchar_node,
8873 fold_convert_loc (loc,
8874 cst_uchar_ptr_node,
8875 arg2)));
8876 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8879 return NULL_TREE;
8882 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8883 Return NULL_TREE if no simplification can be made. */
8885 static tree
8886 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8888 const char *p1, *p2;
8890 if (!validate_arg (arg1, POINTER_TYPE)
8891 || !validate_arg (arg2, POINTER_TYPE))
8892 return NULL_TREE;
8894 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8895 if (operand_equal_p (arg1, arg2, 0))
8896 return integer_zero_node;
8898 p1 = c_getstr (arg1);
8899 p2 = c_getstr (arg2);
8901 if (p1 && p2)
8903 const int i = strcmp (p1, p2);
8904 if (i < 0)
8905 return integer_minus_one_node;
8906 else if (i > 0)
8907 return integer_one_node;
8908 else
8909 return integer_zero_node;
8912 /* If the second arg is "", return *(const unsigned char*)arg1. */
8913 if (p2 && *p2 == '\0')
8915 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8916 tree cst_uchar_ptr_node
8917 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8919 return fold_convert_loc (loc, integer_type_node,
8920 build1 (INDIRECT_REF, cst_uchar_node,
8921 fold_convert_loc (loc,
8922 cst_uchar_ptr_node,
8923 arg1)));
8926 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8927 if (p1 && *p1 == '\0')
8929 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8930 tree cst_uchar_ptr_node
8931 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8933 tree temp
8934 = fold_convert_loc (loc, integer_type_node,
8935 build1 (INDIRECT_REF, cst_uchar_node,
8936 fold_convert_loc (loc,
8937 cst_uchar_ptr_node,
8938 arg2)));
8939 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8942 return NULL_TREE;
8945 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8946 Return NULL_TREE if no simplification can be made. */
8948 static tree
8949 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8951 const char *p1, *p2;
8953 if (!validate_arg (arg1, POINTER_TYPE)
8954 || !validate_arg (arg2, POINTER_TYPE)
8955 || !validate_arg (len, INTEGER_TYPE))
8956 return NULL_TREE;
8958 /* If the LEN parameter is zero, return zero. */
8959 if (integer_zerop (len))
8960 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8961 arg1, arg2);
8963 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8964 if (operand_equal_p (arg1, arg2, 0))
8965 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8967 p1 = c_getstr (arg1);
8968 p2 = c_getstr (arg2);
8970 if (host_integerp (len, 1) && p1 && p2)
8972 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8973 if (i > 0)
8974 return integer_one_node;
8975 else if (i < 0)
8976 return integer_minus_one_node;
8977 else
8978 return integer_zero_node;
8981 /* If the second arg is "", and the length is greater than zero,
8982 return *(const unsigned char*)arg1. */
8983 if (p2 && *p2 == '\0'
8984 && TREE_CODE (len) == INTEGER_CST
8985 && tree_int_cst_sgn (len) == 1)
8987 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8988 tree cst_uchar_ptr_node
8989 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8991 return fold_convert_loc (loc, integer_type_node,
8992 build1 (INDIRECT_REF, cst_uchar_node,
8993 fold_convert_loc (loc,
8994 cst_uchar_ptr_node,
8995 arg1)));
8998 /* If the first arg is "", and the length is greater than zero,
8999 return -*(const unsigned char*)arg2. */
9000 if (p1 && *p1 == '\0'
9001 && TREE_CODE (len) == INTEGER_CST
9002 && tree_int_cst_sgn (len) == 1)
9004 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9005 tree cst_uchar_ptr_node
9006 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9008 tree temp = fold_convert_loc (loc, integer_type_node,
9009 build1 (INDIRECT_REF, cst_uchar_node,
9010 fold_convert_loc (loc,
9011 cst_uchar_ptr_node,
9012 arg2)));
9013 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9016 /* If len parameter is one, return an expression corresponding to
9017 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9018 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9020 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9021 tree cst_uchar_ptr_node
9022 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9024 tree ind1 = fold_convert_loc (loc, integer_type_node,
9025 build1 (INDIRECT_REF, cst_uchar_node,
9026 fold_convert_loc (loc,
9027 cst_uchar_ptr_node,
9028 arg1)));
9029 tree ind2 = fold_convert_loc (loc, integer_type_node,
9030 build1 (INDIRECT_REF, cst_uchar_node,
9031 fold_convert_loc (loc,
9032 cst_uchar_ptr_node,
9033 arg2)));
9034 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9037 return NULL_TREE;
9040 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9041 ARG. Return NULL_TREE if no simplification can be made. */
9043 static tree
9044 fold_builtin_signbit (location_t loc, tree arg, tree type)
9046 if (!validate_arg (arg, REAL_TYPE))
9047 return NULL_TREE;
9049 /* If ARG is a compile-time constant, determine the result. */
9050 if (TREE_CODE (arg) == REAL_CST
9051 && !TREE_OVERFLOW (arg))
9053 REAL_VALUE_TYPE c;
9055 c = TREE_REAL_CST (arg);
9056 return (REAL_VALUE_NEGATIVE (c)
9057 ? build_one_cst (type)
9058 : build_zero_cst (type));
9061 /* If ARG is non-negative, the result is always zero. */
9062 if (tree_expr_nonnegative_p (arg))
9063 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9065 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9066 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9067 return fold_build2_loc (loc, LT_EXPR, type, arg,
9068 build_real (TREE_TYPE (arg), dconst0));
9070 return NULL_TREE;
9073 /* Fold function call to builtin copysign, copysignf or copysignl with
9074 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9075 be made. */
9077 static tree
9078 fold_builtin_copysign (location_t loc, tree fndecl,
9079 tree arg1, tree arg2, tree type)
9081 tree tem;
9083 if (!validate_arg (arg1, REAL_TYPE)
9084 || !validate_arg (arg2, REAL_TYPE))
9085 return NULL_TREE;
9087 /* copysign(X,X) is X. */
9088 if (operand_equal_p (arg1, arg2, 0))
9089 return fold_convert_loc (loc, type, arg1);
9091 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9092 if (TREE_CODE (arg1) == REAL_CST
9093 && TREE_CODE (arg2) == REAL_CST
9094 && !TREE_OVERFLOW (arg1)
9095 && !TREE_OVERFLOW (arg2))
9097 REAL_VALUE_TYPE c1, c2;
9099 c1 = TREE_REAL_CST (arg1);
9100 c2 = TREE_REAL_CST (arg2);
9101 /* c1.sign := c2.sign. */
9102 real_copysign (&c1, &c2);
9103 return build_real (type, c1);
9106 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9107 Remember to evaluate Y for side-effects. */
9108 if (tree_expr_nonnegative_p (arg2))
9109 return omit_one_operand_loc (loc, type,
9110 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9111 arg2);
9113 /* Strip sign changing operations for the first argument. */
9114 tem = fold_strip_sign_ops (arg1);
9115 if (tem)
9116 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9118 return NULL_TREE;
9121 /* Fold a call to builtin isascii with argument ARG. */
9123 static tree
9124 fold_builtin_isascii (location_t loc, tree arg)
9126 if (!validate_arg (arg, INTEGER_TYPE))
9127 return NULL_TREE;
9128 else
9130 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9131 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9132 build_int_cst (integer_type_node,
9133 ~ (unsigned HOST_WIDE_INT) 0x7f));
9134 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9135 arg, integer_zero_node);
9139 /* Fold a call to builtin toascii with argument ARG. */
9141 static tree
9142 fold_builtin_toascii (location_t loc, tree arg)
9144 if (!validate_arg (arg, INTEGER_TYPE))
9145 return NULL_TREE;
9147 /* Transform toascii(c) -> (c & 0x7f). */
9148 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9149 build_int_cst (integer_type_node, 0x7f));
9152 /* Fold a call to builtin isdigit with argument ARG. */
9154 static tree
9155 fold_builtin_isdigit (location_t loc, tree arg)
9157 if (!validate_arg (arg, INTEGER_TYPE))
9158 return NULL_TREE;
9159 else
9161 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9162 /* According to the C standard, isdigit is unaffected by locale.
9163 However, it definitely is affected by the target character set. */
9164 unsigned HOST_WIDE_INT target_digit0
9165 = lang_hooks.to_target_charset ('0');
9167 if (target_digit0 == 0)
9168 return NULL_TREE;
9170 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9171 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9172 build_int_cst (unsigned_type_node, target_digit0));
9173 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9174 build_int_cst (unsigned_type_node, 9));
9178 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9180 static tree
9181 fold_builtin_fabs (location_t loc, tree arg, tree type)
9183 if (!validate_arg (arg, REAL_TYPE))
9184 return NULL_TREE;
9186 arg = fold_convert_loc (loc, type, arg);
9187 if (TREE_CODE (arg) == REAL_CST)
9188 return fold_abs_const (arg, type);
9189 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9192 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9194 static tree
9195 fold_builtin_abs (location_t loc, tree arg, tree type)
9197 if (!validate_arg (arg, INTEGER_TYPE))
9198 return NULL_TREE;
9200 arg = fold_convert_loc (loc, type, arg);
9201 if (TREE_CODE (arg) == INTEGER_CST)
9202 return fold_abs_const (arg, type);
9203 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9206 /* Fold a fma operation with arguments ARG[012]. */
9208 tree
9209 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9210 tree type, tree arg0, tree arg1, tree arg2)
9212 if (TREE_CODE (arg0) == REAL_CST
9213 && TREE_CODE (arg1) == REAL_CST
9214 && TREE_CODE (arg2) == REAL_CST)
9215 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9217 return NULL_TREE;
9220 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9222 static tree
9223 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9225 if (validate_arg (arg0, REAL_TYPE)
9226 && validate_arg(arg1, REAL_TYPE)
9227 && validate_arg(arg2, REAL_TYPE))
9229 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9230 if (tem)
9231 return tem;
9233 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9234 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9235 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9237 return NULL_TREE;
9240 /* Fold a call to builtin fmin or fmax. */
9242 static tree
9243 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9244 tree type, bool max)
9246 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9248 /* Calculate the result when the argument is a constant. */
9249 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9251 if (res)
9252 return res;
9254 /* If either argument is NaN, return the other one. Avoid the
9255 transformation if we get (and honor) a signalling NaN. Using
9256 omit_one_operand() ensures we create a non-lvalue. */
9257 if (TREE_CODE (arg0) == REAL_CST
9258 && real_isnan (&TREE_REAL_CST (arg0))
9259 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9260 || ! TREE_REAL_CST (arg0).signalling))
9261 return omit_one_operand_loc (loc, type, arg1, arg0);
9262 if (TREE_CODE (arg1) == REAL_CST
9263 && real_isnan (&TREE_REAL_CST (arg1))
9264 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9265 || ! TREE_REAL_CST (arg1).signalling))
9266 return omit_one_operand_loc (loc, type, arg0, arg1);
9268 /* Transform fmin/fmax(x,x) -> x. */
9269 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9270 return omit_one_operand_loc (loc, type, arg0, arg1);
9272 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9273 functions to return the numeric arg if the other one is NaN.
9274 These tree codes don't honor that, so only transform if
9275 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9276 handled, so we don't have to worry about it either. */
9277 if (flag_finite_math_only)
9278 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9279 fold_convert_loc (loc, type, arg0),
9280 fold_convert_loc (loc, type, arg1));
9282 return NULL_TREE;
9285 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9287 static tree
9288 fold_builtin_carg (location_t loc, tree arg, tree type)
9290 if (validate_arg (arg, COMPLEX_TYPE)
9291 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9293 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9295 if (atan2_fn)
9297 tree new_arg = builtin_save_expr (arg);
9298 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9299 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9300 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9304 return NULL_TREE;
9307 /* Fold a call to builtin logb/ilogb. */
9309 static tree
9310 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9312 if (! validate_arg (arg, REAL_TYPE))
9313 return NULL_TREE;
9315 STRIP_NOPS (arg);
9317 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9319 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9321 switch (value->cl)
9323 case rvc_nan:
9324 case rvc_inf:
9325 /* If arg is Inf or NaN and we're logb, return it. */
9326 if (TREE_CODE (rettype) == REAL_TYPE)
9327 return fold_convert_loc (loc, rettype, arg);
9328 /* Fall through... */
9329 case rvc_zero:
9330 /* Zero may set errno and/or raise an exception for logb, also
9331 for ilogb we don't know FP_ILOGB0. */
9332 return NULL_TREE;
9333 case rvc_normal:
9334 /* For normal numbers, proceed iff radix == 2. In GCC,
9335 normalized significands are in the range [0.5, 1.0). We
9336 want the exponent as if they were [1.0, 2.0) so get the
9337 exponent and subtract 1. */
9338 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9339 return fold_convert_loc (loc, rettype,
9340 build_int_cst (integer_type_node,
9341 REAL_EXP (value)-1));
9342 break;
9346 return NULL_TREE;
9349 /* Fold a call to builtin significand, if radix == 2. */
9351 static tree
9352 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9354 if (! validate_arg (arg, REAL_TYPE))
9355 return NULL_TREE;
9357 STRIP_NOPS (arg);
9359 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9361 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9363 switch (value->cl)
9365 case rvc_zero:
9366 case rvc_nan:
9367 case rvc_inf:
9368 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9369 return fold_convert_loc (loc, rettype, arg);
9370 case rvc_normal:
9371 /* For normal numbers, proceed iff radix == 2. */
9372 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9374 REAL_VALUE_TYPE result = *value;
9375 /* In GCC, normalized significands are in the range [0.5,
9376 1.0). We want them to be [1.0, 2.0) so set the
9377 exponent to 1. */
9378 SET_REAL_EXP (&result, 1);
9379 return build_real (rettype, result);
9381 break;
9385 return NULL_TREE;
9388 /* Fold a call to builtin frexp, we can assume the base is 2. */
9390 static tree
9391 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9393 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9394 return NULL_TREE;
9396 STRIP_NOPS (arg0);
9398 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9399 return NULL_TREE;
9401 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9403 /* Proceed if a valid pointer type was passed in. */
9404 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9406 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9407 tree frac, exp;
9409 switch (value->cl)
9411 case rvc_zero:
9412 /* For +-0, return (*exp = 0, +-0). */
9413 exp = integer_zero_node;
9414 frac = arg0;
9415 break;
9416 case rvc_nan:
9417 case rvc_inf:
9418 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9419 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9420 case rvc_normal:
9422 /* Since the frexp function always expects base 2, and in
9423 GCC normalized significands are already in the range
9424 [0.5, 1.0), we have exactly what frexp wants. */
9425 REAL_VALUE_TYPE frac_rvt = *value;
9426 SET_REAL_EXP (&frac_rvt, 0);
9427 frac = build_real (rettype, frac_rvt);
9428 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9430 break;
9431 default:
9432 gcc_unreachable ();
9435 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9436 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9437 TREE_SIDE_EFFECTS (arg1) = 1;
9438 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9441 return NULL_TREE;
9444 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9445 then we can assume the base is two. If it's false, then we have to
9446 check the mode of the TYPE parameter in certain cases. */
9448 static tree
9449 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9450 tree type, bool ldexp)
9452 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9454 STRIP_NOPS (arg0);
9455 STRIP_NOPS (arg1);
9457 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9458 if (real_zerop (arg0) || integer_zerop (arg1)
9459 || (TREE_CODE (arg0) == REAL_CST
9460 && !real_isfinite (&TREE_REAL_CST (arg0))))
9461 return omit_one_operand_loc (loc, type, arg0, arg1);
9463 /* If both arguments are constant, then try to evaluate it. */
9464 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9465 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9466 && host_integerp (arg1, 0))
9468 /* Bound the maximum adjustment to twice the range of the
9469 mode's valid exponents. Use abs to ensure the range is
9470 positive as a sanity check. */
9471 const long max_exp_adj = 2 *
9472 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9473 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9475 /* Get the user-requested adjustment. */
9476 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9478 /* The requested adjustment must be inside this range. This
9479 is a preliminary cap to avoid things like overflow, we
9480 may still fail to compute the result for other reasons. */
9481 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9483 REAL_VALUE_TYPE initial_result;
9485 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9487 /* Ensure we didn't overflow. */
9488 if (! real_isinf (&initial_result))
9490 const REAL_VALUE_TYPE trunc_result
9491 = real_value_truncate (TYPE_MODE (type), initial_result);
9493 /* Only proceed if the target mode can hold the
9494 resulting value. */
9495 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9496 return build_real (type, trunc_result);
9502 return NULL_TREE;
9505 /* Fold a call to builtin modf. */
9507 static tree
9508 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9510 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9511 return NULL_TREE;
9513 STRIP_NOPS (arg0);
9515 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9516 return NULL_TREE;
9518 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9520 /* Proceed if a valid pointer type was passed in. */
9521 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9523 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9524 REAL_VALUE_TYPE trunc, frac;
9526 switch (value->cl)
9528 case rvc_nan:
9529 case rvc_zero:
9530 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9531 trunc = frac = *value;
9532 break;
9533 case rvc_inf:
9534 /* For +-Inf, return (*arg1 = arg0, +-0). */
9535 frac = dconst0;
9536 frac.sign = value->sign;
9537 trunc = *value;
9538 break;
9539 case rvc_normal:
9540 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9541 real_trunc (&trunc, VOIDmode, value);
9542 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9543 /* If the original number was negative and already
9544 integral, then the fractional part is -0.0. */
9545 if (value->sign && frac.cl == rvc_zero)
9546 frac.sign = value->sign;
9547 break;
9550 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9551 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9552 build_real (rettype, trunc));
9553 TREE_SIDE_EFFECTS (arg1) = 1;
9554 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9555 build_real (rettype, frac));
9558 return NULL_TREE;
9561 /* Given a location LOC, an interclass builtin function decl FNDECL
9562 and its single argument ARG, return an folded expression computing
9563 the same, or NULL_TREE if we either couldn't or didn't want to fold
9564 (the latter happen if there's an RTL instruction available). */
9566 static tree
9567 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9569 enum machine_mode mode;
9571 if (!validate_arg (arg, REAL_TYPE))
9572 return NULL_TREE;
9574 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9575 return NULL_TREE;
9577 mode = TYPE_MODE (TREE_TYPE (arg));
9579 /* If there is no optab, try generic code. */
9580 switch (DECL_FUNCTION_CODE (fndecl))
9582 tree result;
9584 CASE_FLT_FN (BUILT_IN_ISINF):
9586 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9587 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9588 tree const type = TREE_TYPE (arg);
9589 REAL_VALUE_TYPE r;
9590 char buf[128];
9592 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9593 real_from_string (&r, buf);
9594 result = build_call_expr (isgr_fn, 2,
9595 fold_build1_loc (loc, ABS_EXPR, type, arg),
9596 build_real (type, r));
9597 return result;
9599 CASE_FLT_FN (BUILT_IN_FINITE):
9600 case BUILT_IN_ISFINITE:
9602 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9603 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9604 tree const type = TREE_TYPE (arg);
9605 REAL_VALUE_TYPE r;
9606 char buf[128];
9608 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9609 real_from_string (&r, buf);
9610 result = build_call_expr (isle_fn, 2,
9611 fold_build1_loc (loc, ABS_EXPR, type, arg),
9612 build_real (type, r));
9613 /*result = fold_build2_loc (loc, UNGT_EXPR,
9614 TREE_TYPE (TREE_TYPE (fndecl)),
9615 fold_build1_loc (loc, ABS_EXPR, type, arg),
9616 build_real (type, r));
9617 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9618 TREE_TYPE (TREE_TYPE (fndecl)),
9619 result);*/
9620 return result;
9622 case BUILT_IN_ISNORMAL:
9624 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9625 islessequal(fabs(x),DBL_MAX). */
9626 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9627 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9628 tree const type = TREE_TYPE (arg);
9629 REAL_VALUE_TYPE rmax, rmin;
9630 char buf[128];
9632 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9633 real_from_string (&rmax, buf);
9634 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9635 real_from_string (&rmin, buf);
9636 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9637 result = build_call_expr (isle_fn, 2, arg,
9638 build_real (type, rmax));
9639 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9640 build_call_expr (isge_fn, 2, arg,
9641 build_real (type, rmin)));
9642 return result;
9644 default:
9645 break;
9648 return NULL_TREE;
9651 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9652 ARG is the argument for the call. */
9654 static tree
9655 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9657 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9658 REAL_VALUE_TYPE r;
9660 if (!validate_arg (arg, REAL_TYPE))
9661 return NULL_TREE;
9663 switch (builtin_index)
9665 case BUILT_IN_ISINF:
9666 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9667 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9669 if (TREE_CODE (arg) == REAL_CST)
9671 r = TREE_REAL_CST (arg);
9672 if (real_isinf (&r))
9673 return real_compare (GT_EXPR, &r, &dconst0)
9674 ? integer_one_node : integer_minus_one_node;
9675 else
9676 return integer_zero_node;
9679 return NULL_TREE;
9681 case BUILT_IN_ISINF_SIGN:
9683 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9684 /* In a boolean context, GCC will fold the inner COND_EXPR to
9685 1. So e.g. "if (isinf_sign(x))" would be folded to just
9686 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9687 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9688 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9689 tree tmp = NULL_TREE;
9691 arg = builtin_save_expr (arg);
9693 if (signbit_fn && isinf_fn)
9695 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9696 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9698 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9699 signbit_call, integer_zero_node);
9700 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9701 isinf_call, integer_zero_node);
9703 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9704 integer_minus_one_node, integer_one_node);
9705 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9706 isinf_call, tmp,
9707 integer_zero_node);
9710 return tmp;
9713 case BUILT_IN_ISFINITE:
9714 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9715 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9716 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9718 if (TREE_CODE (arg) == REAL_CST)
9720 r = TREE_REAL_CST (arg);
9721 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9724 return NULL_TREE;
9726 case BUILT_IN_ISNAN:
9727 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9728 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9730 if (TREE_CODE (arg) == REAL_CST)
9732 r = TREE_REAL_CST (arg);
9733 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9736 arg = builtin_save_expr (arg);
9737 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9739 default:
9740 gcc_unreachable ();
9744 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9745 This builtin will generate code to return the appropriate floating
9746 point classification depending on the value of the floating point
9747 number passed in. The possible return values must be supplied as
9748 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9749 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9750 one floating point argument which is "type generic". */
9752 static tree
9753 fold_builtin_fpclassify (location_t loc, tree exp)
9755 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9756 arg, type, res, tmp;
9757 enum machine_mode mode;
9758 REAL_VALUE_TYPE r;
9759 char buf[128];
9761 /* Verify the required arguments in the original call. */
9762 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9763 INTEGER_TYPE, INTEGER_TYPE,
9764 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9765 return NULL_TREE;
9767 fp_nan = CALL_EXPR_ARG (exp, 0);
9768 fp_infinite = CALL_EXPR_ARG (exp, 1);
9769 fp_normal = CALL_EXPR_ARG (exp, 2);
9770 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9771 fp_zero = CALL_EXPR_ARG (exp, 4);
9772 arg = CALL_EXPR_ARG (exp, 5);
9773 type = TREE_TYPE (arg);
9774 mode = TYPE_MODE (type);
9775 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9777 /* fpclassify(x) ->
9778 isnan(x) ? FP_NAN :
9779 (fabs(x) == Inf ? FP_INFINITE :
9780 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9781 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9783 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9784 build_real (type, dconst0));
9785 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9786 tmp, fp_zero, fp_subnormal);
9788 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9789 real_from_string (&r, buf);
9790 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9791 arg, build_real (type, r));
9792 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9794 if (HONOR_INFINITIES (mode))
9796 real_inf (&r);
9797 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9798 build_real (type, r));
9799 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9800 fp_infinite, res);
9803 if (HONOR_NANS (mode))
9805 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9806 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9809 return res;
9812 /* Fold a call to an unordered comparison function such as
9813 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9814 being called and ARG0 and ARG1 are the arguments for the call.
9815 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9816 the opposite of the desired result. UNORDERED_CODE is used
9817 for modes that can hold NaNs and ORDERED_CODE is used for
9818 the rest. */
9820 static tree
9821 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9822 enum tree_code unordered_code,
9823 enum tree_code ordered_code)
9825 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9826 enum tree_code code;
9827 tree type0, type1;
9828 enum tree_code code0, code1;
9829 tree cmp_type = NULL_TREE;
9831 type0 = TREE_TYPE (arg0);
9832 type1 = TREE_TYPE (arg1);
9834 code0 = TREE_CODE (type0);
9835 code1 = TREE_CODE (type1);
9837 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9838 /* Choose the wider of two real types. */
9839 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9840 ? type0 : type1;
9841 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9842 cmp_type = type0;
9843 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9844 cmp_type = type1;
9846 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9847 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9849 if (unordered_code == UNORDERED_EXPR)
9851 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9852 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9853 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9856 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9857 : ordered_code;
9858 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9859 fold_build2_loc (loc, code, type, arg0, arg1));
9862 /* Fold a call to built-in function FNDECL with 0 arguments.
9863 IGNORE is true if the result of the function call is ignored. This
9864 function returns NULL_TREE if no simplification was possible. */
9866 static tree
9867 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9869 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9870 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9871 switch (fcode)
9873 CASE_FLT_FN (BUILT_IN_INF):
9874 case BUILT_IN_INFD32:
9875 case BUILT_IN_INFD64:
9876 case BUILT_IN_INFD128:
9877 return fold_builtin_inf (loc, type, true);
9879 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9880 return fold_builtin_inf (loc, type, false);
9882 case BUILT_IN_CLASSIFY_TYPE:
9883 return fold_builtin_classify_type (NULL_TREE);
9885 default:
9886 break;
9888 return NULL_TREE;
9891 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9892 IGNORE is true if the result of the function call is ignored. This
9893 function returns NULL_TREE if no simplification was possible. */
9895 static tree
9896 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9898 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9899 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9900 switch (fcode)
9902 case BUILT_IN_CONSTANT_P:
9904 tree val = fold_builtin_constant_p (arg0);
9906 /* Gimplification will pull the CALL_EXPR for the builtin out of
9907 an if condition. When not optimizing, we'll not CSE it back.
9908 To avoid link error types of regressions, return false now. */
9909 if (!val && !optimize)
9910 val = integer_zero_node;
9912 return val;
9915 case BUILT_IN_CLASSIFY_TYPE:
9916 return fold_builtin_classify_type (arg0);
9918 case BUILT_IN_STRLEN:
9919 return fold_builtin_strlen (loc, type, arg0);
9921 CASE_FLT_FN (BUILT_IN_FABS):
9922 return fold_builtin_fabs (loc, arg0, type);
9924 case BUILT_IN_ABS:
9925 case BUILT_IN_LABS:
9926 case BUILT_IN_LLABS:
9927 case BUILT_IN_IMAXABS:
9928 return fold_builtin_abs (loc, arg0, type);
9930 CASE_FLT_FN (BUILT_IN_CONJ):
9931 if (validate_arg (arg0, COMPLEX_TYPE)
9932 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9933 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9934 break;
9936 CASE_FLT_FN (BUILT_IN_CREAL):
9937 if (validate_arg (arg0, COMPLEX_TYPE)
9938 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9939 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9940 break;
9942 CASE_FLT_FN (BUILT_IN_CIMAG):
9943 if (validate_arg (arg0, COMPLEX_TYPE)
9944 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9945 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9946 break;
9948 CASE_FLT_FN (BUILT_IN_CCOS):
9949 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9951 CASE_FLT_FN (BUILT_IN_CCOSH):
9952 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9954 CASE_FLT_FN (BUILT_IN_CPROJ):
9955 return fold_builtin_cproj(loc, arg0, type);
9957 CASE_FLT_FN (BUILT_IN_CSIN):
9958 if (validate_arg (arg0, COMPLEX_TYPE)
9959 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9960 return do_mpc_arg1 (arg0, type, mpc_sin);
9961 break;
9963 CASE_FLT_FN (BUILT_IN_CSINH):
9964 if (validate_arg (arg0, COMPLEX_TYPE)
9965 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9966 return do_mpc_arg1 (arg0, type, mpc_sinh);
9967 break;
9969 CASE_FLT_FN (BUILT_IN_CTAN):
9970 if (validate_arg (arg0, COMPLEX_TYPE)
9971 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9972 return do_mpc_arg1 (arg0, type, mpc_tan);
9973 break;
9975 CASE_FLT_FN (BUILT_IN_CTANH):
9976 if (validate_arg (arg0, COMPLEX_TYPE)
9977 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9978 return do_mpc_arg1 (arg0, type, mpc_tanh);
9979 break;
9981 CASE_FLT_FN (BUILT_IN_CLOG):
9982 if (validate_arg (arg0, COMPLEX_TYPE)
9983 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9984 return do_mpc_arg1 (arg0, type, mpc_log);
9985 break;
9987 CASE_FLT_FN (BUILT_IN_CSQRT):
9988 if (validate_arg (arg0, COMPLEX_TYPE)
9989 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9990 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9991 break;
9993 CASE_FLT_FN (BUILT_IN_CASIN):
9994 if (validate_arg (arg0, COMPLEX_TYPE)
9995 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9996 return do_mpc_arg1 (arg0, type, mpc_asin);
9997 break;
9999 CASE_FLT_FN (BUILT_IN_CACOS):
10000 if (validate_arg (arg0, COMPLEX_TYPE)
10001 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10002 return do_mpc_arg1 (arg0, type, mpc_acos);
10003 break;
10005 CASE_FLT_FN (BUILT_IN_CATAN):
10006 if (validate_arg (arg0, COMPLEX_TYPE)
10007 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10008 return do_mpc_arg1 (arg0, type, mpc_atan);
10009 break;
10011 CASE_FLT_FN (BUILT_IN_CASINH):
10012 if (validate_arg (arg0, COMPLEX_TYPE)
10013 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10014 return do_mpc_arg1 (arg0, type, mpc_asinh);
10015 break;
10017 CASE_FLT_FN (BUILT_IN_CACOSH):
10018 if (validate_arg (arg0, COMPLEX_TYPE)
10019 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10020 return do_mpc_arg1 (arg0, type, mpc_acosh);
10021 break;
10023 CASE_FLT_FN (BUILT_IN_CATANH):
10024 if (validate_arg (arg0, COMPLEX_TYPE)
10025 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10026 return do_mpc_arg1 (arg0, type, mpc_atanh);
10027 break;
10029 CASE_FLT_FN (BUILT_IN_CABS):
10030 return fold_builtin_cabs (loc, arg0, type, fndecl);
10032 CASE_FLT_FN (BUILT_IN_CARG):
10033 return fold_builtin_carg (loc, arg0, type);
10035 CASE_FLT_FN (BUILT_IN_SQRT):
10036 return fold_builtin_sqrt (loc, arg0, type);
10038 CASE_FLT_FN (BUILT_IN_CBRT):
10039 return fold_builtin_cbrt (loc, arg0, type);
10041 CASE_FLT_FN (BUILT_IN_ASIN):
10042 if (validate_arg (arg0, REAL_TYPE))
10043 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10044 &dconstm1, &dconst1, true);
10045 break;
10047 CASE_FLT_FN (BUILT_IN_ACOS):
10048 if (validate_arg (arg0, REAL_TYPE))
10049 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10050 &dconstm1, &dconst1, true);
10051 break;
10053 CASE_FLT_FN (BUILT_IN_ATAN):
10054 if (validate_arg (arg0, REAL_TYPE))
10055 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10056 break;
10058 CASE_FLT_FN (BUILT_IN_ASINH):
10059 if (validate_arg (arg0, REAL_TYPE))
10060 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10061 break;
10063 CASE_FLT_FN (BUILT_IN_ACOSH):
10064 if (validate_arg (arg0, REAL_TYPE))
10065 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10066 &dconst1, NULL, true);
10067 break;
10069 CASE_FLT_FN (BUILT_IN_ATANH):
10070 if (validate_arg (arg0, REAL_TYPE))
10071 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10072 &dconstm1, &dconst1, false);
10073 break;
10075 CASE_FLT_FN (BUILT_IN_SIN):
10076 if (validate_arg (arg0, REAL_TYPE))
10077 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10078 break;
10080 CASE_FLT_FN (BUILT_IN_COS):
10081 return fold_builtin_cos (loc, arg0, type, fndecl);
10083 CASE_FLT_FN (BUILT_IN_TAN):
10084 return fold_builtin_tan (arg0, type);
10086 CASE_FLT_FN (BUILT_IN_CEXP):
10087 return fold_builtin_cexp (loc, arg0, type);
10089 CASE_FLT_FN (BUILT_IN_CEXPI):
10090 if (validate_arg (arg0, REAL_TYPE))
10091 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10092 break;
10094 CASE_FLT_FN (BUILT_IN_SINH):
10095 if (validate_arg (arg0, REAL_TYPE))
10096 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10097 break;
10099 CASE_FLT_FN (BUILT_IN_COSH):
10100 return fold_builtin_cosh (loc, arg0, type, fndecl);
10102 CASE_FLT_FN (BUILT_IN_TANH):
10103 if (validate_arg (arg0, REAL_TYPE))
10104 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10105 break;
10107 CASE_FLT_FN (BUILT_IN_ERF):
10108 if (validate_arg (arg0, REAL_TYPE))
10109 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10110 break;
10112 CASE_FLT_FN (BUILT_IN_ERFC):
10113 if (validate_arg (arg0, REAL_TYPE))
10114 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10115 break;
10117 CASE_FLT_FN (BUILT_IN_TGAMMA):
10118 if (validate_arg (arg0, REAL_TYPE))
10119 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10120 break;
10122 CASE_FLT_FN (BUILT_IN_EXP):
10123 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10125 CASE_FLT_FN (BUILT_IN_EXP2):
10126 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10128 CASE_FLT_FN (BUILT_IN_EXP10):
10129 CASE_FLT_FN (BUILT_IN_POW10):
10130 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10132 CASE_FLT_FN (BUILT_IN_EXPM1):
10133 if (validate_arg (arg0, REAL_TYPE))
10134 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10135 break;
10137 CASE_FLT_FN (BUILT_IN_LOG):
10138 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10140 CASE_FLT_FN (BUILT_IN_LOG2):
10141 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10143 CASE_FLT_FN (BUILT_IN_LOG10):
10144 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10146 CASE_FLT_FN (BUILT_IN_LOG1P):
10147 if (validate_arg (arg0, REAL_TYPE))
10148 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10149 &dconstm1, NULL, false);
10150 break;
10152 CASE_FLT_FN (BUILT_IN_J0):
10153 if (validate_arg (arg0, REAL_TYPE))
10154 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10155 NULL, NULL, 0);
10156 break;
10158 CASE_FLT_FN (BUILT_IN_J1):
10159 if (validate_arg (arg0, REAL_TYPE))
10160 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10161 NULL, NULL, 0);
10162 break;
10164 CASE_FLT_FN (BUILT_IN_Y0):
10165 if (validate_arg (arg0, REAL_TYPE))
10166 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10167 &dconst0, NULL, false);
10168 break;
10170 CASE_FLT_FN (BUILT_IN_Y1):
10171 if (validate_arg (arg0, REAL_TYPE))
10172 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10173 &dconst0, NULL, false);
10174 break;
10176 CASE_FLT_FN (BUILT_IN_NAN):
10177 case BUILT_IN_NAND32:
10178 case BUILT_IN_NAND64:
10179 case BUILT_IN_NAND128:
10180 return fold_builtin_nan (arg0, type, true);
10182 CASE_FLT_FN (BUILT_IN_NANS):
10183 return fold_builtin_nan (arg0, type, false);
10185 CASE_FLT_FN (BUILT_IN_FLOOR):
10186 return fold_builtin_floor (loc, fndecl, arg0);
10188 CASE_FLT_FN (BUILT_IN_CEIL):
10189 return fold_builtin_ceil (loc, fndecl, arg0);
10191 CASE_FLT_FN (BUILT_IN_TRUNC):
10192 return fold_builtin_trunc (loc, fndecl, arg0);
10194 CASE_FLT_FN (BUILT_IN_ROUND):
10195 return fold_builtin_round (loc, fndecl, arg0);
10197 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10198 CASE_FLT_FN (BUILT_IN_RINT):
10199 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10201 CASE_FLT_FN (BUILT_IN_LCEIL):
10202 CASE_FLT_FN (BUILT_IN_LLCEIL):
10203 CASE_FLT_FN (BUILT_IN_LFLOOR):
10204 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10205 CASE_FLT_FN (BUILT_IN_LROUND):
10206 CASE_FLT_FN (BUILT_IN_LLROUND):
10207 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10209 CASE_FLT_FN (BUILT_IN_LRINT):
10210 CASE_FLT_FN (BUILT_IN_LLRINT):
10211 return fold_fixed_mathfn (loc, fndecl, arg0);
10213 case BUILT_IN_BSWAP32:
10214 case BUILT_IN_BSWAP64:
10215 return fold_builtin_bswap (fndecl, arg0);
10217 CASE_INT_FN (BUILT_IN_FFS):
10218 CASE_INT_FN (BUILT_IN_CLZ):
10219 CASE_INT_FN (BUILT_IN_CTZ):
10220 CASE_INT_FN (BUILT_IN_POPCOUNT):
10221 CASE_INT_FN (BUILT_IN_PARITY):
10222 return fold_builtin_bitop (fndecl, arg0);
10224 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10225 return fold_builtin_signbit (loc, arg0, type);
10227 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10228 return fold_builtin_significand (loc, arg0, type);
10230 CASE_FLT_FN (BUILT_IN_ILOGB):
10231 CASE_FLT_FN (BUILT_IN_LOGB):
10232 return fold_builtin_logb (loc, arg0, type);
10234 case BUILT_IN_ISASCII:
10235 return fold_builtin_isascii (loc, arg0);
10237 case BUILT_IN_TOASCII:
10238 return fold_builtin_toascii (loc, arg0);
10240 case BUILT_IN_ISDIGIT:
10241 return fold_builtin_isdigit (loc, arg0);
10243 CASE_FLT_FN (BUILT_IN_FINITE):
10244 case BUILT_IN_FINITED32:
10245 case BUILT_IN_FINITED64:
10246 case BUILT_IN_FINITED128:
10247 case BUILT_IN_ISFINITE:
10249 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10250 if (ret)
10251 return ret;
10252 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10255 CASE_FLT_FN (BUILT_IN_ISINF):
10256 case BUILT_IN_ISINFD32:
10257 case BUILT_IN_ISINFD64:
10258 case BUILT_IN_ISINFD128:
10260 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10261 if (ret)
10262 return ret;
10263 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10266 case BUILT_IN_ISNORMAL:
10267 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10269 case BUILT_IN_ISINF_SIGN:
10270 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10272 CASE_FLT_FN (BUILT_IN_ISNAN):
10273 case BUILT_IN_ISNAND32:
10274 case BUILT_IN_ISNAND64:
10275 case BUILT_IN_ISNAND128:
10276 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10278 case BUILT_IN_PRINTF:
10279 case BUILT_IN_PRINTF_UNLOCKED:
10280 case BUILT_IN_VPRINTF:
10281 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10283 case BUILT_IN_FREE:
10284 if (integer_zerop (arg0))
10285 return build_empty_stmt (loc);
10286 break;
10288 default:
10289 break;
10292 return NULL_TREE;
10296 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10297 IGNORE is true if the result of the function call is ignored. This
10298 function returns NULL_TREE if no simplification was possible. */
10300 static tree
10301 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10303 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10304 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10306 switch (fcode)
10308 CASE_FLT_FN (BUILT_IN_JN):
10309 if (validate_arg (arg0, INTEGER_TYPE)
10310 && validate_arg (arg1, REAL_TYPE))
10311 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10312 break;
10314 CASE_FLT_FN (BUILT_IN_YN):
10315 if (validate_arg (arg0, INTEGER_TYPE)
10316 && validate_arg (arg1, REAL_TYPE))
10317 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10318 &dconst0, false);
10319 break;
10321 CASE_FLT_FN (BUILT_IN_DREM):
10322 CASE_FLT_FN (BUILT_IN_REMAINDER):
10323 if (validate_arg (arg0, REAL_TYPE)
10324 && validate_arg(arg1, REAL_TYPE))
10325 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10326 break;
10328 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10329 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10330 if (validate_arg (arg0, REAL_TYPE)
10331 && validate_arg(arg1, POINTER_TYPE))
10332 return do_mpfr_lgamma_r (arg0, arg1, type);
10333 break;
10335 CASE_FLT_FN (BUILT_IN_ATAN2):
10336 if (validate_arg (arg0, REAL_TYPE)
10337 && validate_arg(arg1, REAL_TYPE))
10338 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10339 break;
10341 CASE_FLT_FN (BUILT_IN_FDIM):
10342 if (validate_arg (arg0, REAL_TYPE)
10343 && validate_arg(arg1, REAL_TYPE))
10344 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10345 break;
10347 CASE_FLT_FN (BUILT_IN_HYPOT):
10348 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10350 CASE_FLT_FN (BUILT_IN_CPOW):
10351 if (validate_arg (arg0, COMPLEX_TYPE)
10352 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10353 && validate_arg (arg1, COMPLEX_TYPE)
10354 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10355 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_LDEXP):
10359 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10360 CASE_FLT_FN (BUILT_IN_SCALBN):
10361 CASE_FLT_FN (BUILT_IN_SCALBLN):
10362 return fold_builtin_load_exponent (loc, arg0, arg1,
10363 type, /*ldexp=*/false);
10365 CASE_FLT_FN (BUILT_IN_FREXP):
10366 return fold_builtin_frexp (loc, arg0, arg1, type);
10368 CASE_FLT_FN (BUILT_IN_MODF):
10369 return fold_builtin_modf (loc, arg0, arg1, type);
10371 case BUILT_IN_BZERO:
10372 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10374 case BUILT_IN_FPUTS:
10375 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10377 case BUILT_IN_FPUTS_UNLOCKED:
10378 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10380 case BUILT_IN_STRSTR:
10381 return fold_builtin_strstr (loc, arg0, arg1, type);
10383 case BUILT_IN_STRCAT:
10384 return fold_builtin_strcat (loc, arg0, arg1);
10386 case BUILT_IN_STRSPN:
10387 return fold_builtin_strspn (loc, arg0, arg1);
10389 case BUILT_IN_STRCSPN:
10390 return fold_builtin_strcspn (loc, arg0, arg1);
10392 case BUILT_IN_STRCHR:
10393 case BUILT_IN_INDEX:
10394 return fold_builtin_strchr (loc, arg0, arg1, type);
10396 case BUILT_IN_STRRCHR:
10397 case BUILT_IN_RINDEX:
10398 return fold_builtin_strrchr (loc, arg0, arg1, type);
10400 case BUILT_IN_STRCPY:
10401 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10403 case BUILT_IN_STPCPY:
10404 if (ignore)
10406 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10407 if (!fn)
10408 break;
10410 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10412 else
10413 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10414 break;
10416 case BUILT_IN_STRCMP:
10417 return fold_builtin_strcmp (loc, arg0, arg1);
10419 case BUILT_IN_STRPBRK:
10420 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10422 case BUILT_IN_EXPECT:
10423 return fold_builtin_expect (loc, arg0, arg1);
10425 CASE_FLT_FN (BUILT_IN_POW):
10426 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10428 CASE_FLT_FN (BUILT_IN_POWI):
10429 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10431 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10432 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10434 CASE_FLT_FN (BUILT_IN_FMIN):
10435 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10437 CASE_FLT_FN (BUILT_IN_FMAX):
10438 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10440 case BUILT_IN_ISGREATER:
10441 return fold_builtin_unordered_cmp (loc, fndecl,
10442 arg0, arg1, UNLE_EXPR, LE_EXPR);
10443 case BUILT_IN_ISGREATEREQUAL:
10444 return fold_builtin_unordered_cmp (loc, fndecl,
10445 arg0, arg1, UNLT_EXPR, LT_EXPR);
10446 case BUILT_IN_ISLESS:
10447 return fold_builtin_unordered_cmp (loc, fndecl,
10448 arg0, arg1, UNGE_EXPR, GE_EXPR);
10449 case BUILT_IN_ISLESSEQUAL:
10450 return fold_builtin_unordered_cmp (loc, fndecl,
10451 arg0, arg1, UNGT_EXPR, GT_EXPR);
10452 case BUILT_IN_ISLESSGREATER:
10453 return fold_builtin_unordered_cmp (loc, fndecl,
10454 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10455 case BUILT_IN_ISUNORDERED:
10456 return fold_builtin_unordered_cmp (loc, fndecl,
10457 arg0, arg1, UNORDERED_EXPR,
10458 NOP_EXPR);
10460 /* We do the folding for va_start in the expander. */
10461 case BUILT_IN_VA_START:
10462 break;
10464 case BUILT_IN_SPRINTF:
10465 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10467 case BUILT_IN_OBJECT_SIZE:
10468 return fold_builtin_object_size (arg0, arg1);
10470 case BUILT_IN_PRINTF:
10471 case BUILT_IN_PRINTF_UNLOCKED:
10472 case BUILT_IN_VPRINTF:
10473 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10475 case BUILT_IN_PRINTF_CHK:
10476 case BUILT_IN_VPRINTF_CHK:
10477 if (!validate_arg (arg0, INTEGER_TYPE)
10478 || TREE_SIDE_EFFECTS (arg0))
10479 return NULL_TREE;
10480 else
10481 return fold_builtin_printf (loc, fndecl,
10482 arg1, NULL_TREE, ignore, fcode);
10483 break;
10485 case BUILT_IN_FPRINTF:
10486 case BUILT_IN_FPRINTF_UNLOCKED:
10487 case BUILT_IN_VFPRINTF:
10488 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10489 ignore, fcode);
10491 default:
10492 break;
10494 return NULL_TREE;
10497 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10498 and ARG2. IGNORE is true if the result of the function call is ignored.
10499 This function returns NULL_TREE if no simplification was possible. */
10501 static tree
10502 fold_builtin_3 (location_t loc, tree fndecl,
10503 tree arg0, tree arg1, tree arg2, bool ignore)
10505 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10506 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10507 switch (fcode)
10510 CASE_FLT_FN (BUILT_IN_SINCOS):
10511 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10513 CASE_FLT_FN (BUILT_IN_FMA):
10514 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10515 break;
10517 CASE_FLT_FN (BUILT_IN_REMQUO):
10518 if (validate_arg (arg0, REAL_TYPE)
10519 && validate_arg(arg1, REAL_TYPE)
10520 && validate_arg(arg2, POINTER_TYPE))
10521 return do_mpfr_remquo (arg0, arg1, arg2);
10522 break;
10524 case BUILT_IN_MEMSET:
10525 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10527 case BUILT_IN_BCOPY:
10528 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10529 void_type_node, true, /*endp=*/3);
10531 case BUILT_IN_MEMCPY:
10532 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10533 type, ignore, /*endp=*/0);
10535 case BUILT_IN_MEMPCPY:
10536 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10537 type, ignore, /*endp=*/1);
10539 case BUILT_IN_MEMMOVE:
10540 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10541 type, ignore, /*endp=*/3);
10543 case BUILT_IN_STRNCAT:
10544 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10546 case BUILT_IN_STRNCPY:
10547 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10549 case BUILT_IN_STRNCMP:
10550 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10552 case BUILT_IN_MEMCHR:
10553 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10555 case BUILT_IN_BCMP:
10556 case BUILT_IN_MEMCMP:
10557 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10559 case BUILT_IN_SPRINTF:
10560 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10562 case BUILT_IN_SNPRINTF:
10563 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10565 case BUILT_IN_STRCPY_CHK:
10566 case BUILT_IN_STPCPY_CHK:
10567 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10568 ignore, fcode);
10570 case BUILT_IN_STRCAT_CHK:
10571 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10573 case BUILT_IN_PRINTF_CHK:
10574 case BUILT_IN_VPRINTF_CHK:
10575 if (!validate_arg (arg0, INTEGER_TYPE)
10576 || TREE_SIDE_EFFECTS (arg0))
10577 return NULL_TREE;
10578 else
10579 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10580 break;
10582 case BUILT_IN_FPRINTF:
10583 case BUILT_IN_FPRINTF_UNLOCKED:
10584 case BUILT_IN_VFPRINTF:
10585 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10586 ignore, fcode);
10588 case BUILT_IN_FPRINTF_CHK:
10589 case BUILT_IN_VFPRINTF_CHK:
10590 if (!validate_arg (arg1, INTEGER_TYPE)
10591 || TREE_SIDE_EFFECTS (arg1))
10592 return NULL_TREE;
10593 else
10594 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10595 ignore, fcode);
10597 default:
10598 break;
10600 return NULL_TREE;
10603 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10604 ARG2, and ARG3. IGNORE is true if the result of the function call is
10605 ignored. This function returns NULL_TREE if no simplification was
10606 possible. */
10608 static tree
10609 fold_builtin_4 (location_t loc, tree fndecl,
10610 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10612 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10614 switch (fcode)
10616 case BUILT_IN_MEMCPY_CHK:
10617 case BUILT_IN_MEMPCPY_CHK:
10618 case BUILT_IN_MEMMOVE_CHK:
10619 case BUILT_IN_MEMSET_CHK:
10620 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10621 NULL_TREE, ignore,
10622 DECL_FUNCTION_CODE (fndecl));
10624 case BUILT_IN_STRNCPY_CHK:
10625 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10627 case BUILT_IN_STRNCAT_CHK:
10628 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10630 case BUILT_IN_SNPRINTF:
10631 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10633 case BUILT_IN_FPRINTF_CHK:
10634 case BUILT_IN_VFPRINTF_CHK:
10635 if (!validate_arg (arg1, INTEGER_TYPE)
10636 || TREE_SIDE_EFFECTS (arg1))
10637 return NULL_TREE;
10638 else
10639 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10640 ignore, fcode);
10641 break;
10643 default:
10644 break;
10646 return NULL_TREE;
10649 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10650 arguments, where NARGS <= 4. IGNORE is true if the result of the
10651 function call is ignored. This function returns NULL_TREE if no
10652 simplification was possible. Note that this only folds builtins with
10653 fixed argument patterns. Foldings that do varargs-to-varargs
10654 transformations, or that match calls with more than 4 arguments,
10655 need to be handled with fold_builtin_varargs instead. */
10657 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10659 static tree
10660 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10662 tree ret = NULL_TREE;
10664 switch (nargs)
10666 case 0:
10667 ret = fold_builtin_0 (loc, fndecl, ignore);
10668 break;
10669 case 1:
10670 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10671 break;
10672 case 2:
10673 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10674 break;
10675 case 3:
10676 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10677 break;
10678 case 4:
10679 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10680 ignore);
10681 break;
10682 default:
10683 break;
10685 if (ret)
10687 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10688 SET_EXPR_LOCATION (ret, loc);
10689 TREE_NO_WARNING (ret) = 1;
10690 return ret;
10692 return NULL_TREE;
10695 /* Builtins with folding operations that operate on "..." arguments
10696 need special handling; we need to store the arguments in a convenient
10697 data structure before attempting any folding. Fortunately there are
10698 only a few builtins that fall into this category. FNDECL is the
10699 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10700 result of the function call is ignored. */
10702 static tree
10703 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10704 bool ignore ATTRIBUTE_UNUSED)
10706 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10707 tree ret = NULL_TREE;
10709 switch (fcode)
10711 case BUILT_IN_SPRINTF_CHK:
10712 case BUILT_IN_VSPRINTF_CHK:
10713 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10714 break;
10716 case BUILT_IN_SNPRINTF_CHK:
10717 case BUILT_IN_VSNPRINTF_CHK:
10718 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10719 break;
10721 case BUILT_IN_FPCLASSIFY:
10722 ret = fold_builtin_fpclassify (loc, exp);
10723 break;
10725 default:
10726 break;
10728 if (ret)
10730 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10731 SET_EXPR_LOCATION (ret, loc);
10732 TREE_NO_WARNING (ret) = 1;
10733 return ret;
10735 return NULL_TREE;
10738 /* Return true if FNDECL shouldn't be folded right now.
10739 If a built-in function has an inline attribute always_inline
10740 wrapper, defer folding it after always_inline functions have
10741 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10742 might not be performed. */
10744 static bool
10745 avoid_folding_inline_builtin (tree fndecl)
10747 return (DECL_DECLARED_INLINE_P (fndecl)
10748 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10749 && cfun
10750 && !cfun->always_inline_functions_inlined
10751 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10754 /* A wrapper function for builtin folding that prevents warnings for
10755 "statement without effect" and the like, caused by removing the
10756 call node earlier than the warning is generated. */
10758 tree
10759 fold_call_expr (location_t loc, tree exp, bool ignore)
10761 tree ret = NULL_TREE;
10762 tree fndecl = get_callee_fndecl (exp);
10763 if (fndecl
10764 && TREE_CODE (fndecl) == FUNCTION_DECL
10765 && DECL_BUILT_IN (fndecl)
10766 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10767 yet. Defer folding until we see all the arguments
10768 (after inlining). */
10769 && !CALL_EXPR_VA_ARG_PACK (exp))
10771 int nargs = call_expr_nargs (exp);
10773 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10774 instead last argument is __builtin_va_arg_pack (). Defer folding
10775 even in that case, until arguments are finalized. */
10776 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10778 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10779 if (fndecl2
10780 && TREE_CODE (fndecl2) == FUNCTION_DECL
10781 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10782 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10783 return NULL_TREE;
10786 if (avoid_folding_inline_builtin (fndecl))
10787 return NULL_TREE;
10789 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10790 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10791 CALL_EXPR_ARGP (exp), ignore);
10792 else
10794 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10796 tree *args = CALL_EXPR_ARGP (exp);
10797 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10799 if (!ret)
10800 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10801 if (ret)
10802 return ret;
10805 return NULL_TREE;
10808 /* Conveniently construct a function call expression. FNDECL names the
10809 function to be called and N arguments are passed in the array
10810 ARGARRAY. */
10812 tree
10813 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10815 tree fntype = TREE_TYPE (fndecl);
10816 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10818 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10821 /* Conveniently construct a function call expression. FNDECL names the
10822 function to be called and the arguments are passed in the vector
10823 VEC. */
10825 tree
10826 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10828 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10829 VEC_address (tree, vec));
10833 /* Conveniently construct a function call expression. FNDECL names the
10834 function to be called, N is the number of arguments, and the "..."
10835 parameters are the argument expressions. */
10837 tree
10838 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10840 va_list ap;
10841 tree *argarray = XALLOCAVEC (tree, n);
10842 int i;
10844 va_start (ap, n);
10845 for (i = 0; i < n; i++)
10846 argarray[i] = va_arg (ap, tree);
10847 va_end (ap);
10848 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10851 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10852 varargs macros aren't supported by all bootstrap compilers. */
10854 tree
10855 build_call_expr (tree fndecl, int n, ...)
10857 va_list ap;
10858 tree *argarray = XALLOCAVEC (tree, n);
10859 int i;
10861 va_start (ap, n);
10862 for (i = 0; i < n; i++)
10863 argarray[i] = va_arg (ap, tree);
10864 va_end (ap);
10865 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10868 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10869 N arguments are passed in the array ARGARRAY. */
10871 tree
10872 fold_builtin_call_array (location_t loc, tree type,
10873 tree fn,
10874 int n,
10875 tree *argarray)
10877 tree ret = NULL_TREE;
10878 tree exp;
10880 if (TREE_CODE (fn) == ADDR_EXPR)
10882 tree fndecl = TREE_OPERAND (fn, 0);
10883 if (TREE_CODE (fndecl) == FUNCTION_DECL
10884 && DECL_BUILT_IN (fndecl))
10886 /* If last argument is __builtin_va_arg_pack (), arguments to this
10887 function are not finalized yet. Defer folding until they are. */
10888 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10890 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10891 if (fndecl2
10892 && TREE_CODE (fndecl2) == FUNCTION_DECL
10893 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10894 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10895 return build_call_array_loc (loc, type, fn, n, argarray);
10897 if (avoid_folding_inline_builtin (fndecl))
10898 return build_call_array_loc (loc, type, fn, n, argarray);
10899 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10901 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10902 if (ret)
10903 return ret;
10905 return build_call_array_loc (loc, type, fn, n, argarray);
10907 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10909 /* First try the transformations that don't require consing up
10910 an exp. */
10911 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10912 if (ret)
10913 return ret;
10916 /* If we got this far, we need to build an exp. */
10917 exp = build_call_array_loc (loc, type, fn, n, argarray);
10918 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10919 return ret ? ret : exp;
10923 return build_call_array_loc (loc, type, fn, n, argarray);
10926 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10927 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10928 of arguments in ARGS to be omitted. OLDNARGS is the number of
10929 elements in ARGS. */
10931 static tree
10932 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10933 int skip, tree fndecl, int n, va_list newargs)
10935 int nargs = oldnargs - skip + n;
10936 tree *buffer;
10938 if (n > 0)
10940 int i, j;
10942 buffer = XALLOCAVEC (tree, nargs);
10943 for (i = 0; i < n; i++)
10944 buffer[i] = va_arg (newargs, tree);
10945 for (j = skip; j < oldnargs; j++, i++)
10946 buffer[i] = args[j];
10948 else
10949 buffer = args + skip;
10951 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10954 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10955 list ARGS along with N new arguments specified as the "..."
10956 parameters. SKIP is the number of arguments in ARGS to be omitted.
10957 OLDNARGS is the number of elements in ARGS. */
10959 static tree
10960 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10961 int skip, tree fndecl, int n, ...)
10963 va_list ap;
10964 tree t;
10966 va_start (ap, n);
10967 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10968 va_end (ap);
10970 return t;
10973 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10974 along with N new arguments specified as the "..." parameters. SKIP
10975 is the number of arguments in EXP to be omitted. This function is used
10976 to do varargs-to-varargs transformations. */
10978 static tree
10979 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10981 va_list ap;
10982 tree t;
10984 va_start (ap, n);
10985 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10986 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10987 va_end (ap);
10989 return t;
10992 /* Validate a single argument ARG against a tree code CODE representing
10993 a type. */
10995 static bool
10996 validate_arg (const_tree arg, enum tree_code code)
10998 if (!arg)
10999 return false;
11000 else if (code == POINTER_TYPE)
11001 return POINTER_TYPE_P (TREE_TYPE (arg));
11002 else if (code == INTEGER_TYPE)
11003 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11004 return code == TREE_CODE (TREE_TYPE (arg));
11007 /* This function validates the types of a function call argument list
11008 against a specified list of tree_codes. If the last specifier is a 0,
11009 that represents an ellipses, otherwise the last specifier must be a
11010 VOID_TYPE.
11012 This is the GIMPLE version of validate_arglist. Eventually we want to
11013 completely convert builtins.c to work from GIMPLEs and the tree based
11014 validate_arglist will then be removed. */
11016 bool
11017 validate_gimple_arglist (const_gimple call, ...)
11019 enum tree_code code;
11020 bool res = 0;
11021 va_list ap;
11022 const_tree arg;
11023 size_t i;
11025 va_start (ap, call);
11026 i = 0;
11030 code = (enum tree_code) va_arg (ap, int);
11031 switch (code)
11033 case 0:
11034 /* This signifies an ellipses, any further arguments are all ok. */
11035 res = true;
11036 goto end;
11037 case VOID_TYPE:
11038 /* This signifies an endlink, if no arguments remain, return
11039 true, otherwise return false. */
11040 res = (i == gimple_call_num_args (call));
11041 goto end;
11042 default:
11043 /* If no parameters remain or the parameter's code does not
11044 match the specified code, return false. Otherwise continue
11045 checking any remaining arguments. */
11046 arg = gimple_call_arg (call, i++);
11047 if (!validate_arg (arg, code))
11048 goto end;
11049 break;
11052 while (1);
11054 /* We need gotos here since we can only have one VA_CLOSE in a
11055 function. */
11056 end: ;
11057 va_end (ap);
11059 return res;
11062 /* This function validates the types of a function call argument list
11063 against a specified list of tree_codes. If the last specifier is a 0,
11064 that represents an ellipses, otherwise the last specifier must be a
11065 VOID_TYPE. */
11067 bool
11068 validate_arglist (const_tree callexpr, ...)
11070 enum tree_code code;
11071 bool res = 0;
11072 va_list ap;
11073 const_call_expr_arg_iterator iter;
11074 const_tree arg;
11076 va_start (ap, callexpr);
11077 init_const_call_expr_arg_iterator (callexpr, &iter);
11081 code = (enum tree_code) va_arg (ap, int);
11082 switch (code)
11084 case 0:
11085 /* This signifies an ellipses, any further arguments are all ok. */
11086 res = true;
11087 goto end;
11088 case VOID_TYPE:
11089 /* This signifies an endlink, if no arguments remain, return
11090 true, otherwise return false. */
11091 res = !more_const_call_expr_args_p (&iter);
11092 goto end;
11093 default:
11094 /* If no parameters remain or the parameter's code does not
11095 match the specified code, return false. Otherwise continue
11096 checking any remaining arguments. */
11097 arg = next_const_call_expr_arg (&iter);
11098 if (!validate_arg (arg, code))
11099 goto end;
11100 break;
11103 while (1);
11105 /* We need gotos here since we can only have one VA_CLOSE in a
11106 function. */
11107 end: ;
11108 va_end (ap);
11110 return res;
11113 /* Default target-specific builtin expander that does nothing. */
11116 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11117 rtx target ATTRIBUTE_UNUSED,
11118 rtx subtarget ATTRIBUTE_UNUSED,
11119 enum machine_mode mode ATTRIBUTE_UNUSED,
11120 int ignore ATTRIBUTE_UNUSED)
11122 return NULL_RTX;
11125 /* Returns true is EXP represents data that would potentially reside
11126 in a readonly section. */
11128 static bool
11129 readonly_data_expr (tree exp)
11131 STRIP_NOPS (exp);
11133 if (TREE_CODE (exp) != ADDR_EXPR)
11134 return false;
11136 exp = get_base_address (TREE_OPERAND (exp, 0));
11137 if (!exp)
11138 return false;
11140 /* Make sure we call decl_readonly_section only for trees it
11141 can handle (since it returns true for everything it doesn't
11142 understand). */
11143 if (TREE_CODE (exp) == STRING_CST
11144 || TREE_CODE (exp) == CONSTRUCTOR
11145 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11146 return decl_readonly_section (exp, 0);
11147 else
11148 return false;
11151 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11152 to the call, and TYPE is its return type.
11154 Return NULL_TREE if no simplification was possible, otherwise return the
11155 simplified form of the call as a tree.
11157 The simplified form may be a constant or other expression which
11158 computes the same value, but in a more efficient manner (including
11159 calls to other builtin functions).
11161 The call may contain arguments which need to be evaluated, but
11162 which are not useful to determine the result of the call. In
11163 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11164 COMPOUND_EXPR will be an argument which must be evaluated.
11165 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11166 COMPOUND_EXPR in the chain will contain the tree for the simplified
11167 form of the builtin function call. */
11169 static tree
11170 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11172 if (!validate_arg (s1, POINTER_TYPE)
11173 || !validate_arg (s2, POINTER_TYPE))
11174 return NULL_TREE;
11175 else
11177 tree fn;
11178 const char *p1, *p2;
11180 p2 = c_getstr (s2);
11181 if (p2 == NULL)
11182 return NULL_TREE;
11184 p1 = c_getstr (s1);
11185 if (p1 != NULL)
11187 const char *r = strstr (p1, p2);
11188 tree tem;
11190 if (r == NULL)
11191 return build_int_cst (TREE_TYPE (s1), 0);
11193 /* Return an offset into the constant string argument. */
11194 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11195 s1, size_int (r - p1));
11196 return fold_convert_loc (loc, type, tem);
11199 /* The argument is const char *, and the result is char *, so we need
11200 a type conversion here to avoid a warning. */
11201 if (p2[0] == '\0')
11202 return fold_convert_loc (loc, type, s1);
11204 if (p2[1] != '\0')
11205 return NULL_TREE;
11207 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11208 if (!fn)
11209 return NULL_TREE;
11211 /* New argument list transforming strstr(s1, s2) to
11212 strchr(s1, s2[0]). */
11213 return build_call_expr_loc (loc, fn, 2, s1,
11214 build_int_cst (integer_type_node, p2[0]));
11218 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11219 the call, and TYPE is its return type.
11221 Return NULL_TREE if no simplification was possible, otherwise return the
11222 simplified form of the call as a tree.
11224 The simplified form may be a constant or other expression which
11225 computes the same value, but in a more efficient manner (including
11226 calls to other builtin functions).
11228 The call may contain arguments which need to be evaluated, but
11229 which are not useful to determine the result of the call. In
11230 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11231 COMPOUND_EXPR will be an argument which must be evaluated.
11232 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11233 COMPOUND_EXPR in the chain will contain the tree for the simplified
11234 form of the builtin function call. */
11236 static tree
11237 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11239 if (!validate_arg (s1, POINTER_TYPE)
11240 || !validate_arg (s2, INTEGER_TYPE))
11241 return NULL_TREE;
11242 else
11244 const char *p1;
11246 if (TREE_CODE (s2) != INTEGER_CST)
11247 return NULL_TREE;
11249 p1 = c_getstr (s1);
11250 if (p1 != NULL)
11252 char c;
11253 const char *r;
11254 tree tem;
11256 if (target_char_cast (s2, &c))
11257 return NULL_TREE;
11259 r = strchr (p1, c);
11261 if (r == NULL)
11262 return build_int_cst (TREE_TYPE (s1), 0);
11264 /* Return an offset into the constant string argument. */
11265 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11266 s1, size_int (r - p1));
11267 return fold_convert_loc (loc, type, tem);
11269 return NULL_TREE;
11273 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11274 the call, and TYPE is its return type.
11276 Return NULL_TREE if no simplification was possible, otherwise return the
11277 simplified form of the call as a tree.
11279 The simplified form may be a constant or other expression which
11280 computes the same value, but in a more efficient manner (including
11281 calls to other builtin functions).
11283 The call may contain arguments which need to be evaluated, but
11284 which are not useful to determine the result of the call. In
11285 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11286 COMPOUND_EXPR will be an argument which must be evaluated.
11287 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11288 COMPOUND_EXPR in the chain will contain the tree for the simplified
11289 form of the builtin function call. */
11291 static tree
11292 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11294 if (!validate_arg (s1, POINTER_TYPE)
11295 || !validate_arg (s2, INTEGER_TYPE))
11296 return NULL_TREE;
11297 else
11299 tree fn;
11300 const char *p1;
11302 if (TREE_CODE (s2) != INTEGER_CST)
11303 return NULL_TREE;
11305 p1 = c_getstr (s1);
11306 if (p1 != NULL)
11308 char c;
11309 const char *r;
11310 tree tem;
11312 if (target_char_cast (s2, &c))
11313 return NULL_TREE;
11315 r = strrchr (p1, c);
11317 if (r == NULL)
11318 return build_int_cst (TREE_TYPE (s1), 0);
11320 /* Return an offset into the constant string argument. */
11321 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11322 s1, size_int (r - p1));
11323 return fold_convert_loc (loc, type, tem);
11326 if (! integer_zerop (s2))
11327 return NULL_TREE;
11329 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11330 if (!fn)
11331 return NULL_TREE;
11333 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11334 return build_call_expr_loc (loc, fn, 2, s1, s2);
11338 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11339 to the call, and TYPE is its return type.
11341 Return NULL_TREE if no simplification was possible, otherwise return the
11342 simplified form of the call as a tree.
11344 The simplified form may be a constant or other expression which
11345 computes the same value, but in a more efficient manner (including
11346 calls to other builtin functions).
11348 The call may contain arguments which need to be evaluated, but
11349 which are not useful to determine the result of the call. In
11350 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11351 COMPOUND_EXPR will be an argument which must be evaluated.
11352 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11353 COMPOUND_EXPR in the chain will contain the tree for the simplified
11354 form of the builtin function call. */
11356 static tree
11357 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11359 if (!validate_arg (s1, POINTER_TYPE)
11360 || !validate_arg (s2, POINTER_TYPE))
11361 return NULL_TREE;
11362 else
11364 tree fn;
11365 const char *p1, *p2;
11367 p2 = c_getstr (s2);
11368 if (p2 == NULL)
11369 return NULL_TREE;
11371 p1 = c_getstr (s1);
11372 if (p1 != NULL)
11374 const char *r = strpbrk (p1, p2);
11375 tree tem;
11377 if (r == NULL)
11378 return build_int_cst (TREE_TYPE (s1), 0);
11380 /* Return an offset into the constant string argument. */
11381 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11382 s1, size_int (r - p1));
11383 return fold_convert_loc (loc, type, tem);
11386 if (p2[0] == '\0')
11387 /* strpbrk(x, "") == NULL.
11388 Evaluate and ignore s1 in case it had side-effects. */
11389 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11391 if (p2[1] != '\0')
11392 return NULL_TREE; /* Really call strpbrk. */
11394 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11395 if (!fn)
11396 return NULL_TREE;
11398 /* New argument list transforming strpbrk(s1, s2) to
11399 strchr(s1, s2[0]). */
11400 return build_call_expr_loc (loc, fn, 2, s1,
11401 build_int_cst (integer_type_node, p2[0]));
11405 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11406 to the call.
11408 Return NULL_TREE if no simplification was possible, otherwise return the
11409 simplified form of the call as a tree.
11411 The simplified form may be a constant or other expression which
11412 computes the same value, but in a more efficient manner (including
11413 calls to other builtin functions).
11415 The call may contain arguments which need to be evaluated, but
11416 which are not useful to determine the result of the call. In
11417 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11418 COMPOUND_EXPR will be an argument which must be evaluated.
11419 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11420 COMPOUND_EXPR in the chain will contain the tree for the simplified
11421 form of the builtin function call. */
11423 static tree
11424 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11426 if (!validate_arg (dst, POINTER_TYPE)
11427 || !validate_arg (src, POINTER_TYPE))
11428 return NULL_TREE;
11429 else
11431 const char *p = c_getstr (src);
11433 /* If the string length is zero, return the dst parameter. */
11434 if (p && *p == '\0')
11435 return dst;
11437 if (optimize_insn_for_speed_p ())
11439 /* See if we can store by pieces into (dst + strlen(dst)). */
11440 tree newdst, call;
11441 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11442 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11444 if (!strlen_fn || !strcpy_fn)
11445 return NULL_TREE;
11447 /* If we don't have a movstr we don't want to emit an strcpy
11448 call. We have to do that if the length of the source string
11449 isn't computable (in that case we can use memcpy probably
11450 later expanding to a sequence of mov instructions). If we
11451 have movstr instructions we can emit strcpy calls. */
11452 if (!HAVE_movstr)
11454 tree len = c_strlen (src, 1);
11455 if (! len || TREE_SIDE_EFFECTS (len))
11456 return NULL_TREE;
11459 /* Stabilize the argument list. */
11460 dst = builtin_save_expr (dst);
11462 /* Create strlen (dst). */
11463 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11464 /* Create (dst p+ strlen (dst)). */
11466 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11467 TREE_TYPE (dst), dst, newdst);
11468 newdst = builtin_save_expr (newdst);
11470 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11471 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11473 return NULL_TREE;
11477 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11478 arguments to the call.
11480 Return NULL_TREE if no simplification was possible, otherwise return the
11481 simplified form of the call as a tree.
11483 The simplified form may be a constant or other expression which
11484 computes the same value, but in a more efficient manner (including
11485 calls to other builtin functions).
11487 The call may contain arguments which need to be evaluated, but
11488 which are not useful to determine the result of the call. In
11489 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11490 COMPOUND_EXPR will be an argument which must be evaluated.
11491 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11492 COMPOUND_EXPR in the chain will contain the tree for the simplified
11493 form of the builtin function call. */
11495 static tree
11496 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11498 if (!validate_arg (dst, POINTER_TYPE)
11499 || !validate_arg (src, POINTER_TYPE)
11500 || !validate_arg (len, INTEGER_TYPE))
11501 return NULL_TREE;
11502 else
11504 const char *p = c_getstr (src);
11506 /* If the requested length is zero, or the src parameter string
11507 length is zero, return the dst parameter. */
11508 if (integer_zerop (len) || (p && *p == '\0'))
11509 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11511 /* If the requested len is greater than or equal to the string
11512 length, call strcat. */
11513 if (TREE_CODE (len) == INTEGER_CST && p
11514 && compare_tree_int (len, strlen (p)) >= 0)
11516 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11518 /* If the replacement _DECL isn't initialized, don't do the
11519 transformation. */
11520 if (!fn)
11521 return NULL_TREE;
11523 return build_call_expr_loc (loc, fn, 2, dst, src);
11525 return NULL_TREE;
11529 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11530 to the call.
11532 Return NULL_TREE if no simplification was possible, otherwise return the
11533 simplified form of the call as a tree.
11535 The simplified form may be a constant or other expression which
11536 computes the same value, but in a more efficient manner (including
11537 calls to other builtin functions).
11539 The call may contain arguments which need to be evaluated, but
11540 which are not useful to determine the result of the call. In
11541 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11542 COMPOUND_EXPR will be an argument which must be evaluated.
11543 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11544 COMPOUND_EXPR in the chain will contain the tree for the simplified
11545 form of the builtin function call. */
11547 static tree
11548 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11550 if (!validate_arg (s1, POINTER_TYPE)
11551 || !validate_arg (s2, POINTER_TYPE))
11552 return NULL_TREE;
11553 else
11555 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11557 /* If both arguments are constants, evaluate at compile-time. */
11558 if (p1 && p2)
11560 const size_t r = strspn (p1, p2);
11561 return size_int (r);
11564 /* If either argument is "", return NULL_TREE. */
11565 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11566 /* Evaluate and ignore both arguments in case either one has
11567 side-effects. */
11568 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11569 s1, s2);
11570 return NULL_TREE;
11574 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11575 to the call.
11577 Return NULL_TREE if no simplification was possible, otherwise return the
11578 simplified form of the call as a tree.
11580 The simplified form may be a constant or other expression which
11581 computes the same value, but in a more efficient manner (including
11582 calls to other builtin functions).
11584 The call may contain arguments which need to be evaluated, but
11585 which are not useful to determine the result of the call. In
11586 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11587 COMPOUND_EXPR will be an argument which must be evaluated.
11588 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11589 COMPOUND_EXPR in the chain will contain the tree for the simplified
11590 form of the builtin function call. */
11592 static tree
11593 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11595 if (!validate_arg (s1, POINTER_TYPE)
11596 || !validate_arg (s2, POINTER_TYPE))
11597 return NULL_TREE;
11598 else
11600 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11602 /* If both arguments are constants, evaluate at compile-time. */
11603 if (p1 && p2)
11605 const size_t r = strcspn (p1, p2);
11606 return size_int (r);
11609 /* If the first argument is "", return NULL_TREE. */
11610 if (p1 && *p1 == '\0')
11612 /* Evaluate and ignore argument s2 in case it has
11613 side-effects. */
11614 return omit_one_operand_loc (loc, size_type_node,
11615 size_zero_node, s2);
11618 /* If the second argument is "", return __builtin_strlen(s1). */
11619 if (p2 && *p2 == '\0')
11621 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11623 /* If the replacement _DECL isn't initialized, don't do the
11624 transformation. */
11625 if (!fn)
11626 return NULL_TREE;
11628 return build_call_expr_loc (loc, fn, 1, s1);
11630 return NULL_TREE;
11634 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11635 to the call. IGNORE is true if the value returned
11636 by the builtin will be ignored. UNLOCKED is true is true if this
11637 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11638 the known length of the string. Return NULL_TREE if no simplification
11639 was possible. */
11641 tree
11642 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11643 bool ignore, bool unlocked, tree len)
11645 /* If we're using an unlocked function, assume the other unlocked
11646 functions exist explicitly. */
11647 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11648 : implicit_built_in_decls[BUILT_IN_FPUTC];
11649 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11650 : implicit_built_in_decls[BUILT_IN_FWRITE];
11652 /* If the return value is used, don't do the transformation. */
11653 if (!ignore)
11654 return NULL_TREE;
11656 /* Verify the arguments in the original call. */
11657 if (!validate_arg (arg0, POINTER_TYPE)
11658 || !validate_arg (arg1, POINTER_TYPE))
11659 return NULL_TREE;
11661 if (! len)
11662 len = c_strlen (arg0, 0);
11664 /* Get the length of the string passed to fputs. If the length
11665 can't be determined, punt. */
11666 if (!len
11667 || TREE_CODE (len) != INTEGER_CST)
11668 return NULL_TREE;
11670 switch (compare_tree_int (len, 1))
11672 case -1: /* length is 0, delete the call entirely . */
11673 return omit_one_operand_loc (loc, integer_type_node,
11674 integer_zero_node, arg1);;
11676 case 0: /* length is 1, call fputc. */
11678 const char *p = c_getstr (arg0);
11680 if (p != NULL)
11682 if (fn_fputc)
11683 return build_call_expr_loc (loc, fn_fputc, 2,
11684 build_int_cst
11685 (integer_type_node, p[0]), arg1);
11686 else
11687 return NULL_TREE;
11690 /* FALLTHROUGH */
11691 case 1: /* length is greater than 1, call fwrite. */
11693 /* If optimizing for size keep fputs. */
11694 if (optimize_function_for_size_p (cfun))
11695 return NULL_TREE;
11696 /* New argument list transforming fputs(string, stream) to
11697 fwrite(string, 1, len, stream). */
11698 if (fn_fwrite)
11699 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11700 size_one_node, len, arg1);
11701 else
11702 return NULL_TREE;
11704 default:
11705 gcc_unreachable ();
11707 return NULL_TREE;
11710 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11711 produced. False otherwise. This is done so that we don't output the error
11712 or warning twice or three times. */
11714 bool
11715 fold_builtin_next_arg (tree exp, bool va_start_p)
11717 tree fntype = TREE_TYPE (current_function_decl);
11718 int nargs = call_expr_nargs (exp);
11719 tree arg;
11721 if (!stdarg_p (fntype))
11723 error ("%<va_start%> used in function with fixed args");
11724 return true;
11727 if (va_start_p)
11729 if (va_start_p && (nargs != 2))
11731 error ("wrong number of arguments to function %<va_start%>");
11732 return true;
11734 arg = CALL_EXPR_ARG (exp, 1);
11736 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11737 when we checked the arguments and if needed issued a warning. */
11738 else
11740 if (nargs == 0)
11742 /* Evidently an out of date version of <stdarg.h>; can't validate
11743 va_start's second argument, but can still work as intended. */
11744 warning (0, "%<__builtin_next_arg%> called without an argument");
11745 return true;
11747 else if (nargs > 1)
11749 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11750 return true;
11752 arg = CALL_EXPR_ARG (exp, 0);
11755 if (TREE_CODE (arg) == SSA_NAME)
11756 arg = SSA_NAME_VAR (arg);
11758 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11759 or __builtin_next_arg (0) the first time we see it, after checking
11760 the arguments and if needed issuing a warning. */
11761 if (!integer_zerop (arg))
11763 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11765 /* Strip off all nops for the sake of the comparison. This
11766 is not quite the same as STRIP_NOPS. It does more.
11767 We must also strip off INDIRECT_EXPR for C++ reference
11768 parameters. */
11769 while (CONVERT_EXPR_P (arg)
11770 || TREE_CODE (arg) == INDIRECT_REF)
11771 arg = TREE_OPERAND (arg, 0);
11772 if (arg != last_parm)
11774 /* FIXME: Sometimes with the tree optimizers we can get the
11775 not the last argument even though the user used the last
11776 argument. We just warn and set the arg to be the last
11777 argument so that we will get wrong-code because of
11778 it. */
11779 warning (0, "second parameter of %<va_start%> not last named argument");
11782 /* Undefined by C99 7.15.1.4p4 (va_start):
11783 "If the parameter parmN is declared with the register storage
11784 class, with a function or array type, or with a type that is
11785 not compatible with the type that results after application of
11786 the default argument promotions, the behavior is undefined."
11788 else if (DECL_REGISTER (arg))
11789 warning (0, "undefined behaviour when second parameter of "
11790 "%<va_start%> is declared with %<register%> storage");
11792 /* We want to verify the second parameter just once before the tree
11793 optimizers are run and then avoid keeping it in the tree,
11794 as otherwise we could warn even for correct code like:
11795 void foo (int i, ...)
11796 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11797 if (va_start_p)
11798 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11799 else
11800 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11802 return false;
11806 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11807 ORIG may be null if this is a 2-argument call. We don't attempt to
11808 simplify calls with more than 3 arguments.
11810 Return NULL_TREE if no simplification was possible, otherwise return the
11811 simplified form of the call as a tree. If IGNORED is true, it means that
11812 the caller does not use the returned value of the function. */
11814 static tree
11815 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11816 tree orig, int ignored)
11818 tree call, retval;
11819 const char *fmt_str = NULL;
11821 /* Verify the required arguments in the original call. We deal with two
11822 types of sprintf() calls: 'sprintf (str, fmt)' and
11823 'sprintf (dest, "%s", orig)'. */
11824 if (!validate_arg (dest, POINTER_TYPE)
11825 || !validate_arg (fmt, POINTER_TYPE))
11826 return NULL_TREE;
11827 if (orig && !validate_arg (orig, POINTER_TYPE))
11828 return NULL_TREE;
11830 /* Check whether the format is a literal string constant. */
11831 fmt_str = c_getstr (fmt);
11832 if (fmt_str == NULL)
11833 return NULL_TREE;
11835 call = NULL_TREE;
11836 retval = NULL_TREE;
11838 if (!init_target_chars ())
11839 return NULL_TREE;
11841 /* If the format doesn't contain % args or %%, use strcpy. */
11842 if (strchr (fmt_str, target_percent) == NULL)
11844 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11846 if (!fn)
11847 return NULL_TREE;
11849 /* Don't optimize sprintf (buf, "abc", ptr++). */
11850 if (orig)
11851 return NULL_TREE;
11853 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11854 'format' is known to contain no % formats. */
11855 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11856 if (!ignored)
11857 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11860 /* If the format is "%s", use strcpy if the result isn't used. */
11861 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11863 tree fn;
11864 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11866 if (!fn)
11867 return NULL_TREE;
11869 /* Don't crash on sprintf (str1, "%s"). */
11870 if (!orig)
11871 return NULL_TREE;
11873 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11874 if (!ignored)
11876 retval = c_strlen (orig, 1);
11877 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11878 return NULL_TREE;
11880 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11883 if (call && retval)
11885 retval = fold_convert_loc
11886 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11887 retval);
11888 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11890 else
11891 return call;
11894 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
11895 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
11896 attempt to simplify calls with more than 4 arguments.
11898 Return NULL_TREE if no simplification was possible, otherwise return the
11899 simplified form of the call as a tree. If IGNORED is true, it means that
11900 the caller does not use the returned value of the function. */
11902 static tree
11903 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
11904 tree orig, int ignored)
11906 tree call, retval;
11907 const char *fmt_str = NULL;
11908 unsigned HOST_WIDE_INT destlen;
11910 /* Verify the required arguments in the original call. We deal with two
11911 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
11912 'snprintf (dest, cst, "%s", orig)'. */
11913 if (!validate_arg (dest, POINTER_TYPE)
11914 || !validate_arg (destsize, INTEGER_TYPE)
11915 || !validate_arg (fmt, POINTER_TYPE))
11916 return NULL_TREE;
11917 if (orig && !validate_arg (orig, POINTER_TYPE))
11918 return NULL_TREE;
11920 if (!host_integerp (destsize, 1))
11921 return NULL_TREE;
11923 /* Check whether the format is a literal string constant. */
11924 fmt_str = c_getstr (fmt);
11925 if (fmt_str == NULL)
11926 return NULL_TREE;
11928 call = NULL_TREE;
11929 retval = NULL_TREE;
11931 if (!init_target_chars ())
11932 return NULL_TREE;
11934 destlen = tree_low_cst (destsize, 1);
11936 /* If the format doesn't contain % args or %%, use strcpy. */
11937 if (strchr (fmt_str, target_percent) == NULL)
11939 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11940 size_t len = strlen (fmt_str);
11942 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
11943 if (orig)
11944 return NULL_TREE;
11946 /* We could expand this as
11947 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
11948 or to
11949 memcpy (str, fmt_with_nul_at_cstm1, cst);
11950 but in the former case that might increase code size
11951 and in the latter case grow .rodata section too much.
11952 So punt for now. */
11953 if (len >= destlen)
11954 return NULL_TREE;
11956 if (!fn)
11957 return NULL_TREE;
11959 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
11960 'format' is known to contain no % formats and
11961 strlen (fmt) < cst. */
11962 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11964 if (!ignored)
11965 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11968 /* If the format is "%s", use strcpy if the result isn't used. */
11969 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11971 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11972 unsigned HOST_WIDE_INT origlen;
11974 /* Don't crash on snprintf (str1, cst, "%s"). */
11975 if (!orig)
11976 return NULL_TREE;
11978 retval = c_strlen (orig, 1);
11979 if (!retval || !host_integerp (retval, 1))
11980 return NULL_TREE;
11982 origlen = tree_low_cst (retval, 1);
11983 /* We could expand this as
11984 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
11985 or to
11986 memcpy (str1, str2_with_nul_at_cstm1, cst);
11987 but in the former case that might increase code size
11988 and in the latter case grow .rodata section too much.
11989 So punt for now. */
11990 if (origlen >= destlen)
11991 return NULL_TREE;
11993 /* Convert snprintf (str1, cst, "%s", str2) into
11994 strcpy (str1, str2) if strlen (str2) < cst. */
11995 if (!fn)
11996 return NULL_TREE;
11998 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12000 if (ignored)
12001 retval = NULL_TREE;
12004 if (call && retval)
12006 tree fn = built_in_decls[BUILT_IN_SNPRINTF];
12007 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12008 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12010 else
12011 return call;
12014 /* Expand a call EXP to __builtin_object_size. */
12017 expand_builtin_object_size (tree exp)
12019 tree ost;
12020 int object_size_type;
12021 tree fndecl = get_callee_fndecl (exp);
12023 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12025 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12026 exp, fndecl);
12027 expand_builtin_trap ();
12028 return const0_rtx;
12031 ost = CALL_EXPR_ARG (exp, 1);
12032 STRIP_NOPS (ost);
12034 if (TREE_CODE (ost) != INTEGER_CST
12035 || tree_int_cst_sgn (ost) < 0
12036 || compare_tree_int (ost, 3) > 0)
12038 error ("%Klast argument of %D is not integer constant between 0 and 3",
12039 exp, fndecl);
12040 expand_builtin_trap ();
12041 return const0_rtx;
12044 object_size_type = tree_low_cst (ost, 0);
12046 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12049 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12050 FCODE is the BUILT_IN_* to use.
12051 Return NULL_RTX if we failed; the caller should emit a normal call,
12052 otherwise try to get the result in TARGET, if convenient (and in
12053 mode MODE if that's convenient). */
12055 static rtx
12056 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12057 enum built_in_function fcode)
12059 tree dest, src, len, size;
12061 if (!validate_arglist (exp,
12062 POINTER_TYPE,
12063 fcode == BUILT_IN_MEMSET_CHK
12064 ? INTEGER_TYPE : POINTER_TYPE,
12065 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12066 return NULL_RTX;
12068 dest = CALL_EXPR_ARG (exp, 0);
12069 src = CALL_EXPR_ARG (exp, 1);
12070 len = CALL_EXPR_ARG (exp, 2);
12071 size = CALL_EXPR_ARG (exp, 3);
12073 if (! host_integerp (size, 1))
12074 return NULL_RTX;
12076 if (host_integerp (len, 1) || integer_all_onesp (size))
12078 tree fn;
12080 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12082 warning_at (tree_nonartificial_location (exp),
12083 0, "%Kcall to %D will always overflow destination buffer",
12084 exp, get_callee_fndecl (exp));
12085 return NULL_RTX;
12088 fn = NULL_TREE;
12089 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12090 mem{cpy,pcpy,move,set} is available. */
12091 switch (fcode)
12093 case BUILT_IN_MEMCPY_CHK:
12094 fn = built_in_decls[BUILT_IN_MEMCPY];
12095 break;
12096 case BUILT_IN_MEMPCPY_CHK:
12097 fn = built_in_decls[BUILT_IN_MEMPCPY];
12098 break;
12099 case BUILT_IN_MEMMOVE_CHK:
12100 fn = built_in_decls[BUILT_IN_MEMMOVE];
12101 break;
12102 case BUILT_IN_MEMSET_CHK:
12103 fn = built_in_decls[BUILT_IN_MEMSET];
12104 break;
12105 default:
12106 break;
12109 if (! fn)
12110 return NULL_RTX;
12112 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12113 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12114 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12115 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12117 else if (fcode == BUILT_IN_MEMSET_CHK)
12118 return NULL_RTX;
12119 else
12121 unsigned int dest_align
12122 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12124 /* If DEST is not a pointer type, call the normal function. */
12125 if (dest_align == 0)
12126 return NULL_RTX;
12128 /* If SRC and DEST are the same (and not volatile), do nothing. */
12129 if (operand_equal_p (src, dest, 0))
12131 tree expr;
12133 if (fcode != BUILT_IN_MEMPCPY_CHK)
12135 /* Evaluate and ignore LEN in case it has side-effects. */
12136 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12137 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12140 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12141 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12144 /* __memmove_chk special case. */
12145 if (fcode == BUILT_IN_MEMMOVE_CHK)
12147 unsigned int src_align
12148 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12150 if (src_align == 0)
12151 return NULL_RTX;
12153 /* If src is categorized for a readonly section we can use
12154 normal __memcpy_chk. */
12155 if (readonly_data_expr (src))
12157 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12158 if (!fn)
12159 return NULL_RTX;
12160 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12161 dest, src, len, size);
12162 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12163 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12164 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12167 return NULL_RTX;
12171 /* Emit warning if a buffer overflow is detected at compile time. */
12173 static void
12174 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12176 int is_strlen = 0;
12177 tree len, size;
12178 location_t loc = tree_nonartificial_location (exp);
12180 switch (fcode)
12182 case BUILT_IN_STRCPY_CHK:
12183 case BUILT_IN_STPCPY_CHK:
12184 /* For __strcat_chk the warning will be emitted only if overflowing
12185 by at least strlen (dest) + 1 bytes. */
12186 case BUILT_IN_STRCAT_CHK:
12187 len = CALL_EXPR_ARG (exp, 1);
12188 size = CALL_EXPR_ARG (exp, 2);
12189 is_strlen = 1;
12190 break;
12191 case BUILT_IN_STRNCAT_CHK:
12192 case BUILT_IN_STRNCPY_CHK:
12193 len = CALL_EXPR_ARG (exp, 2);
12194 size = CALL_EXPR_ARG (exp, 3);
12195 break;
12196 case BUILT_IN_SNPRINTF_CHK:
12197 case BUILT_IN_VSNPRINTF_CHK:
12198 len = CALL_EXPR_ARG (exp, 1);
12199 size = CALL_EXPR_ARG (exp, 3);
12200 break;
12201 default:
12202 gcc_unreachable ();
12205 if (!len || !size)
12206 return;
12208 if (! host_integerp (size, 1) || integer_all_onesp (size))
12209 return;
12211 if (is_strlen)
12213 len = c_strlen (len, 1);
12214 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12215 return;
12217 else if (fcode == BUILT_IN_STRNCAT_CHK)
12219 tree src = CALL_EXPR_ARG (exp, 1);
12220 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12221 return;
12222 src = c_strlen (src, 1);
12223 if (! src || ! host_integerp (src, 1))
12225 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12226 exp, get_callee_fndecl (exp));
12227 return;
12229 else if (tree_int_cst_lt (src, size))
12230 return;
12232 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12233 return;
12235 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12236 exp, get_callee_fndecl (exp));
12239 /* Emit warning if a buffer overflow is detected at compile time
12240 in __sprintf_chk/__vsprintf_chk calls. */
12242 static void
12243 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12245 tree size, len, fmt;
12246 const char *fmt_str;
12247 int nargs = call_expr_nargs (exp);
12249 /* Verify the required arguments in the original call. */
12251 if (nargs < 4)
12252 return;
12253 size = CALL_EXPR_ARG (exp, 2);
12254 fmt = CALL_EXPR_ARG (exp, 3);
12256 if (! host_integerp (size, 1) || integer_all_onesp (size))
12257 return;
12259 /* Check whether the format is a literal string constant. */
12260 fmt_str = c_getstr (fmt);
12261 if (fmt_str == NULL)
12262 return;
12264 if (!init_target_chars ())
12265 return;
12267 /* If the format doesn't contain % args or %%, we know its size. */
12268 if (strchr (fmt_str, target_percent) == 0)
12269 len = build_int_cstu (size_type_node, strlen (fmt_str));
12270 /* If the format is "%s" and first ... argument is a string literal,
12271 we know it too. */
12272 else if (fcode == BUILT_IN_SPRINTF_CHK
12273 && strcmp (fmt_str, target_percent_s) == 0)
12275 tree arg;
12277 if (nargs < 5)
12278 return;
12279 arg = CALL_EXPR_ARG (exp, 4);
12280 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12281 return;
12283 len = c_strlen (arg, 1);
12284 if (!len || ! host_integerp (len, 1))
12285 return;
12287 else
12288 return;
12290 if (! tree_int_cst_lt (len, size))
12291 warning_at (tree_nonartificial_location (exp),
12292 0, "%Kcall to %D will always overflow destination buffer",
12293 exp, get_callee_fndecl (exp));
12296 /* Emit warning if a free is called with address of a variable. */
12298 static void
12299 maybe_emit_free_warning (tree exp)
12301 tree arg = CALL_EXPR_ARG (exp, 0);
12303 STRIP_NOPS (arg);
12304 if (TREE_CODE (arg) != ADDR_EXPR)
12305 return;
12307 arg = get_base_address (TREE_OPERAND (arg, 0));
12308 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12309 return;
12311 if (SSA_VAR_P (arg))
12312 warning_at (tree_nonartificial_location (exp),
12313 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12314 else
12315 warning_at (tree_nonartificial_location (exp),
12316 0, "%Kattempt to free a non-heap object", exp);
12319 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12320 if possible. */
12322 tree
12323 fold_builtin_object_size (tree ptr, tree ost)
12325 unsigned HOST_WIDE_INT bytes;
12326 int object_size_type;
12328 if (!validate_arg (ptr, POINTER_TYPE)
12329 || !validate_arg (ost, INTEGER_TYPE))
12330 return NULL_TREE;
12332 STRIP_NOPS (ost);
12334 if (TREE_CODE (ost) != INTEGER_CST
12335 || tree_int_cst_sgn (ost) < 0
12336 || compare_tree_int (ost, 3) > 0)
12337 return NULL_TREE;
12339 object_size_type = tree_low_cst (ost, 0);
12341 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12342 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12343 and (size_t) 0 for types 2 and 3. */
12344 if (TREE_SIDE_EFFECTS (ptr))
12345 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12347 if (TREE_CODE (ptr) == ADDR_EXPR)
12349 bytes = compute_builtin_object_size (ptr, object_size_type);
12350 if (double_int_fits_to_tree_p (size_type_node,
12351 uhwi_to_double_int (bytes)))
12352 return build_int_cstu (size_type_node, bytes);
12354 else if (TREE_CODE (ptr) == SSA_NAME)
12356 /* If object size is not known yet, delay folding until
12357 later. Maybe subsequent passes will help determining
12358 it. */
12359 bytes = compute_builtin_object_size (ptr, object_size_type);
12360 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12361 && double_int_fits_to_tree_p (size_type_node,
12362 uhwi_to_double_int (bytes)))
12363 return build_int_cstu (size_type_node, bytes);
12366 return NULL_TREE;
12369 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12370 DEST, SRC, LEN, and SIZE are the arguments to the call.
12371 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12372 code of the builtin. If MAXLEN is not NULL, it is maximum length
12373 passed as third argument. */
12375 tree
12376 fold_builtin_memory_chk (location_t loc, tree fndecl,
12377 tree dest, tree src, tree len, tree size,
12378 tree maxlen, bool ignore,
12379 enum built_in_function fcode)
12381 tree fn;
12383 if (!validate_arg (dest, POINTER_TYPE)
12384 || !validate_arg (src,
12385 (fcode == BUILT_IN_MEMSET_CHK
12386 ? INTEGER_TYPE : POINTER_TYPE))
12387 || !validate_arg (len, INTEGER_TYPE)
12388 || !validate_arg (size, INTEGER_TYPE))
12389 return NULL_TREE;
12391 /* If SRC and DEST are the same (and not volatile), return DEST
12392 (resp. DEST+LEN for __mempcpy_chk). */
12393 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12395 if (fcode != BUILT_IN_MEMPCPY_CHK)
12396 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12397 dest, len);
12398 else
12400 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12401 dest, len);
12402 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12406 if (! host_integerp (size, 1))
12407 return NULL_TREE;
12409 if (! integer_all_onesp (size))
12411 if (! host_integerp (len, 1))
12413 /* If LEN is not constant, try MAXLEN too.
12414 For MAXLEN only allow optimizing into non-_ocs function
12415 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12416 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12418 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12420 /* (void) __mempcpy_chk () can be optimized into
12421 (void) __memcpy_chk (). */
12422 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12423 if (!fn)
12424 return NULL_TREE;
12426 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12428 return NULL_TREE;
12431 else
12432 maxlen = len;
12434 if (tree_int_cst_lt (size, maxlen))
12435 return NULL_TREE;
12438 fn = NULL_TREE;
12439 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12440 mem{cpy,pcpy,move,set} is available. */
12441 switch (fcode)
12443 case BUILT_IN_MEMCPY_CHK:
12444 fn = built_in_decls[BUILT_IN_MEMCPY];
12445 break;
12446 case BUILT_IN_MEMPCPY_CHK:
12447 fn = built_in_decls[BUILT_IN_MEMPCPY];
12448 break;
12449 case BUILT_IN_MEMMOVE_CHK:
12450 fn = built_in_decls[BUILT_IN_MEMMOVE];
12451 break;
12452 case BUILT_IN_MEMSET_CHK:
12453 fn = built_in_decls[BUILT_IN_MEMSET];
12454 break;
12455 default:
12456 break;
12459 if (!fn)
12460 return NULL_TREE;
12462 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12465 /* Fold a call to the __st[rp]cpy_chk builtin.
12466 DEST, SRC, and SIZE are the arguments to the call.
12467 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12468 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12469 strings passed as second argument. */
12471 tree
12472 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12473 tree src, tree size,
12474 tree maxlen, bool ignore,
12475 enum built_in_function fcode)
12477 tree len, fn;
12479 if (!validate_arg (dest, POINTER_TYPE)
12480 || !validate_arg (src, POINTER_TYPE)
12481 || !validate_arg (size, INTEGER_TYPE))
12482 return NULL_TREE;
12484 /* If SRC and DEST are the same (and not volatile), return DEST. */
12485 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12486 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12488 if (! host_integerp (size, 1))
12489 return NULL_TREE;
12491 if (! integer_all_onesp (size))
12493 len = c_strlen (src, 1);
12494 if (! len || ! host_integerp (len, 1))
12496 /* If LEN is not constant, try MAXLEN too.
12497 For MAXLEN only allow optimizing into non-_ocs function
12498 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12499 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12501 if (fcode == BUILT_IN_STPCPY_CHK)
12503 if (! ignore)
12504 return NULL_TREE;
12506 /* If return value of __stpcpy_chk is ignored,
12507 optimize into __strcpy_chk. */
12508 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12509 if (!fn)
12510 return NULL_TREE;
12512 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12515 if (! len || TREE_SIDE_EFFECTS (len))
12516 return NULL_TREE;
12518 /* If c_strlen returned something, but not a constant,
12519 transform __strcpy_chk into __memcpy_chk. */
12520 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12521 if (!fn)
12522 return NULL_TREE;
12524 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12525 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12526 build_call_expr_loc (loc, fn, 4,
12527 dest, src, len, size));
12530 else
12531 maxlen = len;
12533 if (! tree_int_cst_lt (maxlen, size))
12534 return NULL_TREE;
12537 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12538 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12539 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12540 if (!fn)
12541 return NULL_TREE;
12543 return build_call_expr_loc (loc, fn, 2, dest, src);
12546 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12547 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12548 length passed as third argument. */
12550 tree
12551 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12552 tree len, tree size, tree maxlen)
12554 tree fn;
12556 if (!validate_arg (dest, POINTER_TYPE)
12557 || !validate_arg (src, POINTER_TYPE)
12558 || !validate_arg (len, INTEGER_TYPE)
12559 || !validate_arg (size, INTEGER_TYPE))
12560 return NULL_TREE;
12562 if (! host_integerp (size, 1))
12563 return NULL_TREE;
12565 if (! integer_all_onesp (size))
12567 if (! host_integerp (len, 1))
12569 /* If LEN is not constant, try MAXLEN too.
12570 For MAXLEN only allow optimizing into non-_ocs function
12571 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12572 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12573 return NULL_TREE;
12575 else
12576 maxlen = len;
12578 if (tree_int_cst_lt (size, maxlen))
12579 return NULL_TREE;
12582 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12583 fn = built_in_decls[BUILT_IN_STRNCPY];
12584 if (!fn)
12585 return NULL_TREE;
12587 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12590 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12591 are the arguments to the call. */
12593 static tree
12594 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12595 tree src, tree size)
12597 tree fn;
12598 const char *p;
12600 if (!validate_arg (dest, POINTER_TYPE)
12601 || !validate_arg (src, POINTER_TYPE)
12602 || !validate_arg (size, INTEGER_TYPE))
12603 return NULL_TREE;
12605 p = c_getstr (src);
12606 /* If the SRC parameter is "", return DEST. */
12607 if (p && *p == '\0')
12608 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12610 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12611 return NULL_TREE;
12613 /* If __builtin_strcat_chk is used, assume strcat is available. */
12614 fn = built_in_decls[BUILT_IN_STRCAT];
12615 if (!fn)
12616 return NULL_TREE;
12618 return build_call_expr_loc (loc, fn, 2, dest, src);
12621 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12622 LEN, and SIZE. */
12624 static tree
12625 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12626 tree dest, tree src, tree len, tree size)
12628 tree fn;
12629 const char *p;
12631 if (!validate_arg (dest, POINTER_TYPE)
12632 || !validate_arg (src, POINTER_TYPE)
12633 || !validate_arg (size, INTEGER_TYPE)
12634 || !validate_arg (size, INTEGER_TYPE))
12635 return NULL_TREE;
12637 p = c_getstr (src);
12638 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12639 if (p && *p == '\0')
12640 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12641 else if (integer_zerop (len))
12642 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12644 if (! host_integerp (size, 1))
12645 return NULL_TREE;
12647 if (! integer_all_onesp (size))
12649 tree src_len = c_strlen (src, 1);
12650 if (src_len
12651 && host_integerp (src_len, 1)
12652 && host_integerp (len, 1)
12653 && ! tree_int_cst_lt (len, src_len))
12655 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12656 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12657 if (!fn)
12658 return NULL_TREE;
12660 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12662 return NULL_TREE;
12665 /* If __builtin_strncat_chk is used, assume strncat is available. */
12666 fn = built_in_decls[BUILT_IN_STRNCAT];
12667 if (!fn)
12668 return NULL_TREE;
12670 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12673 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12674 Return NULL_TREE if a normal call should be emitted rather than
12675 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12676 or BUILT_IN_VSPRINTF_CHK. */
12678 static tree
12679 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12680 enum built_in_function fcode)
12682 tree dest, size, len, fn, fmt, flag;
12683 const char *fmt_str;
12685 /* Verify the required arguments in the original call. */
12686 if (nargs < 4)
12687 return NULL_TREE;
12688 dest = args[0];
12689 if (!validate_arg (dest, POINTER_TYPE))
12690 return NULL_TREE;
12691 flag = args[1];
12692 if (!validate_arg (flag, INTEGER_TYPE))
12693 return NULL_TREE;
12694 size = args[2];
12695 if (!validate_arg (size, INTEGER_TYPE))
12696 return NULL_TREE;
12697 fmt = args[3];
12698 if (!validate_arg (fmt, POINTER_TYPE))
12699 return NULL_TREE;
12701 if (! host_integerp (size, 1))
12702 return NULL_TREE;
12704 len = NULL_TREE;
12706 if (!init_target_chars ())
12707 return NULL_TREE;
12709 /* Check whether the format is a literal string constant. */
12710 fmt_str = c_getstr (fmt);
12711 if (fmt_str != NULL)
12713 /* If the format doesn't contain % args or %%, we know the size. */
12714 if (strchr (fmt_str, target_percent) == 0)
12716 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12717 len = build_int_cstu (size_type_node, strlen (fmt_str));
12719 /* If the format is "%s" and first ... argument is a string literal,
12720 we know the size too. */
12721 else if (fcode == BUILT_IN_SPRINTF_CHK
12722 && strcmp (fmt_str, target_percent_s) == 0)
12724 tree arg;
12726 if (nargs == 5)
12728 arg = args[4];
12729 if (validate_arg (arg, POINTER_TYPE))
12731 len = c_strlen (arg, 1);
12732 if (! len || ! host_integerp (len, 1))
12733 len = NULL_TREE;
12739 if (! integer_all_onesp (size))
12741 if (! len || ! tree_int_cst_lt (len, size))
12742 return NULL_TREE;
12745 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12746 or if format doesn't contain % chars or is "%s". */
12747 if (! integer_zerop (flag))
12749 if (fmt_str == NULL)
12750 return NULL_TREE;
12751 if (strchr (fmt_str, target_percent) != NULL
12752 && strcmp (fmt_str, target_percent_s))
12753 return NULL_TREE;
12756 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12757 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12758 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12759 if (!fn)
12760 return NULL_TREE;
12762 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12765 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12766 a normal call should be emitted rather than expanding the function
12767 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12769 static tree
12770 fold_builtin_sprintf_chk (location_t loc, tree exp,
12771 enum built_in_function fcode)
12773 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12774 CALL_EXPR_ARGP (exp), fcode);
12777 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12778 NULL_TREE if a normal call should be emitted rather than expanding
12779 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12780 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12781 passed as second argument. */
12783 static tree
12784 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12785 tree maxlen, enum built_in_function fcode)
12787 tree dest, size, len, fn, fmt, flag;
12788 const char *fmt_str;
12790 /* Verify the required arguments in the original call. */
12791 if (nargs < 5)
12792 return NULL_TREE;
12793 dest = args[0];
12794 if (!validate_arg (dest, POINTER_TYPE))
12795 return NULL_TREE;
12796 len = args[1];
12797 if (!validate_arg (len, INTEGER_TYPE))
12798 return NULL_TREE;
12799 flag = args[2];
12800 if (!validate_arg (flag, INTEGER_TYPE))
12801 return NULL_TREE;
12802 size = args[3];
12803 if (!validate_arg (size, INTEGER_TYPE))
12804 return NULL_TREE;
12805 fmt = args[4];
12806 if (!validate_arg (fmt, POINTER_TYPE))
12807 return NULL_TREE;
12809 if (! host_integerp (size, 1))
12810 return NULL_TREE;
12812 if (! integer_all_onesp (size))
12814 if (! host_integerp (len, 1))
12816 /* If LEN is not constant, try MAXLEN too.
12817 For MAXLEN only allow optimizing into non-_ocs function
12818 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12819 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12820 return NULL_TREE;
12822 else
12823 maxlen = len;
12825 if (tree_int_cst_lt (size, maxlen))
12826 return NULL_TREE;
12829 if (!init_target_chars ())
12830 return NULL_TREE;
12832 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12833 or if format doesn't contain % chars or is "%s". */
12834 if (! integer_zerop (flag))
12836 fmt_str = c_getstr (fmt);
12837 if (fmt_str == NULL)
12838 return NULL_TREE;
12839 if (strchr (fmt_str, target_percent) != NULL
12840 && strcmp (fmt_str, target_percent_s))
12841 return NULL_TREE;
12844 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12845 available. */
12846 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12847 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12848 if (!fn)
12849 return NULL_TREE;
12851 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12854 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12855 a normal call should be emitted rather than expanding the function
12856 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12857 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12858 passed as second argument. */
12860 tree
12861 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12862 enum built_in_function fcode)
12864 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12865 CALL_EXPR_ARGP (exp), maxlen, fcode);
12868 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12869 FMT and ARG are the arguments to the call; we don't fold cases with
12870 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12872 Return NULL_TREE if no simplification was possible, otherwise return the
12873 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12874 code of the function to be simplified. */
12876 static tree
12877 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12878 tree arg, bool ignore,
12879 enum built_in_function fcode)
12881 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12882 const char *fmt_str = NULL;
12884 /* If the return value is used, don't do the transformation. */
12885 if (! ignore)
12886 return NULL_TREE;
12888 /* Verify the required arguments in the original call. */
12889 if (!validate_arg (fmt, POINTER_TYPE))
12890 return NULL_TREE;
12892 /* Check whether the format is a literal string constant. */
12893 fmt_str = c_getstr (fmt);
12894 if (fmt_str == NULL)
12895 return NULL_TREE;
12897 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12899 /* If we're using an unlocked function, assume the other
12900 unlocked functions exist explicitly. */
12901 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12902 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12904 else
12906 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12907 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12910 if (!init_target_chars ())
12911 return NULL_TREE;
12913 if (strcmp (fmt_str, target_percent_s) == 0
12914 || strchr (fmt_str, target_percent) == NULL)
12916 const char *str;
12918 if (strcmp (fmt_str, target_percent_s) == 0)
12920 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12921 return NULL_TREE;
12923 if (!arg || !validate_arg (arg, POINTER_TYPE))
12924 return NULL_TREE;
12926 str = c_getstr (arg);
12927 if (str == NULL)
12928 return NULL_TREE;
12930 else
12932 /* The format specifier doesn't contain any '%' characters. */
12933 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12934 && arg)
12935 return NULL_TREE;
12936 str = fmt_str;
12939 /* If the string was "", printf does nothing. */
12940 if (str[0] == '\0')
12941 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12943 /* If the string has length of 1, call putchar. */
12944 if (str[1] == '\0')
12946 /* Given printf("c"), (where c is any one character,)
12947 convert "c"[0] to an int and pass that to the replacement
12948 function. */
12949 newarg = build_int_cst (integer_type_node, str[0]);
12950 if (fn_putchar)
12951 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12953 else
12955 /* If the string was "string\n", call puts("string"). */
12956 size_t len = strlen (str);
12957 if ((unsigned char)str[len - 1] == target_newline
12958 && (size_t) (int) len == len
12959 && (int) len > 0)
12961 char *newstr;
12962 tree offset_node, string_cst;
12964 /* Create a NUL-terminated string that's one char shorter
12965 than the original, stripping off the trailing '\n'. */
12966 newarg = build_string_literal (len, str);
12967 string_cst = string_constant (newarg, &offset_node);
12968 gcc_checking_assert (string_cst
12969 && (TREE_STRING_LENGTH (string_cst)
12970 == (int) len)
12971 && integer_zerop (offset_node)
12972 && (unsigned char)
12973 TREE_STRING_POINTER (string_cst)[len - 1]
12974 == target_newline);
12975 /* build_string_literal creates a new STRING_CST,
12976 modify it in place to avoid double copying. */
12977 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
12978 newstr[len - 1] = '\0';
12979 if (fn_puts)
12980 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12982 else
12983 /* We'd like to arrange to call fputs(string,stdout) here,
12984 but we need stdout and don't have a way to get it yet. */
12985 return NULL_TREE;
12989 /* The other optimizations can be done only on the non-va_list variants. */
12990 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12991 return NULL_TREE;
12993 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12994 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12996 if (!arg || !validate_arg (arg, POINTER_TYPE))
12997 return NULL_TREE;
12998 if (fn_puts)
12999 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13002 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13003 else if (strcmp (fmt_str, target_percent_c) == 0)
13005 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13006 return NULL_TREE;
13007 if (fn_putchar)
13008 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13011 if (!call)
13012 return NULL_TREE;
13014 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13017 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13018 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13019 more than 3 arguments, and ARG may be null in the 2-argument case.
13021 Return NULL_TREE if no simplification was possible, otherwise return the
13022 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13023 code of the function to be simplified. */
13025 static tree
13026 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13027 tree fmt, tree arg, bool ignore,
13028 enum built_in_function fcode)
13030 tree fn_fputc, fn_fputs, call = NULL_TREE;
13031 const char *fmt_str = NULL;
13033 /* If the return value is used, don't do the transformation. */
13034 if (! ignore)
13035 return NULL_TREE;
13037 /* Verify the required arguments in the original call. */
13038 if (!validate_arg (fp, POINTER_TYPE))
13039 return NULL_TREE;
13040 if (!validate_arg (fmt, POINTER_TYPE))
13041 return NULL_TREE;
13043 /* Check whether the format is a literal string constant. */
13044 fmt_str = c_getstr (fmt);
13045 if (fmt_str == NULL)
13046 return NULL_TREE;
13048 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13050 /* If we're using an unlocked function, assume the other
13051 unlocked functions exist explicitly. */
13052 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
13053 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
13055 else
13057 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
13058 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
13061 if (!init_target_chars ())
13062 return NULL_TREE;
13064 /* If the format doesn't contain % args or %%, use strcpy. */
13065 if (strchr (fmt_str, target_percent) == NULL)
13067 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13068 && arg)
13069 return NULL_TREE;
13071 /* If the format specifier was "", fprintf does nothing. */
13072 if (fmt_str[0] == '\0')
13074 /* If FP has side-effects, just wait until gimplification is
13075 done. */
13076 if (TREE_SIDE_EFFECTS (fp))
13077 return NULL_TREE;
13079 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13082 /* When "string" doesn't contain %, replace all cases of
13083 fprintf (fp, string) with fputs (string, fp). The fputs
13084 builtin will take care of special cases like length == 1. */
13085 if (fn_fputs)
13086 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13089 /* The other optimizations can be done only on the non-va_list variants. */
13090 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13091 return NULL_TREE;
13093 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13094 else if (strcmp (fmt_str, target_percent_s) == 0)
13096 if (!arg || !validate_arg (arg, POINTER_TYPE))
13097 return NULL_TREE;
13098 if (fn_fputs)
13099 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13102 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13103 else if (strcmp (fmt_str, target_percent_c) == 0)
13105 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13106 return NULL_TREE;
13107 if (fn_fputc)
13108 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13111 if (!call)
13112 return NULL_TREE;
13113 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13116 /* Initialize format string characters in the target charset. */
13118 static bool
13119 init_target_chars (void)
13121 static bool init;
13122 if (!init)
13124 target_newline = lang_hooks.to_target_charset ('\n');
13125 target_percent = lang_hooks.to_target_charset ('%');
13126 target_c = lang_hooks.to_target_charset ('c');
13127 target_s = lang_hooks.to_target_charset ('s');
13128 if (target_newline == 0 || target_percent == 0 || target_c == 0
13129 || target_s == 0)
13130 return false;
13132 target_percent_c[0] = target_percent;
13133 target_percent_c[1] = target_c;
13134 target_percent_c[2] = '\0';
13136 target_percent_s[0] = target_percent;
13137 target_percent_s[1] = target_s;
13138 target_percent_s[2] = '\0';
13140 target_percent_s_newline[0] = target_percent;
13141 target_percent_s_newline[1] = target_s;
13142 target_percent_s_newline[2] = target_newline;
13143 target_percent_s_newline[3] = '\0';
13145 init = true;
13147 return true;
13150 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13151 and no overflow/underflow occurred. INEXACT is true if M was not
13152 exactly calculated. TYPE is the tree type for the result. This
13153 function assumes that you cleared the MPFR flags and then
13154 calculated M to see if anything subsequently set a flag prior to
13155 entering this function. Return NULL_TREE if any checks fail. */
13157 static tree
13158 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13160 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13161 overflow/underflow occurred. If -frounding-math, proceed iff the
13162 result of calling FUNC was exact. */
13163 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13164 && (!flag_rounding_math || !inexact))
13166 REAL_VALUE_TYPE rr;
13168 real_from_mpfr (&rr, m, type, GMP_RNDN);
13169 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13170 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13171 but the mpft_t is not, then we underflowed in the
13172 conversion. */
13173 if (real_isfinite (&rr)
13174 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13176 REAL_VALUE_TYPE rmode;
13178 real_convert (&rmode, TYPE_MODE (type), &rr);
13179 /* Proceed iff the specified mode can hold the value. */
13180 if (real_identical (&rmode, &rr))
13181 return build_real (type, rmode);
13184 return NULL_TREE;
13187 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13188 number and no overflow/underflow occurred. INEXACT is true if M
13189 was not exactly calculated. TYPE is the tree type for the result.
13190 This function assumes that you cleared the MPFR flags and then
13191 calculated M to see if anything subsequently set a flag prior to
13192 entering this function. Return NULL_TREE if any checks fail, if
13193 FORCE_CONVERT is true, then bypass the checks. */
13195 static tree
13196 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13198 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13199 overflow/underflow occurred. If -frounding-math, proceed iff the
13200 result of calling FUNC was exact. */
13201 if (force_convert
13202 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13203 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13204 && (!flag_rounding_math || !inexact)))
13206 REAL_VALUE_TYPE re, im;
13208 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13209 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13210 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13211 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13212 but the mpft_t is not, then we underflowed in the
13213 conversion. */
13214 if (force_convert
13215 || (real_isfinite (&re) && real_isfinite (&im)
13216 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13217 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13219 REAL_VALUE_TYPE re_mode, im_mode;
13221 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13222 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13223 /* Proceed iff the specified mode can hold the value. */
13224 if (force_convert
13225 || (real_identical (&re_mode, &re)
13226 && real_identical (&im_mode, &im)))
13227 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13228 build_real (TREE_TYPE (type), im_mode));
13231 return NULL_TREE;
13234 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13235 FUNC on it and return the resulting value as a tree with type TYPE.
13236 If MIN and/or MAX are not NULL, then the supplied ARG must be
13237 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13238 acceptable values, otherwise they are not. The mpfr precision is
13239 set to the precision of TYPE. We assume that function FUNC returns
13240 zero if the result could be calculated exactly within the requested
13241 precision. */
13243 static tree
13244 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13245 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13246 bool inclusive)
13248 tree result = NULL_TREE;
13250 STRIP_NOPS (arg);
13252 /* To proceed, MPFR must exactly represent the target floating point
13253 format, which only happens when the target base equals two. */
13254 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13255 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13257 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13259 if (real_isfinite (ra)
13260 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13261 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13263 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13264 const int prec = fmt->p;
13265 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13266 int inexact;
13267 mpfr_t m;
13269 mpfr_init2 (m, prec);
13270 mpfr_from_real (m, ra, GMP_RNDN);
13271 mpfr_clear_flags ();
13272 inexact = func (m, m, rnd);
13273 result = do_mpfr_ckconv (m, type, inexact);
13274 mpfr_clear (m);
13278 return result;
13281 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13282 FUNC on it and return the resulting value as a tree with type TYPE.
13283 The mpfr precision is set to the precision of TYPE. We assume that
13284 function FUNC returns zero if the result could be calculated
13285 exactly within the requested precision. */
13287 static tree
13288 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13289 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13291 tree result = NULL_TREE;
13293 STRIP_NOPS (arg1);
13294 STRIP_NOPS (arg2);
13296 /* To proceed, MPFR must exactly represent the target floating point
13297 format, which only happens when the target base equals two. */
13298 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13299 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13300 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13302 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13303 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13305 if (real_isfinite (ra1) && real_isfinite (ra2))
13307 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13308 const int prec = fmt->p;
13309 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13310 int inexact;
13311 mpfr_t m1, m2;
13313 mpfr_inits2 (prec, m1, m2, NULL);
13314 mpfr_from_real (m1, ra1, GMP_RNDN);
13315 mpfr_from_real (m2, ra2, GMP_RNDN);
13316 mpfr_clear_flags ();
13317 inexact = func (m1, m1, m2, rnd);
13318 result = do_mpfr_ckconv (m1, type, inexact);
13319 mpfr_clears (m1, m2, NULL);
13323 return result;
13326 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13327 FUNC on it and return the resulting value as a tree with type TYPE.
13328 The mpfr precision is set to the precision of TYPE. We assume that
13329 function FUNC returns zero if the result could be calculated
13330 exactly within the requested precision. */
13332 static tree
13333 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13334 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13336 tree result = NULL_TREE;
13338 STRIP_NOPS (arg1);
13339 STRIP_NOPS (arg2);
13340 STRIP_NOPS (arg3);
13342 /* To proceed, MPFR must exactly represent the target floating point
13343 format, which only happens when the target base equals two. */
13344 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13345 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13346 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13347 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13349 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13350 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13351 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13353 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13355 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13356 const int prec = fmt->p;
13357 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13358 int inexact;
13359 mpfr_t m1, m2, m3;
13361 mpfr_inits2 (prec, m1, m2, m3, NULL);
13362 mpfr_from_real (m1, ra1, GMP_RNDN);
13363 mpfr_from_real (m2, ra2, GMP_RNDN);
13364 mpfr_from_real (m3, ra3, GMP_RNDN);
13365 mpfr_clear_flags ();
13366 inexact = func (m1, m1, m2, m3, rnd);
13367 result = do_mpfr_ckconv (m1, type, inexact);
13368 mpfr_clears (m1, m2, m3, NULL);
13372 return result;
13375 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13376 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13377 If ARG_SINP and ARG_COSP are NULL then the result is returned
13378 as a complex value.
13379 The type is taken from the type of ARG and is used for setting the
13380 precision of the calculation and results. */
13382 static tree
13383 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13385 tree const type = TREE_TYPE (arg);
13386 tree result = NULL_TREE;
13388 STRIP_NOPS (arg);
13390 /* To proceed, MPFR must exactly represent the target floating point
13391 format, which only happens when the target base equals two. */
13392 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13393 && TREE_CODE (arg) == REAL_CST
13394 && !TREE_OVERFLOW (arg))
13396 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13398 if (real_isfinite (ra))
13400 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13401 const int prec = fmt->p;
13402 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13403 tree result_s, result_c;
13404 int inexact;
13405 mpfr_t m, ms, mc;
13407 mpfr_inits2 (prec, m, ms, mc, NULL);
13408 mpfr_from_real (m, ra, GMP_RNDN);
13409 mpfr_clear_flags ();
13410 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13411 result_s = do_mpfr_ckconv (ms, type, inexact);
13412 result_c = do_mpfr_ckconv (mc, type, inexact);
13413 mpfr_clears (m, ms, mc, NULL);
13414 if (result_s && result_c)
13416 /* If we are to return in a complex value do so. */
13417 if (!arg_sinp && !arg_cosp)
13418 return build_complex (build_complex_type (type),
13419 result_c, result_s);
13421 /* Dereference the sin/cos pointer arguments. */
13422 arg_sinp = build_fold_indirect_ref (arg_sinp);
13423 arg_cosp = build_fold_indirect_ref (arg_cosp);
13424 /* Proceed if valid pointer type were passed in. */
13425 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13426 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13428 /* Set the values. */
13429 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13430 result_s);
13431 TREE_SIDE_EFFECTS (result_s) = 1;
13432 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13433 result_c);
13434 TREE_SIDE_EFFECTS (result_c) = 1;
13435 /* Combine the assignments into a compound expr. */
13436 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13437 result_s, result_c));
13442 return result;
13445 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13446 two-argument mpfr order N Bessel function FUNC on them and return
13447 the resulting value as a tree with type TYPE. The mpfr precision
13448 is set to the precision of TYPE. We assume that function FUNC
13449 returns zero if the result could be calculated exactly within the
13450 requested precision. */
13451 static tree
13452 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13453 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13454 const REAL_VALUE_TYPE *min, bool inclusive)
13456 tree result = NULL_TREE;
13458 STRIP_NOPS (arg1);
13459 STRIP_NOPS (arg2);
13461 /* To proceed, MPFR must exactly represent the target floating point
13462 format, which only happens when the target base equals two. */
13463 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13464 && host_integerp (arg1, 0)
13465 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13467 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13468 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13470 if (n == (long)n
13471 && real_isfinite (ra)
13472 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13474 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13475 const int prec = fmt->p;
13476 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13477 int inexact;
13478 mpfr_t m;
13480 mpfr_init2 (m, prec);
13481 mpfr_from_real (m, ra, GMP_RNDN);
13482 mpfr_clear_flags ();
13483 inexact = func (m, n, m, rnd);
13484 result = do_mpfr_ckconv (m, type, inexact);
13485 mpfr_clear (m);
13489 return result;
13492 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13493 the pointer *(ARG_QUO) and return the result. The type is taken
13494 from the type of ARG0 and is used for setting the precision of the
13495 calculation and results. */
13497 static tree
13498 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13500 tree const type = TREE_TYPE (arg0);
13501 tree result = NULL_TREE;
13503 STRIP_NOPS (arg0);
13504 STRIP_NOPS (arg1);
13506 /* To proceed, MPFR must exactly represent the target floating point
13507 format, which only happens when the target base equals two. */
13508 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13509 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13510 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13512 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13513 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13515 if (real_isfinite (ra0) && real_isfinite (ra1))
13517 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13518 const int prec = fmt->p;
13519 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13520 tree result_rem;
13521 long integer_quo;
13522 mpfr_t m0, m1;
13524 mpfr_inits2 (prec, m0, m1, NULL);
13525 mpfr_from_real (m0, ra0, GMP_RNDN);
13526 mpfr_from_real (m1, ra1, GMP_RNDN);
13527 mpfr_clear_flags ();
13528 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13529 /* Remquo is independent of the rounding mode, so pass
13530 inexact=0 to do_mpfr_ckconv(). */
13531 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13532 mpfr_clears (m0, m1, NULL);
13533 if (result_rem)
13535 /* MPFR calculates quo in the host's long so it may
13536 return more bits in quo than the target int can hold
13537 if sizeof(host long) > sizeof(target int). This can
13538 happen even for native compilers in LP64 mode. In
13539 these cases, modulo the quo value with the largest
13540 number that the target int can hold while leaving one
13541 bit for the sign. */
13542 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13543 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13545 /* Dereference the quo pointer argument. */
13546 arg_quo = build_fold_indirect_ref (arg_quo);
13547 /* Proceed iff a valid pointer type was passed in. */
13548 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13550 /* Set the value. */
13551 tree result_quo
13552 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13553 build_int_cst (TREE_TYPE (arg_quo),
13554 integer_quo));
13555 TREE_SIDE_EFFECTS (result_quo) = 1;
13556 /* Combine the quo assignment with the rem. */
13557 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13558 result_quo, result_rem));
13563 return result;
13566 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13567 resulting value as a tree with type TYPE. The mpfr precision is
13568 set to the precision of TYPE. We assume that this mpfr function
13569 returns zero if the result could be calculated exactly within the
13570 requested precision. In addition, the integer pointer represented
13571 by ARG_SG will be dereferenced and set to the appropriate signgam
13572 (-1,1) value. */
13574 static tree
13575 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13577 tree result = NULL_TREE;
13579 STRIP_NOPS (arg);
13581 /* To proceed, MPFR must exactly represent the target floating point
13582 format, which only happens when the target base equals two. Also
13583 verify ARG is a constant and that ARG_SG is an int pointer. */
13584 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13585 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13586 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13587 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13589 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13591 /* In addition to NaN and Inf, the argument cannot be zero or a
13592 negative integer. */
13593 if (real_isfinite (ra)
13594 && ra->cl != rvc_zero
13595 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13597 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13598 const int prec = fmt->p;
13599 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13600 int inexact, sg;
13601 mpfr_t m;
13602 tree result_lg;
13604 mpfr_init2 (m, prec);
13605 mpfr_from_real (m, ra, GMP_RNDN);
13606 mpfr_clear_flags ();
13607 inexact = mpfr_lgamma (m, &sg, m, rnd);
13608 result_lg = do_mpfr_ckconv (m, type, inexact);
13609 mpfr_clear (m);
13610 if (result_lg)
13612 tree result_sg;
13614 /* Dereference the arg_sg pointer argument. */
13615 arg_sg = build_fold_indirect_ref (arg_sg);
13616 /* Assign the signgam value into *arg_sg. */
13617 result_sg = fold_build2 (MODIFY_EXPR,
13618 TREE_TYPE (arg_sg), arg_sg,
13619 build_int_cst (TREE_TYPE (arg_sg), sg));
13620 TREE_SIDE_EFFECTS (result_sg) = 1;
13621 /* Combine the signgam assignment with the lgamma result. */
13622 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13623 result_sg, result_lg));
13628 return result;
13631 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13632 function FUNC on it and return the resulting value as a tree with
13633 type TYPE. The mpfr precision is set to the precision of TYPE. We
13634 assume that function FUNC returns zero if the result could be
13635 calculated exactly within the requested precision. */
13637 static tree
13638 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13640 tree result = NULL_TREE;
13642 STRIP_NOPS (arg);
13644 /* To proceed, MPFR must exactly represent the target floating point
13645 format, which only happens when the target base equals two. */
13646 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13647 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13648 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13650 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13651 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13653 if (real_isfinite (re) && real_isfinite (im))
13655 const struct real_format *const fmt =
13656 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13657 const int prec = fmt->p;
13658 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13659 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13660 int inexact;
13661 mpc_t m;
13663 mpc_init2 (m, prec);
13664 mpfr_from_real (mpc_realref(m), re, rnd);
13665 mpfr_from_real (mpc_imagref(m), im, rnd);
13666 mpfr_clear_flags ();
13667 inexact = func (m, m, crnd);
13668 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13669 mpc_clear (m);
13673 return result;
13676 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13677 mpc function FUNC on it and return the resulting value as a tree
13678 with type TYPE. The mpfr precision is set to the precision of
13679 TYPE. We assume that function FUNC returns zero if the result
13680 could be calculated exactly within the requested precision. If
13681 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13682 in the arguments and/or results. */
13684 tree
13685 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13686 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13688 tree result = NULL_TREE;
13690 STRIP_NOPS (arg0);
13691 STRIP_NOPS (arg1);
13693 /* To proceed, MPFR must exactly represent the target floating point
13694 format, which only happens when the target base equals two. */
13695 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13696 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13697 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13698 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13699 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13701 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13702 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13703 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13704 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13706 if (do_nonfinite
13707 || (real_isfinite (re0) && real_isfinite (im0)
13708 && real_isfinite (re1) && real_isfinite (im1)))
13710 const struct real_format *const fmt =
13711 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13712 const int prec = fmt->p;
13713 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13714 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13715 int inexact;
13716 mpc_t m0, m1;
13718 mpc_init2 (m0, prec);
13719 mpc_init2 (m1, prec);
13720 mpfr_from_real (mpc_realref(m0), re0, rnd);
13721 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13722 mpfr_from_real (mpc_realref(m1), re1, rnd);
13723 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13724 mpfr_clear_flags ();
13725 inexact = func (m0, m0, m1, crnd);
13726 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13727 mpc_clear (m0);
13728 mpc_clear (m1);
13732 return result;
13735 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13736 a normal call should be emitted rather than expanding the function
13737 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13739 static tree
13740 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13742 int nargs = gimple_call_num_args (stmt);
13744 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13745 (nargs > 0
13746 ? gimple_call_arg_ptr (stmt, 0)
13747 : &error_mark_node), fcode);
13750 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13751 a normal call should be emitted rather than expanding the function
13752 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13753 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13754 passed as second argument. */
13756 tree
13757 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13758 enum built_in_function fcode)
13760 int nargs = gimple_call_num_args (stmt);
13762 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13763 (nargs > 0
13764 ? gimple_call_arg_ptr (stmt, 0)
13765 : &error_mark_node), maxlen, fcode);
13768 /* Builtins with folding operations that operate on "..." arguments
13769 need special handling; we need to store the arguments in a convenient
13770 data structure before attempting any folding. Fortunately there are
13771 only a few builtins that fall into this category. FNDECL is the
13772 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13773 result of the function call is ignored. */
13775 static tree
13776 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13777 bool ignore ATTRIBUTE_UNUSED)
13779 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13780 tree ret = NULL_TREE;
13782 switch (fcode)
13784 case BUILT_IN_SPRINTF_CHK:
13785 case BUILT_IN_VSPRINTF_CHK:
13786 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13787 break;
13789 case BUILT_IN_SNPRINTF_CHK:
13790 case BUILT_IN_VSNPRINTF_CHK:
13791 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13793 default:
13794 break;
13796 if (ret)
13798 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13799 TREE_NO_WARNING (ret) = 1;
13800 return ret;
13802 return NULL_TREE;
13805 /* A wrapper function for builtin folding that prevents warnings for
13806 "statement without effect" and the like, caused by removing the
13807 call node earlier than the warning is generated. */
13809 tree
13810 fold_call_stmt (gimple stmt, bool ignore)
13812 tree ret = NULL_TREE;
13813 tree fndecl = gimple_call_fndecl (stmt);
13814 location_t loc = gimple_location (stmt);
13815 if (fndecl
13816 && TREE_CODE (fndecl) == FUNCTION_DECL
13817 && DECL_BUILT_IN (fndecl)
13818 && !gimple_call_va_arg_pack_p (stmt))
13820 int nargs = gimple_call_num_args (stmt);
13821 tree *args = (nargs > 0
13822 ? gimple_call_arg_ptr (stmt, 0)
13823 : &error_mark_node);
13825 if (avoid_folding_inline_builtin (fndecl))
13826 return NULL_TREE;
13827 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13829 return targetm.fold_builtin (fndecl, nargs, args, ignore);
13831 else
13833 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13834 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13835 if (!ret)
13836 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13837 if (ret)
13839 /* Propagate location information from original call to
13840 expansion of builtin. Otherwise things like
13841 maybe_emit_chk_warning, that operate on the expansion
13842 of a builtin, will use the wrong location information. */
13843 if (gimple_has_location (stmt))
13845 tree realret = ret;
13846 if (TREE_CODE (ret) == NOP_EXPR)
13847 realret = TREE_OPERAND (ret, 0);
13848 if (CAN_HAVE_LOCATION_P (realret)
13849 && !EXPR_HAS_LOCATION (realret))
13850 SET_EXPR_LOCATION (realret, loc);
13851 return realret;
13853 return ret;
13857 return NULL_TREE;
13860 /* Look up the function in built_in_decls that corresponds to DECL
13861 and set ASMSPEC as its user assembler name. DECL must be a
13862 function decl that declares a builtin. */
13864 void
13865 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13867 tree builtin;
13868 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13869 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13870 && asmspec != 0);
13872 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13873 set_user_assembler_name (builtin, asmspec);
13874 switch (DECL_FUNCTION_CODE (decl))
13876 case BUILT_IN_MEMCPY:
13877 init_block_move_fn (asmspec);
13878 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13879 break;
13880 case BUILT_IN_MEMSET:
13881 init_block_clear_fn (asmspec);
13882 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13883 break;
13884 case BUILT_IN_MEMMOVE:
13885 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13886 break;
13887 case BUILT_IN_MEMCMP:
13888 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13889 break;
13890 case BUILT_IN_ABORT:
13891 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13892 break;
13893 case BUILT_IN_FFS:
13894 if (INT_TYPE_SIZE < BITS_PER_WORD)
13896 set_user_assembler_libfunc ("ffs", asmspec);
13897 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13898 MODE_INT, 0), "ffs");
13900 break;
13901 default:
13902 break;
13906 /* Return true if DECL is a builtin that expands to a constant or similarly
13907 simple code. */
13908 bool
13909 is_simple_builtin (tree decl)
13911 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13912 switch (DECL_FUNCTION_CODE (decl))
13914 /* Builtins that expand to constants. */
13915 case BUILT_IN_CONSTANT_P:
13916 case BUILT_IN_EXPECT:
13917 case BUILT_IN_OBJECT_SIZE:
13918 case BUILT_IN_UNREACHABLE:
13919 /* Simple register moves or loads from stack. */
13920 case BUILT_IN_RETURN_ADDRESS:
13921 case BUILT_IN_EXTRACT_RETURN_ADDR:
13922 case BUILT_IN_FROB_RETURN_ADDR:
13923 case BUILT_IN_RETURN:
13924 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13925 case BUILT_IN_FRAME_ADDRESS:
13926 case BUILT_IN_VA_END:
13927 case BUILT_IN_STACK_SAVE:
13928 case BUILT_IN_STACK_RESTORE:
13929 /* Exception state returns or moves registers around. */
13930 case BUILT_IN_EH_FILTER:
13931 case BUILT_IN_EH_POINTER:
13932 case BUILT_IN_EH_COPY_VALUES:
13933 return true;
13935 default:
13936 return false;
13939 return false;
13942 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13943 most probably expanded inline into reasonably simple code. This is a
13944 superset of is_simple_builtin. */
13945 bool
13946 is_inexpensive_builtin (tree decl)
13948 if (!decl)
13949 return false;
13950 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13951 return true;
13952 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13953 switch (DECL_FUNCTION_CODE (decl))
13955 case BUILT_IN_ABS:
13956 case BUILT_IN_ALLOCA:
13957 case BUILT_IN_BSWAP32:
13958 case BUILT_IN_BSWAP64:
13959 case BUILT_IN_CLZ:
13960 case BUILT_IN_CLZIMAX:
13961 case BUILT_IN_CLZL:
13962 case BUILT_IN_CLZLL:
13963 case BUILT_IN_CTZ:
13964 case BUILT_IN_CTZIMAX:
13965 case BUILT_IN_CTZL:
13966 case BUILT_IN_CTZLL:
13967 case BUILT_IN_FFS:
13968 case BUILT_IN_FFSIMAX:
13969 case BUILT_IN_FFSL:
13970 case BUILT_IN_FFSLL:
13971 case BUILT_IN_IMAXABS:
13972 case BUILT_IN_FINITE:
13973 case BUILT_IN_FINITEF:
13974 case BUILT_IN_FINITEL:
13975 case BUILT_IN_FINITED32:
13976 case BUILT_IN_FINITED64:
13977 case BUILT_IN_FINITED128:
13978 case BUILT_IN_FPCLASSIFY:
13979 case BUILT_IN_ISFINITE:
13980 case BUILT_IN_ISINF_SIGN:
13981 case BUILT_IN_ISINF:
13982 case BUILT_IN_ISINFF:
13983 case BUILT_IN_ISINFL:
13984 case BUILT_IN_ISINFD32:
13985 case BUILT_IN_ISINFD64:
13986 case BUILT_IN_ISINFD128:
13987 case BUILT_IN_ISNAN:
13988 case BUILT_IN_ISNANF:
13989 case BUILT_IN_ISNANL:
13990 case BUILT_IN_ISNAND32:
13991 case BUILT_IN_ISNAND64:
13992 case BUILT_IN_ISNAND128:
13993 case BUILT_IN_ISNORMAL:
13994 case BUILT_IN_ISGREATER:
13995 case BUILT_IN_ISGREATEREQUAL:
13996 case BUILT_IN_ISLESS:
13997 case BUILT_IN_ISLESSEQUAL:
13998 case BUILT_IN_ISLESSGREATER:
13999 case BUILT_IN_ISUNORDERED:
14000 case BUILT_IN_VA_ARG_PACK:
14001 case BUILT_IN_VA_ARG_PACK_LEN:
14002 case BUILT_IN_VA_COPY:
14003 case BUILT_IN_TRAP:
14004 case BUILT_IN_SAVEREGS:
14005 case BUILT_IN_POPCOUNTL:
14006 case BUILT_IN_POPCOUNTLL:
14007 case BUILT_IN_POPCOUNTIMAX:
14008 case BUILT_IN_POPCOUNT:
14009 case BUILT_IN_PARITYL:
14010 case BUILT_IN_PARITYLL:
14011 case BUILT_IN_PARITYIMAX:
14012 case BUILT_IN_PARITY:
14013 case BUILT_IN_LABS:
14014 case BUILT_IN_LLABS:
14015 case BUILT_IN_PREFETCH:
14016 return true;
14018 default:
14019 return is_simple_builtin (decl);
14022 return false;