PR c++/37766
[official-gcc/constexpr.git] / gcc / builtins.c
blob55228d22c40ffa24296df651b92a61d3b3009642
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
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 "real.h"
28 #include "rtl.h"
29 #include "tree.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 "toplev.h"
44 #include "predict.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
50 #include "tree-flow.h"
51 #include "value-prof.h"
52 #include "diagnostic.h"
54 #ifndef SLOW_UNALIGNED_ACCESS
55 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
56 #endif
58 #ifndef PAD_VARARGS_DOWN
59 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
60 #endif
61 #ifdef HAVE_mpc
62 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
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_interclass_mathfn (tree, rtx, rtx);
106 static rtx expand_builtin_sincos (tree);
107 static rtx expand_builtin_cexpi (tree, rtx, rtx);
108 static rtx expand_builtin_int_roundingfn (tree, rtx);
109 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
110 static rtx expand_builtin_args_info (tree);
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, rtx);
133 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
134 static rtx expand_builtin_frame_address (tree, tree);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list_loc (location_t, tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_expect (location_t, tree, tree);
142 static tree fold_builtin_classify_type (tree);
143 static tree fold_builtin_strlen (location_t, tree);
144 static tree fold_builtin_inf (location_t, tree, int);
145 static tree fold_builtin_nan (tree, tree, int);
146 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
147 static bool validate_arg (const_tree, enum tree_code code);
148 static bool integer_valued_real_p (tree);
149 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
150 static bool readonly_data_expr (tree);
151 static rtx expand_builtin_fabs (tree, rtx, rtx);
152 static rtx expand_builtin_signbit (tree, rtx);
153 static tree fold_builtin_sqrt (location_t, tree, tree);
154 static tree fold_builtin_cbrt (location_t, tree, tree);
155 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
156 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_cos (location_t, tree, tree, tree);
158 static tree fold_builtin_cosh (location_t, tree, tree, tree);
159 static tree fold_builtin_tan (tree, tree);
160 static tree fold_builtin_trunc (location_t, tree, tree);
161 static tree fold_builtin_floor (location_t, tree, tree);
162 static tree fold_builtin_ceil (location_t, tree, tree);
163 static tree fold_builtin_round (location_t, tree, tree);
164 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
165 static tree fold_builtin_bitop (tree, tree);
166 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
167 static tree fold_builtin_strchr (location_t, tree, tree, tree);
168 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
169 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
170 static tree fold_builtin_strcmp (location_t, tree, tree);
171 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
172 static tree fold_builtin_signbit (location_t, tree, tree);
173 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
174 static tree fold_builtin_isascii (location_t, tree);
175 static tree fold_builtin_toascii (location_t, tree);
176 static tree fold_builtin_isdigit (location_t, tree);
177 static tree fold_builtin_fabs (location_t, tree, tree);
178 static tree fold_builtin_abs (location_t, tree, tree);
179 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
180 enum tree_code);
181 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
182 static tree fold_builtin_0 (location_t, tree, bool);
183 static tree fold_builtin_1 (location_t, tree, tree, bool);
184 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
185 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
186 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
187 static tree fold_builtin_varargs (location_t, tree, tree, bool);
189 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
190 static tree fold_builtin_strstr (location_t, tree, tree, tree);
191 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
192 static tree fold_builtin_strcat (location_t, tree, tree);
193 static tree fold_builtin_strncat (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
196 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198 static rtx expand_builtin_object_size (tree);
199 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
200 enum built_in_function);
201 static void maybe_emit_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_free_warning (tree);
204 static tree fold_builtin_object_size (tree, tree);
205 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
206 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
207 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
208 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
209 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
210 enum built_in_function);
211 static bool init_target_chars (void);
213 static unsigned HOST_WIDE_INT target_newline;
214 static unsigned HOST_WIDE_INT target_percent;
215 static unsigned HOST_WIDE_INT target_c;
216 static unsigned HOST_WIDE_INT target_s;
217 static char target_percent_c[3];
218 static char target_percent_s[3];
219 static char target_percent_s_newline[4];
220 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
221 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
222 static tree do_mpfr_arg2 (tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_arg3 (tree, tree, tree, tree,
225 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
226 static tree do_mpfr_sincos (tree, tree, tree);
227 static tree do_mpfr_bessel_n (tree, tree, tree,
228 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
229 const REAL_VALUE_TYPE *, bool);
230 static tree do_mpfr_remquo (tree, tree, tree);
231 static tree do_mpfr_lgamma_r (tree, tree, tree);
233 /* Return true if NAME starts with __builtin_ or __sync_. */
235 bool
236 is_builtin_name (const char *name)
238 if (strncmp (name, "__builtin_", 10) == 0)
239 return true;
240 if (strncmp (name, "__sync_", 7) == 0)
241 return true;
242 return false;
246 /* Return true if DECL is a function symbol representing a built-in. */
248 bool
249 is_builtin_fn (tree decl)
251 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
255 /* Return true if NODE should be considered for inline expansion regardless
256 of the optimization level. This means whenever a function is invoked with
257 its "internal" name, which normally contains the prefix "__builtin". */
259 static bool
260 called_as_built_in (tree node)
262 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
263 we want the name used to call the function, not the name it
264 will have. */
265 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
266 return is_builtin_name (name);
269 /* Return the alignment in bits of EXP, an object.
270 Don't return more than MAX_ALIGN no matter what, ALIGN is the inital
271 guessed alignment e.g. from type alignment. */
274 get_object_alignment (tree exp, unsigned int align, unsigned int max_align)
276 unsigned int inner;
278 inner = max_align;
279 if (handled_component_p (exp))
281 HOST_WIDE_INT bitsize, bitpos;
282 tree offset;
283 enum machine_mode mode;
284 int unsignedp, volatilep;
286 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
287 &mode, &unsignedp, &volatilep, true);
288 if (bitpos)
289 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
290 while (offset)
292 tree next_offset;
294 if (TREE_CODE (offset) == PLUS_EXPR)
296 next_offset = TREE_OPERAND (offset, 0);
297 offset = TREE_OPERAND (offset, 1);
299 else
300 next_offset = NULL;
301 if (host_integerp (offset, 1))
303 /* Any overflow in calculating offset_bits won't change
304 the alignment. */
305 unsigned offset_bits
306 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
308 if (offset_bits)
309 inner = MIN (inner, (offset_bits & -offset_bits));
311 else if (TREE_CODE (offset) == MULT_EXPR
312 && host_integerp (TREE_OPERAND (offset, 1), 1))
314 /* Any overflow in calculating offset_factor won't change
315 the alignment. */
316 unsigned offset_factor
317 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
318 * BITS_PER_UNIT);
320 if (offset_factor)
321 inner = MIN (inner, (offset_factor & -offset_factor));
323 else
325 inner = MIN (inner, BITS_PER_UNIT);
326 break;
328 offset = next_offset;
331 if (DECL_P (exp))
332 align = MIN (inner, DECL_ALIGN (exp));
333 #ifdef CONSTANT_ALIGNMENT
334 else if (CONSTANT_CLASS_P (exp))
335 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
336 #endif
337 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
338 || TREE_CODE (exp) == INDIRECT_REF)
339 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
340 else
341 align = MIN (align, inner);
342 return MIN (align, max_align);
345 /* Returns true iff we can trust that alignment information has been
346 calculated properly. */
348 bool
349 can_trust_pointer_alignment (void)
351 /* We rely on TER to compute accurate alignment information. */
352 return (optimize && flag_tree_ter);
355 /* Return the alignment in bits of EXP, a pointer valued expression.
356 But don't return more than MAX_ALIGN no matter what.
357 The alignment returned is, by default, the alignment of the thing that
358 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
360 Otherwise, look at the expression to see if we can do better, i.e., if the
361 expression is actually pointing at an object whose alignment is tighter. */
364 get_pointer_alignment (tree exp, unsigned int max_align)
366 unsigned int align, inner;
368 if (!can_trust_pointer_alignment ())
369 return 0;
371 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
372 return 0;
374 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
375 align = MIN (align, max_align);
377 while (1)
379 switch (TREE_CODE (exp))
381 CASE_CONVERT:
382 exp = TREE_OPERAND (exp, 0);
383 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
384 return align;
386 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
387 align = MIN (inner, max_align);
388 break;
390 case POINTER_PLUS_EXPR:
391 /* If sum of pointer + int, restrict our maximum alignment to that
392 imposed by the integer. If not, we can't do any better than
393 ALIGN. */
394 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
395 return align;
397 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
398 & (max_align / BITS_PER_UNIT - 1))
399 != 0)
400 max_align >>= 1;
402 exp = TREE_OPERAND (exp, 0);
403 break;
405 case ADDR_EXPR:
406 /* See what we are pointing at and look at its alignment. */
407 return get_object_alignment (TREE_OPERAND (exp, 0), align, max_align);
409 default:
410 return align;
415 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
416 way, because it could contain a zero byte in the middle.
417 TREE_STRING_LENGTH is the size of the character array, not the string.
419 ONLY_VALUE should be nonzero if the result is not going to be emitted
420 into the instruction stream and zero if it is going to be expanded.
421 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
422 is returned, otherwise NULL, since
423 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
424 evaluate the side-effects.
426 The value returned is of type `ssizetype'.
428 Unfortunately, string_constant can't access the values of const char
429 arrays with initializers, so neither can we do so here. */
431 tree
432 c_strlen (tree src, int only_value)
434 tree offset_node;
435 HOST_WIDE_INT offset;
436 int max;
437 const char *ptr;
439 STRIP_NOPS (src);
440 if (TREE_CODE (src) == COND_EXPR
441 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
443 tree len1, len2;
445 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
446 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
447 if (tree_int_cst_equal (len1, len2))
448 return len1;
451 if (TREE_CODE (src) == COMPOUND_EXPR
452 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
453 return c_strlen (TREE_OPERAND (src, 1), only_value);
455 src = string_constant (src, &offset_node);
456 if (src == 0)
457 return NULL_TREE;
459 max = TREE_STRING_LENGTH (src) - 1;
460 ptr = TREE_STRING_POINTER (src);
462 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
464 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
465 compute the offset to the following null if we don't know where to
466 start searching for it. */
467 int i;
469 for (i = 0; i < max; i++)
470 if (ptr[i] == 0)
471 return NULL_TREE;
473 /* We don't know the starting offset, but we do know that the string
474 has no internal zero bytes. We can assume that the offset falls
475 within the bounds of the string; otherwise, the programmer deserves
476 what he gets. Subtract the offset from the length of the string,
477 and return that. This would perhaps not be valid if we were dealing
478 with named arrays in addition to literal string constants. */
480 return size_diffop_loc (input_location, size_int (max), offset_node);
483 /* We have a known offset into the string. Start searching there for
484 a null character if we can represent it as a single HOST_WIDE_INT. */
485 if (offset_node == 0)
486 offset = 0;
487 else if (! host_integerp (offset_node, 0))
488 offset = -1;
489 else
490 offset = tree_low_cst (offset_node, 0);
492 /* If the offset is known to be out of bounds, warn, and call strlen at
493 runtime. */
494 if (offset < 0 || offset > max)
496 /* Suppress multiple warnings for propagated constant strings. */
497 if (! TREE_NO_WARNING (src))
499 warning (0, "offset outside bounds of constant string");
500 TREE_NO_WARNING (src) = 1;
502 return NULL_TREE;
505 /* Use strlen to search for the first zero byte. Since any strings
506 constructed with build_string will have nulls appended, we win even
507 if we get handed something like (char[4])"abcd".
509 Since OFFSET is our starting index into the string, no further
510 calculation is needed. */
511 return ssize_int (strlen (ptr + offset));
514 /* Return a char pointer for a C string if it is a string constant
515 or sum of string constant and integer constant. */
517 static const char *
518 c_getstr (tree src)
520 tree offset_node;
522 src = string_constant (src, &offset_node);
523 if (src == 0)
524 return 0;
526 if (offset_node == 0)
527 return TREE_STRING_POINTER (src);
528 else if (!host_integerp (offset_node, 1)
529 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
530 return 0;
532 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
535 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
536 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
538 static rtx
539 c_readstr (const char *str, enum machine_mode mode)
541 HOST_WIDE_INT c[2];
542 HOST_WIDE_INT ch;
543 unsigned int i, j;
545 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
547 c[0] = 0;
548 c[1] = 0;
549 ch = 1;
550 for (i = 0; i < GET_MODE_SIZE (mode); i++)
552 j = i;
553 if (WORDS_BIG_ENDIAN)
554 j = GET_MODE_SIZE (mode) - i - 1;
555 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
556 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
557 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
558 j *= BITS_PER_UNIT;
559 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
561 if (ch)
562 ch = (unsigned char) str[i];
563 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
565 return immed_double_const (c[0], c[1], mode);
568 /* Cast a target constant CST to target CHAR and if that value fits into
569 host char type, return zero and put that value into variable pointed to by
570 P. */
572 static int
573 target_char_cast (tree cst, char *p)
575 unsigned HOST_WIDE_INT val, hostval;
577 if (!host_integerp (cst, 1)
578 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
579 return 1;
581 val = tree_low_cst (cst, 1);
582 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
583 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
585 hostval = val;
586 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
587 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
589 if (val != hostval)
590 return 1;
592 *p = hostval;
593 return 0;
596 /* Similar to save_expr, but assumes that arbitrary code is not executed
597 in between the multiple evaluations. In particular, we assume that a
598 non-addressable local variable will not be modified. */
600 static tree
601 builtin_save_expr (tree exp)
603 if (TREE_ADDRESSABLE (exp) == 0
604 && (TREE_CODE (exp) == PARM_DECL
605 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
606 return exp;
608 return save_expr (exp);
611 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
612 times to get the address of either a higher stack frame, or a return
613 address located within it (depending on FNDECL_CODE). */
615 static rtx
616 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
618 int i;
620 #ifdef INITIAL_FRAME_ADDRESS_RTX
621 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
622 #else
623 rtx tem;
625 /* For a zero count with __builtin_return_address, we don't care what
626 frame address we return, because target-specific definitions will
627 override us. Therefore frame pointer elimination is OK, and using
628 the soft frame pointer is OK.
630 For a nonzero count, or a zero count with __builtin_frame_address,
631 we require a stable offset from the current frame pointer to the
632 previous one, so we must use the hard frame pointer, and
633 we must disable frame pointer elimination. */
634 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
635 tem = frame_pointer_rtx;
636 else
638 tem = hard_frame_pointer_rtx;
640 /* Tell reload not to eliminate the frame pointer. */
641 crtl->accesses_prior_frames = 1;
643 #endif
645 /* Some machines need special handling before we can access
646 arbitrary frames. For example, on the SPARC, we must first flush
647 all register windows to the stack. */
648 #ifdef SETUP_FRAME_ADDRESSES
649 if (count > 0)
650 SETUP_FRAME_ADDRESSES ();
651 #endif
653 /* On the SPARC, the return address is not in the frame, it is in a
654 register. There is no way to access it off of the current frame
655 pointer, but it can be accessed off the previous frame pointer by
656 reading the value from the register window save area. */
657 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
658 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
659 count--;
660 #endif
662 /* Scan back COUNT frames to the specified frame. */
663 for (i = 0; i < count; i++)
665 /* Assume the dynamic chain pointer is in the word that the
666 frame address points to, unless otherwise specified. */
667 #ifdef DYNAMIC_CHAIN_ADDRESS
668 tem = DYNAMIC_CHAIN_ADDRESS (tem);
669 #endif
670 tem = memory_address (Pmode, tem);
671 tem = gen_frame_mem (Pmode, tem);
672 tem = copy_to_reg (tem);
675 /* For __builtin_frame_address, return what we've got. But, on
676 the SPARC for example, we may have to add a bias. */
677 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
678 #ifdef FRAME_ADDR_RTX
679 return FRAME_ADDR_RTX (tem);
680 #else
681 return tem;
682 #endif
684 /* For __builtin_return_address, get the return address from that frame. */
685 #ifdef RETURN_ADDR_RTX
686 tem = RETURN_ADDR_RTX (count, tem);
687 #else
688 tem = memory_address (Pmode,
689 plus_constant (tem, GET_MODE_SIZE (Pmode)));
690 tem = gen_frame_mem (Pmode, tem);
691 #endif
692 return tem;
695 /* Alias set used for setjmp buffer. */
696 static alias_set_type setjmp_alias_set = -1;
698 /* Construct the leading half of a __builtin_setjmp call. Control will
699 return to RECEIVER_LABEL. This is also called directly by the SJLJ
700 exception handling code. */
702 void
703 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
705 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
706 rtx stack_save;
707 rtx mem;
709 if (setjmp_alias_set == -1)
710 setjmp_alias_set = new_alias_set ();
712 buf_addr = convert_memory_address (Pmode, buf_addr);
714 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
716 /* We store the frame pointer and the address of receiver_label in
717 the buffer and use the rest of it for the stack save area, which
718 is machine-dependent. */
720 mem = gen_rtx_MEM (Pmode, buf_addr);
721 set_mem_alias_set (mem, setjmp_alias_set);
722 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
724 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
725 set_mem_alias_set (mem, setjmp_alias_set);
727 emit_move_insn (validize_mem (mem),
728 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
730 stack_save = gen_rtx_MEM (sa_mode,
731 plus_constant (buf_addr,
732 2 * GET_MODE_SIZE (Pmode)));
733 set_mem_alias_set (stack_save, setjmp_alias_set);
734 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
736 /* If there is further processing to do, do it. */
737 #ifdef HAVE_builtin_setjmp_setup
738 if (HAVE_builtin_setjmp_setup)
739 emit_insn (gen_builtin_setjmp_setup (buf_addr));
740 #endif
742 /* Tell optimize_save_area_alloca that extra work is going to
743 need to go on during alloca. */
744 cfun->calls_setjmp = 1;
746 /* We have a nonlocal label. */
747 cfun->has_nonlocal_label = 1;
750 /* Construct the trailing part of a __builtin_setjmp call. This is
751 also called directly by the SJLJ exception handling code. */
753 void
754 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
756 rtx chain;
758 /* Clobber the FP when we get here, so we have to make sure it's
759 marked as used by this function. */
760 emit_use (hard_frame_pointer_rtx);
762 /* Mark the static chain as clobbered here so life information
763 doesn't get messed up for it. */
764 chain = targetm.calls.static_chain (current_function_decl, true);
765 if (chain && REG_P (chain))
766 emit_clobber (chain);
768 /* Now put in the code to restore the frame pointer, and argument
769 pointer, if needed. */
770 #ifdef HAVE_nonlocal_goto
771 if (! HAVE_nonlocal_goto)
772 #endif
774 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
775 /* This might change the hard frame pointer in ways that aren't
776 apparent to early optimization passes, so force a clobber. */
777 emit_clobber (hard_frame_pointer_rtx);
780 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
781 if (fixed_regs[ARG_POINTER_REGNUM])
783 #ifdef ELIMINABLE_REGS
784 size_t i;
785 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
787 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
788 if (elim_regs[i].from == ARG_POINTER_REGNUM
789 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
790 break;
792 if (i == ARRAY_SIZE (elim_regs))
793 #endif
795 /* Now restore our arg pointer from the address at which it
796 was saved in our stack frame. */
797 emit_move_insn (crtl->args.internal_arg_pointer,
798 copy_to_reg (get_arg_pointer_save_area ()));
801 #endif
803 #ifdef HAVE_builtin_setjmp_receiver
804 if (HAVE_builtin_setjmp_receiver)
805 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
806 else
807 #endif
808 #ifdef HAVE_nonlocal_goto_receiver
809 if (HAVE_nonlocal_goto_receiver)
810 emit_insn (gen_nonlocal_goto_receiver ());
811 else
812 #endif
813 { /* Nothing */ }
815 /* We must not allow the code we just generated to be reordered by
816 scheduling. Specifically, the update of the frame pointer must
817 happen immediately, not later. */
818 emit_insn (gen_blockage ());
821 /* __builtin_longjmp is passed a pointer to an array of five words (not
822 all will be used on all machines). It operates similarly to the C
823 library function of the same name, but is more efficient. Much of
824 the code below is copied from the handling of non-local gotos. */
826 static void
827 expand_builtin_longjmp (rtx buf_addr, rtx value)
829 rtx fp, lab, stack, insn, last;
830 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
832 /* DRAP is needed for stack realign if longjmp is expanded to current
833 function */
834 if (SUPPORTS_STACK_ALIGNMENT)
835 crtl->need_drap = true;
837 if (setjmp_alias_set == -1)
838 setjmp_alias_set = new_alias_set ();
840 buf_addr = convert_memory_address (Pmode, buf_addr);
842 buf_addr = force_reg (Pmode, buf_addr);
844 /* We require that the user must pass a second argument of 1, because
845 that is what builtin_setjmp will return. */
846 gcc_assert (value == const1_rtx);
848 last = get_last_insn ();
849 #ifdef HAVE_builtin_longjmp
850 if (HAVE_builtin_longjmp)
851 emit_insn (gen_builtin_longjmp (buf_addr));
852 else
853 #endif
855 fp = gen_rtx_MEM (Pmode, buf_addr);
856 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
857 GET_MODE_SIZE (Pmode)));
859 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
860 2 * GET_MODE_SIZE (Pmode)));
861 set_mem_alias_set (fp, setjmp_alias_set);
862 set_mem_alias_set (lab, setjmp_alias_set);
863 set_mem_alias_set (stack, setjmp_alias_set);
865 /* Pick up FP, label, and SP from the block and jump. This code is
866 from expand_goto in stmt.c; see there for detailed comments. */
867 #ifdef HAVE_nonlocal_goto
868 if (HAVE_nonlocal_goto)
869 /* We have to pass a value to the nonlocal_goto pattern that will
870 get copied into the static_chain pointer, but it does not matter
871 what that value is, because builtin_setjmp does not use it. */
872 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
873 else
874 #endif
876 lab = copy_to_reg (lab);
878 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
879 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
881 emit_move_insn (hard_frame_pointer_rtx, fp);
882 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
884 emit_use (hard_frame_pointer_rtx);
885 emit_use (stack_pointer_rtx);
886 emit_indirect_jump (lab);
890 /* Search backwards and mark the jump insn as a non-local goto.
891 Note that this precludes the use of __builtin_longjmp to a
892 __builtin_setjmp target in the same function. However, we've
893 already cautioned the user that these functions are for
894 internal exception handling use only. */
895 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
897 gcc_assert (insn != last);
899 if (JUMP_P (insn))
901 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
902 break;
904 else if (CALL_P (insn))
905 break;
909 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
910 and the address of the save area. */
912 static rtx
913 expand_builtin_nonlocal_goto (tree exp)
915 tree t_label, t_save_area;
916 rtx r_label, r_save_area, r_fp, r_sp, insn;
918 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
919 return NULL_RTX;
921 t_label = CALL_EXPR_ARG (exp, 0);
922 t_save_area = CALL_EXPR_ARG (exp, 1);
924 r_label = expand_normal (t_label);
925 r_label = convert_memory_address (Pmode, r_label);
926 r_save_area = expand_normal (t_save_area);
927 r_save_area = convert_memory_address (Pmode, r_save_area);
928 /* Copy the address of the save location to a register just in case it was based
929 on the frame pointer. */
930 r_save_area = copy_to_reg (r_save_area);
931 r_fp = gen_rtx_MEM (Pmode, r_save_area);
932 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
933 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
935 crtl->has_nonlocal_goto = 1;
937 #ifdef HAVE_nonlocal_goto
938 /* ??? We no longer need to pass the static chain value, afaik. */
939 if (HAVE_nonlocal_goto)
940 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
941 else
942 #endif
944 r_label = copy_to_reg (r_label);
946 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
947 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
949 /* Restore frame pointer for containing function.
950 This sets the actual hard register used for the frame pointer
951 to the location of the function's incoming static chain info.
952 The non-local goto handler will then adjust it to contain the
953 proper value and reload the argument pointer, if needed. */
954 emit_move_insn (hard_frame_pointer_rtx, r_fp);
955 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
957 /* USE of hard_frame_pointer_rtx added for consistency;
958 not clear if really needed. */
959 emit_use (hard_frame_pointer_rtx);
960 emit_use (stack_pointer_rtx);
962 /* If the architecture is using a GP register, we must
963 conservatively assume that the target function makes use of it.
964 The prologue of functions with nonlocal gotos must therefore
965 initialize the GP register to the appropriate value, and we
966 must then make sure that this value is live at the point
967 of the jump. (Note that this doesn't necessarily apply
968 to targets with a nonlocal_goto pattern; they are free
969 to implement it in their own way. Note also that this is
970 a no-op if the GP register is a global invariant.) */
971 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
972 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
973 emit_use (pic_offset_table_rtx);
975 emit_indirect_jump (r_label);
978 /* Search backwards to the jump insn and mark it as a
979 non-local goto. */
980 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
982 if (JUMP_P (insn))
984 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
985 break;
987 else if (CALL_P (insn))
988 break;
991 return const0_rtx;
994 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
995 (not all will be used on all machines) that was passed to __builtin_setjmp.
996 It updates the stack pointer in that block to correspond to the current
997 stack pointer. */
999 static void
1000 expand_builtin_update_setjmp_buf (rtx buf_addr)
1002 enum machine_mode sa_mode = Pmode;
1003 rtx stack_save;
1006 #ifdef HAVE_save_stack_nonlocal
1007 if (HAVE_save_stack_nonlocal)
1008 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1009 #endif
1010 #ifdef STACK_SAVEAREA_MODE
1011 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1012 #endif
1014 stack_save
1015 = gen_rtx_MEM (sa_mode,
1016 memory_address
1017 (sa_mode,
1018 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1020 #ifdef HAVE_setjmp
1021 if (HAVE_setjmp)
1022 emit_insn (gen_setjmp ());
1023 #endif
1025 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1028 /* Expand a call to __builtin_prefetch. For a target that does not support
1029 data prefetch, evaluate the memory address argument in case it has side
1030 effects. */
1032 static void
1033 expand_builtin_prefetch (tree exp)
1035 tree arg0, arg1, arg2;
1036 int nargs;
1037 rtx op0, op1, op2;
1039 if (!validate_arglist (exp, POINTER_TYPE, 0))
1040 return;
1042 arg0 = CALL_EXPR_ARG (exp, 0);
1044 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1045 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1046 locality). */
1047 nargs = call_expr_nargs (exp);
1048 if (nargs > 1)
1049 arg1 = CALL_EXPR_ARG (exp, 1);
1050 else
1051 arg1 = integer_zero_node;
1052 if (nargs > 2)
1053 arg2 = CALL_EXPR_ARG (exp, 2);
1054 else
1055 arg2 = build_int_cst (NULL_TREE, 3);
1057 /* Argument 0 is an address. */
1058 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1060 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1061 if (TREE_CODE (arg1) != INTEGER_CST)
1063 error ("second argument to %<__builtin_prefetch%> must be a constant");
1064 arg1 = integer_zero_node;
1066 op1 = expand_normal (arg1);
1067 /* Argument 1 must be either zero or one. */
1068 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1070 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1071 " using zero");
1072 op1 = const0_rtx;
1075 /* Argument 2 (locality) must be a compile-time constant int. */
1076 if (TREE_CODE (arg2) != INTEGER_CST)
1078 error ("third argument to %<__builtin_prefetch%> must be a constant");
1079 arg2 = integer_zero_node;
1081 op2 = expand_normal (arg2);
1082 /* Argument 2 must be 0, 1, 2, or 3. */
1083 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1085 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1086 op2 = const0_rtx;
1089 #ifdef HAVE_prefetch
1090 if (HAVE_prefetch)
1092 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1093 (op0,
1094 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1095 || (GET_MODE (op0) != Pmode))
1097 op0 = convert_memory_address (Pmode, op0);
1098 op0 = force_reg (Pmode, op0);
1100 emit_insn (gen_prefetch (op0, op1, op2));
1102 #endif
1104 /* Don't do anything with direct references to volatile memory, but
1105 generate code to handle other side effects. */
1106 if (!MEM_P (op0) && side_effects_p (op0))
1107 emit_insn (op0);
1110 /* Get a MEM rtx for expression EXP which is the address of an operand
1111 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1112 the maximum length of the block of memory that might be accessed or
1113 NULL if unknown. */
1115 static rtx
1116 get_memory_rtx (tree exp, tree len)
1118 tree orig_exp = exp;
1119 rtx addr, mem;
1120 HOST_WIDE_INT off;
1122 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1123 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1124 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1125 exp = TREE_OPERAND (exp, 0);
1127 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1128 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1130 /* Get an expression we can use to find the attributes to assign to MEM.
1131 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1132 we can. First remove any nops. */
1133 while (CONVERT_EXPR_P (exp)
1134 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1135 exp = TREE_OPERAND (exp, 0);
1137 off = 0;
1138 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1139 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1140 && host_integerp (TREE_OPERAND (exp, 1), 0)
1141 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1142 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1143 else if (TREE_CODE (exp) == ADDR_EXPR)
1144 exp = TREE_OPERAND (exp, 0);
1145 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1146 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1147 else
1148 exp = NULL;
1150 /* Honor attributes derived from exp, except for the alias set
1151 (as builtin stringops may alias with anything) and the size
1152 (as stringops may access multiple array elements). */
1153 if (exp)
1155 set_mem_attributes (mem, exp, 0);
1157 if (off)
1158 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1160 /* Allow the string and memory builtins to overflow from one
1161 field into another, see http://gcc.gnu.org/PR23561.
1162 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1163 memory accessed by the string or memory builtin will fit
1164 within the field. */
1165 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1167 tree mem_expr = MEM_EXPR (mem);
1168 HOST_WIDE_INT offset = -1, length = -1;
1169 tree inner = exp;
1171 while (TREE_CODE (inner) == ARRAY_REF
1172 || CONVERT_EXPR_P (inner)
1173 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1174 || TREE_CODE (inner) == SAVE_EXPR)
1175 inner = TREE_OPERAND (inner, 0);
1177 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1179 if (MEM_OFFSET (mem)
1180 && CONST_INT_P (MEM_OFFSET (mem)))
1181 offset = INTVAL (MEM_OFFSET (mem));
1183 if (offset >= 0 && len && host_integerp (len, 0))
1184 length = tree_low_cst (len, 0);
1186 while (TREE_CODE (inner) == COMPONENT_REF)
1188 tree field = TREE_OPERAND (inner, 1);
1189 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1190 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1192 /* Bitfields are generally not byte-addressable. */
1193 gcc_assert (!DECL_BIT_FIELD (field)
1194 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1195 % BITS_PER_UNIT) == 0
1196 && host_integerp (DECL_SIZE (field), 0)
1197 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1198 % BITS_PER_UNIT) == 0));
1200 /* If we can prove that the memory starting at XEXP (mem, 0) and
1201 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1202 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1203 fields without DECL_SIZE_UNIT like flexible array members. */
1204 if (length >= 0
1205 && DECL_SIZE_UNIT (field)
1206 && host_integerp (DECL_SIZE_UNIT (field), 0))
1208 HOST_WIDE_INT size
1209 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1210 if (offset <= size
1211 && length <= size
1212 && offset + length <= size)
1213 break;
1216 if (offset >= 0
1217 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1218 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1219 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1220 / BITS_PER_UNIT;
1221 else
1223 offset = -1;
1224 length = -1;
1227 mem_expr = TREE_OPERAND (mem_expr, 0);
1228 inner = TREE_OPERAND (inner, 0);
1231 if (mem_expr == NULL)
1232 offset = -1;
1233 if (mem_expr != MEM_EXPR (mem))
1235 set_mem_expr (mem, mem_expr);
1236 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1239 set_mem_alias_set (mem, 0);
1240 set_mem_size (mem, NULL_RTX);
1243 return mem;
1246 /* Built-in functions to perform an untyped call and return. */
1248 /* For each register that may be used for calling a function, this
1249 gives a mode used to copy the register's value. VOIDmode indicates
1250 the register is not used for calling a function. If the machine
1251 has register windows, this gives only the outbound registers.
1252 INCOMING_REGNO gives the corresponding inbound register. */
1253 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1255 /* For each register that may be used for returning values, this gives
1256 a mode used to copy the register's value. VOIDmode indicates the
1257 register is not used for returning values. If the machine has
1258 register windows, this gives only the outbound registers.
1259 INCOMING_REGNO gives the corresponding inbound register. */
1260 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1262 /* For each register that may be used for calling a function, this
1263 gives the offset of that register into the block returned by
1264 __builtin_apply_args. 0 indicates that the register is not
1265 used for calling a function. */
1266 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1268 /* Return the size required for the block returned by __builtin_apply_args,
1269 and initialize apply_args_mode. */
1271 static int
1272 apply_args_size (void)
1274 static int size = -1;
1275 int align;
1276 unsigned int regno;
1277 enum machine_mode mode;
1279 /* The values computed by this function never change. */
1280 if (size < 0)
1282 /* The first value is the incoming arg-pointer. */
1283 size = GET_MODE_SIZE (Pmode);
1285 /* The second value is the structure value address unless this is
1286 passed as an "invisible" first argument. */
1287 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1288 size += GET_MODE_SIZE (Pmode);
1290 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1291 if (FUNCTION_ARG_REGNO_P (regno))
1293 mode = reg_raw_mode[regno];
1295 gcc_assert (mode != VOIDmode);
1297 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1298 if (size % align != 0)
1299 size = CEIL (size, align) * align;
1300 apply_args_reg_offset[regno] = size;
1301 size += GET_MODE_SIZE (mode);
1302 apply_args_mode[regno] = mode;
1304 else
1306 apply_args_mode[regno] = VOIDmode;
1307 apply_args_reg_offset[regno] = 0;
1310 return size;
1313 /* Return the size required for the block returned by __builtin_apply,
1314 and initialize apply_result_mode. */
1316 static int
1317 apply_result_size (void)
1319 static int size = -1;
1320 int align, regno;
1321 enum machine_mode mode;
1323 /* The values computed by this function never change. */
1324 if (size < 0)
1326 size = 0;
1328 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1329 if (FUNCTION_VALUE_REGNO_P (regno))
1331 mode = reg_raw_mode[regno];
1333 gcc_assert (mode != VOIDmode);
1335 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1336 if (size % align != 0)
1337 size = CEIL (size, align) * align;
1338 size += GET_MODE_SIZE (mode);
1339 apply_result_mode[regno] = mode;
1341 else
1342 apply_result_mode[regno] = VOIDmode;
1344 /* Allow targets that use untyped_call and untyped_return to override
1345 the size so that machine-specific information can be stored here. */
1346 #ifdef APPLY_RESULT_SIZE
1347 size = APPLY_RESULT_SIZE;
1348 #endif
1350 return size;
1353 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1354 /* Create a vector describing the result block RESULT. If SAVEP is true,
1355 the result block is used to save the values; otherwise it is used to
1356 restore the values. */
1358 static rtx
1359 result_vector (int savep, rtx result)
1361 int regno, size, align, nelts;
1362 enum machine_mode mode;
1363 rtx reg, mem;
1364 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1366 size = nelts = 0;
1367 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1368 if ((mode = apply_result_mode[regno]) != VOIDmode)
1370 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1371 if (size % align != 0)
1372 size = CEIL (size, align) * align;
1373 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1374 mem = adjust_address (result, mode, size);
1375 savevec[nelts++] = (savep
1376 ? gen_rtx_SET (VOIDmode, mem, reg)
1377 : gen_rtx_SET (VOIDmode, reg, mem));
1378 size += GET_MODE_SIZE (mode);
1380 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1382 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1384 /* Save the state required to perform an untyped call with the same
1385 arguments as were passed to the current function. */
1387 static rtx
1388 expand_builtin_apply_args_1 (void)
1390 rtx registers, tem;
1391 int size, align, regno;
1392 enum machine_mode mode;
1393 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1395 /* Create a block where the arg-pointer, structure value address,
1396 and argument registers can be saved. */
1397 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1399 /* Walk past the arg-pointer and structure value address. */
1400 size = GET_MODE_SIZE (Pmode);
1401 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1402 size += GET_MODE_SIZE (Pmode);
1404 /* Save each register used in calling a function to the block. */
1405 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1406 if ((mode = apply_args_mode[regno]) != VOIDmode)
1408 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1409 if (size % align != 0)
1410 size = CEIL (size, align) * align;
1412 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1414 emit_move_insn (adjust_address (registers, mode, size), tem);
1415 size += GET_MODE_SIZE (mode);
1418 /* Save the arg pointer to the block. */
1419 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1420 #ifdef STACK_GROWS_DOWNWARD
1421 /* We need the pointer as the caller actually passed them to us, not
1422 as we might have pretended they were passed. Make sure it's a valid
1423 operand, as emit_move_insn isn't expected to handle a PLUS. */
1425 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1426 NULL_RTX);
1427 #endif
1428 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1430 size = GET_MODE_SIZE (Pmode);
1432 /* Save the structure value address unless this is passed as an
1433 "invisible" first argument. */
1434 if (struct_incoming_value)
1436 emit_move_insn (adjust_address (registers, Pmode, size),
1437 copy_to_reg (struct_incoming_value));
1438 size += GET_MODE_SIZE (Pmode);
1441 /* Return the address of the block. */
1442 return copy_addr_to_reg (XEXP (registers, 0));
1445 /* __builtin_apply_args returns block of memory allocated on
1446 the stack into which is stored the arg pointer, structure
1447 value address, static chain, and all the registers that might
1448 possibly be used in performing a function call. The code is
1449 moved to the start of the function so the incoming values are
1450 saved. */
1452 static rtx
1453 expand_builtin_apply_args (void)
1455 /* Don't do __builtin_apply_args more than once in a function.
1456 Save the result of the first call and reuse it. */
1457 if (apply_args_value != 0)
1458 return apply_args_value;
1460 /* When this function is called, it means that registers must be
1461 saved on entry to this function. So we migrate the
1462 call to the first insn of this function. */
1463 rtx temp;
1464 rtx seq;
1466 start_sequence ();
1467 temp = expand_builtin_apply_args_1 ();
1468 seq = get_insns ();
1469 end_sequence ();
1471 apply_args_value = temp;
1473 /* Put the insns after the NOTE that starts the function.
1474 If this is inside a start_sequence, make the outer-level insn
1475 chain current, so the code is placed at the start of the
1476 function. If internal_arg_pointer is a non-virtual pseudo,
1477 it needs to be placed after the function that initializes
1478 that pseudo. */
1479 push_topmost_sequence ();
1480 if (REG_P (crtl->args.internal_arg_pointer)
1481 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1482 emit_insn_before (seq, parm_birth_insn);
1483 else
1484 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1485 pop_topmost_sequence ();
1486 return temp;
1490 /* Perform an untyped call and save the state required to perform an
1491 untyped return of whatever value was returned by the given function. */
1493 static rtx
1494 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1496 int size, align, regno;
1497 enum machine_mode mode;
1498 rtx incoming_args, result, reg, dest, src, call_insn;
1499 rtx old_stack_level = 0;
1500 rtx call_fusage = 0;
1501 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1503 arguments = convert_memory_address (Pmode, arguments);
1505 /* Create a block where the return registers can be saved. */
1506 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1508 /* Fetch the arg pointer from the ARGUMENTS block. */
1509 incoming_args = gen_reg_rtx (Pmode);
1510 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1511 #ifndef STACK_GROWS_DOWNWARD
1512 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1513 incoming_args, 0, OPTAB_LIB_WIDEN);
1514 #endif
1516 /* Push a new argument block and copy the arguments. Do not allow
1517 the (potential) memcpy call below to interfere with our stack
1518 manipulations. */
1519 do_pending_stack_adjust ();
1520 NO_DEFER_POP;
1522 /* Save the stack with nonlocal if available. */
1523 #ifdef HAVE_save_stack_nonlocal
1524 if (HAVE_save_stack_nonlocal)
1525 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1526 else
1527 #endif
1528 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1530 /* Allocate a block of memory onto the stack and copy the memory
1531 arguments to the outgoing arguments address. */
1532 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1534 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1535 may have already set current_function_calls_alloca to true.
1536 current_function_calls_alloca won't be set if argsize is zero,
1537 so we have to guarantee need_drap is true here. */
1538 if (SUPPORTS_STACK_ALIGNMENT)
1539 crtl->need_drap = true;
1541 dest = virtual_outgoing_args_rtx;
1542 #ifndef STACK_GROWS_DOWNWARD
1543 if (CONST_INT_P (argsize))
1544 dest = plus_constant (dest, -INTVAL (argsize));
1545 else
1546 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1547 #endif
1548 dest = gen_rtx_MEM (BLKmode, dest);
1549 set_mem_align (dest, PARM_BOUNDARY);
1550 src = gen_rtx_MEM (BLKmode, incoming_args);
1551 set_mem_align (src, PARM_BOUNDARY);
1552 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1554 /* Refer to the argument block. */
1555 apply_args_size ();
1556 arguments = gen_rtx_MEM (BLKmode, arguments);
1557 set_mem_align (arguments, PARM_BOUNDARY);
1559 /* Walk past the arg-pointer and structure value address. */
1560 size = GET_MODE_SIZE (Pmode);
1561 if (struct_value)
1562 size += GET_MODE_SIZE (Pmode);
1564 /* Restore each of the registers previously saved. Make USE insns
1565 for each of these registers for use in making the call. */
1566 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1567 if ((mode = apply_args_mode[regno]) != VOIDmode)
1569 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1570 if (size % align != 0)
1571 size = CEIL (size, align) * align;
1572 reg = gen_rtx_REG (mode, regno);
1573 emit_move_insn (reg, adjust_address (arguments, mode, size));
1574 use_reg (&call_fusage, reg);
1575 size += GET_MODE_SIZE (mode);
1578 /* Restore the structure value address unless this is passed as an
1579 "invisible" first argument. */
1580 size = GET_MODE_SIZE (Pmode);
1581 if (struct_value)
1583 rtx value = gen_reg_rtx (Pmode);
1584 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1585 emit_move_insn (struct_value, value);
1586 if (REG_P (struct_value))
1587 use_reg (&call_fusage, struct_value);
1588 size += GET_MODE_SIZE (Pmode);
1591 /* All arguments and registers used for the call are set up by now! */
1592 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1594 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1595 and we don't want to load it into a register as an optimization,
1596 because prepare_call_address already did it if it should be done. */
1597 if (GET_CODE (function) != SYMBOL_REF)
1598 function = memory_address (FUNCTION_MODE, function);
1600 /* Generate the actual call instruction and save the return value. */
1601 #ifdef HAVE_untyped_call
1602 if (HAVE_untyped_call)
1603 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1604 result, result_vector (1, result)));
1605 else
1606 #endif
1607 #ifdef HAVE_call_value
1608 if (HAVE_call_value)
1610 rtx valreg = 0;
1612 /* Locate the unique return register. It is not possible to
1613 express a call that sets more than one return register using
1614 call_value; use untyped_call for that. In fact, untyped_call
1615 only needs to save the return registers in the given block. */
1616 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1617 if ((mode = apply_result_mode[regno]) != VOIDmode)
1619 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1621 valreg = gen_rtx_REG (mode, regno);
1624 emit_call_insn (GEN_CALL_VALUE (valreg,
1625 gen_rtx_MEM (FUNCTION_MODE, function),
1626 const0_rtx, NULL_RTX, const0_rtx));
1628 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1630 else
1631 #endif
1632 gcc_unreachable ();
1634 /* Find the CALL insn we just emitted, and attach the register usage
1635 information. */
1636 call_insn = last_call_insn ();
1637 add_function_usage_to (call_insn, call_fusage);
1639 /* Restore the stack. */
1640 #ifdef HAVE_save_stack_nonlocal
1641 if (HAVE_save_stack_nonlocal)
1642 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1643 else
1644 #endif
1645 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1647 OK_DEFER_POP;
1649 /* Return the address of the result block. */
1650 result = copy_addr_to_reg (XEXP (result, 0));
1651 return convert_memory_address (ptr_mode, result);
1654 /* Perform an untyped return. */
1656 static void
1657 expand_builtin_return (rtx result)
1659 int size, align, regno;
1660 enum machine_mode mode;
1661 rtx reg;
1662 rtx call_fusage = 0;
1664 result = convert_memory_address (Pmode, result);
1666 apply_result_size ();
1667 result = gen_rtx_MEM (BLKmode, result);
1669 #ifdef HAVE_untyped_return
1670 if (HAVE_untyped_return)
1672 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1673 emit_barrier ();
1674 return;
1676 #endif
1678 /* Restore the return value and note that each value is used. */
1679 size = 0;
1680 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1681 if ((mode = apply_result_mode[regno]) != VOIDmode)
1683 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1684 if (size % align != 0)
1685 size = CEIL (size, align) * align;
1686 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1687 emit_move_insn (reg, adjust_address (result, mode, size));
1689 push_to_sequence (call_fusage);
1690 emit_use (reg);
1691 call_fusage = get_insns ();
1692 end_sequence ();
1693 size += GET_MODE_SIZE (mode);
1696 /* Put the USE insns before the return. */
1697 emit_insn (call_fusage);
1699 /* Return whatever values was restored by jumping directly to the end
1700 of the function. */
1701 expand_naked_return ();
1704 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1706 static enum type_class
1707 type_to_class (tree type)
1709 switch (TREE_CODE (type))
1711 case VOID_TYPE: return void_type_class;
1712 case INTEGER_TYPE: return integer_type_class;
1713 case ENUMERAL_TYPE: return enumeral_type_class;
1714 case BOOLEAN_TYPE: return boolean_type_class;
1715 case POINTER_TYPE: return pointer_type_class;
1716 case REFERENCE_TYPE: return reference_type_class;
1717 case OFFSET_TYPE: return offset_type_class;
1718 case REAL_TYPE: return real_type_class;
1719 case COMPLEX_TYPE: return complex_type_class;
1720 case FUNCTION_TYPE: return function_type_class;
1721 case METHOD_TYPE: return method_type_class;
1722 case RECORD_TYPE: return record_type_class;
1723 case UNION_TYPE:
1724 case QUAL_UNION_TYPE: return union_type_class;
1725 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1726 ? string_type_class : array_type_class);
1727 case LANG_TYPE: return lang_type_class;
1728 default: return no_type_class;
1732 /* Expand a call EXP to __builtin_classify_type. */
1734 static rtx
1735 expand_builtin_classify_type (tree exp)
1737 if (call_expr_nargs (exp))
1738 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1739 return GEN_INT (no_type_class);
1742 /* This helper macro, meant to be used in mathfn_built_in below,
1743 determines which among a set of three builtin math functions is
1744 appropriate for a given type mode. The `F' and `L' cases are
1745 automatically generated from the `double' case. */
1746 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1747 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1748 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1749 fcodel = BUILT_IN_MATHFN##L ; break;
1750 /* Similar to above, but appends _R after any F/L suffix. */
1751 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1752 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1753 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1754 fcodel = BUILT_IN_MATHFN##L_R ; break;
1756 /* Return mathematic function equivalent to FN but operating directly
1757 on TYPE, if available. If IMPLICIT is true find the function in
1758 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1759 can't do the conversion, return zero. */
1761 static tree
1762 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1764 tree const *const fn_arr
1765 = implicit ? implicit_built_in_decls : built_in_decls;
1766 enum built_in_function fcode, fcodef, fcodel;
1768 switch (fn)
1770 CASE_MATHFN (BUILT_IN_ACOS)
1771 CASE_MATHFN (BUILT_IN_ACOSH)
1772 CASE_MATHFN (BUILT_IN_ASIN)
1773 CASE_MATHFN (BUILT_IN_ASINH)
1774 CASE_MATHFN (BUILT_IN_ATAN)
1775 CASE_MATHFN (BUILT_IN_ATAN2)
1776 CASE_MATHFN (BUILT_IN_ATANH)
1777 CASE_MATHFN (BUILT_IN_CBRT)
1778 CASE_MATHFN (BUILT_IN_CEIL)
1779 CASE_MATHFN (BUILT_IN_CEXPI)
1780 CASE_MATHFN (BUILT_IN_COPYSIGN)
1781 CASE_MATHFN (BUILT_IN_COS)
1782 CASE_MATHFN (BUILT_IN_COSH)
1783 CASE_MATHFN (BUILT_IN_DREM)
1784 CASE_MATHFN (BUILT_IN_ERF)
1785 CASE_MATHFN (BUILT_IN_ERFC)
1786 CASE_MATHFN (BUILT_IN_EXP)
1787 CASE_MATHFN (BUILT_IN_EXP10)
1788 CASE_MATHFN (BUILT_IN_EXP2)
1789 CASE_MATHFN (BUILT_IN_EXPM1)
1790 CASE_MATHFN (BUILT_IN_FABS)
1791 CASE_MATHFN (BUILT_IN_FDIM)
1792 CASE_MATHFN (BUILT_IN_FLOOR)
1793 CASE_MATHFN (BUILT_IN_FMA)
1794 CASE_MATHFN (BUILT_IN_FMAX)
1795 CASE_MATHFN (BUILT_IN_FMIN)
1796 CASE_MATHFN (BUILT_IN_FMOD)
1797 CASE_MATHFN (BUILT_IN_FREXP)
1798 CASE_MATHFN (BUILT_IN_GAMMA)
1799 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1800 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1801 CASE_MATHFN (BUILT_IN_HYPOT)
1802 CASE_MATHFN (BUILT_IN_ILOGB)
1803 CASE_MATHFN (BUILT_IN_INF)
1804 CASE_MATHFN (BUILT_IN_ISINF)
1805 CASE_MATHFN (BUILT_IN_J0)
1806 CASE_MATHFN (BUILT_IN_J1)
1807 CASE_MATHFN (BUILT_IN_JN)
1808 CASE_MATHFN (BUILT_IN_LCEIL)
1809 CASE_MATHFN (BUILT_IN_LDEXP)
1810 CASE_MATHFN (BUILT_IN_LFLOOR)
1811 CASE_MATHFN (BUILT_IN_LGAMMA)
1812 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1813 CASE_MATHFN (BUILT_IN_LLCEIL)
1814 CASE_MATHFN (BUILT_IN_LLFLOOR)
1815 CASE_MATHFN (BUILT_IN_LLRINT)
1816 CASE_MATHFN (BUILT_IN_LLROUND)
1817 CASE_MATHFN (BUILT_IN_LOG)
1818 CASE_MATHFN (BUILT_IN_LOG10)
1819 CASE_MATHFN (BUILT_IN_LOG1P)
1820 CASE_MATHFN (BUILT_IN_LOG2)
1821 CASE_MATHFN (BUILT_IN_LOGB)
1822 CASE_MATHFN (BUILT_IN_LRINT)
1823 CASE_MATHFN (BUILT_IN_LROUND)
1824 CASE_MATHFN (BUILT_IN_MODF)
1825 CASE_MATHFN (BUILT_IN_NAN)
1826 CASE_MATHFN (BUILT_IN_NANS)
1827 CASE_MATHFN (BUILT_IN_NEARBYINT)
1828 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1829 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1830 CASE_MATHFN (BUILT_IN_POW)
1831 CASE_MATHFN (BUILT_IN_POWI)
1832 CASE_MATHFN (BUILT_IN_POW10)
1833 CASE_MATHFN (BUILT_IN_REMAINDER)
1834 CASE_MATHFN (BUILT_IN_REMQUO)
1835 CASE_MATHFN (BUILT_IN_RINT)
1836 CASE_MATHFN (BUILT_IN_ROUND)
1837 CASE_MATHFN (BUILT_IN_SCALB)
1838 CASE_MATHFN (BUILT_IN_SCALBLN)
1839 CASE_MATHFN (BUILT_IN_SCALBN)
1840 CASE_MATHFN (BUILT_IN_SIGNBIT)
1841 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1842 CASE_MATHFN (BUILT_IN_SIN)
1843 CASE_MATHFN (BUILT_IN_SINCOS)
1844 CASE_MATHFN (BUILT_IN_SINH)
1845 CASE_MATHFN (BUILT_IN_SQRT)
1846 CASE_MATHFN (BUILT_IN_TAN)
1847 CASE_MATHFN (BUILT_IN_TANH)
1848 CASE_MATHFN (BUILT_IN_TGAMMA)
1849 CASE_MATHFN (BUILT_IN_TRUNC)
1850 CASE_MATHFN (BUILT_IN_Y0)
1851 CASE_MATHFN (BUILT_IN_Y1)
1852 CASE_MATHFN (BUILT_IN_YN)
1854 default:
1855 return NULL_TREE;
1858 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1859 return fn_arr[fcode];
1860 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1861 return fn_arr[fcodef];
1862 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1863 return fn_arr[fcodel];
1864 else
1865 return NULL_TREE;
1868 /* Like mathfn_built_in_1(), but always use the implicit array. */
1870 tree
1871 mathfn_built_in (tree type, enum built_in_function fn)
1873 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1876 /* If errno must be maintained, expand the RTL to check if the result,
1877 TARGET, of a built-in function call, EXP, is NaN, and if so set
1878 errno to EDOM. */
1880 static void
1881 expand_errno_check (tree exp, rtx target)
1883 rtx lab = gen_label_rtx ();
1885 /* Test the result; if it is NaN, set errno=EDOM because
1886 the argument was not in the domain. */
1887 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1888 NULL_RTX, NULL_RTX, lab);
1890 #ifdef TARGET_EDOM
1891 /* If this built-in doesn't throw an exception, set errno directly. */
1892 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1894 #ifdef GEN_ERRNO_RTX
1895 rtx errno_rtx = GEN_ERRNO_RTX;
1896 #else
1897 rtx errno_rtx
1898 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1899 #endif
1900 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1901 emit_label (lab);
1902 return;
1904 #endif
1906 /* Make sure the library call isn't expanded as a tail call. */
1907 CALL_EXPR_TAILCALL (exp) = 0;
1909 /* We can't set errno=EDOM directly; let the library call do it.
1910 Pop the arguments right away in case the call gets deleted. */
1911 NO_DEFER_POP;
1912 expand_call (exp, target, 0);
1913 OK_DEFER_POP;
1914 emit_label (lab);
1917 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1918 Return NULL_RTX if a normal call should be emitted rather than expanding
1919 the function in-line. EXP is the expression that is a call to the builtin
1920 function; if convenient, the result should be placed in TARGET.
1921 SUBTARGET may be used as the target for computing one of EXP's operands. */
1923 static rtx
1924 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1926 optab builtin_optab;
1927 rtx op0, insns, before_call;
1928 tree fndecl = get_callee_fndecl (exp);
1929 enum machine_mode mode;
1930 bool errno_set = false;
1931 tree arg;
1933 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1934 return NULL_RTX;
1936 arg = CALL_EXPR_ARG (exp, 0);
1938 switch (DECL_FUNCTION_CODE (fndecl))
1940 CASE_FLT_FN (BUILT_IN_SQRT):
1941 errno_set = ! tree_expr_nonnegative_p (arg);
1942 builtin_optab = sqrt_optab;
1943 break;
1944 CASE_FLT_FN (BUILT_IN_EXP):
1945 errno_set = true; builtin_optab = exp_optab; break;
1946 CASE_FLT_FN (BUILT_IN_EXP10):
1947 CASE_FLT_FN (BUILT_IN_POW10):
1948 errno_set = true; builtin_optab = exp10_optab; break;
1949 CASE_FLT_FN (BUILT_IN_EXP2):
1950 errno_set = true; builtin_optab = exp2_optab; break;
1951 CASE_FLT_FN (BUILT_IN_EXPM1):
1952 errno_set = true; builtin_optab = expm1_optab; break;
1953 CASE_FLT_FN (BUILT_IN_LOGB):
1954 errno_set = true; builtin_optab = logb_optab; break;
1955 CASE_FLT_FN (BUILT_IN_LOG):
1956 errno_set = true; builtin_optab = log_optab; break;
1957 CASE_FLT_FN (BUILT_IN_LOG10):
1958 errno_set = true; builtin_optab = log10_optab; break;
1959 CASE_FLT_FN (BUILT_IN_LOG2):
1960 errno_set = true; builtin_optab = log2_optab; break;
1961 CASE_FLT_FN (BUILT_IN_LOG1P):
1962 errno_set = true; builtin_optab = log1p_optab; break;
1963 CASE_FLT_FN (BUILT_IN_ASIN):
1964 builtin_optab = asin_optab; break;
1965 CASE_FLT_FN (BUILT_IN_ACOS):
1966 builtin_optab = acos_optab; break;
1967 CASE_FLT_FN (BUILT_IN_TAN):
1968 builtin_optab = tan_optab; break;
1969 CASE_FLT_FN (BUILT_IN_ATAN):
1970 builtin_optab = atan_optab; break;
1971 CASE_FLT_FN (BUILT_IN_FLOOR):
1972 builtin_optab = floor_optab; break;
1973 CASE_FLT_FN (BUILT_IN_CEIL):
1974 builtin_optab = ceil_optab; break;
1975 CASE_FLT_FN (BUILT_IN_TRUNC):
1976 builtin_optab = btrunc_optab; break;
1977 CASE_FLT_FN (BUILT_IN_ROUND):
1978 builtin_optab = round_optab; break;
1979 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1980 builtin_optab = nearbyint_optab;
1981 if (flag_trapping_math)
1982 break;
1983 /* Else fallthrough and expand as rint. */
1984 CASE_FLT_FN (BUILT_IN_RINT):
1985 builtin_optab = rint_optab; break;
1986 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
1987 builtin_optab = significand_optab; break;
1988 default:
1989 gcc_unreachable ();
1992 /* Make a suitable register to place result in. */
1993 mode = TYPE_MODE (TREE_TYPE (exp));
1995 if (! flag_errno_math || ! HONOR_NANS (mode))
1996 errno_set = false;
1998 /* Before working hard, check whether the instruction is available. */
1999 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2001 target = gen_reg_rtx (mode);
2003 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2004 need to expand the argument again. This way, we will not perform
2005 side-effects more the once. */
2006 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2008 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2010 start_sequence ();
2012 /* Compute into TARGET.
2013 Set TARGET to wherever the result comes back. */
2014 target = expand_unop (mode, builtin_optab, op0, target, 0);
2016 if (target != 0)
2018 if (errno_set)
2019 expand_errno_check (exp, target);
2021 /* Output the entire sequence. */
2022 insns = get_insns ();
2023 end_sequence ();
2024 emit_insn (insns);
2025 return target;
2028 /* If we were unable to expand via the builtin, stop the sequence
2029 (without outputting the insns) and call to the library function
2030 with the stabilized argument list. */
2031 end_sequence ();
2034 before_call = get_last_insn ();
2036 return expand_call (exp, target, target == const0_rtx);
2039 /* Expand a call to the builtin binary math functions (pow and atan2).
2040 Return NULL_RTX if a normal call should be emitted rather than expanding the
2041 function in-line. EXP is the expression that is a call to the builtin
2042 function; if convenient, the result should be placed in TARGET.
2043 SUBTARGET may be used as the target for computing one of EXP's
2044 operands. */
2046 static rtx
2047 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2049 optab builtin_optab;
2050 rtx op0, op1, insns;
2051 int op1_type = REAL_TYPE;
2052 tree fndecl = get_callee_fndecl (exp);
2053 tree arg0, arg1;
2054 enum machine_mode mode;
2055 bool errno_set = true;
2057 switch (DECL_FUNCTION_CODE (fndecl))
2059 CASE_FLT_FN (BUILT_IN_SCALBN):
2060 CASE_FLT_FN (BUILT_IN_SCALBLN):
2061 CASE_FLT_FN (BUILT_IN_LDEXP):
2062 op1_type = INTEGER_TYPE;
2063 default:
2064 break;
2067 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2068 return NULL_RTX;
2070 arg0 = CALL_EXPR_ARG (exp, 0);
2071 arg1 = CALL_EXPR_ARG (exp, 1);
2073 switch (DECL_FUNCTION_CODE (fndecl))
2075 CASE_FLT_FN (BUILT_IN_POW):
2076 builtin_optab = pow_optab; break;
2077 CASE_FLT_FN (BUILT_IN_ATAN2):
2078 builtin_optab = atan2_optab; break;
2079 CASE_FLT_FN (BUILT_IN_SCALB):
2080 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2081 return 0;
2082 builtin_optab = scalb_optab; break;
2083 CASE_FLT_FN (BUILT_IN_SCALBN):
2084 CASE_FLT_FN (BUILT_IN_SCALBLN):
2085 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2086 return 0;
2087 /* Fall through... */
2088 CASE_FLT_FN (BUILT_IN_LDEXP):
2089 builtin_optab = ldexp_optab; break;
2090 CASE_FLT_FN (BUILT_IN_FMOD):
2091 builtin_optab = fmod_optab; break;
2092 CASE_FLT_FN (BUILT_IN_REMAINDER):
2093 CASE_FLT_FN (BUILT_IN_DREM):
2094 builtin_optab = remainder_optab; break;
2095 default:
2096 gcc_unreachable ();
2099 /* Make a suitable register to place result in. */
2100 mode = TYPE_MODE (TREE_TYPE (exp));
2102 /* Before working hard, check whether the instruction is available. */
2103 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2104 return NULL_RTX;
2106 target = gen_reg_rtx (mode);
2108 if (! flag_errno_math || ! HONOR_NANS (mode))
2109 errno_set = false;
2111 /* Always stabilize the argument list. */
2112 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2113 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2115 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2116 op1 = expand_normal (arg1);
2118 start_sequence ();
2120 /* Compute into TARGET.
2121 Set TARGET to wherever the result comes back. */
2122 target = expand_binop (mode, builtin_optab, op0, op1,
2123 target, 0, OPTAB_DIRECT);
2125 /* If we were unable to expand via the builtin, stop the sequence
2126 (without outputting the insns) and call to the library function
2127 with the stabilized argument list. */
2128 if (target == 0)
2130 end_sequence ();
2131 return expand_call (exp, target, target == const0_rtx);
2134 if (errno_set)
2135 expand_errno_check (exp, target);
2137 /* Output the entire sequence. */
2138 insns = get_insns ();
2139 end_sequence ();
2140 emit_insn (insns);
2142 return target;
2145 /* Expand a call to the builtin sin and cos math functions.
2146 Return NULL_RTX if a normal call should be emitted rather than expanding the
2147 function in-line. EXP is the expression that is a call to the builtin
2148 function; if convenient, the result should be placed in TARGET.
2149 SUBTARGET may be used as the target for computing one of EXP's
2150 operands. */
2152 static rtx
2153 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2155 optab builtin_optab;
2156 rtx op0, insns;
2157 tree fndecl = get_callee_fndecl (exp);
2158 enum machine_mode mode;
2159 tree arg;
2161 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2162 return NULL_RTX;
2164 arg = CALL_EXPR_ARG (exp, 0);
2166 switch (DECL_FUNCTION_CODE (fndecl))
2168 CASE_FLT_FN (BUILT_IN_SIN):
2169 CASE_FLT_FN (BUILT_IN_COS):
2170 builtin_optab = sincos_optab; break;
2171 default:
2172 gcc_unreachable ();
2175 /* Make a suitable register to place result in. */
2176 mode = TYPE_MODE (TREE_TYPE (exp));
2178 /* Check if sincos insn is available, otherwise fallback
2179 to sin or cos insn. */
2180 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2181 switch (DECL_FUNCTION_CODE (fndecl))
2183 CASE_FLT_FN (BUILT_IN_SIN):
2184 builtin_optab = sin_optab; break;
2185 CASE_FLT_FN (BUILT_IN_COS):
2186 builtin_optab = cos_optab; break;
2187 default:
2188 gcc_unreachable ();
2191 /* Before working hard, check whether the instruction is available. */
2192 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2194 target = gen_reg_rtx (mode);
2196 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2197 need to expand the argument again. This way, we will not perform
2198 side-effects more the once. */
2199 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2201 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2203 start_sequence ();
2205 /* Compute into TARGET.
2206 Set TARGET to wherever the result comes back. */
2207 if (builtin_optab == sincos_optab)
2209 int result;
2211 switch (DECL_FUNCTION_CODE (fndecl))
2213 CASE_FLT_FN (BUILT_IN_SIN):
2214 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2215 break;
2216 CASE_FLT_FN (BUILT_IN_COS):
2217 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2218 break;
2219 default:
2220 gcc_unreachable ();
2222 gcc_assert (result);
2224 else
2226 target = expand_unop (mode, builtin_optab, op0, target, 0);
2229 if (target != 0)
2231 /* Output the entire sequence. */
2232 insns = get_insns ();
2233 end_sequence ();
2234 emit_insn (insns);
2235 return target;
2238 /* If we were unable to expand via the builtin, stop the sequence
2239 (without outputting the insns) and call to the library function
2240 with the stabilized argument list. */
2241 end_sequence ();
2244 target = expand_call (exp, target, target == const0_rtx);
2246 return target;
2249 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2250 return an RTL instruction code that implements the functionality.
2251 If that isn't possible or available return CODE_FOR_nothing. */
2253 static enum insn_code
2254 interclass_mathfn_icode (tree arg, tree fndecl)
2256 bool errno_set = false;
2257 optab builtin_optab = 0;
2258 enum machine_mode mode;
2260 switch (DECL_FUNCTION_CODE (fndecl))
2262 CASE_FLT_FN (BUILT_IN_ILOGB):
2263 errno_set = true; builtin_optab = ilogb_optab; break;
2264 CASE_FLT_FN (BUILT_IN_ISINF):
2265 builtin_optab = isinf_optab; break;
2266 case BUILT_IN_ISNORMAL:
2267 case BUILT_IN_ISFINITE:
2268 CASE_FLT_FN (BUILT_IN_FINITE):
2269 case BUILT_IN_FINITED32:
2270 case BUILT_IN_FINITED64:
2271 case BUILT_IN_FINITED128:
2272 case BUILT_IN_ISINFD32:
2273 case BUILT_IN_ISINFD64:
2274 case BUILT_IN_ISINFD128:
2275 /* These builtins have no optabs (yet). */
2276 break;
2277 default:
2278 gcc_unreachable ();
2281 /* There's no easy way to detect the case we need to set EDOM. */
2282 if (flag_errno_math && errno_set)
2283 return CODE_FOR_nothing;
2285 /* Optab mode depends on the mode of the input argument. */
2286 mode = TYPE_MODE (TREE_TYPE (arg));
2288 if (builtin_optab)
2289 return optab_handler (builtin_optab, mode)->insn_code;
2290 return CODE_FOR_nothing;
2293 /* Expand a call to one of the builtin math functions that operate on
2294 floating point argument and output an integer result (ilogb, isinf,
2295 isnan, etc).
2296 Return 0 if a normal call should be emitted rather than expanding the
2297 function in-line. EXP is the expression that is a call to the builtin
2298 function; if convenient, the result should be placed in TARGET.
2299 SUBTARGET may be used as the target for computing one of EXP's operands. */
2301 static rtx
2302 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2304 enum insn_code icode = CODE_FOR_nothing;
2305 rtx op0;
2306 tree fndecl = get_callee_fndecl (exp);
2307 enum machine_mode mode;
2308 tree arg;
2310 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2311 return NULL_RTX;
2313 arg = CALL_EXPR_ARG (exp, 0);
2314 icode = interclass_mathfn_icode (arg, fndecl);
2315 mode = TYPE_MODE (TREE_TYPE (arg));
2317 if (icode != CODE_FOR_nothing)
2319 /* Make a suitable register to place result in. */
2320 if (!target
2321 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2322 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2324 gcc_assert (insn_data[icode].operand[0].predicate
2325 (target, GET_MODE (target)));
2327 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2328 need to expand the argument again. This way, we will not perform
2329 side-effects more the once. */
2330 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2332 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2334 if (mode != GET_MODE (op0))
2335 op0 = convert_to_mode (mode, op0, 0);
2337 /* Compute into TARGET.
2338 Set TARGET to wherever the result comes back. */
2339 emit_unop_insn (icode, target, op0, UNKNOWN);
2340 return target;
2343 return NULL_RTX;
2346 /* Expand a call to the builtin sincos math function.
2347 Return NULL_RTX if a normal call should be emitted rather than expanding the
2348 function in-line. EXP is the expression that is a call to the builtin
2349 function. */
2351 static rtx
2352 expand_builtin_sincos (tree exp)
2354 rtx op0, op1, op2, target1, target2;
2355 enum machine_mode mode;
2356 tree arg, sinp, cosp;
2357 int result;
2358 location_t loc = EXPR_LOCATION (exp);
2360 if (!validate_arglist (exp, REAL_TYPE,
2361 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2362 return NULL_RTX;
2364 arg = CALL_EXPR_ARG (exp, 0);
2365 sinp = CALL_EXPR_ARG (exp, 1);
2366 cosp = CALL_EXPR_ARG (exp, 2);
2368 /* Make a suitable register to place result in. */
2369 mode = TYPE_MODE (TREE_TYPE (arg));
2371 /* Check if sincos insn is available, otherwise emit the call. */
2372 if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing)
2373 return NULL_RTX;
2375 target1 = gen_reg_rtx (mode);
2376 target2 = gen_reg_rtx (mode);
2378 op0 = expand_normal (arg);
2379 op1 = expand_normal (build_fold_indirect_ref_loc (loc, sinp));
2380 op2 = expand_normal (build_fold_indirect_ref_loc (loc, cosp));
2382 /* Compute into target1 and target2.
2383 Set TARGET to wherever the result comes back. */
2384 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2385 gcc_assert (result);
2387 /* Move target1 and target2 to the memory locations indicated
2388 by op1 and op2. */
2389 emit_move_insn (op1, target1);
2390 emit_move_insn (op2, target2);
2392 return const0_rtx;
2395 /* Expand a call to the internal cexpi builtin to the sincos math function.
2396 EXP is the expression that is a call to the builtin function; if convenient,
2397 the result should be placed in TARGET. SUBTARGET may be used as the target
2398 for computing one of EXP's operands. */
2400 static rtx
2401 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2403 tree fndecl = get_callee_fndecl (exp);
2404 tree arg, type;
2405 enum machine_mode mode;
2406 rtx op0, op1, op2;
2407 location_t loc = EXPR_LOCATION (exp);
2409 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2410 return NULL_RTX;
2412 arg = CALL_EXPR_ARG (exp, 0);
2413 type = TREE_TYPE (arg);
2414 mode = TYPE_MODE (TREE_TYPE (arg));
2416 /* Try expanding via a sincos optab, fall back to emitting a libcall
2417 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2418 is only generated from sincos, cexp or if we have either of them. */
2419 if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing)
2421 op1 = gen_reg_rtx (mode);
2422 op2 = gen_reg_rtx (mode);
2424 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2426 /* Compute into op1 and op2. */
2427 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2429 else if (TARGET_HAS_SINCOS)
2431 tree call, fn = NULL_TREE;
2432 tree top1, top2;
2433 rtx op1a, op2a;
2435 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2436 fn = built_in_decls[BUILT_IN_SINCOSF];
2437 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2438 fn = built_in_decls[BUILT_IN_SINCOS];
2439 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2440 fn = built_in_decls[BUILT_IN_SINCOSL];
2441 else
2442 gcc_unreachable ();
2444 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2445 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2446 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2447 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2448 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2449 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2451 /* Make sure not to fold the sincos call again. */
2452 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2453 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2454 call, 3, arg, top1, top2));
2456 else
2458 tree call, fn = NULL_TREE, narg;
2459 tree ctype = build_complex_type (type);
2461 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2462 fn = built_in_decls[BUILT_IN_CEXPF];
2463 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2464 fn = built_in_decls[BUILT_IN_CEXP];
2465 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2466 fn = built_in_decls[BUILT_IN_CEXPL];
2467 else
2468 gcc_unreachable ();
2470 /* If we don't have a decl for cexp create one. This is the
2471 friendliest fallback if the user calls __builtin_cexpi
2472 without full target C99 function support. */
2473 if (fn == NULL_TREE)
2475 tree fntype;
2476 const char *name = NULL;
2478 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2479 name = "cexpf";
2480 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2481 name = "cexp";
2482 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2483 name = "cexpl";
2485 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2486 fn = build_fn_decl (name, fntype);
2489 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2490 build_real (type, dconst0), arg);
2492 /* Make sure not to fold the cexp call again. */
2493 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2494 return expand_expr (build_call_nary (ctype, call, 1, narg),
2495 target, VOIDmode, EXPAND_NORMAL);
2498 /* Now build the proper return type. */
2499 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2500 make_tree (TREE_TYPE (arg), op2),
2501 make_tree (TREE_TYPE (arg), op1)),
2502 target, VOIDmode, EXPAND_NORMAL);
2505 /* Conveniently construct a function call expression. FNDECL names the
2506 function to be called, N is the number of arguments, and the "..."
2507 parameters are the argument expressions. Unlike build_call_exr
2508 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2510 static tree
2511 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2513 va_list ap;
2514 tree fntype = TREE_TYPE (fndecl);
2515 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2517 va_start (ap, n);
2518 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2519 va_end (ap);
2520 SET_EXPR_LOCATION (fn, loc);
2521 return fn;
2523 #define build_call_nofold(...) \
2524 build_call_nofold_loc (UNKNOWN_LOCATION, __VA_ARGS__)
2526 /* Expand a call to one of the builtin rounding functions gcc defines
2527 as an extension (lfloor and lceil). As these are gcc extensions we
2528 do not need to worry about setting errno to EDOM.
2529 If expanding via optab fails, lower expression to (int)(floor(x)).
2530 EXP is the expression that is a call to the builtin function;
2531 if convenient, the result should be placed in TARGET. */
2533 static rtx
2534 expand_builtin_int_roundingfn (tree exp, rtx target)
2536 convert_optab builtin_optab;
2537 rtx op0, insns, tmp;
2538 tree fndecl = get_callee_fndecl (exp);
2539 enum built_in_function fallback_fn;
2540 tree fallback_fndecl;
2541 enum machine_mode mode;
2542 tree arg;
2544 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2545 gcc_unreachable ();
2547 arg = CALL_EXPR_ARG (exp, 0);
2549 switch (DECL_FUNCTION_CODE (fndecl))
2551 CASE_FLT_FN (BUILT_IN_LCEIL):
2552 CASE_FLT_FN (BUILT_IN_LLCEIL):
2553 builtin_optab = lceil_optab;
2554 fallback_fn = BUILT_IN_CEIL;
2555 break;
2557 CASE_FLT_FN (BUILT_IN_LFLOOR):
2558 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2559 builtin_optab = lfloor_optab;
2560 fallback_fn = BUILT_IN_FLOOR;
2561 break;
2563 default:
2564 gcc_unreachable ();
2567 /* Make a suitable register to place result in. */
2568 mode = TYPE_MODE (TREE_TYPE (exp));
2570 target = gen_reg_rtx (mode);
2572 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2573 need to expand the argument again. This way, we will not perform
2574 side-effects more the once. */
2575 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2577 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2579 start_sequence ();
2581 /* Compute into TARGET. */
2582 if (expand_sfix_optab (target, op0, builtin_optab))
2584 /* Output the entire sequence. */
2585 insns = get_insns ();
2586 end_sequence ();
2587 emit_insn (insns);
2588 return target;
2591 /* If we were unable to expand via the builtin, stop the sequence
2592 (without outputting the insns). */
2593 end_sequence ();
2595 /* Fall back to floating point rounding optab. */
2596 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2598 /* For non-C99 targets we may end up without a fallback fndecl here
2599 if the user called __builtin_lfloor directly. In this case emit
2600 a call to the floor/ceil variants nevertheless. This should result
2601 in the best user experience for not full C99 targets. */
2602 if (fallback_fndecl == NULL_TREE)
2604 tree fntype;
2605 const char *name = NULL;
2607 switch (DECL_FUNCTION_CODE (fndecl))
2609 case BUILT_IN_LCEIL:
2610 case BUILT_IN_LLCEIL:
2611 name = "ceil";
2612 break;
2613 case BUILT_IN_LCEILF:
2614 case BUILT_IN_LLCEILF:
2615 name = "ceilf";
2616 break;
2617 case BUILT_IN_LCEILL:
2618 case BUILT_IN_LLCEILL:
2619 name = "ceill";
2620 break;
2621 case BUILT_IN_LFLOOR:
2622 case BUILT_IN_LLFLOOR:
2623 name = "floor";
2624 break;
2625 case BUILT_IN_LFLOORF:
2626 case BUILT_IN_LLFLOORF:
2627 name = "floorf";
2628 break;
2629 case BUILT_IN_LFLOORL:
2630 case BUILT_IN_LLFLOORL:
2631 name = "floorl";
2632 break;
2633 default:
2634 gcc_unreachable ();
2637 fntype = build_function_type_list (TREE_TYPE (arg),
2638 TREE_TYPE (arg), NULL_TREE);
2639 fallback_fndecl = build_fn_decl (name, fntype);
2642 exp = build_call_nofold (fallback_fndecl, 1, arg);
2644 tmp = expand_normal (exp);
2646 /* Truncate the result of floating point optab to integer
2647 via expand_fix (). */
2648 target = gen_reg_rtx (mode);
2649 expand_fix (target, tmp, 0);
2651 return target;
2654 /* Expand a call to one of the builtin math functions doing integer
2655 conversion (lrint).
2656 Return 0 if a normal call should be emitted rather than expanding the
2657 function in-line. EXP is the expression that is a call to the builtin
2658 function; if convenient, the result should be placed in TARGET. */
2660 static rtx
2661 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2663 convert_optab builtin_optab;
2664 rtx op0, insns;
2665 tree fndecl = get_callee_fndecl (exp);
2666 tree arg;
2667 enum machine_mode mode;
2669 /* There's no easy way to detect the case we need to set EDOM. */
2670 if (flag_errno_math)
2671 return NULL_RTX;
2673 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2674 gcc_unreachable ();
2676 arg = CALL_EXPR_ARG (exp, 0);
2678 switch (DECL_FUNCTION_CODE (fndecl))
2680 CASE_FLT_FN (BUILT_IN_LRINT):
2681 CASE_FLT_FN (BUILT_IN_LLRINT):
2682 builtin_optab = lrint_optab; break;
2683 CASE_FLT_FN (BUILT_IN_LROUND):
2684 CASE_FLT_FN (BUILT_IN_LLROUND):
2685 builtin_optab = lround_optab; break;
2686 default:
2687 gcc_unreachable ();
2690 /* Make a suitable register to place result in. */
2691 mode = TYPE_MODE (TREE_TYPE (exp));
2693 target = gen_reg_rtx (mode);
2695 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2696 need to expand the argument again. This way, we will not perform
2697 side-effects more the once. */
2698 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2700 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2702 start_sequence ();
2704 if (expand_sfix_optab (target, op0, builtin_optab))
2706 /* Output the entire sequence. */
2707 insns = get_insns ();
2708 end_sequence ();
2709 emit_insn (insns);
2710 return target;
2713 /* If we were unable to expand via the builtin, stop the sequence
2714 (without outputting the insns) and call to the library function
2715 with the stabilized argument list. */
2716 end_sequence ();
2718 target = expand_call (exp, target, target == const0_rtx);
2720 return target;
2723 /* To evaluate powi(x,n), the floating point value x raised to the
2724 constant integer exponent n, we use a hybrid algorithm that
2725 combines the "window method" with look-up tables. For an
2726 introduction to exponentiation algorithms and "addition chains",
2727 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2728 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2729 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2730 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2732 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2733 multiplications to inline before calling the system library's pow
2734 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2735 so this default never requires calling pow, powf or powl. */
2737 #ifndef POWI_MAX_MULTS
2738 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2739 #endif
2741 /* The size of the "optimal power tree" lookup table. All
2742 exponents less than this value are simply looked up in the
2743 powi_table below. This threshold is also used to size the
2744 cache of pseudo registers that hold intermediate results. */
2745 #define POWI_TABLE_SIZE 256
2747 /* The size, in bits of the window, used in the "window method"
2748 exponentiation algorithm. This is equivalent to a radix of
2749 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2750 #define POWI_WINDOW_SIZE 3
2752 /* The following table is an efficient representation of an
2753 "optimal power tree". For each value, i, the corresponding
2754 value, j, in the table states than an optimal evaluation
2755 sequence for calculating pow(x,i) can be found by evaluating
2756 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2757 100 integers is given in Knuth's "Seminumerical algorithms". */
2759 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2761 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2762 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2763 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2764 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2765 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2766 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2767 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2768 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2769 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2770 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2771 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2772 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2773 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2774 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2775 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2776 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2777 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2778 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2779 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2780 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2781 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2782 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2783 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2784 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2785 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2786 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2787 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2788 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2789 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2790 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2791 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2792 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2796 /* Return the number of multiplications required to calculate
2797 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2798 subroutine of powi_cost. CACHE is an array indicating
2799 which exponents have already been calculated. */
2801 static int
2802 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2804 /* If we've already calculated this exponent, then this evaluation
2805 doesn't require any additional multiplications. */
2806 if (cache[n])
2807 return 0;
2809 cache[n] = true;
2810 return powi_lookup_cost (n - powi_table[n], cache)
2811 + powi_lookup_cost (powi_table[n], cache) + 1;
2814 /* Return the number of multiplications required to calculate
2815 powi(x,n) for an arbitrary x, given the exponent N. This
2816 function needs to be kept in sync with expand_powi below. */
2818 static int
2819 powi_cost (HOST_WIDE_INT n)
2821 bool cache[POWI_TABLE_SIZE];
2822 unsigned HOST_WIDE_INT digit;
2823 unsigned HOST_WIDE_INT val;
2824 int result;
2826 if (n == 0)
2827 return 0;
2829 /* Ignore the reciprocal when calculating the cost. */
2830 val = (n < 0) ? -n : n;
2832 /* Initialize the exponent cache. */
2833 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2834 cache[1] = true;
2836 result = 0;
2838 while (val >= POWI_TABLE_SIZE)
2840 if (val & 1)
2842 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2843 result += powi_lookup_cost (digit, cache)
2844 + POWI_WINDOW_SIZE + 1;
2845 val >>= POWI_WINDOW_SIZE;
2847 else
2849 val >>= 1;
2850 result++;
2854 return result + powi_lookup_cost (val, cache);
2857 /* Recursive subroutine of expand_powi. This function takes the array,
2858 CACHE, of already calculated exponents and an exponent N and returns
2859 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2861 static rtx
2862 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2864 unsigned HOST_WIDE_INT digit;
2865 rtx target, result;
2866 rtx op0, op1;
2868 if (n < POWI_TABLE_SIZE)
2870 if (cache[n])
2871 return cache[n];
2873 target = gen_reg_rtx (mode);
2874 cache[n] = target;
2876 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2877 op1 = expand_powi_1 (mode, powi_table[n], cache);
2879 else if (n & 1)
2881 target = gen_reg_rtx (mode);
2882 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2883 op0 = expand_powi_1 (mode, n - digit, cache);
2884 op1 = expand_powi_1 (mode, digit, cache);
2886 else
2888 target = gen_reg_rtx (mode);
2889 op0 = expand_powi_1 (mode, n >> 1, cache);
2890 op1 = op0;
2893 result = expand_mult (mode, op0, op1, target, 0);
2894 if (result != target)
2895 emit_move_insn (target, result);
2896 return target;
2899 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2900 floating point operand in mode MODE, and N is the exponent. This
2901 function needs to be kept in sync with powi_cost above. */
2903 static rtx
2904 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2906 unsigned HOST_WIDE_INT val;
2907 rtx cache[POWI_TABLE_SIZE];
2908 rtx result;
2910 if (n == 0)
2911 return CONST1_RTX (mode);
2913 val = (n < 0) ? -n : n;
2915 memset (cache, 0, sizeof (cache));
2916 cache[1] = x;
2918 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2920 /* If the original exponent was negative, reciprocate the result. */
2921 if (n < 0)
2922 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2923 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2925 return result;
2928 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2929 a normal call should be emitted rather than expanding the function
2930 in-line. EXP is the expression that is a call to the builtin
2931 function; if convenient, the result should be placed in TARGET. */
2933 static rtx
2934 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2936 tree arg0, arg1;
2937 tree fn, narg0;
2938 tree type = TREE_TYPE (exp);
2939 REAL_VALUE_TYPE cint, c, c2;
2940 HOST_WIDE_INT n;
2941 rtx op, op2;
2942 enum machine_mode mode = TYPE_MODE (type);
2944 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2945 return NULL_RTX;
2947 arg0 = CALL_EXPR_ARG (exp, 0);
2948 arg1 = CALL_EXPR_ARG (exp, 1);
2950 if (TREE_CODE (arg1) != REAL_CST
2951 || TREE_OVERFLOW (arg1))
2952 return expand_builtin_mathfn_2 (exp, target, subtarget);
2954 /* Handle constant exponents. */
2956 /* For integer valued exponents we can expand to an optimal multiplication
2957 sequence using expand_powi. */
2958 c = TREE_REAL_CST (arg1);
2959 n = real_to_integer (&c);
2960 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2961 if (real_identical (&c, &cint)
2962 && ((n >= -1 && n <= 2)
2963 || (flag_unsafe_math_optimizations
2964 && optimize_insn_for_speed_p ()
2965 && powi_cost (n) <= POWI_MAX_MULTS)))
2967 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2968 if (n != 1)
2970 op = force_reg (mode, op);
2971 op = expand_powi (op, mode, n);
2973 return op;
2976 narg0 = builtin_save_expr (arg0);
2978 /* If the exponent is not integer valued, check if it is half of an integer.
2979 In this case we can expand to sqrt (x) * x**(n/2). */
2980 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2981 if (fn != NULL_TREE)
2983 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2984 n = real_to_integer (&c2);
2985 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2986 if (real_identical (&c2, &cint)
2987 && ((flag_unsafe_math_optimizations
2988 && optimize_insn_for_speed_p ()
2989 && powi_cost (n/2) <= POWI_MAX_MULTS)
2990 || n == 1))
2992 tree call_expr = build_call_nofold (fn, 1, narg0);
2993 /* Use expand_expr in case the newly built call expression
2994 was folded to a non-call. */
2995 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
2996 if (n != 1)
2998 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
2999 op2 = force_reg (mode, op2);
3000 op2 = expand_powi (op2, mode, abs (n / 2));
3001 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3002 0, OPTAB_LIB_WIDEN);
3003 /* If the original exponent was negative, reciprocate the
3004 result. */
3005 if (n < 0)
3006 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3007 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3009 return op;
3013 /* Try if the exponent is a third of an integer. In this case
3014 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3015 different from pow (x, 1./3.) due to rounding and behavior
3016 with negative x we need to constrain this transformation to
3017 unsafe math and positive x or finite math. */
3018 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3019 if (fn != NULL_TREE
3020 && flag_unsafe_math_optimizations
3021 && (tree_expr_nonnegative_p (arg0)
3022 || !HONOR_NANS (mode)))
3024 REAL_VALUE_TYPE dconst3;
3025 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3026 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3027 real_round (&c2, mode, &c2);
3028 n = real_to_integer (&c2);
3029 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3030 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3031 real_convert (&c2, mode, &c2);
3032 if (real_identical (&c2, &c)
3033 && ((optimize_insn_for_speed_p ()
3034 && powi_cost (n/3) <= POWI_MAX_MULTS)
3035 || n == 1))
3037 tree call_expr = build_call_nofold (fn, 1,narg0);
3038 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3039 if (abs (n) % 3 == 2)
3040 op = expand_simple_binop (mode, MULT, op, op, op,
3041 0, OPTAB_LIB_WIDEN);
3042 if (n != 1)
3044 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3045 op2 = force_reg (mode, op2);
3046 op2 = expand_powi (op2, mode, abs (n / 3));
3047 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3048 0, OPTAB_LIB_WIDEN);
3049 /* If the original exponent was negative, reciprocate the
3050 result. */
3051 if (n < 0)
3052 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3053 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3055 return op;
3059 /* Fall back to optab expansion. */
3060 return expand_builtin_mathfn_2 (exp, target, subtarget);
3063 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3064 a normal call should be emitted rather than expanding the function
3065 in-line. EXP is the expression that is a call to the builtin
3066 function; if convenient, the result should be placed in TARGET. */
3068 static rtx
3069 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3071 tree arg0, arg1;
3072 rtx op0, op1;
3073 enum machine_mode mode;
3074 enum machine_mode mode2;
3076 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3077 return NULL_RTX;
3079 arg0 = CALL_EXPR_ARG (exp, 0);
3080 arg1 = CALL_EXPR_ARG (exp, 1);
3081 mode = TYPE_MODE (TREE_TYPE (exp));
3083 /* Handle constant power. */
3085 if (TREE_CODE (arg1) == INTEGER_CST
3086 && !TREE_OVERFLOW (arg1))
3088 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3090 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3091 Otherwise, check the number of multiplications required. */
3092 if ((TREE_INT_CST_HIGH (arg1) == 0
3093 || TREE_INT_CST_HIGH (arg1) == -1)
3094 && ((n >= -1 && n <= 2)
3095 || (optimize_insn_for_speed_p ()
3096 && powi_cost (n) <= POWI_MAX_MULTS)))
3098 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3099 op0 = force_reg (mode, op0);
3100 return expand_powi (op0, mode, n);
3104 /* Emit a libcall to libgcc. */
3106 /* Mode of the 2nd argument must match that of an int. */
3107 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3109 if (target == NULL_RTX)
3110 target = gen_reg_rtx (mode);
3112 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3113 if (GET_MODE (op0) != mode)
3114 op0 = convert_to_mode (mode, op0, 0);
3115 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3116 if (GET_MODE (op1) != mode2)
3117 op1 = convert_to_mode (mode2, op1, 0);
3119 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3120 target, LCT_CONST, mode, 2,
3121 op0, mode, op1, mode2);
3123 return target;
3126 /* Expand expression EXP which is a call to the strlen builtin. Return
3127 NULL_RTX if we failed the caller should emit a normal call, otherwise
3128 try to get the result in TARGET, if convenient. */
3130 static rtx
3131 expand_builtin_strlen (tree exp, rtx target,
3132 enum machine_mode target_mode)
3134 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3135 return NULL_RTX;
3136 else
3138 rtx pat;
3139 tree len;
3140 tree src = CALL_EXPR_ARG (exp, 0);
3141 rtx result, src_reg, char_rtx, before_strlen;
3142 enum machine_mode insn_mode = target_mode, char_mode;
3143 enum insn_code icode = CODE_FOR_nothing;
3144 int align;
3146 /* If the length can be computed at compile-time, return it. */
3147 len = c_strlen (src, 0);
3148 if (len)
3149 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3151 /* If the length can be computed at compile-time and is constant
3152 integer, but there are side-effects in src, evaluate
3153 src for side-effects, then return len.
3154 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3155 can be optimized into: i++; x = 3; */
3156 len = c_strlen (src, 1);
3157 if (len && TREE_CODE (len) == INTEGER_CST)
3159 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3160 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3163 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3165 /* If SRC is not a pointer type, don't do this operation inline. */
3166 if (align == 0)
3167 return NULL_RTX;
3169 /* Bail out if we can't compute strlen in the right mode. */
3170 while (insn_mode != VOIDmode)
3172 icode = optab_handler (strlen_optab, insn_mode)->insn_code;
3173 if (icode != CODE_FOR_nothing)
3174 break;
3176 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3178 if (insn_mode == VOIDmode)
3179 return NULL_RTX;
3181 /* Make a place to write the result of the instruction. */
3182 result = target;
3183 if (! (result != 0
3184 && REG_P (result)
3185 && GET_MODE (result) == insn_mode
3186 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3187 result = gen_reg_rtx (insn_mode);
3189 /* Make a place to hold the source address. We will not expand
3190 the actual source until we are sure that the expansion will
3191 not fail -- there are trees that cannot be expanded twice. */
3192 src_reg = gen_reg_rtx (Pmode);
3194 /* Mark the beginning of the strlen sequence so we can emit the
3195 source operand later. */
3196 before_strlen = get_last_insn ();
3198 char_rtx = const0_rtx;
3199 char_mode = insn_data[(int) icode].operand[2].mode;
3200 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3201 char_mode))
3202 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3204 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3205 char_rtx, GEN_INT (align));
3206 if (! pat)
3207 return NULL_RTX;
3208 emit_insn (pat);
3210 /* Now that we are assured of success, expand the source. */
3211 start_sequence ();
3212 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3213 if (pat != src_reg)
3214 emit_move_insn (src_reg, pat);
3215 pat = get_insns ();
3216 end_sequence ();
3218 if (before_strlen)
3219 emit_insn_after (pat, before_strlen);
3220 else
3221 emit_insn_before (pat, get_insns ());
3223 /* Return the value in the proper mode for this function. */
3224 if (GET_MODE (result) == target_mode)
3225 target = result;
3226 else if (target != 0)
3227 convert_move (target, result, 0);
3228 else
3229 target = convert_to_mode (target_mode, result, 0);
3231 return target;
3235 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3236 bytes from constant string DATA + OFFSET and return it as target
3237 constant. */
3239 static rtx
3240 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3241 enum machine_mode mode)
3243 const char *str = (const char *) data;
3245 gcc_assert (offset >= 0
3246 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3247 <= strlen (str) + 1));
3249 return c_readstr (str + offset, mode);
3252 /* Expand a call EXP to the memcpy builtin.
3253 Return NULL_RTX if we failed, the caller should emit a normal call,
3254 otherwise try to get the result in TARGET, if convenient (and in
3255 mode MODE if that's convenient). */
3257 static rtx
3258 expand_builtin_memcpy (tree exp, rtx target)
3260 if (!validate_arglist (exp,
3261 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3262 return NULL_RTX;
3263 else
3265 tree dest = CALL_EXPR_ARG (exp, 0);
3266 tree src = CALL_EXPR_ARG (exp, 1);
3267 tree len = CALL_EXPR_ARG (exp, 2);
3268 const char *src_str;
3269 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3270 unsigned int dest_align
3271 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3272 rtx dest_mem, src_mem, dest_addr, len_rtx;
3273 HOST_WIDE_INT expected_size = -1;
3274 unsigned int expected_align = 0;
3275 tree_ann_common_t ann;
3277 /* If DEST is not a pointer type, call the normal function. */
3278 if (dest_align == 0)
3279 return NULL_RTX;
3281 /* If either SRC is not a pointer type, don't do this
3282 operation in-line. */
3283 if (src_align == 0)
3284 return NULL_RTX;
3286 ann = tree_common_ann (exp);
3287 if (ann)
3288 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3290 if (expected_align < dest_align)
3291 expected_align = dest_align;
3292 dest_mem = get_memory_rtx (dest, len);
3293 set_mem_align (dest_mem, dest_align);
3294 len_rtx = expand_normal (len);
3295 src_str = c_getstr (src);
3297 /* If SRC is a string constant and block move would be done
3298 by pieces, we can avoid loading the string from memory
3299 and only stored the computed constants. */
3300 if (src_str
3301 && CONST_INT_P (len_rtx)
3302 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3303 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3304 CONST_CAST (char *, src_str),
3305 dest_align, false))
3307 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3308 builtin_memcpy_read_str,
3309 CONST_CAST (char *, src_str),
3310 dest_align, false, 0);
3311 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3312 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3313 return dest_mem;
3316 src_mem = get_memory_rtx (src, len);
3317 set_mem_align (src_mem, src_align);
3319 /* Copy word part most expediently. */
3320 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3321 CALL_EXPR_TAILCALL (exp)
3322 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3323 expected_align, expected_size);
3325 if (dest_addr == 0)
3327 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3328 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3330 return dest_addr;
3334 /* Expand a call EXP to the mempcpy builtin.
3335 Return NULL_RTX if we failed; the caller should emit a normal call,
3336 otherwise try to get the result in TARGET, if convenient (and in
3337 mode MODE if that's convenient). If ENDP is 0 return the
3338 destination pointer, if ENDP is 1 return the end pointer ala
3339 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3340 stpcpy. */
3342 static rtx
3343 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3345 if (!validate_arglist (exp,
3346 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3347 return NULL_RTX;
3348 else
3350 tree dest = CALL_EXPR_ARG (exp, 0);
3351 tree src = CALL_EXPR_ARG (exp, 1);
3352 tree len = CALL_EXPR_ARG (exp, 2);
3353 return expand_builtin_mempcpy_args (dest, src, len,
3354 target, mode, /*endp=*/ 1);
3358 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3359 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3360 so that this can also be called without constructing an actual CALL_EXPR.
3361 The other arguments and return value are the same as for
3362 expand_builtin_mempcpy. */
3364 static rtx
3365 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3366 rtx target, enum machine_mode mode, int endp)
3368 /* If return value is ignored, transform mempcpy into memcpy. */
3369 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3371 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3372 tree result = build_call_nofold (fn, 3, dest, src, len);
3373 return expand_expr (result, target, mode, EXPAND_NORMAL);
3375 else
3377 const char *src_str;
3378 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3379 unsigned int dest_align
3380 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3381 rtx dest_mem, src_mem, len_rtx;
3383 /* If either SRC or DEST is not a pointer type, don't do this
3384 operation in-line. */
3385 if (dest_align == 0 || src_align == 0)
3386 return NULL_RTX;
3388 /* If LEN is not constant, call the normal function. */
3389 if (! host_integerp (len, 1))
3390 return NULL_RTX;
3392 len_rtx = expand_normal (len);
3393 src_str = c_getstr (src);
3395 /* If SRC is a string constant and block move would be done
3396 by pieces, we can avoid loading the string from memory
3397 and only stored the computed constants. */
3398 if (src_str
3399 && CONST_INT_P (len_rtx)
3400 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3401 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3402 CONST_CAST (char *, src_str),
3403 dest_align, false))
3405 dest_mem = get_memory_rtx (dest, len);
3406 set_mem_align (dest_mem, dest_align);
3407 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3408 builtin_memcpy_read_str,
3409 CONST_CAST (char *, src_str),
3410 dest_align, false, endp);
3411 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3412 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3413 return dest_mem;
3416 if (CONST_INT_P (len_rtx)
3417 && can_move_by_pieces (INTVAL (len_rtx),
3418 MIN (dest_align, src_align)))
3420 dest_mem = get_memory_rtx (dest, len);
3421 set_mem_align (dest_mem, dest_align);
3422 src_mem = get_memory_rtx (src, len);
3423 set_mem_align (src_mem, src_align);
3424 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3425 MIN (dest_align, src_align), endp);
3426 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3427 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3428 return dest_mem;
3431 return NULL_RTX;
3435 #ifndef HAVE_movstr
3436 # define HAVE_movstr 0
3437 # define CODE_FOR_movstr CODE_FOR_nothing
3438 #endif
3440 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3441 we failed, the caller should emit a normal call, otherwise try to
3442 get the result in TARGET, if convenient. If ENDP is 0 return the
3443 destination pointer, if ENDP is 1 return the end pointer ala
3444 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3445 stpcpy. */
3447 static rtx
3448 expand_movstr (tree dest, tree src, rtx target, int endp)
3450 rtx end;
3451 rtx dest_mem;
3452 rtx src_mem;
3453 rtx insn;
3454 const struct insn_data * data;
3456 if (!HAVE_movstr)
3457 return NULL_RTX;
3459 dest_mem = get_memory_rtx (dest, NULL);
3460 src_mem = get_memory_rtx (src, NULL);
3461 if (!endp)
3463 target = force_reg (Pmode, XEXP (dest_mem, 0));
3464 dest_mem = replace_equiv_address (dest_mem, target);
3465 end = gen_reg_rtx (Pmode);
3467 else
3469 if (target == 0 || target == const0_rtx)
3471 end = gen_reg_rtx (Pmode);
3472 if (target == 0)
3473 target = end;
3475 else
3476 end = target;
3479 data = insn_data + CODE_FOR_movstr;
3481 if (data->operand[0].mode != VOIDmode)
3482 end = gen_lowpart (data->operand[0].mode, end);
3484 insn = data->genfun (end, dest_mem, src_mem);
3486 gcc_assert (insn);
3488 emit_insn (insn);
3490 /* movstr is supposed to set end to the address of the NUL
3491 terminator. If the caller requested a mempcpy-like return value,
3492 adjust it. */
3493 if (endp == 1 && target != const0_rtx)
3495 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3496 emit_move_insn (target, force_operand (tem, NULL_RTX));
3499 return target;
3502 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3503 NULL_RTX if we failed the caller should emit a normal call, otherwise
3504 try to get the result in TARGET, if convenient (and in mode MODE if that's
3505 convenient). */
3507 static rtx
3508 expand_builtin_strcpy (tree exp, rtx target)
3510 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3512 tree dest = CALL_EXPR_ARG (exp, 0);
3513 tree src = CALL_EXPR_ARG (exp, 1);
3514 return expand_builtin_strcpy_args (dest, src, target);
3516 return NULL_RTX;
3519 /* Helper function to do the actual work for expand_builtin_strcpy. The
3520 arguments to the builtin_strcpy call DEST and SRC are broken out
3521 so that this can also be called without constructing an actual CALL_EXPR.
3522 The other arguments and return value are the same as for
3523 expand_builtin_strcpy. */
3525 static rtx
3526 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3528 return expand_movstr (dest, src, target, /*endp=*/0);
3531 /* Expand a call EXP to the stpcpy builtin.
3532 Return NULL_RTX if we failed the caller should emit a normal call,
3533 otherwise try to get the result in TARGET, if convenient (and in
3534 mode MODE if that's convenient). */
3536 static rtx
3537 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3539 tree dst, src;
3540 location_t loc = EXPR_LOCATION (exp);
3542 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3543 return NULL_RTX;
3545 dst = CALL_EXPR_ARG (exp, 0);
3546 src = CALL_EXPR_ARG (exp, 1);
3548 /* If return value is ignored, transform stpcpy into strcpy. */
3549 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3551 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3552 tree result = build_call_nofold (fn, 2, dst, src);
3553 return expand_expr (result, target, mode, EXPAND_NORMAL);
3555 else
3557 tree len, lenp1;
3558 rtx ret;
3560 /* Ensure we get an actual string whose length can be evaluated at
3561 compile-time, not an expression containing a string. This is
3562 because the latter will potentially produce pessimized code
3563 when used to produce the return value. */
3564 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3565 return expand_movstr (dst, src, target, /*endp=*/2);
3567 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3568 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3569 target, mode, /*endp=*/2);
3571 if (ret)
3572 return ret;
3574 if (TREE_CODE (len) == INTEGER_CST)
3576 rtx len_rtx = expand_normal (len);
3578 if (CONST_INT_P (len_rtx))
3580 ret = expand_builtin_strcpy_args (dst, src, target);
3582 if (ret)
3584 if (! target)
3586 if (mode != VOIDmode)
3587 target = gen_reg_rtx (mode);
3588 else
3589 target = gen_reg_rtx (GET_MODE (ret));
3591 if (GET_MODE (target) != GET_MODE (ret))
3592 ret = gen_lowpart (GET_MODE (target), ret);
3594 ret = plus_constant (ret, INTVAL (len_rtx));
3595 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3596 gcc_assert (ret);
3598 return target;
3603 return expand_movstr (dst, src, target, /*endp=*/2);
3607 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3608 bytes from constant string DATA + OFFSET and return it as target
3609 constant. */
3612 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3613 enum machine_mode mode)
3615 const char *str = (const char *) data;
3617 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3618 return const0_rtx;
3620 return c_readstr (str + offset, mode);
3623 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3624 NULL_RTX if we failed the caller should emit a normal call. */
3626 static rtx
3627 expand_builtin_strncpy (tree exp, rtx target)
3629 location_t loc = EXPR_LOCATION (exp);
3631 if (validate_arglist (exp,
3632 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3634 tree dest = CALL_EXPR_ARG (exp, 0);
3635 tree src = CALL_EXPR_ARG (exp, 1);
3636 tree len = CALL_EXPR_ARG (exp, 2);
3637 tree slen = c_strlen (src, 1);
3639 /* We must be passed a constant len and src parameter. */
3640 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3641 return NULL_RTX;
3643 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3645 /* We're required to pad with trailing zeros if the requested
3646 len is greater than strlen(s2)+1. In that case try to
3647 use store_by_pieces, if it fails, punt. */
3648 if (tree_int_cst_lt (slen, len))
3650 unsigned int dest_align
3651 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3652 const char *p = c_getstr (src);
3653 rtx dest_mem;
3655 if (!p || dest_align == 0 || !host_integerp (len, 1)
3656 || !can_store_by_pieces (tree_low_cst (len, 1),
3657 builtin_strncpy_read_str,
3658 CONST_CAST (char *, p),
3659 dest_align, false))
3660 return NULL_RTX;
3662 dest_mem = get_memory_rtx (dest, len);
3663 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3664 builtin_strncpy_read_str,
3665 CONST_CAST (char *, p), dest_align, false, 0);
3666 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3667 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3668 return dest_mem;
3671 return NULL_RTX;
3674 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3675 bytes from constant string DATA + OFFSET and return it as target
3676 constant. */
3679 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3680 enum machine_mode mode)
3682 const char *c = (const char *) data;
3683 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3685 memset (p, *c, GET_MODE_SIZE (mode));
3687 return c_readstr (p, mode);
3690 /* Callback routine for store_by_pieces. Return the RTL of a register
3691 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3692 char value given in the RTL register data. For example, if mode is
3693 4 bytes wide, return the RTL for 0x01010101*data. */
3695 static rtx
3696 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3697 enum machine_mode mode)
3699 rtx target, coeff;
3700 size_t size;
3701 char *p;
3703 size = GET_MODE_SIZE (mode);
3704 if (size == 1)
3705 return (rtx) data;
3707 p = XALLOCAVEC (char, size);
3708 memset (p, 1, size);
3709 coeff = c_readstr (p, mode);
3711 target = convert_to_mode (mode, (rtx) data, 1);
3712 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3713 return force_reg (mode, target);
3716 /* Expand expression EXP, which is a call to the memset builtin. Return
3717 NULL_RTX if we failed the caller should emit a normal call, otherwise
3718 try to get the result in TARGET, if convenient (and in mode MODE if that's
3719 convenient). */
3721 static rtx
3722 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3724 if (!validate_arglist (exp,
3725 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3726 return NULL_RTX;
3727 else
3729 tree dest = CALL_EXPR_ARG (exp, 0);
3730 tree val = CALL_EXPR_ARG (exp, 1);
3731 tree len = CALL_EXPR_ARG (exp, 2);
3732 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3736 /* Helper function to do the actual work for expand_builtin_memset. The
3737 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3738 so that this can also be called without constructing an actual CALL_EXPR.
3739 The other arguments and return value are the same as for
3740 expand_builtin_memset. */
3742 static rtx
3743 expand_builtin_memset_args (tree dest, tree val, tree len,
3744 rtx target, enum machine_mode mode, tree orig_exp)
3746 tree fndecl, fn;
3747 enum built_in_function fcode;
3748 char c;
3749 unsigned int dest_align;
3750 rtx dest_mem, dest_addr, len_rtx;
3751 HOST_WIDE_INT expected_size = -1;
3752 unsigned int expected_align = 0;
3753 tree_ann_common_t ann;
3755 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3757 /* If DEST is not a pointer type, don't do this operation in-line. */
3758 if (dest_align == 0)
3759 return NULL_RTX;
3761 ann = tree_common_ann (orig_exp);
3762 if (ann)
3763 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3765 if (expected_align < dest_align)
3766 expected_align = dest_align;
3768 /* If the LEN parameter is zero, return DEST. */
3769 if (integer_zerop (len))
3771 /* Evaluate and ignore VAL in case it has side-effects. */
3772 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3773 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3776 /* Stabilize the arguments in case we fail. */
3777 dest = builtin_save_expr (dest);
3778 val = builtin_save_expr (val);
3779 len = builtin_save_expr (len);
3781 len_rtx = expand_normal (len);
3782 dest_mem = get_memory_rtx (dest, len);
3784 if (TREE_CODE (val) != INTEGER_CST)
3786 rtx val_rtx;
3788 val_rtx = expand_normal (val);
3789 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3790 val_rtx, 0);
3792 /* Assume that we can memset by pieces if we can store
3793 * the coefficients by pieces (in the required modes).
3794 * We can't pass builtin_memset_gen_str as that emits RTL. */
3795 c = 1;
3796 if (host_integerp (len, 1)
3797 && can_store_by_pieces (tree_low_cst (len, 1),
3798 builtin_memset_read_str, &c, dest_align,
3799 true))
3801 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3802 val_rtx);
3803 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3804 builtin_memset_gen_str, val_rtx, dest_align,
3805 true, 0);
3807 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3808 dest_align, expected_align,
3809 expected_size))
3810 goto do_libcall;
3812 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3813 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3814 return dest_mem;
3817 if (target_char_cast (val, &c))
3818 goto do_libcall;
3820 if (c)
3822 if (host_integerp (len, 1)
3823 && can_store_by_pieces (tree_low_cst (len, 1),
3824 builtin_memset_read_str, &c, dest_align,
3825 true))
3826 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3827 builtin_memset_read_str, &c, dest_align, true, 0);
3828 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3829 dest_align, expected_align,
3830 expected_size))
3831 goto do_libcall;
3833 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3834 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3835 return dest_mem;
3838 set_mem_align (dest_mem, dest_align);
3839 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3840 CALL_EXPR_TAILCALL (orig_exp)
3841 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3842 expected_align, expected_size);
3844 if (dest_addr == 0)
3846 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3847 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3850 return dest_addr;
3852 do_libcall:
3853 fndecl = get_callee_fndecl (orig_exp);
3854 fcode = DECL_FUNCTION_CODE (fndecl);
3855 if (fcode == BUILT_IN_MEMSET)
3856 fn = build_call_nofold (fndecl, 3, dest, val, len);
3857 else if (fcode == BUILT_IN_BZERO)
3858 fn = build_call_nofold (fndecl, 2, dest, len);
3859 else
3860 gcc_unreachable ();
3861 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3862 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3863 return expand_call (fn, target, target == const0_rtx);
3866 /* Expand expression EXP, which is a call to the bzero builtin. Return
3867 NULL_RTX if we failed the caller should emit a normal call. */
3869 static rtx
3870 expand_builtin_bzero (tree exp)
3872 tree dest, size;
3873 location_t loc = EXPR_LOCATION (exp);
3875 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3876 return NULL_RTX;
3878 dest = CALL_EXPR_ARG (exp, 0);
3879 size = CALL_EXPR_ARG (exp, 1);
3881 /* New argument list transforming bzero(ptr x, int y) to
3882 memset(ptr x, int 0, size_t y). This is done this way
3883 so that if it isn't expanded inline, we fallback to
3884 calling bzero instead of memset. */
3886 return expand_builtin_memset_args (dest, integer_zero_node,
3887 fold_convert_loc (loc, sizetype, size),
3888 const0_rtx, VOIDmode, exp);
3891 /* Expand expression EXP, which is a call to the memcmp built-in function.
3892 Return NULL_RTX if we failed and the
3893 caller should emit a normal call, otherwise try to get the result in
3894 TARGET, if convenient (and in mode MODE, if that's convenient). */
3896 static rtx
3897 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3898 ATTRIBUTE_UNUSED enum machine_mode mode)
3900 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3902 if (!validate_arglist (exp,
3903 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3904 return NULL_RTX;
3906 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3908 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3909 rtx result;
3910 rtx insn;
3911 tree arg1 = CALL_EXPR_ARG (exp, 0);
3912 tree arg2 = CALL_EXPR_ARG (exp, 1);
3913 tree len = CALL_EXPR_ARG (exp, 2);
3915 int arg1_align
3916 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3917 int arg2_align
3918 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3919 enum machine_mode insn_mode;
3921 #ifdef HAVE_cmpmemsi
3922 if (HAVE_cmpmemsi)
3923 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3924 else
3925 #endif
3926 #ifdef HAVE_cmpstrnsi
3927 if (HAVE_cmpstrnsi)
3928 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3929 else
3930 #endif
3931 return NULL_RTX;
3933 /* If we don't have POINTER_TYPE, call the function. */
3934 if (arg1_align == 0 || arg2_align == 0)
3935 return NULL_RTX;
3937 /* Make a place to write the result of the instruction. */
3938 result = target;
3939 if (! (result != 0
3940 && REG_P (result) && GET_MODE (result) == insn_mode
3941 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3942 result = gen_reg_rtx (insn_mode);
3944 arg1_rtx = get_memory_rtx (arg1, len);
3945 arg2_rtx = get_memory_rtx (arg2, len);
3946 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3948 /* Set MEM_SIZE as appropriate. */
3949 if (CONST_INT_P (arg3_rtx))
3951 set_mem_size (arg1_rtx, arg3_rtx);
3952 set_mem_size (arg2_rtx, arg3_rtx);
3955 #ifdef HAVE_cmpmemsi
3956 if (HAVE_cmpmemsi)
3957 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3958 GEN_INT (MIN (arg1_align, arg2_align)));
3959 else
3960 #endif
3961 #ifdef HAVE_cmpstrnsi
3962 if (HAVE_cmpstrnsi)
3963 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3964 GEN_INT (MIN (arg1_align, arg2_align)));
3965 else
3966 #endif
3967 gcc_unreachable ();
3969 if (insn)
3970 emit_insn (insn);
3971 else
3972 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3973 TYPE_MODE (integer_type_node), 3,
3974 XEXP (arg1_rtx, 0), Pmode,
3975 XEXP (arg2_rtx, 0), Pmode,
3976 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3977 TYPE_UNSIGNED (sizetype)),
3978 TYPE_MODE (sizetype));
3980 /* Return the value in the proper mode for this function. */
3981 mode = TYPE_MODE (TREE_TYPE (exp));
3982 if (GET_MODE (result) == mode)
3983 return result;
3984 else if (target != 0)
3986 convert_move (target, result, 0);
3987 return target;
3989 else
3990 return convert_to_mode (mode, result, 0);
3992 #endif
3994 return NULL_RTX;
3997 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3998 if we failed the caller should emit a normal call, otherwise try to get
3999 the result in TARGET, if convenient. */
4001 static rtx
4002 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4004 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4005 return NULL_RTX;
4007 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4008 if (cmpstr_optab[SImode] != CODE_FOR_nothing
4009 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
4011 rtx arg1_rtx, arg2_rtx;
4012 rtx result, insn = NULL_RTX;
4013 tree fndecl, fn;
4014 tree arg1 = CALL_EXPR_ARG (exp, 0);
4015 tree arg2 = CALL_EXPR_ARG (exp, 1);
4017 int arg1_align
4018 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4019 int arg2_align
4020 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4022 /* If we don't have POINTER_TYPE, call the function. */
4023 if (arg1_align == 0 || arg2_align == 0)
4024 return NULL_RTX;
4026 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4027 arg1 = builtin_save_expr (arg1);
4028 arg2 = builtin_save_expr (arg2);
4030 arg1_rtx = get_memory_rtx (arg1, NULL);
4031 arg2_rtx = get_memory_rtx (arg2, NULL);
4033 #ifdef HAVE_cmpstrsi
4034 /* Try to call cmpstrsi. */
4035 if (HAVE_cmpstrsi)
4037 enum machine_mode insn_mode
4038 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4040 /* Make a place to write the result of the instruction. */
4041 result = target;
4042 if (! (result != 0
4043 && REG_P (result) && GET_MODE (result) == insn_mode
4044 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4045 result = gen_reg_rtx (insn_mode);
4047 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4048 GEN_INT (MIN (arg1_align, arg2_align)));
4050 #endif
4051 #ifdef HAVE_cmpstrnsi
4052 /* Try to determine at least one length and call cmpstrnsi. */
4053 if (!insn && HAVE_cmpstrnsi)
4055 tree len;
4056 rtx arg3_rtx;
4058 enum machine_mode insn_mode
4059 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4060 tree len1 = c_strlen (arg1, 1);
4061 tree len2 = c_strlen (arg2, 1);
4063 if (len1)
4064 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4065 if (len2)
4066 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4068 /* If we don't have a constant length for the first, use the length
4069 of the second, if we know it. We don't require a constant for
4070 this case; some cost analysis could be done if both are available
4071 but neither is constant. For now, assume they're equally cheap,
4072 unless one has side effects. If both strings have constant lengths,
4073 use the smaller. */
4075 if (!len1)
4076 len = len2;
4077 else if (!len2)
4078 len = len1;
4079 else if (TREE_SIDE_EFFECTS (len1))
4080 len = len2;
4081 else if (TREE_SIDE_EFFECTS (len2))
4082 len = len1;
4083 else if (TREE_CODE (len1) != INTEGER_CST)
4084 len = len2;
4085 else if (TREE_CODE (len2) != INTEGER_CST)
4086 len = len1;
4087 else if (tree_int_cst_lt (len1, len2))
4088 len = len1;
4089 else
4090 len = len2;
4092 /* If both arguments have side effects, we cannot optimize. */
4093 if (!len || TREE_SIDE_EFFECTS (len))
4094 goto do_libcall;
4096 arg3_rtx = expand_normal (len);
4098 /* Make a place to write the result of the instruction. */
4099 result = target;
4100 if (! (result != 0
4101 && REG_P (result) && GET_MODE (result) == insn_mode
4102 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4103 result = gen_reg_rtx (insn_mode);
4105 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4106 GEN_INT (MIN (arg1_align, arg2_align)));
4108 #endif
4110 if (insn)
4112 enum machine_mode mode;
4113 emit_insn (insn);
4115 /* Return the value in the proper mode for this function. */
4116 mode = TYPE_MODE (TREE_TYPE (exp));
4117 if (GET_MODE (result) == mode)
4118 return result;
4119 if (target == 0)
4120 return convert_to_mode (mode, result, 0);
4121 convert_move (target, result, 0);
4122 return target;
4125 /* Expand the library call ourselves using a stabilized argument
4126 list to avoid re-evaluating the function's arguments twice. */
4127 #ifdef HAVE_cmpstrnsi
4128 do_libcall:
4129 #endif
4130 fndecl = get_callee_fndecl (exp);
4131 fn = build_call_nofold (fndecl, 2, arg1, arg2);
4132 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4133 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4134 return expand_call (fn, target, target == const0_rtx);
4136 #endif
4137 return NULL_RTX;
4140 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4141 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4142 the result in TARGET, if convenient. */
4144 static rtx
4145 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4146 ATTRIBUTE_UNUSED enum machine_mode mode)
4148 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4150 if (!validate_arglist (exp,
4151 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4152 return NULL_RTX;
4154 /* If c_strlen can determine an expression for one of the string
4155 lengths, and it doesn't have side effects, then emit cmpstrnsi
4156 using length MIN(strlen(string)+1, arg3). */
4157 #ifdef HAVE_cmpstrnsi
4158 if (HAVE_cmpstrnsi)
4160 tree len, len1, len2;
4161 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4162 rtx result, insn;
4163 tree fndecl, fn;
4164 tree arg1 = CALL_EXPR_ARG (exp, 0);
4165 tree arg2 = CALL_EXPR_ARG (exp, 1);
4166 tree arg3 = CALL_EXPR_ARG (exp, 2);
4168 int arg1_align
4169 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4170 int arg2_align
4171 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4172 enum machine_mode insn_mode
4173 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4175 len1 = c_strlen (arg1, 1);
4176 len2 = c_strlen (arg2, 1);
4178 if (len1)
4179 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4180 if (len2)
4181 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4183 /* If we don't have a constant length for the first, use the length
4184 of the second, if we know it. We don't require a constant for
4185 this case; some cost analysis could be done if both are available
4186 but neither is constant. For now, assume they're equally cheap,
4187 unless one has side effects. If both strings have constant lengths,
4188 use the smaller. */
4190 if (!len1)
4191 len = len2;
4192 else if (!len2)
4193 len = len1;
4194 else if (TREE_SIDE_EFFECTS (len1))
4195 len = len2;
4196 else if (TREE_SIDE_EFFECTS (len2))
4197 len = len1;
4198 else if (TREE_CODE (len1) != INTEGER_CST)
4199 len = len2;
4200 else if (TREE_CODE (len2) != INTEGER_CST)
4201 len = len1;
4202 else if (tree_int_cst_lt (len1, len2))
4203 len = len1;
4204 else
4205 len = len2;
4207 /* If both arguments have side effects, we cannot optimize. */
4208 if (!len || TREE_SIDE_EFFECTS (len))
4209 return NULL_RTX;
4211 /* The actual new length parameter is MIN(len,arg3). */
4212 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4213 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4215 /* If we don't have POINTER_TYPE, call the function. */
4216 if (arg1_align == 0 || arg2_align == 0)
4217 return NULL_RTX;
4219 /* Make a place to write the result of the instruction. */
4220 result = target;
4221 if (! (result != 0
4222 && REG_P (result) && GET_MODE (result) == insn_mode
4223 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4224 result = gen_reg_rtx (insn_mode);
4226 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4227 arg1 = builtin_save_expr (arg1);
4228 arg2 = builtin_save_expr (arg2);
4229 len = builtin_save_expr (len);
4231 arg1_rtx = get_memory_rtx (arg1, len);
4232 arg2_rtx = get_memory_rtx (arg2, len);
4233 arg3_rtx = expand_normal (len);
4234 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4235 GEN_INT (MIN (arg1_align, arg2_align)));
4236 if (insn)
4238 emit_insn (insn);
4240 /* Return the value in the proper mode for this function. */
4241 mode = TYPE_MODE (TREE_TYPE (exp));
4242 if (GET_MODE (result) == mode)
4243 return result;
4244 if (target == 0)
4245 return convert_to_mode (mode, result, 0);
4246 convert_move (target, result, 0);
4247 return target;
4250 /* Expand the library call ourselves using a stabilized argument
4251 list to avoid re-evaluating the function's arguments twice. */
4252 fndecl = get_callee_fndecl (exp);
4253 fn = build_call_nofold (fndecl, 3, arg1, arg2, len);
4254 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4255 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4256 return expand_call (fn, target, target == const0_rtx);
4258 #endif
4259 return NULL_RTX;
4262 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4263 if that's convenient. */
4266 expand_builtin_saveregs (void)
4268 rtx val, seq;
4270 /* Don't do __builtin_saveregs more than once in a function.
4271 Save the result of the first call and reuse it. */
4272 if (saveregs_value != 0)
4273 return saveregs_value;
4275 /* When this function is called, it means that registers must be
4276 saved on entry to this function. So we migrate the call to the
4277 first insn of this function. */
4279 start_sequence ();
4281 /* Do whatever the machine needs done in this case. */
4282 val = targetm.calls.expand_builtin_saveregs ();
4284 seq = get_insns ();
4285 end_sequence ();
4287 saveregs_value = val;
4289 /* Put the insns after the NOTE that starts the function. If this
4290 is inside a start_sequence, make the outer-level insn chain current, so
4291 the code is placed at the start of the function. */
4292 push_topmost_sequence ();
4293 emit_insn_after (seq, entry_of_function ());
4294 pop_topmost_sequence ();
4296 return val;
4299 /* __builtin_args_info (N) returns word N of the arg space info
4300 for the current function. The number and meanings of words
4301 is controlled by the definition of CUMULATIVE_ARGS. */
4303 static rtx
4304 expand_builtin_args_info (tree exp)
4306 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4307 int *word_ptr = (int *) &crtl->args.info;
4309 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4311 if (call_expr_nargs (exp) != 0)
4313 if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0))
4314 error ("argument of %<__builtin_args_info%> must be constant");
4315 else
4317 HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0);
4319 if (wordnum < 0 || wordnum >= nwords)
4320 error ("argument of %<__builtin_args_info%> out of range");
4321 else
4322 return GEN_INT (word_ptr[wordnum]);
4325 else
4326 error ("missing argument in %<__builtin_args_info%>");
4328 return const0_rtx;
4331 /* Expand a call to __builtin_next_arg. */
4333 static rtx
4334 expand_builtin_next_arg (void)
4336 /* Checking arguments is already done in fold_builtin_next_arg
4337 that must be called before this function. */
4338 return expand_binop (ptr_mode, add_optab,
4339 crtl->args.internal_arg_pointer,
4340 crtl->args.arg_offset_rtx,
4341 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4344 /* Make it easier for the backends by protecting the valist argument
4345 from multiple evaluations. */
4347 static tree
4348 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4350 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4352 gcc_assert (vatype != NULL_TREE);
4354 if (TREE_CODE (vatype) == ARRAY_TYPE)
4356 if (TREE_SIDE_EFFECTS (valist))
4357 valist = save_expr (valist);
4359 /* For this case, the backends will be expecting a pointer to
4360 vatype, but it's possible we've actually been given an array
4361 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4362 So fix it. */
4363 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4365 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4366 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4369 else
4371 tree pt;
4373 if (! needs_lvalue)
4375 if (! TREE_SIDE_EFFECTS (valist))
4376 return valist;
4378 pt = build_pointer_type (vatype);
4379 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4380 TREE_SIDE_EFFECTS (valist) = 1;
4383 if (TREE_SIDE_EFFECTS (valist))
4384 valist = save_expr (valist);
4385 valist = build_fold_indirect_ref_loc (loc, valist);
4388 return valist;
4391 /* The "standard" definition of va_list is void*. */
4393 tree
4394 std_build_builtin_va_list (void)
4396 return ptr_type_node;
4399 /* The "standard" abi va_list is va_list_type_node. */
4401 tree
4402 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4404 return va_list_type_node;
4407 /* The "standard" type of va_list is va_list_type_node. */
4409 tree
4410 std_canonical_va_list_type (tree type)
4412 tree wtype, htype;
4414 if (INDIRECT_REF_P (type))
4415 type = TREE_TYPE (type);
4416 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4417 type = TREE_TYPE (type);
4418 wtype = va_list_type_node;
4419 htype = type;
4420 /* Treat structure va_list types. */
4421 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4422 htype = TREE_TYPE (htype);
4423 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4425 /* If va_list is an array type, the argument may have decayed
4426 to a pointer type, e.g. by being passed to another function.
4427 In that case, unwrap both types so that we can compare the
4428 underlying records. */
4429 if (TREE_CODE (htype) == ARRAY_TYPE
4430 || POINTER_TYPE_P (htype))
4432 wtype = TREE_TYPE (wtype);
4433 htype = TREE_TYPE (htype);
4436 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4437 return va_list_type_node;
4439 return NULL_TREE;
4442 /* The "standard" implementation of va_start: just assign `nextarg' to
4443 the variable. */
4445 void
4446 std_expand_builtin_va_start (tree valist, rtx nextarg)
4448 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4449 convert_move (va_r, nextarg, 0);
4452 /* Expand EXP, a call to __builtin_va_start. */
4454 static rtx
4455 expand_builtin_va_start (tree exp)
4457 rtx nextarg;
4458 tree valist;
4459 location_t loc = EXPR_LOCATION (exp);
4461 if (call_expr_nargs (exp) < 2)
4463 error_at (loc, "too few arguments to function %<va_start%>");
4464 return const0_rtx;
4467 if (fold_builtin_next_arg (exp, true))
4468 return const0_rtx;
4470 nextarg = expand_builtin_next_arg ();
4471 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4473 if (targetm.expand_builtin_va_start)
4474 targetm.expand_builtin_va_start (valist, nextarg);
4475 else
4476 std_expand_builtin_va_start (valist, nextarg);
4478 return const0_rtx;
4481 /* The "standard" implementation of va_arg: read the value from the
4482 current (padded) address and increment by the (padded) size. */
4484 tree
4485 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4486 gimple_seq *post_p)
4488 tree addr, t, type_size, rounded_size, valist_tmp;
4489 unsigned HOST_WIDE_INT align, boundary;
4490 bool indirect;
4492 #ifdef ARGS_GROW_DOWNWARD
4493 /* All of the alignment and movement below is for args-grow-up machines.
4494 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4495 implement their own specialized gimplify_va_arg_expr routines. */
4496 gcc_unreachable ();
4497 #endif
4499 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4500 if (indirect)
4501 type = build_pointer_type (type);
4503 align = PARM_BOUNDARY / BITS_PER_UNIT;
4504 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4506 /* When we align parameter on stack for caller, if the parameter
4507 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4508 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4509 here with caller. */
4510 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4511 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4513 boundary /= BITS_PER_UNIT;
4515 /* Hoist the valist value into a temporary for the moment. */
4516 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4518 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4519 requires greater alignment, we must perform dynamic alignment. */
4520 if (boundary > align
4521 && !integer_zerop (TYPE_SIZE (type)))
4523 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4524 fold_build2 (POINTER_PLUS_EXPR,
4525 TREE_TYPE (valist),
4526 valist_tmp, size_int (boundary - 1)));
4527 gimplify_and_add (t, pre_p);
4529 t = fold_convert (sizetype, valist_tmp);
4530 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4531 fold_convert (TREE_TYPE (valist),
4532 fold_build2 (BIT_AND_EXPR, sizetype, t,
4533 size_int (-boundary))));
4534 gimplify_and_add (t, pre_p);
4536 else
4537 boundary = align;
4539 /* If the actual alignment is less than the alignment of the type,
4540 adjust the type accordingly so that we don't assume strict alignment
4541 when dereferencing the pointer. */
4542 boundary *= BITS_PER_UNIT;
4543 if (boundary < TYPE_ALIGN (type))
4545 type = build_variant_type_copy (type);
4546 TYPE_ALIGN (type) = boundary;
4549 /* Compute the rounded size of the type. */
4550 type_size = size_in_bytes (type);
4551 rounded_size = round_up (type_size, align);
4553 /* Reduce rounded_size so it's sharable with the postqueue. */
4554 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4556 /* Get AP. */
4557 addr = valist_tmp;
4558 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4560 /* Small args are padded downward. */
4561 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4562 rounded_size, size_int (align));
4563 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4564 size_binop (MINUS_EXPR, rounded_size, type_size));
4565 addr = fold_build2 (POINTER_PLUS_EXPR,
4566 TREE_TYPE (addr), addr, t);
4569 /* Compute new value for AP. */
4570 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4571 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4572 gimplify_and_add (t, pre_p);
4574 addr = fold_convert (build_pointer_type (type), addr);
4576 if (indirect)
4577 addr = build_va_arg_indirect_ref (addr);
4579 return build_va_arg_indirect_ref (addr);
4582 /* Build an indirect-ref expression over the given TREE, which represents a
4583 piece of a va_arg() expansion. */
4584 tree
4585 build_va_arg_indirect_ref (tree addr)
4587 addr = build_fold_indirect_ref_loc (EXPR_LOCATION (addr), addr);
4589 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4590 mf_mark (addr);
4592 return addr;
4595 /* Return a dummy expression of type TYPE in order to keep going after an
4596 error. */
4598 static tree
4599 dummy_object (tree type)
4601 tree t = build_int_cst (build_pointer_type (type), 0);
4602 return build1 (INDIRECT_REF, type, t);
4605 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4606 builtin function, but a very special sort of operator. */
4608 enum gimplify_status
4609 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4611 tree promoted_type, have_va_type;
4612 tree valist = TREE_OPERAND (*expr_p, 0);
4613 tree type = TREE_TYPE (*expr_p);
4614 tree t;
4615 location_t loc = EXPR_LOCATION (*expr_p);
4617 /* Verify that valist is of the proper type. */
4618 have_va_type = TREE_TYPE (valist);
4619 if (have_va_type == error_mark_node)
4620 return GS_ERROR;
4621 have_va_type = targetm.canonical_va_list_type (have_va_type);
4623 if (have_va_type == NULL_TREE)
4625 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4626 return GS_ERROR;
4629 /* Generate a diagnostic for requesting data of a type that cannot
4630 be passed through `...' due to type promotion at the call site. */
4631 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4632 != type)
4634 static bool gave_help;
4635 bool warned;
4637 /* Unfortunately, this is merely undefined, rather than a constraint
4638 violation, so we cannot make this an error. If this call is never
4639 executed, the program is still strictly conforming. */
4640 warned = warning_at (loc, 0,
4641 "%qT is promoted to %qT when passed through %<...%>",
4642 type, promoted_type);
4643 if (!gave_help && warned)
4645 gave_help = true;
4646 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4647 promoted_type, type);
4650 /* We can, however, treat "undefined" any way we please.
4651 Call abort to encourage the user to fix the program. */
4652 if (warned)
4653 inform (loc, "if this code is reached, the program will abort");
4654 /* Before the abort, allow the evaluation of the va_list
4655 expression to exit or longjmp. */
4656 gimplify_and_add (valist, pre_p);
4657 t = build_call_expr_loc (loc,
4658 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4659 gimplify_and_add (t, pre_p);
4661 /* This is dead code, but go ahead and finish so that the
4662 mode of the result comes out right. */
4663 *expr_p = dummy_object (type);
4664 return GS_ALL_DONE;
4666 else
4668 /* Make it easier for the backends by protecting the valist argument
4669 from multiple evaluations. */
4670 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4672 /* For this case, the backends will be expecting a pointer to
4673 TREE_TYPE (abi), but it's possible we've
4674 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4675 So fix it. */
4676 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4678 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4679 valist = fold_convert_loc (loc, p1,
4680 build_fold_addr_expr_loc (loc, valist));
4683 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4685 else
4686 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4688 if (!targetm.gimplify_va_arg_expr)
4689 /* FIXME: Once most targets are converted we should merely
4690 assert this is non-null. */
4691 return GS_ALL_DONE;
4693 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4694 return GS_OK;
4698 /* Expand EXP, a call to __builtin_va_end. */
4700 static rtx
4701 expand_builtin_va_end (tree exp)
4703 tree valist = CALL_EXPR_ARG (exp, 0);
4705 /* Evaluate for side effects, if needed. I hate macros that don't
4706 do that. */
4707 if (TREE_SIDE_EFFECTS (valist))
4708 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4710 return const0_rtx;
4713 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4714 builtin rather than just as an assignment in stdarg.h because of the
4715 nastiness of array-type va_list types. */
4717 static rtx
4718 expand_builtin_va_copy (tree exp)
4720 tree dst, src, t;
4721 location_t loc = EXPR_LOCATION (exp);
4723 dst = CALL_EXPR_ARG (exp, 0);
4724 src = CALL_EXPR_ARG (exp, 1);
4726 dst = stabilize_va_list_loc (loc, dst, 1);
4727 src = stabilize_va_list_loc (loc, src, 0);
4729 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4731 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4733 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4734 TREE_SIDE_EFFECTS (t) = 1;
4735 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4737 else
4739 rtx dstb, srcb, size;
4741 /* Evaluate to pointers. */
4742 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4743 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4744 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4745 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4747 dstb = convert_memory_address (Pmode, dstb);
4748 srcb = convert_memory_address (Pmode, srcb);
4750 /* "Dereference" to BLKmode memories. */
4751 dstb = gen_rtx_MEM (BLKmode, dstb);
4752 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4753 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4754 srcb = gen_rtx_MEM (BLKmode, srcb);
4755 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4756 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4758 /* Copy. */
4759 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4762 return const0_rtx;
4765 /* Expand a call to one of the builtin functions __builtin_frame_address or
4766 __builtin_return_address. */
4768 static rtx
4769 expand_builtin_frame_address (tree fndecl, tree exp)
4771 /* The argument must be a nonnegative integer constant.
4772 It counts the number of frames to scan up the stack.
4773 The value is the return address saved in that frame. */
4774 if (call_expr_nargs (exp) == 0)
4775 /* Warning about missing arg was already issued. */
4776 return const0_rtx;
4777 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4779 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4780 error ("invalid argument to %<__builtin_frame_address%>");
4781 else
4782 error ("invalid argument to %<__builtin_return_address%>");
4783 return const0_rtx;
4785 else
4787 rtx tem
4788 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4789 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4791 /* Some ports cannot access arbitrary stack frames. */
4792 if (tem == NULL)
4794 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4795 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4796 else
4797 warning (0, "unsupported argument to %<__builtin_return_address%>");
4798 return const0_rtx;
4801 /* For __builtin_frame_address, return what we've got. */
4802 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4803 return tem;
4805 if (!REG_P (tem)
4806 && ! CONSTANT_P (tem))
4807 tem = copy_to_mode_reg (Pmode, tem);
4808 return tem;
4812 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
4813 we failed and the caller should emit a normal call, otherwise try to get
4814 the result in TARGET, if convenient. */
4816 static rtx
4817 expand_builtin_alloca (tree exp, rtx target)
4819 rtx op0;
4820 rtx result;
4822 /* Emit normal call if marked not-inlineable. */
4823 if (CALL_CANNOT_INLINE_P (exp))
4824 return NULL_RTX;
4826 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4827 return NULL_RTX;
4829 /* Compute the argument. */
4830 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4832 /* Allocate the desired space. */
4833 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4834 result = convert_memory_address (ptr_mode, result);
4836 return result;
4839 /* Expand a call to a bswap builtin with argument ARG0. MODE
4840 is the mode to expand with. */
4842 static rtx
4843 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4845 enum machine_mode mode;
4846 tree arg;
4847 rtx op0;
4849 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4850 return NULL_RTX;
4852 arg = CALL_EXPR_ARG (exp, 0);
4853 mode = TYPE_MODE (TREE_TYPE (arg));
4854 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4856 target = expand_unop (mode, bswap_optab, op0, target, 1);
4858 gcc_assert (target);
4860 return convert_to_mode (mode, target, 0);
4863 /* Expand a call to a unary builtin in EXP.
4864 Return NULL_RTX if a normal call should be emitted rather than expanding the
4865 function in-line. If convenient, the result should be placed in TARGET.
4866 SUBTARGET may be used as the target for computing one of EXP's operands. */
4868 static rtx
4869 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4870 rtx subtarget, optab op_optab)
4872 rtx op0;
4874 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4875 return NULL_RTX;
4877 /* Compute the argument. */
4878 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
4879 VOIDmode, EXPAND_NORMAL);
4880 /* Compute op, into TARGET if possible.
4881 Set TARGET to wherever the result comes back. */
4882 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4883 op_optab, op0, target, 1);
4884 gcc_assert (target);
4886 return convert_to_mode (target_mode, target, 0);
4889 /* Expand a call to __builtin_expect. We just return our argument
4890 as the builtin_expect semantic should've been already executed by
4891 tree branch prediction pass. */
4893 static rtx
4894 expand_builtin_expect (tree exp, rtx target)
4896 tree arg, c;
4898 if (call_expr_nargs (exp) < 2)
4899 return const0_rtx;
4900 arg = CALL_EXPR_ARG (exp, 0);
4901 c = CALL_EXPR_ARG (exp, 1);
4903 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4904 /* When guessing was done, the hints should be already stripped away. */
4905 gcc_assert (!flag_guess_branch_prob
4906 || optimize == 0 || errorcount || sorrycount);
4907 return target;
4910 void
4911 expand_builtin_trap (void)
4913 #ifdef HAVE_trap
4914 if (HAVE_trap)
4915 emit_insn (gen_trap ());
4916 else
4917 #endif
4918 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4919 emit_barrier ();
4922 /* Expand a call to __builtin_unreachable. We do nothing except emit
4923 a barrier saying that control flow will not pass here.
4925 It is the responsibility of the program being compiled to ensure
4926 that control flow does never reach __builtin_unreachable. */
4927 static void
4928 expand_builtin_unreachable (void)
4930 emit_barrier ();
4933 /* Expand EXP, a call to fabs, fabsf or fabsl.
4934 Return NULL_RTX if a normal call should be emitted rather than expanding
4935 the function inline. If convenient, the result should be placed
4936 in TARGET. SUBTARGET may be used as the target for computing
4937 the operand. */
4939 static rtx
4940 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4942 enum machine_mode mode;
4943 tree arg;
4944 rtx op0;
4946 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4947 return NULL_RTX;
4949 arg = CALL_EXPR_ARG (exp, 0);
4950 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4951 mode = TYPE_MODE (TREE_TYPE (arg));
4952 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4953 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4956 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4957 Return NULL is a normal call should be emitted rather than expanding the
4958 function inline. If convenient, the result should be placed in TARGET.
4959 SUBTARGET may be used as the target for computing the operand. */
4961 static rtx
4962 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4964 rtx op0, op1;
4965 tree arg;
4967 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4968 return NULL_RTX;
4970 arg = CALL_EXPR_ARG (exp, 0);
4971 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4973 arg = CALL_EXPR_ARG (exp, 1);
4974 op1 = expand_normal (arg);
4976 return expand_copysign (op0, op1, target);
4979 /* Create a new constant string literal and return a char* pointer to it.
4980 The STRING_CST value is the LEN characters at STR. */
4981 tree
4982 build_string_literal (int len, const char *str)
4984 tree t, elem, index, type;
4986 t = build_string (len, str);
4987 elem = build_type_variant (char_type_node, 1, 0);
4988 index = build_index_type (size_int (len - 1));
4989 type = build_array_type (elem, index);
4990 TREE_TYPE (t) = type;
4991 TREE_CONSTANT (t) = 1;
4992 TREE_READONLY (t) = 1;
4993 TREE_STATIC (t) = 1;
4995 type = build_pointer_type (elem);
4996 t = build1 (ADDR_EXPR, type,
4997 build4 (ARRAY_REF, elem,
4998 t, integer_zero_node, NULL_TREE, NULL_TREE));
4999 return t;
5002 /* Expand EXP, a call to printf or printf_unlocked.
5003 Return NULL_RTX if a normal call should be emitted rather than transforming
5004 the function inline. If convenient, the result should be placed in
5005 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
5006 call. */
5007 static rtx
5008 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
5009 bool unlocked)
5011 /* If we're using an unlocked function, assume the other unlocked
5012 functions exist explicitly. */
5013 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
5014 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
5015 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
5016 : implicit_built_in_decls[BUILT_IN_PUTS];
5017 const char *fmt_str;
5018 tree fn = 0;
5019 tree fmt, arg;
5020 int nargs = call_expr_nargs (exp);
5022 /* If the return value is used, don't do the transformation. */
5023 if (target != const0_rtx)
5024 return NULL_RTX;
5026 /* Verify the required arguments in the original call. */
5027 if (nargs == 0)
5028 return NULL_RTX;
5029 fmt = CALL_EXPR_ARG (exp, 0);
5030 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5031 return NULL_RTX;
5033 /* Check whether the format is a literal string constant. */
5034 fmt_str = c_getstr (fmt);
5035 if (fmt_str == NULL)
5036 return NULL_RTX;
5038 if (!init_target_chars ())
5039 return NULL_RTX;
5041 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5042 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5044 if ((nargs != 2)
5045 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1))))
5046 return NULL_RTX;
5047 if (fn_puts)
5048 fn = build_call_nofold (fn_puts, 1, CALL_EXPR_ARG (exp, 1));
5050 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5051 else if (strcmp (fmt_str, target_percent_c) == 0)
5053 if ((nargs != 2)
5054 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE)
5055 return NULL_RTX;
5056 if (fn_putchar)
5057 fn = build_call_nofold (fn_putchar, 1, CALL_EXPR_ARG (exp, 1));
5059 else
5061 /* We can't handle anything else with % args or %% ... yet. */
5062 if (strchr (fmt_str, target_percent))
5063 return NULL_RTX;
5065 if (nargs > 1)
5066 return NULL_RTX;
5068 /* If the format specifier was "", printf does nothing. */
5069 if (fmt_str[0] == '\0')
5070 return const0_rtx;
5071 /* If the format specifier has length of 1, call putchar. */
5072 if (fmt_str[1] == '\0')
5074 /* Given printf("c"), (where c is any one character,)
5075 convert "c"[0] to an int and pass that to the replacement
5076 function. */
5077 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5078 if (fn_putchar)
5079 fn = build_call_nofold (fn_putchar, 1, arg);
5081 else
5083 /* If the format specifier was "string\n", call puts("string"). */
5084 size_t len = strlen (fmt_str);
5085 if ((unsigned char)fmt_str[len - 1] == target_newline)
5087 /* Create a NUL-terminated string that's one char shorter
5088 than the original, stripping off the trailing '\n'. */
5089 char *newstr = XALLOCAVEC (char, len);
5090 memcpy (newstr, fmt_str, len - 1);
5091 newstr[len - 1] = 0;
5092 arg = build_string_literal (len, newstr);
5093 if (fn_puts)
5094 fn = build_call_nofold (fn_puts, 1, arg);
5096 else
5097 /* We'd like to arrange to call fputs(string,stdout) here,
5098 but we need stdout and don't have a way to get it yet. */
5099 return NULL_RTX;
5103 if (!fn)
5104 return NULL_RTX;
5105 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5106 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5107 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5110 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5111 Return NULL_RTX if a normal call should be emitted rather than transforming
5112 the function inline. If convenient, the result should be placed in
5113 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5114 call. */
5115 static rtx
5116 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5117 bool unlocked)
5119 /* If we're using an unlocked function, assume the other unlocked
5120 functions exist explicitly. */
5121 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5122 : implicit_built_in_decls[BUILT_IN_FPUTC];
5123 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5124 : implicit_built_in_decls[BUILT_IN_FPUTS];
5125 const char *fmt_str;
5126 tree fn = 0;
5127 tree fmt, fp, arg;
5128 int nargs = call_expr_nargs (exp);
5130 /* If the return value is used, don't do the transformation. */
5131 if (target != const0_rtx)
5132 return NULL_RTX;
5134 /* Verify the required arguments in the original call. */
5135 if (nargs < 2)
5136 return NULL_RTX;
5137 fp = CALL_EXPR_ARG (exp, 0);
5138 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5139 return NULL_RTX;
5140 fmt = CALL_EXPR_ARG (exp, 1);
5141 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5142 return NULL_RTX;
5144 /* Check whether the format is a literal string constant. */
5145 fmt_str = c_getstr (fmt);
5146 if (fmt_str == NULL)
5147 return NULL_RTX;
5149 if (!init_target_chars ())
5150 return NULL_RTX;
5152 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5153 if (strcmp (fmt_str, target_percent_s) == 0)
5155 if ((nargs != 3)
5156 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2))))
5157 return NULL_RTX;
5158 arg = CALL_EXPR_ARG (exp, 2);
5159 if (fn_fputs)
5160 fn = build_call_nofold (fn_fputs, 2, arg, fp);
5162 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5163 else if (strcmp (fmt_str, target_percent_c) == 0)
5165 if ((nargs != 3)
5166 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE)
5167 return NULL_RTX;
5168 arg = CALL_EXPR_ARG (exp, 2);
5169 if (fn_fputc)
5170 fn = build_call_nofold (fn_fputc, 2, arg, fp);
5172 else
5174 /* We can't handle anything else with % args or %% ... yet. */
5175 if (strchr (fmt_str, target_percent))
5176 return NULL_RTX;
5178 if (nargs > 2)
5179 return NULL_RTX;
5181 /* If the format specifier was "", fprintf does nothing. */
5182 if (fmt_str[0] == '\0')
5184 /* Evaluate and ignore FILE* argument for side-effects. */
5185 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5186 return const0_rtx;
5189 /* When "string" doesn't contain %, replace all cases of
5190 fprintf(stream,string) with fputs(string,stream). The fputs
5191 builtin will take care of special cases like length == 1. */
5192 if (fn_fputs)
5193 fn = build_call_nofold (fn_fputs, 2, fmt, fp);
5196 if (!fn)
5197 return NULL_RTX;
5198 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5199 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5200 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5203 /* Expand a call EXP to sprintf. Return NULL_RTX if
5204 a normal call should be emitted rather than expanding the function
5205 inline. If convenient, the result should be placed in TARGET with
5206 mode MODE. */
5208 static rtx
5209 expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode)
5211 tree dest, fmt;
5212 const char *fmt_str;
5213 int nargs = call_expr_nargs (exp);
5215 /* Verify the required arguments in the original call. */
5216 if (nargs < 2)
5217 return NULL_RTX;
5218 dest = CALL_EXPR_ARG (exp, 0);
5219 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5220 return NULL_RTX;
5221 fmt = CALL_EXPR_ARG (exp, 0);
5222 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5223 return NULL_RTX;
5225 /* Check whether the format is a literal string constant. */
5226 fmt_str = c_getstr (fmt);
5227 if (fmt_str == NULL)
5228 return NULL_RTX;
5230 if (!init_target_chars ())
5231 return NULL_RTX;
5233 /* If the format doesn't contain % args or %%, use strcpy. */
5234 if (strchr (fmt_str, target_percent) == 0)
5236 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5237 tree exp;
5239 if ((nargs > 2) || ! fn)
5240 return NULL_RTX;
5241 expand_expr (build_call_nofold (fn, 2, dest, fmt),
5242 const0_rtx, VOIDmode, EXPAND_NORMAL);
5243 if (target == const0_rtx)
5244 return const0_rtx;
5245 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5246 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5248 /* If the format is "%s", use strcpy if the result isn't used. */
5249 else if (strcmp (fmt_str, target_percent_s) == 0)
5251 tree fn, arg, len;
5252 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5254 if (! fn)
5255 return NULL_RTX;
5256 if (nargs != 3)
5257 return NULL_RTX;
5258 arg = CALL_EXPR_ARG (exp, 2);
5259 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5260 return NULL_RTX;
5262 if (target != const0_rtx)
5264 len = c_strlen (arg, 1);
5265 if (! len || TREE_CODE (len) != INTEGER_CST)
5266 return NULL_RTX;
5268 else
5269 len = NULL_TREE;
5271 expand_expr (build_call_nofold (fn, 2, dest, arg),
5272 const0_rtx, VOIDmode, EXPAND_NORMAL);
5274 if (target == const0_rtx)
5275 return const0_rtx;
5276 return expand_expr (len, target, mode, EXPAND_NORMAL);
5279 return NULL_RTX;
5282 /* Expand a call to either the entry or exit function profiler. */
5284 static rtx
5285 expand_builtin_profile_func (bool exitp)
5287 rtx this_rtx, which;
5289 this_rtx = DECL_RTL (current_function_decl);
5290 gcc_assert (MEM_P (this_rtx));
5291 this_rtx = XEXP (this_rtx, 0);
5293 if (exitp)
5294 which = profile_function_exit_libfunc;
5295 else
5296 which = profile_function_entry_libfunc;
5298 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5299 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5301 Pmode);
5303 return const0_rtx;
5306 /* Expand a call to __builtin___clear_cache. */
5308 static rtx
5309 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5311 #ifndef HAVE_clear_cache
5312 #ifdef CLEAR_INSN_CACHE
5313 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5314 does something. Just do the default expansion to a call to
5315 __clear_cache(). */
5316 return NULL_RTX;
5317 #else
5318 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5319 does nothing. There is no need to call it. Do nothing. */
5320 return const0_rtx;
5321 #endif /* CLEAR_INSN_CACHE */
5322 #else
5323 /* We have a "clear_cache" insn, and it will handle everything. */
5324 tree begin, end;
5325 rtx begin_rtx, end_rtx;
5326 enum insn_code icode;
5328 /* We must not expand to a library call. If we did, any
5329 fallback library function in libgcc that might contain a call to
5330 __builtin___clear_cache() would recurse infinitely. */
5331 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5333 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5334 return const0_rtx;
5337 if (HAVE_clear_cache)
5339 icode = CODE_FOR_clear_cache;
5341 begin = CALL_EXPR_ARG (exp, 0);
5342 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5343 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5344 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5345 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5347 end = CALL_EXPR_ARG (exp, 1);
5348 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5349 end_rtx = convert_memory_address (Pmode, end_rtx);
5350 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5351 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5353 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5355 return const0_rtx;
5356 #endif /* HAVE_clear_cache */
5359 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5361 static rtx
5362 round_trampoline_addr (rtx tramp)
5364 rtx temp, addend, mask;
5366 /* If we don't need too much alignment, we'll have been guaranteed
5367 proper alignment by get_trampoline_type. */
5368 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5369 return tramp;
5371 /* Round address up to desired boundary. */
5372 temp = gen_reg_rtx (Pmode);
5373 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5374 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5376 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5377 temp, 0, OPTAB_LIB_WIDEN);
5378 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5379 temp, 0, OPTAB_LIB_WIDEN);
5381 return tramp;
5384 static rtx
5385 expand_builtin_init_trampoline (tree exp)
5387 tree t_tramp, t_func, t_chain;
5388 rtx m_tramp, r_tramp, r_chain, tmp;
5390 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5391 POINTER_TYPE, VOID_TYPE))
5392 return NULL_RTX;
5394 t_tramp = CALL_EXPR_ARG (exp, 0);
5395 t_func = CALL_EXPR_ARG (exp, 1);
5396 t_chain = CALL_EXPR_ARG (exp, 2);
5398 r_tramp = expand_normal (t_tramp);
5399 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5400 MEM_NOTRAP_P (m_tramp) = 1;
5402 /* The TRAMP argument should be the address of a field within the
5403 local function's FRAME decl. Let's see if we can fill in the
5404 to fill in the MEM_ATTRs for this memory. */
5405 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5406 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5407 true, 0);
5409 tmp = round_trampoline_addr (r_tramp);
5410 if (tmp != r_tramp)
5412 m_tramp = change_address (m_tramp, BLKmode, tmp);
5413 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5414 set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5417 /* The FUNC argument should be the address of the nested function.
5418 Extract the actual function decl to pass to the hook. */
5419 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5420 t_func = TREE_OPERAND (t_func, 0);
5421 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5423 r_chain = expand_normal (t_chain);
5425 /* Generate insns to initialize the trampoline. */
5426 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5428 trampolines_created = 1;
5429 return const0_rtx;
5432 static rtx
5433 expand_builtin_adjust_trampoline (tree exp)
5435 rtx tramp;
5437 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5438 return NULL_RTX;
5440 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5441 tramp = round_trampoline_addr (tramp);
5442 if (targetm.calls.trampoline_adjust_address)
5443 tramp = targetm.calls.trampoline_adjust_address (tramp);
5445 return tramp;
5448 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5449 function. The function first checks whether the back end provides
5450 an insn to implement signbit for the respective mode. If not, it
5451 checks whether the floating point format of the value is such that
5452 the sign bit can be extracted. If that is not the case, the
5453 function returns NULL_RTX to indicate that a normal call should be
5454 emitted rather than expanding the function in-line. EXP is the
5455 expression that is a call to the builtin function; if convenient,
5456 the result should be placed in TARGET. */
5457 static rtx
5458 expand_builtin_signbit (tree exp, rtx target)
5460 const struct real_format *fmt;
5461 enum machine_mode fmode, imode, rmode;
5462 HOST_WIDE_INT hi, lo;
5463 tree arg;
5464 int word, bitpos;
5465 enum insn_code icode;
5466 rtx temp;
5467 location_t loc = EXPR_LOCATION (exp);
5469 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5470 return NULL_RTX;
5472 arg = CALL_EXPR_ARG (exp, 0);
5473 fmode = TYPE_MODE (TREE_TYPE (arg));
5474 rmode = TYPE_MODE (TREE_TYPE (exp));
5475 fmt = REAL_MODE_FORMAT (fmode);
5477 arg = builtin_save_expr (arg);
5479 /* Expand the argument yielding a RTX expression. */
5480 temp = expand_normal (arg);
5482 /* Check if the back end provides an insn that handles signbit for the
5483 argument's mode. */
5484 icode = signbit_optab->handlers [(int) fmode].insn_code;
5485 if (icode != CODE_FOR_nothing)
5487 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5488 emit_unop_insn (icode, target, temp, UNKNOWN);
5489 return target;
5492 /* For floating point formats without a sign bit, implement signbit
5493 as "ARG < 0.0". */
5494 bitpos = fmt->signbit_ro;
5495 if (bitpos < 0)
5497 /* But we can't do this if the format supports signed zero. */
5498 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5499 return NULL_RTX;
5501 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5502 build_real (TREE_TYPE (arg), dconst0));
5503 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5506 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5508 imode = int_mode_for_mode (fmode);
5509 if (imode == BLKmode)
5510 return NULL_RTX;
5511 temp = gen_lowpart (imode, temp);
5513 else
5515 imode = word_mode;
5516 /* Handle targets with different FP word orders. */
5517 if (FLOAT_WORDS_BIG_ENDIAN)
5518 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5519 else
5520 word = bitpos / BITS_PER_WORD;
5521 temp = operand_subword_force (temp, word, fmode);
5522 bitpos = bitpos % BITS_PER_WORD;
5525 /* Force the intermediate word_mode (or narrower) result into a
5526 register. This avoids attempting to create paradoxical SUBREGs
5527 of floating point modes below. */
5528 temp = force_reg (imode, temp);
5530 /* If the bitpos is within the "result mode" lowpart, the operation
5531 can be implement with a single bitwise AND. Otherwise, we need
5532 a right shift and an AND. */
5534 if (bitpos < GET_MODE_BITSIZE (rmode))
5536 if (bitpos < HOST_BITS_PER_WIDE_INT)
5538 hi = 0;
5539 lo = (HOST_WIDE_INT) 1 << bitpos;
5541 else
5543 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5544 lo = 0;
5547 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5548 temp = gen_lowpart (rmode, temp);
5549 temp = expand_binop (rmode, and_optab, temp,
5550 immed_double_const (lo, hi, rmode),
5551 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5553 else
5555 /* Perform a logical right shift to place the signbit in the least
5556 significant bit, then truncate the result to the desired mode
5557 and mask just this bit. */
5558 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5559 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5560 temp = gen_lowpart (rmode, temp);
5561 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5562 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5565 return temp;
5568 /* Expand fork or exec calls. TARGET is the desired target of the
5569 call. EXP is the call. FN is the
5570 identificator of the actual function. IGNORE is nonzero if the
5571 value is to be ignored. */
5573 static rtx
5574 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5576 tree id, decl;
5577 tree call;
5579 /* If we are not profiling, just call the function. */
5580 if (!profile_arc_flag)
5581 return NULL_RTX;
5583 /* Otherwise call the wrapper. This should be equivalent for the rest of
5584 compiler, so the code does not diverge, and the wrapper may run the
5585 code necessary for keeping the profiling sane. */
5587 switch (DECL_FUNCTION_CODE (fn))
5589 case BUILT_IN_FORK:
5590 id = get_identifier ("__gcov_fork");
5591 break;
5593 case BUILT_IN_EXECL:
5594 id = get_identifier ("__gcov_execl");
5595 break;
5597 case BUILT_IN_EXECV:
5598 id = get_identifier ("__gcov_execv");
5599 break;
5601 case BUILT_IN_EXECLP:
5602 id = get_identifier ("__gcov_execlp");
5603 break;
5605 case BUILT_IN_EXECLE:
5606 id = get_identifier ("__gcov_execle");
5607 break;
5609 case BUILT_IN_EXECVP:
5610 id = get_identifier ("__gcov_execvp");
5611 break;
5613 case BUILT_IN_EXECVE:
5614 id = get_identifier ("__gcov_execve");
5615 break;
5617 default:
5618 gcc_unreachable ();
5621 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5622 FUNCTION_DECL, id, TREE_TYPE (fn));
5623 DECL_EXTERNAL (decl) = 1;
5624 TREE_PUBLIC (decl) = 1;
5625 DECL_ARTIFICIAL (decl) = 1;
5626 TREE_NOTHROW (decl) = 1;
5627 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5628 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5629 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5630 return expand_call (call, target, ignore);
5635 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5636 the pointer in these functions is void*, the tree optimizers may remove
5637 casts. The mode computed in expand_builtin isn't reliable either, due
5638 to __sync_bool_compare_and_swap.
5640 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5641 group of builtins. This gives us log2 of the mode size. */
5643 static inline enum machine_mode
5644 get_builtin_sync_mode (int fcode_diff)
5646 /* The size is not negotiable, so ask not to get BLKmode in return
5647 if the target indicates that a smaller size would be better. */
5648 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5651 /* Expand the memory expression LOC and return the appropriate memory operand
5652 for the builtin_sync operations. */
5654 static rtx
5655 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5657 rtx addr, mem;
5659 addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM);
5661 /* Note that we explicitly do not want any alias information for this
5662 memory, so that we kill all other live memories. Otherwise we don't
5663 satisfy the full barrier semantics of the intrinsic. */
5664 mem = validize_mem (gen_rtx_MEM (mode, addr));
5666 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5667 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5668 MEM_VOLATILE_P (mem) = 1;
5670 return mem;
5673 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5674 EXP is the CALL_EXPR. CODE is the rtx code
5675 that corresponds to the arithmetic or logical operation from the name;
5676 an exception here is that NOT actually means NAND. TARGET is an optional
5677 place for us to store the results; AFTER is true if this is the
5678 fetch_and_xxx form. IGNORE is true if we don't actually care about
5679 the result of the operation at all. */
5681 static rtx
5682 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5683 enum rtx_code code, bool after,
5684 rtx target, bool ignore)
5686 rtx val, mem;
5687 enum machine_mode old_mode;
5688 location_t loc = EXPR_LOCATION (exp);
5690 if (code == NOT && warn_sync_nand)
5692 tree fndecl = get_callee_fndecl (exp);
5693 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5695 static bool warned_f_a_n, warned_n_a_f;
5697 switch (fcode)
5699 case BUILT_IN_FETCH_AND_NAND_1:
5700 case BUILT_IN_FETCH_AND_NAND_2:
5701 case BUILT_IN_FETCH_AND_NAND_4:
5702 case BUILT_IN_FETCH_AND_NAND_8:
5703 case BUILT_IN_FETCH_AND_NAND_16:
5705 if (warned_f_a_n)
5706 break;
5708 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5709 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5710 warned_f_a_n = true;
5711 break;
5713 case BUILT_IN_NAND_AND_FETCH_1:
5714 case BUILT_IN_NAND_AND_FETCH_2:
5715 case BUILT_IN_NAND_AND_FETCH_4:
5716 case BUILT_IN_NAND_AND_FETCH_8:
5717 case BUILT_IN_NAND_AND_FETCH_16:
5719 if (warned_n_a_f)
5720 break;
5722 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5723 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5724 warned_n_a_f = true;
5725 break;
5727 default:
5728 gcc_unreachable ();
5732 /* Expand the operands. */
5733 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5735 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5736 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5737 of CONST_INTs, where we know the old_mode only from the call argument. */
5738 old_mode = GET_MODE (val);
5739 if (old_mode == VOIDmode)
5740 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5741 val = convert_modes (mode, old_mode, val, 1);
5743 if (ignore)
5744 return expand_sync_operation (mem, val, code);
5745 else
5746 return expand_sync_fetch_operation (mem, val, code, after, target);
5749 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5750 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5751 true if this is the boolean form. TARGET is a place for us to store the
5752 results; this is NOT optional if IS_BOOL is true. */
5754 static rtx
5755 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5756 bool is_bool, rtx target)
5758 rtx old_val, new_val, mem;
5759 enum machine_mode old_mode;
5761 /* Expand the operands. */
5762 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5765 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5766 mode, EXPAND_NORMAL);
5767 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5768 of CONST_INTs, where we know the old_mode only from the call argument. */
5769 old_mode = GET_MODE (old_val);
5770 if (old_mode == VOIDmode)
5771 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5772 old_val = convert_modes (mode, old_mode, old_val, 1);
5774 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5775 mode, EXPAND_NORMAL);
5776 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5777 of CONST_INTs, where we know the old_mode only from the call argument. */
5778 old_mode = GET_MODE (new_val);
5779 if (old_mode == VOIDmode)
5780 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5781 new_val = convert_modes (mode, old_mode, new_val, 1);
5783 if (is_bool)
5784 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5785 else
5786 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5789 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5790 general form is actually an atomic exchange, and some targets only
5791 support a reduced form with the second argument being a constant 1.
5792 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5793 the results. */
5795 static rtx
5796 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5797 rtx target)
5799 rtx val, mem;
5800 enum machine_mode old_mode;
5802 /* Expand the operands. */
5803 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5804 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5805 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5806 of CONST_INTs, where we know the old_mode only from the call argument. */
5807 old_mode = GET_MODE (val);
5808 if (old_mode == VOIDmode)
5809 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5810 val = convert_modes (mode, old_mode, val, 1);
5812 return expand_sync_lock_test_and_set (mem, val, target);
5815 /* Expand the __sync_synchronize intrinsic. */
5817 static void
5818 expand_builtin_synchronize (void)
5820 gimple x;
5821 VEC (tree, gc) *v_clobbers;
5823 #ifdef HAVE_memory_barrier
5824 if (HAVE_memory_barrier)
5826 emit_insn (gen_memory_barrier ());
5827 return;
5829 #endif
5831 if (synchronize_libfunc != NULL_RTX)
5833 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5834 return;
5837 /* If no explicit memory barrier instruction is available, create an
5838 empty asm stmt with a memory clobber. */
5839 v_clobbers = VEC_alloc (tree, gc, 1);
5840 VEC_quick_push (tree, v_clobbers,
5841 tree_cons (NULL, build_string (6, "memory"), NULL));
5842 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5843 gimple_asm_set_volatile (x, true);
5844 expand_asm_stmt (x);
5847 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5849 static void
5850 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5852 enum insn_code icode;
5853 rtx mem, insn;
5854 rtx val = const0_rtx;
5856 /* Expand the operands. */
5857 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5859 /* If there is an explicit operation in the md file, use it. */
5860 icode = sync_lock_release[mode];
5861 if (icode != CODE_FOR_nothing)
5863 if (!insn_data[icode].operand[1].predicate (val, mode))
5864 val = force_reg (mode, val);
5866 insn = GEN_FCN (icode) (mem, val);
5867 if (insn)
5869 emit_insn (insn);
5870 return;
5874 /* Otherwise we can implement this operation by emitting a barrier
5875 followed by a store of zero. */
5876 expand_builtin_synchronize ();
5877 emit_move_insn (mem, val);
5880 /* Expand an expression EXP that calls a built-in function,
5881 with result going to TARGET if that's convenient
5882 (and in mode MODE if that's convenient).
5883 SUBTARGET may be used as the target for computing one of EXP's operands.
5884 IGNORE is nonzero if the value is to be ignored. */
5887 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5888 int ignore)
5890 tree fndecl = get_callee_fndecl (exp);
5891 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5892 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5894 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5895 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5897 /* When not optimizing, generate calls to library functions for a certain
5898 set of builtins. */
5899 if (!optimize
5900 && !called_as_built_in (fndecl)
5901 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5902 && fcode != BUILT_IN_ALLOCA
5903 && fcode != BUILT_IN_FREE)
5904 return expand_call (exp, target, ignore);
5906 /* The built-in function expanders test for target == const0_rtx
5907 to determine whether the function's result will be ignored. */
5908 if (ignore)
5909 target = const0_rtx;
5911 /* If the result of a pure or const built-in function is ignored, and
5912 none of its arguments are volatile, we can avoid expanding the
5913 built-in call and just evaluate the arguments for side-effects. */
5914 if (target == const0_rtx
5915 && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl)))
5917 bool volatilep = false;
5918 tree arg;
5919 call_expr_arg_iterator iter;
5921 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5922 if (TREE_THIS_VOLATILE (arg))
5924 volatilep = true;
5925 break;
5928 if (! volatilep)
5930 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5931 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5932 return const0_rtx;
5936 switch (fcode)
5938 CASE_FLT_FN (BUILT_IN_FABS):
5939 target = expand_builtin_fabs (exp, target, subtarget);
5940 if (target)
5941 return target;
5942 break;
5944 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5945 target = expand_builtin_copysign (exp, target, subtarget);
5946 if (target)
5947 return target;
5948 break;
5950 /* Just do a normal library call if we were unable to fold
5951 the values. */
5952 CASE_FLT_FN (BUILT_IN_CABS):
5953 break;
5955 CASE_FLT_FN (BUILT_IN_EXP):
5956 CASE_FLT_FN (BUILT_IN_EXP10):
5957 CASE_FLT_FN (BUILT_IN_POW10):
5958 CASE_FLT_FN (BUILT_IN_EXP2):
5959 CASE_FLT_FN (BUILT_IN_EXPM1):
5960 CASE_FLT_FN (BUILT_IN_LOGB):
5961 CASE_FLT_FN (BUILT_IN_LOG):
5962 CASE_FLT_FN (BUILT_IN_LOG10):
5963 CASE_FLT_FN (BUILT_IN_LOG2):
5964 CASE_FLT_FN (BUILT_IN_LOG1P):
5965 CASE_FLT_FN (BUILT_IN_TAN):
5966 CASE_FLT_FN (BUILT_IN_ASIN):
5967 CASE_FLT_FN (BUILT_IN_ACOS):
5968 CASE_FLT_FN (BUILT_IN_ATAN):
5969 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5970 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5971 because of possible accuracy problems. */
5972 if (! flag_unsafe_math_optimizations)
5973 break;
5974 CASE_FLT_FN (BUILT_IN_SQRT):
5975 CASE_FLT_FN (BUILT_IN_FLOOR):
5976 CASE_FLT_FN (BUILT_IN_CEIL):
5977 CASE_FLT_FN (BUILT_IN_TRUNC):
5978 CASE_FLT_FN (BUILT_IN_ROUND):
5979 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5980 CASE_FLT_FN (BUILT_IN_RINT):
5981 target = expand_builtin_mathfn (exp, target, subtarget);
5982 if (target)
5983 return target;
5984 break;
5986 CASE_FLT_FN (BUILT_IN_ILOGB):
5987 if (! flag_unsafe_math_optimizations)
5988 break;
5989 CASE_FLT_FN (BUILT_IN_ISINF):
5990 CASE_FLT_FN (BUILT_IN_FINITE):
5991 case BUILT_IN_ISFINITE:
5992 case BUILT_IN_ISNORMAL:
5993 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
5994 if (target)
5995 return target;
5996 break;
5998 CASE_FLT_FN (BUILT_IN_LCEIL):
5999 CASE_FLT_FN (BUILT_IN_LLCEIL):
6000 CASE_FLT_FN (BUILT_IN_LFLOOR):
6001 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6002 target = expand_builtin_int_roundingfn (exp, target);
6003 if (target)
6004 return target;
6005 break;
6007 CASE_FLT_FN (BUILT_IN_LRINT):
6008 CASE_FLT_FN (BUILT_IN_LLRINT):
6009 CASE_FLT_FN (BUILT_IN_LROUND):
6010 CASE_FLT_FN (BUILT_IN_LLROUND):
6011 target = expand_builtin_int_roundingfn_2 (exp, target);
6012 if (target)
6013 return target;
6014 break;
6016 CASE_FLT_FN (BUILT_IN_POW):
6017 target = expand_builtin_pow (exp, target, subtarget);
6018 if (target)
6019 return target;
6020 break;
6022 CASE_FLT_FN (BUILT_IN_POWI):
6023 target = expand_builtin_powi (exp, target, subtarget);
6024 if (target)
6025 return target;
6026 break;
6028 CASE_FLT_FN (BUILT_IN_ATAN2):
6029 CASE_FLT_FN (BUILT_IN_LDEXP):
6030 CASE_FLT_FN (BUILT_IN_SCALB):
6031 CASE_FLT_FN (BUILT_IN_SCALBN):
6032 CASE_FLT_FN (BUILT_IN_SCALBLN):
6033 if (! flag_unsafe_math_optimizations)
6034 break;
6036 CASE_FLT_FN (BUILT_IN_FMOD):
6037 CASE_FLT_FN (BUILT_IN_REMAINDER):
6038 CASE_FLT_FN (BUILT_IN_DREM):
6039 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6040 if (target)
6041 return target;
6042 break;
6044 CASE_FLT_FN (BUILT_IN_CEXPI):
6045 target = expand_builtin_cexpi (exp, target, subtarget);
6046 gcc_assert (target);
6047 return target;
6049 CASE_FLT_FN (BUILT_IN_SIN):
6050 CASE_FLT_FN (BUILT_IN_COS):
6051 if (! flag_unsafe_math_optimizations)
6052 break;
6053 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6054 if (target)
6055 return target;
6056 break;
6058 CASE_FLT_FN (BUILT_IN_SINCOS):
6059 if (! flag_unsafe_math_optimizations)
6060 break;
6061 target = expand_builtin_sincos (exp);
6062 if (target)
6063 return target;
6064 break;
6066 case BUILT_IN_APPLY_ARGS:
6067 return expand_builtin_apply_args ();
6069 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6070 FUNCTION with a copy of the parameters described by
6071 ARGUMENTS, and ARGSIZE. It returns a block of memory
6072 allocated on the stack into which is stored all the registers
6073 that might possibly be used for returning the result of a
6074 function. ARGUMENTS is the value returned by
6075 __builtin_apply_args. ARGSIZE is the number of bytes of
6076 arguments that must be copied. ??? How should this value be
6077 computed? We'll also need a safe worst case value for varargs
6078 functions. */
6079 case BUILT_IN_APPLY:
6080 if (!validate_arglist (exp, POINTER_TYPE,
6081 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6082 && !validate_arglist (exp, REFERENCE_TYPE,
6083 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6084 return const0_rtx;
6085 else
6087 rtx ops[3];
6089 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6090 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6091 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6093 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6096 /* __builtin_return (RESULT) causes the function to return the
6097 value described by RESULT. RESULT is address of the block of
6098 memory returned by __builtin_apply. */
6099 case BUILT_IN_RETURN:
6100 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6101 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6102 return const0_rtx;
6104 case BUILT_IN_SAVEREGS:
6105 return expand_builtin_saveregs ();
6107 case BUILT_IN_ARGS_INFO:
6108 return expand_builtin_args_info (exp);
6110 case BUILT_IN_VA_ARG_PACK:
6111 /* All valid uses of __builtin_va_arg_pack () are removed during
6112 inlining. */
6113 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6114 return const0_rtx;
6116 case BUILT_IN_VA_ARG_PACK_LEN:
6117 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6118 inlining. */
6119 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6120 return const0_rtx;
6122 /* Return the address of the first anonymous stack arg. */
6123 case BUILT_IN_NEXT_ARG:
6124 if (fold_builtin_next_arg (exp, false))
6125 return const0_rtx;
6126 return expand_builtin_next_arg ();
6128 case BUILT_IN_CLEAR_CACHE:
6129 target = expand_builtin___clear_cache (exp);
6130 if (target)
6131 return target;
6132 break;
6134 case BUILT_IN_CLASSIFY_TYPE:
6135 return expand_builtin_classify_type (exp);
6137 case BUILT_IN_CONSTANT_P:
6138 return const0_rtx;
6140 case BUILT_IN_FRAME_ADDRESS:
6141 case BUILT_IN_RETURN_ADDRESS:
6142 return expand_builtin_frame_address (fndecl, exp);
6144 /* Returns the address of the area where the structure is returned.
6145 0 otherwise. */
6146 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6147 if (call_expr_nargs (exp) != 0
6148 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6149 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6150 return const0_rtx;
6151 else
6152 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6154 case BUILT_IN_ALLOCA:
6155 target = expand_builtin_alloca (exp, target);
6156 if (target)
6157 return target;
6158 break;
6160 case BUILT_IN_STACK_SAVE:
6161 return expand_stack_save ();
6163 case BUILT_IN_STACK_RESTORE:
6164 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6165 return const0_rtx;
6167 case BUILT_IN_BSWAP32:
6168 case BUILT_IN_BSWAP64:
6169 target = expand_builtin_bswap (exp, target, subtarget);
6171 if (target)
6172 return target;
6173 break;
6175 CASE_INT_FN (BUILT_IN_FFS):
6176 case BUILT_IN_FFSIMAX:
6177 target = expand_builtin_unop (target_mode, exp, target,
6178 subtarget, ffs_optab);
6179 if (target)
6180 return target;
6181 break;
6183 CASE_INT_FN (BUILT_IN_CLZ):
6184 case BUILT_IN_CLZIMAX:
6185 target = expand_builtin_unop (target_mode, exp, target,
6186 subtarget, clz_optab);
6187 if (target)
6188 return target;
6189 break;
6191 CASE_INT_FN (BUILT_IN_CTZ):
6192 case BUILT_IN_CTZIMAX:
6193 target = expand_builtin_unop (target_mode, exp, target,
6194 subtarget, ctz_optab);
6195 if (target)
6196 return target;
6197 break;
6199 CASE_INT_FN (BUILT_IN_POPCOUNT):
6200 case BUILT_IN_POPCOUNTIMAX:
6201 target = expand_builtin_unop (target_mode, exp, target,
6202 subtarget, popcount_optab);
6203 if (target)
6204 return target;
6205 break;
6207 CASE_INT_FN (BUILT_IN_PARITY):
6208 case BUILT_IN_PARITYIMAX:
6209 target = expand_builtin_unop (target_mode, exp, target,
6210 subtarget, parity_optab);
6211 if (target)
6212 return target;
6213 break;
6215 case BUILT_IN_STRLEN:
6216 target = expand_builtin_strlen (exp, target, target_mode);
6217 if (target)
6218 return target;
6219 break;
6221 case BUILT_IN_STRCPY:
6222 target = expand_builtin_strcpy (exp, target);
6223 if (target)
6224 return target;
6225 break;
6227 case BUILT_IN_STRNCPY:
6228 target = expand_builtin_strncpy (exp, target);
6229 if (target)
6230 return target;
6231 break;
6233 case BUILT_IN_STPCPY:
6234 target = expand_builtin_stpcpy (exp, target, mode);
6235 if (target)
6236 return target;
6237 break;
6239 case BUILT_IN_MEMCPY:
6240 target = expand_builtin_memcpy (exp, target);
6241 if (target)
6242 return target;
6243 break;
6245 case BUILT_IN_MEMPCPY:
6246 target = expand_builtin_mempcpy (exp, target, mode);
6247 if (target)
6248 return target;
6249 break;
6251 case BUILT_IN_MEMSET:
6252 target = expand_builtin_memset (exp, target, mode);
6253 if (target)
6254 return target;
6255 break;
6257 case BUILT_IN_BZERO:
6258 target = expand_builtin_bzero (exp);
6259 if (target)
6260 return target;
6261 break;
6263 case BUILT_IN_STRCMP:
6264 target = expand_builtin_strcmp (exp, target);
6265 if (target)
6266 return target;
6267 break;
6269 case BUILT_IN_STRNCMP:
6270 target = expand_builtin_strncmp (exp, target, mode);
6271 if (target)
6272 return target;
6273 break;
6275 case BUILT_IN_BCMP:
6276 case BUILT_IN_MEMCMP:
6277 target = expand_builtin_memcmp (exp, target, mode);
6278 if (target)
6279 return target;
6280 break;
6282 case BUILT_IN_SETJMP:
6283 /* This should have been lowered to the builtins below. */
6284 gcc_unreachable ();
6286 case BUILT_IN_SETJMP_SETUP:
6287 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6288 and the receiver label. */
6289 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6291 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6292 VOIDmode, EXPAND_NORMAL);
6293 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6294 rtx label_r = label_rtx (label);
6296 /* This is copied from the handling of non-local gotos. */
6297 expand_builtin_setjmp_setup (buf_addr, label_r);
6298 nonlocal_goto_handler_labels
6299 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6300 nonlocal_goto_handler_labels);
6301 /* ??? Do not let expand_label treat us as such since we would
6302 not want to be both on the list of non-local labels and on
6303 the list of forced labels. */
6304 FORCED_LABEL (label) = 0;
6305 return const0_rtx;
6307 break;
6309 case BUILT_IN_SETJMP_DISPATCHER:
6310 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6311 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6313 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6314 rtx label_r = label_rtx (label);
6316 /* Remove the dispatcher label from the list of non-local labels
6317 since the receiver labels have been added to it above. */
6318 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6319 return const0_rtx;
6321 break;
6323 case BUILT_IN_SETJMP_RECEIVER:
6324 /* __builtin_setjmp_receiver is passed the receiver label. */
6325 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6327 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6328 rtx label_r = label_rtx (label);
6330 expand_builtin_setjmp_receiver (label_r);
6331 return const0_rtx;
6333 break;
6335 /* __builtin_longjmp is passed a pointer to an array of five words.
6336 It's similar to the C library longjmp function but works with
6337 __builtin_setjmp above. */
6338 case BUILT_IN_LONGJMP:
6339 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6341 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6342 VOIDmode, EXPAND_NORMAL);
6343 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6345 if (value != const1_rtx)
6347 error ("%<__builtin_longjmp%> second argument must be 1");
6348 return const0_rtx;
6351 expand_builtin_longjmp (buf_addr, value);
6352 return const0_rtx;
6354 break;
6356 case BUILT_IN_NONLOCAL_GOTO:
6357 target = expand_builtin_nonlocal_goto (exp);
6358 if (target)
6359 return target;
6360 break;
6362 /* This updates the setjmp buffer that is its argument with the value
6363 of the current stack pointer. */
6364 case BUILT_IN_UPDATE_SETJMP_BUF:
6365 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6367 rtx buf_addr
6368 = expand_normal (CALL_EXPR_ARG (exp, 0));
6370 expand_builtin_update_setjmp_buf (buf_addr);
6371 return const0_rtx;
6373 break;
6375 case BUILT_IN_TRAP:
6376 expand_builtin_trap ();
6377 return const0_rtx;
6379 case BUILT_IN_UNREACHABLE:
6380 expand_builtin_unreachable ();
6381 return const0_rtx;
6383 case BUILT_IN_PRINTF:
6384 target = expand_builtin_printf (exp, target, mode, false);
6385 if (target)
6386 return target;
6387 break;
6389 case BUILT_IN_PRINTF_UNLOCKED:
6390 target = expand_builtin_printf (exp, target, mode, true);
6391 if (target)
6392 return target;
6393 break;
6395 case BUILT_IN_FPRINTF:
6396 target = expand_builtin_fprintf (exp, target, mode, false);
6397 if (target)
6398 return target;
6399 break;
6401 case BUILT_IN_FPRINTF_UNLOCKED:
6402 target = expand_builtin_fprintf (exp, target, mode, true);
6403 if (target)
6404 return target;
6405 break;
6407 case BUILT_IN_SPRINTF:
6408 target = expand_builtin_sprintf (exp, target, mode);
6409 if (target)
6410 return target;
6411 break;
6413 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6414 case BUILT_IN_SIGNBITD32:
6415 case BUILT_IN_SIGNBITD64:
6416 case BUILT_IN_SIGNBITD128:
6417 target = expand_builtin_signbit (exp, target);
6418 if (target)
6419 return target;
6420 break;
6422 /* Various hooks for the DWARF 2 __throw routine. */
6423 case BUILT_IN_UNWIND_INIT:
6424 expand_builtin_unwind_init ();
6425 return const0_rtx;
6426 case BUILT_IN_DWARF_CFA:
6427 return virtual_cfa_rtx;
6428 #ifdef DWARF2_UNWIND_INFO
6429 case BUILT_IN_DWARF_SP_COLUMN:
6430 return expand_builtin_dwarf_sp_column ();
6431 case BUILT_IN_INIT_DWARF_REG_SIZES:
6432 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6433 return const0_rtx;
6434 #endif
6435 case BUILT_IN_FROB_RETURN_ADDR:
6436 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6437 case BUILT_IN_EXTRACT_RETURN_ADDR:
6438 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6439 case BUILT_IN_EH_RETURN:
6440 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6441 CALL_EXPR_ARG (exp, 1));
6442 return const0_rtx;
6443 #ifdef EH_RETURN_DATA_REGNO
6444 case BUILT_IN_EH_RETURN_DATA_REGNO:
6445 return expand_builtin_eh_return_data_regno (exp);
6446 #endif
6447 case BUILT_IN_EXTEND_POINTER:
6448 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6449 case BUILT_IN_EH_POINTER:
6450 return expand_builtin_eh_pointer (exp);
6451 case BUILT_IN_EH_FILTER:
6452 return expand_builtin_eh_filter (exp);
6453 case BUILT_IN_EH_COPY_VALUES:
6454 return expand_builtin_eh_copy_values (exp);
6456 case BUILT_IN_VA_START:
6457 return expand_builtin_va_start (exp);
6458 case BUILT_IN_VA_END:
6459 return expand_builtin_va_end (exp);
6460 case BUILT_IN_VA_COPY:
6461 return expand_builtin_va_copy (exp);
6462 case BUILT_IN_EXPECT:
6463 return expand_builtin_expect (exp, target);
6464 case BUILT_IN_PREFETCH:
6465 expand_builtin_prefetch (exp);
6466 return const0_rtx;
6468 case BUILT_IN_PROFILE_FUNC_ENTER:
6469 return expand_builtin_profile_func (false);
6470 case BUILT_IN_PROFILE_FUNC_EXIT:
6471 return expand_builtin_profile_func (true);
6473 case BUILT_IN_INIT_TRAMPOLINE:
6474 return expand_builtin_init_trampoline (exp);
6475 case BUILT_IN_ADJUST_TRAMPOLINE:
6476 return expand_builtin_adjust_trampoline (exp);
6478 case BUILT_IN_FORK:
6479 case BUILT_IN_EXECL:
6480 case BUILT_IN_EXECV:
6481 case BUILT_IN_EXECLP:
6482 case BUILT_IN_EXECLE:
6483 case BUILT_IN_EXECVP:
6484 case BUILT_IN_EXECVE:
6485 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6486 if (target)
6487 return target;
6488 break;
6490 case BUILT_IN_FETCH_AND_ADD_1:
6491 case BUILT_IN_FETCH_AND_ADD_2:
6492 case BUILT_IN_FETCH_AND_ADD_4:
6493 case BUILT_IN_FETCH_AND_ADD_8:
6494 case BUILT_IN_FETCH_AND_ADD_16:
6495 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6496 target = expand_builtin_sync_operation (mode, exp, PLUS,
6497 false, target, ignore);
6498 if (target)
6499 return target;
6500 break;
6502 case BUILT_IN_FETCH_AND_SUB_1:
6503 case BUILT_IN_FETCH_AND_SUB_2:
6504 case BUILT_IN_FETCH_AND_SUB_4:
6505 case BUILT_IN_FETCH_AND_SUB_8:
6506 case BUILT_IN_FETCH_AND_SUB_16:
6507 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6508 target = expand_builtin_sync_operation (mode, exp, MINUS,
6509 false, target, ignore);
6510 if (target)
6511 return target;
6512 break;
6514 case BUILT_IN_FETCH_AND_OR_1:
6515 case BUILT_IN_FETCH_AND_OR_2:
6516 case BUILT_IN_FETCH_AND_OR_4:
6517 case BUILT_IN_FETCH_AND_OR_8:
6518 case BUILT_IN_FETCH_AND_OR_16:
6519 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6520 target = expand_builtin_sync_operation (mode, exp, IOR,
6521 false, target, ignore);
6522 if (target)
6523 return target;
6524 break;
6526 case BUILT_IN_FETCH_AND_AND_1:
6527 case BUILT_IN_FETCH_AND_AND_2:
6528 case BUILT_IN_FETCH_AND_AND_4:
6529 case BUILT_IN_FETCH_AND_AND_8:
6530 case BUILT_IN_FETCH_AND_AND_16:
6531 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6532 target = expand_builtin_sync_operation (mode, exp, AND,
6533 false, target, ignore);
6534 if (target)
6535 return target;
6536 break;
6538 case BUILT_IN_FETCH_AND_XOR_1:
6539 case BUILT_IN_FETCH_AND_XOR_2:
6540 case BUILT_IN_FETCH_AND_XOR_4:
6541 case BUILT_IN_FETCH_AND_XOR_8:
6542 case BUILT_IN_FETCH_AND_XOR_16:
6543 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6544 target = expand_builtin_sync_operation (mode, exp, XOR,
6545 false, target, ignore);
6546 if (target)
6547 return target;
6548 break;
6550 case BUILT_IN_FETCH_AND_NAND_1:
6551 case BUILT_IN_FETCH_AND_NAND_2:
6552 case BUILT_IN_FETCH_AND_NAND_4:
6553 case BUILT_IN_FETCH_AND_NAND_8:
6554 case BUILT_IN_FETCH_AND_NAND_16:
6555 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6556 target = expand_builtin_sync_operation (mode, exp, NOT,
6557 false, target, ignore);
6558 if (target)
6559 return target;
6560 break;
6562 case BUILT_IN_ADD_AND_FETCH_1:
6563 case BUILT_IN_ADD_AND_FETCH_2:
6564 case BUILT_IN_ADD_AND_FETCH_4:
6565 case BUILT_IN_ADD_AND_FETCH_8:
6566 case BUILT_IN_ADD_AND_FETCH_16:
6567 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6568 target = expand_builtin_sync_operation (mode, exp, PLUS,
6569 true, target, ignore);
6570 if (target)
6571 return target;
6572 break;
6574 case BUILT_IN_SUB_AND_FETCH_1:
6575 case BUILT_IN_SUB_AND_FETCH_2:
6576 case BUILT_IN_SUB_AND_FETCH_4:
6577 case BUILT_IN_SUB_AND_FETCH_8:
6578 case BUILT_IN_SUB_AND_FETCH_16:
6579 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6580 target = expand_builtin_sync_operation (mode, exp, MINUS,
6581 true, target, ignore);
6582 if (target)
6583 return target;
6584 break;
6586 case BUILT_IN_OR_AND_FETCH_1:
6587 case BUILT_IN_OR_AND_FETCH_2:
6588 case BUILT_IN_OR_AND_FETCH_4:
6589 case BUILT_IN_OR_AND_FETCH_8:
6590 case BUILT_IN_OR_AND_FETCH_16:
6591 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6592 target = expand_builtin_sync_operation (mode, exp, IOR,
6593 true, target, ignore);
6594 if (target)
6595 return target;
6596 break;
6598 case BUILT_IN_AND_AND_FETCH_1:
6599 case BUILT_IN_AND_AND_FETCH_2:
6600 case BUILT_IN_AND_AND_FETCH_4:
6601 case BUILT_IN_AND_AND_FETCH_8:
6602 case BUILT_IN_AND_AND_FETCH_16:
6603 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6604 target = expand_builtin_sync_operation (mode, exp, AND,
6605 true, target, ignore);
6606 if (target)
6607 return target;
6608 break;
6610 case BUILT_IN_XOR_AND_FETCH_1:
6611 case BUILT_IN_XOR_AND_FETCH_2:
6612 case BUILT_IN_XOR_AND_FETCH_4:
6613 case BUILT_IN_XOR_AND_FETCH_8:
6614 case BUILT_IN_XOR_AND_FETCH_16:
6615 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6616 target = expand_builtin_sync_operation (mode, exp, XOR,
6617 true, target, ignore);
6618 if (target)
6619 return target;
6620 break;
6622 case BUILT_IN_NAND_AND_FETCH_1:
6623 case BUILT_IN_NAND_AND_FETCH_2:
6624 case BUILT_IN_NAND_AND_FETCH_4:
6625 case BUILT_IN_NAND_AND_FETCH_8:
6626 case BUILT_IN_NAND_AND_FETCH_16:
6627 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6628 target = expand_builtin_sync_operation (mode, exp, NOT,
6629 true, target, ignore);
6630 if (target)
6631 return target;
6632 break;
6634 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6635 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6636 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6637 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6638 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6639 if (mode == VOIDmode)
6640 mode = TYPE_MODE (boolean_type_node);
6641 if (!target || !register_operand (target, mode))
6642 target = gen_reg_rtx (mode);
6644 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6645 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6646 if (target)
6647 return target;
6648 break;
6650 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6651 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6652 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6653 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6654 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6655 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6656 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6657 if (target)
6658 return target;
6659 break;
6661 case BUILT_IN_LOCK_TEST_AND_SET_1:
6662 case BUILT_IN_LOCK_TEST_AND_SET_2:
6663 case BUILT_IN_LOCK_TEST_AND_SET_4:
6664 case BUILT_IN_LOCK_TEST_AND_SET_8:
6665 case BUILT_IN_LOCK_TEST_AND_SET_16:
6666 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6667 target = expand_builtin_lock_test_and_set (mode, exp, target);
6668 if (target)
6669 return target;
6670 break;
6672 case BUILT_IN_LOCK_RELEASE_1:
6673 case BUILT_IN_LOCK_RELEASE_2:
6674 case BUILT_IN_LOCK_RELEASE_4:
6675 case BUILT_IN_LOCK_RELEASE_8:
6676 case BUILT_IN_LOCK_RELEASE_16:
6677 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6678 expand_builtin_lock_release (mode, exp);
6679 return const0_rtx;
6681 case BUILT_IN_SYNCHRONIZE:
6682 expand_builtin_synchronize ();
6683 return const0_rtx;
6685 case BUILT_IN_OBJECT_SIZE:
6686 return expand_builtin_object_size (exp);
6688 case BUILT_IN_MEMCPY_CHK:
6689 case BUILT_IN_MEMPCPY_CHK:
6690 case BUILT_IN_MEMMOVE_CHK:
6691 case BUILT_IN_MEMSET_CHK:
6692 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6693 if (target)
6694 return target;
6695 break;
6697 case BUILT_IN_STRCPY_CHK:
6698 case BUILT_IN_STPCPY_CHK:
6699 case BUILT_IN_STRNCPY_CHK:
6700 case BUILT_IN_STRCAT_CHK:
6701 case BUILT_IN_STRNCAT_CHK:
6702 case BUILT_IN_SNPRINTF_CHK:
6703 case BUILT_IN_VSNPRINTF_CHK:
6704 maybe_emit_chk_warning (exp, fcode);
6705 break;
6707 case BUILT_IN_SPRINTF_CHK:
6708 case BUILT_IN_VSPRINTF_CHK:
6709 maybe_emit_sprintf_chk_warning (exp, fcode);
6710 break;
6712 case BUILT_IN_FREE:
6713 maybe_emit_free_warning (exp);
6714 break;
6716 default: /* just do library call, if unknown builtin */
6717 break;
6720 /* The switch statement above can drop through to cause the function
6721 to be called normally. */
6722 return expand_call (exp, target, ignore);
6725 /* Determine whether a tree node represents a call to a built-in
6726 function. If the tree T is a call to a built-in function with
6727 the right number of arguments of the appropriate types, return
6728 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6729 Otherwise the return value is END_BUILTINS. */
6731 enum built_in_function
6732 builtin_mathfn_code (const_tree t)
6734 const_tree fndecl, arg, parmlist;
6735 const_tree argtype, parmtype;
6736 const_call_expr_arg_iterator iter;
6738 if (TREE_CODE (t) != CALL_EXPR
6739 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6740 return END_BUILTINS;
6742 fndecl = get_callee_fndecl (t);
6743 if (fndecl == NULL_TREE
6744 || TREE_CODE (fndecl) != FUNCTION_DECL
6745 || ! DECL_BUILT_IN (fndecl)
6746 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6747 return END_BUILTINS;
6749 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6750 init_const_call_expr_arg_iterator (t, &iter);
6751 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6753 /* If a function doesn't take a variable number of arguments,
6754 the last element in the list will have type `void'. */
6755 parmtype = TREE_VALUE (parmlist);
6756 if (VOID_TYPE_P (parmtype))
6758 if (more_const_call_expr_args_p (&iter))
6759 return END_BUILTINS;
6760 return DECL_FUNCTION_CODE (fndecl);
6763 if (! more_const_call_expr_args_p (&iter))
6764 return END_BUILTINS;
6766 arg = next_const_call_expr_arg (&iter);
6767 argtype = TREE_TYPE (arg);
6769 if (SCALAR_FLOAT_TYPE_P (parmtype))
6771 if (! SCALAR_FLOAT_TYPE_P (argtype))
6772 return END_BUILTINS;
6774 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6776 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6777 return END_BUILTINS;
6779 else if (POINTER_TYPE_P (parmtype))
6781 if (! POINTER_TYPE_P (argtype))
6782 return END_BUILTINS;
6784 else if (INTEGRAL_TYPE_P (parmtype))
6786 if (! INTEGRAL_TYPE_P (argtype))
6787 return END_BUILTINS;
6789 else
6790 return END_BUILTINS;
6793 /* Variable-length argument list. */
6794 return DECL_FUNCTION_CODE (fndecl);
6797 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6798 evaluate to a constant. */
6800 static tree
6801 fold_builtin_constant_p (tree arg)
6803 /* We return 1 for a numeric type that's known to be a constant
6804 value at compile-time or for an aggregate type that's a
6805 literal constant. */
6806 STRIP_NOPS (arg);
6808 /* If we know this is a constant, emit the constant of one. */
6809 if (CONSTANT_CLASS_P (arg)
6810 || (TREE_CODE (arg) == CONSTRUCTOR
6811 && TREE_CONSTANT (arg)))
6812 return integer_one_node;
6813 if (TREE_CODE (arg) == ADDR_EXPR)
6815 tree op = TREE_OPERAND (arg, 0);
6816 if (TREE_CODE (op) == STRING_CST
6817 || (TREE_CODE (op) == ARRAY_REF
6818 && integer_zerop (TREE_OPERAND (op, 1))
6819 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6820 return integer_one_node;
6823 /* If this expression has side effects, show we don't know it to be a
6824 constant. Likewise if it's a pointer or aggregate type since in
6825 those case we only want literals, since those are only optimized
6826 when generating RTL, not later.
6827 And finally, if we are compiling an initializer, not code, we
6828 need to return a definite result now; there's not going to be any
6829 more optimization done. */
6830 if (TREE_SIDE_EFFECTS (arg)
6831 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6832 || POINTER_TYPE_P (TREE_TYPE (arg))
6833 || cfun == 0
6834 || folding_initializer)
6835 return integer_zero_node;
6837 return NULL_TREE;
6840 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6841 return it as a truthvalue. */
6843 static tree
6844 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6846 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6848 fn = built_in_decls[BUILT_IN_EXPECT];
6849 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6850 ret_type = TREE_TYPE (TREE_TYPE (fn));
6851 pred_type = TREE_VALUE (arg_types);
6852 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6854 pred = fold_convert_loc (loc, pred_type, pred);
6855 expected = fold_convert_loc (loc, expected_type, expected);
6856 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6858 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6859 build_int_cst (ret_type, 0));
6862 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6863 NULL_TREE if no simplification is possible. */
6865 static tree
6866 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6868 tree inner, fndecl;
6869 enum tree_code code;
6871 /* If this is a builtin_expect within a builtin_expect keep the
6872 inner one. See through a comparison against a constant. It
6873 might have been added to create a thruthvalue. */
6874 inner = arg0;
6875 if (COMPARISON_CLASS_P (inner)
6876 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6877 inner = TREE_OPERAND (inner, 0);
6879 if (TREE_CODE (inner) == CALL_EXPR
6880 && (fndecl = get_callee_fndecl (inner))
6881 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6882 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6883 return arg0;
6885 /* Distribute the expected value over short-circuiting operators.
6886 See through the cast from truthvalue_type_node to long. */
6887 inner = arg0;
6888 while (TREE_CODE (inner) == NOP_EXPR
6889 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6890 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6891 inner = TREE_OPERAND (inner, 0);
6893 code = TREE_CODE (inner);
6894 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6896 tree op0 = TREE_OPERAND (inner, 0);
6897 tree op1 = TREE_OPERAND (inner, 1);
6899 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6900 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6901 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6903 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6906 /* If the argument isn't invariant then there's nothing else we can do. */
6907 if (!TREE_CONSTANT (arg0))
6908 return NULL_TREE;
6910 /* If we expect that a comparison against the argument will fold to
6911 a constant return the constant. In practice, this means a true
6912 constant or the address of a non-weak symbol. */
6913 inner = arg0;
6914 STRIP_NOPS (inner);
6915 if (TREE_CODE (inner) == ADDR_EXPR)
6919 inner = TREE_OPERAND (inner, 0);
6921 while (TREE_CODE (inner) == COMPONENT_REF
6922 || TREE_CODE (inner) == ARRAY_REF);
6923 if ((TREE_CODE (inner) == VAR_DECL
6924 || TREE_CODE (inner) == FUNCTION_DECL)
6925 && DECL_WEAK (inner))
6926 return NULL_TREE;
6929 /* Otherwise, ARG0 already has the proper type for the return value. */
6930 return arg0;
6933 /* Fold a call to __builtin_classify_type with argument ARG. */
6935 static tree
6936 fold_builtin_classify_type (tree arg)
6938 if (arg == 0)
6939 return build_int_cst (NULL_TREE, no_type_class);
6941 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
6944 /* Fold a call to __builtin_strlen with argument ARG. */
6946 static tree
6947 fold_builtin_strlen (location_t loc, tree arg)
6949 if (!validate_arg (arg, POINTER_TYPE))
6950 return NULL_TREE;
6951 else
6953 tree len = c_strlen (arg, 0);
6955 if (len)
6957 /* Convert from the internal "sizetype" type to "size_t". */
6958 if (size_type_node)
6959 len = fold_convert_loc (loc, size_type_node, len);
6960 return len;
6963 return NULL_TREE;
6967 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6969 static tree
6970 fold_builtin_inf (location_t loc, tree type, int warn)
6972 REAL_VALUE_TYPE real;
6974 /* __builtin_inff is intended to be usable to define INFINITY on all
6975 targets. If an infinity is not available, INFINITY expands "to a
6976 positive constant of type float that overflows at translation
6977 time", footnote "In this case, using INFINITY will violate the
6978 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6979 Thus we pedwarn to ensure this constraint violation is
6980 diagnosed. */
6981 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6982 pedwarn (loc, 0, "target format does not support infinity");
6984 real_inf (&real);
6985 return build_real (type, real);
6988 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6990 static tree
6991 fold_builtin_nan (tree arg, tree type, int quiet)
6993 REAL_VALUE_TYPE real;
6994 const char *str;
6996 if (!validate_arg (arg, POINTER_TYPE))
6997 return NULL_TREE;
6998 str = c_getstr (arg);
6999 if (!str)
7000 return NULL_TREE;
7002 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7003 return NULL_TREE;
7005 return build_real (type, real);
7008 /* Return true if the floating point expression T has an integer value.
7009 We also allow +Inf, -Inf and NaN to be considered integer values. */
7011 static bool
7012 integer_valued_real_p (tree t)
7014 switch (TREE_CODE (t))
7016 case FLOAT_EXPR:
7017 return true;
7019 case ABS_EXPR:
7020 case SAVE_EXPR:
7021 return integer_valued_real_p (TREE_OPERAND (t, 0));
7023 case COMPOUND_EXPR:
7024 case MODIFY_EXPR:
7025 case BIND_EXPR:
7026 return integer_valued_real_p (TREE_OPERAND (t, 1));
7028 case PLUS_EXPR:
7029 case MINUS_EXPR:
7030 case MULT_EXPR:
7031 case MIN_EXPR:
7032 case MAX_EXPR:
7033 return integer_valued_real_p (TREE_OPERAND (t, 0))
7034 && integer_valued_real_p (TREE_OPERAND (t, 1));
7036 case COND_EXPR:
7037 return integer_valued_real_p (TREE_OPERAND (t, 1))
7038 && integer_valued_real_p (TREE_OPERAND (t, 2));
7040 case REAL_CST:
7041 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7043 case NOP_EXPR:
7045 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7046 if (TREE_CODE (type) == INTEGER_TYPE)
7047 return true;
7048 if (TREE_CODE (type) == REAL_TYPE)
7049 return integer_valued_real_p (TREE_OPERAND (t, 0));
7050 break;
7053 case CALL_EXPR:
7054 switch (builtin_mathfn_code (t))
7056 CASE_FLT_FN (BUILT_IN_CEIL):
7057 CASE_FLT_FN (BUILT_IN_FLOOR):
7058 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7059 CASE_FLT_FN (BUILT_IN_RINT):
7060 CASE_FLT_FN (BUILT_IN_ROUND):
7061 CASE_FLT_FN (BUILT_IN_TRUNC):
7062 return true;
7064 CASE_FLT_FN (BUILT_IN_FMIN):
7065 CASE_FLT_FN (BUILT_IN_FMAX):
7066 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7067 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7069 default:
7070 break;
7072 break;
7074 default:
7075 break;
7077 return false;
7080 /* FNDECL is assumed to be a builtin where truncation can be propagated
7081 across (for instance floor((double)f) == (double)floorf (f).
7082 Do the transformation for a call with argument ARG. */
7084 static tree
7085 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7087 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7089 if (!validate_arg (arg, REAL_TYPE))
7090 return NULL_TREE;
7092 /* Integer rounding functions are idempotent. */
7093 if (fcode == builtin_mathfn_code (arg))
7094 return arg;
7096 /* If argument is already integer valued, and we don't need to worry
7097 about setting errno, there's no need to perform rounding. */
7098 if (! flag_errno_math && integer_valued_real_p (arg))
7099 return arg;
7101 if (optimize)
7103 tree arg0 = strip_float_extensions (arg);
7104 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7105 tree newtype = TREE_TYPE (arg0);
7106 tree decl;
7108 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7109 && (decl = mathfn_built_in (newtype, fcode)))
7110 return fold_convert_loc (loc, ftype,
7111 build_call_expr_loc (loc, decl, 1,
7112 fold_convert_loc (loc,
7113 newtype,
7114 arg0)));
7116 return NULL_TREE;
7119 /* FNDECL is assumed to be builtin which can narrow the FP type of
7120 the argument, for instance lround((double)f) -> lroundf (f).
7121 Do the transformation for a call with argument ARG. */
7123 static tree
7124 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7126 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7128 if (!validate_arg (arg, REAL_TYPE))
7129 return NULL_TREE;
7131 /* If argument is already integer valued, and we don't need to worry
7132 about setting errno, there's no need to perform rounding. */
7133 if (! flag_errno_math && integer_valued_real_p (arg))
7134 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7135 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7137 if (optimize)
7139 tree ftype = TREE_TYPE (arg);
7140 tree arg0 = strip_float_extensions (arg);
7141 tree newtype = TREE_TYPE (arg0);
7142 tree decl;
7144 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7145 && (decl = mathfn_built_in (newtype, fcode)))
7146 return build_call_expr_loc (loc, decl, 1,
7147 fold_convert_loc (loc, newtype, arg0));
7150 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7151 sizeof (long long) == sizeof (long). */
7152 if (TYPE_PRECISION (long_long_integer_type_node)
7153 == TYPE_PRECISION (long_integer_type_node))
7155 tree newfn = NULL_TREE;
7156 switch (fcode)
7158 CASE_FLT_FN (BUILT_IN_LLCEIL):
7159 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7160 break;
7162 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7163 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7164 break;
7166 CASE_FLT_FN (BUILT_IN_LLROUND):
7167 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7168 break;
7170 CASE_FLT_FN (BUILT_IN_LLRINT):
7171 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7172 break;
7174 default:
7175 break;
7178 if (newfn)
7180 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7181 return fold_convert_loc (loc,
7182 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7186 return NULL_TREE;
7189 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7190 return type. Return NULL_TREE if no simplification can be made. */
7192 static tree
7193 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7195 tree res;
7197 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7198 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7199 return NULL_TREE;
7201 /* Calculate the result when the argument is a constant. */
7202 if (TREE_CODE (arg) == COMPLEX_CST
7203 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7204 type, mpfr_hypot)))
7205 return res;
7207 if (TREE_CODE (arg) == COMPLEX_EXPR)
7209 tree real = TREE_OPERAND (arg, 0);
7210 tree imag = TREE_OPERAND (arg, 1);
7212 /* If either part is zero, cabs is fabs of the other. */
7213 if (real_zerop (real))
7214 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7215 if (real_zerop (imag))
7216 return fold_build1_loc (loc, ABS_EXPR, type, real);
7218 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7219 if (flag_unsafe_math_optimizations
7220 && operand_equal_p (real, imag, OEP_PURE_SAME))
7222 const REAL_VALUE_TYPE sqrt2_trunc
7223 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7224 STRIP_NOPS (real);
7225 return fold_build2_loc (loc, MULT_EXPR, type,
7226 fold_build1_loc (loc, ABS_EXPR, type, real),
7227 build_real (type, sqrt2_trunc));
7231 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7232 if (TREE_CODE (arg) == NEGATE_EXPR
7233 || TREE_CODE (arg) == CONJ_EXPR)
7234 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7236 /* Don't do this when optimizing for size. */
7237 if (flag_unsafe_math_optimizations
7238 && optimize && optimize_function_for_speed_p (cfun))
7240 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7242 if (sqrtfn != NULL_TREE)
7244 tree rpart, ipart, result;
7246 arg = builtin_save_expr (arg);
7248 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7249 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7251 rpart = builtin_save_expr (rpart);
7252 ipart = builtin_save_expr (ipart);
7254 result = fold_build2_loc (loc, PLUS_EXPR, type,
7255 fold_build2_loc (loc, MULT_EXPR, type,
7256 rpart, rpart),
7257 fold_build2_loc (loc, MULT_EXPR, type,
7258 ipart, ipart));
7260 return build_call_expr_loc (loc, sqrtfn, 1, result);
7264 return NULL_TREE;
7267 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7268 Return NULL_TREE if no simplification can be made. */
7270 static tree
7271 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7274 enum built_in_function fcode;
7275 tree res;
7277 if (!validate_arg (arg, REAL_TYPE))
7278 return NULL_TREE;
7280 /* Calculate the result when the argument is a constant. */
7281 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7282 return res;
7284 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7285 fcode = builtin_mathfn_code (arg);
7286 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7288 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7289 arg = fold_build2_loc (loc, MULT_EXPR, type,
7290 CALL_EXPR_ARG (arg, 0),
7291 build_real (type, dconsthalf));
7292 return build_call_expr_loc (loc, expfn, 1, arg);
7295 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7296 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7298 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7300 if (powfn)
7302 tree arg0 = CALL_EXPR_ARG (arg, 0);
7303 tree tree_root;
7304 /* The inner root was either sqrt or cbrt. */
7305 /* This was a conditional expression but it triggered a bug
7306 in Sun C 5.5. */
7307 REAL_VALUE_TYPE dconstroot;
7308 if (BUILTIN_SQRT_P (fcode))
7309 dconstroot = dconsthalf;
7310 else
7311 dconstroot = dconst_third ();
7313 /* Adjust for the outer root. */
7314 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7315 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7316 tree_root = build_real (type, dconstroot);
7317 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7321 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7322 if (flag_unsafe_math_optimizations
7323 && (fcode == BUILT_IN_POW
7324 || fcode == BUILT_IN_POWF
7325 || fcode == BUILT_IN_POWL))
7327 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7328 tree arg0 = CALL_EXPR_ARG (arg, 0);
7329 tree arg1 = CALL_EXPR_ARG (arg, 1);
7330 tree narg1;
7331 if (!tree_expr_nonnegative_p (arg0))
7332 arg0 = build1 (ABS_EXPR, type, arg0);
7333 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7334 build_real (type, dconsthalf));
7335 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7338 return NULL_TREE;
7341 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7342 Return NULL_TREE if no simplification can be made. */
7344 static tree
7345 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7347 const enum built_in_function fcode = builtin_mathfn_code (arg);
7348 tree res;
7350 if (!validate_arg (arg, REAL_TYPE))
7351 return NULL_TREE;
7353 /* Calculate the result when the argument is a constant. */
7354 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7355 return res;
7357 if (flag_unsafe_math_optimizations)
7359 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7360 if (BUILTIN_EXPONENT_P (fcode))
7362 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7363 const REAL_VALUE_TYPE third_trunc =
7364 real_value_truncate (TYPE_MODE (type), dconst_third ());
7365 arg = fold_build2_loc (loc, MULT_EXPR, type,
7366 CALL_EXPR_ARG (arg, 0),
7367 build_real (type, third_trunc));
7368 return build_call_expr_loc (loc, expfn, 1, arg);
7371 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7372 if (BUILTIN_SQRT_P (fcode))
7374 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7376 if (powfn)
7378 tree arg0 = CALL_EXPR_ARG (arg, 0);
7379 tree tree_root;
7380 REAL_VALUE_TYPE dconstroot = dconst_third ();
7382 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7383 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7384 tree_root = build_real (type, dconstroot);
7385 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7389 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7390 if (BUILTIN_CBRT_P (fcode))
7392 tree arg0 = CALL_EXPR_ARG (arg, 0);
7393 if (tree_expr_nonnegative_p (arg0))
7395 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7397 if (powfn)
7399 tree tree_root;
7400 REAL_VALUE_TYPE dconstroot;
7402 real_arithmetic (&dconstroot, MULT_EXPR,
7403 dconst_third_ptr (), dconst_third_ptr ());
7404 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7405 tree_root = build_real (type, dconstroot);
7406 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7411 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7412 if (fcode == BUILT_IN_POW
7413 || fcode == BUILT_IN_POWF
7414 || fcode == BUILT_IN_POWL)
7416 tree arg00 = CALL_EXPR_ARG (arg, 0);
7417 tree arg01 = CALL_EXPR_ARG (arg, 1);
7418 if (tree_expr_nonnegative_p (arg00))
7420 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7421 const REAL_VALUE_TYPE dconstroot
7422 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7423 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7424 build_real (type, dconstroot));
7425 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7429 return NULL_TREE;
7432 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7433 TYPE is the type of the return value. Return NULL_TREE if no
7434 simplification can be made. */
7436 static tree
7437 fold_builtin_cos (location_t loc,
7438 tree arg, tree type, tree fndecl)
7440 tree res, narg;
7442 if (!validate_arg (arg, REAL_TYPE))
7443 return NULL_TREE;
7445 /* Calculate the result when the argument is a constant. */
7446 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7447 return res;
7449 /* Optimize cos(-x) into cos (x). */
7450 if ((narg = fold_strip_sign_ops (arg)))
7451 return build_call_expr_loc (loc, fndecl, 1, narg);
7453 return NULL_TREE;
7456 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7457 Return NULL_TREE if no simplification can be made. */
7459 static tree
7460 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7462 if (validate_arg (arg, REAL_TYPE))
7464 tree res, narg;
7466 /* Calculate the result when the argument is a constant. */
7467 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7468 return res;
7470 /* Optimize cosh(-x) into cosh (x). */
7471 if ((narg = fold_strip_sign_ops (arg)))
7472 return build_call_expr_loc (loc, fndecl, 1, narg);
7475 return NULL_TREE;
7478 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7479 argument ARG. TYPE is the type of the return value. Return
7480 NULL_TREE if no simplification can be made. */
7482 static tree
7483 fold_builtin_ccos (location_t loc,
7484 tree arg, tree type ATTRIBUTE_UNUSED, tree fndecl,
7485 bool hyper ATTRIBUTE_UNUSED)
7487 if (validate_arg (arg, COMPLEX_TYPE)
7488 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7490 tree tmp;
7492 #ifdef HAVE_mpc
7493 /* Calculate the result when the argument is a constant. */
7494 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7495 return tmp;
7496 #endif
7498 /* Optimize fn(-x) into fn(x). */
7499 if ((tmp = fold_strip_sign_ops (arg)))
7500 return build_call_expr_loc (loc, fndecl, 1, tmp);
7503 return NULL_TREE;
7506 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7507 Return NULL_TREE if no simplification can be made. */
7509 static tree
7510 fold_builtin_tan (tree arg, tree type)
7512 enum built_in_function fcode;
7513 tree res;
7515 if (!validate_arg (arg, REAL_TYPE))
7516 return NULL_TREE;
7518 /* Calculate the result when the argument is a constant. */
7519 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7520 return res;
7522 /* Optimize tan(atan(x)) = x. */
7523 fcode = builtin_mathfn_code (arg);
7524 if (flag_unsafe_math_optimizations
7525 && (fcode == BUILT_IN_ATAN
7526 || fcode == BUILT_IN_ATANF
7527 || fcode == BUILT_IN_ATANL))
7528 return CALL_EXPR_ARG (arg, 0);
7530 return NULL_TREE;
7533 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7534 NULL_TREE if no simplification can be made. */
7536 static tree
7537 fold_builtin_sincos (location_t loc,
7538 tree arg0, tree arg1, tree arg2)
7540 tree type;
7541 tree res, fn, call;
7543 if (!validate_arg (arg0, REAL_TYPE)
7544 || !validate_arg (arg1, POINTER_TYPE)
7545 || !validate_arg (arg2, POINTER_TYPE))
7546 return NULL_TREE;
7548 type = TREE_TYPE (arg0);
7550 /* Calculate the result when the argument is a constant. */
7551 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7552 return res;
7554 /* Canonicalize sincos to cexpi. */
7555 if (!TARGET_C99_FUNCTIONS)
7556 return NULL_TREE;
7557 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7558 if (!fn)
7559 return NULL_TREE;
7561 call = build_call_expr_loc (loc, fn, 1, arg0);
7562 call = builtin_save_expr (call);
7564 return build2 (COMPOUND_EXPR, void_type_node,
7565 build2 (MODIFY_EXPR, void_type_node,
7566 build_fold_indirect_ref_loc (loc, arg1),
7567 build1 (IMAGPART_EXPR, type, call)),
7568 build2 (MODIFY_EXPR, void_type_node,
7569 build_fold_indirect_ref_loc (loc, arg2),
7570 build1 (REALPART_EXPR, type, call)));
7573 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7574 NULL_TREE if no simplification can be made. */
7576 static tree
7577 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7579 tree rtype;
7580 tree realp, imagp, ifn;
7581 #ifdef HAVE_mpc
7582 tree res;
7583 #endif
7585 if (!validate_arg (arg0, COMPLEX_TYPE)
7586 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
7587 return NULL_TREE;
7589 #ifdef HAVE_mpc
7590 /* Calculate the result when the argument is a constant. */
7591 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7592 return res;
7593 #endif
7595 rtype = TREE_TYPE (TREE_TYPE (arg0));
7597 /* In case we can figure out the real part of arg0 and it is constant zero
7598 fold to cexpi. */
7599 if (!TARGET_C99_FUNCTIONS)
7600 return NULL_TREE;
7601 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7602 if (!ifn)
7603 return NULL_TREE;
7605 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7606 && real_zerop (realp))
7608 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7609 return build_call_expr_loc (loc, ifn, 1, narg);
7612 /* In case we can easily decompose real and imaginary parts split cexp
7613 to exp (r) * cexpi (i). */
7614 if (flag_unsafe_math_optimizations
7615 && realp)
7617 tree rfn, rcall, icall;
7619 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7620 if (!rfn)
7621 return NULL_TREE;
7623 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7624 if (!imagp)
7625 return NULL_TREE;
7627 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7628 icall = builtin_save_expr (icall);
7629 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7630 rcall = builtin_save_expr (rcall);
7631 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7632 fold_build2_loc (loc, MULT_EXPR, rtype,
7633 rcall,
7634 fold_build1_loc (loc, REALPART_EXPR,
7635 rtype, icall)),
7636 fold_build2_loc (loc, MULT_EXPR, rtype,
7637 rcall,
7638 fold_build1_loc (loc, IMAGPART_EXPR,
7639 rtype, icall)));
7642 return NULL_TREE;
7645 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7646 Return NULL_TREE if no simplification can be made. */
7648 static tree
7649 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7651 if (!validate_arg (arg, REAL_TYPE))
7652 return NULL_TREE;
7654 /* Optimize trunc of constant value. */
7655 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7657 REAL_VALUE_TYPE r, x;
7658 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7660 x = TREE_REAL_CST (arg);
7661 real_trunc (&r, TYPE_MODE (type), &x);
7662 return build_real (type, r);
7665 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7668 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7669 Return NULL_TREE if no simplification can be made. */
7671 static tree
7672 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7674 if (!validate_arg (arg, REAL_TYPE))
7675 return NULL_TREE;
7677 /* Optimize floor of constant value. */
7678 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7680 REAL_VALUE_TYPE x;
7682 x = TREE_REAL_CST (arg);
7683 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7685 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7686 REAL_VALUE_TYPE r;
7688 real_floor (&r, TYPE_MODE (type), &x);
7689 return build_real (type, r);
7693 /* Fold floor (x) where x is nonnegative to trunc (x). */
7694 if (tree_expr_nonnegative_p (arg))
7696 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7697 if (truncfn)
7698 return build_call_expr_loc (loc, truncfn, 1, arg);
7701 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7704 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7705 Return NULL_TREE if no simplification can be made. */
7707 static tree
7708 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7710 if (!validate_arg (arg, REAL_TYPE))
7711 return NULL_TREE;
7713 /* Optimize ceil of constant value. */
7714 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7716 REAL_VALUE_TYPE x;
7718 x = TREE_REAL_CST (arg);
7719 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7721 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7722 REAL_VALUE_TYPE r;
7724 real_ceil (&r, TYPE_MODE (type), &x);
7725 return build_real (type, r);
7729 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7732 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7733 Return NULL_TREE if no simplification can be made. */
7735 static tree
7736 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7738 if (!validate_arg (arg, REAL_TYPE))
7739 return NULL_TREE;
7741 /* Optimize round of constant value. */
7742 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7744 REAL_VALUE_TYPE x;
7746 x = TREE_REAL_CST (arg);
7747 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7749 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7750 REAL_VALUE_TYPE r;
7752 real_round (&r, TYPE_MODE (type), &x);
7753 return build_real (type, r);
7757 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7760 /* Fold function call to builtin lround, lroundf or lroundl (or the
7761 corresponding long long versions) and other rounding functions. ARG
7762 is the argument to the call. Return NULL_TREE if no simplification
7763 can be made. */
7765 static tree
7766 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7768 if (!validate_arg (arg, REAL_TYPE))
7769 return NULL_TREE;
7771 /* Optimize lround of constant value. */
7772 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7774 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7776 if (real_isfinite (&x))
7778 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7779 tree ftype = TREE_TYPE (arg);
7780 unsigned HOST_WIDE_INT lo2;
7781 HOST_WIDE_INT hi, lo;
7782 REAL_VALUE_TYPE r;
7784 switch (DECL_FUNCTION_CODE (fndecl))
7786 CASE_FLT_FN (BUILT_IN_LFLOOR):
7787 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7788 real_floor (&r, TYPE_MODE (ftype), &x);
7789 break;
7791 CASE_FLT_FN (BUILT_IN_LCEIL):
7792 CASE_FLT_FN (BUILT_IN_LLCEIL):
7793 real_ceil (&r, TYPE_MODE (ftype), &x);
7794 break;
7796 CASE_FLT_FN (BUILT_IN_LROUND):
7797 CASE_FLT_FN (BUILT_IN_LLROUND):
7798 real_round (&r, TYPE_MODE (ftype), &x);
7799 break;
7801 default:
7802 gcc_unreachable ();
7805 REAL_VALUE_TO_INT (&lo, &hi, r);
7806 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
7807 return build_int_cst_wide (itype, lo2, hi);
7811 switch (DECL_FUNCTION_CODE (fndecl))
7813 CASE_FLT_FN (BUILT_IN_LFLOOR):
7814 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7815 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7816 if (tree_expr_nonnegative_p (arg))
7817 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7818 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7819 break;
7820 default:;
7823 return fold_fixed_mathfn (loc, fndecl, arg);
7826 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7827 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7828 the argument to the call. Return NULL_TREE if no simplification can
7829 be made. */
7831 static tree
7832 fold_builtin_bitop (tree fndecl, tree arg)
7834 if (!validate_arg (arg, INTEGER_TYPE))
7835 return NULL_TREE;
7837 /* Optimize for constant argument. */
7838 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7840 HOST_WIDE_INT hi, width, result;
7841 unsigned HOST_WIDE_INT lo;
7842 tree type;
7844 type = TREE_TYPE (arg);
7845 width = TYPE_PRECISION (type);
7846 lo = TREE_INT_CST_LOW (arg);
7848 /* Clear all the bits that are beyond the type's precision. */
7849 if (width > HOST_BITS_PER_WIDE_INT)
7851 hi = TREE_INT_CST_HIGH (arg);
7852 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7853 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7855 else
7857 hi = 0;
7858 if (width < HOST_BITS_PER_WIDE_INT)
7859 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7862 switch (DECL_FUNCTION_CODE (fndecl))
7864 CASE_INT_FN (BUILT_IN_FFS):
7865 if (lo != 0)
7866 result = exact_log2 (lo & -lo) + 1;
7867 else if (hi != 0)
7868 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7869 else
7870 result = 0;
7871 break;
7873 CASE_INT_FN (BUILT_IN_CLZ):
7874 if (hi != 0)
7875 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7876 else if (lo != 0)
7877 result = width - floor_log2 (lo) - 1;
7878 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7879 result = width;
7880 break;
7882 CASE_INT_FN (BUILT_IN_CTZ):
7883 if (lo != 0)
7884 result = exact_log2 (lo & -lo);
7885 else if (hi != 0)
7886 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7887 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7888 result = width;
7889 break;
7891 CASE_INT_FN (BUILT_IN_POPCOUNT):
7892 result = 0;
7893 while (lo)
7894 result++, lo &= lo - 1;
7895 while (hi)
7896 result++, hi &= hi - 1;
7897 break;
7899 CASE_INT_FN (BUILT_IN_PARITY):
7900 result = 0;
7901 while (lo)
7902 result++, lo &= lo - 1;
7903 while (hi)
7904 result++, hi &= hi - 1;
7905 result &= 1;
7906 break;
7908 default:
7909 gcc_unreachable ();
7912 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7915 return NULL_TREE;
7918 /* Fold function call to builtin_bswap and the long and long long
7919 variants. Return NULL_TREE if no simplification can be made. */
7920 static tree
7921 fold_builtin_bswap (tree fndecl, tree arg)
7923 if (! validate_arg (arg, INTEGER_TYPE))
7924 return NULL_TREE;
7926 /* Optimize constant value. */
7927 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7929 HOST_WIDE_INT hi, width, r_hi = 0;
7930 unsigned HOST_WIDE_INT lo, r_lo = 0;
7931 tree type;
7933 type = TREE_TYPE (arg);
7934 width = TYPE_PRECISION (type);
7935 lo = TREE_INT_CST_LOW (arg);
7936 hi = TREE_INT_CST_HIGH (arg);
7938 switch (DECL_FUNCTION_CODE (fndecl))
7940 case BUILT_IN_BSWAP32:
7941 case BUILT_IN_BSWAP64:
7943 int s;
7945 for (s = 0; s < width; s += 8)
7947 int d = width - s - 8;
7948 unsigned HOST_WIDE_INT byte;
7950 if (s < HOST_BITS_PER_WIDE_INT)
7951 byte = (lo >> s) & 0xff;
7952 else
7953 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7955 if (d < HOST_BITS_PER_WIDE_INT)
7956 r_lo |= byte << d;
7957 else
7958 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7962 break;
7964 default:
7965 gcc_unreachable ();
7968 if (width < HOST_BITS_PER_WIDE_INT)
7969 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7970 else
7971 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7974 return NULL_TREE;
7977 /* A subroutine of fold_builtin to fold the various logarithmic
7978 functions. Return NULL_TREE if no simplification can me made.
7979 FUNC is the corresponding MPFR logarithm function. */
7981 static tree
7982 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7983 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7985 if (validate_arg (arg, REAL_TYPE))
7987 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7988 tree res;
7989 const enum built_in_function fcode = builtin_mathfn_code (arg);
7991 /* Calculate the result when the argument is a constant. */
7992 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7993 return res;
7995 /* Special case, optimize logN(expN(x)) = x. */
7996 if (flag_unsafe_math_optimizations
7997 && ((func == mpfr_log
7998 && (fcode == BUILT_IN_EXP
7999 || fcode == BUILT_IN_EXPF
8000 || fcode == BUILT_IN_EXPL))
8001 || (func == mpfr_log2
8002 && (fcode == BUILT_IN_EXP2
8003 || fcode == BUILT_IN_EXP2F
8004 || fcode == BUILT_IN_EXP2L))
8005 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8006 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8008 /* Optimize logN(func()) for various exponential functions. We
8009 want to determine the value "x" and the power "exponent" in
8010 order to transform logN(x**exponent) into exponent*logN(x). */
8011 if (flag_unsafe_math_optimizations)
8013 tree exponent = 0, x = 0;
8015 switch (fcode)
8017 CASE_FLT_FN (BUILT_IN_EXP):
8018 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8019 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8020 dconst_e ()));
8021 exponent = CALL_EXPR_ARG (arg, 0);
8022 break;
8023 CASE_FLT_FN (BUILT_IN_EXP2):
8024 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8025 x = build_real (type, dconst2);
8026 exponent = CALL_EXPR_ARG (arg, 0);
8027 break;
8028 CASE_FLT_FN (BUILT_IN_EXP10):
8029 CASE_FLT_FN (BUILT_IN_POW10):
8030 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8032 REAL_VALUE_TYPE dconst10;
8033 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8034 x = build_real (type, dconst10);
8036 exponent = CALL_EXPR_ARG (arg, 0);
8037 break;
8038 CASE_FLT_FN (BUILT_IN_SQRT):
8039 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8040 x = CALL_EXPR_ARG (arg, 0);
8041 exponent = build_real (type, dconsthalf);
8042 break;
8043 CASE_FLT_FN (BUILT_IN_CBRT):
8044 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8045 x = CALL_EXPR_ARG (arg, 0);
8046 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8047 dconst_third ()));
8048 break;
8049 CASE_FLT_FN (BUILT_IN_POW):
8050 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8051 x = CALL_EXPR_ARG (arg, 0);
8052 exponent = CALL_EXPR_ARG (arg, 1);
8053 break;
8054 default:
8055 break;
8058 /* Now perform the optimization. */
8059 if (x && exponent)
8061 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8062 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8067 return NULL_TREE;
8070 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8071 NULL_TREE if no simplification can be made. */
8073 static tree
8074 fold_builtin_hypot (location_t loc, tree fndecl,
8075 tree arg0, tree arg1, tree type)
8077 tree res, narg0, narg1;
8079 if (!validate_arg (arg0, REAL_TYPE)
8080 || !validate_arg (arg1, REAL_TYPE))
8081 return NULL_TREE;
8083 /* Calculate the result when the argument is a constant. */
8084 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8085 return res;
8087 /* If either argument to hypot has a negate or abs, strip that off.
8088 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8089 narg0 = fold_strip_sign_ops (arg0);
8090 narg1 = fold_strip_sign_ops (arg1);
8091 if (narg0 || narg1)
8093 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8094 narg1 ? narg1 : arg1);
8097 /* If either argument is zero, hypot is fabs of the other. */
8098 if (real_zerop (arg0))
8099 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8100 else if (real_zerop (arg1))
8101 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8103 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8104 if (flag_unsafe_math_optimizations
8105 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8107 const REAL_VALUE_TYPE sqrt2_trunc
8108 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8109 return fold_build2_loc (loc, MULT_EXPR, type,
8110 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8111 build_real (type, sqrt2_trunc));
8114 return NULL_TREE;
8118 /* Fold a builtin function call to pow, powf, or powl. Return
8119 NULL_TREE if no simplification can be made. */
8120 static tree
8121 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8123 tree res;
8125 if (!validate_arg (arg0, REAL_TYPE)
8126 || !validate_arg (arg1, REAL_TYPE))
8127 return NULL_TREE;
8129 /* Calculate the result when the argument is a constant. */
8130 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8131 return res;
8133 /* Optimize pow(1.0,y) = 1.0. */
8134 if (real_onep (arg0))
8135 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8137 if (TREE_CODE (arg1) == REAL_CST
8138 && !TREE_OVERFLOW (arg1))
8140 REAL_VALUE_TYPE cint;
8141 REAL_VALUE_TYPE c;
8142 HOST_WIDE_INT n;
8144 c = TREE_REAL_CST (arg1);
8146 /* Optimize pow(x,0.0) = 1.0. */
8147 if (REAL_VALUES_EQUAL (c, dconst0))
8148 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8149 arg0);
8151 /* Optimize pow(x,1.0) = x. */
8152 if (REAL_VALUES_EQUAL (c, dconst1))
8153 return arg0;
8155 /* Optimize pow(x,-1.0) = 1.0/x. */
8156 if (REAL_VALUES_EQUAL (c, dconstm1))
8157 return fold_build2_loc (loc, RDIV_EXPR, type,
8158 build_real (type, dconst1), arg0);
8160 /* Optimize pow(x,0.5) = sqrt(x). */
8161 if (flag_unsafe_math_optimizations
8162 && REAL_VALUES_EQUAL (c, dconsthalf))
8164 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8166 if (sqrtfn != NULL_TREE)
8167 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8170 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8171 if (flag_unsafe_math_optimizations)
8173 const REAL_VALUE_TYPE dconstroot
8174 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8176 if (REAL_VALUES_EQUAL (c, dconstroot))
8178 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8179 if (cbrtfn != NULL_TREE)
8180 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8184 /* Check for an integer exponent. */
8185 n = real_to_integer (&c);
8186 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8187 if (real_identical (&c, &cint))
8189 /* Attempt to evaluate pow at compile-time, unless this should
8190 raise an exception. */
8191 if (TREE_CODE (arg0) == REAL_CST
8192 && !TREE_OVERFLOW (arg0)
8193 && (n > 0
8194 || (!flag_trapping_math && !flag_errno_math)
8195 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8197 REAL_VALUE_TYPE x;
8198 bool inexact;
8200 x = TREE_REAL_CST (arg0);
8201 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8202 if (flag_unsafe_math_optimizations || !inexact)
8203 return build_real (type, x);
8206 /* Strip sign ops from even integer powers. */
8207 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8209 tree narg0 = fold_strip_sign_ops (arg0);
8210 if (narg0)
8211 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8216 if (flag_unsafe_math_optimizations)
8218 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8220 /* Optimize pow(expN(x),y) = expN(x*y). */
8221 if (BUILTIN_EXPONENT_P (fcode))
8223 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8224 tree arg = CALL_EXPR_ARG (arg0, 0);
8225 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8226 return build_call_expr_loc (loc, expfn, 1, arg);
8229 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8230 if (BUILTIN_SQRT_P (fcode))
8232 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8233 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8234 build_real (type, dconsthalf));
8235 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8238 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8239 if (BUILTIN_CBRT_P (fcode))
8241 tree arg = CALL_EXPR_ARG (arg0, 0);
8242 if (tree_expr_nonnegative_p (arg))
8244 const REAL_VALUE_TYPE dconstroot
8245 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8246 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8247 build_real (type, dconstroot));
8248 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8252 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8253 if (fcode == BUILT_IN_POW
8254 || fcode == BUILT_IN_POWF
8255 || fcode == BUILT_IN_POWL)
8257 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8258 if (tree_expr_nonnegative_p (arg00))
8260 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8261 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8262 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8267 return NULL_TREE;
8270 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8271 Return NULL_TREE if no simplification can be made. */
8272 static tree
8273 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8274 tree arg0, tree arg1, tree type)
8276 if (!validate_arg (arg0, REAL_TYPE)
8277 || !validate_arg (arg1, INTEGER_TYPE))
8278 return NULL_TREE;
8280 /* Optimize pow(1.0,y) = 1.0. */
8281 if (real_onep (arg0))
8282 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8284 if (host_integerp (arg1, 0))
8286 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8288 /* Evaluate powi at compile-time. */
8289 if (TREE_CODE (arg0) == REAL_CST
8290 && !TREE_OVERFLOW (arg0))
8292 REAL_VALUE_TYPE x;
8293 x = TREE_REAL_CST (arg0);
8294 real_powi (&x, TYPE_MODE (type), &x, c);
8295 return build_real (type, x);
8298 /* Optimize pow(x,0) = 1.0. */
8299 if (c == 0)
8300 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8301 arg0);
8303 /* Optimize pow(x,1) = x. */
8304 if (c == 1)
8305 return arg0;
8307 /* Optimize pow(x,-1) = 1.0/x. */
8308 if (c == -1)
8309 return fold_build2_loc (loc, RDIV_EXPR, type,
8310 build_real (type, dconst1), arg0);
8313 return NULL_TREE;
8316 /* A subroutine of fold_builtin to fold the various exponent
8317 functions. Return NULL_TREE if no simplification can be made.
8318 FUNC is the corresponding MPFR exponent function. */
8320 static tree
8321 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8322 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8324 if (validate_arg (arg, REAL_TYPE))
8326 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8327 tree res;
8329 /* Calculate the result when the argument is a constant. */
8330 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8331 return res;
8333 /* Optimize expN(logN(x)) = x. */
8334 if (flag_unsafe_math_optimizations)
8336 const enum built_in_function fcode = builtin_mathfn_code (arg);
8338 if ((func == mpfr_exp
8339 && (fcode == BUILT_IN_LOG
8340 || fcode == BUILT_IN_LOGF
8341 || fcode == BUILT_IN_LOGL))
8342 || (func == mpfr_exp2
8343 && (fcode == BUILT_IN_LOG2
8344 || fcode == BUILT_IN_LOG2F
8345 || fcode == BUILT_IN_LOG2L))
8346 || (func == mpfr_exp10
8347 && (fcode == BUILT_IN_LOG10
8348 || fcode == BUILT_IN_LOG10F
8349 || fcode == BUILT_IN_LOG10L)))
8350 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8354 return NULL_TREE;
8357 /* Return true if VAR is a VAR_DECL or a component thereof. */
8359 static bool
8360 var_decl_component_p (tree var)
8362 tree inner = var;
8363 while (handled_component_p (inner))
8364 inner = TREE_OPERAND (inner, 0);
8365 return SSA_VAR_P (inner);
8368 /* Fold function call to builtin memset. Return
8369 NULL_TREE if no simplification can be made. */
8371 static tree
8372 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8373 tree type, bool ignore)
8375 tree var, ret, etype;
8376 unsigned HOST_WIDE_INT length, cval;
8378 if (! validate_arg (dest, POINTER_TYPE)
8379 || ! validate_arg (c, INTEGER_TYPE)
8380 || ! validate_arg (len, INTEGER_TYPE))
8381 return NULL_TREE;
8383 if (! host_integerp (len, 1))
8384 return NULL_TREE;
8386 /* If the LEN parameter is zero, return DEST. */
8387 if (integer_zerop (len))
8388 return omit_one_operand_loc (loc, type, dest, c);
8390 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8391 return NULL_TREE;
8393 var = dest;
8394 STRIP_NOPS (var);
8395 if (TREE_CODE (var) != ADDR_EXPR)
8396 return NULL_TREE;
8398 var = TREE_OPERAND (var, 0);
8399 if (TREE_THIS_VOLATILE (var))
8400 return NULL_TREE;
8402 etype = TREE_TYPE (var);
8403 if (TREE_CODE (etype) == ARRAY_TYPE)
8404 etype = TREE_TYPE (etype);
8406 if (!INTEGRAL_TYPE_P (etype)
8407 && !POINTER_TYPE_P (etype))
8408 return NULL_TREE;
8410 if (! var_decl_component_p (var))
8411 return NULL_TREE;
8413 length = tree_low_cst (len, 1);
8414 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8415 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8416 < (int) length)
8417 return NULL_TREE;
8419 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8420 return NULL_TREE;
8422 if (integer_zerop (c))
8423 cval = 0;
8424 else
8426 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8427 return NULL_TREE;
8429 cval = tree_low_cst (c, 1);
8430 cval &= 0xff;
8431 cval |= cval << 8;
8432 cval |= cval << 16;
8433 cval |= (cval << 31) << 1;
8436 ret = build_int_cst_type (etype, cval);
8437 var = build_fold_indirect_ref_loc (loc,
8438 fold_convert_loc (loc,
8439 build_pointer_type (etype),
8440 dest));
8441 ret = build2 (MODIFY_EXPR, etype, var, ret);
8442 if (ignore)
8443 return ret;
8445 return omit_one_operand_loc (loc, type, dest, ret);
8448 /* Fold function call to builtin memset. Return
8449 NULL_TREE if no simplification can be made. */
8451 static tree
8452 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8454 if (! validate_arg (dest, POINTER_TYPE)
8455 || ! validate_arg (size, INTEGER_TYPE))
8456 return NULL_TREE;
8458 if (!ignore)
8459 return NULL_TREE;
8461 /* New argument list transforming bzero(ptr x, int y) to
8462 memset(ptr x, int 0, size_t y). This is done this way
8463 so that if it isn't expanded inline, we fallback to
8464 calling bzero instead of memset. */
8466 return fold_builtin_memset (loc, dest, integer_zero_node,
8467 fold_convert_loc (loc, sizetype, size),
8468 void_type_node, ignore);
8471 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8472 NULL_TREE if no simplification can be made.
8473 If ENDP is 0, return DEST (like memcpy).
8474 If ENDP is 1, return DEST+LEN (like mempcpy).
8475 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8476 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8477 (memmove). */
8479 static tree
8480 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8481 tree len, tree type, bool ignore, int endp)
8483 tree destvar, srcvar, expr;
8485 if (! validate_arg (dest, POINTER_TYPE)
8486 || ! validate_arg (src, POINTER_TYPE)
8487 || ! validate_arg (len, INTEGER_TYPE))
8488 return NULL_TREE;
8490 /* If the LEN parameter is zero, return DEST. */
8491 if (integer_zerop (len))
8492 return omit_one_operand_loc (loc, type, dest, src);
8494 /* If SRC and DEST are the same (and not volatile), return
8495 DEST{,+LEN,+LEN-1}. */
8496 if (operand_equal_p (src, dest, 0))
8497 expr = len;
8498 else
8500 tree srctype, desttype;
8501 int src_align, dest_align;
8503 if (endp == 3)
8505 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8506 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8508 /* Both DEST and SRC must be pointer types.
8509 ??? This is what old code did. Is the testing for pointer types
8510 really mandatory?
8512 If either SRC is readonly or length is 1, we can use memcpy. */
8513 if (!dest_align || !src_align)
8514 return NULL_TREE;
8515 if (readonly_data_expr (src)
8516 || (host_integerp (len, 1)
8517 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8518 >= tree_low_cst (len, 1))))
8520 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8521 if (!fn)
8522 return NULL_TREE;
8523 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8526 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8527 srcvar = build_fold_indirect_ref_loc (loc, src);
8528 destvar = build_fold_indirect_ref_loc (loc, dest);
8529 if (srcvar
8530 && !TREE_THIS_VOLATILE (srcvar)
8531 && destvar
8532 && !TREE_THIS_VOLATILE (destvar))
8534 tree src_base, dest_base, fn;
8535 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8536 HOST_WIDE_INT size = -1;
8537 HOST_WIDE_INT maxsize = -1;
8539 src_base = srcvar;
8540 if (handled_component_p (src_base))
8541 src_base = get_ref_base_and_extent (src_base, &src_offset,
8542 &size, &maxsize);
8543 dest_base = destvar;
8544 if (handled_component_p (dest_base))
8545 dest_base = get_ref_base_and_extent (dest_base, &dest_offset,
8546 &size, &maxsize);
8547 if (host_integerp (len, 1))
8549 maxsize = tree_low_cst (len, 1);
8550 if (maxsize
8551 > INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT)
8552 maxsize = -1;
8553 else
8554 maxsize *= BITS_PER_UNIT;
8556 else
8557 maxsize = -1;
8558 if (SSA_VAR_P (src_base)
8559 && SSA_VAR_P (dest_base))
8561 if (operand_equal_p (src_base, dest_base, 0)
8562 && ranges_overlap_p (src_offset, maxsize,
8563 dest_offset, maxsize))
8564 return NULL_TREE;
8566 else if (TREE_CODE (src_base) == INDIRECT_REF
8567 && TREE_CODE (dest_base) == INDIRECT_REF)
8569 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8570 TREE_OPERAND (dest_base, 0), 0)
8571 || ranges_overlap_p (src_offset, maxsize,
8572 dest_offset, maxsize))
8573 return NULL_TREE;
8575 else
8576 return NULL_TREE;
8578 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8579 if (!fn)
8580 return NULL_TREE;
8581 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8583 return NULL_TREE;
8586 if (!host_integerp (len, 0))
8587 return NULL_TREE;
8588 /* FIXME:
8589 This logic lose for arguments like (type *)malloc (sizeof (type)),
8590 since we strip the casts of up to VOID return value from malloc.
8591 Perhaps we ought to inherit type from non-VOID argument here? */
8592 STRIP_NOPS (src);
8593 STRIP_NOPS (dest);
8594 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8595 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8597 tree tem = TREE_OPERAND (src, 0);
8598 STRIP_NOPS (tem);
8599 if (tem != TREE_OPERAND (src, 0))
8600 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8602 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8604 tree tem = TREE_OPERAND (dest, 0);
8605 STRIP_NOPS (tem);
8606 if (tem != TREE_OPERAND (dest, 0))
8607 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8609 srctype = TREE_TYPE (TREE_TYPE (src));
8610 if (srctype
8611 && TREE_CODE (srctype) == ARRAY_TYPE
8612 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8614 srctype = TREE_TYPE (srctype);
8615 STRIP_NOPS (src);
8616 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8618 desttype = TREE_TYPE (TREE_TYPE (dest));
8619 if (desttype
8620 && TREE_CODE (desttype) == ARRAY_TYPE
8621 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8623 desttype = TREE_TYPE (desttype);
8624 STRIP_NOPS (dest);
8625 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8627 if (!srctype || !desttype
8628 || !TYPE_SIZE_UNIT (srctype)
8629 || !TYPE_SIZE_UNIT (desttype)
8630 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8631 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8632 || TYPE_VOLATILE (srctype)
8633 || TYPE_VOLATILE (desttype))
8634 return NULL_TREE;
8636 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8637 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8638 if (dest_align < (int) TYPE_ALIGN (desttype)
8639 || src_align < (int) TYPE_ALIGN (srctype))
8640 return NULL_TREE;
8642 if (!ignore)
8643 dest = builtin_save_expr (dest);
8645 srcvar = NULL_TREE;
8646 if (tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8648 srcvar = build_fold_indirect_ref_loc (loc, src);
8649 if (TREE_THIS_VOLATILE (srcvar))
8650 return NULL_TREE;
8651 else if (!tree_int_cst_equal (tree_expr_size (srcvar), len))
8652 srcvar = NULL_TREE;
8653 /* With memcpy, it is possible to bypass aliasing rules, so without
8654 this check i.e. execute/20060930-2.c would be misoptimized,
8655 because it use conflicting alias set to hold argument for the
8656 memcpy call. This check is probably unnecessary with
8657 -fno-strict-aliasing. Similarly for destvar. See also
8658 PR29286. */
8659 else if (!var_decl_component_p (srcvar))
8660 srcvar = NULL_TREE;
8663 destvar = NULL_TREE;
8664 if (tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8666 destvar = build_fold_indirect_ref_loc (loc, dest);
8667 if (TREE_THIS_VOLATILE (destvar))
8668 return NULL_TREE;
8669 else if (!tree_int_cst_equal (tree_expr_size (destvar), len))
8670 destvar = NULL_TREE;
8671 else if (!var_decl_component_p (destvar))
8672 destvar = NULL_TREE;
8675 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8676 return NULL_TREE;
8678 if (srcvar == NULL_TREE)
8680 tree srcptype;
8681 if (TREE_ADDRESSABLE (TREE_TYPE (destvar)))
8682 return NULL_TREE;
8684 srctype = build_qualified_type (desttype, 0);
8685 if (src_align < (int) TYPE_ALIGN (srctype))
8687 if (AGGREGATE_TYPE_P (srctype)
8688 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (srctype), src_align))
8689 return NULL_TREE;
8691 srctype = build_variant_type_copy (srctype);
8692 TYPE_ALIGN (srctype) = src_align;
8693 TYPE_USER_ALIGN (srctype) = 1;
8694 TYPE_PACKED (srctype) = 1;
8696 srcptype = build_pointer_type_for_mode (srctype, ptr_mode, true);
8697 src = fold_convert_loc (loc, srcptype, src);
8698 srcvar = build_fold_indirect_ref_loc (loc, src);
8700 else if (destvar == NULL_TREE)
8702 tree destptype;
8703 if (TREE_ADDRESSABLE (TREE_TYPE (srcvar)))
8704 return NULL_TREE;
8706 desttype = build_qualified_type (srctype, 0);
8707 if (dest_align < (int) TYPE_ALIGN (desttype))
8709 if (AGGREGATE_TYPE_P (desttype)
8710 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (desttype), dest_align))
8711 return NULL_TREE;
8713 desttype = build_variant_type_copy (desttype);
8714 TYPE_ALIGN (desttype) = dest_align;
8715 TYPE_USER_ALIGN (desttype) = 1;
8716 TYPE_PACKED (desttype) = 1;
8718 destptype = build_pointer_type_for_mode (desttype, ptr_mode, true);
8719 dest = fold_convert_loc (loc, destptype, dest);
8720 destvar = build_fold_indirect_ref_loc (loc, dest);
8723 if (srctype == desttype
8724 || (gimple_in_ssa_p (cfun)
8725 && useless_type_conversion_p (desttype, srctype)))
8726 expr = srcvar;
8727 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8728 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8729 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8730 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8731 expr = fold_convert_loc (loc, TREE_TYPE (destvar), srcvar);
8732 else
8733 expr = fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8734 TREE_TYPE (destvar), srcvar);
8735 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8738 if (ignore)
8739 return expr;
8741 if (endp == 0 || endp == 3)
8742 return omit_one_operand_loc (loc, type, dest, expr);
8744 if (expr == len)
8745 expr = NULL_TREE;
8747 if (endp == 2)
8748 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8749 ssize_int (1));
8751 len = fold_convert_loc (loc, sizetype, len);
8752 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8753 dest = fold_convert_loc (loc, type, dest);
8754 if (expr)
8755 dest = omit_one_operand_loc (loc, type, dest, expr);
8756 return dest;
8759 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8760 If LEN is not NULL, it represents the length of the string to be
8761 copied. Return NULL_TREE if no simplification can be made. */
8763 tree
8764 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8766 tree fn;
8768 if (!validate_arg (dest, POINTER_TYPE)
8769 || !validate_arg (src, POINTER_TYPE))
8770 return NULL_TREE;
8772 /* If SRC and DEST are the same (and not volatile), return DEST. */
8773 if (operand_equal_p (src, dest, 0))
8774 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8776 if (optimize_function_for_size_p (cfun))
8777 return NULL_TREE;
8779 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8780 if (!fn)
8781 return NULL_TREE;
8783 if (!len)
8785 len = c_strlen (src, 1);
8786 if (! len || TREE_SIDE_EFFECTS (len))
8787 return NULL_TREE;
8790 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8791 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8792 build_call_expr_loc (loc, fn, 3, dest, src, len));
8795 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8796 Return NULL_TREE if no simplification can be made. */
8798 static tree
8799 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8801 tree fn, len, lenp1, call, type;
8803 if (!validate_arg (dest, POINTER_TYPE)
8804 || !validate_arg (src, POINTER_TYPE))
8805 return NULL_TREE;
8807 len = c_strlen (src, 1);
8808 if (!len
8809 || TREE_CODE (len) != INTEGER_CST)
8810 return NULL_TREE;
8812 if (optimize_function_for_size_p (cfun)
8813 /* If length is zero it's small enough. */
8814 && !integer_zerop (len))
8815 return NULL_TREE;
8817 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8818 if (!fn)
8819 return NULL_TREE;
8821 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8822 /* We use dest twice in building our expression. Save it from
8823 multiple expansions. */
8824 dest = builtin_save_expr (dest);
8825 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8827 type = TREE_TYPE (TREE_TYPE (fndecl));
8828 len = fold_convert_loc (loc, sizetype, len);
8829 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8830 dest = fold_convert_loc (loc, type, dest);
8831 dest = omit_one_operand_loc (loc, type, dest, call);
8832 return dest;
8835 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8836 If SLEN is not NULL, it represents the length of the source string.
8837 Return NULL_TREE if no simplification can be made. */
8839 tree
8840 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8841 tree src, tree len, tree slen)
8843 tree fn;
8845 if (!validate_arg (dest, POINTER_TYPE)
8846 || !validate_arg (src, POINTER_TYPE)
8847 || !validate_arg (len, INTEGER_TYPE))
8848 return NULL_TREE;
8850 /* If the LEN parameter is zero, return DEST. */
8851 if (integer_zerop (len))
8852 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8854 /* We can't compare slen with len as constants below if len is not a
8855 constant. */
8856 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8857 return NULL_TREE;
8859 if (!slen)
8860 slen = c_strlen (src, 1);
8862 /* Now, we must be passed a constant src ptr parameter. */
8863 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8864 return NULL_TREE;
8866 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8868 /* We do not support simplification of this case, though we do
8869 support it when expanding trees into RTL. */
8870 /* FIXME: generate a call to __builtin_memset. */
8871 if (tree_int_cst_lt (slen, len))
8872 return NULL_TREE;
8874 /* OK transform into builtin memcpy. */
8875 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8876 if (!fn)
8877 return NULL_TREE;
8878 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8879 build_call_expr_loc (loc, fn, 3, dest, src, len));
8882 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8883 arguments to the call, and TYPE is its return type.
8884 Return NULL_TREE if no simplification can be made. */
8886 static tree
8887 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8889 if (!validate_arg (arg1, POINTER_TYPE)
8890 || !validate_arg (arg2, INTEGER_TYPE)
8891 || !validate_arg (len, INTEGER_TYPE))
8892 return NULL_TREE;
8893 else
8895 const char *p1;
8897 if (TREE_CODE (arg2) != INTEGER_CST
8898 || !host_integerp (len, 1))
8899 return NULL_TREE;
8901 p1 = c_getstr (arg1);
8902 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8904 char c;
8905 const char *r;
8906 tree tem;
8908 if (target_char_cast (arg2, &c))
8909 return NULL_TREE;
8911 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8913 if (r == NULL)
8914 return build_int_cst (TREE_TYPE (arg1), 0);
8916 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8917 size_int (r - p1));
8918 return fold_convert_loc (loc, type, tem);
8920 return NULL_TREE;
8924 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8925 Return NULL_TREE if no simplification can be made. */
8927 static tree
8928 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8930 const char *p1, *p2;
8932 if (!validate_arg (arg1, POINTER_TYPE)
8933 || !validate_arg (arg2, POINTER_TYPE)
8934 || !validate_arg (len, INTEGER_TYPE))
8935 return NULL_TREE;
8937 /* If the LEN parameter is zero, return zero. */
8938 if (integer_zerop (len))
8939 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8940 arg1, arg2);
8942 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8943 if (operand_equal_p (arg1, arg2, 0))
8944 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8946 p1 = c_getstr (arg1);
8947 p2 = c_getstr (arg2);
8949 /* If all arguments are constant, and the value of len is not greater
8950 than the lengths of arg1 and arg2, evaluate at compile-time. */
8951 if (host_integerp (len, 1) && p1 && p2
8952 && compare_tree_int (len, strlen (p1) + 1) <= 0
8953 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8955 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8957 if (r > 0)
8958 return integer_one_node;
8959 else if (r < 0)
8960 return integer_minus_one_node;
8961 else
8962 return integer_zero_node;
8965 /* If len parameter is one, return an expression corresponding to
8966 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8967 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8969 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8970 tree cst_uchar_ptr_node
8971 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8973 tree ind1
8974 = fold_convert_loc (loc, integer_type_node,
8975 build1 (INDIRECT_REF, cst_uchar_node,
8976 fold_convert_loc (loc,
8977 cst_uchar_ptr_node,
8978 arg1)));
8979 tree ind2
8980 = fold_convert_loc (loc, integer_type_node,
8981 build1 (INDIRECT_REF, cst_uchar_node,
8982 fold_convert_loc (loc,
8983 cst_uchar_ptr_node,
8984 arg2)));
8985 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8988 return NULL_TREE;
8991 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8992 Return NULL_TREE if no simplification can be made. */
8994 static tree
8995 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8997 const char *p1, *p2;
8999 if (!validate_arg (arg1, POINTER_TYPE)
9000 || !validate_arg (arg2, POINTER_TYPE))
9001 return NULL_TREE;
9003 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9004 if (operand_equal_p (arg1, arg2, 0))
9005 return integer_zero_node;
9007 p1 = c_getstr (arg1);
9008 p2 = c_getstr (arg2);
9010 if (p1 && p2)
9012 const int i = strcmp (p1, p2);
9013 if (i < 0)
9014 return integer_minus_one_node;
9015 else if (i > 0)
9016 return integer_one_node;
9017 else
9018 return integer_zero_node;
9021 /* If the second arg is "", return *(const unsigned char*)arg1. */
9022 if (p2 && *p2 == '\0')
9024 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9025 tree cst_uchar_ptr_node
9026 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9028 return fold_convert_loc (loc, integer_type_node,
9029 build1 (INDIRECT_REF, cst_uchar_node,
9030 fold_convert_loc (loc,
9031 cst_uchar_ptr_node,
9032 arg1)));
9035 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9036 if (p1 && *p1 == '\0')
9038 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9039 tree cst_uchar_ptr_node
9040 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9042 tree temp
9043 = fold_convert_loc (loc, integer_type_node,
9044 build1 (INDIRECT_REF, cst_uchar_node,
9045 fold_convert_loc (loc,
9046 cst_uchar_ptr_node,
9047 arg2)));
9048 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9051 return NULL_TREE;
9054 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9055 Return NULL_TREE if no simplification can be made. */
9057 static tree
9058 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9060 const char *p1, *p2;
9062 if (!validate_arg (arg1, POINTER_TYPE)
9063 || !validate_arg (arg2, POINTER_TYPE)
9064 || !validate_arg (len, INTEGER_TYPE))
9065 return NULL_TREE;
9067 /* If the LEN parameter is zero, return zero. */
9068 if (integer_zerop (len))
9069 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9070 arg1, arg2);
9072 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9073 if (operand_equal_p (arg1, arg2, 0))
9074 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9076 p1 = c_getstr (arg1);
9077 p2 = c_getstr (arg2);
9079 if (host_integerp (len, 1) && p1 && p2)
9081 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9082 if (i > 0)
9083 return integer_one_node;
9084 else if (i < 0)
9085 return integer_minus_one_node;
9086 else
9087 return integer_zero_node;
9090 /* If the second arg is "", and the length is greater than zero,
9091 return *(const unsigned char*)arg1. */
9092 if (p2 && *p2 == '\0'
9093 && TREE_CODE (len) == INTEGER_CST
9094 && tree_int_cst_sgn (len) == 1)
9096 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9097 tree cst_uchar_ptr_node
9098 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9100 return fold_convert_loc (loc, integer_type_node,
9101 build1 (INDIRECT_REF, cst_uchar_node,
9102 fold_convert_loc (loc,
9103 cst_uchar_ptr_node,
9104 arg1)));
9107 /* If the first arg is "", and the length is greater than zero,
9108 return -*(const unsigned char*)arg2. */
9109 if (p1 && *p1 == '\0'
9110 && TREE_CODE (len) == INTEGER_CST
9111 && tree_int_cst_sgn (len) == 1)
9113 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9114 tree cst_uchar_ptr_node
9115 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9117 tree temp = fold_convert_loc (loc, integer_type_node,
9118 build1 (INDIRECT_REF, cst_uchar_node,
9119 fold_convert_loc (loc,
9120 cst_uchar_ptr_node,
9121 arg2)));
9122 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9125 /* If len parameter is one, return an expression corresponding to
9126 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9127 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9129 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9130 tree cst_uchar_ptr_node
9131 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9133 tree ind1 = fold_convert_loc (loc, integer_type_node,
9134 build1 (INDIRECT_REF, cst_uchar_node,
9135 fold_convert_loc (loc,
9136 cst_uchar_ptr_node,
9137 arg1)));
9138 tree ind2 = fold_convert_loc (loc, integer_type_node,
9139 build1 (INDIRECT_REF, cst_uchar_node,
9140 fold_convert_loc (loc,
9141 cst_uchar_ptr_node,
9142 arg2)));
9143 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9146 return NULL_TREE;
9149 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9150 ARG. Return NULL_TREE if no simplification can be made. */
9152 static tree
9153 fold_builtin_signbit (location_t loc, tree arg, tree type)
9155 tree temp;
9157 if (!validate_arg (arg, REAL_TYPE))
9158 return NULL_TREE;
9160 /* If ARG is a compile-time constant, determine the result. */
9161 if (TREE_CODE (arg) == REAL_CST
9162 && !TREE_OVERFLOW (arg))
9164 REAL_VALUE_TYPE c;
9166 c = TREE_REAL_CST (arg);
9167 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9168 return fold_convert_loc (loc, type, temp);
9171 /* If ARG is non-negative, the result is always zero. */
9172 if (tree_expr_nonnegative_p (arg))
9173 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9175 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9176 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9177 return fold_build2_loc (loc, LT_EXPR, type, arg,
9178 build_real (TREE_TYPE (arg), dconst0));
9180 return NULL_TREE;
9183 /* Fold function call to builtin copysign, copysignf or copysignl with
9184 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9185 be made. */
9187 static tree
9188 fold_builtin_copysign (location_t loc, tree fndecl,
9189 tree arg1, tree arg2, tree type)
9191 tree tem;
9193 if (!validate_arg (arg1, REAL_TYPE)
9194 || !validate_arg (arg2, REAL_TYPE))
9195 return NULL_TREE;
9197 /* copysign(X,X) is X. */
9198 if (operand_equal_p (arg1, arg2, 0))
9199 return fold_convert_loc (loc, type, arg1);
9201 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9202 if (TREE_CODE (arg1) == REAL_CST
9203 && TREE_CODE (arg2) == REAL_CST
9204 && !TREE_OVERFLOW (arg1)
9205 && !TREE_OVERFLOW (arg2))
9207 REAL_VALUE_TYPE c1, c2;
9209 c1 = TREE_REAL_CST (arg1);
9210 c2 = TREE_REAL_CST (arg2);
9211 /* c1.sign := c2.sign. */
9212 real_copysign (&c1, &c2);
9213 return build_real (type, c1);
9216 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9217 Remember to evaluate Y for side-effects. */
9218 if (tree_expr_nonnegative_p (arg2))
9219 return omit_one_operand_loc (loc, type,
9220 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9221 arg2);
9223 /* Strip sign changing operations for the first argument. */
9224 tem = fold_strip_sign_ops (arg1);
9225 if (tem)
9226 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9228 return NULL_TREE;
9231 /* Fold a call to builtin isascii with argument ARG. */
9233 static tree
9234 fold_builtin_isascii (location_t loc, tree arg)
9236 if (!validate_arg (arg, INTEGER_TYPE))
9237 return NULL_TREE;
9238 else
9240 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9241 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9242 build_int_cst (NULL_TREE,
9243 ~ (unsigned HOST_WIDE_INT) 0x7f));
9244 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9245 arg, integer_zero_node);
9249 /* Fold a call to builtin toascii with argument ARG. */
9251 static tree
9252 fold_builtin_toascii (location_t loc, tree arg)
9254 if (!validate_arg (arg, INTEGER_TYPE))
9255 return NULL_TREE;
9257 /* Transform toascii(c) -> (c & 0x7f). */
9258 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9259 build_int_cst (NULL_TREE, 0x7f));
9262 /* Fold a call to builtin isdigit with argument ARG. */
9264 static tree
9265 fold_builtin_isdigit (location_t loc, tree arg)
9267 if (!validate_arg (arg, INTEGER_TYPE))
9268 return NULL_TREE;
9269 else
9271 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9272 /* According to the C standard, isdigit is unaffected by locale.
9273 However, it definitely is affected by the target character set. */
9274 unsigned HOST_WIDE_INT target_digit0
9275 = lang_hooks.to_target_charset ('0');
9277 if (target_digit0 == 0)
9278 return NULL_TREE;
9280 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9281 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9282 build_int_cst (unsigned_type_node, target_digit0));
9283 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9284 build_int_cst (unsigned_type_node, 9));
9288 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9290 static tree
9291 fold_builtin_fabs (location_t loc, tree arg, tree type)
9293 if (!validate_arg (arg, REAL_TYPE))
9294 return NULL_TREE;
9296 arg = fold_convert_loc (loc, type, arg);
9297 if (TREE_CODE (arg) == REAL_CST)
9298 return fold_abs_const (arg, type);
9299 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9302 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9304 static tree
9305 fold_builtin_abs (location_t loc, tree arg, tree type)
9307 if (!validate_arg (arg, INTEGER_TYPE))
9308 return NULL_TREE;
9310 arg = fold_convert_loc (loc, type, arg);
9311 if (TREE_CODE (arg) == INTEGER_CST)
9312 return fold_abs_const (arg, type);
9313 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9316 /* Fold a call to builtin fmin or fmax. */
9318 static tree
9319 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9320 tree type, bool max)
9322 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9324 /* Calculate the result when the argument is a constant. */
9325 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9327 if (res)
9328 return res;
9330 /* If either argument is NaN, return the other one. Avoid the
9331 transformation if we get (and honor) a signalling NaN. Using
9332 omit_one_operand() ensures we create a non-lvalue. */
9333 if (TREE_CODE (arg0) == REAL_CST
9334 && real_isnan (&TREE_REAL_CST (arg0))
9335 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9336 || ! TREE_REAL_CST (arg0).signalling))
9337 return omit_one_operand_loc (loc, type, arg1, arg0);
9338 if (TREE_CODE (arg1) == REAL_CST
9339 && real_isnan (&TREE_REAL_CST (arg1))
9340 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9341 || ! TREE_REAL_CST (arg1).signalling))
9342 return omit_one_operand_loc (loc, type, arg0, arg1);
9344 /* Transform fmin/fmax(x,x) -> x. */
9345 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9346 return omit_one_operand_loc (loc, type, arg0, arg1);
9348 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9349 functions to return the numeric arg if the other one is NaN.
9350 These tree codes don't honor that, so only transform if
9351 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9352 handled, so we don't have to worry about it either. */
9353 if (flag_finite_math_only)
9354 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9355 fold_convert_loc (loc, type, arg0),
9356 fold_convert_loc (loc, type, arg1));
9358 return NULL_TREE;
9361 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9363 static tree
9364 fold_builtin_carg (location_t loc, tree arg, tree type)
9366 if (validate_arg (arg, COMPLEX_TYPE)
9367 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9369 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9371 if (atan2_fn)
9373 tree new_arg = builtin_save_expr (arg);
9374 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9375 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9376 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9380 return NULL_TREE;
9383 /* Fold a call to builtin logb/ilogb. */
9385 static tree
9386 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9388 if (! validate_arg (arg, REAL_TYPE))
9389 return NULL_TREE;
9391 STRIP_NOPS (arg);
9393 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9395 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9397 switch (value->cl)
9399 case rvc_nan:
9400 case rvc_inf:
9401 /* If arg is Inf or NaN and we're logb, return it. */
9402 if (TREE_CODE (rettype) == REAL_TYPE)
9403 return fold_convert_loc (loc, rettype, arg);
9404 /* Fall through... */
9405 case rvc_zero:
9406 /* Zero may set errno and/or raise an exception for logb, also
9407 for ilogb we don't know FP_ILOGB0. */
9408 return NULL_TREE;
9409 case rvc_normal:
9410 /* For normal numbers, proceed iff radix == 2. In GCC,
9411 normalized significands are in the range [0.5, 1.0). We
9412 want the exponent as if they were [1.0, 2.0) so get the
9413 exponent and subtract 1. */
9414 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9415 return fold_convert_loc (loc, rettype,
9416 build_int_cst (NULL_TREE,
9417 REAL_EXP (value)-1));
9418 break;
9422 return NULL_TREE;
9425 /* Fold a call to builtin significand, if radix == 2. */
9427 static tree
9428 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9430 if (! validate_arg (arg, REAL_TYPE))
9431 return NULL_TREE;
9433 STRIP_NOPS (arg);
9435 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9437 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9439 switch (value->cl)
9441 case rvc_zero:
9442 case rvc_nan:
9443 case rvc_inf:
9444 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9445 return fold_convert_loc (loc, rettype, arg);
9446 case rvc_normal:
9447 /* For normal numbers, proceed iff radix == 2. */
9448 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9450 REAL_VALUE_TYPE result = *value;
9451 /* In GCC, normalized significands are in the range [0.5,
9452 1.0). We want them to be [1.0, 2.0) so set the
9453 exponent to 1. */
9454 SET_REAL_EXP (&result, 1);
9455 return build_real (rettype, result);
9457 break;
9461 return NULL_TREE;
9464 /* Fold a call to builtin frexp, we can assume the base is 2. */
9466 static tree
9467 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9469 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9470 return NULL_TREE;
9472 STRIP_NOPS (arg0);
9474 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9475 return NULL_TREE;
9477 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9479 /* Proceed if a valid pointer type was passed in. */
9480 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9482 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9483 tree frac, exp;
9485 switch (value->cl)
9487 case rvc_zero:
9488 /* For +-0, return (*exp = 0, +-0). */
9489 exp = integer_zero_node;
9490 frac = arg0;
9491 break;
9492 case rvc_nan:
9493 case rvc_inf:
9494 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9495 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9496 case rvc_normal:
9498 /* Since the frexp function always expects base 2, and in
9499 GCC normalized significands are already in the range
9500 [0.5, 1.0), we have exactly what frexp wants. */
9501 REAL_VALUE_TYPE frac_rvt = *value;
9502 SET_REAL_EXP (&frac_rvt, 0);
9503 frac = build_real (rettype, frac_rvt);
9504 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9506 break;
9507 default:
9508 gcc_unreachable ();
9511 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9512 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9513 TREE_SIDE_EFFECTS (arg1) = 1;
9514 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9517 return NULL_TREE;
9520 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9521 then we can assume the base is two. If it's false, then we have to
9522 check the mode of the TYPE parameter in certain cases. */
9524 static tree
9525 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9526 tree type, bool ldexp)
9528 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9530 STRIP_NOPS (arg0);
9531 STRIP_NOPS (arg1);
9533 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9534 if (real_zerop (arg0) || integer_zerop (arg1)
9535 || (TREE_CODE (arg0) == REAL_CST
9536 && !real_isfinite (&TREE_REAL_CST (arg0))))
9537 return omit_one_operand_loc (loc, type, arg0, arg1);
9539 /* If both arguments are constant, then try to evaluate it. */
9540 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9541 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9542 && host_integerp (arg1, 0))
9544 /* Bound the maximum adjustment to twice the range of the
9545 mode's valid exponents. Use abs to ensure the range is
9546 positive as a sanity check. */
9547 const long max_exp_adj = 2 *
9548 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9549 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9551 /* Get the user-requested adjustment. */
9552 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9554 /* The requested adjustment must be inside this range. This
9555 is a preliminary cap to avoid things like overflow, we
9556 may still fail to compute the result for other reasons. */
9557 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9559 REAL_VALUE_TYPE initial_result;
9561 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9563 /* Ensure we didn't overflow. */
9564 if (! real_isinf (&initial_result))
9566 const REAL_VALUE_TYPE trunc_result
9567 = real_value_truncate (TYPE_MODE (type), initial_result);
9569 /* Only proceed if the target mode can hold the
9570 resulting value. */
9571 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9572 return build_real (type, trunc_result);
9578 return NULL_TREE;
9581 /* Fold a call to builtin modf. */
9583 static tree
9584 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9586 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9587 return NULL_TREE;
9589 STRIP_NOPS (arg0);
9591 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9592 return NULL_TREE;
9594 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9596 /* Proceed if a valid pointer type was passed in. */
9597 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9599 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9600 REAL_VALUE_TYPE trunc, frac;
9602 switch (value->cl)
9604 case rvc_nan:
9605 case rvc_zero:
9606 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9607 trunc = frac = *value;
9608 break;
9609 case rvc_inf:
9610 /* For +-Inf, return (*arg1 = arg0, +-0). */
9611 frac = dconst0;
9612 frac.sign = value->sign;
9613 trunc = *value;
9614 break;
9615 case rvc_normal:
9616 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9617 real_trunc (&trunc, VOIDmode, value);
9618 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9619 /* If the original number was negative and already
9620 integral, then the fractional part is -0.0. */
9621 if (value->sign && frac.cl == rvc_zero)
9622 frac.sign = value->sign;
9623 break;
9626 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9627 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9628 build_real (rettype, trunc));
9629 TREE_SIDE_EFFECTS (arg1) = 1;
9630 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9631 build_real (rettype, frac));
9634 return NULL_TREE;
9637 /* Given a location LOC, an interclass builtin function decl FNDECL
9638 and its single argument ARG, return an folded expression computing
9639 the same, or NULL_TREE if we either couldn't or didn't want to fold
9640 (the latter happen if there's an RTL instruction available). */
9642 static tree
9643 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9645 enum machine_mode mode;
9647 if (!validate_arg (arg, REAL_TYPE))
9648 return NULL_TREE;
9650 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9651 return NULL_TREE;
9653 mode = TYPE_MODE (TREE_TYPE (arg));
9655 /* If there is no optab, try generic code. */
9656 switch (DECL_FUNCTION_CODE (fndecl))
9658 tree result;
9660 CASE_FLT_FN (BUILT_IN_ISINF):
9662 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9663 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9664 tree const type = TREE_TYPE (arg);
9665 REAL_VALUE_TYPE r;
9666 char buf[128];
9668 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9669 real_from_string (&r, buf);
9670 result = build_call_expr (isgr_fn, 2,
9671 fold_build1_loc (loc, ABS_EXPR, type, arg),
9672 build_real (type, r));
9673 return result;
9675 CASE_FLT_FN (BUILT_IN_FINITE):
9676 case BUILT_IN_ISFINITE:
9678 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9679 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9680 tree const type = TREE_TYPE (arg);
9681 REAL_VALUE_TYPE r;
9682 char buf[128];
9684 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9685 real_from_string (&r, buf);
9686 result = build_call_expr (isle_fn, 2,
9687 fold_build1_loc (loc, ABS_EXPR, type, arg),
9688 build_real (type, r));
9689 /*result = fold_build2_loc (loc, UNGT_EXPR,
9690 TREE_TYPE (TREE_TYPE (fndecl)),
9691 fold_build1_loc (loc, ABS_EXPR, type, arg),
9692 build_real (type, r));
9693 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9694 TREE_TYPE (TREE_TYPE (fndecl)),
9695 result);*/
9696 return result;
9698 case BUILT_IN_ISNORMAL:
9700 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9701 islessequal(fabs(x),DBL_MAX). */
9702 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9703 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9704 tree const type = TREE_TYPE (arg);
9705 REAL_VALUE_TYPE rmax, rmin;
9706 char buf[128];
9708 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9709 real_from_string (&rmax, buf);
9710 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9711 real_from_string (&rmin, buf);
9712 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9713 result = build_call_expr (isle_fn, 2, arg,
9714 build_real (type, rmax));
9715 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9716 build_call_expr (isge_fn, 2, arg,
9717 build_real (type, rmin)));
9718 return result;
9720 default:
9721 break;
9724 return NULL_TREE;
9727 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9728 ARG is the argument for the call. */
9730 static tree
9731 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9733 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9734 REAL_VALUE_TYPE r;
9736 if (!validate_arg (arg, REAL_TYPE))
9737 return NULL_TREE;
9739 switch (builtin_index)
9741 case BUILT_IN_ISINF:
9742 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9743 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9745 if (TREE_CODE (arg) == REAL_CST)
9747 r = TREE_REAL_CST (arg);
9748 if (real_isinf (&r))
9749 return real_compare (GT_EXPR, &r, &dconst0)
9750 ? integer_one_node : integer_minus_one_node;
9751 else
9752 return integer_zero_node;
9755 return NULL_TREE;
9757 case BUILT_IN_ISINF_SIGN:
9759 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9760 /* In a boolean context, GCC will fold the inner COND_EXPR to
9761 1. So e.g. "if (isinf_sign(x))" would be folded to just
9762 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9763 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9764 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9765 tree tmp = NULL_TREE;
9767 arg = builtin_save_expr (arg);
9769 if (signbit_fn && isinf_fn)
9771 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9772 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9774 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9775 signbit_call, integer_zero_node);
9776 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9777 isinf_call, integer_zero_node);
9779 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9780 integer_minus_one_node, integer_one_node);
9781 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9782 isinf_call, tmp,
9783 integer_zero_node);
9786 return tmp;
9789 case BUILT_IN_ISFINITE:
9790 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9791 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9792 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9794 if (TREE_CODE (arg) == REAL_CST)
9796 r = TREE_REAL_CST (arg);
9797 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9800 return NULL_TREE;
9802 case BUILT_IN_ISNAN:
9803 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9804 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9806 if (TREE_CODE (arg) == REAL_CST)
9808 r = TREE_REAL_CST (arg);
9809 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9812 arg = builtin_save_expr (arg);
9813 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9815 default:
9816 gcc_unreachable ();
9820 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9821 This builtin will generate code to return the appropriate floating
9822 point classification depending on the value of the floating point
9823 number passed in. The possible return values must be supplied as
9824 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9825 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9826 one floating point argument which is "type generic". */
9828 static tree
9829 fold_builtin_fpclassify (location_t loc, tree exp)
9831 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9832 arg, type, res, tmp;
9833 enum machine_mode mode;
9834 REAL_VALUE_TYPE r;
9835 char buf[128];
9837 /* Verify the required arguments in the original call. */
9838 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9839 INTEGER_TYPE, INTEGER_TYPE,
9840 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9841 return NULL_TREE;
9843 fp_nan = CALL_EXPR_ARG (exp, 0);
9844 fp_infinite = CALL_EXPR_ARG (exp, 1);
9845 fp_normal = CALL_EXPR_ARG (exp, 2);
9846 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9847 fp_zero = CALL_EXPR_ARG (exp, 4);
9848 arg = CALL_EXPR_ARG (exp, 5);
9849 type = TREE_TYPE (arg);
9850 mode = TYPE_MODE (type);
9851 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9853 /* fpclassify(x) ->
9854 isnan(x) ? FP_NAN :
9855 (fabs(x) == Inf ? FP_INFINITE :
9856 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9857 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9859 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9860 build_real (type, dconst0));
9861 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9862 tmp, fp_zero, fp_subnormal);
9864 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9865 real_from_string (&r, buf);
9866 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9867 arg, build_real (type, r));
9868 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9870 if (HONOR_INFINITIES (mode))
9872 real_inf (&r);
9873 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9874 build_real (type, r));
9875 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9876 fp_infinite, res);
9879 if (HONOR_NANS (mode))
9881 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9882 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9885 return res;
9888 /* Fold a call to an unordered comparison function such as
9889 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9890 being called and ARG0 and ARG1 are the arguments for the call.
9891 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9892 the opposite of the desired result. UNORDERED_CODE is used
9893 for modes that can hold NaNs and ORDERED_CODE is used for
9894 the rest. */
9896 static tree
9897 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9898 enum tree_code unordered_code,
9899 enum tree_code ordered_code)
9901 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9902 enum tree_code code;
9903 tree type0, type1;
9904 enum tree_code code0, code1;
9905 tree cmp_type = NULL_TREE;
9907 type0 = TREE_TYPE (arg0);
9908 type1 = TREE_TYPE (arg1);
9910 code0 = TREE_CODE (type0);
9911 code1 = TREE_CODE (type1);
9913 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9914 /* Choose the wider of two real types. */
9915 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9916 ? type0 : type1;
9917 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9918 cmp_type = type0;
9919 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9920 cmp_type = type1;
9922 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9923 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9925 if (unordered_code == UNORDERED_EXPR)
9927 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9928 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9929 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9932 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9933 : ordered_code;
9934 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9935 fold_build2_loc (loc, code, type, arg0, arg1));
9938 /* Fold a call to built-in function FNDECL with 0 arguments.
9939 IGNORE is true if the result of the function call is ignored. This
9940 function returns NULL_TREE if no simplification was possible. */
9942 static tree
9943 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9945 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9946 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9947 switch (fcode)
9949 CASE_FLT_FN (BUILT_IN_INF):
9950 case BUILT_IN_INFD32:
9951 case BUILT_IN_INFD64:
9952 case BUILT_IN_INFD128:
9953 return fold_builtin_inf (loc, type, true);
9955 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9956 return fold_builtin_inf (loc, type, false);
9958 case BUILT_IN_CLASSIFY_TYPE:
9959 return fold_builtin_classify_type (NULL_TREE);
9961 default:
9962 break;
9964 return NULL_TREE;
9967 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9968 IGNORE is true if the result of the function call is ignored. This
9969 function returns NULL_TREE if no simplification was possible. */
9971 static tree
9972 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9974 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9975 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9976 switch (fcode)
9979 case BUILT_IN_CONSTANT_P:
9981 tree val = fold_builtin_constant_p (arg0);
9983 /* Gimplification will pull the CALL_EXPR for the builtin out of
9984 an if condition. When not optimizing, we'll not CSE it back.
9985 To avoid link error types of regressions, return false now. */
9986 if (!val && !optimize)
9987 val = integer_zero_node;
9989 return val;
9992 case BUILT_IN_CLASSIFY_TYPE:
9993 return fold_builtin_classify_type (arg0);
9995 case BUILT_IN_STRLEN:
9996 return fold_builtin_strlen (loc, arg0);
9998 CASE_FLT_FN (BUILT_IN_FABS):
9999 return fold_builtin_fabs (loc, arg0, type);
10001 case BUILT_IN_ABS:
10002 case BUILT_IN_LABS:
10003 case BUILT_IN_LLABS:
10004 case BUILT_IN_IMAXABS:
10005 return fold_builtin_abs (loc, arg0, type);
10007 CASE_FLT_FN (BUILT_IN_CONJ):
10008 if (validate_arg (arg0, COMPLEX_TYPE)
10009 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10010 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10011 break;
10013 CASE_FLT_FN (BUILT_IN_CREAL):
10014 if (validate_arg (arg0, COMPLEX_TYPE)
10015 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10016 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10017 break;
10019 CASE_FLT_FN (BUILT_IN_CIMAG):
10020 if (validate_arg (arg0, COMPLEX_TYPE))
10021 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10022 break;
10024 CASE_FLT_FN (BUILT_IN_CCOS):
10025 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10027 CASE_FLT_FN (BUILT_IN_CCOSH):
10028 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10030 #ifdef HAVE_mpc
10031 CASE_FLT_FN (BUILT_IN_CSIN):
10032 if (validate_arg (arg0, COMPLEX_TYPE)
10033 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10034 return do_mpc_arg1 (arg0, type, mpc_sin);
10035 break;
10037 CASE_FLT_FN (BUILT_IN_CSINH):
10038 if (validate_arg (arg0, COMPLEX_TYPE)
10039 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10040 return do_mpc_arg1 (arg0, type, mpc_sinh);
10041 break;
10043 CASE_FLT_FN (BUILT_IN_CTAN):
10044 if (validate_arg (arg0, COMPLEX_TYPE)
10045 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10046 return do_mpc_arg1 (arg0, type, mpc_tan);
10047 break;
10049 CASE_FLT_FN (BUILT_IN_CTANH):
10050 if (validate_arg (arg0, COMPLEX_TYPE)
10051 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10052 return do_mpc_arg1 (arg0, type, mpc_tanh);
10053 break;
10055 CASE_FLT_FN (BUILT_IN_CLOG):
10056 if (validate_arg (arg0, COMPLEX_TYPE)
10057 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10058 return do_mpc_arg1 (arg0, type, mpc_log);
10059 break;
10061 CASE_FLT_FN (BUILT_IN_CSQRT):
10062 if (validate_arg (arg0, COMPLEX_TYPE)
10063 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10064 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10065 break;
10066 #endif
10068 CASE_FLT_FN (BUILT_IN_CABS):
10069 return fold_builtin_cabs (loc, arg0, type, fndecl);
10071 CASE_FLT_FN (BUILT_IN_CARG):
10072 return fold_builtin_carg (loc, arg0, type);
10074 CASE_FLT_FN (BUILT_IN_SQRT):
10075 return fold_builtin_sqrt (loc, arg0, type);
10077 CASE_FLT_FN (BUILT_IN_CBRT):
10078 return fold_builtin_cbrt (loc, arg0, type);
10080 CASE_FLT_FN (BUILT_IN_ASIN):
10081 if (validate_arg (arg0, REAL_TYPE))
10082 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10083 &dconstm1, &dconst1, true);
10084 break;
10086 CASE_FLT_FN (BUILT_IN_ACOS):
10087 if (validate_arg (arg0, REAL_TYPE))
10088 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10089 &dconstm1, &dconst1, true);
10090 break;
10092 CASE_FLT_FN (BUILT_IN_ATAN):
10093 if (validate_arg (arg0, REAL_TYPE))
10094 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10095 break;
10097 CASE_FLT_FN (BUILT_IN_ASINH):
10098 if (validate_arg (arg0, REAL_TYPE))
10099 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10100 break;
10102 CASE_FLT_FN (BUILT_IN_ACOSH):
10103 if (validate_arg (arg0, REAL_TYPE))
10104 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10105 &dconst1, NULL, true);
10106 break;
10108 CASE_FLT_FN (BUILT_IN_ATANH):
10109 if (validate_arg (arg0, REAL_TYPE))
10110 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10111 &dconstm1, &dconst1, false);
10112 break;
10114 CASE_FLT_FN (BUILT_IN_SIN):
10115 if (validate_arg (arg0, REAL_TYPE))
10116 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10117 break;
10119 CASE_FLT_FN (BUILT_IN_COS):
10120 return fold_builtin_cos (loc, arg0, type, fndecl);
10122 CASE_FLT_FN (BUILT_IN_TAN):
10123 return fold_builtin_tan (arg0, type);
10125 CASE_FLT_FN (BUILT_IN_CEXP):
10126 return fold_builtin_cexp (loc, arg0, type);
10128 CASE_FLT_FN (BUILT_IN_CEXPI):
10129 if (validate_arg (arg0, REAL_TYPE))
10130 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10131 break;
10133 CASE_FLT_FN (BUILT_IN_SINH):
10134 if (validate_arg (arg0, REAL_TYPE))
10135 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10136 break;
10138 CASE_FLT_FN (BUILT_IN_COSH):
10139 return fold_builtin_cosh (loc, arg0, type, fndecl);
10141 CASE_FLT_FN (BUILT_IN_TANH):
10142 if (validate_arg (arg0, REAL_TYPE))
10143 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10144 break;
10146 CASE_FLT_FN (BUILT_IN_ERF):
10147 if (validate_arg (arg0, REAL_TYPE))
10148 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10149 break;
10151 CASE_FLT_FN (BUILT_IN_ERFC):
10152 if (validate_arg (arg0, REAL_TYPE))
10153 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10154 break;
10156 CASE_FLT_FN (BUILT_IN_TGAMMA):
10157 if (validate_arg (arg0, REAL_TYPE))
10158 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10159 break;
10161 CASE_FLT_FN (BUILT_IN_EXP):
10162 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10164 CASE_FLT_FN (BUILT_IN_EXP2):
10165 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10167 CASE_FLT_FN (BUILT_IN_EXP10):
10168 CASE_FLT_FN (BUILT_IN_POW10):
10169 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10171 CASE_FLT_FN (BUILT_IN_EXPM1):
10172 if (validate_arg (arg0, REAL_TYPE))
10173 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10174 break;
10176 CASE_FLT_FN (BUILT_IN_LOG):
10177 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10179 CASE_FLT_FN (BUILT_IN_LOG2):
10180 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10182 CASE_FLT_FN (BUILT_IN_LOG10):
10183 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10185 CASE_FLT_FN (BUILT_IN_LOG1P):
10186 if (validate_arg (arg0, REAL_TYPE))
10187 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10188 &dconstm1, NULL, false);
10189 break;
10191 CASE_FLT_FN (BUILT_IN_J0):
10192 if (validate_arg (arg0, REAL_TYPE))
10193 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10194 NULL, NULL, 0);
10195 break;
10197 CASE_FLT_FN (BUILT_IN_J1):
10198 if (validate_arg (arg0, REAL_TYPE))
10199 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10200 NULL, NULL, 0);
10201 break;
10203 CASE_FLT_FN (BUILT_IN_Y0):
10204 if (validate_arg (arg0, REAL_TYPE))
10205 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10206 &dconst0, NULL, false);
10207 break;
10209 CASE_FLT_FN (BUILT_IN_Y1):
10210 if (validate_arg (arg0, REAL_TYPE))
10211 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10212 &dconst0, NULL, false);
10213 break;
10215 CASE_FLT_FN (BUILT_IN_NAN):
10216 case BUILT_IN_NAND32:
10217 case BUILT_IN_NAND64:
10218 case BUILT_IN_NAND128:
10219 return fold_builtin_nan (arg0, type, true);
10221 CASE_FLT_FN (BUILT_IN_NANS):
10222 return fold_builtin_nan (arg0, type, false);
10224 CASE_FLT_FN (BUILT_IN_FLOOR):
10225 return fold_builtin_floor (loc, fndecl, arg0);
10227 CASE_FLT_FN (BUILT_IN_CEIL):
10228 return fold_builtin_ceil (loc, fndecl, arg0);
10230 CASE_FLT_FN (BUILT_IN_TRUNC):
10231 return fold_builtin_trunc (loc, fndecl, arg0);
10233 CASE_FLT_FN (BUILT_IN_ROUND):
10234 return fold_builtin_round (loc, fndecl, arg0);
10236 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10237 CASE_FLT_FN (BUILT_IN_RINT):
10238 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10240 CASE_FLT_FN (BUILT_IN_LCEIL):
10241 CASE_FLT_FN (BUILT_IN_LLCEIL):
10242 CASE_FLT_FN (BUILT_IN_LFLOOR):
10243 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10244 CASE_FLT_FN (BUILT_IN_LROUND):
10245 CASE_FLT_FN (BUILT_IN_LLROUND):
10246 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10248 CASE_FLT_FN (BUILT_IN_LRINT):
10249 CASE_FLT_FN (BUILT_IN_LLRINT):
10250 return fold_fixed_mathfn (loc, fndecl, arg0);
10252 case BUILT_IN_BSWAP32:
10253 case BUILT_IN_BSWAP64:
10254 return fold_builtin_bswap (fndecl, arg0);
10256 CASE_INT_FN (BUILT_IN_FFS):
10257 CASE_INT_FN (BUILT_IN_CLZ):
10258 CASE_INT_FN (BUILT_IN_CTZ):
10259 CASE_INT_FN (BUILT_IN_POPCOUNT):
10260 CASE_INT_FN (BUILT_IN_PARITY):
10261 return fold_builtin_bitop (fndecl, arg0);
10263 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10264 return fold_builtin_signbit (loc, arg0, type);
10266 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10267 return fold_builtin_significand (loc, arg0, type);
10269 CASE_FLT_FN (BUILT_IN_ILOGB):
10270 CASE_FLT_FN (BUILT_IN_LOGB):
10271 return fold_builtin_logb (loc, arg0, type);
10273 case BUILT_IN_ISASCII:
10274 return fold_builtin_isascii (loc, arg0);
10276 case BUILT_IN_TOASCII:
10277 return fold_builtin_toascii (loc, arg0);
10279 case BUILT_IN_ISDIGIT:
10280 return fold_builtin_isdigit (loc, arg0);
10282 CASE_FLT_FN (BUILT_IN_FINITE):
10283 case BUILT_IN_FINITED32:
10284 case BUILT_IN_FINITED64:
10285 case BUILT_IN_FINITED128:
10286 case BUILT_IN_ISFINITE:
10288 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10289 if (ret)
10290 return ret;
10291 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10294 CASE_FLT_FN (BUILT_IN_ISINF):
10295 case BUILT_IN_ISINFD32:
10296 case BUILT_IN_ISINFD64:
10297 case BUILT_IN_ISINFD128:
10299 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10300 if (ret)
10301 return ret;
10302 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10305 case BUILT_IN_ISNORMAL:
10306 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10308 case BUILT_IN_ISINF_SIGN:
10309 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10311 CASE_FLT_FN (BUILT_IN_ISNAN):
10312 case BUILT_IN_ISNAND32:
10313 case BUILT_IN_ISNAND64:
10314 case BUILT_IN_ISNAND128:
10315 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10317 case BUILT_IN_PRINTF:
10318 case BUILT_IN_PRINTF_UNLOCKED:
10319 case BUILT_IN_VPRINTF:
10320 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10322 default:
10323 break;
10326 return NULL_TREE;
10330 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10331 IGNORE is true if the result of the function call is ignored. This
10332 function returns NULL_TREE if no simplification was possible. */
10334 static tree
10335 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10337 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10338 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10340 switch (fcode)
10342 CASE_FLT_FN (BUILT_IN_JN):
10343 if (validate_arg (arg0, INTEGER_TYPE)
10344 && validate_arg (arg1, REAL_TYPE))
10345 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10346 break;
10348 CASE_FLT_FN (BUILT_IN_YN):
10349 if (validate_arg (arg0, INTEGER_TYPE)
10350 && validate_arg (arg1, REAL_TYPE))
10351 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10352 &dconst0, false);
10353 break;
10355 CASE_FLT_FN (BUILT_IN_DREM):
10356 CASE_FLT_FN (BUILT_IN_REMAINDER):
10357 if (validate_arg (arg0, REAL_TYPE)
10358 && validate_arg(arg1, REAL_TYPE))
10359 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10360 break;
10362 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10363 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10364 if (validate_arg (arg0, REAL_TYPE)
10365 && validate_arg(arg1, POINTER_TYPE))
10366 return do_mpfr_lgamma_r (arg0, arg1, type);
10367 break;
10369 CASE_FLT_FN (BUILT_IN_ATAN2):
10370 if (validate_arg (arg0, REAL_TYPE)
10371 && validate_arg(arg1, REAL_TYPE))
10372 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10373 break;
10375 CASE_FLT_FN (BUILT_IN_FDIM):
10376 if (validate_arg (arg0, REAL_TYPE)
10377 && validate_arg(arg1, REAL_TYPE))
10378 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10379 break;
10381 CASE_FLT_FN (BUILT_IN_HYPOT):
10382 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10384 #ifdef HAVE_mpc_pow
10385 CASE_FLT_FN (BUILT_IN_CPOW):
10386 if (validate_arg (arg0, COMPLEX_TYPE)
10387 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10388 && validate_arg (arg1, COMPLEX_TYPE)
10389 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10390 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10391 break;
10392 #endif
10394 CASE_FLT_FN (BUILT_IN_LDEXP):
10395 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10396 CASE_FLT_FN (BUILT_IN_SCALBN):
10397 CASE_FLT_FN (BUILT_IN_SCALBLN):
10398 return fold_builtin_load_exponent (loc, arg0, arg1,
10399 type, /*ldexp=*/false);
10401 CASE_FLT_FN (BUILT_IN_FREXP):
10402 return fold_builtin_frexp (loc, arg0, arg1, type);
10404 CASE_FLT_FN (BUILT_IN_MODF):
10405 return fold_builtin_modf (loc, arg0, arg1, type);
10407 case BUILT_IN_BZERO:
10408 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10410 case BUILT_IN_FPUTS:
10411 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10413 case BUILT_IN_FPUTS_UNLOCKED:
10414 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10416 case BUILT_IN_STRSTR:
10417 return fold_builtin_strstr (loc, arg0, arg1, type);
10419 case BUILT_IN_STRCAT:
10420 return fold_builtin_strcat (loc, arg0, arg1);
10422 case BUILT_IN_STRSPN:
10423 return fold_builtin_strspn (loc, arg0, arg1);
10425 case BUILT_IN_STRCSPN:
10426 return fold_builtin_strcspn (loc, arg0, arg1);
10428 case BUILT_IN_STRCHR:
10429 case BUILT_IN_INDEX:
10430 return fold_builtin_strchr (loc, arg0, arg1, type);
10432 case BUILT_IN_STRRCHR:
10433 case BUILT_IN_RINDEX:
10434 return fold_builtin_strrchr (loc, arg0, arg1, type);
10436 case BUILT_IN_STRCPY:
10437 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10439 case BUILT_IN_STPCPY:
10440 if (ignore)
10442 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10443 if (!fn)
10444 break;
10446 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10448 else
10449 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10450 break;
10452 case BUILT_IN_STRCMP:
10453 return fold_builtin_strcmp (loc, arg0, arg1);
10455 case BUILT_IN_STRPBRK:
10456 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10458 case BUILT_IN_EXPECT:
10459 return fold_builtin_expect (loc, arg0, arg1);
10461 CASE_FLT_FN (BUILT_IN_POW):
10462 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10464 CASE_FLT_FN (BUILT_IN_POWI):
10465 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10467 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10468 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10470 CASE_FLT_FN (BUILT_IN_FMIN):
10471 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10473 CASE_FLT_FN (BUILT_IN_FMAX):
10474 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10476 case BUILT_IN_ISGREATER:
10477 return fold_builtin_unordered_cmp (loc, fndecl,
10478 arg0, arg1, UNLE_EXPR, LE_EXPR);
10479 case BUILT_IN_ISGREATEREQUAL:
10480 return fold_builtin_unordered_cmp (loc, fndecl,
10481 arg0, arg1, UNLT_EXPR, LT_EXPR);
10482 case BUILT_IN_ISLESS:
10483 return fold_builtin_unordered_cmp (loc, fndecl,
10484 arg0, arg1, UNGE_EXPR, GE_EXPR);
10485 case BUILT_IN_ISLESSEQUAL:
10486 return fold_builtin_unordered_cmp (loc, fndecl,
10487 arg0, arg1, UNGT_EXPR, GT_EXPR);
10488 case BUILT_IN_ISLESSGREATER:
10489 return fold_builtin_unordered_cmp (loc, fndecl,
10490 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10491 case BUILT_IN_ISUNORDERED:
10492 return fold_builtin_unordered_cmp (loc, fndecl,
10493 arg0, arg1, UNORDERED_EXPR,
10494 NOP_EXPR);
10496 /* We do the folding for va_start in the expander. */
10497 case BUILT_IN_VA_START:
10498 break;
10500 case BUILT_IN_SPRINTF:
10501 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10503 case BUILT_IN_OBJECT_SIZE:
10504 return fold_builtin_object_size (arg0, arg1);
10506 case BUILT_IN_PRINTF:
10507 case BUILT_IN_PRINTF_UNLOCKED:
10508 case BUILT_IN_VPRINTF:
10509 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10511 case BUILT_IN_PRINTF_CHK:
10512 case BUILT_IN_VPRINTF_CHK:
10513 if (!validate_arg (arg0, INTEGER_TYPE)
10514 || TREE_SIDE_EFFECTS (arg0))
10515 return NULL_TREE;
10516 else
10517 return fold_builtin_printf (loc, fndecl,
10518 arg1, NULL_TREE, ignore, fcode);
10519 break;
10521 case BUILT_IN_FPRINTF:
10522 case BUILT_IN_FPRINTF_UNLOCKED:
10523 case BUILT_IN_VFPRINTF:
10524 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10525 ignore, fcode);
10527 default:
10528 break;
10530 return NULL_TREE;
10533 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10534 and ARG2. IGNORE is true if the result of the function call is ignored.
10535 This function returns NULL_TREE if no simplification was possible. */
10537 static tree
10538 fold_builtin_3 (location_t loc, tree fndecl,
10539 tree arg0, tree arg1, tree arg2, bool ignore)
10541 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10542 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10543 switch (fcode)
10546 CASE_FLT_FN (BUILT_IN_SINCOS):
10547 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10549 CASE_FLT_FN (BUILT_IN_FMA):
10550 if (validate_arg (arg0, REAL_TYPE)
10551 && validate_arg(arg1, REAL_TYPE)
10552 && validate_arg(arg2, REAL_TYPE))
10553 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10554 break;
10556 CASE_FLT_FN (BUILT_IN_REMQUO):
10557 if (validate_arg (arg0, REAL_TYPE)
10558 && validate_arg(arg1, REAL_TYPE)
10559 && validate_arg(arg2, POINTER_TYPE))
10560 return do_mpfr_remquo (arg0, arg1, arg2);
10561 break;
10563 case BUILT_IN_MEMSET:
10564 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10566 case BUILT_IN_BCOPY:
10567 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10568 void_type_node, true, /*endp=*/3);
10570 case BUILT_IN_MEMCPY:
10571 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10572 type, ignore, /*endp=*/0);
10574 case BUILT_IN_MEMPCPY:
10575 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10576 type, ignore, /*endp=*/1);
10578 case BUILT_IN_MEMMOVE:
10579 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10580 type, ignore, /*endp=*/3);
10582 case BUILT_IN_STRNCAT:
10583 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10585 case BUILT_IN_STRNCPY:
10586 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10588 case BUILT_IN_STRNCMP:
10589 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10591 case BUILT_IN_MEMCHR:
10592 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10594 case BUILT_IN_BCMP:
10595 case BUILT_IN_MEMCMP:
10596 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10598 case BUILT_IN_SPRINTF:
10599 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10601 case BUILT_IN_STRCPY_CHK:
10602 case BUILT_IN_STPCPY_CHK:
10603 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10604 ignore, fcode);
10606 case BUILT_IN_STRCAT_CHK:
10607 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10609 case BUILT_IN_PRINTF_CHK:
10610 case BUILT_IN_VPRINTF_CHK:
10611 if (!validate_arg (arg0, INTEGER_TYPE)
10612 || TREE_SIDE_EFFECTS (arg0))
10613 return NULL_TREE;
10614 else
10615 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10616 break;
10618 case BUILT_IN_FPRINTF:
10619 case BUILT_IN_FPRINTF_UNLOCKED:
10620 case BUILT_IN_VFPRINTF:
10621 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10622 ignore, fcode);
10624 case BUILT_IN_FPRINTF_CHK:
10625 case BUILT_IN_VFPRINTF_CHK:
10626 if (!validate_arg (arg1, INTEGER_TYPE)
10627 || TREE_SIDE_EFFECTS (arg1))
10628 return NULL_TREE;
10629 else
10630 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10631 ignore, fcode);
10633 default:
10634 break;
10636 return NULL_TREE;
10639 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10640 ARG2, and ARG3. IGNORE is true if the result of the function call is
10641 ignored. This function returns NULL_TREE if no simplification was
10642 possible. */
10644 static tree
10645 fold_builtin_4 (location_t loc, tree fndecl,
10646 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10648 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10650 switch (fcode)
10652 case BUILT_IN_MEMCPY_CHK:
10653 case BUILT_IN_MEMPCPY_CHK:
10654 case BUILT_IN_MEMMOVE_CHK:
10655 case BUILT_IN_MEMSET_CHK:
10656 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10657 NULL_TREE, ignore,
10658 DECL_FUNCTION_CODE (fndecl));
10660 case BUILT_IN_STRNCPY_CHK:
10661 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10663 case BUILT_IN_STRNCAT_CHK:
10664 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10666 case BUILT_IN_FPRINTF_CHK:
10667 case BUILT_IN_VFPRINTF_CHK:
10668 if (!validate_arg (arg1, INTEGER_TYPE)
10669 || TREE_SIDE_EFFECTS (arg1))
10670 return NULL_TREE;
10671 else
10672 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10673 ignore, fcode);
10674 break;
10676 default:
10677 break;
10679 return NULL_TREE;
10682 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10683 arguments, where NARGS <= 4. IGNORE is true if the result of the
10684 function call is ignored. This function returns NULL_TREE if no
10685 simplification was possible. Note that this only folds builtins with
10686 fixed argument patterns. Foldings that do varargs-to-varargs
10687 transformations, or that match calls with more than 4 arguments,
10688 need to be handled with fold_builtin_varargs instead. */
10690 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10692 static tree
10693 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10695 tree ret = NULL_TREE;
10697 switch (nargs)
10699 case 0:
10700 ret = fold_builtin_0 (loc, fndecl, ignore);
10701 break;
10702 case 1:
10703 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10704 break;
10705 case 2:
10706 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10707 break;
10708 case 3:
10709 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10710 break;
10711 case 4:
10712 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10713 ignore);
10714 break;
10715 default:
10716 break;
10718 if (ret)
10720 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10721 SET_EXPR_LOCATION (ret, loc);
10722 TREE_NO_WARNING (ret) = 1;
10723 return ret;
10725 return NULL_TREE;
10728 /* Builtins with folding operations that operate on "..." arguments
10729 need special handling; we need to store the arguments in a convenient
10730 data structure before attempting any folding. Fortunately there are
10731 only a few builtins that fall into this category. FNDECL is the
10732 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10733 result of the function call is ignored. */
10735 static tree
10736 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10737 bool ignore ATTRIBUTE_UNUSED)
10739 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10740 tree ret = NULL_TREE;
10742 switch (fcode)
10744 case BUILT_IN_SPRINTF_CHK:
10745 case BUILT_IN_VSPRINTF_CHK:
10746 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10747 break;
10749 case BUILT_IN_SNPRINTF_CHK:
10750 case BUILT_IN_VSNPRINTF_CHK:
10751 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10752 break;
10754 case BUILT_IN_FPCLASSIFY:
10755 ret = fold_builtin_fpclassify (loc, exp);
10756 break;
10758 default:
10759 break;
10761 if (ret)
10763 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10764 SET_EXPR_LOCATION (ret, loc);
10765 TREE_NO_WARNING (ret) = 1;
10766 return ret;
10768 return NULL_TREE;
10771 /* Return true if FNDECL shouldn't be folded right now.
10772 If a built-in function has an inline attribute always_inline
10773 wrapper, defer folding it after always_inline functions have
10774 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10775 might not be performed. */
10777 static bool
10778 avoid_folding_inline_builtin (tree fndecl)
10780 return (DECL_DECLARED_INLINE_P (fndecl)
10781 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10782 && cfun
10783 && !cfun->always_inline_functions_inlined
10784 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10787 /* A wrapper function for builtin folding that prevents warnings for
10788 "statement without effect" and the like, caused by removing the
10789 call node earlier than the warning is generated. */
10791 tree
10792 fold_call_expr (location_t loc, tree exp, bool ignore)
10794 tree ret = NULL_TREE;
10795 tree fndecl = get_callee_fndecl (exp);
10796 if (fndecl
10797 && TREE_CODE (fndecl) == FUNCTION_DECL
10798 && DECL_BUILT_IN (fndecl)
10799 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10800 yet. Defer folding until we see all the arguments
10801 (after inlining). */
10802 && !CALL_EXPR_VA_ARG_PACK (exp))
10804 int nargs = call_expr_nargs (exp);
10806 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10807 instead last argument is __builtin_va_arg_pack (). Defer folding
10808 even in that case, until arguments are finalized. */
10809 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10811 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10812 if (fndecl2
10813 && TREE_CODE (fndecl2) == FUNCTION_DECL
10814 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10815 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10816 return NULL_TREE;
10819 if (avoid_folding_inline_builtin (fndecl))
10820 return NULL_TREE;
10822 /* FIXME: Don't use a list in this interface. */
10823 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10824 return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore);
10825 else
10827 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10829 tree *args = CALL_EXPR_ARGP (exp);
10830 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10832 if (!ret)
10833 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10834 if (ret)
10835 return ret;
10838 return NULL_TREE;
10841 /* Conveniently construct a function call expression. FNDECL names the
10842 function to be called and ARGLIST is a TREE_LIST of arguments. */
10844 tree
10845 build_function_call_expr (location_t loc, tree fndecl, tree arglist)
10847 tree fntype = TREE_TYPE (fndecl);
10848 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10849 int n = list_length (arglist);
10850 tree *argarray = (tree *) alloca (n * sizeof (tree));
10851 int i;
10853 for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist))
10854 argarray[i] = TREE_VALUE (arglist);
10855 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10858 /* Conveniently construct a function call expression. FNDECL names the
10859 function to be called, N is the number of arguments, and the "..."
10860 parameters are the argument expressions. */
10862 tree
10863 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10865 va_list ap;
10866 tree fntype = TREE_TYPE (fndecl);
10867 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10868 tree *argarray = (tree *) alloca (n * sizeof (tree));
10869 int i;
10871 va_start (ap, n);
10872 for (i = 0; i < n; i++)
10873 argarray[i] = va_arg (ap, tree);
10874 va_end (ap);
10875 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10878 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10879 N arguments are passed in the array ARGARRAY. */
10881 tree
10882 fold_builtin_call_array (location_t loc, tree type,
10883 tree fn,
10884 int n,
10885 tree *argarray)
10887 tree ret = NULL_TREE;
10888 int i;
10889 tree exp;
10891 if (TREE_CODE (fn) == ADDR_EXPR)
10893 tree fndecl = TREE_OPERAND (fn, 0);
10894 if (TREE_CODE (fndecl) == FUNCTION_DECL
10895 && DECL_BUILT_IN (fndecl))
10897 /* If last argument is __builtin_va_arg_pack (), arguments to this
10898 function are not finalized yet. Defer folding until they are. */
10899 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10901 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10902 if (fndecl2
10903 && TREE_CODE (fndecl2) == FUNCTION_DECL
10904 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10905 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10906 return build_call_array_loc (loc, type, fn, n, argarray);
10908 if (avoid_folding_inline_builtin (fndecl))
10909 return build_call_array_loc (loc, type, fn, n, argarray);
10910 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10912 tree arglist = NULL_TREE;
10913 for (i = n - 1; i >= 0; i--)
10914 arglist = tree_cons (NULL_TREE, argarray[i], arglist);
10915 ret = targetm.fold_builtin (fndecl, arglist, false);
10916 if (ret)
10917 return ret;
10918 return build_call_array_loc (loc, type, fn, n, argarray);
10920 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10922 /* First try the transformations that don't require consing up
10923 an exp. */
10924 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10925 if (ret)
10926 return ret;
10929 /* If we got this far, we need to build an exp. */
10930 exp = build_call_array_loc (loc, type, fn, n, argarray);
10931 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10932 return ret ? ret : exp;
10936 return build_call_array_loc (loc, type, fn, n, argarray);
10939 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10940 along with N new arguments specified as the "..." parameters. SKIP
10941 is the number of arguments in EXP to be omitted. This function is used
10942 to do varargs-to-varargs transformations. */
10944 static tree
10945 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10947 int oldnargs = call_expr_nargs (exp);
10948 int nargs = oldnargs - skip + n;
10949 tree fntype = TREE_TYPE (fndecl);
10950 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10951 tree *buffer;
10953 if (n > 0)
10955 int i, j;
10956 va_list ap;
10958 buffer = XALLOCAVEC (tree, nargs);
10959 va_start (ap, n);
10960 for (i = 0; i < n; i++)
10961 buffer[i] = va_arg (ap, tree);
10962 va_end (ap);
10963 for (j = skip; j < oldnargs; j++, i++)
10964 buffer[i] = CALL_EXPR_ARG (exp, j);
10966 else
10967 buffer = CALL_EXPR_ARGP (exp) + skip;
10969 return fold (build_call_array_loc (loc, TREE_TYPE (exp), fn, nargs, buffer));
10972 /* Validate a single argument ARG against a tree code CODE representing
10973 a type. */
10975 static bool
10976 validate_arg (const_tree arg, enum tree_code code)
10978 if (!arg)
10979 return false;
10980 else if (code == POINTER_TYPE)
10981 return POINTER_TYPE_P (TREE_TYPE (arg));
10982 else if (code == INTEGER_TYPE)
10983 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10984 return code == TREE_CODE (TREE_TYPE (arg));
10987 /* This function validates the types of a function call argument list
10988 against a specified list of tree_codes. If the last specifier is a 0,
10989 that represents an ellipses, otherwise the last specifier must be a
10990 VOID_TYPE.
10992 This is the GIMPLE version of validate_arglist. Eventually we want to
10993 completely convert builtins.c to work from GIMPLEs and the tree based
10994 validate_arglist will then be removed. */
10996 bool
10997 validate_gimple_arglist (const_gimple call, ...)
10999 enum tree_code code;
11000 bool res = 0;
11001 va_list ap;
11002 const_tree arg;
11003 size_t i;
11005 va_start (ap, call);
11006 i = 0;
11010 code = (enum tree_code) va_arg (ap, int);
11011 switch (code)
11013 case 0:
11014 /* This signifies an ellipses, any further arguments are all ok. */
11015 res = true;
11016 goto end;
11017 case VOID_TYPE:
11018 /* This signifies an endlink, if no arguments remain, return
11019 true, otherwise return false. */
11020 res = (i == gimple_call_num_args (call));
11021 goto end;
11022 default:
11023 /* If no parameters remain or the parameter's code does not
11024 match the specified code, return false. Otherwise continue
11025 checking any remaining arguments. */
11026 arg = gimple_call_arg (call, i++);
11027 if (!validate_arg (arg, code))
11028 goto end;
11029 break;
11032 while (1);
11034 /* We need gotos here since we can only have one VA_CLOSE in a
11035 function. */
11036 end: ;
11037 va_end (ap);
11039 return res;
11042 /* This function validates the types of a function call argument list
11043 against a specified list of tree_codes. If the last specifier is a 0,
11044 that represents an ellipses, otherwise the last specifier must be a
11045 VOID_TYPE. */
11047 bool
11048 validate_arglist (const_tree callexpr, ...)
11050 enum tree_code code;
11051 bool res = 0;
11052 va_list ap;
11053 const_call_expr_arg_iterator iter;
11054 const_tree arg;
11056 va_start (ap, callexpr);
11057 init_const_call_expr_arg_iterator (callexpr, &iter);
11061 code = (enum tree_code) va_arg (ap, int);
11062 switch (code)
11064 case 0:
11065 /* This signifies an ellipses, any further arguments are all ok. */
11066 res = true;
11067 goto end;
11068 case VOID_TYPE:
11069 /* This signifies an endlink, if no arguments remain, return
11070 true, otherwise return false. */
11071 res = !more_const_call_expr_args_p (&iter);
11072 goto end;
11073 default:
11074 /* If no parameters remain or the parameter's code does not
11075 match the specified code, return false. Otherwise continue
11076 checking any remaining arguments. */
11077 arg = next_const_call_expr_arg (&iter);
11078 if (!validate_arg (arg, code))
11079 goto end;
11080 break;
11083 while (1);
11085 /* We need gotos here since we can only have one VA_CLOSE in a
11086 function. */
11087 end: ;
11088 va_end (ap);
11090 return res;
11093 /* Default target-specific builtin expander that does nothing. */
11096 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11097 rtx target ATTRIBUTE_UNUSED,
11098 rtx subtarget ATTRIBUTE_UNUSED,
11099 enum machine_mode mode ATTRIBUTE_UNUSED,
11100 int ignore ATTRIBUTE_UNUSED)
11102 return NULL_RTX;
11105 /* Returns true is EXP represents data that would potentially reside
11106 in a readonly section. */
11108 static bool
11109 readonly_data_expr (tree exp)
11111 STRIP_NOPS (exp);
11113 if (TREE_CODE (exp) != ADDR_EXPR)
11114 return false;
11116 exp = get_base_address (TREE_OPERAND (exp, 0));
11117 if (!exp)
11118 return false;
11120 /* Make sure we call decl_readonly_section only for trees it
11121 can handle (since it returns true for everything it doesn't
11122 understand). */
11123 if (TREE_CODE (exp) == STRING_CST
11124 || TREE_CODE (exp) == CONSTRUCTOR
11125 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11126 return decl_readonly_section (exp, 0);
11127 else
11128 return false;
11131 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11132 to the call, and TYPE is its return type.
11134 Return NULL_TREE if no simplification was possible, otherwise return the
11135 simplified form of the call as a tree.
11137 The simplified form may be a constant or other expression which
11138 computes the same value, but in a more efficient manner (including
11139 calls to other builtin functions).
11141 The call may contain arguments which need to be evaluated, but
11142 which are not useful to determine the result of the call. In
11143 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11144 COMPOUND_EXPR will be an argument which must be evaluated.
11145 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11146 COMPOUND_EXPR in the chain will contain the tree for the simplified
11147 form of the builtin function call. */
11149 static tree
11150 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11152 if (!validate_arg (s1, POINTER_TYPE)
11153 || !validate_arg (s2, POINTER_TYPE))
11154 return NULL_TREE;
11155 else
11157 tree fn;
11158 const char *p1, *p2;
11160 p2 = c_getstr (s2);
11161 if (p2 == NULL)
11162 return NULL_TREE;
11164 p1 = c_getstr (s1);
11165 if (p1 != NULL)
11167 const char *r = strstr (p1, p2);
11168 tree tem;
11170 if (r == NULL)
11171 return build_int_cst (TREE_TYPE (s1), 0);
11173 /* Return an offset into the constant string argument. */
11174 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11175 s1, size_int (r - p1));
11176 return fold_convert_loc (loc, type, tem);
11179 /* The argument is const char *, and the result is char *, so we need
11180 a type conversion here to avoid a warning. */
11181 if (p2[0] == '\0')
11182 return fold_convert_loc (loc, type, s1);
11184 if (p2[1] != '\0')
11185 return NULL_TREE;
11187 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11188 if (!fn)
11189 return NULL_TREE;
11191 /* New argument list transforming strstr(s1, s2) to
11192 strchr(s1, s2[0]). */
11193 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11197 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11198 the call, and TYPE is its return type.
11200 Return NULL_TREE if no simplification was possible, otherwise return the
11201 simplified form of the call as a tree.
11203 The simplified form may be a constant or other expression which
11204 computes the same value, but in a more efficient manner (including
11205 calls to other builtin functions).
11207 The call may contain arguments which need to be evaluated, but
11208 which are not useful to determine the result of the call. In
11209 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11210 COMPOUND_EXPR will be an argument which must be evaluated.
11211 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11212 COMPOUND_EXPR in the chain will contain the tree for the simplified
11213 form of the builtin function call. */
11215 static tree
11216 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11218 if (!validate_arg (s1, POINTER_TYPE)
11219 || !validate_arg (s2, INTEGER_TYPE))
11220 return NULL_TREE;
11221 else
11223 const char *p1;
11225 if (TREE_CODE (s2) != INTEGER_CST)
11226 return NULL_TREE;
11228 p1 = c_getstr (s1);
11229 if (p1 != NULL)
11231 char c;
11232 const char *r;
11233 tree tem;
11235 if (target_char_cast (s2, &c))
11236 return NULL_TREE;
11238 r = strchr (p1, c);
11240 if (r == NULL)
11241 return build_int_cst (TREE_TYPE (s1), 0);
11243 /* Return an offset into the constant string argument. */
11244 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11245 s1, size_int (r - p1));
11246 return fold_convert_loc (loc, type, tem);
11248 return NULL_TREE;
11252 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11253 the call, and TYPE is its return type.
11255 Return NULL_TREE if no simplification was possible, otherwise return the
11256 simplified form of the call as a tree.
11258 The simplified form may be a constant or other expression which
11259 computes the same value, but in a more efficient manner (including
11260 calls to other builtin functions).
11262 The call may contain arguments which need to be evaluated, but
11263 which are not useful to determine the result of the call. In
11264 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11265 COMPOUND_EXPR will be an argument which must be evaluated.
11266 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11267 COMPOUND_EXPR in the chain will contain the tree for the simplified
11268 form of the builtin function call. */
11270 static tree
11271 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11273 if (!validate_arg (s1, POINTER_TYPE)
11274 || !validate_arg (s2, INTEGER_TYPE))
11275 return NULL_TREE;
11276 else
11278 tree fn;
11279 const char *p1;
11281 if (TREE_CODE (s2) != INTEGER_CST)
11282 return NULL_TREE;
11284 p1 = c_getstr (s1);
11285 if (p1 != NULL)
11287 char c;
11288 const char *r;
11289 tree tem;
11291 if (target_char_cast (s2, &c))
11292 return NULL_TREE;
11294 r = strrchr (p1, c);
11296 if (r == NULL)
11297 return build_int_cst (TREE_TYPE (s1), 0);
11299 /* Return an offset into the constant string argument. */
11300 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11301 s1, size_int (r - p1));
11302 return fold_convert_loc (loc, type, tem);
11305 if (! integer_zerop (s2))
11306 return NULL_TREE;
11308 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11309 if (!fn)
11310 return NULL_TREE;
11312 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11313 return build_call_expr_loc (loc, fn, 2, s1, s2);
11317 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11318 to the call, and TYPE is its return type.
11320 Return NULL_TREE if no simplification was possible, otherwise return the
11321 simplified form of the call as a tree.
11323 The simplified form may be a constant or other expression which
11324 computes the same value, but in a more efficient manner (including
11325 calls to other builtin functions).
11327 The call may contain arguments which need to be evaluated, but
11328 which are not useful to determine the result of the call. In
11329 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11330 COMPOUND_EXPR will be an argument which must be evaluated.
11331 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11332 COMPOUND_EXPR in the chain will contain the tree for the simplified
11333 form of the builtin function call. */
11335 static tree
11336 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11338 if (!validate_arg (s1, POINTER_TYPE)
11339 || !validate_arg (s2, POINTER_TYPE))
11340 return NULL_TREE;
11341 else
11343 tree fn;
11344 const char *p1, *p2;
11346 p2 = c_getstr (s2);
11347 if (p2 == NULL)
11348 return NULL_TREE;
11350 p1 = c_getstr (s1);
11351 if (p1 != NULL)
11353 const char *r = strpbrk (p1, p2);
11354 tree tem;
11356 if (r == NULL)
11357 return build_int_cst (TREE_TYPE (s1), 0);
11359 /* Return an offset into the constant string argument. */
11360 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11361 s1, size_int (r - p1));
11362 return fold_convert_loc (loc, type, tem);
11365 if (p2[0] == '\0')
11366 /* strpbrk(x, "") == NULL.
11367 Evaluate and ignore s1 in case it had side-effects. */
11368 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11370 if (p2[1] != '\0')
11371 return NULL_TREE; /* Really call strpbrk. */
11373 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11374 if (!fn)
11375 return NULL_TREE;
11377 /* New argument list transforming strpbrk(s1, s2) to
11378 strchr(s1, s2[0]). */
11379 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11383 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11384 to the call.
11386 Return NULL_TREE if no simplification was possible, otherwise return the
11387 simplified form of the call as a tree.
11389 The simplified form may be a constant or other expression which
11390 computes the same value, but in a more efficient manner (including
11391 calls to other builtin functions).
11393 The call may contain arguments which need to be evaluated, but
11394 which are not useful to determine the result of the call. In
11395 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11396 COMPOUND_EXPR will be an argument which must be evaluated.
11397 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11398 COMPOUND_EXPR in the chain will contain the tree for the simplified
11399 form of the builtin function call. */
11401 static tree
11402 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11404 if (!validate_arg (dst, POINTER_TYPE)
11405 || !validate_arg (src, POINTER_TYPE))
11406 return NULL_TREE;
11407 else
11409 const char *p = c_getstr (src);
11411 /* If the string length is zero, return the dst parameter. */
11412 if (p && *p == '\0')
11413 return dst;
11415 if (optimize_insn_for_speed_p ())
11417 /* See if we can store by pieces into (dst + strlen(dst)). */
11418 tree newdst, call;
11419 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11420 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11422 if (!strlen_fn || !strcpy_fn)
11423 return NULL_TREE;
11425 /* If we don't have a movstr we don't want to emit an strcpy
11426 call. We have to do that if the length of the source string
11427 isn't computable (in that case we can use memcpy probably
11428 later expanding to a sequence of mov instructions). If we
11429 have movstr instructions we can emit strcpy calls. */
11430 if (!HAVE_movstr)
11432 tree len = c_strlen (src, 1);
11433 if (! len || TREE_SIDE_EFFECTS (len))
11434 return NULL_TREE;
11437 /* Stabilize the argument list. */
11438 dst = builtin_save_expr (dst);
11440 /* Create strlen (dst). */
11441 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11442 /* Create (dst p+ strlen (dst)). */
11444 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11445 TREE_TYPE (dst), dst, newdst);
11446 newdst = builtin_save_expr (newdst);
11448 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11449 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11451 return NULL_TREE;
11455 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11456 arguments to the call.
11458 Return NULL_TREE if no simplification was possible, otherwise return the
11459 simplified form of the call as a tree.
11461 The simplified form may be a constant or other expression which
11462 computes the same value, but in a more efficient manner (including
11463 calls to other builtin functions).
11465 The call may contain arguments which need to be evaluated, but
11466 which are not useful to determine the result of the call. In
11467 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11468 COMPOUND_EXPR will be an argument which must be evaluated.
11469 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11470 COMPOUND_EXPR in the chain will contain the tree for the simplified
11471 form of the builtin function call. */
11473 static tree
11474 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11476 if (!validate_arg (dst, POINTER_TYPE)
11477 || !validate_arg (src, POINTER_TYPE)
11478 || !validate_arg (len, INTEGER_TYPE))
11479 return NULL_TREE;
11480 else
11482 const char *p = c_getstr (src);
11484 /* If the requested length is zero, or the src parameter string
11485 length is zero, return the dst parameter. */
11486 if (integer_zerop (len) || (p && *p == '\0'))
11487 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11489 /* If the requested len is greater than or equal to the string
11490 length, call strcat. */
11491 if (TREE_CODE (len) == INTEGER_CST && p
11492 && compare_tree_int (len, strlen (p)) >= 0)
11494 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11496 /* If the replacement _DECL isn't initialized, don't do the
11497 transformation. */
11498 if (!fn)
11499 return NULL_TREE;
11501 return build_call_expr_loc (loc, fn, 2, dst, src);
11503 return NULL_TREE;
11507 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11508 to the call.
11510 Return NULL_TREE if no simplification was possible, otherwise return the
11511 simplified form of the call as a tree.
11513 The simplified form may be a constant or other expression which
11514 computes the same value, but in a more efficient manner (including
11515 calls to other builtin functions).
11517 The call may contain arguments which need to be evaluated, but
11518 which are not useful to determine the result of the call. In
11519 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11520 COMPOUND_EXPR will be an argument which must be evaluated.
11521 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11522 COMPOUND_EXPR in the chain will contain the tree for the simplified
11523 form of the builtin function call. */
11525 static tree
11526 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11528 if (!validate_arg (s1, POINTER_TYPE)
11529 || !validate_arg (s2, POINTER_TYPE))
11530 return NULL_TREE;
11531 else
11533 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11535 /* If both arguments are constants, evaluate at compile-time. */
11536 if (p1 && p2)
11538 const size_t r = strspn (p1, p2);
11539 return size_int (r);
11542 /* If either argument is "", return NULL_TREE. */
11543 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11544 /* Evaluate and ignore both arguments in case either one has
11545 side-effects. */
11546 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11547 s1, s2);
11548 return NULL_TREE;
11552 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11553 to the call.
11555 Return NULL_TREE if no simplification was possible, otherwise return the
11556 simplified form of the call as a tree.
11558 The simplified form may be a constant or other expression which
11559 computes the same value, but in a more efficient manner (including
11560 calls to other builtin functions).
11562 The call may contain arguments which need to be evaluated, but
11563 which are not useful to determine the result of the call. In
11564 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11565 COMPOUND_EXPR will be an argument which must be evaluated.
11566 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11567 COMPOUND_EXPR in the chain will contain the tree for the simplified
11568 form of the builtin function call. */
11570 static tree
11571 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11573 if (!validate_arg (s1, POINTER_TYPE)
11574 || !validate_arg (s2, POINTER_TYPE))
11575 return NULL_TREE;
11576 else
11578 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11580 /* If both arguments are constants, evaluate at compile-time. */
11581 if (p1 && p2)
11583 const size_t r = strcspn (p1, p2);
11584 return size_int (r);
11587 /* If the first argument is "", return NULL_TREE. */
11588 if (p1 && *p1 == '\0')
11590 /* Evaluate and ignore argument s2 in case it has
11591 side-effects. */
11592 return omit_one_operand_loc (loc, size_type_node,
11593 size_zero_node, s2);
11596 /* If the second argument is "", return __builtin_strlen(s1). */
11597 if (p2 && *p2 == '\0')
11599 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11601 /* If the replacement _DECL isn't initialized, don't do the
11602 transformation. */
11603 if (!fn)
11604 return NULL_TREE;
11606 return build_call_expr_loc (loc, fn, 1, s1);
11608 return NULL_TREE;
11612 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11613 to the call. IGNORE is true if the value returned
11614 by the builtin will be ignored. UNLOCKED is true is true if this
11615 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11616 the known length of the string. Return NULL_TREE if no simplification
11617 was possible. */
11619 tree
11620 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11621 bool ignore, bool unlocked, tree len)
11623 /* If we're using an unlocked function, assume the other unlocked
11624 functions exist explicitly. */
11625 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11626 : implicit_built_in_decls[BUILT_IN_FPUTC];
11627 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11628 : implicit_built_in_decls[BUILT_IN_FWRITE];
11630 /* If the return value is used, don't do the transformation. */
11631 if (!ignore)
11632 return NULL_TREE;
11634 /* Verify the arguments in the original call. */
11635 if (!validate_arg (arg0, POINTER_TYPE)
11636 || !validate_arg (arg1, POINTER_TYPE))
11637 return NULL_TREE;
11639 if (! len)
11640 len = c_strlen (arg0, 0);
11642 /* Get the length of the string passed to fputs. If the length
11643 can't be determined, punt. */
11644 if (!len
11645 || TREE_CODE (len) != INTEGER_CST)
11646 return NULL_TREE;
11648 switch (compare_tree_int (len, 1))
11650 case -1: /* length is 0, delete the call entirely . */
11651 return omit_one_operand_loc (loc, integer_type_node,
11652 integer_zero_node, arg1);;
11654 case 0: /* length is 1, call fputc. */
11656 const char *p = c_getstr (arg0);
11658 if (p != NULL)
11660 if (fn_fputc)
11661 return build_call_expr_loc (loc, fn_fputc, 2,
11662 build_int_cst (NULL_TREE, p[0]), arg1);
11663 else
11664 return NULL_TREE;
11667 /* FALLTHROUGH */
11668 case 1: /* length is greater than 1, call fwrite. */
11670 /* If optimizing for size keep fputs. */
11671 if (optimize_function_for_size_p (cfun))
11672 return NULL_TREE;
11673 /* New argument list transforming fputs(string, stream) to
11674 fwrite(string, 1, len, stream). */
11675 if (fn_fwrite)
11676 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11677 size_one_node, len, arg1);
11678 else
11679 return NULL_TREE;
11681 default:
11682 gcc_unreachable ();
11684 return NULL_TREE;
11687 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11688 produced. False otherwise. This is done so that we don't output the error
11689 or warning twice or three times. */
11691 bool
11692 fold_builtin_next_arg (tree exp, bool va_start_p)
11694 tree fntype = TREE_TYPE (current_function_decl);
11695 int nargs = call_expr_nargs (exp);
11696 tree arg;
11698 if (TYPE_ARG_TYPES (fntype) == 0
11699 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11700 == void_type_node))
11702 error ("%<va_start%> used in function with fixed args");
11703 return true;
11706 if (va_start_p)
11708 if (va_start_p && (nargs != 2))
11710 error ("wrong number of arguments to function %<va_start%>");
11711 return true;
11713 arg = CALL_EXPR_ARG (exp, 1);
11715 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11716 when we checked the arguments and if needed issued a warning. */
11717 else
11719 if (nargs == 0)
11721 /* Evidently an out of date version of <stdarg.h>; can't validate
11722 va_start's second argument, but can still work as intended. */
11723 warning (0, "%<__builtin_next_arg%> called without an argument");
11724 return true;
11726 else if (nargs > 1)
11728 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11729 return true;
11731 arg = CALL_EXPR_ARG (exp, 0);
11734 if (TREE_CODE (arg) == SSA_NAME)
11735 arg = SSA_NAME_VAR (arg);
11737 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11738 or __builtin_next_arg (0) the first time we see it, after checking
11739 the arguments and if needed issuing a warning. */
11740 if (!integer_zerop (arg))
11742 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11744 /* Strip off all nops for the sake of the comparison. This
11745 is not quite the same as STRIP_NOPS. It does more.
11746 We must also strip off INDIRECT_EXPR for C++ reference
11747 parameters. */
11748 while (CONVERT_EXPR_P (arg)
11749 || TREE_CODE (arg) == INDIRECT_REF)
11750 arg = TREE_OPERAND (arg, 0);
11751 if (arg != last_parm)
11753 /* FIXME: Sometimes with the tree optimizers we can get the
11754 not the last argument even though the user used the last
11755 argument. We just warn and set the arg to be the last
11756 argument so that we will get wrong-code because of
11757 it. */
11758 warning (0, "second parameter of %<va_start%> not last named argument");
11761 /* Undefined by C99 7.15.1.4p4 (va_start):
11762 "If the parameter parmN is declared with the register storage
11763 class, with a function or array type, or with a type that is
11764 not compatible with the type that results after application of
11765 the default argument promotions, the behavior is undefined."
11767 else if (DECL_REGISTER (arg))
11768 warning (0, "undefined behaviour when second parameter of "
11769 "%<va_start%> is declared with %<register%> storage");
11771 /* We want to verify the second parameter just once before the tree
11772 optimizers are run and then avoid keeping it in the tree,
11773 as otherwise we could warn even for correct code like:
11774 void foo (int i, ...)
11775 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11776 if (va_start_p)
11777 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11778 else
11779 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11781 return false;
11785 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11786 ORIG may be null if this is a 2-argument call. We don't attempt to
11787 simplify calls with more than 3 arguments.
11789 Return NULL_TREE if no simplification was possible, otherwise return the
11790 simplified form of the call as a tree. If IGNORED is true, it means that
11791 the caller does not use the returned value of the function. */
11793 static tree
11794 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11795 tree orig, int ignored)
11797 tree call, retval;
11798 const char *fmt_str = NULL;
11800 /* Verify the required arguments in the original call. We deal with two
11801 types of sprintf() calls: 'sprintf (str, fmt)' and
11802 'sprintf (dest, "%s", orig)'. */
11803 if (!validate_arg (dest, POINTER_TYPE)
11804 || !validate_arg (fmt, POINTER_TYPE))
11805 return NULL_TREE;
11806 if (orig && !validate_arg (orig, POINTER_TYPE))
11807 return NULL_TREE;
11809 /* Check whether the format is a literal string constant. */
11810 fmt_str = c_getstr (fmt);
11811 if (fmt_str == NULL)
11812 return NULL_TREE;
11814 call = NULL_TREE;
11815 retval = NULL_TREE;
11817 if (!init_target_chars ())
11818 return NULL_TREE;
11820 /* If the format doesn't contain % args or %%, use strcpy. */
11821 if (strchr (fmt_str, target_percent) == NULL)
11823 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11825 if (!fn)
11826 return NULL_TREE;
11828 /* Don't optimize sprintf (buf, "abc", ptr++). */
11829 if (orig)
11830 return NULL_TREE;
11832 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11833 'format' is known to contain no % formats. */
11834 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11835 if (!ignored)
11836 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11839 /* If the format is "%s", use strcpy if the result isn't used. */
11840 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11842 tree fn;
11843 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11845 if (!fn)
11846 return NULL_TREE;
11848 /* Don't crash on sprintf (str1, "%s"). */
11849 if (!orig)
11850 return NULL_TREE;
11852 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11853 if (!ignored)
11855 retval = c_strlen (orig, 1);
11856 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11857 return NULL_TREE;
11859 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11862 if (call && retval)
11864 retval = fold_convert_loc
11865 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11866 retval);
11867 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11869 else
11870 return call;
11873 /* Expand a call EXP to __builtin_object_size. */
11876 expand_builtin_object_size (tree exp)
11878 tree ost;
11879 int object_size_type;
11880 tree fndecl = get_callee_fndecl (exp);
11882 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11884 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11885 exp, fndecl);
11886 expand_builtin_trap ();
11887 return const0_rtx;
11890 ost = CALL_EXPR_ARG (exp, 1);
11891 STRIP_NOPS (ost);
11893 if (TREE_CODE (ost) != INTEGER_CST
11894 || tree_int_cst_sgn (ost) < 0
11895 || compare_tree_int (ost, 3) > 0)
11897 error ("%Klast argument of %D is not integer constant between 0 and 3",
11898 exp, fndecl);
11899 expand_builtin_trap ();
11900 return const0_rtx;
11903 object_size_type = tree_low_cst (ost, 0);
11905 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11908 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11909 FCODE is the BUILT_IN_* to use.
11910 Return NULL_RTX if we failed; the caller should emit a normal call,
11911 otherwise try to get the result in TARGET, if convenient (and in
11912 mode MODE if that's convenient). */
11914 static rtx
11915 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11916 enum built_in_function fcode)
11918 tree dest, src, len, size;
11920 if (!validate_arglist (exp,
11921 POINTER_TYPE,
11922 fcode == BUILT_IN_MEMSET_CHK
11923 ? INTEGER_TYPE : POINTER_TYPE,
11924 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11925 return NULL_RTX;
11927 dest = CALL_EXPR_ARG (exp, 0);
11928 src = CALL_EXPR_ARG (exp, 1);
11929 len = CALL_EXPR_ARG (exp, 2);
11930 size = CALL_EXPR_ARG (exp, 3);
11932 if (! host_integerp (size, 1))
11933 return NULL_RTX;
11935 if (host_integerp (len, 1) || integer_all_onesp (size))
11937 tree fn;
11939 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11941 warning_at (tree_nonartificial_location (exp),
11942 0, "%Kcall to %D will always overflow destination buffer",
11943 exp, get_callee_fndecl (exp));
11944 return NULL_RTX;
11947 fn = NULL_TREE;
11948 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11949 mem{cpy,pcpy,move,set} is available. */
11950 switch (fcode)
11952 case BUILT_IN_MEMCPY_CHK:
11953 fn = built_in_decls[BUILT_IN_MEMCPY];
11954 break;
11955 case BUILT_IN_MEMPCPY_CHK:
11956 fn = built_in_decls[BUILT_IN_MEMPCPY];
11957 break;
11958 case BUILT_IN_MEMMOVE_CHK:
11959 fn = built_in_decls[BUILT_IN_MEMMOVE];
11960 break;
11961 case BUILT_IN_MEMSET_CHK:
11962 fn = built_in_decls[BUILT_IN_MEMSET];
11963 break;
11964 default:
11965 break;
11968 if (! fn)
11969 return NULL_RTX;
11971 fn = build_call_nofold (fn, 3, dest, src, len);
11972 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11973 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11974 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11976 else if (fcode == BUILT_IN_MEMSET_CHK)
11977 return NULL_RTX;
11978 else
11980 unsigned int dest_align
11981 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
11983 /* If DEST is not a pointer type, call the normal function. */
11984 if (dest_align == 0)
11985 return NULL_RTX;
11987 /* If SRC and DEST are the same (and not volatile), do nothing. */
11988 if (operand_equal_p (src, dest, 0))
11990 tree expr;
11992 if (fcode != BUILT_IN_MEMPCPY_CHK)
11994 /* Evaluate and ignore LEN in case it has side-effects. */
11995 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11996 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11999 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12000 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12003 /* __memmove_chk special case. */
12004 if (fcode == BUILT_IN_MEMMOVE_CHK)
12006 unsigned int src_align
12007 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12009 if (src_align == 0)
12010 return NULL_RTX;
12012 /* If src is categorized for a readonly section we can use
12013 normal __memcpy_chk. */
12014 if (readonly_data_expr (src))
12016 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12017 if (!fn)
12018 return NULL_RTX;
12019 fn = build_call_nofold (fn, 4, dest, src, len, size);
12020 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12021 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12022 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12025 return NULL_RTX;
12029 /* Emit warning if a buffer overflow is detected at compile time. */
12031 static void
12032 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12034 int is_strlen = 0;
12035 tree len, size;
12036 location_t loc = tree_nonartificial_location (exp);
12038 switch (fcode)
12040 case BUILT_IN_STRCPY_CHK:
12041 case BUILT_IN_STPCPY_CHK:
12042 /* For __strcat_chk the warning will be emitted only if overflowing
12043 by at least strlen (dest) + 1 bytes. */
12044 case BUILT_IN_STRCAT_CHK:
12045 len = CALL_EXPR_ARG (exp, 1);
12046 size = CALL_EXPR_ARG (exp, 2);
12047 is_strlen = 1;
12048 break;
12049 case BUILT_IN_STRNCAT_CHK:
12050 case BUILT_IN_STRNCPY_CHK:
12051 len = CALL_EXPR_ARG (exp, 2);
12052 size = CALL_EXPR_ARG (exp, 3);
12053 break;
12054 case BUILT_IN_SNPRINTF_CHK:
12055 case BUILT_IN_VSNPRINTF_CHK:
12056 len = CALL_EXPR_ARG (exp, 1);
12057 size = CALL_EXPR_ARG (exp, 3);
12058 break;
12059 default:
12060 gcc_unreachable ();
12063 if (!len || !size)
12064 return;
12066 if (! host_integerp (size, 1) || integer_all_onesp (size))
12067 return;
12069 if (is_strlen)
12071 len = c_strlen (len, 1);
12072 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12073 return;
12075 else if (fcode == BUILT_IN_STRNCAT_CHK)
12077 tree src = CALL_EXPR_ARG (exp, 1);
12078 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12079 return;
12080 src = c_strlen (src, 1);
12081 if (! src || ! host_integerp (src, 1))
12083 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12084 exp, get_callee_fndecl (exp));
12085 return;
12087 else if (tree_int_cst_lt (src, size))
12088 return;
12090 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12091 return;
12093 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12094 exp, get_callee_fndecl (exp));
12097 /* Emit warning if a buffer overflow is detected at compile time
12098 in __sprintf_chk/__vsprintf_chk calls. */
12100 static void
12101 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12103 tree dest, size, len, fmt, flag;
12104 const char *fmt_str;
12105 int nargs = call_expr_nargs (exp);
12107 /* Verify the required arguments in the original call. */
12109 if (nargs < 4)
12110 return;
12111 dest = CALL_EXPR_ARG (exp, 0);
12112 flag = CALL_EXPR_ARG (exp, 1);
12113 size = CALL_EXPR_ARG (exp, 2);
12114 fmt = CALL_EXPR_ARG (exp, 3);
12116 if (! host_integerp (size, 1) || integer_all_onesp (size))
12117 return;
12119 /* Check whether the format is a literal string constant. */
12120 fmt_str = c_getstr (fmt);
12121 if (fmt_str == NULL)
12122 return;
12124 if (!init_target_chars ())
12125 return;
12127 /* If the format doesn't contain % args or %%, we know its size. */
12128 if (strchr (fmt_str, target_percent) == 0)
12129 len = build_int_cstu (size_type_node, strlen (fmt_str));
12130 /* If the format is "%s" and first ... argument is a string literal,
12131 we know it too. */
12132 else if (fcode == BUILT_IN_SPRINTF_CHK
12133 && strcmp (fmt_str, target_percent_s) == 0)
12135 tree arg;
12137 if (nargs < 5)
12138 return;
12139 arg = CALL_EXPR_ARG (exp, 4);
12140 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12141 return;
12143 len = c_strlen (arg, 1);
12144 if (!len || ! host_integerp (len, 1))
12145 return;
12147 else
12148 return;
12150 if (! tree_int_cst_lt (len, size))
12151 warning_at (tree_nonartificial_location (exp),
12152 0, "%Kcall to %D will always overflow destination buffer",
12153 exp, get_callee_fndecl (exp));
12156 /* Emit warning if a free is called with address of a variable. */
12158 static void
12159 maybe_emit_free_warning (tree exp)
12161 tree arg = CALL_EXPR_ARG (exp, 0);
12163 STRIP_NOPS (arg);
12164 if (TREE_CODE (arg) != ADDR_EXPR)
12165 return;
12167 arg = get_base_address (TREE_OPERAND (arg, 0));
12168 if (arg == NULL || INDIRECT_REF_P (arg))
12169 return;
12171 if (SSA_VAR_P (arg))
12172 warning_at (tree_nonartificial_location (exp),
12173 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12174 else
12175 warning_at (tree_nonartificial_location (exp),
12176 0, "%Kattempt to free a non-heap object", exp);
12179 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12180 if possible. */
12182 tree
12183 fold_builtin_object_size (tree ptr, tree ost)
12185 tree ret = NULL_TREE;
12186 int object_size_type;
12188 if (!validate_arg (ptr, POINTER_TYPE)
12189 || !validate_arg (ost, INTEGER_TYPE))
12190 return NULL_TREE;
12192 STRIP_NOPS (ost);
12194 if (TREE_CODE (ost) != INTEGER_CST
12195 || tree_int_cst_sgn (ost) < 0
12196 || compare_tree_int (ost, 3) > 0)
12197 return NULL_TREE;
12199 object_size_type = tree_low_cst (ost, 0);
12201 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12202 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12203 and (size_t) 0 for types 2 and 3. */
12204 if (TREE_SIDE_EFFECTS (ptr))
12205 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12207 if (TREE_CODE (ptr) == ADDR_EXPR)
12208 ret = build_int_cstu (size_type_node,
12209 compute_builtin_object_size (ptr, object_size_type));
12211 else if (TREE_CODE (ptr) == SSA_NAME)
12213 unsigned HOST_WIDE_INT bytes;
12215 /* If object size is not known yet, delay folding until
12216 later. Maybe subsequent passes will help determining
12217 it. */
12218 bytes = compute_builtin_object_size (ptr, object_size_type);
12219 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
12220 ? -1 : 0))
12221 ret = build_int_cstu (size_type_node, bytes);
12224 if (ret)
12226 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
12227 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
12228 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
12229 ret = NULL_TREE;
12232 return ret;
12235 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12236 DEST, SRC, LEN, and SIZE are the arguments to the call.
12237 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12238 code of the builtin. If MAXLEN is not NULL, it is maximum length
12239 passed as third argument. */
12241 tree
12242 fold_builtin_memory_chk (location_t loc, tree fndecl,
12243 tree dest, tree src, tree len, tree size,
12244 tree maxlen, bool ignore,
12245 enum built_in_function fcode)
12247 tree fn;
12249 if (!validate_arg (dest, POINTER_TYPE)
12250 || !validate_arg (src,
12251 (fcode == BUILT_IN_MEMSET_CHK
12252 ? INTEGER_TYPE : POINTER_TYPE))
12253 || !validate_arg (len, INTEGER_TYPE)
12254 || !validate_arg (size, INTEGER_TYPE))
12255 return NULL_TREE;
12257 /* If SRC and DEST are the same (and not volatile), return DEST
12258 (resp. DEST+LEN for __mempcpy_chk). */
12259 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12261 if (fcode != BUILT_IN_MEMPCPY_CHK)
12262 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12263 dest, len);
12264 else
12266 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12267 dest, len);
12268 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12272 if (! host_integerp (size, 1))
12273 return NULL_TREE;
12275 if (! integer_all_onesp (size))
12277 if (! host_integerp (len, 1))
12279 /* If LEN is not constant, try MAXLEN too.
12280 For MAXLEN only allow optimizing into non-_ocs function
12281 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12282 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12284 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12286 /* (void) __mempcpy_chk () can be optimized into
12287 (void) __memcpy_chk (). */
12288 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12289 if (!fn)
12290 return NULL_TREE;
12292 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12294 return NULL_TREE;
12297 else
12298 maxlen = len;
12300 if (tree_int_cst_lt (size, maxlen))
12301 return NULL_TREE;
12304 fn = NULL_TREE;
12305 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12306 mem{cpy,pcpy,move,set} is available. */
12307 switch (fcode)
12309 case BUILT_IN_MEMCPY_CHK:
12310 fn = built_in_decls[BUILT_IN_MEMCPY];
12311 break;
12312 case BUILT_IN_MEMPCPY_CHK:
12313 fn = built_in_decls[BUILT_IN_MEMPCPY];
12314 break;
12315 case BUILT_IN_MEMMOVE_CHK:
12316 fn = built_in_decls[BUILT_IN_MEMMOVE];
12317 break;
12318 case BUILT_IN_MEMSET_CHK:
12319 fn = built_in_decls[BUILT_IN_MEMSET];
12320 break;
12321 default:
12322 break;
12325 if (!fn)
12326 return NULL_TREE;
12328 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12331 /* Fold a call to the __st[rp]cpy_chk builtin.
12332 DEST, SRC, and SIZE are the arguments to the call.
12333 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12334 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12335 strings passed as second argument. */
12337 tree
12338 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12339 tree src, tree size,
12340 tree maxlen, bool ignore,
12341 enum built_in_function fcode)
12343 tree len, fn;
12345 if (!validate_arg (dest, POINTER_TYPE)
12346 || !validate_arg (src, POINTER_TYPE)
12347 || !validate_arg (size, INTEGER_TYPE))
12348 return NULL_TREE;
12350 /* If SRC and DEST are the same (and not volatile), return DEST. */
12351 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12352 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12354 if (! host_integerp (size, 1))
12355 return NULL_TREE;
12357 if (! integer_all_onesp (size))
12359 len = c_strlen (src, 1);
12360 if (! len || ! host_integerp (len, 1))
12362 /* If LEN is not constant, try MAXLEN too.
12363 For MAXLEN only allow optimizing into non-_ocs function
12364 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12365 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12367 if (fcode == BUILT_IN_STPCPY_CHK)
12369 if (! ignore)
12370 return NULL_TREE;
12372 /* If return value of __stpcpy_chk is ignored,
12373 optimize into __strcpy_chk. */
12374 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12375 if (!fn)
12376 return NULL_TREE;
12378 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12381 if (! len || TREE_SIDE_EFFECTS (len))
12382 return NULL_TREE;
12384 /* If c_strlen returned something, but not a constant,
12385 transform __strcpy_chk into __memcpy_chk. */
12386 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12387 if (!fn)
12388 return NULL_TREE;
12390 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12391 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12392 build_call_expr_loc (loc, fn, 4,
12393 dest, src, len, size));
12396 else
12397 maxlen = len;
12399 if (! tree_int_cst_lt (maxlen, size))
12400 return NULL_TREE;
12403 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12404 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12405 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12406 if (!fn)
12407 return NULL_TREE;
12409 return build_call_expr_loc (loc, fn, 2, dest, src);
12412 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12413 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12414 length passed as third argument. */
12416 tree
12417 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12418 tree len, tree size, tree maxlen)
12420 tree fn;
12422 if (!validate_arg (dest, POINTER_TYPE)
12423 || !validate_arg (src, POINTER_TYPE)
12424 || !validate_arg (len, INTEGER_TYPE)
12425 || !validate_arg (size, INTEGER_TYPE))
12426 return NULL_TREE;
12428 if (! host_integerp (size, 1))
12429 return NULL_TREE;
12431 if (! integer_all_onesp (size))
12433 if (! host_integerp (len, 1))
12435 /* If LEN is not constant, try MAXLEN too.
12436 For MAXLEN only allow optimizing into non-_ocs function
12437 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12438 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12439 return NULL_TREE;
12441 else
12442 maxlen = len;
12444 if (tree_int_cst_lt (size, maxlen))
12445 return NULL_TREE;
12448 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12449 fn = built_in_decls[BUILT_IN_STRNCPY];
12450 if (!fn)
12451 return NULL_TREE;
12453 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12456 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12457 are the arguments to the call. */
12459 static tree
12460 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12461 tree src, tree size)
12463 tree fn;
12464 const char *p;
12466 if (!validate_arg (dest, POINTER_TYPE)
12467 || !validate_arg (src, POINTER_TYPE)
12468 || !validate_arg (size, INTEGER_TYPE))
12469 return NULL_TREE;
12471 p = c_getstr (src);
12472 /* If the SRC parameter is "", return DEST. */
12473 if (p && *p == '\0')
12474 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12476 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12477 return NULL_TREE;
12479 /* If __builtin_strcat_chk is used, assume strcat is available. */
12480 fn = built_in_decls[BUILT_IN_STRCAT];
12481 if (!fn)
12482 return NULL_TREE;
12484 return build_call_expr_loc (loc, fn, 2, dest, src);
12487 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12488 LEN, and SIZE. */
12490 static tree
12491 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12492 tree dest, tree src, tree len, tree size)
12494 tree fn;
12495 const char *p;
12497 if (!validate_arg (dest, POINTER_TYPE)
12498 || !validate_arg (src, POINTER_TYPE)
12499 || !validate_arg (size, INTEGER_TYPE)
12500 || !validate_arg (size, INTEGER_TYPE))
12501 return NULL_TREE;
12503 p = c_getstr (src);
12504 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12505 if (p && *p == '\0')
12506 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12507 else if (integer_zerop (len))
12508 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12510 if (! host_integerp (size, 1))
12511 return NULL_TREE;
12513 if (! integer_all_onesp (size))
12515 tree src_len = c_strlen (src, 1);
12516 if (src_len
12517 && host_integerp (src_len, 1)
12518 && host_integerp (len, 1)
12519 && ! tree_int_cst_lt (len, src_len))
12521 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12522 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12523 if (!fn)
12524 return NULL_TREE;
12526 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12528 return NULL_TREE;
12531 /* If __builtin_strncat_chk is used, assume strncat is available. */
12532 fn = built_in_decls[BUILT_IN_STRNCAT];
12533 if (!fn)
12534 return NULL_TREE;
12536 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12539 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12540 a normal call should be emitted rather than expanding the function
12541 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12543 static tree
12544 fold_builtin_sprintf_chk (location_t loc, tree exp,
12545 enum built_in_function fcode)
12547 tree dest, size, len, fn, fmt, flag;
12548 const char *fmt_str;
12549 int nargs = call_expr_nargs (exp);
12551 /* Verify the required arguments in the original call. */
12552 if (nargs < 4)
12553 return NULL_TREE;
12554 dest = CALL_EXPR_ARG (exp, 0);
12555 if (!validate_arg (dest, POINTER_TYPE))
12556 return NULL_TREE;
12557 flag = CALL_EXPR_ARG (exp, 1);
12558 if (!validate_arg (flag, INTEGER_TYPE))
12559 return NULL_TREE;
12560 size = CALL_EXPR_ARG (exp, 2);
12561 if (!validate_arg (size, INTEGER_TYPE))
12562 return NULL_TREE;
12563 fmt = CALL_EXPR_ARG (exp, 3);
12564 if (!validate_arg (fmt, POINTER_TYPE))
12565 return NULL_TREE;
12567 if (! host_integerp (size, 1))
12568 return NULL_TREE;
12570 len = NULL_TREE;
12572 if (!init_target_chars ())
12573 return NULL_TREE;
12575 /* Check whether the format is a literal string constant. */
12576 fmt_str = c_getstr (fmt);
12577 if (fmt_str != NULL)
12579 /* If the format doesn't contain % args or %%, we know the size. */
12580 if (strchr (fmt_str, target_percent) == 0)
12582 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12583 len = build_int_cstu (size_type_node, strlen (fmt_str));
12585 /* If the format is "%s" and first ... argument is a string literal,
12586 we know the size too. */
12587 else if (fcode == BUILT_IN_SPRINTF_CHK
12588 && strcmp (fmt_str, target_percent_s) == 0)
12590 tree arg;
12592 if (nargs == 5)
12594 arg = CALL_EXPR_ARG (exp, 4);
12595 if (validate_arg (arg, POINTER_TYPE))
12597 len = c_strlen (arg, 1);
12598 if (! len || ! host_integerp (len, 1))
12599 len = NULL_TREE;
12605 if (! integer_all_onesp (size))
12607 if (! len || ! tree_int_cst_lt (len, size))
12608 return NULL_TREE;
12611 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12612 or if format doesn't contain % chars or is "%s". */
12613 if (! integer_zerop (flag))
12615 if (fmt_str == NULL)
12616 return NULL_TREE;
12617 if (strchr (fmt_str, target_percent) != NULL
12618 && strcmp (fmt_str, target_percent_s))
12619 return NULL_TREE;
12622 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12623 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12624 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12625 if (!fn)
12626 return NULL_TREE;
12628 return rewrite_call_expr (loc, exp, 4, fn, 2, dest, fmt);
12631 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12632 a normal call should be emitted rather than expanding the function
12633 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12634 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12635 passed as second argument. */
12637 tree
12638 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12639 enum built_in_function fcode)
12641 tree dest, size, len, fn, fmt, flag;
12642 const char *fmt_str;
12644 /* Verify the required arguments in the original call. */
12645 if (call_expr_nargs (exp) < 5)
12646 return NULL_TREE;
12647 dest = CALL_EXPR_ARG (exp, 0);
12648 if (!validate_arg (dest, POINTER_TYPE))
12649 return NULL_TREE;
12650 len = CALL_EXPR_ARG (exp, 1);
12651 if (!validate_arg (len, INTEGER_TYPE))
12652 return NULL_TREE;
12653 flag = CALL_EXPR_ARG (exp, 2);
12654 if (!validate_arg (flag, INTEGER_TYPE))
12655 return NULL_TREE;
12656 size = CALL_EXPR_ARG (exp, 3);
12657 if (!validate_arg (size, INTEGER_TYPE))
12658 return NULL_TREE;
12659 fmt = CALL_EXPR_ARG (exp, 4);
12660 if (!validate_arg (fmt, POINTER_TYPE))
12661 return NULL_TREE;
12663 if (! host_integerp (size, 1))
12664 return NULL_TREE;
12666 if (! integer_all_onesp (size))
12668 if (! host_integerp (len, 1))
12670 /* If LEN is not constant, try MAXLEN too.
12671 For MAXLEN only allow optimizing into non-_ocs function
12672 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12673 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12674 return NULL_TREE;
12676 else
12677 maxlen = len;
12679 if (tree_int_cst_lt (size, maxlen))
12680 return NULL_TREE;
12683 if (!init_target_chars ())
12684 return NULL_TREE;
12686 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12687 or if format doesn't contain % chars or is "%s". */
12688 if (! integer_zerop (flag))
12690 fmt_str = c_getstr (fmt);
12691 if (fmt_str == NULL)
12692 return NULL_TREE;
12693 if (strchr (fmt_str, target_percent) != NULL
12694 && strcmp (fmt_str, target_percent_s))
12695 return NULL_TREE;
12698 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12699 available. */
12700 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12701 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12702 if (!fn)
12703 return NULL_TREE;
12705 return rewrite_call_expr (loc, exp, 5, fn, 3, dest, len, fmt);
12708 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12709 FMT and ARG are the arguments to the call; we don't fold cases with
12710 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12712 Return NULL_TREE if no simplification was possible, otherwise return the
12713 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12714 code of the function to be simplified. */
12716 static tree
12717 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12718 tree arg, bool ignore,
12719 enum built_in_function fcode)
12721 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12722 const char *fmt_str = NULL;
12724 /* If the return value is used, don't do the transformation. */
12725 if (! ignore)
12726 return NULL_TREE;
12728 /* Verify the required arguments in the original call. */
12729 if (!validate_arg (fmt, POINTER_TYPE))
12730 return NULL_TREE;
12732 /* Check whether the format is a literal string constant. */
12733 fmt_str = c_getstr (fmt);
12734 if (fmt_str == NULL)
12735 return NULL_TREE;
12737 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12739 /* If we're using an unlocked function, assume the other
12740 unlocked functions exist explicitly. */
12741 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12742 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12744 else
12746 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12747 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12750 if (!init_target_chars ())
12751 return NULL_TREE;
12753 if (strcmp (fmt_str, target_percent_s) == 0
12754 || strchr (fmt_str, target_percent) == NULL)
12756 const char *str;
12758 if (strcmp (fmt_str, target_percent_s) == 0)
12760 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12761 return NULL_TREE;
12763 if (!arg || !validate_arg (arg, POINTER_TYPE))
12764 return NULL_TREE;
12766 str = c_getstr (arg);
12767 if (str == NULL)
12768 return NULL_TREE;
12770 else
12772 /* The format specifier doesn't contain any '%' characters. */
12773 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12774 && arg)
12775 return NULL_TREE;
12776 str = fmt_str;
12779 /* If the string was "", printf does nothing. */
12780 if (str[0] == '\0')
12781 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12783 /* If the string has length of 1, call putchar. */
12784 if (str[1] == '\0')
12786 /* Given printf("c"), (where c is any one character,)
12787 convert "c"[0] to an int and pass that to the replacement
12788 function. */
12789 newarg = build_int_cst (NULL_TREE, str[0]);
12790 if (fn_putchar)
12791 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12793 else
12795 /* If the string was "string\n", call puts("string"). */
12796 size_t len = strlen (str);
12797 if ((unsigned char)str[len - 1] == target_newline)
12799 /* Create a NUL-terminated string that's one char shorter
12800 than the original, stripping off the trailing '\n'. */
12801 char *newstr = XALLOCAVEC (char, len);
12802 memcpy (newstr, str, len - 1);
12803 newstr[len - 1] = 0;
12805 newarg = build_string_literal (len, newstr);
12806 if (fn_puts)
12807 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12809 else
12810 /* We'd like to arrange to call fputs(string,stdout) here,
12811 but we need stdout and don't have a way to get it yet. */
12812 return NULL_TREE;
12816 /* The other optimizations can be done only on the non-va_list variants. */
12817 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12818 return NULL_TREE;
12820 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12821 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12823 if (!arg || !validate_arg (arg, POINTER_TYPE))
12824 return NULL_TREE;
12825 if (fn_puts)
12826 call = build_call_expr_loc (loc, fn_puts, 1, arg);
12829 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12830 else if (strcmp (fmt_str, target_percent_c) == 0)
12832 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12833 return NULL_TREE;
12834 if (fn_putchar)
12835 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
12838 if (!call)
12839 return NULL_TREE;
12841 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12844 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12845 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12846 more than 3 arguments, and ARG may be null in the 2-argument case.
12848 Return NULL_TREE if no simplification was possible, otherwise return the
12849 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12850 code of the function to be simplified. */
12852 static tree
12853 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
12854 tree fmt, tree arg, bool ignore,
12855 enum built_in_function fcode)
12857 tree fn_fputc, fn_fputs, call = NULL_TREE;
12858 const char *fmt_str = NULL;
12860 /* If the return value is used, don't do the transformation. */
12861 if (! ignore)
12862 return NULL_TREE;
12864 /* Verify the required arguments in the original call. */
12865 if (!validate_arg (fp, POINTER_TYPE))
12866 return NULL_TREE;
12867 if (!validate_arg (fmt, POINTER_TYPE))
12868 return NULL_TREE;
12870 /* Check whether the format is a literal string constant. */
12871 fmt_str = c_getstr (fmt);
12872 if (fmt_str == NULL)
12873 return NULL_TREE;
12875 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12877 /* If we're using an unlocked function, assume the other
12878 unlocked functions exist explicitly. */
12879 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12880 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12882 else
12884 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12885 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12888 if (!init_target_chars ())
12889 return NULL_TREE;
12891 /* If the format doesn't contain % args or %%, use strcpy. */
12892 if (strchr (fmt_str, target_percent) == NULL)
12894 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12895 && arg)
12896 return NULL_TREE;
12898 /* If the format specifier was "", fprintf does nothing. */
12899 if (fmt_str[0] == '\0')
12901 /* If FP has side-effects, just wait until gimplification is
12902 done. */
12903 if (TREE_SIDE_EFFECTS (fp))
12904 return NULL_TREE;
12906 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12909 /* When "string" doesn't contain %, replace all cases of
12910 fprintf (fp, string) with fputs (string, fp). The fputs
12911 builtin will take care of special cases like length == 1. */
12912 if (fn_fputs)
12913 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
12916 /* The other optimizations can be done only on the non-va_list variants. */
12917 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12918 return NULL_TREE;
12920 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12921 else if (strcmp (fmt_str, target_percent_s) == 0)
12923 if (!arg || !validate_arg (arg, POINTER_TYPE))
12924 return NULL_TREE;
12925 if (fn_fputs)
12926 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
12929 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12930 else if (strcmp (fmt_str, target_percent_c) == 0)
12932 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12933 return NULL_TREE;
12934 if (fn_fputc)
12935 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
12938 if (!call)
12939 return NULL_TREE;
12940 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12943 /* Initialize format string characters in the target charset. */
12945 static bool
12946 init_target_chars (void)
12948 static bool init;
12949 if (!init)
12951 target_newline = lang_hooks.to_target_charset ('\n');
12952 target_percent = lang_hooks.to_target_charset ('%');
12953 target_c = lang_hooks.to_target_charset ('c');
12954 target_s = lang_hooks.to_target_charset ('s');
12955 if (target_newline == 0 || target_percent == 0 || target_c == 0
12956 || target_s == 0)
12957 return false;
12959 target_percent_c[0] = target_percent;
12960 target_percent_c[1] = target_c;
12961 target_percent_c[2] = '\0';
12963 target_percent_s[0] = target_percent;
12964 target_percent_s[1] = target_s;
12965 target_percent_s[2] = '\0';
12967 target_percent_s_newline[0] = target_percent;
12968 target_percent_s_newline[1] = target_s;
12969 target_percent_s_newline[2] = target_newline;
12970 target_percent_s_newline[3] = '\0';
12972 init = true;
12974 return true;
12977 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12978 and no overflow/underflow occurred. INEXACT is true if M was not
12979 exactly calculated. TYPE is the tree type for the result. This
12980 function assumes that you cleared the MPFR flags and then
12981 calculated M to see if anything subsequently set a flag prior to
12982 entering this function. Return NULL_TREE if any checks fail. */
12984 static tree
12985 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12987 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12988 overflow/underflow occurred. If -frounding-math, proceed iff the
12989 result of calling FUNC was exact. */
12990 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12991 && (!flag_rounding_math || !inexact))
12993 REAL_VALUE_TYPE rr;
12995 real_from_mpfr (&rr, m, type, GMP_RNDN);
12996 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12997 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12998 but the mpft_t is not, then we underflowed in the
12999 conversion. */
13000 if (real_isfinite (&rr)
13001 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13003 REAL_VALUE_TYPE rmode;
13005 real_convert (&rmode, TYPE_MODE (type), &rr);
13006 /* Proceed iff the specified mode can hold the value. */
13007 if (real_identical (&rmode, &rr))
13008 return build_real (type, rmode);
13011 return NULL_TREE;
13014 #ifdef HAVE_mpc
13015 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13016 number and no overflow/underflow occurred. INEXACT is true if M
13017 was not exactly calculated. TYPE is the tree type for the result.
13018 This function assumes that you cleared the MPFR flags and then
13019 calculated M to see if anything subsequently set a flag prior to
13020 entering this function. Return NULL_TREE if any checks fail, if
13021 FORCE_CONVERT is true, then bypass the checks. */
13023 static tree
13024 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13026 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13027 overflow/underflow occurred. If -frounding-math, proceed iff the
13028 result of calling FUNC was exact. */
13029 if (force_convert
13030 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13031 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13032 && (!flag_rounding_math || !inexact)))
13034 REAL_VALUE_TYPE re, im;
13036 real_from_mpfr (&re, mpc_realref (m), type, GMP_RNDN);
13037 real_from_mpfr (&im, mpc_imagref (m), type, GMP_RNDN);
13038 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13039 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13040 but the mpft_t is not, then we underflowed in the
13041 conversion. */
13042 if (force_convert
13043 || (real_isfinite (&re) && real_isfinite (&im)
13044 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13045 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13047 REAL_VALUE_TYPE re_mode, im_mode;
13049 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13050 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13051 /* Proceed iff the specified mode can hold the value. */
13052 if (force_convert
13053 || (real_identical (&re_mode, &re)
13054 && real_identical (&im_mode, &im)))
13055 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13056 build_real (TREE_TYPE (type), im_mode));
13059 return NULL_TREE;
13061 #endif /* HAVE_mpc */
13063 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13064 FUNC on it and return the resulting value as a tree with type TYPE.
13065 If MIN and/or MAX are not NULL, then the supplied ARG must be
13066 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13067 acceptable values, otherwise they are not. The mpfr precision is
13068 set to the precision of TYPE. We assume that function FUNC returns
13069 zero if the result could be calculated exactly within the requested
13070 precision. */
13072 static tree
13073 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13074 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13075 bool inclusive)
13077 tree result = NULL_TREE;
13079 STRIP_NOPS (arg);
13081 /* To proceed, MPFR must exactly represent the target floating point
13082 format, which only happens when the target base equals two. */
13083 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13084 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13086 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13088 if (real_isfinite (ra)
13089 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13090 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13092 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13093 const int prec = fmt->p;
13094 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13095 int inexact;
13096 mpfr_t m;
13098 mpfr_init2 (m, prec);
13099 mpfr_from_real (m, ra, GMP_RNDN);
13100 mpfr_clear_flags ();
13101 inexact = func (m, m, rnd);
13102 result = do_mpfr_ckconv (m, type, inexact);
13103 mpfr_clear (m);
13107 return result;
13110 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13111 FUNC on it and return the resulting value as a tree with type TYPE.
13112 The mpfr precision is set to the precision of TYPE. We assume that
13113 function FUNC returns zero if the result could be calculated
13114 exactly within the requested precision. */
13116 static tree
13117 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13118 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13120 tree result = NULL_TREE;
13122 STRIP_NOPS (arg1);
13123 STRIP_NOPS (arg2);
13125 /* To proceed, MPFR must exactly represent the target floating point
13126 format, which only happens when the target base equals two. */
13127 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13128 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13129 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13131 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13132 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13134 if (real_isfinite (ra1) && real_isfinite (ra2))
13136 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13137 const int prec = fmt->p;
13138 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13139 int inexact;
13140 mpfr_t m1, m2;
13142 mpfr_inits2 (prec, m1, m2, NULL);
13143 mpfr_from_real (m1, ra1, GMP_RNDN);
13144 mpfr_from_real (m2, ra2, GMP_RNDN);
13145 mpfr_clear_flags ();
13146 inexact = func (m1, m1, m2, rnd);
13147 result = do_mpfr_ckconv (m1, type, inexact);
13148 mpfr_clears (m1, m2, NULL);
13152 return result;
13155 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13156 FUNC on it and return the resulting value as a tree with type TYPE.
13157 The mpfr precision is set to the precision of TYPE. We assume that
13158 function FUNC returns zero if the result could be calculated
13159 exactly within the requested precision. */
13161 static tree
13162 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13163 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13165 tree result = NULL_TREE;
13167 STRIP_NOPS (arg1);
13168 STRIP_NOPS (arg2);
13169 STRIP_NOPS (arg3);
13171 /* To proceed, MPFR must exactly represent the target floating point
13172 format, which only happens when the target base equals two. */
13173 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13174 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13175 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13176 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13178 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13179 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13180 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13182 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13184 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13185 const int prec = fmt->p;
13186 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13187 int inexact;
13188 mpfr_t m1, m2, m3;
13190 mpfr_inits2 (prec, m1, m2, m3, NULL);
13191 mpfr_from_real (m1, ra1, GMP_RNDN);
13192 mpfr_from_real (m2, ra2, GMP_RNDN);
13193 mpfr_from_real (m3, ra3, GMP_RNDN);
13194 mpfr_clear_flags ();
13195 inexact = func (m1, m1, m2, m3, rnd);
13196 result = do_mpfr_ckconv (m1, type, inexact);
13197 mpfr_clears (m1, m2, m3, NULL);
13201 return result;
13204 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13205 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13206 If ARG_SINP and ARG_COSP are NULL then the result is returned
13207 as a complex value.
13208 The type is taken from the type of ARG and is used for setting the
13209 precision of the calculation and results. */
13211 static tree
13212 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13214 tree const type = TREE_TYPE (arg);
13215 tree result = NULL_TREE;
13217 STRIP_NOPS (arg);
13219 /* To proceed, MPFR must exactly represent the target floating point
13220 format, which only happens when the target base equals two. */
13221 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13222 && TREE_CODE (arg) == REAL_CST
13223 && !TREE_OVERFLOW (arg))
13225 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13227 if (real_isfinite (ra))
13229 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13230 const int prec = fmt->p;
13231 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13232 tree result_s, result_c;
13233 int inexact;
13234 mpfr_t m, ms, mc;
13236 mpfr_inits2 (prec, m, ms, mc, NULL);
13237 mpfr_from_real (m, ra, GMP_RNDN);
13238 mpfr_clear_flags ();
13239 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13240 result_s = do_mpfr_ckconv (ms, type, inexact);
13241 result_c = do_mpfr_ckconv (mc, type, inexact);
13242 mpfr_clears (m, ms, mc, NULL);
13243 if (result_s && result_c)
13245 /* If we are to return in a complex value do so. */
13246 if (!arg_sinp && !arg_cosp)
13247 return build_complex (build_complex_type (type),
13248 result_c, result_s);
13250 /* Dereference the sin/cos pointer arguments. */
13251 arg_sinp = build_fold_indirect_ref (arg_sinp);
13252 arg_cosp = build_fold_indirect_ref (arg_cosp);
13253 /* Proceed if valid pointer type were passed in. */
13254 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13255 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13257 /* Set the values. */
13258 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13259 result_s);
13260 TREE_SIDE_EFFECTS (result_s) = 1;
13261 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13262 result_c);
13263 TREE_SIDE_EFFECTS (result_c) = 1;
13264 /* Combine the assignments into a compound expr. */
13265 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13266 result_s, result_c));
13271 return result;
13274 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13275 two-argument mpfr order N Bessel function FUNC on them and return
13276 the resulting value as a tree with type TYPE. The mpfr precision
13277 is set to the precision of TYPE. We assume that function FUNC
13278 returns zero if the result could be calculated exactly within the
13279 requested precision. */
13280 static tree
13281 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13282 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13283 const REAL_VALUE_TYPE *min, bool inclusive)
13285 tree result = NULL_TREE;
13287 STRIP_NOPS (arg1);
13288 STRIP_NOPS (arg2);
13290 /* To proceed, MPFR must exactly represent the target floating point
13291 format, which only happens when the target base equals two. */
13292 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13293 && host_integerp (arg1, 0)
13294 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13296 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13297 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13299 if (n == (long)n
13300 && real_isfinite (ra)
13301 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13303 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13304 const int prec = fmt->p;
13305 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13306 int inexact;
13307 mpfr_t m;
13309 mpfr_init2 (m, prec);
13310 mpfr_from_real (m, ra, GMP_RNDN);
13311 mpfr_clear_flags ();
13312 inexact = func (m, n, m, rnd);
13313 result = do_mpfr_ckconv (m, type, inexact);
13314 mpfr_clear (m);
13318 return result;
13321 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13322 the pointer *(ARG_QUO) and return the result. The type is taken
13323 from the type of ARG0 and is used for setting the precision of the
13324 calculation and results. */
13326 static tree
13327 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13329 tree const type = TREE_TYPE (arg0);
13330 tree result = NULL_TREE;
13332 STRIP_NOPS (arg0);
13333 STRIP_NOPS (arg1);
13335 /* To proceed, MPFR must exactly represent the target floating point
13336 format, which only happens when the target base equals two. */
13337 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13338 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13339 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13341 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13342 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13344 if (real_isfinite (ra0) && real_isfinite (ra1))
13346 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13347 const int prec = fmt->p;
13348 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13349 tree result_rem;
13350 long integer_quo;
13351 mpfr_t m0, m1;
13353 mpfr_inits2 (prec, m0, m1, NULL);
13354 mpfr_from_real (m0, ra0, GMP_RNDN);
13355 mpfr_from_real (m1, ra1, GMP_RNDN);
13356 mpfr_clear_flags ();
13357 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13358 /* Remquo is independent of the rounding mode, so pass
13359 inexact=0 to do_mpfr_ckconv(). */
13360 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13361 mpfr_clears (m0, m1, NULL);
13362 if (result_rem)
13364 /* MPFR calculates quo in the host's long so it may
13365 return more bits in quo than the target int can hold
13366 if sizeof(host long) > sizeof(target int). This can
13367 happen even for native compilers in LP64 mode. In
13368 these cases, modulo the quo value with the largest
13369 number that the target int can hold while leaving one
13370 bit for the sign. */
13371 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13372 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13374 /* Dereference the quo pointer argument. */
13375 arg_quo = build_fold_indirect_ref (arg_quo);
13376 /* Proceed iff a valid pointer type was passed in. */
13377 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13379 /* Set the value. */
13380 tree result_quo = fold_build2 (MODIFY_EXPR,
13381 TREE_TYPE (arg_quo), arg_quo,
13382 build_int_cst (NULL, integer_quo));
13383 TREE_SIDE_EFFECTS (result_quo) = 1;
13384 /* Combine the quo assignment with the rem. */
13385 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13386 result_quo, result_rem));
13391 return result;
13394 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13395 resulting value as a tree with type TYPE. The mpfr precision is
13396 set to the precision of TYPE. We assume that this mpfr function
13397 returns zero if the result could be calculated exactly within the
13398 requested precision. In addition, the integer pointer represented
13399 by ARG_SG will be dereferenced and set to the appropriate signgam
13400 (-1,1) value. */
13402 static tree
13403 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13405 tree result = NULL_TREE;
13407 STRIP_NOPS (arg);
13409 /* To proceed, MPFR must exactly represent the target floating point
13410 format, which only happens when the target base equals two. Also
13411 verify ARG is a constant and that ARG_SG is an int pointer. */
13412 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13413 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13414 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13415 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13417 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13419 /* In addition to NaN and Inf, the argument cannot be zero or a
13420 negative integer. */
13421 if (real_isfinite (ra)
13422 && ra->cl != rvc_zero
13423 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13425 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13426 const int prec = fmt->p;
13427 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13428 int inexact, sg;
13429 mpfr_t m;
13430 tree result_lg;
13432 mpfr_init2 (m, prec);
13433 mpfr_from_real (m, ra, GMP_RNDN);
13434 mpfr_clear_flags ();
13435 inexact = mpfr_lgamma (m, &sg, m, rnd);
13436 result_lg = do_mpfr_ckconv (m, type, inexact);
13437 mpfr_clear (m);
13438 if (result_lg)
13440 tree result_sg;
13442 /* Dereference the arg_sg pointer argument. */
13443 arg_sg = build_fold_indirect_ref (arg_sg);
13444 /* Assign the signgam value into *arg_sg. */
13445 result_sg = fold_build2 (MODIFY_EXPR,
13446 TREE_TYPE (arg_sg), arg_sg,
13447 build_int_cst (NULL, sg));
13448 TREE_SIDE_EFFECTS (result_sg) = 1;
13449 /* Combine the signgam assignment with the lgamma result. */
13450 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13451 result_sg, result_lg));
13456 return result;
13459 #ifdef HAVE_mpc
13460 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13461 function FUNC on it and return the resulting value as a tree with
13462 type TYPE. The mpfr precision is set to the precision of TYPE. We
13463 assume that function FUNC returns zero if the result could be
13464 calculated exactly within the requested precision. */
13466 static tree
13467 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13469 tree result = NULL_TREE;
13471 STRIP_NOPS (arg);
13473 /* To proceed, MPFR must exactly represent the target floating point
13474 format, which only happens when the target base equals two. */
13475 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13476 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13477 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13479 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13480 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13482 if (real_isfinite (re) && real_isfinite (im))
13484 const struct real_format *const fmt =
13485 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13486 const int prec = fmt->p;
13487 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13488 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13489 int inexact;
13490 mpc_t m;
13492 mpc_init2 (m, prec);
13493 mpfr_from_real (mpc_realref(m), re, rnd);
13494 mpfr_from_real (mpc_imagref(m), im, rnd);
13495 mpfr_clear_flags ();
13496 inexact = func (m, m, crnd);
13497 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13498 mpc_clear (m);
13502 return result;
13505 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13506 mpc function FUNC on it and return the resulting value as a tree
13507 with type TYPE. The mpfr precision is set to the precision of
13508 TYPE. We assume that function FUNC returns zero if the result
13509 could be calculated exactly within the requested precision. If
13510 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13511 in the arguments and/or results. */
13513 #ifdef HAVE_mpc
13514 tree
13515 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13516 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13518 tree result = NULL_TREE;
13520 STRIP_NOPS (arg0);
13521 STRIP_NOPS (arg1);
13523 /* To proceed, MPFR must exactly represent the target floating point
13524 format, which only happens when the target base equals two. */
13525 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13526 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13527 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13528 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13529 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13531 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13532 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13533 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13534 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13536 if (do_nonfinite
13537 || (real_isfinite (re0) && real_isfinite (im0)
13538 && real_isfinite (re1) && real_isfinite (im1)))
13540 const struct real_format *const fmt =
13541 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13542 const int prec = fmt->p;
13543 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13544 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13545 int inexact;
13546 mpc_t m0, m1;
13548 mpc_init2 (m0, prec);
13549 mpc_init2 (m1, prec);
13550 mpfr_from_real (mpc_realref(m0), re0, rnd);
13551 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13552 mpfr_from_real (mpc_realref(m1), re1, rnd);
13553 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13554 mpfr_clear_flags ();
13555 inexact = func (m0, m0, m1, crnd);
13556 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13557 mpc_clear (m0);
13558 mpc_clear (m1);
13562 return result;
13564 # endif
13565 #endif /* HAVE_mpc */
13567 /* FIXME tuples.
13568 The functions below provide an alternate interface for folding
13569 builtin function calls presented as GIMPLE_CALL statements rather
13570 than as CALL_EXPRs. The folded result is still expressed as a
13571 tree. There is too much code duplication in the handling of
13572 varargs functions, and a more intrusive re-factoring would permit
13573 better sharing of code between the tree and statement-based
13574 versions of these functions. */
13576 /* Construct a new CALL_EXPR using the tail of the argument list of STMT
13577 along with N new arguments specified as the "..." parameters. SKIP
13578 is the number of arguments in STMT to be omitted. This function is used
13579 to do varargs-to-varargs transformations. */
13581 static tree
13582 gimple_rewrite_call_expr (gimple stmt, int skip, tree fndecl, int n, ...)
13584 int oldnargs = gimple_call_num_args (stmt);
13585 int nargs = oldnargs - skip + n;
13586 tree fntype = TREE_TYPE (fndecl);
13587 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
13588 tree *buffer;
13589 int i, j;
13590 va_list ap;
13591 location_t loc = gimple_location (stmt);
13593 buffer = XALLOCAVEC (tree, nargs);
13594 va_start (ap, n);
13595 for (i = 0; i < n; i++)
13596 buffer[i] = va_arg (ap, tree);
13597 va_end (ap);
13598 for (j = skip; j < oldnargs; j++, i++)
13599 buffer[i] = gimple_call_arg (stmt, j);
13601 return fold (build_call_array_loc (loc, TREE_TYPE (fntype), fn, nargs, buffer));
13604 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13605 a normal call should be emitted rather than expanding the function
13606 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13608 static tree
13609 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13611 tree dest, size, len, fn, fmt, flag;
13612 const char *fmt_str;
13613 int nargs = gimple_call_num_args (stmt);
13615 /* Verify the required arguments in the original call. */
13616 if (nargs < 4)
13617 return NULL_TREE;
13618 dest = gimple_call_arg (stmt, 0);
13619 if (!validate_arg (dest, POINTER_TYPE))
13620 return NULL_TREE;
13621 flag = gimple_call_arg (stmt, 1);
13622 if (!validate_arg (flag, INTEGER_TYPE))
13623 return NULL_TREE;
13624 size = gimple_call_arg (stmt, 2);
13625 if (!validate_arg (size, INTEGER_TYPE))
13626 return NULL_TREE;
13627 fmt = gimple_call_arg (stmt, 3);
13628 if (!validate_arg (fmt, POINTER_TYPE))
13629 return NULL_TREE;
13631 if (! host_integerp (size, 1))
13632 return NULL_TREE;
13634 len = NULL_TREE;
13636 if (!init_target_chars ())
13637 return NULL_TREE;
13639 /* Check whether the format is a literal string constant. */
13640 fmt_str = c_getstr (fmt);
13641 if (fmt_str != NULL)
13643 /* If the format doesn't contain % args or %%, we know the size. */
13644 if (strchr (fmt_str, target_percent) == 0)
13646 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13647 len = build_int_cstu (size_type_node, strlen (fmt_str));
13649 /* If the format is "%s" and first ... argument is a string literal,
13650 we know the size too. */
13651 else if (fcode == BUILT_IN_SPRINTF_CHK
13652 && strcmp (fmt_str, target_percent_s) == 0)
13654 tree arg;
13656 if (nargs == 5)
13658 arg = gimple_call_arg (stmt, 4);
13659 if (validate_arg (arg, POINTER_TYPE))
13661 len = c_strlen (arg, 1);
13662 if (! len || ! host_integerp (len, 1))
13663 len = NULL_TREE;
13669 if (! integer_all_onesp (size))
13671 if (! len || ! tree_int_cst_lt (len, size))
13672 return NULL_TREE;
13675 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13676 or if format doesn't contain % chars or is "%s". */
13677 if (! integer_zerop (flag))
13679 if (fmt_str == NULL)
13680 return NULL_TREE;
13681 if (strchr (fmt_str, target_percent) != NULL
13682 && strcmp (fmt_str, target_percent_s))
13683 return NULL_TREE;
13686 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13687 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
13688 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
13689 if (!fn)
13690 return NULL_TREE;
13692 return gimple_rewrite_call_expr (stmt, 4, fn, 2, dest, fmt);
13695 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13696 a normal call should be emitted rather than expanding the function
13697 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13698 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13699 passed as second argument. */
13701 tree
13702 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13703 enum built_in_function fcode)
13705 tree dest, size, len, fn, fmt, flag;
13706 const char *fmt_str;
13708 /* Verify the required arguments in the original call. */
13709 if (gimple_call_num_args (stmt) < 5)
13710 return NULL_TREE;
13711 dest = gimple_call_arg (stmt, 0);
13712 if (!validate_arg (dest, POINTER_TYPE))
13713 return NULL_TREE;
13714 len = gimple_call_arg (stmt, 1);
13715 if (!validate_arg (len, INTEGER_TYPE))
13716 return NULL_TREE;
13717 flag = gimple_call_arg (stmt, 2);
13718 if (!validate_arg (flag, INTEGER_TYPE))
13719 return NULL_TREE;
13720 size = gimple_call_arg (stmt, 3);
13721 if (!validate_arg (size, INTEGER_TYPE))
13722 return NULL_TREE;
13723 fmt = gimple_call_arg (stmt, 4);
13724 if (!validate_arg (fmt, POINTER_TYPE))
13725 return NULL_TREE;
13727 if (! host_integerp (size, 1))
13728 return NULL_TREE;
13730 if (! integer_all_onesp (size))
13732 if (! host_integerp (len, 1))
13734 /* If LEN is not constant, try MAXLEN too.
13735 For MAXLEN only allow optimizing into non-_ocs function
13736 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13737 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13738 return NULL_TREE;
13740 else
13741 maxlen = len;
13743 if (tree_int_cst_lt (size, maxlen))
13744 return NULL_TREE;
13747 if (!init_target_chars ())
13748 return NULL_TREE;
13750 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13751 or if format doesn't contain % chars or is "%s". */
13752 if (! integer_zerop (flag))
13754 fmt_str = c_getstr (fmt);
13755 if (fmt_str == NULL)
13756 return NULL_TREE;
13757 if (strchr (fmt_str, target_percent) != NULL
13758 && strcmp (fmt_str, target_percent_s))
13759 return NULL_TREE;
13762 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13763 available. */
13764 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
13765 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
13766 if (!fn)
13767 return NULL_TREE;
13769 return gimple_rewrite_call_expr (stmt, 5, fn, 3, dest, len, fmt);
13772 /* Builtins with folding operations that operate on "..." arguments
13773 need special handling; we need to store the arguments in a convenient
13774 data structure before attempting any folding. Fortunately there are
13775 only a few builtins that fall into this category. FNDECL is the
13776 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13777 result of the function call is ignored. */
13779 static tree
13780 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13781 bool ignore ATTRIBUTE_UNUSED)
13783 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13784 tree ret = NULL_TREE;
13786 switch (fcode)
13788 case BUILT_IN_SPRINTF_CHK:
13789 case BUILT_IN_VSPRINTF_CHK:
13790 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13791 break;
13793 case BUILT_IN_SNPRINTF_CHK:
13794 case BUILT_IN_VSNPRINTF_CHK:
13795 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13797 default:
13798 break;
13800 if (ret)
13802 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13803 TREE_NO_WARNING (ret) = 1;
13804 return ret;
13806 return NULL_TREE;
13809 /* A wrapper function for builtin folding that prevents warnings for
13810 "statement without effect" and the like, caused by removing the
13811 call node earlier than the warning is generated. */
13813 tree
13814 fold_call_stmt (gimple stmt, bool ignore)
13816 tree ret = NULL_TREE;
13817 tree fndecl = gimple_call_fndecl (stmt);
13818 location_t loc = gimple_location (stmt);
13819 if (fndecl
13820 && TREE_CODE (fndecl) == FUNCTION_DECL
13821 && DECL_BUILT_IN (fndecl)
13822 && !gimple_call_va_arg_pack_p (stmt))
13824 int nargs = gimple_call_num_args (stmt);
13826 if (avoid_folding_inline_builtin (fndecl))
13827 return NULL_TREE;
13828 /* FIXME: Don't use a list in this interface. */
13829 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13831 tree arglist = NULL_TREE;
13832 int i;
13833 for (i = nargs - 1; i >= 0; i--)
13834 arglist = tree_cons (NULL_TREE, gimple_call_arg (stmt, i), arglist);
13835 return targetm.fold_builtin (fndecl, arglist, ignore);
13837 else
13839 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13841 tree args[MAX_ARGS_TO_FOLD_BUILTIN];
13842 int i;
13843 for (i = 0; i < nargs; i++)
13844 args[i] = gimple_call_arg (stmt, i);
13845 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13847 if (!ret)
13848 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13849 if (ret)
13851 /* Propagate location information from original call to
13852 expansion of builtin. Otherwise things like
13853 maybe_emit_chk_warning, that operate on the expansion
13854 of a builtin, will use the wrong location information. */
13855 if (gimple_has_location (stmt))
13857 tree realret = ret;
13858 if (TREE_CODE (ret) == NOP_EXPR)
13859 realret = TREE_OPERAND (ret, 0);
13860 if (CAN_HAVE_LOCATION_P (realret)
13861 && !EXPR_HAS_LOCATION (realret))
13862 SET_EXPR_LOCATION (realret, loc);
13863 return realret;
13865 return ret;
13869 return NULL_TREE;
13872 /* Look up the function in built_in_decls that corresponds to DECL
13873 and set ASMSPEC as its user assembler name. DECL must be a
13874 function decl that declares a builtin. */
13876 void
13877 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13879 tree builtin;
13880 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13881 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13882 && asmspec != 0);
13884 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13885 set_user_assembler_name (builtin, asmspec);
13886 switch (DECL_FUNCTION_CODE (decl))
13888 case BUILT_IN_MEMCPY:
13889 init_block_move_fn (asmspec);
13890 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13891 break;
13892 case BUILT_IN_MEMSET:
13893 init_block_clear_fn (asmspec);
13894 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13895 break;
13896 case BUILT_IN_MEMMOVE:
13897 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13898 break;
13899 case BUILT_IN_MEMCMP:
13900 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13901 break;
13902 case BUILT_IN_ABORT:
13903 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13904 break;
13905 default:
13906 break;