2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / builtins.c
blob9b6fb1033b1722b1eca06155150896c39f50d155
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "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-core.h"
53 #include "builtins.h"
55 #ifndef SLOW_UNALIGNED_ACCESS
56 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
57 #endif
59 #ifndef PAD_VARARGS_DOWN
60 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
61 #endif
62 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
64 struct target_builtins default_target_builtins;
65 #if SWITCHABLE_TARGET
66 struct target_builtins *this_target_builtins = &default_target_builtins;
67 #endif
69 /* Define the names of the builtin function types and codes. */
70 const char *const built_in_class_names[4]
71 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
73 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
74 const char * built_in_names[(int) END_BUILTINS] =
76 #include "builtins.def"
78 #undef DEF_BUILTIN
80 /* Setup an array of _DECL trees, make sure each element is
81 initialized to NULL_TREE. */
82 tree built_in_decls[(int) END_BUILTINS];
83 /* Declarations used when constructing the builtin implicitly in the compiler.
84 It may be NULL_TREE when this is invalid (for instance runtime is not
85 required to implement the function call in all cases). */
86 tree implicit_built_in_decls[(int) END_BUILTINS];
88 static const char *c_getstr (tree);
89 static rtx c_readstr (const char *, enum machine_mode);
90 static int target_char_cast (tree, char *);
91 static rtx get_memory_rtx (tree, tree);
92 static int apply_args_size (void);
93 static int apply_result_size (void);
94 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
95 static rtx result_vector (int, rtx);
96 #endif
97 static void expand_builtin_update_setjmp_buf (rtx);
98 static void expand_builtin_prefetch (tree);
99 static rtx expand_builtin_apply_args (void);
100 static rtx expand_builtin_apply_args_1 (void);
101 static rtx expand_builtin_apply (rtx, rtx, rtx);
102 static void expand_builtin_return (rtx);
103 static enum type_class type_to_class (tree);
104 static rtx expand_builtin_classify_type (tree);
105 static void expand_errno_check (tree, rtx);
106 static rtx expand_builtin_mathfn (tree, rtx, rtx);
107 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
109 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
110 static rtx expand_builtin_sincos (tree);
111 static rtx expand_builtin_cexpi (tree, rtx, rtx);
112 static rtx expand_builtin_int_roundingfn (tree, rtx);
113 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
114 static rtx expand_builtin_next_arg (void);
115 static rtx expand_builtin_va_start (tree);
116 static rtx expand_builtin_va_end (tree);
117 static rtx expand_builtin_va_copy (tree);
118 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_strcmp (tree, rtx);
120 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
121 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memcpy (tree, rtx);
123 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
125 enum machine_mode, int);
126 static rtx expand_builtin_strcpy (tree, rtx);
127 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
128 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strncpy (tree, rtx);
130 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
131 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
133 static rtx expand_builtin_bzero (tree);
134 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_alloca (tree, rtx);
136 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
137 static rtx expand_builtin_frame_address (tree, tree);
138 static tree stabilize_va_list_loc (location_t, tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_expect (location_t, tree, tree);
142 static tree fold_builtin_classify_type (tree);
143 static tree fold_builtin_strlen (location_t, tree, tree);
144 static tree fold_builtin_inf (location_t, tree, int);
145 static tree fold_builtin_nan (tree, tree, int);
146 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
147 static bool validate_arg (const_tree, enum tree_code code);
148 static bool integer_valued_real_p (tree);
149 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
150 static bool readonly_data_expr (tree);
151 static rtx expand_builtin_fabs (tree, rtx, rtx);
152 static rtx expand_builtin_signbit (tree, rtx);
153 static tree fold_builtin_sqrt (location_t, tree, tree);
154 static tree fold_builtin_cbrt (location_t, tree, tree);
155 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
156 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_cos (location_t, tree, tree, tree);
158 static tree fold_builtin_cosh (location_t, tree, tree, tree);
159 static tree fold_builtin_tan (tree, tree);
160 static tree fold_builtin_trunc (location_t, tree, tree);
161 static tree fold_builtin_floor (location_t, tree, tree);
162 static tree fold_builtin_ceil (location_t, tree, tree);
163 static tree fold_builtin_round (location_t, tree, tree);
164 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
165 static tree fold_builtin_bitop (tree, tree);
166 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
167 static tree fold_builtin_strchr (location_t, tree, tree, tree);
168 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
169 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
170 static tree fold_builtin_strcmp (location_t, tree, tree);
171 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
172 static tree fold_builtin_signbit (location_t, tree, tree);
173 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
174 static tree fold_builtin_isascii (location_t, tree);
175 static tree fold_builtin_toascii (location_t, tree);
176 static tree fold_builtin_isdigit (location_t, tree);
177 static tree fold_builtin_fabs (location_t, tree, tree);
178 static tree fold_builtin_abs (location_t, tree, tree);
179 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
180 enum tree_code);
181 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
182 static tree fold_builtin_0 (location_t, tree, bool);
183 static tree fold_builtin_1 (location_t, tree, tree, bool);
184 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
185 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
186 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
187 static tree fold_builtin_varargs (location_t, tree, tree, bool);
189 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
190 static tree fold_builtin_strstr (location_t, tree, tree, tree);
191 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
192 static tree fold_builtin_strcat (location_t, tree, tree);
193 static tree fold_builtin_strncat (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
196 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198 static rtx expand_builtin_object_size (tree);
199 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
200 enum built_in_function);
201 static void maybe_emit_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_free_warning (tree);
204 static tree fold_builtin_object_size (tree, tree);
205 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
206 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
207 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
208 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
209 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
210 enum built_in_function);
211 static bool init_target_chars (void);
213 static unsigned HOST_WIDE_INT target_newline;
214 static unsigned HOST_WIDE_INT target_percent;
215 static unsigned HOST_WIDE_INT target_c;
216 static unsigned HOST_WIDE_INT target_s;
217 static char target_percent_c[3];
218 static char target_percent_s[3];
219 static char target_percent_s_newline[4];
220 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
221 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
222 static tree do_mpfr_arg2 (tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_arg3 (tree, tree, tree, tree,
225 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
226 static tree do_mpfr_sincos (tree, tree, tree);
227 static tree do_mpfr_bessel_n (tree, tree, tree,
228 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
229 const REAL_VALUE_TYPE *, bool);
230 static tree do_mpfr_remquo (tree, tree, tree);
231 static tree do_mpfr_lgamma_r (tree, tree, tree);
233 /* Return true if NAME starts with __builtin_ or __sync_. */
235 bool
236 is_builtin_name (const char *name)
238 if (strncmp (name, "__builtin_", 10) == 0)
239 return true;
240 if (strncmp (name, "__sync_", 7) == 0)
241 return true;
242 return false;
246 /* Return true if DECL is a function symbol representing a built-in. */
248 bool
249 is_builtin_fn (tree decl)
251 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
255 /* Return true if NODE should be considered for inline expansion regardless
256 of the optimization level. This means whenever a function is invoked with
257 its "internal" name, which normally contains the prefix "__builtin". */
259 static bool
260 called_as_built_in (tree node)
262 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
263 we want the name used to call the function, not the name it
264 will have. */
265 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
266 return is_builtin_name (name);
269 /* Return the alignment in bits of EXP, an object.
270 Don't return more than MAX_ALIGN no matter what, 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 (TREE_CODE (exp) == CONST_DECL)
332 exp = DECL_INITIAL (exp);
333 if (DECL_P (exp)
334 && TREE_CODE (exp) != LABEL_DECL)
335 align = MIN (inner, DECL_ALIGN (exp));
336 #ifdef CONSTANT_ALIGNMENT
337 else if (CONSTANT_CLASS_P (exp))
338 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
339 #endif
340 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
341 || TREE_CODE (exp) == INDIRECT_REF)
342 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
343 else
344 align = MIN (align, inner);
345 return MIN (align, max_align);
348 /* Returns true iff we can trust that alignment information has been
349 calculated properly. */
351 bool
352 can_trust_pointer_alignment (void)
354 /* We rely on TER to compute accurate alignment information. */
355 return (optimize && flag_tree_ter);
358 /* Return the alignment in bits of EXP, a pointer valued expression.
359 But don't return more than MAX_ALIGN no matter what.
360 The alignment returned is, by default, the alignment of the thing that
361 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
363 Otherwise, look at the expression to see if we can do better, i.e., if the
364 expression is actually pointing at an object whose alignment is tighter. */
367 get_pointer_alignment (tree exp, unsigned int max_align)
369 unsigned int align, inner;
371 if (!can_trust_pointer_alignment ())
372 return 0;
374 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
375 return 0;
377 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
378 align = MIN (align, max_align);
380 while (1)
382 switch (TREE_CODE (exp))
384 CASE_CONVERT:
385 exp = TREE_OPERAND (exp, 0);
386 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
387 return align;
389 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
390 align = MIN (inner, max_align);
391 break;
393 case POINTER_PLUS_EXPR:
394 /* If sum of pointer + int, restrict our maximum alignment to that
395 imposed by the integer. If not, we can't do any better than
396 ALIGN. */
397 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
398 return align;
400 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
401 & (max_align / BITS_PER_UNIT - 1))
402 != 0)
403 max_align >>= 1;
405 exp = TREE_OPERAND (exp, 0);
406 break;
408 case ADDR_EXPR:
409 /* See what we are pointing at and look at its alignment. */
410 return get_object_alignment (TREE_OPERAND (exp, 0), align, max_align);
412 default:
413 return align;
418 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
419 way, because it could contain a zero byte in the middle.
420 TREE_STRING_LENGTH is the size of the character array, not the string.
422 ONLY_VALUE should be nonzero if the result is not going to be emitted
423 into the instruction stream and zero if it is going to be expanded.
424 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
425 is returned, otherwise NULL, since
426 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
427 evaluate the side-effects.
429 The value returned is of type `ssizetype'.
431 Unfortunately, string_constant can't access the values of const char
432 arrays with initializers, so neither can we do so here. */
434 tree
435 c_strlen (tree src, int only_value)
437 tree offset_node;
438 HOST_WIDE_INT offset;
439 int max;
440 const char *ptr;
441 location_t loc;
443 STRIP_NOPS (src);
444 if (TREE_CODE (src) == COND_EXPR
445 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
447 tree len1, len2;
449 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
450 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
451 if (tree_int_cst_equal (len1, len2))
452 return len1;
455 if (TREE_CODE (src) == COMPOUND_EXPR
456 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
457 return c_strlen (TREE_OPERAND (src, 1), only_value);
459 if (EXPR_HAS_LOCATION (src))
460 loc = EXPR_LOCATION (src);
461 else
462 loc = input_location;
464 src = string_constant (src, &offset_node);
465 if (src == 0)
466 return NULL_TREE;
468 max = TREE_STRING_LENGTH (src) - 1;
469 ptr = TREE_STRING_POINTER (src);
471 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
473 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
474 compute the offset to the following null if we don't know where to
475 start searching for it. */
476 int i;
478 for (i = 0; i < max; i++)
479 if (ptr[i] == 0)
480 return NULL_TREE;
482 /* We don't know the starting offset, but we do know that the string
483 has no internal zero bytes. We can assume that the offset falls
484 within the bounds of the string; otherwise, the programmer deserves
485 what he gets. Subtract the offset from the length of the string,
486 and return that. This would perhaps not be valid if we were dealing
487 with named arrays in addition to literal string constants. */
489 return size_diffop_loc (loc, size_int (max), offset_node);
492 /* We have a known offset into the string. Start searching there for
493 a null character if we can represent it as a single HOST_WIDE_INT. */
494 if (offset_node == 0)
495 offset = 0;
496 else if (! host_integerp (offset_node, 0))
497 offset = -1;
498 else
499 offset = tree_low_cst (offset_node, 0);
501 /* If the offset is known to be out of bounds, warn, and call strlen at
502 runtime. */
503 if (offset < 0 || offset > max)
505 /* Suppress multiple warnings for propagated constant strings. */
506 if (! TREE_NO_WARNING (src))
508 warning_at (loc, 0, "offset outside bounds of constant string");
509 TREE_NO_WARNING (src) = 1;
511 return NULL_TREE;
514 /* Use strlen to search for the first zero byte. Since any strings
515 constructed with build_string will have nulls appended, we win even
516 if we get handed something like (char[4])"abcd".
518 Since OFFSET is our starting index into the string, no further
519 calculation is needed. */
520 return ssize_int (strlen (ptr + offset));
523 /* Return a char pointer for a C string if it is a string constant
524 or sum of string constant and integer constant. */
526 static const char *
527 c_getstr (tree src)
529 tree offset_node;
531 src = string_constant (src, &offset_node);
532 if (src == 0)
533 return 0;
535 if (offset_node == 0)
536 return TREE_STRING_POINTER (src);
537 else if (!host_integerp (offset_node, 1)
538 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
539 return 0;
541 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
544 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
545 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
547 static rtx
548 c_readstr (const char *str, enum machine_mode mode)
550 HOST_WIDE_INT c[2];
551 HOST_WIDE_INT ch;
552 unsigned int i, j;
554 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
556 c[0] = 0;
557 c[1] = 0;
558 ch = 1;
559 for (i = 0; i < GET_MODE_SIZE (mode); i++)
561 j = i;
562 if (WORDS_BIG_ENDIAN)
563 j = GET_MODE_SIZE (mode) - i - 1;
564 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
565 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
566 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
567 j *= BITS_PER_UNIT;
568 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
570 if (ch)
571 ch = (unsigned char) str[i];
572 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
574 return immed_double_const (c[0], c[1], mode);
577 /* Cast a target constant CST to target CHAR and if that value fits into
578 host char type, return zero and put that value into variable pointed to by
579 P. */
581 static int
582 target_char_cast (tree cst, char *p)
584 unsigned HOST_WIDE_INT val, hostval;
586 if (!host_integerp (cst, 1)
587 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
588 return 1;
590 val = tree_low_cst (cst, 1);
591 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
592 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
594 hostval = val;
595 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
596 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
598 if (val != hostval)
599 return 1;
601 *p = hostval;
602 return 0;
605 /* Similar to save_expr, but assumes that arbitrary code is not executed
606 in between the multiple evaluations. In particular, we assume that a
607 non-addressable local variable will not be modified. */
609 static tree
610 builtin_save_expr (tree exp)
612 if (TREE_ADDRESSABLE (exp) == 0
613 && (TREE_CODE (exp) == PARM_DECL
614 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
615 return exp;
617 return save_expr (exp);
620 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
621 times to get the address of either a higher stack frame, or a return
622 address located within it (depending on FNDECL_CODE). */
624 static rtx
625 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
627 int i;
629 #ifdef INITIAL_FRAME_ADDRESS_RTX
630 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
631 #else
632 rtx tem;
634 /* For a zero count with __builtin_return_address, we don't care what
635 frame address we return, because target-specific definitions will
636 override us. Therefore frame pointer elimination is OK, and using
637 the soft frame pointer is OK.
639 For a nonzero count, or a zero count with __builtin_frame_address,
640 we require a stable offset from the current frame pointer to the
641 previous one, so we must use the hard frame pointer, and
642 we must disable frame pointer elimination. */
643 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
644 tem = frame_pointer_rtx;
645 else
647 tem = hard_frame_pointer_rtx;
649 /* Tell reload not to eliminate the frame pointer. */
650 crtl->accesses_prior_frames = 1;
652 #endif
654 /* Some machines need special handling before we can access
655 arbitrary frames. For example, on the SPARC, we must first flush
656 all register windows to the stack. */
657 #ifdef SETUP_FRAME_ADDRESSES
658 if (count > 0)
659 SETUP_FRAME_ADDRESSES ();
660 #endif
662 /* On the SPARC, the return address is not in the frame, it is in a
663 register. There is no way to access it off of the current frame
664 pointer, but it can be accessed off the previous frame pointer by
665 reading the value from the register window save area. */
666 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
667 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
668 count--;
669 #endif
671 /* Scan back COUNT frames to the specified frame. */
672 for (i = 0; i < count; i++)
674 /* Assume the dynamic chain pointer is in the word that the
675 frame address points to, unless otherwise specified. */
676 #ifdef DYNAMIC_CHAIN_ADDRESS
677 tem = DYNAMIC_CHAIN_ADDRESS (tem);
678 #endif
679 tem = memory_address (Pmode, tem);
680 tem = gen_frame_mem (Pmode, tem);
681 tem = copy_to_reg (tem);
684 /* For __builtin_frame_address, return what we've got. But, on
685 the SPARC for example, we may have to add a bias. */
686 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
687 #ifdef FRAME_ADDR_RTX
688 return FRAME_ADDR_RTX (tem);
689 #else
690 return tem;
691 #endif
693 /* For __builtin_return_address, get the return address from that frame. */
694 #ifdef RETURN_ADDR_RTX
695 tem = RETURN_ADDR_RTX (count, tem);
696 #else
697 tem = memory_address (Pmode,
698 plus_constant (tem, GET_MODE_SIZE (Pmode)));
699 tem = gen_frame_mem (Pmode, tem);
700 #endif
701 return tem;
704 /* Alias set used for setjmp buffer. */
705 static alias_set_type setjmp_alias_set = -1;
707 /* Construct the leading half of a __builtin_setjmp call. Control will
708 return to RECEIVER_LABEL. This is also called directly by the SJLJ
709 exception handling code. */
711 void
712 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
714 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
715 rtx stack_save;
716 rtx mem;
718 if (setjmp_alias_set == -1)
719 setjmp_alias_set = new_alias_set ();
721 buf_addr = convert_memory_address (Pmode, buf_addr);
723 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
725 /* We store the frame pointer and the address of receiver_label in
726 the buffer and use the rest of it for the stack save area, which
727 is machine-dependent. */
729 mem = gen_rtx_MEM (Pmode, buf_addr);
730 set_mem_alias_set (mem, setjmp_alias_set);
731 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
733 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
734 set_mem_alias_set (mem, setjmp_alias_set);
736 emit_move_insn (validize_mem (mem),
737 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
739 stack_save = gen_rtx_MEM (sa_mode,
740 plus_constant (buf_addr,
741 2 * GET_MODE_SIZE (Pmode)));
742 set_mem_alias_set (stack_save, setjmp_alias_set);
743 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
745 /* If there is further processing to do, do it. */
746 #ifdef HAVE_builtin_setjmp_setup
747 if (HAVE_builtin_setjmp_setup)
748 emit_insn (gen_builtin_setjmp_setup (buf_addr));
749 #endif
751 /* Tell optimize_save_area_alloca that extra work is going to
752 need to go on during alloca. */
753 cfun->calls_setjmp = 1;
755 /* We have a nonlocal label. */
756 cfun->has_nonlocal_label = 1;
759 /* Construct the trailing part of a __builtin_setjmp call. This is
760 also called directly by the SJLJ exception handling code. */
762 void
763 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
765 rtx chain;
767 /* Clobber the FP when we get here, so we have to make sure it's
768 marked as used by this function. */
769 emit_use (hard_frame_pointer_rtx);
771 /* Mark the static chain as clobbered here so life information
772 doesn't get messed up for it. */
773 chain = targetm.calls.static_chain (current_function_decl, true);
774 if (chain && REG_P (chain))
775 emit_clobber (chain);
777 /* Now put in the code to restore the frame pointer, and argument
778 pointer, if needed. */
779 #ifdef HAVE_nonlocal_goto
780 if (! HAVE_nonlocal_goto)
781 #endif
783 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
784 /* This might change the hard frame pointer in ways that aren't
785 apparent to early optimization passes, so force a clobber. */
786 emit_clobber (hard_frame_pointer_rtx);
789 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
790 if (fixed_regs[ARG_POINTER_REGNUM])
792 #ifdef ELIMINABLE_REGS
793 size_t i;
794 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
796 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
797 if (elim_regs[i].from == ARG_POINTER_REGNUM
798 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
799 break;
801 if (i == ARRAY_SIZE (elim_regs))
802 #endif
804 /* Now restore our arg pointer from the address at which it
805 was saved in our stack frame. */
806 emit_move_insn (crtl->args.internal_arg_pointer,
807 copy_to_reg (get_arg_pointer_save_area ()));
810 #endif
812 #ifdef HAVE_builtin_setjmp_receiver
813 if (HAVE_builtin_setjmp_receiver)
814 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
815 else
816 #endif
817 #ifdef HAVE_nonlocal_goto_receiver
818 if (HAVE_nonlocal_goto_receiver)
819 emit_insn (gen_nonlocal_goto_receiver ());
820 else
821 #endif
822 { /* Nothing */ }
824 /* We must not allow the code we just generated to be reordered by
825 scheduling. Specifically, the update of the frame pointer must
826 happen immediately, not later. */
827 emit_insn (gen_blockage ());
830 /* __builtin_longjmp is passed a pointer to an array of five words (not
831 all will be used on all machines). It operates similarly to the C
832 library function of the same name, but is more efficient. Much of
833 the code below is copied from the handling of non-local gotos. */
835 static void
836 expand_builtin_longjmp (rtx buf_addr, rtx value)
838 rtx fp, lab, stack, insn, last;
839 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
841 /* DRAP is needed for stack realign if longjmp is expanded to current
842 function */
843 if (SUPPORTS_STACK_ALIGNMENT)
844 crtl->need_drap = true;
846 if (setjmp_alias_set == -1)
847 setjmp_alias_set = new_alias_set ();
849 buf_addr = convert_memory_address (Pmode, buf_addr);
851 buf_addr = force_reg (Pmode, buf_addr);
853 /* We require that the user must pass a second argument of 1, because
854 that is what builtin_setjmp will return. */
855 gcc_assert (value == const1_rtx);
857 last = get_last_insn ();
858 #ifdef HAVE_builtin_longjmp
859 if (HAVE_builtin_longjmp)
860 emit_insn (gen_builtin_longjmp (buf_addr));
861 else
862 #endif
864 fp = gen_rtx_MEM (Pmode, buf_addr);
865 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
866 GET_MODE_SIZE (Pmode)));
868 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
869 2 * GET_MODE_SIZE (Pmode)));
870 set_mem_alias_set (fp, setjmp_alias_set);
871 set_mem_alias_set (lab, setjmp_alias_set);
872 set_mem_alias_set (stack, setjmp_alias_set);
874 /* Pick up FP, label, and SP from the block and jump. This code is
875 from expand_goto in stmt.c; see there for detailed comments. */
876 #ifdef HAVE_nonlocal_goto
877 if (HAVE_nonlocal_goto)
878 /* We have to pass a value to the nonlocal_goto pattern that will
879 get copied into the static_chain pointer, but it does not matter
880 what that value is, because builtin_setjmp does not use it. */
881 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
882 else
883 #endif
885 lab = copy_to_reg (lab);
887 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
888 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
890 emit_move_insn (hard_frame_pointer_rtx, fp);
891 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
893 emit_use (hard_frame_pointer_rtx);
894 emit_use (stack_pointer_rtx);
895 emit_indirect_jump (lab);
899 /* Search backwards and mark the jump insn as a non-local goto.
900 Note that this precludes the use of __builtin_longjmp to a
901 __builtin_setjmp target in the same function. However, we've
902 already cautioned the user that these functions are for
903 internal exception handling use only. */
904 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
906 gcc_assert (insn != last);
908 if (JUMP_P (insn))
910 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
911 break;
913 else if (CALL_P (insn))
914 break;
918 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
919 and the address of the save area. */
921 static rtx
922 expand_builtin_nonlocal_goto (tree exp)
924 tree t_label, t_save_area;
925 rtx r_label, r_save_area, r_fp, r_sp, insn;
927 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
928 return NULL_RTX;
930 t_label = CALL_EXPR_ARG (exp, 0);
931 t_save_area = CALL_EXPR_ARG (exp, 1);
933 r_label = expand_normal (t_label);
934 r_label = convert_memory_address (Pmode, r_label);
935 r_save_area = expand_normal (t_save_area);
936 r_save_area = convert_memory_address (Pmode, r_save_area);
937 /* Copy the address of the save location to a register just in case it was based
938 on the frame pointer. */
939 r_save_area = copy_to_reg (r_save_area);
940 r_fp = gen_rtx_MEM (Pmode, r_save_area);
941 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
942 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
944 crtl->has_nonlocal_goto = 1;
946 #ifdef HAVE_nonlocal_goto
947 /* ??? We no longer need to pass the static chain value, afaik. */
948 if (HAVE_nonlocal_goto)
949 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
950 else
951 #endif
953 r_label = copy_to_reg (r_label);
955 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
956 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
958 /* Restore frame pointer for containing function.
959 This sets the actual hard register used for the frame pointer
960 to the location of the function's incoming static chain info.
961 The non-local goto handler will then adjust it to contain the
962 proper value and reload the argument pointer, if needed. */
963 emit_move_insn (hard_frame_pointer_rtx, r_fp);
964 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
966 /* USE of hard_frame_pointer_rtx added for consistency;
967 not clear if really needed. */
968 emit_use (hard_frame_pointer_rtx);
969 emit_use (stack_pointer_rtx);
971 /* If the architecture is using a GP register, we must
972 conservatively assume that the target function makes use of it.
973 The prologue of functions with nonlocal gotos must therefore
974 initialize the GP register to the appropriate value, and we
975 must then make sure that this value is live at the point
976 of the jump. (Note that this doesn't necessarily apply
977 to targets with a nonlocal_goto pattern; they are free
978 to implement it in their own way. Note also that this is
979 a no-op if the GP register is a global invariant.) */
980 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
981 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
982 emit_use (pic_offset_table_rtx);
984 emit_indirect_jump (r_label);
987 /* Search backwards to the jump insn and mark it as a
988 non-local goto. */
989 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
991 if (JUMP_P (insn))
993 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
994 break;
996 else if (CALL_P (insn))
997 break;
1000 return const0_rtx;
1003 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1004 (not all will be used on all machines) that was passed to __builtin_setjmp.
1005 It updates the stack pointer in that block to correspond to the current
1006 stack pointer. */
1008 static void
1009 expand_builtin_update_setjmp_buf (rtx buf_addr)
1011 enum machine_mode sa_mode = Pmode;
1012 rtx stack_save;
1015 #ifdef HAVE_save_stack_nonlocal
1016 if (HAVE_save_stack_nonlocal)
1017 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1018 #endif
1019 #ifdef STACK_SAVEAREA_MODE
1020 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1021 #endif
1023 stack_save
1024 = gen_rtx_MEM (sa_mode,
1025 memory_address
1026 (sa_mode,
1027 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1029 #ifdef HAVE_setjmp
1030 if (HAVE_setjmp)
1031 emit_insn (gen_setjmp ());
1032 #endif
1034 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1037 /* Expand a call to __builtin_prefetch. For a target that does not support
1038 data prefetch, evaluate the memory address argument in case it has side
1039 effects. */
1041 static void
1042 expand_builtin_prefetch (tree exp)
1044 tree arg0, arg1, arg2;
1045 int nargs;
1046 rtx op0, op1, op2;
1048 if (!validate_arglist (exp, POINTER_TYPE, 0))
1049 return;
1051 arg0 = CALL_EXPR_ARG (exp, 0);
1053 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1054 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1055 locality). */
1056 nargs = call_expr_nargs (exp);
1057 if (nargs > 1)
1058 arg1 = CALL_EXPR_ARG (exp, 1);
1059 else
1060 arg1 = integer_zero_node;
1061 if (nargs > 2)
1062 arg2 = CALL_EXPR_ARG (exp, 2);
1063 else
1064 arg2 = integer_three_node;
1066 /* Argument 0 is an address. */
1067 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1069 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1070 if (TREE_CODE (arg1) != INTEGER_CST)
1072 error ("second argument to %<__builtin_prefetch%> must be a constant");
1073 arg1 = integer_zero_node;
1075 op1 = expand_normal (arg1);
1076 /* Argument 1 must be either zero or one. */
1077 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1079 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1080 " using zero");
1081 op1 = const0_rtx;
1084 /* Argument 2 (locality) must be a compile-time constant int. */
1085 if (TREE_CODE (arg2) != INTEGER_CST)
1087 error ("third argument to %<__builtin_prefetch%> must be a constant");
1088 arg2 = integer_zero_node;
1090 op2 = expand_normal (arg2);
1091 /* Argument 2 must be 0, 1, 2, or 3. */
1092 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1094 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1095 op2 = const0_rtx;
1098 #ifdef HAVE_prefetch
1099 if (HAVE_prefetch)
1101 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1102 (op0,
1103 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1104 || (GET_MODE (op0) != Pmode))
1106 op0 = convert_memory_address (Pmode, op0);
1107 op0 = force_reg (Pmode, op0);
1109 emit_insn (gen_prefetch (op0, op1, op2));
1111 #endif
1113 /* Don't do anything with direct references to volatile memory, but
1114 generate code to handle other side effects. */
1115 if (!MEM_P (op0) && side_effects_p (op0))
1116 emit_insn (op0);
1119 /* Get a MEM rtx for expression EXP which is the address of an operand
1120 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1121 the maximum length of the block of memory that might be accessed or
1122 NULL if unknown. */
1124 static rtx
1125 get_memory_rtx (tree exp, tree len)
1127 tree orig_exp = exp;
1128 rtx addr, mem;
1129 HOST_WIDE_INT off;
1131 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1132 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1133 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1134 exp = TREE_OPERAND (exp, 0);
1136 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1137 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1139 /* Get an expression we can use to find the attributes to assign to MEM.
1140 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1141 we can. First remove any nops. */
1142 while (CONVERT_EXPR_P (exp)
1143 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1144 exp = TREE_OPERAND (exp, 0);
1146 off = 0;
1147 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1148 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1149 && host_integerp (TREE_OPERAND (exp, 1), 0)
1150 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1151 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1152 else if (TREE_CODE (exp) == ADDR_EXPR)
1153 exp = TREE_OPERAND (exp, 0);
1154 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1155 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1156 else
1157 exp = NULL;
1159 /* Honor attributes derived from exp, except for the alias set
1160 (as builtin stringops may alias with anything) and the size
1161 (as stringops may access multiple array elements). */
1162 if (exp)
1164 set_mem_attributes (mem, exp, 0);
1166 if (off)
1167 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1169 /* Allow the string and memory builtins to overflow from one
1170 field into another, see http://gcc.gnu.org/PR23561.
1171 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1172 memory accessed by the string or memory builtin will fit
1173 within the field. */
1174 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1176 tree mem_expr = MEM_EXPR (mem);
1177 HOST_WIDE_INT offset = -1, length = -1;
1178 tree inner = exp;
1180 while (TREE_CODE (inner) == ARRAY_REF
1181 || CONVERT_EXPR_P (inner)
1182 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1183 || TREE_CODE (inner) == SAVE_EXPR)
1184 inner = TREE_OPERAND (inner, 0);
1186 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1188 if (MEM_OFFSET (mem)
1189 && CONST_INT_P (MEM_OFFSET (mem)))
1190 offset = INTVAL (MEM_OFFSET (mem));
1192 if (offset >= 0 && len && host_integerp (len, 0))
1193 length = tree_low_cst (len, 0);
1195 while (TREE_CODE (inner) == COMPONENT_REF)
1197 tree field = TREE_OPERAND (inner, 1);
1198 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1199 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1201 /* Bitfields are generally not byte-addressable. */
1202 gcc_assert (!DECL_BIT_FIELD (field)
1203 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1204 % BITS_PER_UNIT) == 0
1205 && host_integerp (DECL_SIZE (field), 0)
1206 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1207 % BITS_PER_UNIT) == 0));
1209 /* If we can prove that the memory starting at XEXP (mem, 0) and
1210 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1211 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1212 fields without DECL_SIZE_UNIT like flexible array members. */
1213 if (length >= 0
1214 && DECL_SIZE_UNIT (field)
1215 && host_integerp (DECL_SIZE_UNIT (field), 0))
1217 HOST_WIDE_INT size
1218 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1219 if (offset <= size
1220 && length <= size
1221 && offset + length <= size)
1222 break;
1225 if (offset >= 0
1226 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1227 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1228 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1229 / BITS_PER_UNIT;
1230 else
1232 offset = -1;
1233 length = -1;
1236 mem_expr = TREE_OPERAND (mem_expr, 0);
1237 inner = TREE_OPERAND (inner, 0);
1240 if (mem_expr == NULL)
1241 offset = -1;
1242 if (mem_expr != MEM_EXPR (mem))
1244 set_mem_expr (mem, mem_expr);
1245 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1248 set_mem_alias_set (mem, 0);
1249 set_mem_size (mem, NULL_RTX);
1252 return mem;
1255 /* Built-in functions to perform an untyped call and return. */
1257 #define apply_args_mode \
1258 (this_target_builtins->x_apply_args_mode)
1259 #define apply_result_mode \
1260 (this_target_builtins->x_apply_result_mode)
1262 /* Return the size required for the block returned by __builtin_apply_args,
1263 and initialize apply_args_mode. */
1265 static int
1266 apply_args_size (void)
1268 static int size = -1;
1269 int align;
1270 unsigned int regno;
1271 enum machine_mode mode;
1273 /* The values computed by this function never change. */
1274 if (size < 0)
1276 /* The first value is the incoming arg-pointer. */
1277 size = GET_MODE_SIZE (Pmode);
1279 /* The second value is the structure value address unless this is
1280 passed as an "invisible" first argument. */
1281 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1282 size += GET_MODE_SIZE (Pmode);
1284 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1285 if (FUNCTION_ARG_REGNO_P (regno))
1287 mode = reg_raw_mode[regno];
1289 gcc_assert (mode != VOIDmode);
1291 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1292 if (size % align != 0)
1293 size = CEIL (size, align) * align;
1294 size += GET_MODE_SIZE (mode);
1295 apply_args_mode[regno] = mode;
1297 else
1299 apply_args_mode[regno] = VOIDmode;
1302 return size;
1305 /* Return the size required for the block returned by __builtin_apply,
1306 and initialize apply_result_mode. */
1308 static int
1309 apply_result_size (void)
1311 static int size = -1;
1312 int align, regno;
1313 enum machine_mode mode;
1315 /* The values computed by this function never change. */
1316 if (size < 0)
1318 size = 0;
1320 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1321 if (targetm.calls.function_value_regno_p (regno))
1323 mode = reg_raw_mode[regno];
1325 gcc_assert (mode != VOIDmode);
1327 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1328 if (size % align != 0)
1329 size = CEIL (size, align) * align;
1330 size += GET_MODE_SIZE (mode);
1331 apply_result_mode[regno] = mode;
1333 else
1334 apply_result_mode[regno] = VOIDmode;
1336 /* Allow targets that use untyped_call and untyped_return to override
1337 the size so that machine-specific information can be stored here. */
1338 #ifdef APPLY_RESULT_SIZE
1339 size = APPLY_RESULT_SIZE;
1340 #endif
1342 return size;
1345 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1346 /* Create a vector describing the result block RESULT. If SAVEP is true,
1347 the result block is used to save the values; otherwise it is used to
1348 restore the values. */
1350 static rtx
1351 result_vector (int savep, rtx result)
1353 int regno, size, align, nelts;
1354 enum machine_mode mode;
1355 rtx reg, mem;
1356 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1358 size = nelts = 0;
1359 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1360 if ((mode = apply_result_mode[regno]) != VOIDmode)
1362 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1363 if (size % align != 0)
1364 size = CEIL (size, align) * align;
1365 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1366 mem = adjust_address (result, mode, size);
1367 savevec[nelts++] = (savep
1368 ? gen_rtx_SET (VOIDmode, mem, reg)
1369 : gen_rtx_SET (VOIDmode, reg, mem));
1370 size += GET_MODE_SIZE (mode);
1372 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1374 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1376 /* Save the state required to perform an untyped call with the same
1377 arguments as were passed to the current function. */
1379 static rtx
1380 expand_builtin_apply_args_1 (void)
1382 rtx registers, tem;
1383 int size, align, regno;
1384 enum machine_mode mode;
1385 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1387 /* Create a block where the arg-pointer, structure value address,
1388 and argument registers can be saved. */
1389 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1391 /* Walk past the arg-pointer and structure value address. */
1392 size = GET_MODE_SIZE (Pmode);
1393 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1394 size += GET_MODE_SIZE (Pmode);
1396 /* Save each register used in calling a function to the block. */
1397 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1398 if ((mode = apply_args_mode[regno]) != VOIDmode)
1400 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1401 if (size % align != 0)
1402 size = CEIL (size, align) * align;
1404 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1406 emit_move_insn (adjust_address (registers, mode, size), tem);
1407 size += GET_MODE_SIZE (mode);
1410 /* Save the arg pointer to the block. */
1411 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1412 #ifdef STACK_GROWS_DOWNWARD
1413 /* We need the pointer as the caller actually passed them to us, not
1414 as we might have pretended they were passed. Make sure it's a valid
1415 operand, as emit_move_insn isn't expected to handle a PLUS. */
1417 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1418 NULL_RTX);
1419 #endif
1420 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1422 size = GET_MODE_SIZE (Pmode);
1424 /* Save the structure value address unless this is passed as an
1425 "invisible" first argument. */
1426 if (struct_incoming_value)
1428 emit_move_insn (adjust_address (registers, Pmode, size),
1429 copy_to_reg (struct_incoming_value));
1430 size += GET_MODE_SIZE (Pmode);
1433 /* Return the address of the block. */
1434 return copy_addr_to_reg (XEXP (registers, 0));
1437 /* __builtin_apply_args returns block of memory allocated on
1438 the stack into which is stored the arg pointer, structure
1439 value address, static chain, and all the registers that might
1440 possibly be used in performing a function call. The code is
1441 moved to the start of the function so the incoming values are
1442 saved. */
1444 static rtx
1445 expand_builtin_apply_args (void)
1447 /* Don't do __builtin_apply_args more than once in a function.
1448 Save the result of the first call and reuse it. */
1449 if (apply_args_value != 0)
1450 return apply_args_value;
1452 /* When this function is called, it means that registers must be
1453 saved on entry to this function. So we migrate the
1454 call to the first insn of this function. */
1455 rtx temp;
1456 rtx seq;
1458 start_sequence ();
1459 temp = expand_builtin_apply_args_1 ();
1460 seq = get_insns ();
1461 end_sequence ();
1463 apply_args_value = temp;
1465 /* Put the insns after the NOTE that starts the function.
1466 If this is inside a start_sequence, make the outer-level insn
1467 chain current, so the code is placed at the start of the
1468 function. If internal_arg_pointer is a non-virtual pseudo,
1469 it needs to be placed after the function that initializes
1470 that pseudo. */
1471 push_topmost_sequence ();
1472 if (REG_P (crtl->args.internal_arg_pointer)
1473 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1474 emit_insn_before (seq, parm_birth_insn);
1475 else
1476 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1477 pop_topmost_sequence ();
1478 return temp;
1482 /* Perform an untyped call and save the state required to perform an
1483 untyped return of whatever value was returned by the given function. */
1485 static rtx
1486 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1488 int size, align, regno;
1489 enum machine_mode mode;
1490 rtx incoming_args, result, reg, dest, src, call_insn;
1491 rtx old_stack_level = 0;
1492 rtx call_fusage = 0;
1493 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1495 arguments = convert_memory_address (Pmode, arguments);
1497 /* Create a block where the return registers can be saved. */
1498 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1500 /* Fetch the arg pointer from the ARGUMENTS block. */
1501 incoming_args = gen_reg_rtx (Pmode);
1502 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1503 #ifndef STACK_GROWS_DOWNWARD
1504 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1505 incoming_args, 0, OPTAB_LIB_WIDEN);
1506 #endif
1508 /* Push a new argument block and copy the arguments. Do not allow
1509 the (potential) memcpy call below to interfere with our stack
1510 manipulations. */
1511 do_pending_stack_adjust ();
1512 NO_DEFER_POP;
1514 /* Save the stack with nonlocal if available. */
1515 #ifdef HAVE_save_stack_nonlocal
1516 if (HAVE_save_stack_nonlocal)
1517 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1518 else
1519 #endif
1520 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1522 /* Allocate a block of memory onto the stack and copy the memory
1523 arguments to the outgoing arguments address. */
1524 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1526 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1527 may have already set current_function_calls_alloca to true.
1528 current_function_calls_alloca won't be set if argsize is zero,
1529 so we have to guarantee need_drap is true here. */
1530 if (SUPPORTS_STACK_ALIGNMENT)
1531 crtl->need_drap = true;
1533 dest = virtual_outgoing_args_rtx;
1534 #ifndef STACK_GROWS_DOWNWARD
1535 if (CONST_INT_P (argsize))
1536 dest = plus_constant (dest, -INTVAL (argsize));
1537 else
1538 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1539 #endif
1540 dest = gen_rtx_MEM (BLKmode, dest);
1541 set_mem_align (dest, PARM_BOUNDARY);
1542 src = gen_rtx_MEM (BLKmode, incoming_args);
1543 set_mem_align (src, PARM_BOUNDARY);
1544 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1546 /* Refer to the argument block. */
1547 apply_args_size ();
1548 arguments = gen_rtx_MEM (BLKmode, arguments);
1549 set_mem_align (arguments, PARM_BOUNDARY);
1551 /* Walk past the arg-pointer and structure value address. */
1552 size = GET_MODE_SIZE (Pmode);
1553 if (struct_value)
1554 size += GET_MODE_SIZE (Pmode);
1556 /* Restore each of the registers previously saved. Make USE insns
1557 for each of these registers for use in making the call. */
1558 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1559 if ((mode = apply_args_mode[regno]) != VOIDmode)
1561 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1562 if (size % align != 0)
1563 size = CEIL (size, align) * align;
1564 reg = gen_rtx_REG (mode, regno);
1565 emit_move_insn (reg, adjust_address (arguments, mode, size));
1566 use_reg (&call_fusage, reg);
1567 size += GET_MODE_SIZE (mode);
1570 /* Restore the structure value address unless this is passed as an
1571 "invisible" first argument. */
1572 size = GET_MODE_SIZE (Pmode);
1573 if (struct_value)
1575 rtx value = gen_reg_rtx (Pmode);
1576 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1577 emit_move_insn (struct_value, value);
1578 if (REG_P (struct_value))
1579 use_reg (&call_fusage, struct_value);
1580 size += GET_MODE_SIZE (Pmode);
1583 /* All arguments and registers used for the call are set up by now! */
1584 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1586 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1587 and we don't want to load it into a register as an optimization,
1588 because prepare_call_address already did it if it should be done. */
1589 if (GET_CODE (function) != SYMBOL_REF)
1590 function = memory_address (FUNCTION_MODE, function);
1592 /* Generate the actual call instruction and save the return value. */
1593 #ifdef HAVE_untyped_call
1594 if (HAVE_untyped_call)
1595 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1596 result, result_vector (1, result)));
1597 else
1598 #endif
1599 #ifdef HAVE_call_value
1600 if (HAVE_call_value)
1602 rtx valreg = 0;
1604 /* Locate the unique return register. It is not possible to
1605 express a call that sets more than one return register using
1606 call_value; use untyped_call for that. In fact, untyped_call
1607 only needs to save the return registers in the given block. */
1608 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1609 if ((mode = apply_result_mode[regno]) != VOIDmode)
1611 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1613 valreg = gen_rtx_REG (mode, regno);
1616 emit_call_insn (GEN_CALL_VALUE (valreg,
1617 gen_rtx_MEM (FUNCTION_MODE, function),
1618 const0_rtx, NULL_RTX, const0_rtx));
1620 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1622 else
1623 #endif
1624 gcc_unreachable ();
1626 /* Find the CALL insn we just emitted, and attach the register usage
1627 information. */
1628 call_insn = last_call_insn ();
1629 add_function_usage_to (call_insn, call_fusage);
1631 /* Restore the stack. */
1632 #ifdef HAVE_save_stack_nonlocal
1633 if (HAVE_save_stack_nonlocal)
1634 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1635 else
1636 #endif
1637 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1639 OK_DEFER_POP;
1641 /* Return the address of the result block. */
1642 result = copy_addr_to_reg (XEXP (result, 0));
1643 return convert_memory_address (ptr_mode, result);
1646 /* Perform an untyped return. */
1648 static void
1649 expand_builtin_return (rtx result)
1651 int size, align, regno;
1652 enum machine_mode mode;
1653 rtx reg;
1654 rtx call_fusage = 0;
1656 result = convert_memory_address (Pmode, result);
1658 apply_result_size ();
1659 result = gen_rtx_MEM (BLKmode, result);
1661 #ifdef HAVE_untyped_return
1662 if (HAVE_untyped_return)
1664 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1665 emit_barrier ();
1666 return;
1668 #endif
1670 /* Restore the return value and note that each value is used. */
1671 size = 0;
1672 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1673 if ((mode = apply_result_mode[regno]) != VOIDmode)
1675 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1676 if (size % align != 0)
1677 size = CEIL (size, align) * align;
1678 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1679 emit_move_insn (reg, adjust_address (result, mode, size));
1681 push_to_sequence (call_fusage);
1682 emit_use (reg);
1683 call_fusage = get_insns ();
1684 end_sequence ();
1685 size += GET_MODE_SIZE (mode);
1688 /* Put the USE insns before the return. */
1689 emit_insn (call_fusage);
1691 /* Return whatever values was restored by jumping directly to the end
1692 of the function. */
1693 expand_naked_return ();
1696 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1698 static enum type_class
1699 type_to_class (tree type)
1701 switch (TREE_CODE (type))
1703 case VOID_TYPE: return void_type_class;
1704 case INTEGER_TYPE: return integer_type_class;
1705 case ENUMERAL_TYPE: return enumeral_type_class;
1706 case BOOLEAN_TYPE: return boolean_type_class;
1707 case POINTER_TYPE: return pointer_type_class;
1708 case REFERENCE_TYPE: return reference_type_class;
1709 case OFFSET_TYPE: return offset_type_class;
1710 case REAL_TYPE: return real_type_class;
1711 case COMPLEX_TYPE: return complex_type_class;
1712 case FUNCTION_TYPE: return function_type_class;
1713 case METHOD_TYPE: return method_type_class;
1714 case RECORD_TYPE: return record_type_class;
1715 case UNION_TYPE:
1716 case QUAL_UNION_TYPE: return union_type_class;
1717 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1718 ? string_type_class : array_type_class);
1719 case LANG_TYPE: return lang_type_class;
1720 default: return no_type_class;
1724 /* Expand a call EXP to __builtin_classify_type. */
1726 static rtx
1727 expand_builtin_classify_type (tree exp)
1729 if (call_expr_nargs (exp))
1730 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1731 return GEN_INT (no_type_class);
1734 /* This helper macro, meant to be used in mathfn_built_in below,
1735 determines which among a set of three builtin math functions is
1736 appropriate for a given type mode. The `F' and `L' cases are
1737 automatically generated from the `double' case. */
1738 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1739 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1740 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1741 fcodel = BUILT_IN_MATHFN##L ; break;
1742 /* Similar to above, but appends _R after any F/L suffix. */
1743 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1744 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1745 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1746 fcodel = BUILT_IN_MATHFN##L_R ; break;
1748 /* Return mathematic function equivalent to FN but operating directly
1749 on TYPE, if available. If IMPLICIT is true find the function in
1750 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1751 can't do the conversion, return zero. */
1753 static tree
1754 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1756 tree const *const fn_arr
1757 = implicit ? implicit_built_in_decls : built_in_decls;
1758 enum built_in_function fcode, fcodef, fcodel;
1760 switch (fn)
1762 CASE_MATHFN (BUILT_IN_ACOS)
1763 CASE_MATHFN (BUILT_IN_ACOSH)
1764 CASE_MATHFN (BUILT_IN_ASIN)
1765 CASE_MATHFN (BUILT_IN_ASINH)
1766 CASE_MATHFN (BUILT_IN_ATAN)
1767 CASE_MATHFN (BUILT_IN_ATAN2)
1768 CASE_MATHFN (BUILT_IN_ATANH)
1769 CASE_MATHFN (BUILT_IN_CBRT)
1770 CASE_MATHFN (BUILT_IN_CEIL)
1771 CASE_MATHFN (BUILT_IN_CEXPI)
1772 CASE_MATHFN (BUILT_IN_COPYSIGN)
1773 CASE_MATHFN (BUILT_IN_COS)
1774 CASE_MATHFN (BUILT_IN_COSH)
1775 CASE_MATHFN (BUILT_IN_DREM)
1776 CASE_MATHFN (BUILT_IN_ERF)
1777 CASE_MATHFN (BUILT_IN_ERFC)
1778 CASE_MATHFN (BUILT_IN_EXP)
1779 CASE_MATHFN (BUILT_IN_EXP10)
1780 CASE_MATHFN (BUILT_IN_EXP2)
1781 CASE_MATHFN (BUILT_IN_EXPM1)
1782 CASE_MATHFN (BUILT_IN_FABS)
1783 CASE_MATHFN (BUILT_IN_FDIM)
1784 CASE_MATHFN (BUILT_IN_FLOOR)
1785 CASE_MATHFN (BUILT_IN_FMA)
1786 CASE_MATHFN (BUILT_IN_FMAX)
1787 CASE_MATHFN (BUILT_IN_FMIN)
1788 CASE_MATHFN (BUILT_IN_FMOD)
1789 CASE_MATHFN (BUILT_IN_FREXP)
1790 CASE_MATHFN (BUILT_IN_GAMMA)
1791 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1792 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1793 CASE_MATHFN (BUILT_IN_HYPOT)
1794 CASE_MATHFN (BUILT_IN_ILOGB)
1795 CASE_MATHFN (BUILT_IN_INF)
1796 CASE_MATHFN (BUILT_IN_ISINF)
1797 CASE_MATHFN (BUILT_IN_J0)
1798 CASE_MATHFN (BUILT_IN_J1)
1799 CASE_MATHFN (BUILT_IN_JN)
1800 CASE_MATHFN (BUILT_IN_LCEIL)
1801 CASE_MATHFN (BUILT_IN_LDEXP)
1802 CASE_MATHFN (BUILT_IN_LFLOOR)
1803 CASE_MATHFN (BUILT_IN_LGAMMA)
1804 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1805 CASE_MATHFN (BUILT_IN_LLCEIL)
1806 CASE_MATHFN (BUILT_IN_LLFLOOR)
1807 CASE_MATHFN (BUILT_IN_LLRINT)
1808 CASE_MATHFN (BUILT_IN_LLROUND)
1809 CASE_MATHFN (BUILT_IN_LOG)
1810 CASE_MATHFN (BUILT_IN_LOG10)
1811 CASE_MATHFN (BUILT_IN_LOG1P)
1812 CASE_MATHFN (BUILT_IN_LOG2)
1813 CASE_MATHFN (BUILT_IN_LOGB)
1814 CASE_MATHFN (BUILT_IN_LRINT)
1815 CASE_MATHFN (BUILT_IN_LROUND)
1816 CASE_MATHFN (BUILT_IN_MODF)
1817 CASE_MATHFN (BUILT_IN_NAN)
1818 CASE_MATHFN (BUILT_IN_NANS)
1819 CASE_MATHFN (BUILT_IN_NEARBYINT)
1820 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1821 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1822 CASE_MATHFN (BUILT_IN_POW)
1823 CASE_MATHFN (BUILT_IN_POWI)
1824 CASE_MATHFN (BUILT_IN_POW10)
1825 CASE_MATHFN (BUILT_IN_REMAINDER)
1826 CASE_MATHFN (BUILT_IN_REMQUO)
1827 CASE_MATHFN (BUILT_IN_RINT)
1828 CASE_MATHFN (BUILT_IN_ROUND)
1829 CASE_MATHFN (BUILT_IN_SCALB)
1830 CASE_MATHFN (BUILT_IN_SCALBLN)
1831 CASE_MATHFN (BUILT_IN_SCALBN)
1832 CASE_MATHFN (BUILT_IN_SIGNBIT)
1833 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1834 CASE_MATHFN (BUILT_IN_SIN)
1835 CASE_MATHFN (BUILT_IN_SINCOS)
1836 CASE_MATHFN (BUILT_IN_SINH)
1837 CASE_MATHFN (BUILT_IN_SQRT)
1838 CASE_MATHFN (BUILT_IN_TAN)
1839 CASE_MATHFN (BUILT_IN_TANH)
1840 CASE_MATHFN (BUILT_IN_TGAMMA)
1841 CASE_MATHFN (BUILT_IN_TRUNC)
1842 CASE_MATHFN (BUILT_IN_Y0)
1843 CASE_MATHFN (BUILT_IN_Y1)
1844 CASE_MATHFN (BUILT_IN_YN)
1846 default:
1847 return NULL_TREE;
1850 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1851 return fn_arr[fcode];
1852 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1853 return fn_arr[fcodef];
1854 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1855 return fn_arr[fcodel];
1856 else
1857 return NULL_TREE;
1860 /* Like mathfn_built_in_1(), but always use the implicit array. */
1862 tree
1863 mathfn_built_in (tree type, enum built_in_function fn)
1865 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1868 /* If errno must be maintained, expand the RTL to check if the result,
1869 TARGET, of a built-in function call, EXP, is NaN, and if so set
1870 errno to EDOM. */
1872 static void
1873 expand_errno_check (tree exp, rtx target)
1875 rtx lab = gen_label_rtx ();
1877 /* Test the result; if it is NaN, set errno=EDOM because
1878 the argument was not in the domain. */
1879 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1880 NULL_RTX, NULL_RTX, lab,
1881 /* The jump is very likely. */
1882 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1884 #ifdef TARGET_EDOM
1885 /* If this built-in doesn't throw an exception, set errno directly. */
1886 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1888 #ifdef GEN_ERRNO_RTX
1889 rtx errno_rtx = GEN_ERRNO_RTX;
1890 #else
1891 rtx errno_rtx
1892 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1893 #endif
1894 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1895 emit_label (lab);
1896 return;
1898 #endif
1900 /* Make sure the library call isn't expanded as a tail call. */
1901 CALL_EXPR_TAILCALL (exp) = 0;
1903 /* We can't set errno=EDOM directly; let the library call do it.
1904 Pop the arguments right away in case the call gets deleted. */
1905 NO_DEFER_POP;
1906 expand_call (exp, target, 0);
1907 OK_DEFER_POP;
1908 emit_label (lab);
1911 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1912 Return NULL_RTX if a normal call should be emitted rather than expanding
1913 the function in-line. EXP is the expression that is a call to the builtin
1914 function; if convenient, the result should be placed in TARGET.
1915 SUBTARGET may be used as the target for computing one of EXP's operands. */
1917 static rtx
1918 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1920 optab builtin_optab;
1921 rtx op0, insns;
1922 tree fndecl = get_callee_fndecl (exp);
1923 enum machine_mode mode;
1924 bool errno_set = false;
1925 tree arg;
1927 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1928 return NULL_RTX;
1930 arg = CALL_EXPR_ARG (exp, 0);
1932 switch (DECL_FUNCTION_CODE (fndecl))
1934 CASE_FLT_FN (BUILT_IN_SQRT):
1935 errno_set = ! tree_expr_nonnegative_p (arg);
1936 builtin_optab = sqrt_optab;
1937 break;
1938 CASE_FLT_FN (BUILT_IN_EXP):
1939 errno_set = true; builtin_optab = exp_optab; break;
1940 CASE_FLT_FN (BUILT_IN_EXP10):
1941 CASE_FLT_FN (BUILT_IN_POW10):
1942 errno_set = true; builtin_optab = exp10_optab; break;
1943 CASE_FLT_FN (BUILT_IN_EXP2):
1944 errno_set = true; builtin_optab = exp2_optab; break;
1945 CASE_FLT_FN (BUILT_IN_EXPM1):
1946 errno_set = true; builtin_optab = expm1_optab; break;
1947 CASE_FLT_FN (BUILT_IN_LOGB):
1948 errno_set = true; builtin_optab = logb_optab; break;
1949 CASE_FLT_FN (BUILT_IN_LOG):
1950 errno_set = true; builtin_optab = log_optab; break;
1951 CASE_FLT_FN (BUILT_IN_LOG10):
1952 errno_set = true; builtin_optab = log10_optab; break;
1953 CASE_FLT_FN (BUILT_IN_LOG2):
1954 errno_set = true; builtin_optab = log2_optab; break;
1955 CASE_FLT_FN (BUILT_IN_LOG1P):
1956 errno_set = true; builtin_optab = log1p_optab; break;
1957 CASE_FLT_FN (BUILT_IN_ASIN):
1958 builtin_optab = asin_optab; break;
1959 CASE_FLT_FN (BUILT_IN_ACOS):
1960 builtin_optab = acos_optab; break;
1961 CASE_FLT_FN (BUILT_IN_TAN):
1962 builtin_optab = tan_optab; break;
1963 CASE_FLT_FN (BUILT_IN_ATAN):
1964 builtin_optab = atan_optab; break;
1965 CASE_FLT_FN (BUILT_IN_FLOOR):
1966 builtin_optab = floor_optab; break;
1967 CASE_FLT_FN (BUILT_IN_CEIL):
1968 builtin_optab = ceil_optab; break;
1969 CASE_FLT_FN (BUILT_IN_TRUNC):
1970 builtin_optab = btrunc_optab; break;
1971 CASE_FLT_FN (BUILT_IN_ROUND):
1972 builtin_optab = round_optab; break;
1973 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1974 builtin_optab = nearbyint_optab;
1975 if (flag_trapping_math)
1976 break;
1977 /* Else fallthrough and expand as rint. */
1978 CASE_FLT_FN (BUILT_IN_RINT):
1979 builtin_optab = rint_optab; break;
1980 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
1981 builtin_optab = significand_optab; break;
1982 default:
1983 gcc_unreachable ();
1986 /* Make a suitable register to place result in. */
1987 mode = TYPE_MODE (TREE_TYPE (exp));
1989 if (! flag_errno_math || ! HONOR_NANS (mode))
1990 errno_set = false;
1992 /* Before working hard, check whether the instruction is available. */
1993 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
1995 target = gen_reg_rtx (mode);
1997 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1998 need to expand the argument again. This way, we will not perform
1999 side-effects more the once. */
2000 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2002 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2004 start_sequence ();
2006 /* Compute into TARGET.
2007 Set TARGET to wherever the result comes back. */
2008 target = expand_unop (mode, builtin_optab, op0, target, 0);
2010 if (target != 0)
2012 if (errno_set)
2013 expand_errno_check (exp, target);
2015 /* Output the entire sequence. */
2016 insns = get_insns ();
2017 end_sequence ();
2018 emit_insn (insns);
2019 return target;
2022 /* If we were unable to expand via the builtin, stop the sequence
2023 (without outputting the insns) and call to the library function
2024 with the stabilized argument list. */
2025 end_sequence ();
2028 return expand_call (exp, target, target == const0_rtx);
2031 /* Expand a call to the builtin binary math functions (pow and atan2).
2032 Return NULL_RTX if a normal call should be emitted rather than expanding the
2033 function in-line. EXP is the expression that is a call to the builtin
2034 function; if convenient, the result should be placed in TARGET.
2035 SUBTARGET may be used as the target for computing one of EXP's
2036 operands. */
2038 static rtx
2039 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2041 optab builtin_optab;
2042 rtx op0, op1, insns;
2043 int op1_type = REAL_TYPE;
2044 tree fndecl = get_callee_fndecl (exp);
2045 tree arg0, arg1;
2046 enum machine_mode mode;
2047 bool errno_set = true;
2049 switch (DECL_FUNCTION_CODE (fndecl))
2051 CASE_FLT_FN (BUILT_IN_SCALBN):
2052 CASE_FLT_FN (BUILT_IN_SCALBLN):
2053 CASE_FLT_FN (BUILT_IN_LDEXP):
2054 op1_type = INTEGER_TYPE;
2055 default:
2056 break;
2059 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2060 return NULL_RTX;
2062 arg0 = CALL_EXPR_ARG (exp, 0);
2063 arg1 = CALL_EXPR_ARG (exp, 1);
2065 switch (DECL_FUNCTION_CODE (fndecl))
2067 CASE_FLT_FN (BUILT_IN_POW):
2068 builtin_optab = pow_optab; break;
2069 CASE_FLT_FN (BUILT_IN_ATAN2):
2070 builtin_optab = atan2_optab; break;
2071 CASE_FLT_FN (BUILT_IN_SCALB):
2072 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2073 return 0;
2074 builtin_optab = scalb_optab; break;
2075 CASE_FLT_FN (BUILT_IN_SCALBN):
2076 CASE_FLT_FN (BUILT_IN_SCALBLN):
2077 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2078 return 0;
2079 /* Fall through... */
2080 CASE_FLT_FN (BUILT_IN_LDEXP):
2081 builtin_optab = ldexp_optab; break;
2082 CASE_FLT_FN (BUILT_IN_FMOD):
2083 builtin_optab = fmod_optab; break;
2084 CASE_FLT_FN (BUILT_IN_REMAINDER):
2085 CASE_FLT_FN (BUILT_IN_DREM):
2086 builtin_optab = remainder_optab; break;
2087 default:
2088 gcc_unreachable ();
2091 /* Make a suitable register to place result in. */
2092 mode = TYPE_MODE (TREE_TYPE (exp));
2094 /* Before working hard, check whether the instruction is available. */
2095 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2096 return NULL_RTX;
2098 target = gen_reg_rtx (mode);
2100 if (! flag_errno_math || ! HONOR_NANS (mode))
2101 errno_set = false;
2103 /* Always stabilize the argument list. */
2104 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2105 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2107 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2108 op1 = expand_normal (arg1);
2110 start_sequence ();
2112 /* Compute into TARGET.
2113 Set TARGET to wherever the result comes back. */
2114 target = expand_binop (mode, builtin_optab, op0, op1,
2115 target, 0, OPTAB_DIRECT);
2117 /* If we were unable to expand via the builtin, stop the sequence
2118 (without outputting the insns) and call to the library function
2119 with the stabilized argument list. */
2120 if (target == 0)
2122 end_sequence ();
2123 return expand_call (exp, target, target == const0_rtx);
2126 if (errno_set)
2127 expand_errno_check (exp, target);
2129 /* Output the entire sequence. */
2130 insns = get_insns ();
2131 end_sequence ();
2132 emit_insn (insns);
2134 return target;
2137 /* Expand a call to the builtin sin and cos math functions.
2138 Return NULL_RTX if a normal call should be emitted rather than expanding the
2139 function in-line. EXP is the expression that is a call to the builtin
2140 function; if convenient, the result should be placed in TARGET.
2141 SUBTARGET may be used as the target for computing one of EXP's
2142 operands. */
2144 static rtx
2145 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2147 optab builtin_optab;
2148 rtx op0, insns;
2149 tree fndecl = get_callee_fndecl (exp);
2150 enum machine_mode mode;
2151 tree arg;
2153 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2154 return NULL_RTX;
2156 arg = CALL_EXPR_ARG (exp, 0);
2158 switch (DECL_FUNCTION_CODE (fndecl))
2160 CASE_FLT_FN (BUILT_IN_SIN):
2161 CASE_FLT_FN (BUILT_IN_COS):
2162 builtin_optab = sincos_optab; break;
2163 default:
2164 gcc_unreachable ();
2167 /* Make a suitable register to place result in. */
2168 mode = TYPE_MODE (TREE_TYPE (exp));
2170 /* Check if sincos insn is available, otherwise fallback
2171 to sin or cos insn. */
2172 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2173 switch (DECL_FUNCTION_CODE (fndecl))
2175 CASE_FLT_FN (BUILT_IN_SIN):
2176 builtin_optab = sin_optab; break;
2177 CASE_FLT_FN (BUILT_IN_COS):
2178 builtin_optab = cos_optab; break;
2179 default:
2180 gcc_unreachable ();
2183 /* Before working hard, check whether the instruction is available. */
2184 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2186 target = gen_reg_rtx (mode);
2188 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2189 need to expand the argument again. This way, we will not perform
2190 side-effects more the once. */
2191 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2193 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2195 start_sequence ();
2197 /* Compute into TARGET.
2198 Set TARGET to wherever the result comes back. */
2199 if (builtin_optab == sincos_optab)
2201 int result;
2203 switch (DECL_FUNCTION_CODE (fndecl))
2205 CASE_FLT_FN (BUILT_IN_SIN):
2206 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2207 break;
2208 CASE_FLT_FN (BUILT_IN_COS):
2209 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2210 break;
2211 default:
2212 gcc_unreachable ();
2214 gcc_assert (result);
2216 else
2218 target = expand_unop (mode, builtin_optab, op0, target, 0);
2221 if (target != 0)
2223 /* Output the entire sequence. */
2224 insns = get_insns ();
2225 end_sequence ();
2226 emit_insn (insns);
2227 return target;
2230 /* If we were unable to expand via the builtin, stop the sequence
2231 (without outputting the insns) and call to the library function
2232 with the stabilized argument list. */
2233 end_sequence ();
2236 target = expand_call (exp, target, target == const0_rtx);
2238 return target;
2241 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2242 return an RTL instruction code that implements the functionality.
2243 If that isn't possible or available return CODE_FOR_nothing. */
2245 static enum insn_code
2246 interclass_mathfn_icode (tree arg, tree fndecl)
2248 bool errno_set = false;
2249 optab builtin_optab = 0;
2250 enum machine_mode mode;
2252 switch (DECL_FUNCTION_CODE (fndecl))
2254 CASE_FLT_FN (BUILT_IN_ILOGB):
2255 errno_set = true; builtin_optab = ilogb_optab; break;
2256 CASE_FLT_FN (BUILT_IN_ISINF):
2257 builtin_optab = isinf_optab; break;
2258 case BUILT_IN_ISNORMAL:
2259 case BUILT_IN_ISFINITE:
2260 CASE_FLT_FN (BUILT_IN_FINITE):
2261 case BUILT_IN_FINITED32:
2262 case BUILT_IN_FINITED64:
2263 case BUILT_IN_FINITED128:
2264 case BUILT_IN_ISINFD32:
2265 case BUILT_IN_ISINFD64:
2266 case BUILT_IN_ISINFD128:
2267 /* These builtins have no optabs (yet). */
2268 break;
2269 default:
2270 gcc_unreachable ();
2273 /* There's no easy way to detect the case we need to set EDOM. */
2274 if (flag_errno_math && errno_set)
2275 return CODE_FOR_nothing;
2277 /* Optab mode depends on the mode of the input argument. */
2278 mode = TYPE_MODE (TREE_TYPE (arg));
2280 if (builtin_optab)
2281 return optab_handler (builtin_optab, mode);
2282 return CODE_FOR_nothing;
2285 /* Expand a call to one of the builtin math functions that operate on
2286 floating point argument and output an integer result (ilogb, isinf,
2287 isnan, etc).
2288 Return 0 if a normal call should be emitted rather than expanding the
2289 function in-line. EXP is the expression that is a call to the builtin
2290 function; if convenient, the result should be placed in TARGET.
2291 SUBTARGET may be used as the target for computing one of EXP's operands. */
2293 static rtx
2294 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2296 enum insn_code icode = CODE_FOR_nothing;
2297 rtx op0;
2298 tree fndecl = get_callee_fndecl (exp);
2299 enum machine_mode mode;
2300 tree arg;
2302 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2303 return NULL_RTX;
2305 arg = CALL_EXPR_ARG (exp, 0);
2306 icode = interclass_mathfn_icode (arg, fndecl);
2307 mode = TYPE_MODE (TREE_TYPE (arg));
2309 if (icode != CODE_FOR_nothing)
2311 rtx last = get_last_insn ();
2312 tree orig_arg = arg;
2313 /* Make a suitable register to place result in. */
2314 if (!target
2315 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp))
2316 || !insn_data[icode].operand[0].predicate (target, GET_MODE (target)))
2317 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2319 gcc_assert (insn_data[icode].operand[0].predicate
2320 (target, GET_MODE (target)));
2322 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2323 need to expand the argument again. This way, we will not perform
2324 side-effects more the once. */
2325 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2327 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2329 if (mode != GET_MODE (op0))
2330 op0 = convert_to_mode (mode, op0, 0);
2332 /* Compute into TARGET.
2333 Set TARGET to wherever the result comes back. */
2334 if (maybe_emit_unop_insn (icode, target, op0, UNKNOWN))
2335 return target;
2336 delete_insns_since (last);
2337 CALL_EXPR_ARG (exp, 0) = orig_arg;
2340 return NULL_RTX;
2343 /* Expand a call to the builtin sincos math function.
2344 Return NULL_RTX if a normal call should be emitted rather than expanding the
2345 function in-line. EXP is the expression that is a call to the builtin
2346 function. */
2348 static rtx
2349 expand_builtin_sincos (tree exp)
2351 rtx op0, op1, op2, target1, target2;
2352 enum machine_mode mode;
2353 tree arg, sinp, cosp;
2354 int result;
2355 location_t loc = EXPR_LOCATION (exp);
2357 if (!validate_arglist (exp, REAL_TYPE,
2358 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2359 return NULL_RTX;
2361 arg = CALL_EXPR_ARG (exp, 0);
2362 sinp = CALL_EXPR_ARG (exp, 1);
2363 cosp = CALL_EXPR_ARG (exp, 2);
2365 /* Make a suitable register to place result in. */
2366 mode = TYPE_MODE (TREE_TYPE (arg));
2368 /* Check if sincos insn is available, otherwise emit the call. */
2369 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2370 return NULL_RTX;
2372 target1 = gen_reg_rtx (mode);
2373 target2 = gen_reg_rtx (mode);
2375 op0 = expand_normal (arg);
2376 op1 = expand_normal (build_fold_indirect_ref_loc (loc, sinp));
2377 op2 = expand_normal (build_fold_indirect_ref_loc (loc, cosp));
2379 /* Compute into target1 and target2.
2380 Set TARGET to wherever the result comes back. */
2381 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2382 gcc_assert (result);
2384 /* Move target1 and target2 to the memory locations indicated
2385 by op1 and op2. */
2386 emit_move_insn (op1, target1);
2387 emit_move_insn (op2, target2);
2389 return const0_rtx;
2392 /* Expand a call to the internal cexpi builtin to the sincos math function.
2393 EXP is the expression that is a call to the builtin function; if convenient,
2394 the result should be placed in TARGET. SUBTARGET may be used as the target
2395 for computing one of EXP's operands. */
2397 static rtx
2398 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2400 tree fndecl = get_callee_fndecl (exp);
2401 tree arg, type;
2402 enum machine_mode mode;
2403 rtx op0, op1, op2;
2404 location_t loc = EXPR_LOCATION (exp);
2406 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2407 return NULL_RTX;
2409 arg = CALL_EXPR_ARG (exp, 0);
2410 type = TREE_TYPE (arg);
2411 mode = TYPE_MODE (TREE_TYPE (arg));
2413 /* Try expanding via a sincos optab, fall back to emitting a libcall
2414 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2415 is only generated from sincos, cexp or if we have either of them. */
2416 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2418 op1 = gen_reg_rtx (mode);
2419 op2 = gen_reg_rtx (mode);
2421 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2423 /* Compute into op1 and op2. */
2424 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2426 else if (TARGET_HAS_SINCOS)
2428 tree call, fn = NULL_TREE;
2429 tree top1, top2;
2430 rtx op1a, op2a;
2432 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2433 fn = built_in_decls[BUILT_IN_SINCOSF];
2434 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2435 fn = built_in_decls[BUILT_IN_SINCOS];
2436 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2437 fn = built_in_decls[BUILT_IN_SINCOSL];
2438 else
2439 gcc_unreachable ();
2441 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2442 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2443 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2444 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2445 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2446 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2448 /* Make sure not to fold the sincos call again. */
2449 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2450 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2451 call, 3, arg, top1, top2));
2453 else
2455 tree call, fn = NULL_TREE, narg;
2456 tree ctype = build_complex_type (type);
2458 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2459 fn = built_in_decls[BUILT_IN_CEXPF];
2460 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2461 fn = built_in_decls[BUILT_IN_CEXP];
2462 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2463 fn = built_in_decls[BUILT_IN_CEXPL];
2464 else
2465 gcc_unreachable ();
2467 /* If we don't have a decl for cexp create one. This is the
2468 friendliest fallback if the user calls __builtin_cexpi
2469 without full target C99 function support. */
2470 if (fn == NULL_TREE)
2472 tree fntype;
2473 const char *name = NULL;
2475 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2476 name = "cexpf";
2477 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2478 name = "cexp";
2479 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2480 name = "cexpl";
2482 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2483 fn = build_fn_decl (name, fntype);
2486 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2487 build_real (type, dconst0), arg);
2489 /* Make sure not to fold the cexp call again. */
2490 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2491 return expand_expr (build_call_nary (ctype, call, 1, narg),
2492 target, VOIDmode, EXPAND_NORMAL);
2495 /* Now build the proper return type. */
2496 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2497 make_tree (TREE_TYPE (arg), op2),
2498 make_tree (TREE_TYPE (arg), op1)),
2499 target, VOIDmode, EXPAND_NORMAL);
2502 /* Conveniently construct a function call expression. FNDECL names the
2503 function to be called, N is the number of arguments, and the "..."
2504 parameters are the argument expressions. Unlike build_call_exr
2505 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2507 static tree
2508 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2510 va_list ap;
2511 tree fntype = TREE_TYPE (fndecl);
2512 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2514 va_start (ap, n);
2515 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2516 va_end (ap);
2517 SET_EXPR_LOCATION (fn, loc);
2518 return fn;
2521 /* Expand a call to one of the builtin rounding functions gcc defines
2522 as an extension (lfloor and lceil). As these are gcc extensions we
2523 do not need to worry about setting errno to EDOM.
2524 If expanding via optab fails, lower expression to (int)(floor(x)).
2525 EXP is the expression that is a call to the builtin function;
2526 if convenient, the result should be placed in TARGET. */
2528 static rtx
2529 expand_builtin_int_roundingfn (tree exp, rtx target)
2531 convert_optab builtin_optab;
2532 rtx op0, insns, tmp;
2533 tree fndecl = get_callee_fndecl (exp);
2534 enum built_in_function fallback_fn;
2535 tree fallback_fndecl;
2536 enum machine_mode mode;
2537 tree arg;
2539 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2540 gcc_unreachable ();
2542 arg = CALL_EXPR_ARG (exp, 0);
2544 switch (DECL_FUNCTION_CODE (fndecl))
2546 CASE_FLT_FN (BUILT_IN_LCEIL):
2547 CASE_FLT_FN (BUILT_IN_LLCEIL):
2548 builtin_optab = lceil_optab;
2549 fallback_fn = BUILT_IN_CEIL;
2550 break;
2552 CASE_FLT_FN (BUILT_IN_LFLOOR):
2553 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2554 builtin_optab = lfloor_optab;
2555 fallback_fn = BUILT_IN_FLOOR;
2556 break;
2558 default:
2559 gcc_unreachable ();
2562 /* Make a suitable register to place result in. */
2563 mode = TYPE_MODE (TREE_TYPE (exp));
2565 target = gen_reg_rtx (mode);
2567 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2568 need to expand the argument again. This way, we will not perform
2569 side-effects more the once. */
2570 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2572 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2574 start_sequence ();
2576 /* Compute into TARGET. */
2577 if (expand_sfix_optab (target, op0, builtin_optab))
2579 /* Output the entire sequence. */
2580 insns = get_insns ();
2581 end_sequence ();
2582 emit_insn (insns);
2583 return target;
2586 /* If we were unable to expand via the builtin, stop the sequence
2587 (without outputting the insns). */
2588 end_sequence ();
2590 /* Fall back to floating point rounding optab. */
2591 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2593 /* For non-C99 targets we may end up without a fallback fndecl here
2594 if the user called __builtin_lfloor directly. In this case emit
2595 a call to the floor/ceil variants nevertheless. This should result
2596 in the best user experience for not full C99 targets. */
2597 if (fallback_fndecl == NULL_TREE)
2599 tree fntype;
2600 const char *name = NULL;
2602 switch (DECL_FUNCTION_CODE (fndecl))
2604 case BUILT_IN_LCEIL:
2605 case BUILT_IN_LLCEIL:
2606 name = "ceil";
2607 break;
2608 case BUILT_IN_LCEILF:
2609 case BUILT_IN_LLCEILF:
2610 name = "ceilf";
2611 break;
2612 case BUILT_IN_LCEILL:
2613 case BUILT_IN_LLCEILL:
2614 name = "ceill";
2615 break;
2616 case BUILT_IN_LFLOOR:
2617 case BUILT_IN_LLFLOOR:
2618 name = "floor";
2619 break;
2620 case BUILT_IN_LFLOORF:
2621 case BUILT_IN_LLFLOORF:
2622 name = "floorf";
2623 break;
2624 case BUILT_IN_LFLOORL:
2625 case BUILT_IN_LLFLOORL:
2626 name = "floorl";
2627 break;
2628 default:
2629 gcc_unreachable ();
2632 fntype = build_function_type_list (TREE_TYPE (arg),
2633 TREE_TYPE (arg), NULL_TREE);
2634 fallback_fndecl = build_fn_decl (name, fntype);
2637 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2639 tmp = expand_normal (exp);
2641 /* Truncate the result of floating point optab to integer
2642 via expand_fix (). */
2643 target = gen_reg_rtx (mode);
2644 expand_fix (target, tmp, 0);
2646 return target;
2649 /* Expand a call to one of the builtin math functions doing integer
2650 conversion (lrint).
2651 Return 0 if a normal call should be emitted rather than expanding the
2652 function in-line. EXP is the expression that is a call to the builtin
2653 function; if convenient, the result should be placed in TARGET. */
2655 static rtx
2656 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2658 convert_optab builtin_optab;
2659 rtx op0, insns;
2660 tree fndecl = get_callee_fndecl (exp);
2661 tree arg;
2662 enum machine_mode mode;
2664 /* There's no easy way to detect the case we need to set EDOM. */
2665 if (flag_errno_math)
2666 return NULL_RTX;
2668 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2669 gcc_unreachable ();
2671 arg = CALL_EXPR_ARG (exp, 0);
2673 switch (DECL_FUNCTION_CODE (fndecl))
2675 CASE_FLT_FN (BUILT_IN_LRINT):
2676 CASE_FLT_FN (BUILT_IN_LLRINT):
2677 builtin_optab = lrint_optab; break;
2678 CASE_FLT_FN (BUILT_IN_LROUND):
2679 CASE_FLT_FN (BUILT_IN_LLROUND):
2680 builtin_optab = lround_optab; break;
2681 default:
2682 gcc_unreachable ();
2685 /* Make a suitable register to place result in. */
2686 mode = TYPE_MODE (TREE_TYPE (exp));
2688 target = gen_reg_rtx (mode);
2690 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2691 need to expand the argument again. This way, we will not perform
2692 side-effects more the once. */
2693 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2695 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2697 start_sequence ();
2699 if (expand_sfix_optab (target, op0, builtin_optab))
2701 /* Output the entire sequence. */
2702 insns = get_insns ();
2703 end_sequence ();
2704 emit_insn (insns);
2705 return target;
2708 /* If we were unable to expand via the builtin, stop the sequence
2709 (without outputting the insns) and call to the library function
2710 with the stabilized argument list. */
2711 end_sequence ();
2713 target = expand_call (exp, target, target == const0_rtx);
2715 return target;
2718 /* To evaluate powi(x,n), the floating point value x raised to the
2719 constant integer exponent n, we use a hybrid algorithm that
2720 combines the "window method" with look-up tables. For an
2721 introduction to exponentiation algorithms and "addition chains",
2722 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2723 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2724 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2725 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2727 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2728 multiplications to inline before calling the system library's pow
2729 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2730 so this default never requires calling pow, powf or powl. */
2732 #ifndef POWI_MAX_MULTS
2733 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2734 #endif
2736 /* The size of the "optimal power tree" lookup table. All
2737 exponents less than this value are simply looked up in the
2738 powi_table below. This threshold is also used to size the
2739 cache of pseudo registers that hold intermediate results. */
2740 #define POWI_TABLE_SIZE 256
2742 /* The size, in bits of the window, used in the "window method"
2743 exponentiation algorithm. This is equivalent to a radix of
2744 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2745 #define POWI_WINDOW_SIZE 3
2747 /* The following table is an efficient representation of an
2748 "optimal power tree". For each value, i, the corresponding
2749 value, j, in the table states than an optimal evaluation
2750 sequence for calculating pow(x,i) can be found by evaluating
2751 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2752 100 integers is given in Knuth's "Seminumerical algorithms". */
2754 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2756 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2757 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2758 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2759 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2760 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2761 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2762 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2763 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2764 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2765 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2766 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2767 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2768 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2769 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2770 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2771 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2772 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2773 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2774 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2775 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2776 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2777 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2778 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2779 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2780 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2781 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2782 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2783 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2784 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2785 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2786 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2787 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2791 /* Return the number of multiplications required to calculate
2792 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2793 subroutine of powi_cost. CACHE is an array indicating
2794 which exponents have already been calculated. */
2796 static int
2797 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2799 /* If we've already calculated this exponent, then this evaluation
2800 doesn't require any additional multiplications. */
2801 if (cache[n])
2802 return 0;
2804 cache[n] = true;
2805 return powi_lookup_cost (n - powi_table[n], cache)
2806 + powi_lookup_cost (powi_table[n], cache) + 1;
2809 /* Return the number of multiplications required to calculate
2810 powi(x,n) for an arbitrary x, given the exponent N. This
2811 function needs to be kept in sync with expand_powi below. */
2813 static int
2814 powi_cost (HOST_WIDE_INT n)
2816 bool cache[POWI_TABLE_SIZE];
2817 unsigned HOST_WIDE_INT digit;
2818 unsigned HOST_WIDE_INT val;
2819 int result;
2821 if (n == 0)
2822 return 0;
2824 /* Ignore the reciprocal when calculating the cost. */
2825 val = (n < 0) ? -n : n;
2827 /* Initialize the exponent cache. */
2828 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2829 cache[1] = true;
2831 result = 0;
2833 while (val >= POWI_TABLE_SIZE)
2835 if (val & 1)
2837 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2838 result += powi_lookup_cost (digit, cache)
2839 + POWI_WINDOW_SIZE + 1;
2840 val >>= POWI_WINDOW_SIZE;
2842 else
2844 val >>= 1;
2845 result++;
2849 return result + powi_lookup_cost (val, cache);
2852 /* Recursive subroutine of expand_powi. This function takes the array,
2853 CACHE, of already calculated exponents and an exponent N and returns
2854 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2856 static rtx
2857 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2859 unsigned HOST_WIDE_INT digit;
2860 rtx target, result;
2861 rtx op0, op1;
2863 if (n < POWI_TABLE_SIZE)
2865 if (cache[n])
2866 return cache[n];
2868 target = gen_reg_rtx (mode);
2869 cache[n] = target;
2871 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2872 op1 = expand_powi_1 (mode, powi_table[n], cache);
2874 else if (n & 1)
2876 target = gen_reg_rtx (mode);
2877 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2878 op0 = expand_powi_1 (mode, n - digit, cache);
2879 op1 = expand_powi_1 (mode, digit, cache);
2881 else
2883 target = gen_reg_rtx (mode);
2884 op0 = expand_powi_1 (mode, n >> 1, cache);
2885 op1 = op0;
2888 result = expand_mult (mode, op0, op1, target, 0);
2889 if (result != target)
2890 emit_move_insn (target, result);
2891 return target;
2894 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2895 floating point operand in mode MODE, and N is the exponent. This
2896 function needs to be kept in sync with powi_cost above. */
2898 static rtx
2899 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2901 rtx cache[POWI_TABLE_SIZE];
2902 rtx result;
2904 if (n == 0)
2905 return CONST1_RTX (mode);
2907 memset (cache, 0, sizeof (cache));
2908 cache[1] = x;
2910 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2912 /* If the original exponent was negative, reciprocate the result. */
2913 if (n < 0)
2914 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2915 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2917 return result;
2920 /* Fold a builtin function call to pow, powf, or powl into a series of sqrts or
2921 cbrts. Return NULL_RTX if no simplification can be made or expand the tree
2922 if we can simplify it. */
2923 static rtx
2924 expand_builtin_pow_root (location_t loc, tree arg0, tree arg1, tree type,
2925 rtx subtarget)
2927 if (TREE_CODE (arg1) == REAL_CST
2928 && !TREE_OVERFLOW (arg1)
2929 && flag_unsafe_math_optimizations)
2931 enum machine_mode mode = TYPE_MODE (type);
2932 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
2933 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
2934 REAL_VALUE_TYPE c = TREE_REAL_CST (arg1);
2935 tree op = NULL_TREE;
2937 if (sqrtfn)
2939 /* Optimize pow (x, 0.5) into sqrt. */
2940 if (REAL_VALUES_EQUAL (c, dconsthalf))
2941 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
2943 else
2945 REAL_VALUE_TYPE dconst1_4 = dconst1;
2946 REAL_VALUE_TYPE dconst3_4;
2947 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
2949 real_from_integer (&dconst3_4, VOIDmode, 3, 0, 0);
2950 SET_REAL_EXP (&dconst3_4, REAL_EXP (&dconst3_4) - 2);
2952 /* Optimize pow (x, 0.25) into sqrt (sqrt (x)). Assume on most
2953 machines that a builtin sqrt instruction is smaller than a
2954 call to pow with 0.25, so do this optimization even if
2955 -Os. */
2956 if (REAL_VALUES_EQUAL (c, dconst1_4))
2958 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
2959 op = build_call_nofold_loc (loc, sqrtfn, 1, op);
2962 /* Optimize pow (x, 0.75) = sqrt (x) * sqrt (sqrt (x)) unless we
2963 are optimizing for space. */
2964 else if (optimize_insn_for_speed_p ()
2965 && !TREE_SIDE_EFFECTS (arg0)
2966 && REAL_VALUES_EQUAL (c, dconst3_4))
2968 tree sqrt1 = build_call_expr_loc (loc, sqrtfn, 1, arg0);
2969 tree sqrt2 = builtin_save_expr (sqrt1);
2970 tree sqrt3 = build_call_expr_loc (loc, sqrtfn, 1, sqrt1);
2971 op = fold_build2_loc (loc, MULT_EXPR, type, sqrt2, sqrt3);
2976 /* Check whether we can do cbrt insstead of pow (x, 1./3.) and
2977 cbrt/sqrts instead of pow (x, 1./6.). */
2978 if (cbrtfn && ! op
2979 && (tree_expr_nonnegative_p (arg0) || !HONOR_NANS (mode)))
2981 /* First try 1/3. */
2982 REAL_VALUE_TYPE dconst1_3
2983 = real_value_truncate (mode, dconst_third ());
2985 if (REAL_VALUES_EQUAL (c, dconst1_3))
2986 op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
2988 /* Now try 1/6. */
2989 else if (optimize_insn_for_speed_p ())
2991 REAL_VALUE_TYPE dconst1_6 = dconst1_3;
2992 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
2994 if (REAL_VALUES_EQUAL (c, dconst1_6))
2996 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
2997 op = build_call_nofold_loc (loc, cbrtfn, 1, op);
3002 if (op)
3003 return expand_expr (op, subtarget, mode, EXPAND_NORMAL);
3006 return NULL_RTX;
3009 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
3010 a normal call should be emitted rather than expanding the function
3011 in-line. EXP is the expression that is a call to the builtin
3012 function; if convenient, the result should be placed in TARGET. */
3014 static rtx
3015 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
3017 tree arg0, arg1;
3018 tree fn, narg0;
3019 tree type = TREE_TYPE (exp);
3020 REAL_VALUE_TYPE cint, c, c2;
3021 HOST_WIDE_INT n;
3022 rtx op, op2;
3023 enum machine_mode mode = TYPE_MODE (type);
3025 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
3026 return NULL_RTX;
3028 arg0 = CALL_EXPR_ARG (exp, 0);
3029 arg1 = CALL_EXPR_ARG (exp, 1);
3031 if (TREE_CODE (arg1) != REAL_CST
3032 || TREE_OVERFLOW (arg1))
3033 return expand_builtin_mathfn_2 (exp, target, subtarget);
3035 /* Handle constant exponents. */
3037 /* For integer valued exponents we can expand to an optimal multiplication
3038 sequence using expand_powi. */
3039 c = TREE_REAL_CST (arg1);
3040 n = real_to_integer (&c);
3041 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3042 if (real_identical (&c, &cint)
3043 && ((n >= -1 && n <= 2)
3044 || (flag_unsafe_math_optimizations
3045 && optimize_insn_for_speed_p ()
3046 && powi_cost (n) <= POWI_MAX_MULTS)))
3048 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3049 if (n != 1)
3051 op = force_reg (mode, op);
3052 op = expand_powi (op, mode, n);
3054 return op;
3057 narg0 = builtin_save_expr (arg0);
3059 /* If the exponent is not integer valued, check if it is half of an integer.
3060 In this case we can expand to sqrt (x) * x**(n/2). */
3061 fn = mathfn_built_in (type, BUILT_IN_SQRT);
3062 if (fn != NULL_TREE)
3064 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3065 n = real_to_integer (&c2);
3066 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3067 if (real_identical (&c2, &cint)
3068 && ((flag_unsafe_math_optimizations
3069 && optimize_insn_for_speed_p ()
3070 && powi_cost (n/2) <= POWI_MAX_MULTS)
3071 /* Even the c == 0.5 case cannot be done unconditionally
3072 when we need to preserve signed zeros, as
3073 pow (-0, 0.5) is +0, while sqrt(-0) is -0. */
3074 || (!HONOR_SIGNED_ZEROS (mode) && n == 1)
3075 /* For c == 1.5 we can assume that x * sqrt (x) is always
3076 smaller than pow (x, 1.5) if sqrt will not be expanded
3077 as a call. */
3078 || (n == 3
3079 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)))
3081 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3082 narg0);
3083 /* Use expand_expr in case the newly built call expression
3084 was folded to a non-call. */
3085 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3086 if (n != 1)
3088 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3089 op2 = force_reg (mode, op2);
3090 op2 = expand_powi (op2, mode, abs (n / 2));
3091 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3092 0, OPTAB_LIB_WIDEN);
3093 /* If the original exponent was negative, reciprocate the
3094 result. */
3095 if (n < 0)
3096 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3097 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3099 return op;
3103 /* Check whether we can do a series of sqrt or cbrt's instead of the pow
3104 call. */
3105 op = expand_builtin_pow_root (EXPR_LOCATION (exp), arg0, arg1, type,
3106 subtarget);
3107 if (op)
3108 return op;
3110 /* Try if the exponent is a third of an integer. In this case
3111 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3112 different from pow (x, 1./3.) due to rounding and behavior
3113 with negative x we need to constrain this transformation to
3114 unsafe math and positive x or finite math. */
3115 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3116 if (fn != NULL_TREE
3117 && flag_unsafe_math_optimizations
3118 && (tree_expr_nonnegative_p (arg0)
3119 || !HONOR_NANS (mode)))
3121 REAL_VALUE_TYPE dconst3;
3122 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3123 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3124 real_round (&c2, mode, &c2);
3125 n = real_to_integer (&c2);
3126 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3127 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3128 real_convert (&c2, mode, &c2);
3129 if (real_identical (&c2, &c)
3130 && ((optimize_insn_for_speed_p ()
3131 && powi_cost (n/3) <= POWI_MAX_MULTS)
3132 || n == 1))
3134 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3135 narg0);
3136 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3137 if (abs (n) % 3 == 2)
3138 op = expand_simple_binop (mode, MULT, op, op, op,
3139 0, OPTAB_LIB_WIDEN);
3140 if (n != 1)
3142 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3143 op2 = force_reg (mode, op2);
3144 op2 = expand_powi (op2, mode, abs (n / 3));
3145 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3146 0, OPTAB_LIB_WIDEN);
3147 /* If the original exponent was negative, reciprocate the
3148 result. */
3149 if (n < 0)
3150 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3151 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3153 return op;
3157 /* Fall back to optab expansion. */
3158 return expand_builtin_mathfn_2 (exp, target, subtarget);
3161 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3162 a normal call should be emitted rather than expanding the function
3163 in-line. EXP is the expression that is a call to the builtin
3164 function; if convenient, the result should be placed in TARGET. */
3166 static rtx
3167 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3169 tree arg0, arg1;
3170 rtx op0, op1;
3171 enum machine_mode mode;
3172 enum machine_mode mode2;
3174 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3175 return NULL_RTX;
3177 arg0 = CALL_EXPR_ARG (exp, 0);
3178 arg1 = CALL_EXPR_ARG (exp, 1);
3179 mode = TYPE_MODE (TREE_TYPE (exp));
3181 /* Handle constant power. */
3183 if (TREE_CODE (arg1) == INTEGER_CST
3184 && !TREE_OVERFLOW (arg1))
3186 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3188 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3189 Otherwise, check the number of multiplications required. */
3190 if ((TREE_INT_CST_HIGH (arg1) == 0
3191 || TREE_INT_CST_HIGH (arg1) == -1)
3192 && ((n >= -1 && n <= 2)
3193 || (optimize_insn_for_speed_p ()
3194 && powi_cost (n) <= POWI_MAX_MULTS)))
3196 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3197 op0 = force_reg (mode, op0);
3198 return expand_powi (op0, mode, n);
3202 /* Emit a libcall to libgcc. */
3204 /* Mode of the 2nd argument must match that of an int. */
3205 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3207 if (target == NULL_RTX)
3208 target = gen_reg_rtx (mode);
3210 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3211 if (GET_MODE (op0) != mode)
3212 op0 = convert_to_mode (mode, op0, 0);
3213 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3214 if (GET_MODE (op1) != mode2)
3215 op1 = convert_to_mode (mode2, op1, 0);
3217 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3218 target, LCT_CONST, mode, 2,
3219 op0, mode, op1, mode2);
3221 return target;
3224 /* Expand expression EXP which is a call to the strlen builtin. Return
3225 NULL_RTX if we failed the caller should emit a normal call, otherwise
3226 try to get the result in TARGET, if convenient. */
3228 static rtx
3229 expand_builtin_strlen (tree exp, rtx target,
3230 enum machine_mode target_mode)
3232 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3233 return NULL_RTX;
3234 else
3236 rtx pat;
3237 tree len;
3238 tree src = CALL_EXPR_ARG (exp, 0);
3239 rtx result, src_reg, char_rtx, before_strlen;
3240 enum machine_mode insn_mode = target_mode, char_mode;
3241 enum insn_code icode = CODE_FOR_nothing;
3242 int align;
3244 /* If the length can be computed at compile-time, return it. */
3245 len = c_strlen (src, 0);
3246 if (len)
3247 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3249 /* If the length can be computed at compile-time and is constant
3250 integer, but there are side-effects in src, evaluate
3251 src for side-effects, then return len.
3252 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3253 can be optimized into: i++; x = 3; */
3254 len = c_strlen (src, 1);
3255 if (len && TREE_CODE (len) == INTEGER_CST)
3257 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3258 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3261 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3263 /* If SRC is not a pointer type, don't do this operation inline. */
3264 if (align == 0)
3265 return NULL_RTX;
3267 /* Bail out if we can't compute strlen in the right mode. */
3268 while (insn_mode != VOIDmode)
3270 icode = optab_handler (strlen_optab, insn_mode);
3271 if (icode != CODE_FOR_nothing)
3272 break;
3274 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3276 if (insn_mode == VOIDmode)
3277 return NULL_RTX;
3279 /* Make a place to write the result of the instruction. */
3280 result = target;
3281 if (! (result != 0
3282 && REG_P (result)
3283 && GET_MODE (result) == insn_mode
3284 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3285 result = gen_reg_rtx (insn_mode);
3287 /* Make a place to hold the source address. We will not expand
3288 the actual source until we are sure that the expansion will
3289 not fail -- there are trees that cannot be expanded twice. */
3290 src_reg = gen_reg_rtx (Pmode);
3292 /* Mark the beginning of the strlen sequence so we can emit the
3293 source operand later. */
3294 before_strlen = get_last_insn ();
3296 char_rtx = const0_rtx;
3297 char_mode = insn_data[(int) icode].operand[2].mode;
3298 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3299 char_mode))
3300 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3302 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3303 char_rtx, GEN_INT (align));
3304 if (! pat)
3305 return NULL_RTX;
3306 emit_insn (pat);
3308 /* Now that we are assured of success, expand the source. */
3309 start_sequence ();
3310 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3311 if (pat != src_reg)
3312 emit_move_insn (src_reg, pat);
3313 pat = get_insns ();
3314 end_sequence ();
3316 if (before_strlen)
3317 emit_insn_after (pat, before_strlen);
3318 else
3319 emit_insn_before (pat, get_insns ());
3321 /* Return the value in the proper mode for this function. */
3322 if (GET_MODE (result) == target_mode)
3323 target = result;
3324 else if (target != 0)
3325 convert_move (target, result, 0);
3326 else
3327 target = convert_to_mode (target_mode, result, 0);
3329 return target;
3333 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3334 bytes from constant string DATA + OFFSET and return it as target
3335 constant. */
3337 static rtx
3338 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3339 enum machine_mode mode)
3341 const char *str = (const char *) data;
3343 gcc_assert (offset >= 0
3344 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3345 <= strlen (str) + 1));
3347 return c_readstr (str + offset, mode);
3350 /* Expand a call EXP to the memcpy builtin.
3351 Return NULL_RTX if we failed, the caller should emit a normal call,
3352 otherwise try to get the result in TARGET, if convenient (and in
3353 mode MODE if that's convenient). */
3355 static rtx
3356 expand_builtin_memcpy (tree exp, rtx target)
3358 if (!validate_arglist (exp,
3359 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3360 return NULL_RTX;
3361 else
3363 tree dest = CALL_EXPR_ARG (exp, 0);
3364 tree src = CALL_EXPR_ARG (exp, 1);
3365 tree len = CALL_EXPR_ARG (exp, 2);
3366 const char *src_str;
3367 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3368 unsigned int dest_align
3369 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3370 rtx dest_mem, src_mem, dest_addr, len_rtx;
3371 HOST_WIDE_INT expected_size = -1;
3372 unsigned int expected_align = 0;
3374 /* If DEST is not a pointer type, call the normal function. */
3375 if (dest_align == 0)
3376 return NULL_RTX;
3378 /* If either SRC is not a pointer type, don't do this
3379 operation in-line. */
3380 if (src_align == 0)
3381 return NULL_RTX;
3383 if (currently_expanding_gimple_stmt)
3384 stringop_block_profile (currently_expanding_gimple_stmt,
3385 &expected_align, &expected_size);
3387 if (expected_align < dest_align)
3388 expected_align = dest_align;
3389 dest_mem = get_memory_rtx (dest, len);
3390 set_mem_align (dest_mem, dest_align);
3391 len_rtx = expand_normal (len);
3392 src_str = c_getstr (src);
3394 /* If SRC is a string constant and block move would be done
3395 by pieces, we can avoid loading the string from memory
3396 and only stored the computed constants. */
3397 if (src_str
3398 && CONST_INT_P (len_rtx)
3399 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3400 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3401 CONST_CAST (char *, src_str),
3402 dest_align, false))
3404 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3405 builtin_memcpy_read_str,
3406 CONST_CAST (char *, src_str),
3407 dest_align, false, 0);
3408 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3409 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3410 return dest_mem;
3413 src_mem = get_memory_rtx (src, len);
3414 set_mem_align (src_mem, src_align);
3416 /* Copy word part most expediently. */
3417 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3418 CALL_EXPR_TAILCALL (exp)
3419 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3420 expected_align, expected_size);
3422 if (dest_addr == 0)
3424 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3425 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3427 return dest_addr;
3431 /* Expand a call EXP to the mempcpy builtin.
3432 Return NULL_RTX if we failed; the caller should emit a normal call,
3433 otherwise try to get the result in TARGET, if convenient (and in
3434 mode MODE if that's convenient). If ENDP is 0 return the
3435 destination pointer, if ENDP is 1 return the end pointer ala
3436 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3437 stpcpy. */
3439 static rtx
3440 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3442 if (!validate_arglist (exp,
3443 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3444 return NULL_RTX;
3445 else
3447 tree dest = CALL_EXPR_ARG (exp, 0);
3448 tree src = CALL_EXPR_ARG (exp, 1);
3449 tree len = CALL_EXPR_ARG (exp, 2);
3450 return expand_builtin_mempcpy_args (dest, src, len,
3451 target, mode, /*endp=*/ 1);
3455 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3456 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3457 so that this can also be called without constructing an actual CALL_EXPR.
3458 The other arguments and return value are the same as for
3459 expand_builtin_mempcpy. */
3461 static rtx
3462 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3463 rtx target, enum machine_mode mode, int endp)
3465 /* If return value is ignored, transform mempcpy into memcpy. */
3466 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3468 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3469 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3470 dest, src, len);
3471 return expand_expr (result, target, mode, EXPAND_NORMAL);
3473 else
3475 const char *src_str;
3476 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3477 unsigned int dest_align
3478 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3479 rtx dest_mem, src_mem, len_rtx;
3481 /* If either SRC or DEST is not a pointer type, don't do this
3482 operation in-line. */
3483 if (dest_align == 0 || src_align == 0)
3484 return NULL_RTX;
3486 /* If LEN is not constant, call the normal function. */
3487 if (! host_integerp (len, 1))
3488 return NULL_RTX;
3490 len_rtx = expand_normal (len);
3491 src_str = c_getstr (src);
3493 /* If SRC is a string constant and block move would be done
3494 by pieces, we can avoid loading the string from memory
3495 and only stored the computed constants. */
3496 if (src_str
3497 && CONST_INT_P (len_rtx)
3498 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3499 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3500 CONST_CAST (char *, src_str),
3501 dest_align, false))
3503 dest_mem = get_memory_rtx (dest, len);
3504 set_mem_align (dest_mem, dest_align);
3505 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3506 builtin_memcpy_read_str,
3507 CONST_CAST (char *, src_str),
3508 dest_align, false, endp);
3509 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3510 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3511 return dest_mem;
3514 if (CONST_INT_P (len_rtx)
3515 && can_move_by_pieces (INTVAL (len_rtx),
3516 MIN (dest_align, src_align)))
3518 dest_mem = get_memory_rtx (dest, len);
3519 set_mem_align (dest_mem, dest_align);
3520 src_mem = get_memory_rtx (src, len);
3521 set_mem_align (src_mem, src_align);
3522 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3523 MIN (dest_align, src_align), endp);
3524 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3525 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3526 return dest_mem;
3529 return NULL_RTX;
3533 #ifndef HAVE_movstr
3534 # define HAVE_movstr 0
3535 # define CODE_FOR_movstr CODE_FOR_nothing
3536 #endif
3538 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3539 we failed, the caller should emit a normal call, otherwise try to
3540 get the result in TARGET, if convenient. If ENDP is 0 return the
3541 destination pointer, if ENDP is 1 return the end pointer ala
3542 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3543 stpcpy. */
3545 static rtx
3546 expand_movstr (tree dest, tree src, rtx target, int endp)
3548 rtx end;
3549 rtx dest_mem;
3550 rtx src_mem;
3551 rtx insn;
3552 const struct insn_data_d * data;
3554 if (!HAVE_movstr)
3555 return NULL_RTX;
3557 dest_mem = get_memory_rtx (dest, NULL);
3558 src_mem = get_memory_rtx (src, NULL);
3559 data = insn_data + CODE_FOR_movstr;
3560 if (!endp)
3562 target = force_reg (Pmode, XEXP (dest_mem, 0));
3563 dest_mem = replace_equiv_address (dest_mem, target);
3564 end = gen_reg_rtx (Pmode);
3566 else
3568 if (target == 0
3569 || target == const0_rtx
3570 || ! (*data->operand[0].predicate) (target, Pmode))
3572 end = gen_reg_rtx (Pmode);
3573 if (target != const0_rtx)
3574 target = end;
3576 else
3577 end = target;
3580 if (data->operand[0].mode != VOIDmode)
3581 end = gen_lowpart (data->operand[0].mode, end);
3583 insn = data->genfun (end, dest_mem, src_mem);
3585 gcc_assert (insn);
3587 emit_insn (insn);
3589 /* movstr is supposed to set end to the address of the NUL
3590 terminator. If the caller requested a mempcpy-like return value,
3591 adjust it. */
3592 if (endp == 1 && target != const0_rtx)
3594 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3595 emit_move_insn (target, force_operand (tem, NULL_RTX));
3598 return target;
3601 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3602 NULL_RTX if we failed the caller should emit a normal call, otherwise
3603 try to get the result in TARGET, if convenient (and in mode MODE if that's
3604 convenient). */
3606 static rtx
3607 expand_builtin_strcpy (tree exp, rtx target)
3609 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3611 tree dest = CALL_EXPR_ARG (exp, 0);
3612 tree src = CALL_EXPR_ARG (exp, 1);
3613 return expand_builtin_strcpy_args (dest, src, target);
3615 return NULL_RTX;
3618 /* Helper function to do the actual work for expand_builtin_strcpy. The
3619 arguments to the builtin_strcpy call DEST and SRC are broken out
3620 so that this can also be called without constructing an actual CALL_EXPR.
3621 The other arguments and return value are the same as for
3622 expand_builtin_strcpy. */
3624 static rtx
3625 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3627 return expand_movstr (dest, src, target, /*endp=*/0);
3630 /* Expand a call EXP to the stpcpy builtin.
3631 Return NULL_RTX if we failed the caller should emit a normal call,
3632 otherwise try to get the result in TARGET, if convenient (and in
3633 mode MODE if that's convenient). */
3635 static rtx
3636 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3638 tree dst, src;
3639 location_t loc = EXPR_LOCATION (exp);
3641 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3642 return NULL_RTX;
3644 dst = CALL_EXPR_ARG (exp, 0);
3645 src = CALL_EXPR_ARG (exp, 1);
3647 /* If return value is ignored, transform stpcpy into strcpy. */
3648 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3650 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3651 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3652 return expand_expr (result, target, mode, EXPAND_NORMAL);
3654 else
3656 tree len, lenp1;
3657 rtx ret;
3659 /* Ensure we get an actual string whose length can be evaluated at
3660 compile-time, not an expression containing a string. This is
3661 because the latter will potentially produce pessimized code
3662 when used to produce the return value. */
3663 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3664 return expand_movstr (dst, src, target, /*endp=*/2);
3666 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3667 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3668 target, mode, /*endp=*/2);
3670 if (ret)
3671 return ret;
3673 if (TREE_CODE (len) == INTEGER_CST)
3675 rtx len_rtx = expand_normal (len);
3677 if (CONST_INT_P (len_rtx))
3679 ret = expand_builtin_strcpy_args (dst, src, target);
3681 if (ret)
3683 if (! target)
3685 if (mode != VOIDmode)
3686 target = gen_reg_rtx (mode);
3687 else
3688 target = gen_reg_rtx (GET_MODE (ret));
3690 if (GET_MODE (target) != GET_MODE (ret))
3691 ret = gen_lowpart (GET_MODE (target), ret);
3693 ret = plus_constant (ret, INTVAL (len_rtx));
3694 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3695 gcc_assert (ret);
3697 return target;
3702 return expand_movstr (dst, src, target, /*endp=*/2);
3706 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3707 bytes from constant string DATA + OFFSET and return it as target
3708 constant. */
3711 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3712 enum machine_mode mode)
3714 const char *str = (const char *) data;
3716 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3717 return const0_rtx;
3719 return c_readstr (str + offset, mode);
3722 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3723 NULL_RTX if we failed the caller should emit a normal call. */
3725 static rtx
3726 expand_builtin_strncpy (tree exp, rtx target)
3728 location_t loc = EXPR_LOCATION (exp);
3730 if (validate_arglist (exp,
3731 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3733 tree dest = CALL_EXPR_ARG (exp, 0);
3734 tree src = CALL_EXPR_ARG (exp, 1);
3735 tree len = CALL_EXPR_ARG (exp, 2);
3736 tree slen = c_strlen (src, 1);
3738 /* We must be passed a constant len and src parameter. */
3739 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3740 return NULL_RTX;
3742 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3744 /* We're required to pad with trailing zeros if the requested
3745 len is greater than strlen(s2)+1. In that case try to
3746 use store_by_pieces, if it fails, punt. */
3747 if (tree_int_cst_lt (slen, len))
3749 unsigned int dest_align
3750 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3751 const char *p = c_getstr (src);
3752 rtx dest_mem;
3754 if (!p || dest_align == 0 || !host_integerp (len, 1)
3755 || !can_store_by_pieces (tree_low_cst (len, 1),
3756 builtin_strncpy_read_str,
3757 CONST_CAST (char *, p),
3758 dest_align, false))
3759 return NULL_RTX;
3761 dest_mem = get_memory_rtx (dest, len);
3762 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3763 builtin_strncpy_read_str,
3764 CONST_CAST (char *, p), dest_align, false, 0);
3765 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3766 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3767 return dest_mem;
3770 return NULL_RTX;
3773 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3774 bytes from constant string DATA + OFFSET and return it as target
3775 constant. */
3778 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3779 enum machine_mode mode)
3781 const char *c = (const char *) data;
3782 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3784 memset (p, *c, GET_MODE_SIZE (mode));
3786 return c_readstr (p, mode);
3789 /* Callback routine for store_by_pieces. Return the RTL of a register
3790 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3791 char value given in the RTL register data. For example, if mode is
3792 4 bytes wide, return the RTL for 0x01010101*data. */
3794 static rtx
3795 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3796 enum machine_mode mode)
3798 rtx target, coeff;
3799 size_t size;
3800 char *p;
3802 size = GET_MODE_SIZE (mode);
3803 if (size == 1)
3804 return (rtx) data;
3806 p = XALLOCAVEC (char, size);
3807 memset (p, 1, size);
3808 coeff = c_readstr (p, mode);
3810 target = convert_to_mode (mode, (rtx) data, 1);
3811 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3812 return force_reg (mode, target);
3815 /* Expand expression EXP, which is a call to the memset builtin. Return
3816 NULL_RTX if we failed the caller should emit a normal call, otherwise
3817 try to get the result in TARGET, if convenient (and in mode MODE if that's
3818 convenient). */
3820 static rtx
3821 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3823 if (!validate_arglist (exp,
3824 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3825 return NULL_RTX;
3826 else
3828 tree dest = CALL_EXPR_ARG (exp, 0);
3829 tree val = CALL_EXPR_ARG (exp, 1);
3830 tree len = CALL_EXPR_ARG (exp, 2);
3831 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3835 /* Helper function to do the actual work for expand_builtin_memset. The
3836 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3837 so that this can also be called without constructing an actual CALL_EXPR.
3838 The other arguments and return value are the same as for
3839 expand_builtin_memset. */
3841 static rtx
3842 expand_builtin_memset_args (tree dest, tree val, tree len,
3843 rtx target, enum machine_mode mode, tree orig_exp)
3845 tree fndecl, fn;
3846 enum built_in_function fcode;
3847 char c;
3848 unsigned int dest_align;
3849 rtx dest_mem, dest_addr, len_rtx;
3850 HOST_WIDE_INT expected_size = -1;
3851 unsigned int expected_align = 0;
3853 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3855 /* If DEST is not a pointer type, don't do this operation in-line. */
3856 if (dest_align == 0)
3857 return NULL_RTX;
3859 if (currently_expanding_gimple_stmt)
3860 stringop_block_profile (currently_expanding_gimple_stmt,
3861 &expected_align, &expected_size);
3863 if (expected_align < dest_align)
3864 expected_align = dest_align;
3866 /* If the LEN parameter is zero, return DEST. */
3867 if (integer_zerop (len))
3869 /* Evaluate and ignore VAL in case it has side-effects. */
3870 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3871 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3874 /* Stabilize the arguments in case we fail. */
3875 dest = builtin_save_expr (dest);
3876 val = builtin_save_expr (val);
3877 len = builtin_save_expr (len);
3879 len_rtx = expand_normal (len);
3880 dest_mem = get_memory_rtx (dest, len);
3882 if (TREE_CODE (val) != INTEGER_CST)
3884 rtx val_rtx;
3886 val_rtx = expand_normal (val);
3887 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3888 val_rtx, 0);
3890 /* Assume that we can memset by pieces if we can store
3891 * the coefficients by pieces (in the required modes).
3892 * We can't pass builtin_memset_gen_str as that emits RTL. */
3893 c = 1;
3894 if (host_integerp (len, 1)
3895 && can_store_by_pieces (tree_low_cst (len, 1),
3896 builtin_memset_read_str, &c, dest_align,
3897 true))
3899 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3900 val_rtx);
3901 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3902 builtin_memset_gen_str, val_rtx, dest_align,
3903 true, 0);
3905 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3906 dest_align, expected_align,
3907 expected_size))
3908 goto do_libcall;
3910 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3911 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3912 return dest_mem;
3915 if (target_char_cast (val, &c))
3916 goto do_libcall;
3918 if (c)
3920 if (host_integerp (len, 1)
3921 && can_store_by_pieces (tree_low_cst (len, 1),
3922 builtin_memset_read_str, &c, dest_align,
3923 true))
3924 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3925 builtin_memset_read_str, &c, dest_align, true, 0);
3926 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3927 dest_align, expected_align,
3928 expected_size))
3929 goto do_libcall;
3931 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3932 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3933 return dest_mem;
3936 set_mem_align (dest_mem, dest_align);
3937 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3938 CALL_EXPR_TAILCALL (orig_exp)
3939 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3940 expected_align, expected_size);
3942 if (dest_addr == 0)
3944 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3945 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3948 return dest_addr;
3950 do_libcall:
3951 fndecl = get_callee_fndecl (orig_exp);
3952 fcode = DECL_FUNCTION_CODE (fndecl);
3953 if (fcode == BUILT_IN_MEMSET)
3954 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3955 dest, val, len);
3956 else if (fcode == BUILT_IN_BZERO)
3957 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3958 dest, len);
3959 else
3960 gcc_unreachable ();
3961 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3962 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3963 return expand_call (fn, target, target == const0_rtx);
3966 /* Expand expression EXP, which is a call to the bzero builtin. Return
3967 NULL_RTX if we failed the caller should emit a normal call. */
3969 static rtx
3970 expand_builtin_bzero (tree exp)
3972 tree dest, size;
3973 location_t loc = EXPR_LOCATION (exp);
3975 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3976 return NULL_RTX;
3978 dest = CALL_EXPR_ARG (exp, 0);
3979 size = CALL_EXPR_ARG (exp, 1);
3981 /* New argument list transforming bzero(ptr x, int y) to
3982 memset(ptr x, int 0, size_t y). This is done this way
3983 so that if it isn't expanded inline, we fallback to
3984 calling bzero instead of memset. */
3986 return expand_builtin_memset_args (dest, integer_zero_node,
3987 fold_convert_loc (loc, sizetype, size),
3988 const0_rtx, VOIDmode, exp);
3991 /* Expand expression EXP, which is a call to the memcmp built-in function.
3992 Return NULL_RTX if we failed and the
3993 caller should emit a normal call, otherwise try to get the result in
3994 TARGET, if convenient (and in mode MODE, if that's convenient). */
3996 static rtx
3997 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3998 ATTRIBUTE_UNUSED enum machine_mode mode)
4000 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4002 if (!validate_arglist (exp,
4003 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4004 return NULL_RTX;
4006 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4008 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4009 rtx result;
4010 rtx insn;
4011 tree arg1 = CALL_EXPR_ARG (exp, 0);
4012 tree arg2 = CALL_EXPR_ARG (exp, 1);
4013 tree len = CALL_EXPR_ARG (exp, 2);
4015 int arg1_align
4016 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4017 int arg2_align
4018 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4019 enum machine_mode insn_mode;
4021 #ifdef HAVE_cmpmemsi
4022 if (HAVE_cmpmemsi)
4023 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4024 else
4025 #endif
4026 #ifdef HAVE_cmpstrnsi
4027 if (HAVE_cmpstrnsi)
4028 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4029 else
4030 #endif
4031 return NULL_RTX;
4033 /* If we don't have POINTER_TYPE, call the function. */
4034 if (arg1_align == 0 || arg2_align == 0)
4035 return NULL_RTX;
4037 /* Make a place to write the result of the instruction. */
4038 result = target;
4039 if (! (result != 0
4040 && REG_P (result) && GET_MODE (result) == insn_mode
4041 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4042 result = gen_reg_rtx (insn_mode);
4044 arg1_rtx = get_memory_rtx (arg1, len);
4045 arg2_rtx = get_memory_rtx (arg2, len);
4046 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4048 /* Set MEM_SIZE as appropriate. */
4049 if (CONST_INT_P (arg3_rtx))
4051 set_mem_size (arg1_rtx, arg3_rtx);
4052 set_mem_size (arg2_rtx, arg3_rtx);
4055 #ifdef HAVE_cmpmemsi
4056 if (HAVE_cmpmemsi)
4057 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4058 GEN_INT (MIN (arg1_align, arg2_align)));
4059 else
4060 #endif
4061 #ifdef HAVE_cmpstrnsi
4062 if (HAVE_cmpstrnsi)
4063 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4064 GEN_INT (MIN (arg1_align, arg2_align)));
4065 else
4066 #endif
4067 gcc_unreachable ();
4069 if (insn)
4070 emit_insn (insn);
4071 else
4072 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4073 TYPE_MODE (integer_type_node), 3,
4074 XEXP (arg1_rtx, 0), Pmode,
4075 XEXP (arg2_rtx, 0), Pmode,
4076 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4077 TYPE_UNSIGNED (sizetype)),
4078 TYPE_MODE (sizetype));
4080 /* Return the value in the proper mode for this function. */
4081 mode = TYPE_MODE (TREE_TYPE (exp));
4082 if (GET_MODE (result) == mode)
4083 return result;
4084 else if (target != 0)
4086 convert_move (target, result, 0);
4087 return target;
4089 else
4090 return convert_to_mode (mode, result, 0);
4092 #endif
4094 return NULL_RTX;
4097 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4098 if we failed the caller should emit a normal call, otherwise try to get
4099 the result in TARGET, if convenient. */
4101 static rtx
4102 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4104 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4105 return NULL_RTX;
4107 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4108 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4109 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4111 rtx arg1_rtx, arg2_rtx;
4112 rtx result, insn = NULL_RTX;
4113 tree fndecl, fn;
4114 tree arg1 = CALL_EXPR_ARG (exp, 0);
4115 tree arg2 = CALL_EXPR_ARG (exp, 1);
4117 int arg1_align
4118 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4119 int arg2_align
4120 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4122 /* If we don't have POINTER_TYPE, call the function. */
4123 if (arg1_align == 0 || arg2_align == 0)
4124 return NULL_RTX;
4126 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4127 arg1 = builtin_save_expr (arg1);
4128 arg2 = builtin_save_expr (arg2);
4130 arg1_rtx = get_memory_rtx (arg1, NULL);
4131 arg2_rtx = get_memory_rtx (arg2, NULL);
4133 #ifdef HAVE_cmpstrsi
4134 /* Try to call cmpstrsi. */
4135 if (HAVE_cmpstrsi)
4137 enum machine_mode insn_mode
4138 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4140 /* Make a place to write the result of the instruction. */
4141 result = target;
4142 if (! (result != 0
4143 && REG_P (result) && GET_MODE (result) == insn_mode
4144 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4145 result = gen_reg_rtx (insn_mode);
4147 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4148 GEN_INT (MIN (arg1_align, arg2_align)));
4150 #endif
4151 #ifdef HAVE_cmpstrnsi
4152 /* Try to determine at least one length and call cmpstrnsi. */
4153 if (!insn && HAVE_cmpstrnsi)
4155 tree len;
4156 rtx arg3_rtx;
4158 enum machine_mode insn_mode
4159 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4160 tree len1 = c_strlen (arg1, 1);
4161 tree len2 = c_strlen (arg2, 1);
4163 if (len1)
4164 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4165 if (len2)
4166 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4168 /* If we don't have a constant length for the first, use the length
4169 of the second, if we know it. We don't require a constant for
4170 this case; some cost analysis could be done if both are available
4171 but neither is constant. For now, assume they're equally cheap,
4172 unless one has side effects. If both strings have constant lengths,
4173 use the smaller. */
4175 if (!len1)
4176 len = len2;
4177 else if (!len2)
4178 len = len1;
4179 else if (TREE_SIDE_EFFECTS (len1))
4180 len = len2;
4181 else if (TREE_SIDE_EFFECTS (len2))
4182 len = len1;
4183 else if (TREE_CODE (len1) != INTEGER_CST)
4184 len = len2;
4185 else if (TREE_CODE (len2) != INTEGER_CST)
4186 len = len1;
4187 else if (tree_int_cst_lt (len1, len2))
4188 len = len1;
4189 else
4190 len = len2;
4192 /* If both arguments have side effects, we cannot optimize. */
4193 if (!len || TREE_SIDE_EFFECTS (len))
4194 goto do_libcall;
4196 arg3_rtx = expand_normal (len);
4198 /* Make a place to write the result of the instruction. */
4199 result = target;
4200 if (! (result != 0
4201 && REG_P (result) && GET_MODE (result) == insn_mode
4202 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4203 result = gen_reg_rtx (insn_mode);
4205 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4206 GEN_INT (MIN (arg1_align, arg2_align)));
4208 #endif
4210 if (insn)
4212 enum machine_mode mode;
4213 emit_insn (insn);
4215 /* Return the value in the proper mode for this function. */
4216 mode = TYPE_MODE (TREE_TYPE (exp));
4217 if (GET_MODE (result) == mode)
4218 return result;
4219 if (target == 0)
4220 return convert_to_mode (mode, result, 0);
4221 convert_move (target, result, 0);
4222 return target;
4225 /* Expand the library call ourselves using a stabilized argument
4226 list to avoid re-evaluating the function's arguments twice. */
4227 #ifdef HAVE_cmpstrnsi
4228 do_libcall:
4229 #endif
4230 fndecl = get_callee_fndecl (exp);
4231 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4232 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4233 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4234 return expand_call (fn, target, target == const0_rtx);
4236 #endif
4237 return NULL_RTX;
4240 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4241 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4242 the result in TARGET, if convenient. */
4244 static rtx
4245 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4246 ATTRIBUTE_UNUSED enum machine_mode mode)
4248 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4250 if (!validate_arglist (exp,
4251 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4252 return NULL_RTX;
4254 /* If c_strlen can determine an expression for one of the string
4255 lengths, and it doesn't have side effects, then emit cmpstrnsi
4256 using length MIN(strlen(string)+1, arg3). */
4257 #ifdef HAVE_cmpstrnsi
4258 if (HAVE_cmpstrnsi)
4260 tree len, len1, len2;
4261 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4262 rtx result, insn;
4263 tree fndecl, fn;
4264 tree arg1 = CALL_EXPR_ARG (exp, 0);
4265 tree arg2 = CALL_EXPR_ARG (exp, 1);
4266 tree arg3 = CALL_EXPR_ARG (exp, 2);
4268 int arg1_align
4269 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4270 int arg2_align
4271 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4272 enum machine_mode insn_mode
4273 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4275 len1 = c_strlen (arg1, 1);
4276 len2 = c_strlen (arg2, 1);
4278 if (len1)
4279 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4280 if (len2)
4281 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4283 /* If we don't have a constant length for the first, use the length
4284 of the second, if we know it. We don't require a constant for
4285 this case; some cost analysis could be done if both are available
4286 but neither is constant. For now, assume they're equally cheap,
4287 unless one has side effects. If both strings have constant lengths,
4288 use the smaller. */
4290 if (!len1)
4291 len = len2;
4292 else if (!len2)
4293 len = len1;
4294 else if (TREE_SIDE_EFFECTS (len1))
4295 len = len2;
4296 else if (TREE_SIDE_EFFECTS (len2))
4297 len = len1;
4298 else if (TREE_CODE (len1) != INTEGER_CST)
4299 len = len2;
4300 else if (TREE_CODE (len2) != INTEGER_CST)
4301 len = len1;
4302 else if (tree_int_cst_lt (len1, len2))
4303 len = len1;
4304 else
4305 len = len2;
4307 /* If both arguments have side effects, we cannot optimize. */
4308 if (!len || TREE_SIDE_EFFECTS (len))
4309 return NULL_RTX;
4311 /* The actual new length parameter is MIN(len,arg3). */
4312 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4313 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4315 /* If we don't have POINTER_TYPE, call the function. */
4316 if (arg1_align == 0 || arg2_align == 0)
4317 return NULL_RTX;
4319 /* Make a place to write the result of the instruction. */
4320 result = target;
4321 if (! (result != 0
4322 && REG_P (result) && GET_MODE (result) == insn_mode
4323 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4324 result = gen_reg_rtx (insn_mode);
4326 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4327 arg1 = builtin_save_expr (arg1);
4328 arg2 = builtin_save_expr (arg2);
4329 len = builtin_save_expr (len);
4331 arg1_rtx = get_memory_rtx (arg1, len);
4332 arg2_rtx = get_memory_rtx (arg2, len);
4333 arg3_rtx = expand_normal (len);
4334 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4335 GEN_INT (MIN (arg1_align, arg2_align)));
4336 if (insn)
4338 emit_insn (insn);
4340 /* Return the value in the proper mode for this function. */
4341 mode = TYPE_MODE (TREE_TYPE (exp));
4342 if (GET_MODE (result) == mode)
4343 return result;
4344 if (target == 0)
4345 return convert_to_mode (mode, result, 0);
4346 convert_move (target, result, 0);
4347 return target;
4350 /* Expand the library call ourselves using a stabilized argument
4351 list to avoid re-evaluating the function's arguments twice. */
4352 fndecl = get_callee_fndecl (exp);
4353 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4354 arg1, arg2, len);
4355 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4356 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4357 return expand_call (fn, target, target == const0_rtx);
4359 #endif
4360 return NULL_RTX;
4363 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4364 if that's convenient. */
4367 expand_builtin_saveregs (void)
4369 rtx val, seq;
4371 /* Don't do __builtin_saveregs more than once in a function.
4372 Save the result of the first call and reuse it. */
4373 if (saveregs_value != 0)
4374 return saveregs_value;
4376 /* When this function is called, it means that registers must be
4377 saved on entry to this function. So we migrate the call to the
4378 first insn of this function. */
4380 start_sequence ();
4382 /* Do whatever the machine needs done in this case. */
4383 val = targetm.calls.expand_builtin_saveregs ();
4385 seq = get_insns ();
4386 end_sequence ();
4388 saveregs_value = val;
4390 /* Put the insns after the NOTE that starts the function. If this
4391 is inside a start_sequence, make the outer-level insn chain current, so
4392 the code is placed at the start of the function. */
4393 push_topmost_sequence ();
4394 emit_insn_after (seq, entry_of_function ());
4395 pop_topmost_sequence ();
4397 return val;
4400 /* Expand a call to __builtin_next_arg. */
4402 static rtx
4403 expand_builtin_next_arg (void)
4405 /* Checking arguments is already done in fold_builtin_next_arg
4406 that must be called before this function. */
4407 return expand_binop (ptr_mode, add_optab,
4408 crtl->args.internal_arg_pointer,
4409 crtl->args.arg_offset_rtx,
4410 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4413 /* Make it easier for the backends by protecting the valist argument
4414 from multiple evaluations. */
4416 static tree
4417 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4419 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4421 /* The current way of determining the type of valist is completely
4422 bogus. We should have the information on the va builtin instead. */
4423 if (!vatype)
4424 vatype = targetm.fn_abi_va_list (cfun->decl);
4426 if (TREE_CODE (vatype) == ARRAY_TYPE)
4428 if (TREE_SIDE_EFFECTS (valist))
4429 valist = save_expr (valist);
4431 /* For this case, the backends will be expecting a pointer to
4432 vatype, but it's possible we've actually been given an array
4433 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4434 So fix it. */
4435 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4437 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4438 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4441 else
4443 tree pt = build_pointer_type (vatype);
4445 if (! needs_lvalue)
4447 if (! TREE_SIDE_EFFECTS (valist))
4448 return valist;
4450 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4451 TREE_SIDE_EFFECTS (valist) = 1;
4454 if (TREE_SIDE_EFFECTS (valist))
4455 valist = save_expr (valist);
4456 valist = fold_build2_loc (loc, MEM_REF,
4457 vatype, valist, build_int_cst (pt, 0));
4460 return valist;
4463 /* The "standard" definition of va_list is void*. */
4465 tree
4466 std_build_builtin_va_list (void)
4468 return ptr_type_node;
4471 /* The "standard" abi va_list is va_list_type_node. */
4473 tree
4474 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4476 return va_list_type_node;
4479 /* The "standard" type of va_list is va_list_type_node. */
4481 tree
4482 std_canonical_va_list_type (tree type)
4484 tree wtype, htype;
4486 if (INDIRECT_REF_P (type))
4487 type = TREE_TYPE (type);
4488 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4489 type = TREE_TYPE (type);
4490 wtype = va_list_type_node;
4491 htype = type;
4492 /* Treat structure va_list types. */
4493 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4494 htype = TREE_TYPE (htype);
4495 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4497 /* If va_list is an array type, the argument may have decayed
4498 to a pointer type, e.g. by being passed to another function.
4499 In that case, unwrap both types so that we can compare the
4500 underlying records. */
4501 if (TREE_CODE (htype) == ARRAY_TYPE
4502 || POINTER_TYPE_P (htype))
4504 wtype = TREE_TYPE (wtype);
4505 htype = TREE_TYPE (htype);
4508 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4509 return va_list_type_node;
4511 return NULL_TREE;
4514 /* The "standard" implementation of va_start: just assign `nextarg' to
4515 the variable. */
4517 void
4518 std_expand_builtin_va_start (tree valist, rtx nextarg)
4520 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4521 convert_move (va_r, nextarg, 0);
4524 /* Expand EXP, a call to __builtin_va_start. */
4526 static rtx
4527 expand_builtin_va_start (tree exp)
4529 rtx nextarg;
4530 tree valist;
4531 location_t loc = EXPR_LOCATION (exp);
4533 if (call_expr_nargs (exp) < 2)
4535 error_at (loc, "too few arguments to function %<va_start%>");
4536 return const0_rtx;
4539 if (fold_builtin_next_arg (exp, true))
4540 return const0_rtx;
4542 nextarg = expand_builtin_next_arg ();
4543 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4545 if (targetm.expand_builtin_va_start)
4546 targetm.expand_builtin_va_start (valist, nextarg);
4547 else
4548 std_expand_builtin_va_start (valist, nextarg);
4550 return const0_rtx;
4553 /* The "standard" implementation of va_arg: read the value from the
4554 current (padded) address and increment by the (padded) size. */
4556 tree
4557 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4558 gimple_seq *post_p)
4560 tree addr, t, type_size, rounded_size, valist_tmp;
4561 unsigned HOST_WIDE_INT align, boundary;
4562 bool indirect;
4564 #ifdef ARGS_GROW_DOWNWARD
4565 /* All of the alignment and movement below is for args-grow-up machines.
4566 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4567 implement their own specialized gimplify_va_arg_expr routines. */
4568 gcc_unreachable ();
4569 #endif
4571 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4572 if (indirect)
4573 type = build_pointer_type (type);
4575 align = PARM_BOUNDARY / BITS_PER_UNIT;
4576 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4578 /* When we align parameter on stack for caller, if the parameter
4579 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4580 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4581 here with caller. */
4582 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4583 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4585 boundary /= BITS_PER_UNIT;
4587 /* Hoist the valist value into a temporary for the moment. */
4588 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4590 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4591 requires greater alignment, we must perform dynamic alignment. */
4592 if (boundary > align
4593 && !integer_zerop (TYPE_SIZE (type)))
4595 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4596 fold_build2 (POINTER_PLUS_EXPR,
4597 TREE_TYPE (valist),
4598 valist_tmp, size_int (boundary - 1)));
4599 gimplify_and_add (t, pre_p);
4601 t = fold_convert (sizetype, valist_tmp);
4602 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4603 fold_convert (TREE_TYPE (valist),
4604 fold_build2 (BIT_AND_EXPR, sizetype, t,
4605 size_int (-boundary))));
4606 gimplify_and_add (t, pre_p);
4608 else
4609 boundary = align;
4611 /* If the actual alignment is less than the alignment of the type,
4612 adjust the type accordingly so that we don't assume strict alignment
4613 when dereferencing the pointer. */
4614 boundary *= BITS_PER_UNIT;
4615 if (boundary < TYPE_ALIGN (type))
4617 type = build_variant_type_copy (type);
4618 TYPE_ALIGN (type) = boundary;
4621 /* Compute the rounded size of the type. */
4622 type_size = size_in_bytes (type);
4623 rounded_size = round_up (type_size, align);
4625 /* Reduce rounded_size so it's sharable with the postqueue. */
4626 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4628 /* Get AP. */
4629 addr = valist_tmp;
4630 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4632 /* Small args are padded downward. */
4633 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4634 rounded_size, size_int (align));
4635 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4636 size_binop (MINUS_EXPR, rounded_size, type_size));
4637 addr = fold_build2 (POINTER_PLUS_EXPR,
4638 TREE_TYPE (addr), addr, t);
4641 /* Compute new value for AP. */
4642 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4643 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4644 gimplify_and_add (t, pre_p);
4646 addr = fold_convert (build_pointer_type (type), addr);
4648 if (indirect)
4649 addr = build_va_arg_indirect_ref (addr);
4651 return build_va_arg_indirect_ref (addr);
4654 /* Build an indirect-ref expression over the given TREE, which represents a
4655 piece of a va_arg() expansion. */
4656 tree
4657 build_va_arg_indirect_ref (tree addr)
4659 addr = build_fold_indirect_ref_loc (EXPR_LOCATION (addr), addr);
4661 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4662 mf_mark (addr);
4664 return addr;
4667 /* Return a dummy expression of type TYPE in order to keep going after an
4668 error. */
4670 static tree
4671 dummy_object (tree type)
4673 tree t = build_int_cst (build_pointer_type (type), 0);
4674 return build1 (INDIRECT_REF, type, t);
4677 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4678 builtin function, but a very special sort of operator. */
4680 enum gimplify_status
4681 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4683 tree promoted_type, have_va_type;
4684 tree valist = TREE_OPERAND (*expr_p, 0);
4685 tree type = TREE_TYPE (*expr_p);
4686 tree t;
4687 location_t loc = EXPR_LOCATION (*expr_p);
4689 /* Verify that valist is of the proper type. */
4690 have_va_type = TREE_TYPE (valist);
4691 if (have_va_type == error_mark_node)
4692 return GS_ERROR;
4693 have_va_type = targetm.canonical_va_list_type (have_va_type);
4695 if (have_va_type == NULL_TREE)
4697 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4698 return GS_ERROR;
4701 /* Generate a diagnostic for requesting data of a type that cannot
4702 be passed through `...' due to type promotion at the call site. */
4703 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4704 != type)
4706 static bool gave_help;
4707 bool warned;
4709 /* Unfortunately, this is merely undefined, rather than a constraint
4710 violation, so we cannot make this an error. If this call is never
4711 executed, the program is still strictly conforming. */
4712 warned = warning_at (loc, 0,
4713 "%qT is promoted to %qT when passed through %<...%>",
4714 type, promoted_type);
4715 if (!gave_help && warned)
4717 gave_help = true;
4718 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4719 promoted_type, type);
4722 /* We can, however, treat "undefined" any way we please.
4723 Call abort to encourage the user to fix the program. */
4724 if (warned)
4725 inform (loc, "if this code is reached, the program will abort");
4726 /* Before the abort, allow the evaluation of the va_list
4727 expression to exit or longjmp. */
4728 gimplify_and_add (valist, pre_p);
4729 t = build_call_expr_loc (loc,
4730 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4731 gimplify_and_add (t, pre_p);
4733 /* This is dead code, but go ahead and finish so that the
4734 mode of the result comes out right. */
4735 *expr_p = dummy_object (type);
4736 return GS_ALL_DONE;
4738 else
4740 /* Make it easier for the backends by protecting the valist argument
4741 from multiple evaluations. */
4742 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4744 /* For this case, the backends will be expecting a pointer to
4745 TREE_TYPE (abi), but it's possible we've
4746 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4747 So fix it. */
4748 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4750 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4751 valist = fold_convert_loc (loc, p1,
4752 build_fold_addr_expr_loc (loc, valist));
4755 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4757 else
4758 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4760 if (!targetm.gimplify_va_arg_expr)
4761 /* FIXME: Once most targets are converted we should merely
4762 assert this is non-null. */
4763 return GS_ALL_DONE;
4765 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4766 return GS_OK;
4770 /* Expand EXP, a call to __builtin_va_end. */
4772 static rtx
4773 expand_builtin_va_end (tree exp)
4775 tree valist = CALL_EXPR_ARG (exp, 0);
4777 /* Evaluate for side effects, if needed. I hate macros that don't
4778 do that. */
4779 if (TREE_SIDE_EFFECTS (valist))
4780 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4782 return const0_rtx;
4785 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4786 builtin rather than just as an assignment in stdarg.h because of the
4787 nastiness of array-type va_list types. */
4789 static rtx
4790 expand_builtin_va_copy (tree exp)
4792 tree dst, src, t;
4793 location_t loc = EXPR_LOCATION (exp);
4795 dst = CALL_EXPR_ARG (exp, 0);
4796 src = CALL_EXPR_ARG (exp, 1);
4798 dst = stabilize_va_list_loc (loc, dst, 1);
4799 src = stabilize_va_list_loc (loc, src, 0);
4801 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4803 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4805 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4806 TREE_SIDE_EFFECTS (t) = 1;
4807 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4809 else
4811 rtx dstb, srcb, size;
4813 /* Evaluate to pointers. */
4814 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4815 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4816 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4817 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4819 dstb = convert_memory_address (Pmode, dstb);
4820 srcb = convert_memory_address (Pmode, srcb);
4822 /* "Dereference" to BLKmode memories. */
4823 dstb = gen_rtx_MEM (BLKmode, dstb);
4824 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4825 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4826 srcb = gen_rtx_MEM (BLKmode, srcb);
4827 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4828 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4830 /* Copy. */
4831 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4834 return const0_rtx;
4837 /* Expand a call to one of the builtin functions __builtin_frame_address or
4838 __builtin_return_address. */
4840 static rtx
4841 expand_builtin_frame_address (tree fndecl, tree exp)
4843 /* The argument must be a nonnegative integer constant.
4844 It counts the number of frames to scan up the stack.
4845 The value is the return address saved in that frame. */
4846 if (call_expr_nargs (exp) == 0)
4847 /* Warning about missing arg was already issued. */
4848 return const0_rtx;
4849 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4851 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4852 error ("invalid argument to %<__builtin_frame_address%>");
4853 else
4854 error ("invalid argument to %<__builtin_return_address%>");
4855 return const0_rtx;
4857 else
4859 rtx tem
4860 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4861 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4863 /* Some ports cannot access arbitrary stack frames. */
4864 if (tem == NULL)
4866 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4867 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4868 else
4869 warning (0, "unsupported argument to %<__builtin_return_address%>");
4870 return const0_rtx;
4873 /* For __builtin_frame_address, return what we've got. */
4874 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4875 return tem;
4877 if (!REG_P (tem)
4878 && ! CONSTANT_P (tem))
4879 tem = copy_to_mode_reg (Pmode, tem);
4880 return tem;
4884 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
4885 we failed and the caller should emit a normal call, otherwise try to get
4886 the result in TARGET, if convenient. */
4888 static rtx
4889 expand_builtin_alloca (tree exp, rtx target)
4891 rtx op0;
4892 rtx result;
4894 /* Emit normal call if marked not-inlineable. */
4895 if (CALL_CANNOT_INLINE_P (exp))
4896 return NULL_RTX;
4898 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4899 return NULL_RTX;
4901 /* Compute the argument. */
4902 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4904 /* Allocate the desired space. */
4905 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4906 result = convert_memory_address (ptr_mode, result);
4908 return result;
4911 /* Expand a call to a bswap builtin with argument ARG0. MODE
4912 is the mode to expand with. */
4914 static rtx
4915 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4917 enum machine_mode mode;
4918 tree arg;
4919 rtx op0;
4921 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4922 return NULL_RTX;
4924 arg = CALL_EXPR_ARG (exp, 0);
4925 mode = TYPE_MODE (TREE_TYPE (arg));
4926 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4928 target = expand_unop (mode, bswap_optab, op0, target, 1);
4930 gcc_assert (target);
4932 return convert_to_mode (mode, target, 0);
4935 /* Expand a call to a unary builtin in EXP.
4936 Return NULL_RTX if a normal call should be emitted rather than expanding the
4937 function in-line. If convenient, the result should be placed in TARGET.
4938 SUBTARGET may be used as the target for computing one of EXP's operands. */
4940 static rtx
4941 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4942 rtx subtarget, optab op_optab)
4944 rtx op0;
4946 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4947 return NULL_RTX;
4949 /* Compute the argument. */
4950 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
4951 VOIDmode, EXPAND_NORMAL);
4952 /* Compute op, into TARGET if possible.
4953 Set TARGET to wherever the result comes back. */
4954 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4955 op_optab, op0, target, 1);
4956 gcc_assert (target);
4958 return convert_to_mode (target_mode, target, 0);
4961 /* Expand a call to __builtin_expect. We just return our argument
4962 as the builtin_expect semantic should've been already executed by
4963 tree branch prediction pass. */
4965 static rtx
4966 expand_builtin_expect (tree exp, rtx target)
4968 tree arg;
4970 if (call_expr_nargs (exp) < 2)
4971 return const0_rtx;
4972 arg = CALL_EXPR_ARG (exp, 0);
4974 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4975 /* When guessing was done, the hints should be already stripped away. */
4976 gcc_assert (!flag_guess_branch_prob
4977 || optimize == 0 || seen_error ());
4978 return target;
4981 void
4982 expand_builtin_trap (void)
4984 #ifdef HAVE_trap
4985 if (HAVE_trap)
4986 emit_insn (gen_trap ());
4987 else
4988 #endif
4989 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4990 emit_barrier ();
4993 /* Expand a call to __builtin_unreachable. We do nothing except emit
4994 a barrier saying that control flow will not pass here.
4996 It is the responsibility of the program being compiled to ensure
4997 that control flow does never reach __builtin_unreachable. */
4998 static void
4999 expand_builtin_unreachable (void)
5001 emit_barrier ();
5004 /* Expand EXP, a call to fabs, fabsf or fabsl.
5005 Return NULL_RTX if a normal call should be emitted rather than expanding
5006 the function inline. If convenient, the result should be placed
5007 in TARGET. SUBTARGET may be used as the target for computing
5008 the operand. */
5010 static rtx
5011 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5013 enum machine_mode mode;
5014 tree arg;
5015 rtx op0;
5017 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5018 return NULL_RTX;
5020 arg = CALL_EXPR_ARG (exp, 0);
5021 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5022 mode = TYPE_MODE (TREE_TYPE (arg));
5023 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5024 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5027 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5028 Return NULL is a normal call should be emitted rather than expanding the
5029 function inline. If convenient, the result should be placed in TARGET.
5030 SUBTARGET may be used as the target for computing the operand. */
5032 static rtx
5033 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5035 rtx op0, op1;
5036 tree arg;
5038 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5039 return NULL_RTX;
5041 arg = CALL_EXPR_ARG (exp, 0);
5042 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5044 arg = CALL_EXPR_ARG (exp, 1);
5045 op1 = expand_normal (arg);
5047 return expand_copysign (op0, op1, target);
5050 /* Create a new constant string literal and return a char* pointer to it.
5051 The STRING_CST value is the LEN characters at STR. */
5052 tree
5053 build_string_literal (int len, const char *str)
5055 tree t, elem, index, type;
5057 t = build_string (len, str);
5058 elem = build_type_variant (char_type_node, 1, 0);
5059 index = build_index_type (size_int (len - 1));
5060 type = build_array_type (elem, index);
5061 TREE_TYPE (t) = type;
5062 TREE_CONSTANT (t) = 1;
5063 TREE_READONLY (t) = 1;
5064 TREE_STATIC (t) = 1;
5066 type = build_pointer_type (elem);
5067 t = build1 (ADDR_EXPR, type,
5068 build4 (ARRAY_REF, elem,
5069 t, integer_zero_node, NULL_TREE, NULL_TREE));
5070 return t;
5073 /* Expand a call to either the entry or exit function profiler. */
5075 static rtx
5076 expand_builtin_profile_func (bool exitp)
5078 rtx this_rtx, which;
5080 this_rtx = DECL_RTL (current_function_decl);
5081 gcc_assert (MEM_P (this_rtx));
5082 this_rtx = XEXP (this_rtx, 0);
5084 if (exitp)
5085 which = profile_function_exit_libfunc;
5086 else
5087 which = profile_function_entry_libfunc;
5089 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5090 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5092 Pmode);
5094 return const0_rtx;
5097 /* Expand a call to __builtin___clear_cache. */
5099 static rtx
5100 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5102 #ifndef HAVE_clear_cache
5103 #ifdef CLEAR_INSN_CACHE
5104 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5105 does something. Just do the default expansion to a call to
5106 __clear_cache(). */
5107 return NULL_RTX;
5108 #else
5109 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5110 does nothing. There is no need to call it. Do nothing. */
5111 return const0_rtx;
5112 #endif /* CLEAR_INSN_CACHE */
5113 #else
5114 /* We have a "clear_cache" insn, and it will handle everything. */
5115 tree begin, end;
5116 rtx begin_rtx, end_rtx;
5117 enum insn_code icode;
5119 /* We must not expand to a library call. If we did, any
5120 fallback library function in libgcc that might contain a call to
5121 __builtin___clear_cache() would recurse infinitely. */
5122 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5124 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5125 return const0_rtx;
5128 if (HAVE_clear_cache)
5130 icode = CODE_FOR_clear_cache;
5132 begin = CALL_EXPR_ARG (exp, 0);
5133 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5134 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5135 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5136 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5138 end = CALL_EXPR_ARG (exp, 1);
5139 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5140 end_rtx = convert_memory_address (Pmode, end_rtx);
5141 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5142 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5144 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5146 return const0_rtx;
5147 #endif /* HAVE_clear_cache */
5150 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5152 static rtx
5153 round_trampoline_addr (rtx tramp)
5155 rtx temp, addend, mask;
5157 /* If we don't need too much alignment, we'll have been guaranteed
5158 proper alignment by get_trampoline_type. */
5159 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5160 return tramp;
5162 /* Round address up to desired boundary. */
5163 temp = gen_reg_rtx (Pmode);
5164 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5165 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5167 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5168 temp, 0, OPTAB_LIB_WIDEN);
5169 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5170 temp, 0, OPTAB_LIB_WIDEN);
5172 return tramp;
5175 static rtx
5176 expand_builtin_init_trampoline (tree exp)
5178 tree t_tramp, t_func, t_chain;
5179 rtx m_tramp, r_tramp, r_chain, tmp;
5181 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5182 POINTER_TYPE, VOID_TYPE))
5183 return NULL_RTX;
5185 t_tramp = CALL_EXPR_ARG (exp, 0);
5186 t_func = CALL_EXPR_ARG (exp, 1);
5187 t_chain = CALL_EXPR_ARG (exp, 2);
5189 r_tramp = expand_normal (t_tramp);
5190 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5191 MEM_NOTRAP_P (m_tramp) = 1;
5193 /* The TRAMP argument should be the address of a field within the
5194 local function's FRAME decl. Let's see if we can fill in the
5195 to fill in the MEM_ATTRs for this memory. */
5196 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5197 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5198 true, 0);
5200 tmp = round_trampoline_addr (r_tramp);
5201 if (tmp != r_tramp)
5203 m_tramp = change_address (m_tramp, BLKmode, tmp);
5204 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5205 set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5208 /* The FUNC argument should be the address of the nested function.
5209 Extract the actual function decl to pass to the hook. */
5210 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5211 t_func = TREE_OPERAND (t_func, 0);
5212 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5214 r_chain = expand_normal (t_chain);
5216 /* Generate insns to initialize the trampoline. */
5217 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5219 trampolines_created = 1;
5221 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5222 "trampoline generated for nested function %qD", t_func);
5224 return const0_rtx;
5227 static rtx
5228 expand_builtin_adjust_trampoline (tree exp)
5230 rtx tramp;
5232 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5233 return NULL_RTX;
5235 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5236 tramp = round_trampoline_addr (tramp);
5237 if (targetm.calls.trampoline_adjust_address)
5238 tramp = targetm.calls.trampoline_adjust_address (tramp);
5240 return tramp;
5243 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5244 function. The function first checks whether the back end provides
5245 an insn to implement signbit for the respective mode. If not, it
5246 checks whether the floating point format of the value is such that
5247 the sign bit can be extracted. If that is not the case, the
5248 function returns NULL_RTX to indicate that a normal call should be
5249 emitted rather than expanding the function in-line. EXP is the
5250 expression that is a call to the builtin function; if convenient,
5251 the result should be placed in TARGET. */
5252 static rtx
5253 expand_builtin_signbit (tree exp, rtx target)
5255 const struct real_format *fmt;
5256 enum machine_mode fmode, imode, rmode;
5257 tree arg;
5258 int word, bitpos;
5259 enum insn_code icode;
5260 rtx temp;
5261 location_t loc = EXPR_LOCATION (exp);
5263 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5264 return NULL_RTX;
5266 arg = CALL_EXPR_ARG (exp, 0);
5267 fmode = TYPE_MODE (TREE_TYPE (arg));
5268 rmode = TYPE_MODE (TREE_TYPE (exp));
5269 fmt = REAL_MODE_FORMAT (fmode);
5271 arg = builtin_save_expr (arg);
5273 /* Expand the argument yielding a RTX expression. */
5274 temp = expand_normal (arg);
5276 /* Check if the back end provides an insn that handles signbit for the
5277 argument's mode. */
5278 icode = optab_handler (signbit_optab, fmode);
5279 if (icode != CODE_FOR_nothing)
5281 rtx last = get_last_insn ();
5282 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5283 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5284 return target;
5285 delete_insns_since (last);
5288 /* For floating point formats without a sign bit, implement signbit
5289 as "ARG < 0.0". */
5290 bitpos = fmt->signbit_ro;
5291 if (bitpos < 0)
5293 /* But we can't do this if the format supports signed zero. */
5294 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5295 return NULL_RTX;
5297 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5298 build_real (TREE_TYPE (arg), dconst0));
5299 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5302 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5304 imode = int_mode_for_mode (fmode);
5305 if (imode == BLKmode)
5306 return NULL_RTX;
5307 temp = gen_lowpart (imode, temp);
5309 else
5311 imode = word_mode;
5312 /* Handle targets with different FP word orders. */
5313 if (FLOAT_WORDS_BIG_ENDIAN)
5314 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5315 else
5316 word = bitpos / BITS_PER_WORD;
5317 temp = operand_subword_force (temp, word, fmode);
5318 bitpos = bitpos % BITS_PER_WORD;
5321 /* Force the intermediate word_mode (or narrower) result into a
5322 register. This avoids attempting to create paradoxical SUBREGs
5323 of floating point modes below. */
5324 temp = force_reg (imode, temp);
5326 /* If the bitpos is within the "result mode" lowpart, the operation
5327 can be implement with a single bitwise AND. Otherwise, we need
5328 a right shift and an AND. */
5330 if (bitpos < GET_MODE_BITSIZE (rmode))
5332 double_int mask = double_int_setbit (double_int_zero, bitpos);
5334 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5335 temp = gen_lowpart (rmode, temp);
5336 temp = expand_binop (rmode, and_optab, temp,
5337 immed_double_int_const (mask, rmode),
5338 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5340 else
5342 /* Perform a logical right shift to place the signbit in the least
5343 significant bit, then truncate the result to the desired mode
5344 and mask just this bit. */
5345 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5346 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5347 temp = gen_lowpart (rmode, temp);
5348 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5349 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5352 return temp;
5355 /* Expand fork or exec calls. TARGET is the desired target of the
5356 call. EXP is the call. FN is the
5357 identificator of the actual function. IGNORE is nonzero if the
5358 value is to be ignored. */
5360 static rtx
5361 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5363 tree id, decl;
5364 tree call;
5366 /* If we are not profiling, just call the function. */
5367 if (!profile_arc_flag)
5368 return NULL_RTX;
5370 /* Otherwise call the wrapper. This should be equivalent for the rest of
5371 compiler, so the code does not diverge, and the wrapper may run the
5372 code necessary for keeping the profiling sane. */
5374 switch (DECL_FUNCTION_CODE (fn))
5376 case BUILT_IN_FORK:
5377 id = get_identifier ("__gcov_fork");
5378 break;
5380 case BUILT_IN_EXECL:
5381 id = get_identifier ("__gcov_execl");
5382 break;
5384 case BUILT_IN_EXECV:
5385 id = get_identifier ("__gcov_execv");
5386 break;
5388 case BUILT_IN_EXECLP:
5389 id = get_identifier ("__gcov_execlp");
5390 break;
5392 case BUILT_IN_EXECLE:
5393 id = get_identifier ("__gcov_execle");
5394 break;
5396 case BUILT_IN_EXECVP:
5397 id = get_identifier ("__gcov_execvp");
5398 break;
5400 case BUILT_IN_EXECVE:
5401 id = get_identifier ("__gcov_execve");
5402 break;
5404 default:
5405 gcc_unreachable ();
5408 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5409 FUNCTION_DECL, id, TREE_TYPE (fn));
5410 DECL_EXTERNAL (decl) = 1;
5411 TREE_PUBLIC (decl) = 1;
5412 DECL_ARTIFICIAL (decl) = 1;
5413 TREE_NOTHROW (decl) = 1;
5414 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5415 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5416 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5417 return expand_call (call, target, ignore);
5422 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5423 the pointer in these functions is void*, the tree optimizers may remove
5424 casts. The mode computed in expand_builtin isn't reliable either, due
5425 to __sync_bool_compare_and_swap.
5427 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5428 group of builtins. This gives us log2 of the mode size. */
5430 static inline enum machine_mode
5431 get_builtin_sync_mode (int fcode_diff)
5433 /* The size is not negotiable, so ask not to get BLKmode in return
5434 if the target indicates that a smaller size would be better. */
5435 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5438 /* Expand the memory expression LOC and return the appropriate memory operand
5439 for the builtin_sync operations. */
5441 static rtx
5442 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5444 rtx addr, mem;
5446 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5447 addr = convert_memory_address (Pmode, addr);
5449 /* Note that we explicitly do not want any alias information for this
5450 memory, so that we kill all other live memories. Otherwise we don't
5451 satisfy the full barrier semantics of the intrinsic. */
5452 mem = validize_mem (gen_rtx_MEM (mode, addr));
5454 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5455 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5456 MEM_VOLATILE_P (mem) = 1;
5458 return mem;
5461 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5462 EXP is the CALL_EXPR. CODE is the rtx code
5463 that corresponds to the arithmetic or logical operation from the name;
5464 an exception here is that NOT actually means NAND. TARGET is an optional
5465 place for us to store the results; AFTER is true if this is the
5466 fetch_and_xxx form. IGNORE is true if we don't actually care about
5467 the result of the operation at all. */
5469 static rtx
5470 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5471 enum rtx_code code, bool after,
5472 rtx target, bool ignore)
5474 rtx val, mem;
5475 enum machine_mode old_mode;
5476 location_t loc = EXPR_LOCATION (exp);
5478 if (code == NOT && warn_sync_nand)
5480 tree fndecl = get_callee_fndecl (exp);
5481 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5483 static bool warned_f_a_n, warned_n_a_f;
5485 switch (fcode)
5487 case BUILT_IN_FETCH_AND_NAND_1:
5488 case BUILT_IN_FETCH_AND_NAND_2:
5489 case BUILT_IN_FETCH_AND_NAND_4:
5490 case BUILT_IN_FETCH_AND_NAND_8:
5491 case BUILT_IN_FETCH_AND_NAND_16:
5493 if (warned_f_a_n)
5494 break;
5496 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5497 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5498 warned_f_a_n = true;
5499 break;
5501 case BUILT_IN_NAND_AND_FETCH_1:
5502 case BUILT_IN_NAND_AND_FETCH_2:
5503 case BUILT_IN_NAND_AND_FETCH_4:
5504 case BUILT_IN_NAND_AND_FETCH_8:
5505 case BUILT_IN_NAND_AND_FETCH_16:
5507 if (warned_n_a_f)
5508 break;
5510 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5511 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5512 warned_n_a_f = true;
5513 break;
5515 default:
5516 gcc_unreachable ();
5520 /* Expand the operands. */
5521 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5523 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5524 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5525 of CONST_INTs, where we know the old_mode only from the call argument. */
5526 old_mode = GET_MODE (val);
5527 if (old_mode == VOIDmode)
5528 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5529 val = convert_modes (mode, old_mode, val, 1);
5531 if (ignore)
5532 return expand_sync_operation (mem, val, code);
5533 else
5534 return expand_sync_fetch_operation (mem, val, code, after, target);
5537 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5538 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5539 true if this is the boolean form. TARGET is a place for us to store the
5540 results; this is NOT optional if IS_BOOL is true. */
5542 static rtx
5543 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5544 bool is_bool, rtx target)
5546 rtx old_val, new_val, mem;
5547 enum machine_mode old_mode;
5549 /* Expand the operands. */
5550 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5553 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5554 mode, EXPAND_NORMAL);
5555 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5556 of CONST_INTs, where we know the old_mode only from the call argument. */
5557 old_mode = GET_MODE (old_val);
5558 if (old_mode == VOIDmode)
5559 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5560 old_val = convert_modes (mode, old_mode, old_val, 1);
5562 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5563 mode, EXPAND_NORMAL);
5564 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5565 of CONST_INTs, where we know the old_mode only from the call argument. */
5566 old_mode = GET_MODE (new_val);
5567 if (old_mode == VOIDmode)
5568 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5569 new_val = convert_modes (mode, old_mode, new_val, 1);
5571 if (is_bool)
5572 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5573 else
5574 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5577 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5578 general form is actually an atomic exchange, and some targets only
5579 support a reduced form with the second argument being a constant 1.
5580 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5581 the results. */
5583 static rtx
5584 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5585 rtx target)
5587 rtx val, mem;
5588 enum machine_mode old_mode;
5590 /* Expand the operands. */
5591 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5592 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5593 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5594 of CONST_INTs, where we know the old_mode only from the call argument. */
5595 old_mode = GET_MODE (val);
5596 if (old_mode == VOIDmode)
5597 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5598 val = convert_modes (mode, old_mode, val, 1);
5600 return expand_sync_lock_test_and_set (mem, val, target);
5603 /* Expand the __sync_synchronize intrinsic. */
5605 static void
5606 expand_builtin_synchronize (void)
5608 gimple x;
5609 VEC (tree, gc) *v_clobbers;
5611 #ifdef HAVE_memory_barrier
5612 if (HAVE_memory_barrier)
5614 emit_insn (gen_memory_barrier ());
5615 return;
5617 #endif
5619 if (synchronize_libfunc != NULL_RTX)
5621 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5622 return;
5625 /* If no explicit memory barrier instruction is available, create an
5626 empty asm stmt with a memory clobber. */
5627 v_clobbers = VEC_alloc (tree, gc, 1);
5628 VEC_quick_push (tree, v_clobbers,
5629 tree_cons (NULL, build_string (6, "memory"), NULL));
5630 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5631 gimple_asm_set_volatile (x, true);
5632 expand_asm_stmt (x);
5635 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5637 static void
5638 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5640 enum insn_code icode;
5641 rtx mem, insn;
5642 rtx val = const0_rtx;
5644 /* Expand the operands. */
5645 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5647 /* If there is an explicit operation in the md file, use it. */
5648 icode = direct_optab_handler (sync_lock_release_optab, mode);
5649 if (icode != CODE_FOR_nothing)
5651 if (!insn_data[icode].operand[1].predicate (val, mode))
5652 val = force_reg (mode, val);
5654 insn = GEN_FCN (icode) (mem, val);
5655 if (insn)
5657 emit_insn (insn);
5658 return;
5662 /* Otherwise we can implement this operation by emitting a barrier
5663 followed by a store of zero. */
5664 expand_builtin_synchronize ();
5665 emit_move_insn (mem, val);
5668 /* Expand an expression EXP that calls a built-in function,
5669 with result going to TARGET if that's convenient
5670 (and in mode MODE if that's convenient).
5671 SUBTARGET may be used as the target for computing one of EXP's operands.
5672 IGNORE is nonzero if the value is to be ignored. */
5675 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5676 int ignore)
5678 tree fndecl = get_callee_fndecl (exp);
5679 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5680 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5682 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5683 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5685 /* When not optimizing, generate calls to library functions for a certain
5686 set of builtins. */
5687 if (!optimize
5688 && !called_as_built_in (fndecl)
5689 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5690 && fcode != BUILT_IN_ALLOCA
5691 && fcode != BUILT_IN_FREE)
5692 return expand_call (exp, target, ignore);
5694 /* The built-in function expanders test for target == const0_rtx
5695 to determine whether the function's result will be ignored. */
5696 if (ignore)
5697 target = const0_rtx;
5699 /* If the result of a pure or const built-in function is ignored, and
5700 none of its arguments are volatile, we can avoid expanding the
5701 built-in call and just evaluate the arguments for side-effects. */
5702 if (target == const0_rtx
5703 && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl)))
5705 bool volatilep = false;
5706 tree arg;
5707 call_expr_arg_iterator iter;
5709 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5710 if (TREE_THIS_VOLATILE (arg))
5712 volatilep = true;
5713 break;
5716 if (! volatilep)
5718 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5719 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5720 return const0_rtx;
5724 switch (fcode)
5726 CASE_FLT_FN (BUILT_IN_FABS):
5727 target = expand_builtin_fabs (exp, target, subtarget);
5728 if (target)
5729 return target;
5730 break;
5732 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5733 target = expand_builtin_copysign (exp, target, subtarget);
5734 if (target)
5735 return target;
5736 break;
5738 /* Just do a normal library call if we were unable to fold
5739 the values. */
5740 CASE_FLT_FN (BUILT_IN_CABS):
5741 break;
5743 CASE_FLT_FN (BUILT_IN_EXP):
5744 CASE_FLT_FN (BUILT_IN_EXP10):
5745 CASE_FLT_FN (BUILT_IN_POW10):
5746 CASE_FLT_FN (BUILT_IN_EXP2):
5747 CASE_FLT_FN (BUILT_IN_EXPM1):
5748 CASE_FLT_FN (BUILT_IN_LOGB):
5749 CASE_FLT_FN (BUILT_IN_LOG):
5750 CASE_FLT_FN (BUILT_IN_LOG10):
5751 CASE_FLT_FN (BUILT_IN_LOG2):
5752 CASE_FLT_FN (BUILT_IN_LOG1P):
5753 CASE_FLT_FN (BUILT_IN_TAN):
5754 CASE_FLT_FN (BUILT_IN_ASIN):
5755 CASE_FLT_FN (BUILT_IN_ACOS):
5756 CASE_FLT_FN (BUILT_IN_ATAN):
5757 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5758 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5759 because of possible accuracy problems. */
5760 if (! flag_unsafe_math_optimizations)
5761 break;
5762 CASE_FLT_FN (BUILT_IN_SQRT):
5763 CASE_FLT_FN (BUILT_IN_FLOOR):
5764 CASE_FLT_FN (BUILT_IN_CEIL):
5765 CASE_FLT_FN (BUILT_IN_TRUNC):
5766 CASE_FLT_FN (BUILT_IN_ROUND):
5767 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5768 CASE_FLT_FN (BUILT_IN_RINT):
5769 target = expand_builtin_mathfn (exp, target, subtarget);
5770 if (target)
5771 return target;
5772 break;
5774 CASE_FLT_FN (BUILT_IN_ILOGB):
5775 if (! flag_unsafe_math_optimizations)
5776 break;
5777 CASE_FLT_FN (BUILT_IN_ISINF):
5778 CASE_FLT_FN (BUILT_IN_FINITE):
5779 case BUILT_IN_ISFINITE:
5780 case BUILT_IN_ISNORMAL:
5781 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
5782 if (target)
5783 return target;
5784 break;
5786 CASE_FLT_FN (BUILT_IN_LCEIL):
5787 CASE_FLT_FN (BUILT_IN_LLCEIL):
5788 CASE_FLT_FN (BUILT_IN_LFLOOR):
5789 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5790 target = expand_builtin_int_roundingfn (exp, target);
5791 if (target)
5792 return target;
5793 break;
5795 CASE_FLT_FN (BUILT_IN_LRINT):
5796 CASE_FLT_FN (BUILT_IN_LLRINT):
5797 CASE_FLT_FN (BUILT_IN_LROUND):
5798 CASE_FLT_FN (BUILT_IN_LLROUND):
5799 target = expand_builtin_int_roundingfn_2 (exp, target);
5800 if (target)
5801 return target;
5802 break;
5804 CASE_FLT_FN (BUILT_IN_POW):
5805 target = expand_builtin_pow (exp, target, subtarget);
5806 if (target)
5807 return target;
5808 break;
5810 CASE_FLT_FN (BUILT_IN_POWI):
5811 target = expand_builtin_powi (exp, target, subtarget);
5812 if (target)
5813 return target;
5814 break;
5816 CASE_FLT_FN (BUILT_IN_ATAN2):
5817 CASE_FLT_FN (BUILT_IN_LDEXP):
5818 CASE_FLT_FN (BUILT_IN_SCALB):
5819 CASE_FLT_FN (BUILT_IN_SCALBN):
5820 CASE_FLT_FN (BUILT_IN_SCALBLN):
5821 if (! flag_unsafe_math_optimizations)
5822 break;
5824 CASE_FLT_FN (BUILT_IN_FMOD):
5825 CASE_FLT_FN (BUILT_IN_REMAINDER):
5826 CASE_FLT_FN (BUILT_IN_DREM):
5827 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5828 if (target)
5829 return target;
5830 break;
5832 CASE_FLT_FN (BUILT_IN_CEXPI):
5833 target = expand_builtin_cexpi (exp, target, subtarget);
5834 gcc_assert (target);
5835 return target;
5837 CASE_FLT_FN (BUILT_IN_SIN):
5838 CASE_FLT_FN (BUILT_IN_COS):
5839 if (! flag_unsafe_math_optimizations)
5840 break;
5841 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5842 if (target)
5843 return target;
5844 break;
5846 CASE_FLT_FN (BUILT_IN_SINCOS):
5847 if (! flag_unsafe_math_optimizations)
5848 break;
5849 target = expand_builtin_sincos (exp);
5850 if (target)
5851 return target;
5852 break;
5854 case BUILT_IN_APPLY_ARGS:
5855 return expand_builtin_apply_args ();
5857 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5858 FUNCTION with a copy of the parameters described by
5859 ARGUMENTS, and ARGSIZE. It returns a block of memory
5860 allocated on the stack into which is stored all the registers
5861 that might possibly be used for returning the result of a
5862 function. ARGUMENTS is the value returned by
5863 __builtin_apply_args. ARGSIZE is the number of bytes of
5864 arguments that must be copied. ??? How should this value be
5865 computed? We'll also need a safe worst case value for varargs
5866 functions. */
5867 case BUILT_IN_APPLY:
5868 if (!validate_arglist (exp, POINTER_TYPE,
5869 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5870 && !validate_arglist (exp, REFERENCE_TYPE,
5871 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5872 return const0_rtx;
5873 else
5875 rtx ops[3];
5877 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5878 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5879 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5881 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5884 /* __builtin_return (RESULT) causes the function to return the
5885 value described by RESULT. RESULT is address of the block of
5886 memory returned by __builtin_apply. */
5887 case BUILT_IN_RETURN:
5888 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5889 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5890 return const0_rtx;
5892 case BUILT_IN_SAVEREGS:
5893 return expand_builtin_saveregs ();
5895 case BUILT_IN_VA_ARG_PACK:
5896 /* All valid uses of __builtin_va_arg_pack () are removed during
5897 inlining. */
5898 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5899 return const0_rtx;
5901 case BUILT_IN_VA_ARG_PACK_LEN:
5902 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5903 inlining. */
5904 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5905 return const0_rtx;
5907 /* Return the address of the first anonymous stack arg. */
5908 case BUILT_IN_NEXT_ARG:
5909 if (fold_builtin_next_arg (exp, false))
5910 return const0_rtx;
5911 return expand_builtin_next_arg ();
5913 case BUILT_IN_CLEAR_CACHE:
5914 target = expand_builtin___clear_cache (exp);
5915 if (target)
5916 return target;
5917 break;
5919 case BUILT_IN_CLASSIFY_TYPE:
5920 return expand_builtin_classify_type (exp);
5922 case BUILT_IN_CONSTANT_P:
5923 return const0_rtx;
5925 case BUILT_IN_FRAME_ADDRESS:
5926 case BUILT_IN_RETURN_ADDRESS:
5927 return expand_builtin_frame_address (fndecl, exp);
5929 /* Returns the address of the area where the structure is returned.
5930 0 otherwise. */
5931 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5932 if (call_expr_nargs (exp) != 0
5933 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5934 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5935 return const0_rtx;
5936 else
5937 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5939 case BUILT_IN_ALLOCA:
5940 target = expand_builtin_alloca (exp, target);
5941 if (target)
5942 return target;
5943 break;
5945 case BUILT_IN_STACK_SAVE:
5946 return expand_stack_save ();
5948 case BUILT_IN_STACK_RESTORE:
5949 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
5950 return const0_rtx;
5952 case BUILT_IN_BSWAP32:
5953 case BUILT_IN_BSWAP64:
5954 target = expand_builtin_bswap (exp, target, subtarget);
5956 if (target)
5957 return target;
5958 break;
5960 CASE_INT_FN (BUILT_IN_FFS):
5961 case BUILT_IN_FFSIMAX:
5962 target = expand_builtin_unop (target_mode, exp, target,
5963 subtarget, ffs_optab);
5964 if (target)
5965 return target;
5966 break;
5968 CASE_INT_FN (BUILT_IN_CLZ):
5969 case BUILT_IN_CLZIMAX:
5970 target = expand_builtin_unop (target_mode, exp, target,
5971 subtarget, clz_optab);
5972 if (target)
5973 return target;
5974 break;
5976 CASE_INT_FN (BUILT_IN_CTZ):
5977 case BUILT_IN_CTZIMAX:
5978 target = expand_builtin_unop (target_mode, exp, target,
5979 subtarget, ctz_optab);
5980 if (target)
5981 return target;
5982 break;
5984 CASE_INT_FN (BUILT_IN_POPCOUNT):
5985 case BUILT_IN_POPCOUNTIMAX:
5986 target = expand_builtin_unop (target_mode, exp, target,
5987 subtarget, popcount_optab);
5988 if (target)
5989 return target;
5990 break;
5992 CASE_INT_FN (BUILT_IN_PARITY):
5993 case BUILT_IN_PARITYIMAX:
5994 target = expand_builtin_unop (target_mode, exp, target,
5995 subtarget, parity_optab);
5996 if (target)
5997 return target;
5998 break;
6000 case BUILT_IN_STRLEN:
6001 target = expand_builtin_strlen (exp, target, target_mode);
6002 if (target)
6003 return target;
6004 break;
6006 case BUILT_IN_STRCPY:
6007 target = expand_builtin_strcpy (exp, target);
6008 if (target)
6009 return target;
6010 break;
6012 case BUILT_IN_STRNCPY:
6013 target = expand_builtin_strncpy (exp, target);
6014 if (target)
6015 return target;
6016 break;
6018 case BUILT_IN_STPCPY:
6019 target = expand_builtin_stpcpy (exp, target, mode);
6020 if (target)
6021 return target;
6022 break;
6024 case BUILT_IN_MEMCPY:
6025 target = expand_builtin_memcpy (exp, target);
6026 if (target)
6027 return target;
6028 break;
6030 case BUILT_IN_MEMPCPY:
6031 target = expand_builtin_mempcpy (exp, target, mode);
6032 if (target)
6033 return target;
6034 break;
6036 case BUILT_IN_MEMSET:
6037 target = expand_builtin_memset (exp, target, mode);
6038 if (target)
6039 return target;
6040 break;
6042 case BUILT_IN_BZERO:
6043 target = expand_builtin_bzero (exp);
6044 if (target)
6045 return target;
6046 break;
6048 case BUILT_IN_STRCMP:
6049 target = expand_builtin_strcmp (exp, target);
6050 if (target)
6051 return target;
6052 break;
6054 case BUILT_IN_STRNCMP:
6055 target = expand_builtin_strncmp (exp, target, mode);
6056 if (target)
6057 return target;
6058 break;
6060 case BUILT_IN_BCMP:
6061 case BUILT_IN_MEMCMP:
6062 target = expand_builtin_memcmp (exp, target, mode);
6063 if (target)
6064 return target;
6065 break;
6067 case BUILT_IN_SETJMP:
6068 /* This should have been lowered to the builtins below. */
6069 gcc_unreachable ();
6071 case BUILT_IN_SETJMP_SETUP:
6072 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6073 and the receiver label. */
6074 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6076 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6077 VOIDmode, EXPAND_NORMAL);
6078 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6079 rtx label_r = label_rtx (label);
6081 /* This is copied from the handling of non-local gotos. */
6082 expand_builtin_setjmp_setup (buf_addr, label_r);
6083 nonlocal_goto_handler_labels
6084 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6085 nonlocal_goto_handler_labels);
6086 /* ??? Do not let expand_label treat us as such since we would
6087 not want to be both on the list of non-local labels and on
6088 the list of forced labels. */
6089 FORCED_LABEL (label) = 0;
6090 return const0_rtx;
6092 break;
6094 case BUILT_IN_SETJMP_DISPATCHER:
6095 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6096 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6098 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6099 rtx label_r = label_rtx (label);
6101 /* Remove the dispatcher label from the list of non-local labels
6102 since the receiver labels have been added to it above. */
6103 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6104 return const0_rtx;
6106 break;
6108 case BUILT_IN_SETJMP_RECEIVER:
6109 /* __builtin_setjmp_receiver is passed the receiver label. */
6110 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6112 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6113 rtx label_r = label_rtx (label);
6115 expand_builtin_setjmp_receiver (label_r);
6116 return const0_rtx;
6118 break;
6120 /* __builtin_longjmp is passed a pointer to an array of five words.
6121 It's similar to the C library longjmp function but works with
6122 __builtin_setjmp above. */
6123 case BUILT_IN_LONGJMP:
6124 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6126 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6127 VOIDmode, EXPAND_NORMAL);
6128 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6130 if (value != const1_rtx)
6132 error ("%<__builtin_longjmp%> second argument must be 1");
6133 return const0_rtx;
6136 expand_builtin_longjmp (buf_addr, value);
6137 return const0_rtx;
6139 break;
6141 case BUILT_IN_NONLOCAL_GOTO:
6142 target = expand_builtin_nonlocal_goto (exp);
6143 if (target)
6144 return target;
6145 break;
6147 /* This updates the setjmp buffer that is its argument with the value
6148 of the current stack pointer. */
6149 case BUILT_IN_UPDATE_SETJMP_BUF:
6150 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6152 rtx buf_addr
6153 = expand_normal (CALL_EXPR_ARG (exp, 0));
6155 expand_builtin_update_setjmp_buf (buf_addr);
6156 return const0_rtx;
6158 break;
6160 case BUILT_IN_TRAP:
6161 expand_builtin_trap ();
6162 return const0_rtx;
6164 case BUILT_IN_UNREACHABLE:
6165 expand_builtin_unreachable ();
6166 return const0_rtx;
6168 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6169 case BUILT_IN_SIGNBITD32:
6170 case BUILT_IN_SIGNBITD64:
6171 case BUILT_IN_SIGNBITD128:
6172 target = expand_builtin_signbit (exp, target);
6173 if (target)
6174 return target;
6175 break;
6177 /* Various hooks for the DWARF 2 __throw routine. */
6178 case BUILT_IN_UNWIND_INIT:
6179 expand_builtin_unwind_init ();
6180 return const0_rtx;
6181 case BUILT_IN_DWARF_CFA:
6182 return virtual_cfa_rtx;
6183 #ifdef DWARF2_UNWIND_INFO
6184 case BUILT_IN_DWARF_SP_COLUMN:
6185 return expand_builtin_dwarf_sp_column ();
6186 case BUILT_IN_INIT_DWARF_REG_SIZES:
6187 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6188 return const0_rtx;
6189 #endif
6190 case BUILT_IN_FROB_RETURN_ADDR:
6191 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6192 case BUILT_IN_EXTRACT_RETURN_ADDR:
6193 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6194 case BUILT_IN_EH_RETURN:
6195 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6196 CALL_EXPR_ARG (exp, 1));
6197 return const0_rtx;
6198 #ifdef EH_RETURN_DATA_REGNO
6199 case BUILT_IN_EH_RETURN_DATA_REGNO:
6200 return expand_builtin_eh_return_data_regno (exp);
6201 #endif
6202 case BUILT_IN_EXTEND_POINTER:
6203 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6204 case BUILT_IN_EH_POINTER:
6205 return expand_builtin_eh_pointer (exp);
6206 case BUILT_IN_EH_FILTER:
6207 return expand_builtin_eh_filter (exp);
6208 case BUILT_IN_EH_COPY_VALUES:
6209 return expand_builtin_eh_copy_values (exp);
6211 case BUILT_IN_VA_START:
6212 return expand_builtin_va_start (exp);
6213 case BUILT_IN_VA_END:
6214 return expand_builtin_va_end (exp);
6215 case BUILT_IN_VA_COPY:
6216 return expand_builtin_va_copy (exp);
6217 case BUILT_IN_EXPECT:
6218 return expand_builtin_expect (exp, target);
6219 case BUILT_IN_PREFETCH:
6220 expand_builtin_prefetch (exp);
6221 return const0_rtx;
6223 case BUILT_IN_PROFILE_FUNC_ENTER:
6224 return expand_builtin_profile_func (false);
6225 case BUILT_IN_PROFILE_FUNC_EXIT:
6226 return expand_builtin_profile_func (true);
6228 case BUILT_IN_INIT_TRAMPOLINE:
6229 return expand_builtin_init_trampoline (exp);
6230 case BUILT_IN_ADJUST_TRAMPOLINE:
6231 return expand_builtin_adjust_trampoline (exp);
6233 case BUILT_IN_FORK:
6234 case BUILT_IN_EXECL:
6235 case BUILT_IN_EXECV:
6236 case BUILT_IN_EXECLP:
6237 case BUILT_IN_EXECLE:
6238 case BUILT_IN_EXECVP:
6239 case BUILT_IN_EXECVE:
6240 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6241 if (target)
6242 return target;
6243 break;
6245 case BUILT_IN_FETCH_AND_ADD_1:
6246 case BUILT_IN_FETCH_AND_ADD_2:
6247 case BUILT_IN_FETCH_AND_ADD_4:
6248 case BUILT_IN_FETCH_AND_ADD_8:
6249 case BUILT_IN_FETCH_AND_ADD_16:
6250 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6251 target = expand_builtin_sync_operation (mode, exp, PLUS,
6252 false, target, ignore);
6253 if (target)
6254 return target;
6255 break;
6257 case BUILT_IN_FETCH_AND_SUB_1:
6258 case BUILT_IN_FETCH_AND_SUB_2:
6259 case BUILT_IN_FETCH_AND_SUB_4:
6260 case BUILT_IN_FETCH_AND_SUB_8:
6261 case BUILT_IN_FETCH_AND_SUB_16:
6262 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6263 target = expand_builtin_sync_operation (mode, exp, MINUS,
6264 false, target, ignore);
6265 if (target)
6266 return target;
6267 break;
6269 case BUILT_IN_FETCH_AND_OR_1:
6270 case BUILT_IN_FETCH_AND_OR_2:
6271 case BUILT_IN_FETCH_AND_OR_4:
6272 case BUILT_IN_FETCH_AND_OR_8:
6273 case BUILT_IN_FETCH_AND_OR_16:
6274 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6275 target = expand_builtin_sync_operation (mode, exp, IOR,
6276 false, target, ignore);
6277 if (target)
6278 return target;
6279 break;
6281 case BUILT_IN_FETCH_AND_AND_1:
6282 case BUILT_IN_FETCH_AND_AND_2:
6283 case BUILT_IN_FETCH_AND_AND_4:
6284 case BUILT_IN_FETCH_AND_AND_8:
6285 case BUILT_IN_FETCH_AND_AND_16:
6286 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6287 target = expand_builtin_sync_operation (mode, exp, AND,
6288 false, target, ignore);
6289 if (target)
6290 return target;
6291 break;
6293 case BUILT_IN_FETCH_AND_XOR_1:
6294 case BUILT_IN_FETCH_AND_XOR_2:
6295 case BUILT_IN_FETCH_AND_XOR_4:
6296 case BUILT_IN_FETCH_AND_XOR_8:
6297 case BUILT_IN_FETCH_AND_XOR_16:
6298 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6299 target = expand_builtin_sync_operation (mode, exp, XOR,
6300 false, target, ignore);
6301 if (target)
6302 return target;
6303 break;
6305 case BUILT_IN_FETCH_AND_NAND_1:
6306 case BUILT_IN_FETCH_AND_NAND_2:
6307 case BUILT_IN_FETCH_AND_NAND_4:
6308 case BUILT_IN_FETCH_AND_NAND_8:
6309 case BUILT_IN_FETCH_AND_NAND_16:
6310 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6311 target = expand_builtin_sync_operation (mode, exp, NOT,
6312 false, target, ignore);
6313 if (target)
6314 return target;
6315 break;
6317 case BUILT_IN_ADD_AND_FETCH_1:
6318 case BUILT_IN_ADD_AND_FETCH_2:
6319 case BUILT_IN_ADD_AND_FETCH_4:
6320 case BUILT_IN_ADD_AND_FETCH_8:
6321 case BUILT_IN_ADD_AND_FETCH_16:
6322 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6323 target = expand_builtin_sync_operation (mode, exp, PLUS,
6324 true, target, ignore);
6325 if (target)
6326 return target;
6327 break;
6329 case BUILT_IN_SUB_AND_FETCH_1:
6330 case BUILT_IN_SUB_AND_FETCH_2:
6331 case BUILT_IN_SUB_AND_FETCH_4:
6332 case BUILT_IN_SUB_AND_FETCH_8:
6333 case BUILT_IN_SUB_AND_FETCH_16:
6334 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6335 target = expand_builtin_sync_operation (mode, exp, MINUS,
6336 true, target, ignore);
6337 if (target)
6338 return target;
6339 break;
6341 case BUILT_IN_OR_AND_FETCH_1:
6342 case BUILT_IN_OR_AND_FETCH_2:
6343 case BUILT_IN_OR_AND_FETCH_4:
6344 case BUILT_IN_OR_AND_FETCH_8:
6345 case BUILT_IN_OR_AND_FETCH_16:
6346 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6347 target = expand_builtin_sync_operation (mode, exp, IOR,
6348 true, target, ignore);
6349 if (target)
6350 return target;
6351 break;
6353 case BUILT_IN_AND_AND_FETCH_1:
6354 case BUILT_IN_AND_AND_FETCH_2:
6355 case BUILT_IN_AND_AND_FETCH_4:
6356 case BUILT_IN_AND_AND_FETCH_8:
6357 case BUILT_IN_AND_AND_FETCH_16:
6358 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6359 target = expand_builtin_sync_operation (mode, exp, AND,
6360 true, target, ignore);
6361 if (target)
6362 return target;
6363 break;
6365 case BUILT_IN_XOR_AND_FETCH_1:
6366 case BUILT_IN_XOR_AND_FETCH_2:
6367 case BUILT_IN_XOR_AND_FETCH_4:
6368 case BUILT_IN_XOR_AND_FETCH_8:
6369 case BUILT_IN_XOR_AND_FETCH_16:
6370 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6371 target = expand_builtin_sync_operation (mode, exp, XOR,
6372 true, target, ignore);
6373 if (target)
6374 return target;
6375 break;
6377 case BUILT_IN_NAND_AND_FETCH_1:
6378 case BUILT_IN_NAND_AND_FETCH_2:
6379 case BUILT_IN_NAND_AND_FETCH_4:
6380 case BUILT_IN_NAND_AND_FETCH_8:
6381 case BUILT_IN_NAND_AND_FETCH_16:
6382 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6383 target = expand_builtin_sync_operation (mode, exp, NOT,
6384 true, target, ignore);
6385 if (target)
6386 return target;
6387 break;
6389 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6390 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6391 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6392 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6393 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6394 if (mode == VOIDmode)
6395 mode = TYPE_MODE (boolean_type_node);
6396 if (!target || !register_operand (target, mode))
6397 target = gen_reg_rtx (mode);
6399 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6400 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6401 if (target)
6402 return target;
6403 break;
6405 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6406 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6407 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6408 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6409 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6410 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6411 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6412 if (target)
6413 return target;
6414 break;
6416 case BUILT_IN_LOCK_TEST_AND_SET_1:
6417 case BUILT_IN_LOCK_TEST_AND_SET_2:
6418 case BUILT_IN_LOCK_TEST_AND_SET_4:
6419 case BUILT_IN_LOCK_TEST_AND_SET_8:
6420 case BUILT_IN_LOCK_TEST_AND_SET_16:
6421 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6422 target = expand_builtin_lock_test_and_set (mode, exp, target);
6423 if (target)
6424 return target;
6425 break;
6427 case BUILT_IN_LOCK_RELEASE_1:
6428 case BUILT_IN_LOCK_RELEASE_2:
6429 case BUILT_IN_LOCK_RELEASE_4:
6430 case BUILT_IN_LOCK_RELEASE_8:
6431 case BUILT_IN_LOCK_RELEASE_16:
6432 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6433 expand_builtin_lock_release (mode, exp);
6434 return const0_rtx;
6436 case BUILT_IN_SYNCHRONIZE:
6437 expand_builtin_synchronize ();
6438 return const0_rtx;
6440 case BUILT_IN_OBJECT_SIZE:
6441 return expand_builtin_object_size (exp);
6443 case BUILT_IN_MEMCPY_CHK:
6444 case BUILT_IN_MEMPCPY_CHK:
6445 case BUILT_IN_MEMMOVE_CHK:
6446 case BUILT_IN_MEMSET_CHK:
6447 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6448 if (target)
6449 return target;
6450 break;
6452 case BUILT_IN_STRCPY_CHK:
6453 case BUILT_IN_STPCPY_CHK:
6454 case BUILT_IN_STRNCPY_CHK:
6455 case BUILT_IN_STRCAT_CHK:
6456 case BUILT_IN_STRNCAT_CHK:
6457 case BUILT_IN_SNPRINTF_CHK:
6458 case BUILT_IN_VSNPRINTF_CHK:
6459 maybe_emit_chk_warning (exp, fcode);
6460 break;
6462 case BUILT_IN_SPRINTF_CHK:
6463 case BUILT_IN_VSPRINTF_CHK:
6464 maybe_emit_sprintf_chk_warning (exp, fcode);
6465 break;
6467 case BUILT_IN_FREE:
6468 maybe_emit_free_warning (exp);
6469 break;
6471 default: /* just do library call, if unknown builtin */
6472 break;
6475 /* The switch statement above can drop through to cause the function
6476 to be called normally. */
6477 return expand_call (exp, target, ignore);
6480 /* Determine whether a tree node represents a call to a built-in
6481 function. If the tree T is a call to a built-in function with
6482 the right number of arguments of the appropriate types, return
6483 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6484 Otherwise the return value is END_BUILTINS. */
6486 enum built_in_function
6487 builtin_mathfn_code (const_tree t)
6489 const_tree fndecl, arg, parmlist;
6490 const_tree argtype, parmtype;
6491 const_call_expr_arg_iterator iter;
6493 if (TREE_CODE (t) != CALL_EXPR
6494 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6495 return END_BUILTINS;
6497 fndecl = get_callee_fndecl (t);
6498 if (fndecl == NULL_TREE
6499 || TREE_CODE (fndecl) != FUNCTION_DECL
6500 || ! DECL_BUILT_IN (fndecl)
6501 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6502 return END_BUILTINS;
6504 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6505 init_const_call_expr_arg_iterator (t, &iter);
6506 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6508 /* If a function doesn't take a variable number of arguments,
6509 the last element in the list will have type `void'. */
6510 parmtype = TREE_VALUE (parmlist);
6511 if (VOID_TYPE_P (parmtype))
6513 if (more_const_call_expr_args_p (&iter))
6514 return END_BUILTINS;
6515 return DECL_FUNCTION_CODE (fndecl);
6518 if (! more_const_call_expr_args_p (&iter))
6519 return END_BUILTINS;
6521 arg = next_const_call_expr_arg (&iter);
6522 argtype = TREE_TYPE (arg);
6524 if (SCALAR_FLOAT_TYPE_P (parmtype))
6526 if (! SCALAR_FLOAT_TYPE_P (argtype))
6527 return END_BUILTINS;
6529 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6531 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6532 return END_BUILTINS;
6534 else if (POINTER_TYPE_P (parmtype))
6536 if (! POINTER_TYPE_P (argtype))
6537 return END_BUILTINS;
6539 else if (INTEGRAL_TYPE_P (parmtype))
6541 if (! INTEGRAL_TYPE_P (argtype))
6542 return END_BUILTINS;
6544 else
6545 return END_BUILTINS;
6548 /* Variable-length argument list. */
6549 return DECL_FUNCTION_CODE (fndecl);
6552 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6553 evaluate to a constant. */
6555 static tree
6556 fold_builtin_constant_p (tree arg)
6558 /* We return 1 for a numeric type that's known to be a constant
6559 value at compile-time or for an aggregate type that's a
6560 literal constant. */
6561 STRIP_NOPS (arg);
6563 /* If we know this is a constant, emit the constant of one. */
6564 if (CONSTANT_CLASS_P (arg)
6565 || (TREE_CODE (arg) == CONSTRUCTOR
6566 && TREE_CONSTANT (arg)))
6567 return integer_one_node;
6568 if (TREE_CODE (arg) == ADDR_EXPR)
6570 tree op = TREE_OPERAND (arg, 0);
6571 if (TREE_CODE (op) == STRING_CST
6572 || (TREE_CODE (op) == ARRAY_REF
6573 && integer_zerop (TREE_OPERAND (op, 1))
6574 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6575 return integer_one_node;
6578 /* If this expression has side effects, show we don't know it to be a
6579 constant. Likewise if it's a pointer or aggregate type since in
6580 those case we only want literals, since those are only optimized
6581 when generating RTL, not later.
6582 And finally, if we are compiling an initializer, not code, we
6583 need to return a definite result now; there's not going to be any
6584 more optimization done. */
6585 if (TREE_SIDE_EFFECTS (arg)
6586 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6587 || POINTER_TYPE_P (TREE_TYPE (arg))
6588 || cfun == 0
6589 || folding_initializer)
6590 return integer_zero_node;
6592 return NULL_TREE;
6595 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6596 return it as a truthvalue. */
6598 static tree
6599 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6601 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6603 fn = built_in_decls[BUILT_IN_EXPECT];
6604 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6605 ret_type = TREE_TYPE (TREE_TYPE (fn));
6606 pred_type = TREE_VALUE (arg_types);
6607 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6609 pred = fold_convert_loc (loc, pred_type, pred);
6610 expected = fold_convert_loc (loc, expected_type, expected);
6611 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6613 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6614 build_int_cst (ret_type, 0));
6617 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6618 NULL_TREE if no simplification is possible. */
6620 static tree
6621 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6623 tree inner, fndecl;
6624 enum tree_code code;
6626 /* If this is a builtin_expect within a builtin_expect keep the
6627 inner one. See through a comparison against a constant. It
6628 might have been added to create a thruthvalue. */
6629 inner = arg0;
6630 if (COMPARISON_CLASS_P (inner)
6631 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6632 inner = TREE_OPERAND (inner, 0);
6634 if (TREE_CODE (inner) == CALL_EXPR
6635 && (fndecl = get_callee_fndecl (inner))
6636 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6637 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6638 return arg0;
6640 /* Distribute the expected value over short-circuiting operators.
6641 See through the cast from truthvalue_type_node to long. */
6642 inner = arg0;
6643 while (TREE_CODE (inner) == NOP_EXPR
6644 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6645 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6646 inner = TREE_OPERAND (inner, 0);
6648 code = TREE_CODE (inner);
6649 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6651 tree op0 = TREE_OPERAND (inner, 0);
6652 tree op1 = TREE_OPERAND (inner, 1);
6654 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6655 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6656 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6658 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6661 /* If the argument isn't invariant then there's nothing else we can do. */
6662 if (!TREE_CONSTANT (arg0))
6663 return NULL_TREE;
6665 /* If we expect that a comparison against the argument will fold to
6666 a constant return the constant. In practice, this means a true
6667 constant or the address of a non-weak symbol. */
6668 inner = arg0;
6669 STRIP_NOPS (inner);
6670 if (TREE_CODE (inner) == ADDR_EXPR)
6674 inner = TREE_OPERAND (inner, 0);
6676 while (TREE_CODE (inner) == COMPONENT_REF
6677 || TREE_CODE (inner) == ARRAY_REF);
6678 if ((TREE_CODE (inner) == VAR_DECL
6679 || TREE_CODE (inner) == FUNCTION_DECL)
6680 && DECL_WEAK (inner))
6681 return NULL_TREE;
6684 /* Otherwise, ARG0 already has the proper type for the return value. */
6685 return arg0;
6688 /* Fold a call to __builtin_classify_type with argument ARG. */
6690 static tree
6691 fold_builtin_classify_type (tree arg)
6693 if (arg == 0)
6694 return build_int_cst (NULL_TREE, no_type_class);
6696 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
6699 /* Fold a call to __builtin_strlen with argument ARG. */
6701 static tree
6702 fold_builtin_strlen (location_t loc, tree type, tree arg)
6704 if (!validate_arg (arg, POINTER_TYPE))
6705 return NULL_TREE;
6706 else
6708 tree len = c_strlen (arg, 0);
6710 if (len)
6711 return fold_convert_loc (loc, type, len);
6713 return NULL_TREE;
6717 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6719 static tree
6720 fold_builtin_inf (location_t loc, tree type, int warn)
6722 REAL_VALUE_TYPE real;
6724 /* __builtin_inff is intended to be usable to define INFINITY on all
6725 targets. If an infinity is not available, INFINITY expands "to a
6726 positive constant of type float that overflows at translation
6727 time", footnote "In this case, using INFINITY will violate the
6728 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6729 Thus we pedwarn to ensure this constraint violation is
6730 diagnosed. */
6731 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6732 pedwarn (loc, 0, "target format does not support infinity");
6734 real_inf (&real);
6735 return build_real (type, real);
6738 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6740 static tree
6741 fold_builtin_nan (tree arg, tree type, int quiet)
6743 REAL_VALUE_TYPE real;
6744 const char *str;
6746 if (!validate_arg (arg, POINTER_TYPE))
6747 return NULL_TREE;
6748 str = c_getstr (arg);
6749 if (!str)
6750 return NULL_TREE;
6752 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6753 return NULL_TREE;
6755 return build_real (type, real);
6758 /* Return true if the floating point expression T has an integer value.
6759 We also allow +Inf, -Inf and NaN to be considered integer values. */
6761 static bool
6762 integer_valued_real_p (tree t)
6764 switch (TREE_CODE (t))
6766 case FLOAT_EXPR:
6767 return true;
6769 case ABS_EXPR:
6770 case SAVE_EXPR:
6771 return integer_valued_real_p (TREE_OPERAND (t, 0));
6773 case COMPOUND_EXPR:
6774 case MODIFY_EXPR:
6775 case BIND_EXPR:
6776 return integer_valued_real_p (TREE_OPERAND (t, 1));
6778 case PLUS_EXPR:
6779 case MINUS_EXPR:
6780 case MULT_EXPR:
6781 case MIN_EXPR:
6782 case MAX_EXPR:
6783 return integer_valued_real_p (TREE_OPERAND (t, 0))
6784 && integer_valued_real_p (TREE_OPERAND (t, 1));
6786 case COND_EXPR:
6787 return integer_valued_real_p (TREE_OPERAND (t, 1))
6788 && integer_valued_real_p (TREE_OPERAND (t, 2));
6790 case REAL_CST:
6791 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6793 case NOP_EXPR:
6795 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6796 if (TREE_CODE (type) == INTEGER_TYPE)
6797 return true;
6798 if (TREE_CODE (type) == REAL_TYPE)
6799 return integer_valued_real_p (TREE_OPERAND (t, 0));
6800 break;
6803 case CALL_EXPR:
6804 switch (builtin_mathfn_code (t))
6806 CASE_FLT_FN (BUILT_IN_CEIL):
6807 CASE_FLT_FN (BUILT_IN_FLOOR):
6808 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6809 CASE_FLT_FN (BUILT_IN_RINT):
6810 CASE_FLT_FN (BUILT_IN_ROUND):
6811 CASE_FLT_FN (BUILT_IN_TRUNC):
6812 return true;
6814 CASE_FLT_FN (BUILT_IN_FMIN):
6815 CASE_FLT_FN (BUILT_IN_FMAX):
6816 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6817 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6819 default:
6820 break;
6822 break;
6824 default:
6825 break;
6827 return false;
6830 /* FNDECL is assumed to be a builtin where truncation can be propagated
6831 across (for instance floor((double)f) == (double)floorf (f).
6832 Do the transformation for a call with argument ARG. */
6834 static tree
6835 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6837 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6839 if (!validate_arg (arg, REAL_TYPE))
6840 return NULL_TREE;
6842 /* Integer rounding functions are idempotent. */
6843 if (fcode == builtin_mathfn_code (arg))
6844 return arg;
6846 /* If argument is already integer valued, and we don't need to worry
6847 about setting errno, there's no need to perform rounding. */
6848 if (! flag_errno_math && integer_valued_real_p (arg))
6849 return arg;
6851 if (optimize)
6853 tree arg0 = strip_float_extensions (arg);
6854 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6855 tree newtype = TREE_TYPE (arg0);
6856 tree decl;
6858 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6859 && (decl = mathfn_built_in (newtype, fcode)))
6860 return fold_convert_loc (loc, ftype,
6861 build_call_expr_loc (loc, decl, 1,
6862 fold_convert_loc (loc,
6863 newtype,
6864 arg0)));
6866 return NULL_TREE;
6869 /* FNDECL is assumed to be builtin which can narrow the FP type of
6870 the argument, for instance lround((double)f) -> lroundf (f).
6871 Do the transformation for a call with argument ARG. */
6873 static tree
6874 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
6876 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6878 if (!validate_arg (arg, REAL_TYPE))
6879 return NULL_TREE;
6881 /* If argument is already integer valued, and we don't need to worry
6882 about setting errno, there's no need to perform rounding. */
6883 if (! flag_errno_math && integer_valued_real_p (arg))
6884 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
6885 TREE_TYPE (TREE_TYPE (fndecl)), arg);
6887 if (optimize)
6889 tree ftype = TREE_TYPE (arg);
6890 tree arg0 = strip_float_extensions (arg);
6891 tree newtype = TREE_TYPE (arg0);
6892 tree decl;
6894 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6895 && (decl = mathfn_built_in (newtype, fcode)))
6896 return build_call_expr_loc (loc, decl, 1,
6897 fold_convert_loc (loc, newtype, arg0));
6900 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6901 sizeof (long long) == sizeof (long). */
6902 if (TYPE_PRECISION (long_long_integer_type_node)
6903 == TYPE_PRECISION (long_integer_type_node))
6905 tree newfn = NULL_TREE;
6906 switch (fcode)
6908 CASE_FLT_FN (BUILT_IN_LLCEIL):
6909 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6910 break;
6912 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6913 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6914 break;
6916 CASE_FLT_FN (BUILT_IN_LLROUND):
6917 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6918 break;
6920 CASE_FLT_FN (BUILT_IN_LLRINT):
6921 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6922 break;
6924 default:
6925 break;
6928 if (newfn)
6930 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
6931 return fold_convert_loc (loc,
6932 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6936 return NULL_TREE;
6939 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
6940 return type. Return NULL_TREE if no simplification can be made. */
6942 static tree
6943 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
6945 tree res;
6947 if (!validate_arg (arg, COMPLEX_TYPE)
6948 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6949 return NULL_TREE;
6951 /* Calculate the result when the argument is a constant. */
6952 if (TREE_CODE (arg) == COMPLEX_CST
6953 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
6954 type, mpfr_hypot)))
6955 return res;
6957 if (TREE_CODE (arg) == COMPLEX_EXPR)
6959 tree real = TREE_OPERAND (arg, 0);
6960 tree imag = TREE_OPERAND (arg, 1);
6962 /* If either part is zero, cabs is fabs of the other. */
6963 if (real_zerop (real))
6964 return fold_build1_loc (loc, ABS_EXPR, type, imag);
6965 if (real_zerop (imag))
6966 return fold_build1_loc (loc, ABS_EXPR, type, real);
6968 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6969 if (flag_unsafe_math_optimizations
6970 && operand_equal_p (real, imag, OEP_PURE_SAME))
6972 const REAL_VALUE_TYPE sqrt2_trunc
6973 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
6974 STRIP_NOPS (real);
6975 return fold_build2_loc (loc, MULT_EXPR, type,
6976 fold_build1_loc (loc, ABS_EXPR, type, real),
6977 build_real (type, sqrt2_trunc));
6981 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6982 if (TREE_CODE (arg) == NEGATE_EXPR
6983 || TREE_CODE (arg) == CONJ_EXPR)
6984 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
6986 /* Don't do this when optimizing for size. */
6987 if (flag_unsafe_math_optimizations
6988 && optimize && optimize_function_for_speed_p (cfun))
6990 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6992 if (sqrtfn != NULL_TREE)
6994 tree rpart, ipart, result;
6996 arg = builtin_save_expr (arg);
6998 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
6999 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7001 rpart = builtin_save_expr (rpart);
7002 ipart = builtin_save_expr (ipart);
7004 result = fold_build2_loc (loc, PLUS_EXPR, type,
7005 fold_build2_loc (loc, MULT_EXPR, type,
7006 rpart, rpart),
7007 fold_build2_loc (loc, MULT_EXPR, type,
7008 ipart, ipart));
7010 return build_call_expr_loc (loc, sqrtfn, 1, result);
7014 return NULL_TREE;
7017 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7018 complex tree type of the result. If NEG is true, the imaginary
7019 zero is negative. */
7021 static tree
7022 build_complex_cproj (tree type, bool neg)
7024 REAL_VALUE_TYPE rinf, rzero = dconst0;
7026 real_inf (&rinf);
7027 rzero.sign = neg;
7028 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7029 build_real (TREE_TYPE (type), rzero));
7032 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7033 return type. Return NULL_TREE if no simplification can be made. */
7035 static tree
7036 fold_builtin_cproj (location_t loc, tree arg, tree type)
7038 if (!validate_arg (arg, COMPLEX_TYPE)
7039 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7040 return NULL_TREE;
7042 /* If there are no infinities, return arg. */
7043 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7044 return non_lvalue_loc (loc, arg);
7046 /* Calculate the result when the argument is a constant. */
7047 if (TREE_CODE (arg) == COMPLEX_CST)
7049 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7050 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7052 if (real_isinf (real) || real_isinf (imag))
7053 return build_complex_cproj (type, imag->sign);
7054 else
7055 return arg;
7057 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7059 tree real = TREE_OPERAND (arg, 0);
7060 tree imag = TREE_OPERAND (arg, 1);
7062 STRIP_NOPS (real);
7063 STRIP_NOPS (imag);
7065 /* If the real part is inf and the imag part is known to be
7066 nonnegative, return (inf + 0i). Remember side-effects are
7067 possible in the imag part. */
7068 if (TREE_CODE (real) == REAL_CST
7069 && real_isinf (TREE_REAL_CST_PTR (real))
7070 && tree_expr_nonnegative_p (imag))
7071 return omit_one_operand_loc (loc, type,
7072 build_complex_cproj (type, false),
7073 arg);
7075 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7076 Remember side-effects are possible in the real part. */
7077 if (TREE_CODE (imag) == REAL_CST
7078 && real_isinf (TREE_REAL_CST_PTR (imag)))
7079 return
7080 omit_one_operand_loc (loc, type,
7081 build_complex_cproj (type, TREE_REAL_CST_PTR
7082 (imag)->sign), arg);
7085 return NULL_TREE;
7088 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7089 Return NULL_TREE if no simplification can be made. */
7091 static tree
7092 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7095 enum built_in_function fcode;
7096 tree res;
7098 if (!validate_arg (arg, REAL_TYPE))
7099 return NULL_TREE;
7101 /* Calculate the result when the argument is a constant. */
7102 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7103 return res;
7105 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7106 fcode = builtin_mathfn_code (arg);
7107 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7109 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7110 arg = fold_build2_loc (loc, MULT_EXPR, type,
7111 CALL_EXPR_ARG (arg, 0),
7112 build_real (type, dconsthalf));
7113 return build_call_expr_loc (loc, expfn, 1, arg);
7116 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7117 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7119 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7121 if (powfn)
7123 tree arg0 = CALL_EXPR_ARG (arg, 0);
7124 tree tree_root;
7125 /* The inner root was either sqrt or cbrt. */
7126 /* This was a conditional expression but it triggered a bug
7127 in Sun C 5.5. */
7128 REAL_VALUE_TYPE dconstroot;
7129 if (BUILTIN_SQRT_P (fcode))
7130 dconstroot = dconsthalf;
7131 else
7132 dconstroot = dconst_third ();
7134 /* Adjust for the outer root. */
7135 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7136 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7137 tree_root = build_real (type, dconstroot);
7138 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7142 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7143 if (flag_unsafe_math_optimizations
7144 && (fcode == BUILT_IN_POW
7145 || fcode == BUILT_IN_POWF
7146 || fcode == BUILT_IN_POWL))
7148 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7149 tree arg0 = CALL_EXPR_ARG (arg, 0);
7150 tree arg1 = CALL_EXPR_ARG (arg, 1);
7151 tree narg1;
7152 if (!tree_expr_nonnegative_p (arg0))
7153 arg0 = build1 (ABS_EXPR, type, arg0);
7154 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7155 build_real (type, dconsthalf));
7156 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7159 return NULL_TREE;
7162 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7163 Return NULL_TREE if no simplification can be made. */
7165 static tree
7166 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7168 const enum built_in_function fcode = builtin_mathfn_code (arg);
7169 tree res;
7171 if (!validate_arg (arg, REAL_TYPE))
7172 return NULL_TREE;
7174 /* Calculate the result when the argument is a constant. */
7175 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7176 return res;
7178 if (flag_unsafe_math_optimizations)
7180 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7181 if (BUILTIN_EXPONENT_P (fcode))
7183 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7184 const REAL_VALUE_TYPE third_trunc =
7185 real_value_truncate (TYPE_MODE (type), dconst_third ());
7186 arg = fold_build2_loc (loc, MULT_EXPR, type,
7187 CALL_EXPR_ARG (arg, 0),
7188 build_real (type, third_trunc));
7189 return build_call_expr_loc (loc, expfn, 1, arg);
7192 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7193 if (BUILTIN_SQRT_P (fcode))
7195 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7197 if (powfn)
7199 tree arg0 = CALL_EXPR_ARG (arg, 0);
7200 tree tree_root;
7201 REAL_VALUE_TYPE dconstroot = dconst_third ();
7203 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7204 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7205 tree_root = build_real (type, dconstroot);
7206 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7210 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7211 if (BUILTIN_CBRT_P (fcode))
7213 tree arg0 = CALL_EXPR_ARG (arg, 0);
7214 if (tree_expr_nonnegative_p (arg0))
7216 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7218 if (powfn)
7220 tree tree_root;
7221 REAL_VALUE_TYPE dconstroot;
7223 real_arithmetic (&dconstroot, MULT_EXPR,
7224 dconst_third_ptr (), dconst_third_ptr ());
7225 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7226 tree_root = build_real (type, dconstroot);
7227 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7232 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7233 if (fcode == BUILT_IN_POW
7234 || fcode == BUILT_IN_POWF
7235 || fcode == BUILT_IN_POWL)
7237 tree arg00 = CALL_EXPR_ARG (arg, 0);
7238 tree arg01 = CALL_EXPR_ARG (arg, 1);
7239 if (tree_expr_nonnegative_p (arg00))
7241 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7242 const REAL_VALUE_TYPE dconstroot
7243 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7244 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7245 build_real (type, dconstroot));
7246 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7250 return NULL_TREE;
7253 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7254 TYPE is the type of the return value. Return NULL_TREE if no
7255 simplification can be made. */
7257 static tree
7258 fold_builtin_cos (location_t loc,
7259 tree arg, tree type, tree fndecl)
7261 tree res, narg;
7263 if (!validate_arg (arg, REAL_TYPE))
7264 return NULL_TREE;
7266 /* Calculate the result when the argument is a constant. */
7267 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7268 return res;
7270 /* Optimize cos(-x) into cos (x). */
7271 if ((narg = fold_strip_sign_ops (arg)))
7272 return build_call_expr_loc (loc, fndecl, 1, narg);
7274 return NULL_TREE;
7277 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7278 Return NULL_TREE if no simplification can be made. */
7280 static tree
7281 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7283 if (validate_arg (arg, REAL_TYPE))
7285 tree res, narg;
7287 /* Calculate the result when the argument is a constant. */
7288 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7289 return res;
7291 /* Optimize cosh(-x) into cosh (x). */
7292 if ((narg = fold_strip_sign_ops (arg)))
7293 return build_call_expr_loc (loc, fndecl, 1, narg);
7296 return NULL_TREE;
7299 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7300 argument ARG. TYPE is the type of the return value. Return
7301 NULL_TREE if no simplification can be made. */
7303 static tree
7304 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7305 bool hyper)
7307 if (validate_arg (arg, COMPLEX_TYPE)
7308 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7310 tree tmp;
7312 /* Calculate the result when the argument is a constant. */
7313 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7314 return tmp;
7316 /* Optimize fn(-x) into fn(x). */
7317 if ((tmp = fold_strip_sign_ops (arg)))
7318 return build_call_expr_loc (loc, fndecl, 1, tmp);
7321 return NULL_TREE;
7324 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7325 Return NULL_TREE if no simplification can be made. */
7327 static tree
7328 fold_builtin_tan (tree arg, tree type)
7330 enum built_in_function fcode;
7331 tree res;
7333 if (!validate_arg (arg, REAL_TYPE))
7334 return NULL_TREE;
7336 /* Calculate the result when the argument is a constant. */
7337 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7338 return res;
7340 /* Optimize tan(atan(x)) = x. */
7341 fcode = builtin_mathfn_code (arg);
7342 if (flag_unsafe_math_optimizations
7343 && (fcode == BUILT_IN_ATAN
7344 || fcode == BUILT_IN_ATANF
7345 || fcode == BUILT_IN_ATANL))
7346 return CALL_EXPR_ARG (arg, 0);
7348 return NULL_TREE;
7351 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7352 NULL_TREE if no simplification can be made. */
7354 static tree
7355 fold_builtin_sincos (location_t loc,
7356 tree arg0, tree arg1, tree arg2)
7358 tree type;
7359 tree res, fn, call;
7361 if (!validate_arg (arg0, REAL_TYPE)
7362 || !validate_arg (arg1, POINTER_TYPE)
7363 || !validate_arg (arg2, POINTER_TYPE))
7364 return NULL_TREE;
7366 type = TREE_TYPE (arg0);
7368 /* Calculate the result when the argument is a constant. */
7369 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7370 return res;
7372 /* Canonicalize sincos to cexpi. */
7373 if (!TARGET_C99_FUNCTIONS)
7374 return NULL_TREE;
7375 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7376 if (!fn)
7377 return NULL_TREE;
7379 call = build_call_expr_loc (loc, fn, 1, arg0);
7380 call = builtin_save_expr (call);
7382 return build2 (COMPOUND_EXPR, void_type_node,
7383 build2 (MODIFY_EXPR, void_type_node,
7384 build_fold_indirect_ref_loc (loc, arg1),
7385 build1 (IMAGPART_EXPR, type, call)),
7386 build2 (MODIFY_EXPR, void_type_node,
7387 build_fold_indirect_ref_loc (loc, arg2),
7388 build1 (REALPART_EXPR, type, call)));
7391 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7392 NULL_TREE if no simplification can be made. */
7394 static tree
7395 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7397 tree rtype;
7398 tree realp, imagp, ifn;
7399 tree res;
7401 if (!validate_arg (arg0, COMPLEX_TYPE)
7402 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7403 return NULL_TREE;
7405 /* Calculate the result when the argument is a constant. */
7406 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7407 return res;
7409 rtype = TREE_TYPE (TREE_TYPE (arg0));
7411 /* In case we can figure out the real part of arg0 and it is constant zero
7412 fold to cexpi. */
7413 if (!TARGET_C99_FUNCTIONS)
7414 return NULL_TREE;
7415 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7416 if (!ifn)
7417 return NULL_TREE;
7419 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7420 && real_zerop (realp))
7422 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7423 return build_call_expr_loc (loc, ifn, 1, narg);
7426 /* In case we can easily decompose real and imaginary parts split cexp
7427 to exp (r) * cexpi (i). */
7428 if (flag_unsafe_math_optimizations
7429 && realp)
7431 tree rfn, rcall, icall;
7433 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7434 if (!rfn)
7435 return NULL_TREE;
7437 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7438 if (!imagp)
7439 return NULL_TREE;
7441 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7442 icall = builtin_save_expr (icall);
7443 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7444 rcall = builtin_save_expr (rcall);
7445 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7446 fold_build2_loc (loc, MULT_EXPR, rtype,
7447 rcall,
7448 fold_build1_loc (loc, REALPART_EXPR,
7449 rtype, icall)),
7450 fold_build2_loc (loc, MULT_EXPR, rtype,
7451 rcall,
7452 fold_build1_loc (loc, IMAGPART_EXPR,
7453 rtype, icall)));
7456 return NULL_TREE;
7459 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7460 Return NULL_TREE if no simplification can be made. */
7462 static tree
7463 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7465 if (!validate_arg (arg, REAL_TYPE))
7466 return NULL_TREE;
7468 /* Optimize trunc of constant value. */
7469 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7471 REAL_VALUE_TYPE r, x;
7472 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7474 x = TREE_REAL_CST (arg);
7475 real_trunc (&r, TYPE_MODE (type), &x);
7476 return build_real (type, r);
7479 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7482 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7483 Return NULL_TREE if no simplification can be made. */
7485 static tree
7486 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7488 if (!validate_arg (arg, REAL_TYPE))
7489 return NULL_TREE;
7491 /* Optimize floor of constant value. */
7492 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7494 REAL_VALUE_TYPE x;
7496 x = TREE_REAL_CST (arg);
7497 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7499 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7500 REAL_VALUE_TYPE r;
7502 real_floor (&r, TYPE_MODE (type), &x);
7503 return build_real (type, r);
7507 /* Fold floor (x) where x is nonnegative to trunc (x). */
7508 if (tree_expr_nonnegative_p (arg))
7510 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7511 if (truncfn)
7512 return build_call_expr_loc (loc, truncfn, 1, arg);
7515 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7518 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7519 Return NULL_TREE if no simplification can be made. */
7521 static tree
7522 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7524 if (!validate_arg (arg, REAL_TYPE))
7525 return NULL_TREE;
7527 /* Optimize ceil of constant value. */
7528 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7530 REAL_VALUE_TYPE x;
7532 x = TREE_REAL_CST (arg);
7533 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7535 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7536 REAL_VALUE_TYPE r;
7538 real_ceil (&r, TYPE_MODE (type), &x);
7539 return build_real (type, r);
7543 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7546 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7547 Return NULL_TREE if no simplification can be made. */
7549 static tree
7550 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7552 if (!validate_arg (arg, REAL_TYPE))
7553 return NULL_TREE;
7555 /* Optimize round of constant value. */
7556 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7558 REAL_VALUE_TYPE x;
7560 x = TREE_REAL_CST (arg);
7561 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7563 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7564 REAL_VALUE_TYPE r;
7566 real_round (&r, TYPE_MODE (type), &x);
7567 return build_real (type, r);
7571 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7574 /* Fold function call to builtin lround, lroundf or lroundl (or the
7575 corresponding long long versions) and other rounding functions. ARG
7576 is the argument to the call. Return NULL_TREE if no simplification
7577 can be made. */
7579 static tree
7580 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7582 if (!validate_arg (arg, REAL_TYPE))
7583 return NULL_TREE;
7585 /* Optimize lround of constant value. */
7586 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7588 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7590 if (real_isfinite (&x))
7592 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7593 tree ftype = TREE_TYPE (arg);
7594 double_int val;
7595 REAL_VALUE_TYPE r;
7597 switch (DECL_FUNCTION_CODE (fndecl))
7599 CASE_FLT_FN (BUILT_IN_LFLOOR):
7600 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7601 real_floor (&r, TYPE_MODE (ftype), &x);
7602 break;
7604 CASE_FLT_FN (BUILT_IN_LCEIL):
7605 CASE_FLT_FN (BUILT_IN_LLCEIL):
7606 real_ceil (&r, TYPE_MODE (ftype), &x);
7607 break;
7609 CASE_FLT_FN (BUILT_IN_LROUND):
7610 CASE_FLT_FN (BUILT_IN_LLROUND):
7611 real_round (&r, TYPE_MODE (ftype), &x);
7612 break;
7614 default:
7615 gcc_unreachable ();
7618 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7619 if (double_int_fits_to_tree_p (itype, val))
7620 return double_int_to_tree (itype, val);
7624 switch (DECL_FUNCTION_CODE (fndecl))
7626 CASE_FLT_FN (BUILT_IN_LFLOOR):
7627 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7628 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7629 if (tree_expr_nonnegative_p (arg))
7630 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7631 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7632 break;
7633 default:;
7636 return fold_fixed_mathfn (loc, fndecl, arg);
7639 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7640 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7641 the argument to the call. Return NULL_TREE if no simplification can
7642 be made. */
7644 static tree
7645 fold_builtin_bitop (tree fndecl, tree arg)
7647 if (!validate_arg (arg, INTEGER_TYPE))
7648 return NULL_TREE;
7650 /* Optimize for constant argument. */
7651 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7653 HOST_WIDE_INT hi, width, result;
7654 unsigned HOST_WIDE_INT lo;
7655 tree type;
7657 type = TREE_TYPE (arg);
7658 width = TYPE_PRECISION (type);
7659 lo = TREE_INT_CST_LOW (arg);
7661 /* Clear all the bits that are beyond the type's precision. */
7662 if (width > HOST_BITS_PER_WIDE_INT)
7664 hi = TREE_INT_CST_HIGH (arg);
7665 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7666 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7668 else
7670 hi = 0;
7671 if (width < HOST_BITS_PER_WIDE_INT)
7672 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7675 switch (DECL_FUNCTION_CODE (fndecl))
7677 CASE_INT_FN (BUILT_IN_FFS):
7678 if (lo != 0)
7679 result = exact_log2 (lo & -lo) + 1;
7680 else if (hi != 0)
7681 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7682 else
7683 result = 0;
7684 break;
7686 CASE_INT_FN (BUILT_IN_CLZ):
7687 if (hi != 0)
7688 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7689 else if (lo != 0)
7690 result = width - floor_log2 (lo) - 1;
7691 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7692 result = width;
7693 break;
7695 CASE_INT_FN (BUILT_IN_CTZ):
7696 if (lo != 0)
7697 result = exact_log2 (lo & -lo);
7698 else if (hi != 0)
7699 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7700 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7701 result = width;
7702 break;
7704 CASE_INT_FN (BUILT_IN_POPCOUNT):
7705 result = 0;
7706 while (lo)
7707 result++, lo &= lo - 1;
7708 while (hi)
7709 result++, hi &= hi - 1;
7710 break;
7712 CASE_INT_FN (BUILT_IN_PARITY):
7713 result = 0;
7714 while (lo)
7715 result++, lo &= lo - 1;
7716 while (hi)
7717 result++, hi &= hi - 1;
7718 result &= 1;
7719 break;
7721 default:
7722 gcc_unreachable ();
7725 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7728 return NULL_TREE;
7731 /* Fold function call to builtin_bswap and the long and long long
7732 variants. Return NULL_TREE if no simplification can be made. */
7733 static tree
7734 fold_builtin_bswap (tree fndecl, tree arg)
7736 if (! validate_arg (arg, INTEGER_TYPE))
7737 return NULL_TREE;
7739 /* Optimize constant value. */
7740 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7742 HOST_WIDE_INT hi, width, r_hi = 0;
7743 unsigned HOST_WIDE_INT lo, r_lo = 0;
7744 tree type;
7746 type = TREE_TYPE (arg);
7747 width = TYPE_PRECISION (type);
7748 lo = TREE_INT_CST_LOW (arg);
7749 hi = TREE_INT_CST_HIGH (arg);
7751 switch (DECL_FUNCTION_CODE (fndecl))
7753 case BUILT_IN_BSWAP32:
7754 case BUILT_IN_BSWAP64:
7756 int s;
7758 for (s = 0; s < width; s += 8)
7760 int d = width - s - 8;
7761 unsigned HOST_WIDE_INT byte;
7763 if (s < HOST_BITS_PER_WIDE_INT)
7764 byte = (lo >> s) & 0xff;
7765 else
7766 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7768 if (d < HOST_BITS_PER_WIDE_INT)
7769 r_lo |= byte << d;
7770 else
7771 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7775 break;
7777 default:
7778 gcc_unreachable ();
7781 if (width < HOST_BITS_PER_WIDE_INT)
7782 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7783 else
7784 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7787 return NULL_TREE;
7790 /* A subroutine of fold_builtin to fold the various logarithmic
7791 functions. Return NULL_TREE if no simplification can me made.
7792 FUNC is the corresponding MPFR logarithm function. */
7794 static tree
7795 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7796 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7798 if (validate_arg (arg, REAL_TYPE))
7800 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7801 tree res;
7802 const enum built_in_function fcode = builtin_mathfn_code (arg);
7804 /* Calculate the result when the argument is a constant. */
7805 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7806 return res;
7808 /* Special case, optimize logN(expN(x)) = x. */
7809 if (flag_unsafe_math_optimizations
7810 && ((func == mpfr_log
7811 && (fcode == BUILT_IN_EXP
7812 || fcode == BUILT_IN_EXPF
7813 || fcode == BUILT_IN_EXPL))
7814 || (func == mpfr_log2
7815 && (fcode == BUILT_IN_EXP2
7816 || fcode == BUILT_IN_EXP2F
7817 || fcode == BUILT_IN_EXP2L))
7818 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7819 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7821 /* Optimize logN(func()) for various exponential functions. We
7822 want to determine the value "x" and the power "exponent" in
7823 order to transform logN(x**exponent) into exponent*logN(x). */
7824 if (flag_unsafe_math_optimizations)
7826 tree exponent = 0, x = 0;
7828 switch (fcode)
7830 CASE_FLT_FN (BUILT_IN_EXP):
7831 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7832 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7833 dconst_e ()));
7834 exponent = CALL_EXPR_ARG (arg, 0);
7835 break;
7836 CASE_FLT_FN (BUILT_IN_EXP2):
7837 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7838 x = build_real (type, dconst2);
7839 exponent = CALL_EXPR_ARG (arg, 0);
7840 break;
7841 CASE_FLT_FN (BUILT_IN_EXP10):
7842 CASE_FLT_FN (BUILT_IN_POW10):
7843 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7845 REAL_VALUE_TYPE dconst10;
7846 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7847 x = build_real (type, dconst10);
7849 exponent = CALL_EXPR_ARG (arg, 0);
7850 break;
7851 CASE_FLT_FN (BUILT_IN_SQRT):
7852 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7853 x = CALL_EXPR_ARG (arg, 0);
7854 exponent = build_real (type, dconsthalf);
7855 break;
7856 CASE_FLT_FN (BUILT_IN_CBRT):
7857 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7858 x = CALL_EXPR_ARG (arg, 0);
7859 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7860 dconst_third ()));
7861 break;
7862 CASE_FLT_FN (BUILT_IN_POW):
7863 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7864 x = CALL_EXPR_ARG (arg, 0);
7865 exponent = CALL_EXPR_ARG (arg, 1);
7866 break;
7867 default:
7868 break;
7871 /* Now perform the optimization. */
7872 if (x && exponent)
7874 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
7875 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
7880 return NULL_TREE;
7883 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7884 NULL_TREE if no simplification can be made. */
7886 static tree
7887 fold_builtin_hypot (location_t loc, tree fndecl,
7888 tree arg0, tree arg1, tree type)
7890 tree res, narg0, narg1;
7892 if (!validate_arg (arg0, REAL_TYPE)
7893 || !validate_arg (arg1, REAL_TYPE))
7894 return NULL_TREE;
7896 /* Calculate the result when the argument is a constant. */
7897 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7898 return res;
7900 /* If either argument to hypot has a negate or abs, strip that off.
7901 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7902 narg0 = fold_strip_sign_ops (arg0);
7903 narg1 = fold_strip_sign_ops (arg1);
7904 if (narg0 || narg1)
7906 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
7907 narg1 ? narg1 : arg1);
7910 /* If either argument is zero, hypot is fabs of the other. */
7911 if (real_zerop (arg0))
7912 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
7913 else if (real_zerop (arg1))
7914 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
7916 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7917 if (flag_unsafe_math_optimizations
7918 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7920 const REAL_VALUE_TYPE sqrt2_trunc
7921 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7922 return fold_build2_loc (loc, MULT_EXPR, type,
7923 fold_build1_loc (loc, ABS_EXPR, type, arg0),
7924 build_real (type, sqrt2_trunc));
7927 return NULL_TREE;
7931 /* Fold a builtin function call to pow, powf, or powl. Return
7932 NULL_TREE if no simplification can be made. */
7933 static tree
7934 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
7936 tree res;
7938 if (!validate_arg (arg0, REAL_TYPE)
7939 || !validate_arg (arg1, REAL_TYPE))
7940 return NULL_TREE;
7942 /* Calculate the result when the argument is a constant. */
7943 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7944 return res;
7946 /* Optimize pow(1.0,y) = 1.0. */
7947 if (real_onep (arg0))
7948 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
7950 if (TREE_CODE (arg1) == REAL_CST
7951 && !TREE_OVERFLOW (arg1))
7953 REAL_VALUE_TYPE cint;
7954 REAL_VALUE_TYPE c;
7955 HOST_WIDE_INT n;
7957 c = TREE_REAL_CST (arg1);
7959 /* Optimize pow(x,0.0) = 1.0. */
7960 if (REAL_VALUES_EQUAL (c, dconst0))
7961 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
7962 arg0);
7964 /* Optimize pow(x,1.0) = x. */
7965 if (REAL_VALUES_EQUAL (c, dconst1))
7966 return arg0;
7968 /* Optimize pow(x,-1.0) = 1.0/x. */
7969 if (REAL_VALUES_EQUAL (c, dconstm1))
7970 return fold_build2_loc (loc, RDIV_EXPR, type,
7971 build_real (type, dconst1), arg0);
7973 /* Optimize pow(x,0.5) = sqrt(x). */
7974 if (flag_unsafe_math_optimizations
7975 && REAL_VALUES_EQUAL (c, dconsthalf))
7977 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7979 if (sqrtfn != NULL_TREE)
7980 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
7983 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7984 if (flag_unsafe_math_optimizations)
7986 const REAL_VALUE_TYPE dconstroot
7987 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7989 if (REAL_VALUES_EQUAL (c, dconstroot))
7991 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7992 if (cbrtfn != NULL_TREE)
7993 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
7997 /* Check for an integer exponent. */
7998 n = real_to_integer (&c);
7999 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8000 if (real_identical (&c, &cint))
8002 /* Attempt to evaluate pow at compile-time, unless this should
8003 raise an exception. */
8004 if (TREE_CODE (arg0) == REAL_CST
8005 && !TREE_OVERFLOW (arg0)
8006 && (n > 0
8007 || (!flag_trapping_math && !flag_errno_math)
8008 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8010 REAL_VALUE_TYPE x;
8011 bool inexact;
8013 x = TREE_REAL_CST (arg0);
8014 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8015 if (flag_unsafe_math_optimizations || !inexact)
8016 return build_real (type, x);
8019 /* Strip sign ops from even integer powers. */
8020 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8022 tree narg0 = fold_strip_sign_ops (arg0);
8023 if (narg0)
8024 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8029 if (flag_unsafe_math_optimizations)
8031 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8033 /* Optimize pow(expN(x),y) = expN(x*y). */
8034 if (BUILTIN_EXPONENT_P (fcode))
8036 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8037 tree arg = CALL_EXPR_ARG (arg0, 0);
8038 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8039 return build_call_expr_loc (loc, expfn, 1, arg);
8042 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8043 if (BUILTIN_SQRT_P (fcode))
8045 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8046 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8047 build_real (type, dconsthalf));
8048 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8051 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8052 if (BUILTIN_CBRT_P (fcode))
8054 tree arg = CALL_EXPR_ARG (arg0, 0);
8055 if (tree_expr_nonnegative_p (arg))
8057 const REAL_VALUE_TYPE dconstroot
8058 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8059 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8060 build_real (type, dconstroot));
8061 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8065 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8066 if (fcode == BUILT_IN_POW
8067 || fcode == BUILT_IN_POWF
8068 || fcode == BUILT_IN_POWL)
8070 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8071 if (tree_expr_nonnegative_p (arg00))
8073 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8074 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8075 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8080 return NULL_TREE;
8083 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8084 Return NULL_TREE if no simplification can be made. */
8085 static tree
8086 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8087 tree arg0, tree arg1, tree type)
8089 if (!validate_arg (arg0, REAL_TYPE)
8090 || !validate_arg (arg1, INTEGER_TYPE))
8091 return NULL_TREE;
8093 /* Optimize pow(1.0,y) = 1.0. */
8094 if (real_onep (arg0))
8095 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8097 if (host_integerp (arg1, 0))
8099 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8101 /* Evaluate powi at compile-time. */
8102 if (TREE_CODE (arg0) == REAL_CST
8103 && !TREE_OVERFLOW (arg0))
8105 REAL_VALUE_TYPE x;
8106 x = TREE_REAL_CST (arg0);
8107 real_powi (&x, TYPE_MODE (type), &x, c);
8108 return build_real (type, x);
8111 /* Optimize pow(x,0) = 1.0. */
8112 if (c == 0)
8113 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8114 arg0);
8116 /* Optimize pow(x,1) = x. */
8117 if (c == 1)
8118 return arg0;
8120 /* Optimize pow(x,-1) = 1.0/x. */
8121 if (c == -1)
8122 return fold_build2_loc (loc, RDIV_EXPR, type,
8123 build_real (type, dconst1), arg0);
8126 return NULL_TREE;
8129 /* A subroutine of fold_builtin to fold the various exponent
8130 functions. Return NULL_TREE if no simplification can be made.
8131 FUNC is the corresponding MPFR exponent function. */
8133 static tree
8134 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8135 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8137 if (validate_arg (arg, REAL_TYPE))
8139 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8140 tree res;
8142 /* Calculate the result when the argument is a constant. */
8143 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8144 return res;
8146 /* Optimize expN(logN(x)) = x. */
8147 if (flag_unsafe_math_optimizations)
8149 const enum built_in_function fcode = builtin_mathfn_code (arg);
8151 if ((func == mpfr_exp
8152 && (fcode == BUILT_IN_LOG
8153 || fcode == BUILT_IN_LOGF
8154 || fcode == BUILT_IN_LOGL))
8155 || (func == mpfr_exp2
8156 && (fcode == BUILT_IN_LOG2
8157 || fcode == BUILT_IN_LOG2F
8158 || fcode == BUILT_IN_LOG2L))
8159 || (func == mpfr_exp10
8160 && (fcode == BUILT_IN_LOG10
8161 || fcode == BUILT_IN_LOG10F
8162 || fcode == BUILT_IN_LOG10L)))
8163 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8167 return NULL_TREE;
8170 /* Return true if VAR is a VAR_DECL or a component thereof. */
8172 static bool
8173 var_decl_component_p (tree var)
8175 tree inner = var;
8176 while (handled_component_p (inner))
8177 inner = TREE_OPERAND (inner, 0);
8178 return SSA_VAR_P (inner);
8181 /* Fold function call to builtin memset. Return
8182 NULL_TREE if no simplification can be made. */
8184 static tree
8185 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8186 tree type, bool ignore)
8188 tree var, ret, etype;
8189 unsigned HOST_WIDE_INT length, cval;
8191 if (! validate_arg (dest, POINTER_TYPE)
8192 || ! validate_arg (c, INTEGER_TYPE)
8193 || ! validate_arg (len, INTEGER_TYPE))
8194 return NULL_TREE;
8196 if (! host_integerp (len, 1))
8197 return NULL_TREE;
8199 /* If the LEN parameter is zero, return DEST. */
8200 if (integer_zerop (len))
8201 return omit_one_operand_loc (loc, type, dest, c);
8203 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8204 return NULL_TREE;
8206 var = dest;
8207 STRIP_NOPS (var);
8208 if (TREE_CODE (var) != ADDR_EXPR)
8209 return NULL_TREE;
8211 var = TREE_OPERAND (var, 0);
8212 if (TREE_THIS_VOLATILE (var))
8213 return NULL_TREE;
8215 etype = TREE_TYPE (var);
8216 if (TREE_CODE (etype) == ARRAY_TYPE)
8217 etype = TREE_TYPE (etype);
8219 if (!INTEGRAL_TYPE_P (etype)
8220 && !POINTER_TYPE_P (etype))
8221 return NULL_TREE;
8223 if (! var_decl_component_p (var))
8224 return NULL_TREE;
8226 length = tree_low_cst (len, 1);
8227 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8228 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8229 < (int) length)
8230 return NULL_TREE;
8232 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8233 return NULL_TREE;
8235 if (integer_zerop (c))
8236 cval = 0;
8237 else
8239 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8240 return NULL_TREE;
8242 cval = tree_low_cst (c, 1);
8243 cval &= 0xff;
8244 cval |= cval << 8;
8245 cval |= cval << 16;
8246 cval |= (cval << 31) << 1;
8249 ret = build_int_cst_type (etype, cval);
8250 var = build_fold_indirect_ref_loc (loc,
8251 fold_convert_loc (loc,
8252 build_pointer_type (etype),
8253 dest));
8254 ret = build2 (MODIFY_EXPR, etype, var, ret);
8255 if (ignore)
8256 return ret;
8258 return omit_one_operand_loc (loc, type, dest, ret);
8261 /* Fold function call to builtin memset. Return
8262 NULL_TREE if no simplification can be made. */
8264 static tree
8265 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8267 if (! validate_arg (dest, POINTER_TYPE)
8268 || ! validate_arg (size, INTEGER_TYPE))
8269 return NULL_TREE;
8271 if (!ignore)
8272 return NULL_TREE;
8274 /* New argument list transforming bzero(ptr x, int y) to
8275 memset(ptr x, int 0, size_t y). This is done this way
8276 so that if it isn't expanded inline, we fallback to
8277 calling bzero instead of memset. */
8279 return fold_builtin_memset (loc, dest, integer_zero_node,
8280 fold_convert_loc (loc, sizetype, size),
8281 void_type_node, ignore);
8284 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8285 NULL_TREE if no simplification can be made.
8286 If ENDP is 0, return DEST (like memcpy).
8287 If ENDP is 1, return DEST+LEN (like mempcpy).
8288 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8289 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8290 (memmove). */
8292 static tree
8293 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8294 tree len, tree type, bool ignore, int endp)
8296 tree destvar, srcvar, expr;
8298 if (! validate_arg (dest, POINTER_TYPE)
8299 || ! validate_arg (src, POINTER_TYPE)
8300 || ! validate_arg (len, INTEGER_TYPE))
8301 return NULL_TREE;
8303 /* If the LEN parameter is zero, return DEST. */
8304 if (integer_zerop (len))
8305 return omit_one_operand_loc (loc, type, dest, src);
8307 /* If SRC and DEST are the same (and not volatile), return
8308 DEST{,+LEN,+LEN-1}. */
8309 if (operand_equal_p (src, dest, 0))
8310 expr = len;
8311 else
8313 tree srctype, desttype;
8314 int src_align, dest_align;
8315 tree off0;
8317 if (endp == 3)
8319 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8320 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8322 /* Both DEST and SRC must be pointer types.
8323 ??? This is what old code did. Is the testing for pointer types
8324 really mandatory?
8326 If either SRC is readonly or length is 1, we can use memcpy. */
8327 if (!dest_align || !src_align)
8328 return NULL_TREE;
8329 if (readonly_data_expr (src)
8330 || (host_integerp (len, 1)
8331 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8332 >= tree_low_cst (len, 1))))
8334 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8335 if (!fn)
8336 return NULL_TREE;
8337 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8340 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8341 if (TREE_CODE (src) == ADDR_EXPR
8342 && TREE_CODE (dest) == ADDR_EXPR)
8344 tree src_base, dest_base, fn;
8345 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8346 HOST_WIDE_INT size = -1;
8347 HOST_WIDE_INT maxsize = -1;
8349 srcvar = TREE_OPERAND (src, 0);
8350 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8351 &size, &maxsize);
8352 destvar = TREE_OPERAND (dest, 0);
8353 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8354 &size, &maxsize);
8355 if (host_integerp (len, 1))
8356 maxsize = tree_low_cst (len, 1);
8357 else
8358 maxsize = -1;
8359 src_offset /= BITS_PER_UNIT;
8360 dest_offset /= BITS_PER_UNIT;
8361 if (SSA_VAR_P (src_base)
8362 && SSA_VAR_P (dest_base))
8364 if (operand_equal_p (src_base, dest_base, 0)
8365 && ranges_overlap_p (src_offset, maxsize,
8366 dest_offset, maxsize))
8367 return NULL_TREE;
8369 else if (TREE_CODE (src_base) == MEM_REF
8370 && TREE_CODE (dest_base) == MEM_REF)
8372 double_int off;
8373 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8374 TREE_OPERAND (dest_base, 0), 0))
8375 return NULL_TREE;
8376 off = double_int_add (mem_ref_offset (src_base),
8377 shwi_to_double_int (src_offset));
8378 if (!double_int_fits_in_shwi_p (off))
8379 return NULL_TREE;
8380 src_offset = off.low;
8381 off = double_int_add (mem_ref_offset (dest_base),
8382 shwi_to_double_int (dest_offset));
8383 if (!double_int_fits_in_shwi_p (off))
8384 return NULL_TREE;
8385 dest_offset = off.low;
8386 if (ranges_overlap_p (src_offset, maxsize,
8387 dest_offset, maxsize))
8388 return NULL_TREE;
8390 else
8391 return NULL_TREE;
8393 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8394 if (!fn)
8395 return NULL_TREE;
8396 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8398 return NULL_TREE;
8401 if (!host_integerp (len, 0))
8402 return NULL_TREE;
8403 /* FIXME:
8404 This logic lose for arguments like (type *)malloc (sizeof (type)),
8405 since we strip the casts of up to VOID return value from malloc.
8406 Perhaps we ought to inherit type from non-VOID argument here? */
8407 STRIP_NOPS (src);
8408 STRIP_NOPS (dest);
8409 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8410 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8412 tree tem = TREE_OPERAND (src, 0);
8413 STRIP_NOPS (tem);
8414 if (tem != TREE_OPERAND (src, 0))
8415 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8417 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8419 tree tem = TREE_OPERAND (dest, 0);
8420 STRIP_NOPS (tem);
8421 if (tem != TREE_OPERAND (dest, 0))
8422 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8424 srctype = TREE_TYPE (TREE_TYPE (src));
8425 if (srctype
8426 && TREE_CODE (srctype) == ARRAY_TYPE
8427 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8429 srctype = TREE_TYPE (srctype);
8430 STRIP_NOPS (src);
8431 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8433 desttype = TREE_TYPE (TREE_TYPE (dest));
8434 if (desttype
8435 && TREE_CODE (desttype) == ARRAY_TYPE
8436 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8438 desttype = TREE_TYPE (desttype);
8439 STRIP_NOPS (dest);
8440 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8442 if (!srctype || !desttype
8443 || TREE_ADDRESSABLE (srctype)
8444 || TREE_ADDRESSABLE (desttype)
8445 || !TYPE_SIZE_UNIT (srctype)
8446 || !TYPE_SIZE_UNIT (desttype)
8447 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8448 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8449 return NULL_TREE;
8451 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8452 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8453 if (dest_align < (int) TYPE_ALIGN (desttype)
8454 || src_align < (int) TYPE_ALIGN (srctype))
8455 return NULL_TREE;
8457 if (!ignore)
8458 dest = builtin_save_expr (dest);
8460 /* Build accesses at offset zero with a ref-all character type. */
8461 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8462 ptr_mode, true), 0);
8464 destvar = dest;
8465 STRIP_NOPS (destvar);
8466 if (TREE_CODE (destvar) == ADDR_EXPR
8467 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8468 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8469 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8470 else
8471 destvar = NULL_TREE;
8473 srcvar = src;
8474 STRIP_NOPS (srcvar);
8475 if (TREE_CODE (srcvar) == ADDR_EXPR
8476 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8477 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8478 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8479 srcvar, off0);
8480 else
8481 srcvar = NULL_TREE;
8483 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8484 return NULL_TREE;
8486 if (srcvar == NULL_TREE)
8488 STRIP_NOPS (src);
8489 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8491 else if (destvar == NULL_TREE)
8493 STRIP_NOPS (dest);
8494 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8497 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8500 if (ignore)
8501 return expr;
8503 if (endp == 0 || endp == 3)
8504 return omit_one_operand_loc (loc, type, dest, expr);
8506 if (expr == len)
8507 expr = NULL_TREE;
8509 if (endp == 2)
8510 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8511 ssize_int (1));
8513 len = fold_convert_loc (loc, sizetype, len);
8514 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8515 dest = fold_convert_loc (loc, type, dest);
8516 if (expr)
8517 dest = omit_one_operand_loc (loc, type, dest, expr);
8518 return dest;
8521 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8522 If LEN is not NULL, it represents the length of the string to be
8523 copied. Return NULL_TREE if no simplification can be made. */
8525 tree
8526 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8528 tree fn;
8530 if (!validate_arg (dest, POINTER_TYPE)
8531 || !validate_arg (src, POINTER_TYPE))
8532 return NULL_TREE;
8534 /* If SRC and DEST are the same (and not volatile), return DEST. */
8535 if (operand_equal_p (src, dest, 0))
8536 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8538 if (optimize_function_for_size_p (cfun))
8539 return NULL_TREE;
8541 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8542 if (!fn)
8543 return NULL_TREE;
8545 if (!len)
8547 len = c_strlen (src, 1);
8548 if (! len || TREE_SIDE_EFFECTS (len))
8549 return NULL_TREE;
8552 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8553 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8554 build_call_expr_loc (loc, fn, 3, dest, src, len));
8557 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8558 Return NULL_TREE if no simplification can be made. */
8560 static tree
8561 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8563 tree fn, len, lenp1, call, type;
8565 if (!validate_arg (dest, POINTER_TYPE)
8566 || !validate_arg (src, POINTER_TYPE))
8567 return NULL_TREE;
8569 len = c_strlen (src, 1);
8570 if (!len
8571 || TREE_CODE (len) != INTEGER_CST)
8572 return NULL_TREE;
8574 if (optimize_function_for_size_p (cfun)
8575 /* If length is zero it's small enough. */
8576 && !integer_zerop (len))
8577 return NULL_TREE;
8579 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8580 if (!fn)
8581 return NULL_TREE;
8583 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8584 /* We use dest twice in building our expression. Save it from
8585 multiple expansions. */
8586 dest = builtin_save_expr (dest);
8587 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8589 type = TREE_TYPE (TREE_TYPE (fndecl));
8590 len = fold_convert_loc (loc, sizetype, len);
8591 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8592 dest = fold_convert_loc (loc, type, dest);
8593 dest = omit_one_operand_loc (loc, type, dest, call);
8594 return dest;
8597 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8598 If SLEN is not NULL, it represents the length of the source string.
8599 Return NULL_TREE if no simplification can be made. */
8601 tree
8602 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8603 tree src, tree len, tree slen)
8605 tree fn;
8607 if (!validate_arg (dest, POINTER_TYPE)
8608 || !validate_arg (src, POINTER_TYPE)
8609 || !validate_arg (len, INTEGER_TYPE))
8610 return NULL_TREE;
8612 /* If the LEN parameter is zero, return DEST. */
8613 if (integer_zerop (len))
8614 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8616 /* We can't compare slen with len as constants below if len is not a
8617 constant. */
8618 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8619 return NULL_TREE;
8621 if (!slen)
8622 slen = c_strlen (src, 1);
8624 /* Now, we must be passed a constant src ptr parameter. */
8625 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8626 return NULL_TREE;
8628 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8630 /* We do not support simplification of this case, though we do
8631 support it when expanding trees into RTL. */
8632 /* FIXME: generate a call to __builtin_memset. */
8633 if (tree_int_cst_lt (slen, len))
8634 return NULL_TREE;
8636 /* OK transform into builtin memcpy. */
8637 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8638 if (!fn)
8639 return NULL_TREE;
8640 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8641 build_call_expr_loc (loc, fn, 3, dest, src, len));
8644 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8645 arguments to the call, and TYPE is its return type.
8646 Return NULL_TREE if no simplification can be made. */
8648 static tree
8649 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8651 if (!validate_arg (arg1, POINTER_TYPE)
8652 || !validate_arg (arg2, INTEGER_TYPE)
8653 || !validate_arg (len, INTEGER_TYPE))
8654 return NULL_TREE;
8655 else
8657 const char *p1;
8659 if (TREE_CODE (arg2) != INTEGER_CST
8660 || !host_integerp (len, 1))
8661 return NULL_TREE;
8663 p1 = c_getstr (arg1);
8664 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8666 char c;
8667 const char *r;
8668 tree tem;
8670 if (target_char_cast (arg2, &c))
8671 return NULL_TREE;
8673 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8675 if (r == NULL)
8676 return build_int_cst (TREE_TYPE (arg1), 0);
8678 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8679 size_int (r - p1));
8680 return fold_convert_loc (loc, type, tem);
8682 return NULL_TREE;
8686 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8687 Return NULL_TREE if no simplification can be made. */
8689 static tree
8690 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8692 const char *p1, *p2;
8694 if (!validate_arg (arg1, POINTER_TYPE)
8695 || !validate_arg (arg2, POINTER_TYPE)
8696 || !validate_arg (len, INTEGER_TYPE))
8697 return NULL_TREE;
8699 /* If the LEN parameter is zero, return zero. */
8700 if (integer_zerop (len))
8701 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8702 arg1, arg2);
8704 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8705 if (operand_equal_p (arg1, arg2, 0))
8706 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8708 p1 = c_getstr (arg1);
8709 p2 = c_getstr (arg2);
8711 /* If all arguments are constant, and the value of len is not greater
8712 than the lengths of arg1 and arg2, evaluate at compile-time. */
8713 if (host_integerp (len, 1) && p1 && p2
8714 && compare_tree_int (len, strlen (p1) + 1) <= 0
8715 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8717 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8719 if (r > 0)
8720 return integer_one_node;
8721 else if (r < 0)
8722 return integer_minus_one_node;
8723 else
8724 return integer_zero_node;
8727 /* If len parameter is one, return an expression corresponding to
8728 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8729 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8731 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8732 tree cst_uchar_ptr_node
8733 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8735 tree ind1
8736 = fold_convert_loc (loc, integer_type_node,
8737 build1 (INDIRECT_REF, cst_uchar_node,
8738 fold_convert_loc (loc,
8739 cst_uchar_ptr_node,
8740 arg1)));
8741 tree ind2
8742 = fold_convert_loc (loc, integer_type_node,
8743 build1 (INDIRECT_REF, cst_uchar_node,
8744 fold_convert_loc (loc,
8745 cst_uchar_ptr_node,
8746 arg2)));
8747 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8750 return NULL_TREE;
8753 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8754 Return NULL_TREE if no simplification can be made. */
8756 static tree
8757 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8759 const char *p1, *p2;
8761 if (!validate_arg (arg1, POINTER_TYPE)
8762 || !validate_arg (arg2, POINTER_TYPE))
8763 return NULL_TREE;
8765 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8766 if (operand_equal_p (arg1, arg2, 0))
8767 return integer_zero_node;
8769 p1 = c_getstr (arg1);
8770 p2 = c_getstr (arg2);
8772 if (p1 && p2)
8774 const int i = strcmp (p1, p2);
8775 if (i < 0)
8776 return integer_minus_one_node;
8777 else if (i > 0)
8778 return integer_one_node;
8779 else
8780 return integer_zero_node;
8783 /* If the second arg is "", return *(const unsigned char*)arg1. */
8784 if (p2 && *p2 == '\0')
8786 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8787 tree cst_uchar_ptr_node
8788 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8790 return fold_convert_loc (loc, integer_type_node,
8791 build1 (INDIRECT_REF, cst_uchar_node,
8792 fold_convert_loc (loc,
8793 cst_uchar_ptr_node,
8794 arg1)));
8797 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8798 if (p1 && *p1 == '\0')
8800 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8801 tree cst_uchar_ptr_node
8802 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8804 tree temp
8805 = fold_convert_loc (loc, integer_type_node,
8806 build1 (INDIRECT_REF, cst_uchar_node,
8807 fold_convert_loc (loc,
8808 cst_uchar_ptr_node,
8809 arg2)));
8810 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8813 return NULL_TREE;
8816 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8817 Return NULL_TREE if no simplification can be made. */
8819 static tree
8820 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8822 const char *p1, *p2;
8824 if (!validate_arg (arg1, POINTER_TYPE)
8825 || !validate_arg (arg2, POINTER_TYPE)
8826 || !validate_arg (len, INTEGER_TYPE))
8827 return NULL_TREE;
8829 /* If the LEN parameter is zero, return zero. */
8830 if (integer_zerop (len))
8831 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8832 arg1, arg2);
8834 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8835 if (operand_equal_p (arg1, arg2, 0))
8836 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8838 p1 = c_getstr (arg1);
8839 p2 = c_getstr (arg2);
8841 if (host_integerp (len, 1) && p1 && p2)
8843 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8844 if (i > 0)
8845 return integer_one_node;
8846 else if (i < 0)
8847 return integer_minus_one_node;
8848 else
8849 return integer_zero_node;
8852 /* If the second arg is "", and the length is greater than zero,
8853 return *(const unsigned char*)arg1. */
8854 if (p2 && *p2 == '\0'
8855 && TREE_CODE (len) == INTEGER_CST
8856 && tree_int_cst_sgn (len) == 1)
8858 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8859 tree cst_uchar_ptr_node
8860 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8862 return fold_convert_loc (loc, integer_type_node,
8863 build1 (INDIRECT_REF, cst_uchar_node,
8864 fold_convert_loc (loc,
8865 cst_uchar_ptr_node,
8866 arg1)));
8869 /* If the first arg is "", and the length is greater than zero,
8870 return -*(const unsigned char*)arg2. */
8871 if (p1 && *p1 == '\0'
8872 && TREE_CODE (len) == INTEGER_CST
8873 && tree_int_cst_sgn (len) == 1)
8875 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8876 tree cst_uchar_ptr_node
8877 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8879 tree temp = fold_convert_loc (loc, integer_type_node,
8880 build1 (INDIRECT_REF, cst_uchar_node,
8881 fold_convert_loc (loc,
8882 cst_uchar_ptr_node,
8883 arg2)));
8884 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8887 /* If len parameter is one, return an expression corresponding to
8888 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8889 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8891 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8892 tree cst_uchar_ptr_node
8893 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8895 tree ind1 = fold_convert_loc (loc, integer_type_node,
8896 build1 (INDIRECT_REF, cst_uchar_node,
8897 fold_convert_loc (loc,
8898 cst_uchar_ptr_node,
8899 arg1)));
8900 tree ind2 = fold_convert_loc (loc, integer_type_node,
8901 build1 (INDIRECT_REF, cst_uchar_node,
8902 fold_convert_loc (loc,
8903 cst_uchar_ptr_node,
8904 arg2)));
8905 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8908 return NULL_TREE;
8911 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8912 ARG. Return NULL_TREE if no simplification can be made. */
8914 static tree
8915 fold_builtin_signbit (location_t loc, tree arg, tree type)
8917 tree temp;
8919 if (!validate_arg (arg, REAL_TYPE))
8920 return NULL_TREE;
8922 /* If ARG is a compile-time constant, determine the result. */
8923 if (TREE_CODE (arg) == REAL_CST
8924 && !TREE_OVERFLOW (arg))
8926 REAL_VALUE_TYPE c;
8928 c = TREE_REAL_CST (arg);
8929 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8930 return fold_convert_loc (loc, type, temp);
8933 /* If ARG is non-negative, the result is always zero. */
8934 if (tree_expr_nonnegative_p (arg))
8935 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8937 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8938 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8939 return fold_build2_loc (loc, LT_EXPR, type, arg,
8940 build_real (TREE_TYPE (arg), dconst0));
8942 return NULL_TREE;
8945 /* Fold function call to builtin copysign, copysignf or copysignl with
8946 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8947 be made. */
8949 static tree
8950 fold_builtin_copysign (location_t loc, tree fndecl,
8951 tree arg1, tree arg2, tree type)
8953 tree tem;
8955 if (!validate_arg (arg1, REAL_TYPE)
8956 || !validate_arg (arg2, REAL_TYPE))
8957 return NULL_TREE;
8959 /* copysign(X,X) is X. */
8960 if (operand_equal_p (arg1, arg2, 0))
8961 return fold_convert_loc (loc, type, arg1);
8963 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8964 if (TREE_CODE (arg1) == REAL_CST
8965 && TREE_CODE (arg2) == REAL_CST
8966 && !TREE_OVERFLOW (arg1)
8967 && !TREE_OVERFLOW (arg2))
8969 REAL_VALUE_TYPE c1, c2;
8971 c1 = TREE_REAL_CST (arg1);
8972 c2 = TREE_REAL_CST (arg2);
8973 /* c1.sign := c2.sign. */
8974 real_copysign (&c1, &c2);
8975 return build_real (type, c1);
8978 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8979 Remember to evaluate Y for side-effects. */
8980 if (tree_expr_nonnegative_p (arg2))
8981 return omit_one_operand_loc (loc, type,
8982 fold_build1_loc (loc, ABS_EXPR, type, arg1),
8983 arg2);
8985 /* Strip sign changing operations for the first argument. */
8986 tem = fold_strip_sign_ops (arg1);
8987 if (tem)
8988 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
8990 return NULL_TREE;
8993 /* Fold a call to builtin isascii with argument ARG. */
8995 static tree
8996 fold_builtin_isascii (location_t loc, tree arg)
8998 if (!validate_arg (arg, INTEGER_TYPE))
8999 return NULL_TREE;
9000 else
9002 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9003 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9004 build_int_cst (NULL_TREE,
9005 ~ (unsigned HOST_WIDE_INT) 0x7f));
9006 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9007 arg, integer_zero_node);
9011 /* Fold a call to builtin toascii with argument ARG. */
9013 static tree
9014 fold_builtin_toascii (location_t loc, tree arg)
9016 if (!validate_arg (arg, INTEGER_TYPE))
9017 return NULL_TREE;
9019 /* Transform toascii(c) -> (c & 0x7f). */
9020 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9021 build_int_cst (NULL_TREE, 0x7f));
9024 /* Fold a call to builtin isdigit with argument ARG. */
9026 static tree
9027 fold_builtin_isdigit (location_t loc, tree arg)
9029 if (!validate_arg (arg, INTEGER_TYPE))
9030 return NULL_TREE;
9031 else
9033 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9034 /* According to the C standard, isdigit is unaffected by locale.
9035 However, it definitely is affected by the target character set. */
9036 unsigned HOST_WIDE_INT target_digit0
9037 = lang_hooks.to_target_charset ('0');
9039 if (target_digit0 == 0)
9040 return NULL_TREE;
9042 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9043 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9044 build_int_cst (unsigned_type_node, target_digit0));
9045 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9046 build_int_cst (unsigned_type_node, 9));
9050 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9052 static tree
9053 fold_builtin_fabs (location_t loc, tree arg, tree type)
9055 if (!validate_arg (arg, REAL_TYPE))
9056 return NULL_TREE;
9058 arg = fold_convert_loc (loc, type, arg);
9059 if (TREE_CODE (arg) == REAL_CST)
9060 return fold_abs_const (arg, type);
9061 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9064 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9066 static tree
9067 fold_builtin_abs (location_t loc, tree arg, tree type)
9069 if (!validate_arg (arg, INTEGER_TYPE))
9070 return NULL_TREE;
9072 arg = fold_convert_loc (loc, type, arg);
9073 if (TREE_CODE (arg) == INTEGER_CST)
9074 return fold_abs_const (arg, type);
9075 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9078 /* Fold a call to builtin fmin or fmax. */
9080 static tree
9081 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9082 tree type, bool max)
9084 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9086 /* Calculate the result when the argument is a constant. */
9087 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9089 if (res)
9090 return res;
9092 /* If either argument is NaN, return the other one. Avoid the
9093 transformation if we get (and honor) a signalling NaN. Using
9094 omit_one_operand() ensures we create a non-lvalue. */
9095 if (TREE_CODE (arg0) == REAL_CST
9096 && real_isnan (&TREE_REAL_CST (arg0))
9097 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9098 || ! TREE_REAL_CST (arg0).signalling))
9099 return omit_one_operand_loc (loc, type, arg1, arg0);
9100 if (TREE_CODE (arg1) == REAL_CST
9101 && real_isnan (&TREE_REAL_CST (arg1))
9102 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9103 || ! TREE_REAL_CST (arg1).signalling))
9104 return omit_one_operand_loc (loc, type, arg0, arg1);
9106 /* Transform fmin/fmax(x,x) -> x. */
9107 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9108 return omit_one_operand_loc (loc, type, arg0, arg1);
9110 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9111 functions to return the numeric arg if the other one is NaN.
9112 These tree codes don't honor that, so only transform if
9113 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9114 handled, so we don't have to worry about it either. */
9115 if (flag_finite_math_only)
9116 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9117 fold_convert_loc (loc, type, arg0),
9118 fold_convert_loc (loc, type, arg1));
9120 return NULL_TREE;
9123 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9125 static tree
9126 fold_builtin_carg (location_t loc, tree arg, tree type)
9128 if (validate_arg (arg, COMPLEX_TYPE)
9129 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9131 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9133 if (atan2_fn)
9135 tree new_arg = builtin_save_expr (arg);
9136 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9137 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9138 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9142 return NULL_TREE;
9145 /* Fold a call to builtin logb/ilogb. */
9147 static tree
9148 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9150 if (! validate_arg (arg, REAL_TYPE))
9151 return NULL_TREE;
9153 STRIP_NOPS (arg);
9155 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9157 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9159 switch (value->cl)
9161 case rvc_nan:
9162 case rvc_inf:
9163 /* If arg is Inf or NaN and we're logb, return it. */
9164 if (TREE_CODE (rettype) == REAL_TYPE)
9165 return fold_convert_loc (loc, rettype, arg);
9166 /* Fall through... */
9167 case rvc_zero:
9168 /* Zero may set errno and/or raise an exception for logb, also
9169 for ilogb we don't know FP_ILOGB0. */
9170 return NULL_TREE;
9171 case rvc_normal:
9172 /* For normal numbers, proceed iff radix == 2. In GCC,
9173 normalized significands are in the range [0.5, 1.0). We
9174 want the exponent as if they were [1.0, 2.0) so get the
9175 exponent and subtract 1. */
9176 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9177 return fold_convert_loc (loc, rettype,
9178 build_int_cst (NULL_TREE,
9179 REAL_EXP (value)-1));
9180 break;
9184 return NULL_TREE;
9187 /* Fold a call to builtin significand, if radix == 2. */
9189 static tree
9190 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9192 if (! validate_arg (arg, REAL_TYPE))
9193 return NULL_TREE;
9195 STRIP_NOPS (arg);
9197 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9199 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9201 switch (value->cl)
9203 case rvc_zero:
9204 case rvc_nan:
9205 case rvc_inf:
9206 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9207 return fold_convert_loc (loc, rettype, arg);
9208 case rvc_normal:
9209 /* For normal numbers, proceed iff radix == 2. */
9210 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9212 REAL_VALUE_TYPE result = *value;
9213 /* In GCC, normalized significands are in the range [0.5,
9214 1.0). We want them to be [1.0, 2.0) so set the
9215 exponent to 1. */
9216 SET_REAL_EXP (&result, 1);
9217 return build_real (rettype, result);
9219 break;
9223 return NULL_TREE;
9226 /* Fold a call to builtin frexp, we can assume the base is 2. */
9228 static tree
9229 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9231 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9232 return NULL_TREE;
9234 STRIP_NOPS (arg0);
9236 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9237 return NULL_TREE;
9239 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9241 /* Proceed if a valid pointer type was passed in. */
9242 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9244 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9245 tree frac, exp;
9247 switch (value->cl)
9249 case rvc_zero:
9250 /* For +-0, return (*exp = 0, +-0). */
9251 exp = integer_zero_node;
9252 frac = arg0;
9253 break;
9254 case rvc_nan:
9255 case rvc_inf:
9256 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9257 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9258 case rvc_normal:
9260 /* Since the frexp function always expects base 2, and in
9261 GCC normalized significands are already in the range
9262 [0.5, 1.0), we have exactly what frexp wants. */
9263 REAL_VALUE_TYPE frac_rvt = *value;
9264 SET_REAL_EXP (&frac_rvt, 0);
9265 frac = build_real (rettype, frac_rvt);
9266 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9268 break;
9269 default:
9270 gcc_unreachable ();
9273 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9274 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9275 TREE_SIDE_EFFECTS (arg1) = 1;
9276 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9279 return NULL_TREE;
9282 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9283 then we can assume the base is two. If it's false, then we have to
9284 check the mode of the TYPE parameter in certain cases. */
9286 static tree
9287 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9288 tree type, bool ldexp)
9290 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9292 STRIP_NOPS (arg0);
9293 STRIP_NOPS (arg1);
9295 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9296 if (real_zerop (arg0) || integer_zerop (arg1)
9297 || (TREE_CODE (arg0) == REAL_CST
9298 && !real_isfinite (&TREE_REAL_CST (arg0))))
9299 return omit_one_operand_loc (loc, type, arg0, arg1);
9301 /* If both arguments are constant, then try to evaluate it. */
9302 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9303 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9304 && host_integerp (arg1, 0))
9306 /* Bound the maximum adjustment to twice the range of the
9307 mode's valid exponents. Use abs to ensure the range is
9308 positive as a sanity check. */
9309 const long max_exp_adj = 2 *
9310 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9311 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9313 /* Get the user-requested adjustment. */
9314 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9316 /* The requested adjustment must be inside this range. This
9317 is a preliminary cap to avoid things like overflow, we
9318 may still fail to compute the result for other reasons. */
9319 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9321 REAL_VALUE_TYPE initial_result;
9323 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9325 /* Ensure we didn't overflow. */
9326 if (! real_isinf (&initial_result))
9328 const REAL_VALUE_TYPE trunc_result
9329 = real_value_truncate (TYPE_MODE (type), initial_result);
9331 /* Only proceed if the target mode can hold the
9332 resulting value. */
9333 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9334 return build_real (type, trunc_result);
9340 return NULL_TREE;
9343 /* Fold a call to builtin modf. */
9345 static tree
9346 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9348 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9349 return NULL_TREE;
9351 STRIP_NOPS (arg0);
9353 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9354 return NULL_TREE;
9356 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9358 /* Proceed if a valid pointer type was passed in. */
9359 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9361 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9362 REAL_VALUE_TYPE trunc, frac;
9364 switch (value->cl)
9366 case rvc_nan:
9367 case rvc_zero:
9368 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9369 trunc = frac = *value;
9370 break;
9371 case rvc_inf:
9372 /* For +-Inf, return (*arg1 = arg0, +-0). */
9373 frac = dconst0;
9374 frac.sign = value->sign;
9375 trunc = *value;
9376 break;
9377 case rvc_normal:
9378 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9379 real_trunc (&trunc, VOIDmode, value);
9380 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9381 /* If the original number was negative and already
9382 integral, then the fractional part is -0.0. */
9383 if (value->sign && frac.cl == rvc_zero)
9384 frac.sign = value->sign;
9385 break;
9388 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9389 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9390 build_real (rettype, trunc));
9391 TREE_SIDE_EFFECTS (arg1) = 1;
9392 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9393 build_real (rettype, frac));
9396 return NULL_TREE;
9399 /* Given a location LOC, an interclass builtin function decl FNDECL
9400 and its single argument ARG, return an folded expression computing
9401 the same, or NULL_TREE if we either couldn't or didn't want to fold
9402 (the latter happen if there's an RTL instruction available). */
9404 static tree
9405 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9407 enum machine_mode mode;
9409 if (!validate_arg (arg, REAL_TYPE))
9410 return NULL_TREE;
9412 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9413 return NULL_TREE;
9415 mode = TYPE_MODE (TREE_TYPE (arg));
9417 /* If there is no optab, try generic code. */
9418 switch (DECL_FUNCTION_CODE (fndecl))
9420 tree result;
9422 CASE_FLT_FN (BUILT_IN_ISINF):
9424 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9425 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9426 tree const type = TREE_TYPE (arg);
9427 REAL_VALUE_TYPE r;
9428 char buf[128];
9430 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9431 real_from_string (&r, buf);
9432 result = build_call_expr (isgr_fn, 2,
9433 fold_build1_loc (loc, ABS_EXPR, type, arg),
9434 build_real (type, r));
9435 return result;
9437 CASE_FLT_FN (BUILT_IN_FINITE):
9438 case BUILT_IN_ISFINITE:
9440 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9441 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9442 tree const type = TREE_TYPE (arg);
9443 REAL_VALUE_TYPE r;
9444 char buf[128];
9446 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9447 real_from_string (&r, buf);
9448 result = build_call_expr (isle_fn, 2,
9449 fold_build1_loc (loc, ABS_EXPR, type, arg),
9450 build_real (type, r));
9451 /*result = fold_build2_loc (loc, UNGT_EXPR,
9452 TREE_TYPE (TREE_TYPE (fndecl)),
9453 fold_build1_loc (loc, ABS_EXPR, type, arg),
9454 build_real (type, r));
9455 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9456 TREE_TYPE (TREE_TYPE (fndecl)),
9457 result);*/
9458 return result;
9460 case BUILT_IN_ISNORMAL:
9462 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9463 islessequal(fabs(x),DBL_MAX). */
9464 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9465 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9466 tree const type = TREE_TYPE (arg);
9467 REAL_VALUE_TYPE rmax, rmin;
9468 char buf[128];
9470 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9471 real_from_string (&rmax, buf);
9472 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9473 real_from_string (&rmin, buf);
9474 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9475 result = build_call_expr (isle_fn, 2, arg,
9476 build_real (type, rmax));
9477 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9478 build_call_expr (isge_fn, 2, arg,
9479 build_real (type, rmin)));
9480 return result;
9482 default:
9483 break;
9486 return NULL_TREE;
9489 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9490 ARG is the argument for the call. */
9492 static tree
9493 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9495 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9496 REAL_VALUE_TYPE r;
9498 if (!validate_arg (arg, REAL_TYPE))
9499 return NULL_TREE;
9501 switch (builtin_index)
9503 case BUILT_IN_ISINF:
9504 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9505 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9507 if (TREE_CODE (arg) == REAL_CST)
9509 r = TREE_REAL_CST (arg);
9510 if (real_isinf (&r))
9511 return real_compare (GT_EXPR, &r, &dconst0)
9512 ? integer_one_node : integer_minus_one_node;
9513 else
9514 return integer_zero_node;
9517 return NULL_TREE;
9519 case BUILT_IN_ISINF_SIGN:
9521 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9522 /* In a boolean context, GCC will fold the inner COND_EXPR to
9523 1. So e.g. "if (isinf_sign(x))" would be folded to just
9524 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9525 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9526 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9527 tree tmp = NULL_TREE;
9529 arg = builtin_save_expr (arg);
9531 if (signbit_fn && isinf_fn)
9533 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9534 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9536 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9537 signbit_call, integer_zero_node);
9538 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9539 isinf_call, integer_zero_node);
9541 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9542 integer_minus_one_node, integer_one_node);
9543 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9544 isinf_call, tmp,
9545 integer_zero_node);
9548 return tmp;
9551 case BUILT_IN_ISFINITE:
9552 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9553 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9554 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9556 if (TREE_CODE (arg) == REAL_CST)
9558 r = TREE_REAL_CST (arg);
9559 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9562 return NULL_TREE;
9564 case BUILT_IN_ISNAN:
9565 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9566 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9568 if (TREE_CODE (arg) == REAL_CST)
9570 r = TREE_REAL_CST (arg);
9571 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9574 arg = builtin_save_expr (arg);
9575 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9577 default:
9578 gcc_unreachable ();
9582 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9583 This builtin will generate code to return the appropriate floating
9584 point classification depending on the value of the floating point
9585 number passed in. The possible return values must be supplied as
9586 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9587 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9588 one floating point argument which is "type generic". */
9590 static tree
9591 fold_builtin_fpclassify (location_t loc, tree exp)
9593 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9594 arg, type, res, tmp;
9595 enum machine_mode mode;
9596 REAL_VALUE_TYPE r;
9597 char buf[128];
9599 /* Verify the required arguments in the original call. */
9600 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9601 INTEGER_TYPE, INTEGER_TYPE,
9602 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9603 return NULL_TREE;
9605 fp_nan = CALL_EXPR_ARG (exp, 0);
9606 fp_infinite = CALL_EXPR_ARG (exp, 1);
9607 fp_normal = CALL_EXPR_ARG (exp, 2);
9608 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9609 fp_zero = CALL_EXPR_ARG (exp, 4);
9610 arg = CALL_EXPR_ARG (exp, 5);
9611 type = TREE_TYPE (arg);
9612 mode = TYPE_MODE (type);
9613 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9615 /* fpclassify(x) ->
9616 isnan(x) ? FP_NAN :
9617 (fabs(x) == Inf ? FP_INFINITE :
9618 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9619 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9621 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9622 build_real (type, dconst0));
9623 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9624 tmp, fp_zero, fp_subnormal);
9626 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9627 real_from_string (&r, buf);
9628 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9629 arg, build_real (type, r));
9630 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9632 if (HONOR_INFINITIES (mode))
9634 real_inf (&r);
9635 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9636 build_real (type, r));
9637 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9638 fp_infinite, res);
9641 if (HONOR_NANS (mode))
9643 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9644 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9647 return res;
9650 /* Fold a call to an unordered comparison function such as
9651 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9652 being called and ARG0 and ARG1 are the arguments for the call.
9653 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9654 the opposite of the desired result. UNORDERED_CODE is used
9655 for modes that can hold NaNs and ORDERED_CODE is used for
9656 the rest. */
9658 static tree
9659 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9660 enum tree_code unordered_code,
9661 enum tree_code ordered_code)
9663 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9664 enum tree_code code;
9665 tree type0, type1;
9666 enum tree_code code0, code1;
9667 tree cmp_type = NULL_TREE;
9669 type0 = TREE_TYPE (arg0);
9670 type1 = TREE_TYPE (arg1);
9672 code0 = TREE_CODE (type0);
9673 code1 = TREE_CODE (type1);
9675 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9676 /* Choose the wider of two real types. */
9677 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9678 ? type0 : type1;
9679 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9680 cmp_type = type0;
9681 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9682 cmp_type = type1;
9684 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9685 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9687 if (unordered_code == UNORDERED_EXPR)
9689 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9690 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9691 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9694 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9695 : ordered_code;
9696 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9697 fold_build2_loc (loc, code, type, arg0, arg1));
9700 /* Fold a call to built-in function FNDECL with 0 arguments.
9701 IGNORE is true if the result of the function call is ignored. This
9702 function returns NULL_TREE if no simplification was possible. */
9704 static tree
9705 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9707 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9708 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9709 switch (fcode)
9711 CASE_FLT_FN (BUILT_IN_INF):
9712 case BUILT_IN_INFD32:
9713 case BUILT_IN_INFD64:
9714 case BUILT_IN_INFD128:
9715 return fold_builtin_inf (loc, type, true);
9717 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9718 return fold_builtin_inf (loc, type, false);
9720 case BUILT_IN_CLASSIFY_TYPE:
9721 return fold_builtin_classify_type (NULL_TREE);
9723 default:
9724 break;
9726 return NULL_TREE;
9729 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9730 IGNORE is true if the result of the function call is ignored. This
9731 function returns NULL_TREE if no simplification was possible. */
9733 static tree
9734 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9736 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9737 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9738 switch (fcode)
9740 case BUILT_IN_CONSTANT_P:
9742 tree val = fold_builtin_constant_p (arg0);
9744 /* Gimplification will pull the CALL_EXPR for the builtin out of
9745 an if condition. When not optimizing, we'll not CSE it back.
9746 To avoid link error types of regressions, return false now. */
9747 if (!val && !optimize)
9748 val = integer_zero_node;
9750 return val;
9753 case BUILT_IN_CLASSIFY_TYPE:
9754 return fold_builtin_classify_type (arg0);
9756 case BUILT_IN_STRLEN:
9757 return fold_builtin_strlen (loc, type, arg0);
9759 CASE_FLT_FN (BUILT_IN_FABS):
9760 return fold_builtin_fabs (loc, arg0, type);
9762 case BUILT_IN_ABS:
9763 case BUILT_IN_LABS:
9764 case BUILT_IN_LLABS:
9765 case BUILT_IN_IMAXABS:
9766 return fold_builtin_abs (loc, arg0, type);
9768 CASE_FLT_FN (BUILT_IN_CONJ):
9769 if (validate_arg (arg0, COMPLEX_TYPE)
9770 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9771 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9772 break;
9774 CASE_FLT_FN (BUILT_IN_CREAL):
9775 if (validate_arg (arg0, COMPLEX_TYPE)
9776 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9777 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9778 break;
9780 CASE_FLT_FN (BUILT_IN_CIMAG):
9781 if (validate_arg (arg0, COMPLEX_TYPE)
9782 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9783 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9784 break;
9786 CASE_FLT_FN (BUILT_IN_CCOS):
9787 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9789 CASE_FLT_FN (BUILT_IN_CCOSH):
9790 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9792 CASE_FLT_FN (BUILT_IN_CPROJ):
9793 return fold_builtin_cproj(loc, arg0, type);
9795 CASE_FLT_FN (BUILT_IN_CSIN):
9796 if (validate_arg (arg0, COMPLEX_TYPE)
9797 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9798 return do_mpc_arg1 (arg0, type, mpc_sin);
9799 break;
9801 CASE_FLT_FN (BUILT_IN_CSINH):
9802 if (validate_arg (arg0, COMPLEX_TYPE)
9803 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9804 return do_mpc_arg1 (arg0, type, mpc_sinh);
9805 break;
9807 CASE_FLT_FN (BUILT_IN_CTAN):
9808 if (validate_arg (arg0, COMPLEX_TYPE)
9809 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9810 return do_mpc_arg1 (arg0, type, mpc_tan);
9811 break;
9813 CASE_FLT_FN (BUILT_IN_CTANH):
9814 if (validate_arg (arg0, COMPLEX_TYPE)
9815 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9816 return do_mpc_arg1 (arg0, type, mpc_tanh);
9817 break;
9819 CASE_FLT_FN (BUILT_IN_CLOG):
9820 if (validate_arg (arg0, COMPLEX_TYPE)
9821 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9822 return do_mpc_arg1 (arg0, type, mpc_log);
9823 break;
9825 CASE_FLT_FN (BUILT_IN_CSQRT):
9826 if (validate_arg (arg0, COMPLEX_TYPE)
9827 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9828 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9829 break;
9831 CASE_FLT_FN (BUILT_IN_CASIN):
9832 if (validate_arg (arg0, COMPLEX_TYPE)
9833 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9834 return do_mpc_arg1 (arg0, type, mpc_asin);
9835 break;
9837 CASE_FLT_FN (BUILT_IN_CACOS):
9838 if (validate_arg (arg0, COMPLEX_TYPE)
9839 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9840 return do_mpc_arg1 (arg0, type, mpc_acos);
9841 break;
9843 CASE_FLT_FN (BUILT_IN_CATAN):
9844 if (validate_arg (arg0, COMPLEX_TYPE)
9845 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9846 return do_mpc_arg1 (arg0, type, mpc_atan);
9847 break;
9849 CASE_FLT_FN (BUILT_IN_CASINH):
9850 if (validate_arg (arg0, COMPLEX_TYPE)
9851 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9852 return do_mpc_arg1 (arg0, type, mpc_asinh);
9853 break;
9855 CASE_FLT_FN (BUILT_IN_CACOSH):
9856 if (validate_arg (arg0, COMPLEX_TYPE)
9857 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9858 return do_mpc_arg1 (arg0, type, mpc_acosh);
9859 break;
9861 CASE_FLT_FN (BUILT_IN_CATANH):
9862 if (validate_arg (arg0, COMPLEX_TYPE)
9863 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9864 return do_mpc_arg1 (arg0, type, mpc_atanh);
9865 break;
9867 CASE_FLT_FN (BUILT_IN_CABS):
9868 return fold_builtin_cabs (loc, arg0, type, fndecl);
9870 CASE_FLT_FN (BUILT_IN_CARG):
9871 return fold_builtin_carg (loc, arg0, type);
9873 CASE_FLT_FN (BUILT_IN_SQRT):
9874 return fold_builtin_sqrt (loc, arg0, type);
9876 CASE_FLT_FN (BUILT_IN_CBRT):
9877 return fold_builtin_cbrt (loc, arg0, type);
9879 CASE_FLT_FN (BUILT_IN_ASIN):
9880 if (validate_arg (arg0, REAL_TYPE))
9881 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9882 &dconstm1, &dconst1, true);
9883 break;
9885 CASE_FLT_FN (BUILT_IN_ACOS):
9886 if (validate_arg (arg0, REAL_TYPE))
9887 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9888 &dconstm1, &dconst1, true);
9889 break;
9891 CASE_FLT_FN (BUILT_IN_ATAN):
9892 if (validate_arg (arg0, REAL_TYPE))
9893 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9894 break;
9896 CASE_FLT_FN (BUILT_IN_ASINH):
9897 if (validate_arg (arg0, REAL_TYPE))
9898 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9899 break;
9901 CASE_FLT_FN (BUILT_IN_ACOSH):
9902 if (validate_arg (arg0, REAL_TYPE))
9903 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9904 &dconst1, NULL, true);
9905 break;
9907 CASE_FLT_FN (BUILT_IN_ATANH):
9908 if (validate_arg (arg0, REAL_TYPE))
9909 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9910 &dconstm1, &dconst1, false);
9911 break;
9913 CASE_FLT_FN (BUILT_IN_SIN):
9914 if (validate_arg (arg0, REAL_TYPE))
9915 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9916 break;
9918 CASE_FLT_FN (BUILT_IN_COS):
9919 return fold_builtin_cos (loc, arg0, type, fndecl);
9921 CASE_FLT_FN (BUILT_IN_TAN):
9922 return fold_builtin_tan (arg0, type);
9924 CASE_FLT_FN (BUILT_IN_CEXP):
9925 return fold_builtin_cexp (loc, arg0, type);
9927 CASE_FLT_FN (BUILT_IN_CEXPI):
9928 if (validate_arg (arg0, REAL_TYPE))
9929 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9930 break;
9932 CASE_FLT_FN (BUILT_IN_SINH):
9933 if (validate_arg (arg0, REAL_TYPE))
9934 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9935 break;
9937 CASE_FLT_FN (BUILT_IN_COSH):
9938 return fold_builtin_cosh (loc, arg0, type, fndecl);
9940 CASE_FLT_FN (BUILT_IN_TANH):
9941 if (validate_arg (arg0, REAL_TYPE))
9942 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9943 break;
9945 CASE_FLT_FN (BUILT_IN_ERF):
9946 if (validate_arg (arg0, REAL_TYPE))
9947 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9948 break;
9950 CASE_FLT_FN (BUILT_IN_ERFC):
9951 if (validate_arg (arg0, REAL_TYPE))
9952 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9953 break;
9955 CASE_FLT_FN (BUILT_IN_TGAMMA):
9956 if (validate_arg (arg0, REAL_TYPE))
9957 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9958 break;
9960 CASE_FLT_FN (BUILT_IN_EXP):
9961 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
9963 CASE_FLT_FN (BUILT_IN_EXP2):
9964 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
9966 CASE_FLT_FN (BUILT_IN_EXP10):
9967 CASE_FLT_FN (BUILT_IN_POW10):
9968 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
9970 CASE_FLT_FN (BUILT_IN_EXPM1):
9971 if (validate_arg (arg0, REAL_TYPE))
9972 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
9973 break;
9975 CASE_FLT_FN (BUILT_IN_LOG):
9976 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
9978 CASE_FLT_FN (BUILT_IN_LOG2):
9979 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
9981 CASE_FLT_FN (BUILT_IN_LOG10):
9982 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
9984 CASE_FLT_FN (BUILT_IN_LOG1P):
9985 if (validate_arg (arg0, REAL_TYPE))
9986 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
9987 &dconstm1, NULL, false);
9988 break;
9990 CASE_FLT_FN (BUILT_IN_J0):
9991 if (validate_arg (arg0, REAL_TYPE))
9992 return do_mpfr_arg1 (arg0, type, mpfr_j0,
9993 NULL, NULL, 0);
9994 break;
9996 CASE_FLT_FN (BUILT_IN_J1):
9997 if (validate_arg (arg0, REAL_TYPE))
9998 return do_mpfr_arg1 (arg0, type, mpfr_j1,
9999 NULL, NULL, 0);
10000 break;
10002 CASE_FLT_FN (BUILT_IN_Y0):
10003 if (validate_arg (arg0, REAL_TYPE))
10004 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10005 &dconst0, NULL, false);
10006 break;
10008 CASE_FLT_FN (BUILT_IN_Y1):
10009 if (validate_arg (arg0, REAL_TYPE))
10010 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10011 &dconst0, NULL, false);
10012 break;
10014 CASE_FLT_FN (BUILT_IN_NAN):
10015 case BUILT_IN_NAND32:
10016 case BUILT_IN_NAND64:
10017 case BUILT_IN_NAND128:
10018 return fold_builtin_nan (arg0, type, true);
10020 CASE_FLT_FN (BUILT_IN_NANS):
10021 return fold_builtin_nan (arg0, type, false);
10023 CASE_FLT_FN (BUILT_IN_FLOOR):
10024 return fold_builtin_floor (loc, fndecl, arg0);
10026 CASE_FLT_FN (BUILT_IN_CEIL):
10027 return fold_builtin_ceil (loc, fndecl, arg0);
10029 CASE_FLT_FN (BUILT_IN_TRUNC):
10030 return fold_builtin_trunc (loc, fndecl, arg0);
10032 CASE_FLT_FN (BUILT_IN_ROUND):
10033 return fold_builtin_round (loc, fndecl, arg0);
10035 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10036 CASE_FLT_FN (BUILT_IN_RINT):
10037 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10039 CASE_FLT_FN (BUILT_IN_LCEIL):
10040 CASE_FLT_FN (BUILT_IN_LLCEIL):
10041 CASE_FLT_FN (BUILT_IN_LFLOOR):
10042 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10043 CASE_FLT_FN (BUILT_IN_LROUND):
10044 CASE_FLT_FN (BUILT_IN_LLROUND):
10045 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10047 CASE_FLT_FN (BUILT_IN_LRINT):
10048 CASE_FLT_FN (BUILT_IN_LLRINT):
10049 return fold_fixed_mathfn (loc, fndecl, arg0);
10051 case BUILT_IN_BSWAP32:
10052 case BUILT_IN_BSWAP64:
10053 return fold_builtin_bswap (fndecl, arg0);
10055 CASE_INT_FN (BUILT_IN_FFS):
10056 CASE_INT_FN (BUILT_IN_CLZ):
10057 CASE_INT_FN (BUILT_IN_CTZ):
10058 CASE_INT_FN (BUILT_IN_POPCOUNT):
10059 CASE_INT_FN (BUILT_IN_PARITY):
10060 return fold_builtin_bitop (fndecl, arg0);
10062 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10063 return fold_builtin_signbit (loc, arg0, type);
10065 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10066 return fold_builtin_significand (loc, arg0, type);
10068 CASE_FLT_FN (BUILT_IN_ILOGB):
10069 CASE_FLT_FN (BUILT_IN_LOGB):
10070 return fold_builtin_logb (loc, arg0, type);
10072 case BUILT_IN_ISASCII:
10073 return fold_builtin_isascii (loc, arg0);
10075 case BUILT_IN_TOASCII:
10076 return fold_builtin_toascii (loc, arg0);
10078 case BUILT_IN_ISDIGIT:
10079 return fold_builtin_isdigit (loc, arg0);
10081 CASE_FLT_FN (BUILT_IN_FINITE):
10082 case BUILT_IN_FINITED32:
10083 case BUILT_IN_FINITED64:
10084 case BUILT_IN_FINITED128:
10085 case BUILT_IN_ISFINITE:
10087 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10088 if (ret)
10089 return ret;
10090 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10093 CASE_FLT_FN (BUILT_IN_ISINF):
10094 case BUILT_IN_ISINFD32:
10095 case BUILT_IN_ISINFD64:
10096 case BUILT_IN_ISINFD128:
10098 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10099 if (ret)
10100 return ret;
10101 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10104 case BUILT_IN_ISNORMAL:
10105 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10107 case BUILT_IN_ISINF_SIGN:
10108 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10110 CASE_FLT_FN (BUILT_IN_ISNAN):
10111 case BUILT_IN_ISNAND32:
10112 case BUILT_IN_ISNAND64:
10113 case BUILT_IN_ISNAND128:
10114 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10116 case BUILT_IN_PRINTF:
10117 case BUILT_IN_PRINTF_UNLOCKED:
10118 case BUILT_IN_VPRINTF:
10119 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10121 case BUILT_IN_FREE:
10122 if (integer_zerop (arg0))
10123 return build_empty_stmt (loc);
10124 break;
10126 default:
10127 break;
10130 return NULL_TREE;
10134 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10135 IGNORE is true if the result of the function call is ignored. This
10136 function returns NULL_TREE if no simplification was possible. */
10138 static tree
10139 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10141 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10142 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10144 switch (fcode)
10146 CASE_FLT_FN (BUILT_IN_JN):
10147 if (validate_arg (arg0, INTEGER_TYPE)
10148 && validate_arg (arg1, REAL_TYPE))
10149 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10150 break;
10152 CASE_FLT_FN (BUILT_IN_YN):
10153 if (validate_arg (arg0, INTEGER_TYPE)
10154 && validate_arg (arg1, REAL_TYPE))
10155 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10156 &dconst0, false);
10157 break;
10159 CASE_FLT_FN (BUILT_IN_DREM):
10160 CASE_FLT_FN (BUILT_IN_REMAINDER):
10161 if (validate_arg (arg0, REAL_TYPE)
10162 && validate_arg(arg1, REAL_TYPE))
10163 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10164 break;
10166 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10167 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10168 if (validate_arg (arg0, REAL_TYPE)
10169 && validate_arg(arg1, POINTER_TYPE))
10170 return do_mpfr_lgamma_r (arg0, arg1, type);
10171 break;
10173 CASE_FLT_FN (BUILT_IN_ATAN2):
10174 if (validate_arg (arg0, REAL_TYPE)
10175 && validate_arg(arg1, REAL_TYPE))
10176 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10177 break;
10179 CASE_FLT_FN (BUILT_IN_FDIM):
10180 if (validate_arg (arg0, REAL_TYPE)
10181 && validate_arg(arg1, REAL_TYPE))
10182 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10183 break;
10185 CASE_FLT_FN (BUILT_IN_HYPOT):
10186 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10188 CASE_FLT_FN (BUILT_IN_CPOW):
10189 if (validate_arg (arg0, COMPLEX_TYPE)
10190 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10191 && validate_arg (arg1, COMPLEX_TYPE)
10192 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10193 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10194 break;
10196 CASE_FLT_FN (BUILT_IN_LDEXP):
10197 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10198 CASE_FLT_FN (BUILT_IN_SCALBN):
10199 CASE_FLT_FN (BUILT_IN_SCALBLN):
10200 return fold_builtin_load_exponent (loc, arg0, arg1,
10201 type, /*ldexp=*/false);
10203 CASE_FLT_FN (BUILT_IN_FREXP):
10204 return fold_builtin_frexp (loc, arg0, arg1, type);
10206 CASE_FLT_FN (BUILT_IN_MODF):
10207 return fold_builtin_modf (loc, arg0, arg1, type);
10209 case BUILT_IN_BZERO:
10210 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10212 case BUILT_IN_FPUTS:
10213 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10215 case BUILT_IN_FPUTS_UNLOCKED:
10216 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10218 case BUILT_IN_STRSTR:
10219 return fold_builtin_strstr (loc, arg0, arg1, type);
10221 case BUILT_IN_STRCAT:
10222 return fold_builtin_strcat (loc, arg0, arg1);
10224 case BUILT_IN_STRSPN:
10225 return fold_builtin_strspn (loc, arg0, arg1);
10227 case BUILT_IN_STRCSPN:
10228 return fold_builtin_strcspn (loc, arg0, arg1);
10230 case BUILT_IN_STRCHR:
10231 case BUILT_IN_INDEX:
10232 return fold_builtin_strchr (loc, arg0, arg1, type);
10234 case BUILT_IN_STRRCHR:
10235 case BUILT_IN_RINDEX:
10236 return fold_builtin_strrchr (loc, arg0, arg1, type);
10238 case BUILT_IN_STRCPY:
10239 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10241 case BUILT_IN_STPCPY:
10242 if (ignore)
10244 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10245 if (!fn)
10246 break;
10248 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10250 else
10251 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10252 break;
10254 case BUILT_IN_STRCMP:
10255 return fold_builtin_strcmp (loc, arg0, arg1);
10257 case BUILT_IN_STRPBRK:
10258 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10260 case BUILT_IN_EXPECT:
10261 return fold_builtin_expect (loc, arg0, arg1);
10263 CASE_FLT_FN (BUILT_IN_POW):
10264 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10266 CASE_FLT_FN (BUILT_IN_POWI):
10267 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10269 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10270 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10272 CASE_FLT_FN (BUILT_IN_FMIN):
10273 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10275 CASE_FLT_FN (BUILT_IN_FMAX):
10276 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10278 case BUILT_IN_ISGREATER:
10279 return fold_builtin_unordered_cmp (loc, fndecl,
10280 arg0, arg1, UNLE_EXPR, LE_EXPR);
10281 case BUILT_IN_ISGREATEREQUAL:
10282 return fold_builtin_unordered_cmp (loc, fndecl,
10283 arg0, arg1, UNLT_EXPR, LT_EXPR);
10284 case BUILT_IN_ISLESS:
10285 return fold_builtin_unordered_cmp (loc, fndecl,
10286 arg0, arg1, UNGE_EXPR, GE_EXPR);
10287 case BUILT_IN_ISLESSEQUAL:
10288 return fold_builtin_unordered_cmp (loc, fndecl,
10289 arg0, arg1, UNGT_EXPR, GT_EXPR);
10290 case BUILT_IN_ISLESSGREATER:
10291 return fold_builtin_unordered_cmp (loc, fndecl,
10292 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10293 case BUILT_IN_ISUNORDERED:
10294 return fold_builtin_unordered_cmp (loc, fndecl,
10295 arg0, arg1, UNORDERED_EXPR,
10296 NOP_EXPR);
10298 /* We do the folding for va_start in the expander. */
10299 case BUILT_IN_VA_START:
10300 break;
10302 case BUILT_IN_SPRINTF:
10303 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10305 case BUILT_IN_OBJECT_SIZE:
10306 return fold_builtin_object_size (arg0, arg1);
10308 case BUILT_IN_PRINTF:
10309 case BUILT_IN_PRINTF_UNLOCKED:
10310 case BUILT_IN_VPRINTF:
10311 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10313 case BUILT_IN_PRINTF_CHK:
10314 case BUILT_IN_VPRINTF_CHK:
10315 if (!validate_arg (arg0, INTEGER_TYPE)
10316 || TREE_SIDE_EFFECTS (arg0))
10317 return NULL_TREE;
10318 else
10319 return fold_builtin_printf (loc, fndecl,
10320 arg1, NULL_TREE, ignore, fcode);
10321 break;
10323 case BUILT_IN_FPRINTF:
10324 case BUILT_IN_FPRINTF_UNLOCKED:
10325 case BUILT_IN_VFPRINTF:
10326 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10327 ignore, fcode);
10329 default:
10330 break;
10332 return NULL_TREE;
10335 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10336 and ARG2. IGNORE is true if the result of the function call is ignored.
10337 This function returns NULL_TREE if no simplification was possible. */
10339 static tree
10340 fold_builtin_3 (location_t loc, tree fndecl,
10341 tree arg0, tree arg1, tree arg2, bool ignore)
10343 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10344 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10345 switch (fcode)
10348 CASE_FLT_FN (BUILT_IN_SINCOS):
10349 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10351 CASE_FLT_FN (BUILT_IN_FMA):
10352 if (validate_arg (arg0, REAL_TYPE)
10353 && validate_arg(arg1, REAL_TYPE)
10354 && validate_arg(arg2, REAL_TYPE))
10355 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_REMQUO):
10359 if (validate_arg (arg0, REAL_TYPE)
10360 && validate_arg(arg1, REAL_TYPE)
10361 && validate_arg(arg2, POINTER_TYPE))
10362 return do_mpfr_remquo (arg0, arg1, arg2);
10363 break;
10365 case BUILT_IN_MEMSET:
10366 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10368 case BUILT_IN_BCOPY:
10369 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10370 void_type_node, true, /*endp=*/3);
10372 case BUILT_IN_MEMCPY:
10373 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10374 type, ignore, /*endp=*/0);
10376 case BUILT_IN_MEMPCPY:
10377 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10378 type, ignore, /*endp=*/1);
10380 case BUILT_IN_MEMMOVE:
10381 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10382 type, ignore, /*endp=*/3);
10384 case BUILT_IN_STRNCAT:
10385 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10387 case BUILT_IN_STRNCPY:
10388 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10390 case BUILT_IN_STRNCMP:
10391 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10393 case BUILT_IN_MEMCHR:
10394 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10396 case BUILT_IN_BCMP:
10397 case BUILT_IN_MEMCMP:
10398 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10400 case BUILT_IN_SPRINTF:
10401 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10403 case BUILT_IN_STRCPY_CHK:
10404 case BUILT_IN_STPCPY_CHK:
10405 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10406 ignore, fcode);
10408 case BUILT_IN_STRCAT_CHK:
10409 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10411 case BUILT_IN_PRINTF_CHK:
10412 case BUILT_IN_VPRINTF_CHK:
10413 if (!validate_arg (arg0, INTEGER_TYPE)
10414 || TREE_SIDE_EFFECTS (arg0))
10415 return NULL_TREE;
10416 else
10417 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10418 break;
10420 case BUILT_IN_FPRINTF:
10421 case BUILT_IN_FPRINTF_UNLOCKED:
10422 case BUILT_IN_VFPRINTF:
10423 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10424 ignore, fcode);
10426 case BUILT_IN_FPRINTF_CHK:
10427 case BUILT_IN_VFPRINTF_CHK:
10428 if (!validate_arg (arg1, INTEGER_TYPE)
10429 || TREE_SIDE_EFFECTS (arg1))
10430 return NULL_TREE;
10431 else
10432 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10433 ignore, fcode);
10435 default:
10436 break;
10438 return NULL_TREE;
10441 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10442 ARG2, and ARG3. IGNORE is true if the result of the function call is
10443 ignored. This function returns NULL_TREE if no simplification was
10444 possible. */
10446 static tree
10447 fold_builtin_4 (location_t loc, tree fndecl,
10448 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10450 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10452 switch (fcode)
10454 case BUILT_IN_MEMCPY_CHK:
10455 case BUILT_IN_MEMPCPY_CHK:
10456 case BUILT_IN_MEMMOVE_CHK:
10457 case BUILT_IN_MEMSET_CHK:
10458 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10459 NULL_TREE, ignore,
10460 DECL_FUNCTION_CODE (fndecl));
10462 case BUILT_IN_STRNCPY_CHK:
10463 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10465 case BUILT_IN_STRNCAT_CHK:
10466 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10468 case BUILT_IN_FPRINTF_CHK:
10469 case BUILT_IN_VFPRINTF_CHK:
10470 if (!validate_arg (arg1, INTEGER_TYPE)
10471 || TREE_SIDE_EFFECTS (arg1))
10472 return NULL_TREE;
10473 else
10474 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10475 ignore, fcode);
10476 break;
10478 default:
10479 break;
10481 return NULL_TREE;
10484 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10485 arguments, where NARGS <= 4. IGNORE is true if the result of the
10486 function call is ignored. This function returns NULL_TREE if no
10487 simplification was possible. Note that this only folds builtins with
10488 fixed argument patterns. Foldings that do varargs-to-varargs
10489 transformations, or that match calls with more than 4 arguments,
10490 need to be handled with fold_builtin_varargs instead. */
10492 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10494 static tree
10495 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10497 tree ret = NULL_TREE;
10499 switch (nargs)
10501 case 0:
10502 ret = fold_builtin_0 (loc, fndecl, ignore);
10503 break;
10504 case 1:
10505 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10506 break;
10507 case 2:
10508 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10509 break;
10510 case 3:
10511 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10512 break;
10513 case 4:
10514 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10515 ignore);
10516 break;
10517 default:
10518 break;
10520 if (ret)
10522 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10523 SET_EXPR_LOCATION (ret, loc);
10524 TREE_NO_WARNING (ret) = 1;
10525 return ret;
10527 return NULL_TREE;
10530 /* Builtins with folding operations that operate on "..." arguments
10531 need special handling; we need to store the arguments in a convenient
10532 data structure before attempting any folding. Fortunately there are
10533 only a few builtins that fall into this category. FNDECL is the
10534 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10535 result of the function call is ignored. */
10537 static tree
10538 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10539 bool ignore ATTRIBUTE_UNUSED)
10541 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10542 tree ret = NULL_TREE;
10544 switch (fcode)
10546 case BUILT_IN_SPRINTF_CHK:
10547 case BUILT_IN_VSPRINTF_CHK:
10548 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10549 break;
10551 case BUILT_IN_SNPRINTF_CHK:
10552 case BUILT_IN_VSNPRINTF_CHK:
10553 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10554 break;
10556 case BUILT_IN_FPCLASSIFY:
10557 ret = fold_builtin_fpclassify (loc, exp);
10558 break;
10560 default:
10561 break;
10563 if (ret)
10565 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10566 SET_EXPR_LOCATION (ret, loc);
10567 TREE_NO_WARNING (ret) = 1;
10568 return ret;
10570 return NULL_TREE;
10573 /* Return true if FNDECL shouldn't be folded right now.
10574 If a built-in function has an inline attribute always_inline
10575 wrapper, defer folding it after always_inline functions have
10576 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10577 might not be performed. */
10579 static bool
10580 avoid_folding_inline_builtin (tree fndecl)
10582 return (DECL_DECLARED_INLINE_P (fndecl)
10583 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10584 && cfun
10585 && !cfun->always_inline_functions_inlined
10586 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10589 /* A wrapper function for builtin folding that prevents warnings for
10590 "statement without effect" and the like, caused by removing the
10591 call node earlier than the warning is generated. */
10593 tree
10594 fold_call_expr (location_t loc, tree exp, bool ignore)
10596 tree ret = NULL_TREE;
10597 tree fndecl = get_callee_fndecl (exp);
10598 if (fndecl
10599 && TREE_CODE (fndecl) == FUNCTION_DECL
10600 && DECL_BUILT_IN (fndecl)
10601 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10602 yet. Defer folding until we see all the arguments
10603 (after inlining). */
10604 && !CALL_EXPR_VA_ARG_PACK (exp))
10606 int nargs = call_expr_nargs (exp);
10608 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10609 instead last argument is __builtin_va_arg_pack (). Defer folding
10610 even in that case, until arguments are finalized. */
10611 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10613 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10614 if (fndecl2
10615 && TREE_CODE (fndecl2) == FUNCTION_DECL
10616 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10617 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10618 return NULL_TREE;
10621 if (avoid_folding_inline_builtin (fndecl))
10622 return NULL_TREE;
10624 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10625 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10626 CALL_EXPR_ARGP (exp), ignore);
10627 else
10629 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10631 tree *args = CALL_EXPR_ARGP (exp);
10632 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10634 if (!ret)
10635 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10636 if (ret)
10637 return ret;
10640 return NULL_TREE;
10643 /* Conveniently construct a function call expression. FNDECL names the
10644 function to be called and N arguments are passed in the array
10645 ARGARRAY. */
10647 tree
10648 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10650 tree fntype = TREE_TYPE (fndecl);
10651 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10653 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10656 /* Conveniently construct a function call expression. FNDECL names the
10657 function to be called and the arguments are passed in the vector
10658 VEC. */
10660 tree
10661 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10663 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10664 VEC_address (tree, vec));
10668 /* Conveniently construct a function call expression. FNDECL names the
10669 function to be called, N is the number of arguments, and the "..."
10670 parameters are the argument expressions. */
10672 tree
10673 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10675 va_list ap;
10676 tree *argarray = XALLOCAVEC (tree, n);
10677 int i;
10679 va_start (ap, n);
10680 for (i = 0; i < n; i++)
10681 argarray[i] = va_arg (ap, tree);
10682 va_end (ap);
10683 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10686 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10687 varargs macros aren't supported by all bootstrap compilers. */
10689 tree
10690 build_call_expr (tree fndecl, int n, ...)
10692 va_list ap;
10693 tree *argarray = XALLOCAVEC (tree, n);
10694 int i;
10696 va_start (ap, n);
10697 for (i = 0; i < n; i++)
10698 argarray[i] = va_arg (ap, tree);
10699 va_end (ap);
10700 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10703 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10704 N arguments are passed in the array ARGARRAY. */
10706 tree
10707 fold_builtin_call_array (location_t loc, tree type,
10708 tree fn,
10709 int n,
10710 tree *argarray)
10712 tree ret = NULL_TREE;
10713 tree exp;
10715 if (TREE_CODE (fn) == ADDR_EXPR)
10717 tree fndecl = TREE_OPERAND (fn, 0);
10718 if (TREE_CODE (fndecl) == FUNCTION_DECL
10719 && DECL_BUILT_IN (fndecl))
10721 /* If last argument is __builtin_va_arg_pack (), arguments to this
10722 function are not finalized yet. Defer folding until they are. */
10723 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10725 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10726 if (fndecl2
10727 && TREE_CODE (fndecl2) == FUNCTION_DECL
10728 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10729 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10730 return build_call_array_loc (loc, type, fn, n, argarray);
10732 if (avoid_folding_inline_builtin (fndecl))
10733 return build_call_array_loc (loc, type, fn, n, argarray);
10734 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10736 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10737 if (ret)
10738 return ret;
10740 return build_call_array_loc (loc, type, fn, n, argarray);
10742 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10744 /* First try the transformations that don't require consing up
10745 an exp. */
10746 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10747 if (ret)
10748 return ret;
10751 /* If we got this far, we need to build an exp. */
10752 exp = build_call_array_loc (loc, type, fn, n, argarray);
10753 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10754 return ret ? ret : exp;
10758 return build_call_array_loc (loc, type, fn, n, argarray);
10761 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10762 along with N new arguments specified as the "..." parameters. SKIP
10763 is the number of arguments in EXP to be omitted. This function is used
10764 to do varargs-to-varargs transformations. */
10766 static tree
10767 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10769 int oldnargs = call_expr_nargs (exp);
10770 int nargs = oldnargs - skip + n;
10771 tree fntype = TREE_TYPE (fndecl);
10772 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10773 tree *buffer;
10775 if (n > 0)
10777 int i, j;
10778 va_list ap;
10780 buffer = XALLOCAVEC (tree, nargs);
10781 va_start (ap, n);
10782 for (i = 0; i < n; i++)
10783 buffer[i] = va_arg (ap, tree);
10784 va_end (ap);
10785 for (j = skip; j < oldnargs; j++, i++)
10786 buffer[i] = CALL_EXPR_ARG (exp, j);
10788 else
10789 buffer = CALL_EXPR_ARGP (exp) + skip;
10791 return fold (build_call_array_loc (loc, TREE_TYPE (exp), fn, nargs, buffer));
10794 /* Validate a single argument ARG against a tree code CODE representing
10795 a type. */
10797 static bool
10798 validate_arg (const_tree arg, enum tree_code code)
10800 if (!arg)
10801 return false;
10802 else if (code == POINTER_TYPE)
10803 return POINTER_TYPE_P (TREE_TYPE (arg));
10804 else if (code == INTEGER_TYPE)
10805 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10806 return code == TREE_CODE (TREE_TYPE (arg));
10809 /* This function validates the types of a function call argument list
10810 against a specified list of tree_codes. If the last specifier is a 0,
10811 that represents an ellipses, otherwise the last specifier must be a
10812 VOID_TYPE.
10814 This is the GIMPLE version of validate_arglist. Eventually we want to
10815 completely convert builtins.c to work from GIMPLEs and the tree based
10816 validate_arglist will then be removed. */
10818 bool
10819 validate_gimple_arglist (const_gimple call, ...)
10821 enum tree_code code;
10822 bool res = 0;
10823 va_list ap;
10824 const_tree arg;
10825 size_t i;
10827 va_start (ap, call);
10828 i = 0;
10832 code = (enum tree_code) va_arg (ap, int);
10833 switch (code)
10835 case 0:
10836 /* This signifies an ellipses, any further arguments are all ok. */
10837 res = true;
10838 goto end;
10839 case VOID_TYPE:
10840 /* This signifies an endlink, if no arguments remain, return
10841 true, otherwise return false. */
10842 res = (i == gimple_call_num_args (call));
10843 goto end;
10844 default:
10845 /* If no parameters remain or the parameter's code does not
10846 match the specified code, return false. Otherwise continue
10847 checking any remaining arguments. */
10848 arg = gimple_call_arg (call, i++);
10849 if (!validate_arg (arg, code))
10850 goto end;
10851 break;
10854 while (1);
10856 /* We need gotos here since we can only have one VA_CLOSE in a
10857 function. */
10858 end: ;
10859 va_end (ap);
10861 return res;
10864 /* This function validates the types of a function call argument list
10865 against a specified list of tree_codes. If the last specifier is a 0,
10866 that represents an ellipses, otherwise the last specifier must be a
10867 VOID_TYPE. */
10869 bool
10870 validate_arglist (const_tree callexpr, ...)
10872 enum tree_code code;
10873 bool res = 0;
10874 va_list ap;
10875 const_call_expr_arg_iterator iter;
10876 const_tree arg;
10878 va_start (ap, callexpr);
10879 init_const_call_expr_arg_iterator (callexpr, &iter);
10883 code = (enum tree_code) va_arg (ap, int);
10884 switch (code)
10886 case 0:
10887 /* This signifies an ellipses, any further arguments are all ok. */
10888 res = true;
10889 goto end;
10890 case VOID_TYPE:
10891 /* This signifies an endlink, if no arguments remain, return
10892 true, otherwise return false. */
10893 res = !more_const_call_expr_args_p (&iter);
10894 goto end;
10895 default:
10896 /* If no parameters remain or the parameter's code does not
10897 match the specified code, return false. Otherwise continue
10898 checking any remaining arguments. */
10899 arg = next_const_call_expr_arg (&iter);
10900 if (!validate_arg (arg, code))
10901 goto end;
10902 break;
10905 while (1);
10907 /* We need gotos here since we can only have one VA_CLOSE in a
10908 function. */
10909 end: ;
10910 va_end (ap);
10912 return res;
10915 /* Default target-specific builtin expander that does nothing. */
10918 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10919 rtx target ATTRIBUTE_UNUSED,
10920 rtx subtarget ATTRIBUTE_UNUSED,
10921 enum machine_mode mode ATTRIBUTE_UNUSED,
10922 int ignore ATTRIBUTE_UNUSED)
10924 return NULL_RTX;
10927 /* Returns true is EXP represents data that would potentially reside
10928 in a readonly section. */
10930 static bool
10931 readonly_data_expr (tree exp)
10933 STRIP_NOPS (exp);
10935 if (TREE_CODE (exp) != ADDR_EXPR)
10936 return false;
10938 exp = get_base_address (TREE_OPERAND (exp, 0));
10939 if (!exp)
10940 return false;
10942 /* Make sure we call decl_readonly_section only for trees it
10943 can handle (since it returns true for everything it doesn't
10944 understand). */
10945 if (TREE_CODE (exp) == STRING_CST
10946 || TREE_CODE (exp) == CONSTRUCTOR
10947 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10948 return decl_readonly_section (exp, 0);
10949 else
10950 return false;
10953 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10954 to the call, and TYPE is its return type.
10956 Return NULL_TREE if no simplification was possible, otherwise return the
10957 simplified form of the call as a tree.
10959 The simplified form may be a constant or other expression which
10960 computes the same value, but in a more efficient manner (including
10961 calls to other builtin functions).
10963 The call may contain arguments which need to be evaluated, but
10964 which are not useful to determine the result of the call. In
10965 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10966 COMPOUND_EXPR will be an argument which must be evaluated.
10967 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10968 COMPOUND_EXPR in the chain will contain the tree for the simplified
10969 form of the builtin function call. */
10971 static tree
10972 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10974 if (!validate_arg (s1, POINTER_TYPE)
10975 || !validate_arg (s2, POINTER_TYPE))
10976 return NULL_TREE;
10977 else
10979 tree fn;
10980 const char *p1, *p2;
10982 p2 = c_getstr (s2);
10983 if (p2 == NULL)
10984 return NULL_TREE;
10986 p1 = c_getstr (s1);
10987 if (p1 != NULL)
10989 const char *r = strstr (p1, p2);
10990 tree tem;
10992 if (r == NULL)
10993 return build_int_cst (TREE_TYPE (s1), 0);
10995 /* Return an offset into the constant string argument. */
10996 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
10997 s1, size_int (r - p1));
10998 return fold_convert_loc (loc, type, tem);
11001 /* The argument is const char *, and the result is char *, so we need
11002 a type conversion here to avoid a warning. */
11003 if (p2[0] == '\0')
11004 return fold_convert_loc (loc, type, s1);
11006 if (p2[1] != '\0')
11007 return NULL_TREE;
11009 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11010 if (!fn)
11011 return NULL_TREE;
11013 /* New argument list transforming strstr(s1, s2) to
11014 strchr(s1, s2[0]). */
11015 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11019 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11020 the call, and TYPE is its return type.
11022 Return NULL_TREE if no simplification was possible, otherwise return the
11023 simplified form of the call as a tree.
11025 The simplified form may be a constant or other expression which
11026 computes the same value, but in a more efficient manner (including
11027 calls to other builtin functions).
11029 The call may contain arguments which need to be evaluated, but
11030 which are not useful to determine the result of the call. In
11031 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11032 COMPOUND_EXPR will be an argument which must be evaluated.
11033 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11034 COMPOUND_EXPR in the chain will contain the tree for the simplified
11035 form of the builtin function call. */
11037 static tree
11038 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11040 if (!validate_arg (s1, POINTER_TYPE)
11041 || !validate_arg (s2, INTEGER_TYPE))
11042 return NULL_TREE;
11043 else
11045 const char *p1;
11047 if (TREE_CODE (s2) != INTEGER_CST)
11048 return NULL_TREE;
11050 p1 = c_getstr (s1);
11051 if (p1 != NULL)
11053 char c;
11054 const char *r;
11055 tree tem;
11057 if (target_char_cast (s2, &c))
11058 return NULL_TREE;
11060 r = strchr (p1, c);
11062 if (r == NULL)
11063 return build_int_cst (TREE_TYPE (s1), 0);
11065 /* Return an offset into the constant string argument. */
11066 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11067 s1, size_int (r - p1));
11068 return fold_convert_loc (loc, type, tem);
11070 return NULL_TREE;
11074 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11075 the call, and TYPE is its return type.
11077 Return NULL_TREE if no simplification was possible, otherwise return the
11078 simplified form of the call as a tree.
11080 The simplified form may be a constant or other expression which
11081 computes the same value, but in a more efficient manner (including
11082 calls to other builtin functions).
11084 The call may contain arguments which need to be evaluated, but
11085 which are not useful to determine the result of the call. In
11086 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11087 COMPOUND_EXPR will be an argument which must be evaluated.
11088 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11089 COMPOUND_EXPR in the chain will contain the tree for the simplified
11090 form of the builtin function call. */
11092 static tree
11093 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11095 if (!validate_arg (s1, POINTER_TYPE)
11096 || !validate_arg (s2, INTEGER_TYPE))
11097 return NULL_TREE;
11098 else
11100 tree fn;
11101 const char *p1;
11103 if (TREE_CODE (s2) != INTEGER_CST)
11104 return NULL_TREE;
11106 p1 = c_getstr (s1);
11107 if (p1 != NULL)
11109 char c;
11110 const char *r;
11111 tree tem;
11113 if (target_char_cast (s2, &c))
11114 return NULL_TREE;
11116 r = strrchr (p1, c);
11118 if (r == NULL)
11119 return build_int_cst (TREE_TYPE (s1), 0);
11121 /* Return an offset into the constant string argument. */
11122 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11123 s1, size_int (r - p1));
11124 return fold_convert_loc (loc, type, tem);
11127 if (! integer_zerop (s2))
11128 return NULL_TREE;
11130 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11131 if (!fn)
11132 return NULL_TREE;
11134 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11135 return build_call_expr_loc (loc, fn, 2, s1, s2);
11139 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11140 to the call, and TYPE is its return type.
11142 Return NULL_TREE if no simplification was possible, otherwise return the
11143 simplified form of the call as a tree.
11145 The simplified form may be a constant or other expression which
11146 computes the same value, but in a more efficient manner (including
11147 calls to other builtin functions).
11149 The call may contain arguments which need to be evaluated, but
11150 which are not useful to determine the result of the call. In
11151 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11152 COMPOUND_EXPR will be an argument which must be evaluated.
11153 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11154 COMPOUND_EXPR in the chain will contain the tree for the simplified
11155 form of the builtin function call. */
11157 static tree
11158 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11160 if (!validate_arg (s1, POINTER_TYPE)
11161 || !validate_arg (s2, POINTER_TYPE))
11162 return NULL_TREE;
11163 else
11165 tree fn;
11166 const char *p1, *p2;
11168 p2 = c_getstr (s2);
11169 if (p2 == NULL)
11170 return NULL_TREE;
11172 p1 = c_getstr (s1);
11173 if (p1 != NULL)
11175 const char *r = strpbrk (p1, p2);
11176 tree tem;
11178 if (r == NULL)
11179 return build_int_cst (TREE_TYPE (s1), 0);
11181 /* Return an offset into the constant string argument. */
11182 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11183 s1, size_int (r - p1));
11184 return fold_convert_loc (loc, type, tem);
11187 if (p2[0] == '\0')
11188 /* strpbrk(x, "") == NULL.
11189 Evaluate and ignore s1 in case it had side-effects. */
11190 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11192 if (p2[1] != '\0')
11193 return NULL_TREE; /* Really call strpbrk. */
11195 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11196 if (!fn)
11197 return NULL_TREE;
11199 /* New argument list transforming strpbrk(s1, s2) to
11200 strchr(s1, s2[0]). */
11201 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11205 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11206 to the call.
11208 Return NULL_TREE if no simplification was possible, otherwise return the
11209 simplified form of the call as a tree.
11211 The simplified form may be a constant or other expression which
11212 computes the same value, but in a more efficient manner (including
11213 calls to other builtin functions).
11215 The call may contain arguments which need to be evaluated, but
11216 which are not useful to determine the result of the call. In
11217 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11218 COMPOUND_EXPR will be an argument which must be evaluated.
11219 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11220 COMPOUND_EXPR in the chain will contain the tree for the simplified
11221 form of the builtin function call. */
11223 static tree
11224 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11226 if (!validate_arg (dst, POINTER_TYPE)
11227 || !validate_arg (src, POINTER_TYPE))
11228 return NULL_TREE;
11229 else
11231 const char *p = c_getstr (src);
11233 /* If the string length is zero, return the dst parameter. */
11234 if (p && *p == '\0')
11235 return dst;
11237 if (optimize_insn_for_speed_p ())
11239 /* See if we can store by pieces into (dst + strlen(dst)). */
11240 tree newdst, call;
11241 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11242 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11244 if (!strlen_fn || !strcpy_fn)
11245 return NULL_TREE;
11247 /* If we don't have a movstr we don't want to emit an strcpy
11248 call. We have to do that if the length of the source string
11249 isn't computable (in that case we can use memcpy probably
11250 later expanding to a sequence of mov instructions). If we
11251 have movstr instructions we can emit strcpy calls. */
11252 if (!HAVE_movstr)
11254 tree len = c_strlen (src, 1);
11255 if (! len || TREE_SIDE_EFFECTS (len))
11256 return NULL_TREE;
11259 /* Stabilize the argument list. */
11260 dst = builtin_save_expr (dst);
11262 /* Create strlen (dst). */
11263 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11264 /* Create (dst p+ strlen (dst)). */
11266 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11267 TREE_TYPE (dst), dst, newdst);
11268 newdst = builtin_save_expr (newdst);
11270 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11271 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11273 return NULL_TREE;
11277 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11278 arguments to the call.
11280 Return NULL_TREE if no simplification was possible, otherwise return the
11281 simplified form of the call as a tree.
11283 The simplified form may be a constant or other expression which
11284 computes the same value, but in a more efficient manner (including
11285 calls to other builtin functions).
11287 The call may contain arguments which need to be evaluated, but
11288 which are not useful to determine the result of the call. In
11289 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11290 COMPOUND_EXPR will be an argument which must be evaluated.
11291 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11292 COMPOUND_EXPR in the chain will contain the tree for the simplified
11293 form of the builtin function call. */
11295 static tree
11296 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11298 if (!validate_arg (dst, POINTER_TYPE)
11299 || !validate_arg (src, POINTER_TYPE)
11300 || !validate_arg (len, INTEGER_TYPE))
11301 return NULL_TREE;
11302 else
11304 const char *p = c_getstr (src);
11306 /* If the requested length is zero, or the src parameter string
11307 length is zero, return the dst parameter. */
11308 if (integer_zerop (len) || (p && *p == '\0'))
11309 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11311 /* If the requested len is greater than or equal to the string
11312 length, call strcat. */
11313 if (TREE_CODE (len) == INTEGER_CST && p
11314 && compare_tree_int (len, strlen (p)) >= 0)
11316 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11318 /* If the replacement _DECL isn't initialized, don't do the
11319 transformation. */
11320 if (!fn)
11321 return NULL_TREE;
11323 return build_call_expr_loc (loc, fn, 2, dst, src);
11325 return NULL_TREE;
11329 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11330 to the call.
11332 Return NULL_TREE if no simplification was possible, otherwise return the
11333 simplified form of the call as a tree.
11335 The simplified form may be a constant or other expression which
11336 computes the same value, but in a more efficient manner (including
11337 calls to other builtin functions).
11339 The call may contain arguments which need to be evaluated, but
11340 which are not useful to determine the result of the call. In
11341 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11342 COMPOUND_EXPR will be an argument which must be evaluated.
11343 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11344 COMPOUND_EXPR in the chain will contain the tree for the simplified
11345 form of the builtin function call. */
11347 static tree
11348 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11350 if (!validate_arg (s1, POINTER_TYPE)
11351 || !validate_arg (s2, POINTER_TYPE))
11352 return NULL_TREE;
11353 else
11355 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11357 /* If both arguments are constants, evaluate at compile-time. */
11358 if (p1 && p2)
11360 const size_t r = strspn (p1, p2);
11361 return size_int (r);
11364 /* If either argument is "", return NULL_TREE. */
11365 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11366 /* Evaluate and ignore both arguments in case either one has
11367 side-effects. */
11368 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11369 s1, s2);
11370 return NULL_TREE;
11374 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11375 to the call.
11377 Return NULL_TREE if no simplification was possible, otherwise return the
11378 simplified form of the call as a tree.
11380 The simplified form may be a constant or other expression which
11381 computes the same value, but in a more efficient manner (including
11382 calls to other builtin functions).
11384 The call may contain arguments which need to be evaluated, but
11385 which are not useful to determine the result of the call. In
11386 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11387 COMPOUND_EXPR will be an argument which must be evaluated.
11388 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11389 COMPOUND_EXPR in the chain will contain the tree for the simplified
11390 form of the builtin function call. */
11392 static tree
11393 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11395 if (!validate_arg (s1, POINTER_TYPE)
11396 || !validate_arg (s2, POINTER_TYPE))
11397 return NULL_TREE;
11398 else
11400 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11402 /* If both arguments are constants, evaluate at compile-time. */
11403 if (p1 && p2)
11405 const size_t r = strcspn (p1, p2);
11406 return size_int (r);
11409 /* If the first argument is "", return NULL_TREE. */
11410 if (p1 && *p1 == '\0')
11412 /* Evaluate and ignore argument s2 in case it has
11413 side-effects. */
11414 return omit_one_operand_loc (loc, size_type_node,
11415 size_zero_node, s2);
11418 /* If the second argument is "", return __builtin_strlen(s1). */
11419 if (p2 && *p2 == '\0')
11421 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11423 /* If the replacement _DECL isn't initialized, don't do the
11424 transformation. */
11425 if (!fn)
11426 return NULL_TREE;
11428 return build_call_expr_loc (loc, fn, 1, s1);
11430 return NULL_TREE;
11434 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11435 to the call. IGNORE is true if the value returned
11436 by the builtin will be ignored. UNLOCKED is true is true if this
11437 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11438 the known length of the string. Return NULL_TREE if no simplification
11439 was possible. */
11441 tree
11442 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11443 bool ignore, bool unlocked, tree len)
11445 /* If we're using an unlocked function, assume the other unlocked
11446 functions exist explicitly. */
11447 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11448 : implicit_built_in_decls[BUILT_IN_FPUTC];
11449 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11450 : implicit_built_in_decls[BUILT_IN_FWRITE];
11452 /* If the return value is used, don't do the transformation. */
11453 if (!ignore)
11454 return NULL_TREE;
11456 /* Verify the arguments in the original call. */
11457 if (!validate_arg (arg0, POINTER_TYPE)
11458 || !validate_arg (arg1, POINTER_TYPE))
11459 return NULL_TREE;
11461 if (! len)
11462 len = c_strlen (arg0, 0);
11464 /* Get the length of the string passed to fputs. If the length
11465 can't be determined, punt. */
11466 if (!len
11467 || TREE_CODE (len) != INTEGER_CST)
11468 return NULL_TREE;
11470 switch (compare_tree_int (len, 1))
11472 case -1: /* length is 0, delete the call entirely . */
11473 return omit_one_operand_loc (loc, integer_type_node,
11474 integer_zero_node, arg1);;
11476 case 0: /* length is 1, call fputc. */
11478 const char *p = c_getstr (arg0);
11480 if (p != NULL)
11482 if (fn_fputc)
11483 return build_call_expr_loc (loc, fn_fputc, 2,
11484 build_int_cst (NULL_TREE, p[0]), arg1);
11485 else
11486 return NULL_TREE;
11489 /* FALLTHROUGH */
11490 case 1: /* length is greater than 1, call fwrite. */
11492 /* If optimizing for size keep fputs. */
11493 if (optimize_function_for_size_p (cfun))
11494 return NULL_TREE;
11495 /* New argument list transforming fputs(string, stream) to
11496 fwrite(string, 1, len, stream). */
11497 if (fn_fwrite)
11498 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11499 size_one_node, len, arg1);
11500 else
11501 return NULL_TREE;
11503 default:
11504 gcc_unreachable ();
11506 return NULL_TREE;
11509 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11510 produced. False otherwise. This is done so that we don't output the error
11511 or warning twice or three times. */
11513 bool
11514 fold_builtin_next_arg (tree exp, bool va_start_p)
11516 tree fntype = TREE_TYPE (current_function_decl);
11517 int nargs = call_expr_nargs (exp);
11518 tree arg;
11520 if (TYPE_ARG_TYPES (fntype) == 0
11521 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11522 == void_type_node))
11524 error ("%<va_start%> used in function with fixed args");
11525 return true;
11528 if (va_start_p)
11530 if (va_start_p && (nargs != 2))
11532 error ("wrong number of arguments to function %<va_start%>");
11533 return true;
11535 arg = CALL_EXPR_ARG (exp, 1);
11537 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11538 when we checked the arguments and if needed issued a warning. */
11539 else
11541 if (nargs == 0)
11543 /* Evidently an out of date version of <stdarg.h>; can't validate
11544 va_start's second argument, but can still work as intended. */
11545 warning (0, "%<__builtin_next_arg%> called without an argument");
11546 return true;
11548 else if (nargs > 1)
11550 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11551 return true;
11553 arg = CALL_EXPR_ARG (exp, 0);
11556 if (TREE_CODE (arg) == SSA_NAME)
11557 arg = SSA_NAME_VAR (arg);
11559 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11560 or __builtin_next_arg (0) the first time we see it, after checking
11561 the arguments and if needed issuing a warning. */
11562 if (!integer_zerop (arg))
11564 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11566 /* Strip off all nops for the sake of the comparison. This
11567 is not quite the same as STRIP_NOPS. It does more.
11568 We must also strip off INDIRECT_EXPR for C++ reference
11569 parameters. */
11570 while (CONVERT_EXPR_P (arg)
11571 || TREE_CODE (arg) == INDIRECT_REF)
11572 arg = TREE_OPERAND (arg, 0);
11573 if (arg != last_parm)
11575 /* FIXME: Sometimes with the tree optimizers we can get the
11576 not the last argument even though the user used the last
11577 argument. We just warn and set the arg to be the last
11578 argument so that we will get wrong-code because of
11579 it. */
11580 warning (0, "second parameter of %<va_start%> not last named argument");
11583 /* Undefined by C99 7.15.1.4p4 (va_start):
11584 "If the parameter parmN is declared with the register storage
11585 class, with a function or array type, or with a type that is
11586 not compatible with the type that results after application of
11587 the default argument promotions, the behavior is undefined."
11589 else if (DECL_REGISTER (arg))
11590 warning (0, "undefined behaviour when second parameter of "
11591 "%<va_start%> is declared with %<register%> storage");
11593 /* We want to verify the second parameter just once before the tree
11594 optimizers are run and then avoid keeping it in the tree,
11595 as otherwise we could warn even for correct code like:
11596 void foo (int i, ...)
11597 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11598 if (va_start_p)
11599 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11600 else
11601 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11603 return false;
11607 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11608 ORIG may be null if this is a 2-argument call. We don't attempt to
11609 simplify calls with more than 3 arguments.
11611 Return NULL_TREE if no simplification was possible, otherwise return the
11612 simplified form of the call as a tree. If IGNORED is true, it means that
11613 the caller does not use the returned value of the function. */
11615 static tree
11616 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11617 tree orig, int ignored)
11619 tree call, retval;
11620 const char *fmt_str = NULL;
11622 /* Verify the required arguments in the original call. We deal with two
11623 types of sprintf() calls: 'sprintf (str, fmt)' and
11624 'sprintf (dest, "%s", orig)'. */
11625 if (!validate_arg (dest, POINTER_TYPE)
11626 || !validate_arg (fmt, POINTER_TYPE))
11627 return NULL_TREE;
11628 if (orig && !validate_arg (orig, POINTER_TYPE))
11629 return NULL_TREE;
11631 /* Check whether the format is a literal string constant. */
11632 fmt_str = c_getstr (fmt);
11633 if (fmt_str == NULL)
11634 return NULL_TREE;
11636 call = NULL_TREE;
11637 retval = NULL_TREE;
11639 if (!init_target_chars ())
11640 return NULL_TREE;
11642 /* If the format doesn't contain % args or %%, use strcpy. */
11643 if (strchr (fmt_str, target_percent) == NULL)
11645 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11647 if (!fn)
11648 return NULL_TREE;
11650 /* Don't optimize sprintf (buf, "abc", ptr++). */
11651 if (orig)
11652 return NULL_TREE;
11654 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11655 'format' is known to contain no % formats. */
11656 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11657 if (!ignored)
11658 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11661 /* If the format is "%s", use strcpy if the result isn't used. */
11662 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11664 tree fn;
11665 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11667 if (!fn)
11668 return NULL_TREE;
11670 /* Don't crash on sprintf (str1, "%s"). */
11671 if (!orig)
11672 return NULL_TREE;
11674 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11675 if (!ignored)
11677 retval = c_strlen (orig, 1);
11678 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11679 return NULL_TREE;
11681 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11684 if (call && retval)
11686 retval = fold_convert_loc
11687 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11688 retval);
11689 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11691 else
11692 return call;
11695 /* Expand a call EXP to __builtin_object_size. */
11698 expand_builtin_object_size (tree exp)
11700 tree ost;
11701 int object_size_type;
11702 tree fndecl = get_callee_fndecl (exp);
11704 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11706 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11707 exp, fndecl);
11708 expand_builtin_trap ();
11709 return const0_rtx;
11712 ost = CALL_EXPR_ARG (exp, 1);
11713 STRIP_NOPS (ost);
11715 if (TREE_CODE (ost) != INTEGER_CST
11716 || tree_int_cst_sgn (ost) < 0
11717 || compare_tree_int (ost, 3) > 0)
11719 error ("%Klast argument of %D is not integer constant between 0 and 3",
11720 exp, fndecl);
11721 expand_builtin_trap ();
11722 return const0_rtx;
11725 object_size_type = tree_low_cst (ost, 0);
11727 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11730 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11731 FCODE is the BUILT_IN_* to use.
11732 Return NULL_RTX if we failed; the caller should emit a normal call,
11733 otherwise try to get the result in TARGET, if convenient (and in
11734 mode MODE if that's convenient). */
11736 static rtx
11737 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11738 enum built_in_function fcode)
11740 tree dest, src, len, size;
11742 if (!validate_arglist (exp,
11743 POINTER_TYPE,
11744 fcode == BUILT_IN_MEMSET_CHK
11745 ? INTEGER_TYPE : POINTER_TYPE,
11746 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11747 return NULL_RTX;
11749 dest = CALL_EXPR_ARG (exp, 0);
11750 src = CALL_EXPR_ARG (exp, 1);
11751 len = CALL_EXPR_ARG (exp, 2);
11752 size = CALL_EXPR_ARG (exp, 3);
11754 if (! host_integerp (size, 1))
11755 return NULL_RTX;
11757 if (host_integerp (len, 1) || integer_all_onesp (size))
11759 tree fn;
11761 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11763 warning_at (tree_nonartificial_location (exp),
11764 0, "%Kcall to %D will always overflow destination buffer",
11765 exp, get_callee_fndecl (exp));
11766 return NULL_RTX;
11769 fn = NULL_TREE;
11770 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11771 mem{cpy,pcpy,move,set} is available. */
11772 switch (fcode)
11774 case BUILT_IN_MEMCPY_CHK:
11775 fn = built_in_decls[BUILT_IN_MEMCPY];
11776 break;
11777 case BUILT_IN_MEMPCPY_CHK:
11778 fn = built_in_decls[BUILT_IN_MEMPCPY];
11779 break;
11780 case BUILT_IN_MEMMOVE_CHK:
11781 fn = built_in_decls[BUILT_IN_MEMMOVE];
11782 break;
11783 case BUILT_IN_MEMSET_CHK:
11784 fn = built_in_decls[BUILT_IN_MEMSET];
11785 break;
11786 default:
11787 break;
11790 if (! fn)
11791 return NULL_RTX;
11793 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11794 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11795 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11796 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11798 else if (fcode == BUILT_IN_MEMSET_CHK)
11799 return NULL_RTX;
11800 else
11802 unsigned int dest_align
11803 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
11805 /* If DEST is not a pointer type, call the normal function. */
11806 if (dest_align == 0)
11807 return NULL_RTX;
11809 /* If SRC and DEST are the same (and not volatile), do nothing. */
11810 if (operand_equal_p (src, dest, 0))
11812 tree expr;
11814 if (fcode != BUILT_IN_MEMPCPY_CHK)
11816 /* Evaluate and ignore LEN in case it has side-effects. */
11817 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11818 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11821 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
11822 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11825 /* __memmove_chk special case. */
11826 if (fcode == BUILT_IN_MEMMOVE_CHK)
11828 unsigned int src_align
11829 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
11831 if (src_align == 0)
11832 return NULL_RTX;
11834 /* If src is categorized for a readonly section we can use
11835 normal __memcpy_chk. */
11836 if (readonly_data_expr (src))
11838 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11839 if (!fn)
11840 return NULL_RTX;
11841 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11842 dest, src, len, size);
11843 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11844 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11845 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11848 return NULL_RTX;
11852 /* Emit warning if a buffer overflow is detected at compile time. */
11854 static void
11855 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11857 int is_strlen = 0;
11858 tree len, size;
11859 location_t loc = tree_nonartificial_location (exp);
11861 switch (fcode)
11863 case BUILT_IN_STRCPY_CHK:
11864 case BUILT_IN_STPCPY_CHK:
11865 /* For __strcat_chk the warning will be emitted only if overflowing
11866 by at least strlen (dest) + 1 bytes. */
11867 case BUILT_IN_STRCAT_CHK:
11868 len = CALL_EXPR_ARG (exp, 1);
11869 size = CALL_EXPR_ARG (exp, 2);
11870 is_strlen = 1;
11871 break;
11872 case BUILT_IN_STRNCAT_CHK:
11873 case BUILT_IN_STRNCPY_CHK:
11874 len = CALL_EXPR_ARG (exp, 2);
11875 size = CALL_EXPR_ARG (exp, 3);
11876 break;
11877 case BUILT_IN_SNPRINTF_CHK:
11878 case BUILT_IN_VSNPRINTF_CHK:
11879 len = CALL_EXPR_ARG (exp, 1);
11880 size = CALL_EXPR_ARG (exp, 3);
11881 break;
11882 default:
11883 gcc_unreachable ();
11886 if (!len || !size)
11887 return;
11889 if (! host_integerp (size, 1) || integer_all_onesp (size))
11890 return;
11892 if (is_strlen)
11894 len = c_strlen (len, 1);
11895 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11896 return;
11898 else if (fcode == BUILT_IN_STRNCAT_CHK)
11900 tree src = CALL_EXPR_ARG (exp, 1);
11901 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11902 return;
11903 src = c_strlen (src, 1);
11904 if (! src || ! host_integerp (src, 1))
11906 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11907 exp, get_callee_fndecl (exp));
11908 return;
11910 else if (tree_int_cst_lt (src, size))
11911 return;
11913 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
11914 return;
11916 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11917 exp, get_callee_fndecl (exp));
11920 /* Emit warning if a buffer overflow is detected at compile time
11921 in __sprintf_chk/__vsprintf_chk calls. */
11923 static void
11924 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11926 tree size, len, fmt;
11927 const char *fmt_str;
11928 int nargs = call_expr_nargs (exp);
11930 /* Verify the required arguments in the original call. */
11932 if (nargs < 4)
11933 return;
11934 size = CALL_EXPR_ARG (exp, 2);
11935 fmt = CALL_EXPR_ARG (exp, 3);
11937 if (! host_integerp (size, 1) || integer_all_onesp (size))
11938 return;
11940 /* Check whether the format is a literal string constant. */
11941 fmt_str = c_getstr (fmt);
11942 if (fmt_str == NULL)
11943 return;
11945 if (!init_target_chars ())
11946 return;
11948 /* If the format doesn't contain % args or %%, we know its size. */
11949 if (strchr (fmt_str, target_percent) == 0)
11950 len = build_int_cstu (size_type_node, strlen (fmt_str));
11951 /* If the format is "%s" and first ... argument is a string literal,
11952 we know it too. */
11953 else if (fcode == BUILT_IN_SPRINTF_CHK
11954 && strcmp (fmt_str, target_percent_s) == 0)
11956 tree arg;
11958 if (nargs < 5)
11959 return;
11960 arg = CALL_EXPR_ARG (exp, 4);
11961 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11962 return;
11964 len = c_strlen (arg, 1);
11965 if (!len || ! host_integerp (len, 1))
11966 return;
11968 else
11969 return;
11971 if (! tree_int_cst_lt (len, size))
11972 warning_at (tree_nonartificial_location (exp),
11973 0, "%Kcall to %D will always overflow destination buffer",
11974 exp, get_callee_fndecl (exp));
11977 /* Emit warning if a free is called with address of a variable. */
11979 static void
11980 maybe_emit_free_warning (tree exp)
11982 tree arg = CALL_EXPR_ARG (exp, 0);
11984 STRIP_NOPS (arg);
11985 if (TREE_CODE (arg) != ADDR_EXPR)
11986 return;
11988 arg = get_base_address (TREE_OPERAND (arg, 0));
11989 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11990 return;
11992 if (SSA_VAR_P (arg))
11993 warning_at (tree_nonartificial_location (exp),
11994 0, "%Kattempt to free a non-heap object %qD", exp, arg);
11995 else
11996 warning_at (tree_nonartificial_location (exp),
11997 0, "%Kattempt to free a non-heap object", exp);
12000 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12001 if possible. */
12003 tree
12004 fold_builtin_object_size (tree ptr, tree ost)
12006 unsigned HOST_WIDE_INT bytes;
12007 int object_size_type;
12009 if (!validate_arg (ptr, POINTER_TYPE)
12010 || !validate_arg (ost, INTEGER_TYPE))
12011 return NULL_TREE;
12013 STRIP_NOPS (ost);
12015 if (TREE_CODE (ost) != INTEGER_CST
12016 || tree_int_cst_sgn (ost) < 0
12017 || compare_tree_int (ost, 3) > 0)
12018 return NULL_TREE;
12020 object_size_type = tree_low_cst (ost, 0);
12022 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12023 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12024 and (size_t) 0 for types 2 and 3. */
12025 if (TREE_SIDE_EFFECTS (ptr))
12026 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12028 if (TREE_CODE (ptr) == ADDR_EXPR)
12030 bytes = compute_builtin_object_size (ptr, object_size_type);
12031 if (double_int_fits_to_tree_p (size_type_node,
12032 uhwi_to_double_int (bytes)))
12033 return build_int_cstu (size_type_node, bytes);
12035 else if (TREE_CODE (ptr) == SSA_NAME)
12037 /* If object size is not known yet, delay folding until
12038 later. Maybe subsequent passes will help determining
12039 it. */
12040 bytes = compute_builtin_object_size (ptr, object_size_type);
12041 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12042 && double_int_fits_to_tree_p (size_type_node,
12043 uhwi_to_double_int (bytes)))
12044 return build_int_cstu (size_type_node, bytes);
12047 return NULL_TREE;
12050 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12051 DEST, SRC, LEN, and SIZE are the arguments to the call.
12052 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12053 code of the builtin. If MAXLEN is not NULL, it is maximum length
12054 passed as third argument. */
12056 tree
12057 fold_builtin_memory_chk (location_t loc, tree fndecl,
12058 tree dest, tree src, tree len, tree size,
12059 tree maxlen, bool ignore,
12060 enum built_in_function fcode)
12062 tree fn;
12064 if (!validate_arg (dest, POINTER_TYPE)
12065 || !validate_arg (src,
12066 (fcode == BUILT_IN_MEMSET_CHK
12067 ? INTEGER_TYPE : POINTER_TYPE))
12068 || !validate_arg (len, INTEGER_TYPE)
12069 || !validate_arg (size, INTEGER_TYPE))
12070 return NULL_TREE;
12072 /* If SRC and DEST are the same (and not volatile), return DEST
12073 (resp. DEST+LEN for __mempcpy_chk). */
12074 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12076 if (fcode != BUILT_IN_MEMPCPY_CHK)
12077 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12078 dest, len);
12079 else
12081 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12082 dest, len);
12083 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12087 if (! host_integerp (size, 1))
12088 return NULL_TREE;
12090 if (! integer_all_onesp (size))
12092 if (! host_integerp (len, 1))
12094 /* If LEN is not constant, try MAXLEN too.
12095 For MAXLEN only allow optimizing into non-_ocs function
12096 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12097 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12099 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12101 /* (void) __mempcpy_chk () can be optimized into
12102 (void) __memcpy_chk (). */
12103 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12104 if (!fn)
12105 return NULL_TREE;
12107 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12109 return NULL_TREE;
12112 else
12113 maxlen = len;
12115 if (tree_int_cst_lt (size, maxlen))
12116 return NULL_TREE;
12119 fn = NULL_TREE;
12120 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12121 mem{cpy,pcpy,move,set} is available. */
12122 switch (fcode)
12124 case BUILT_IN_MEMCPY_CHK:
12125 fn = built_in_decls[BUILT_IN_MEMCPY];
12126 break;
12127 case BUILT_IN_MEMPCPY_CHK:
12128 fn = built_in_decls[BUILT_IN_MEMPCPY];
12129 break;
12130 case BUILT_IN_MEMMOVE_CHK:
12131 fn = built_in_decls[BUILT_IN_MEMMOVE];
12132 break;
12133 case BUILT_IN_MEMSET_CHK:
12134 fn = built_in_decls[BUILT_IN_MEMSET];
12135 break;
12136 default:
12137 break;
12140 if (!fn)
12141 return NULL_TREE;
12143 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12146 /* Fold a call to the __st[rp]cpy_chk builtin.
12147 DEST, SRC, and SIZE are the arguments to the call.
12148 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12149 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12150 strings passed as second argument. */
12152 tree
12153 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12154 tree src, tree size,
12155 tree maxlen, bool ignore,
12156 enum built_in_function fcode)
12158 tree len, fn;
12160 if (!validate_arg (dest, POINTER_TYPE)
12161 || !validate_arg (src, POINTER_TYPE)
12162 || !validate_arg (size, INTEGER_TYPE))
12163 return NULL_TREE;
12165 /* If SRC and DEST are the same (and not volatile), return DEST. */
12166 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12167 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12169 if (! host_integerp (size, 1))
12170 return NULL_TREE;
12172 if (! integer_all_onesp (size))
12174 len = c_strlen (src, 1);
12175 if (! len || ! host_integerp (len, 1))
12177 /* If LEN is not constant, try MAXLEN too.
12178 For MAXLEN only allow optimizing into non-_ocs function
12179 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12180 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12182 if (fcode == BUILT_IN_STPCPY_CHK)
12184 if (! ignore)
12185 return NULL_TREE;
12187 /* If return value of __stpcpy_chk is ignored,
12188 optimize into __strcpy_chk. */
12189 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12190 if (!fn)
12191 return NULL_TREE;
12193 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12196 if (! len || TREE_SIDE_EFFECTS (len))
12197 return NULL_TREE;
12199 /* If c_strlen returned something, but not a constant,
12200 transform __strcpy_chk into __memcpy_chk. */
12201 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12202 if (!fn)
12203 return NULL_TREE;
12205 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12206 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12207 build_call_expr_loc (loc, fn, 4,
12208 dest, src, len, size));
12211 else
12212 maxlen = len;
12214 if (! tree_int_cst_lt (maxlen, size))
12215 return NULL_TREE;
12218 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12219 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12220 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12221 if (!fn)
12222 return NULL_TREE;
12224 return build_call_expr_loc (loc, fn, 2, dest, src);
12227 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12228 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12229 length passed as third argument. */
12231 tree
12232 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12233 tree len, tree size, tree maxlen)
12235 tree fn;
12237 if (!validate_arg (dest, POINTER_TYPE)
12238 || !validate_arg (src, POINTER_TYPE)
12239 || !validate_arg (len, INTEGER_TYPE)
12240 || !validate_arg (size, INTEGER_TYPE))
12241 return NULL_TREE;
12243 if (! host_integerp (size, 1))
12244 return NULL_TREE;
12246 if (! integer_all_onesp (size))
12248 if (! host_integerp (len, 1))
12250 /* If LEN is not constant, try MAXLEN too.
12251 For MAXLEN only allow optimizing into non-_ocs function
12252 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12253 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12254 return NULL_TREE;
12256 else
12257 maxlen = len;
12259 if (tree_int_cst_lt (size, maxlen))
12260 return NULL_TREE;
12263 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12264 fn = built_in_decls[BUILT_IN_STRNCPY];
12265 if (!fn)
12266 return NULL_TREE;
12268 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12271 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12272 are the arguments to the call. */
12274 static tree
12275 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12276 tree src, tree size)
12278 tree fn;
12279 const char *p;
12281 if (!validate_arg (dest, POINTER_TYPE)
12282 || !validate_arg (src, POINTER_TYPE)
12283 || !validate_arg (size, INTEGER_TYPE))
12284 return NULL_TREE;
12286 p = c_getstr (src);
12287 /* If the SRC parameter is "", return DEST. */
12288 if (p && *p == '\0')
12289 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12291 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12292 return NULL_TREE;
12294 /* If __builtin_strcat_chk is used, assume strcat is available. */
12295 fn = built_in_decls[BUILT_IN_STRCAT];
12296 if (!fn)
12297 return NULL_TREE;
12299 return build_call_expr_loc (loc, fn, 2, dest, src);
12302 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12303 LEN, and SIZE. */
12305 static tree
12306 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12307 tree dest, tree src, tree len, tree size)
12309 tree fn;
12310 const char *p;
12312 if (!validate_arg (dest, POINTER_TYPE)
12313 || !validate_arg (src, POINTER_TYPE)
12314 || !validate_arg (size, INTEGER_TYPE)
12315 || !validate_arg (size, INTEGER_TYPE))
12316 return NULL_TREE;
12318 p = c_getstr (src);
12319 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12320 if (p && *p == '\0')
12321 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12322 else if (integer_zerop (len))
12323 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12325 if (! host_integerp (size, 1))
12326 return NULL_TREE;
12328 if (! integer_all_onesp (size))
12330 tree src_len = c_strlen (src, 1);
12331 if (src_len
12332 && host_integerp (src_len, 1)
12333 && host_integerp (len, 1)
12334 && ! tree_int_cst_lt (len, src_len))
12336 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12337 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12338 if (!fn)
12339 return NULL_TREE;
12341 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12343 return NULL_TREE;
12346 /* If __builtin_strncat_chk is used, assume strncat is available. */
12347 fn = built_in_decls[BUILT_IN_STRNCAT];
12348 if (!fn)
12349 return NULL_TREE;
12351 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12354 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12355 a normal call should be emitted rather than expanding the function
12356 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12358 static tree
12359 fold_builtin_sprintf_chk (location_t loc, tree exp,
12360 enum built_in_function fcode)
12362 tree dest, size, len, fn, fmt, flag;
12363 const char *fmt_str;
12364 int nargs = call_expr_nargs (exp);
12366 /* Verify the required arguments in the original call. */
12367 if (nargs < 4)
12368 return NULL_TREE;
12369 dest = CALL_EXPR_ARG (exp, 0);
12370 if (!validate_arg (dest, POINTER_TYPE))
12371 return NULL_TREE;
12372 flag = CALL_EXPR_ARG (exp, 1);
12373 if (!validate_arg (flag, INTEGER_TYPE))
12374 return NULL_TREE;
12375 size = CALL_EXPR_ARG (exp, 2);
12376 if (!validate_arg (size, INTEGER_TYPE))
12377 return NULL_TREE;
12378 fmt = CALL_EXPR_ARG (exp, 3);
12379 if (!validate_arg (fmt, POINTER_TYPE))
12380 return NULL_TREE;
12382 if (! host_integerp (size, 1))
12383 return NULL_TREE;
12385 len = NULL_TREE;
12387 if (!init_target_chars ())
12388 return NULL_TREE;
12390 /* Check whether the format is a literal string constant. */
12391 fmt_str = c_getstr (fmt);
12392 if (fmt_str != NULL)
12394 /* If the format doesn't contain % args or %%, we know the size. */
12395 if (strchr (fmt_str, target_percent) == 0)
12397 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12398 len = build_int_cstu (size_type_node, strlen (fmt_str));
12400 /* If the format is "%s" and first ... argument is a string literal,
12401 we know the size too. */
12402 else if (fcode == BUILT_IN_SPRINTF_CHK
12403 && strcmp (fmt_str, target_percent_s) == 0)
12405 tree arg;
12407 if (nargs == 5)
12409 arg = CALL_EXPR_ARG (exp, 4);
12410 if (validate_arg (arg, POINTER_TYPE))
12412 len = c_strlen (arg, 1);
12413 if (! len || ! host_integerp (len, 1))
12414 len = NULL_TREE;
12420 if (! integer_all_onesp (size))
12422 if (! len || ! tree_int_cst_lt (len, size))
12423 return NULL_TREE;
12426 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12427 or if format doesn't contain % chars or is "%s". */
12428 if (! integer_zerop (flag))
12430 if (fmt_str == NULL)
12431 return NULL_TREE;
12432 if (strchr (fmt_str, target_percent) != NULL
12433 && strcmp (fmt_str, target_percent_s))
12434 return NULL_TREE;
12437 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12438 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12439 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12440 if (!fn)
12441 return NULL_TREE;
12443 return rewrite_call_expr (loc, exp, 4, fn, 2, dest, fmt);
12446 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12447 a normal call should be emitted rather than expanding the function
12448 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12449 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12450 passed as second argument. */
12452 tree
12453 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12454 enum built_in_function fcode)
12456 tree dest, size, len, fn, fmt, flag;
12457 const char *fmt_str;
12459 /* Verify the required arguments in the original call. */
12460 if (call_expr_nargs (exp) < 5)
12461 return NULL_TREE;
12462 dest = CALL_EXPR_ARG (exp, 0);
12463 if (!validate_arg (dest, POINTER_TYPE))
12464 return NULL_TREE;
12465 len = CALL_EXPR_ARG (exp, 1);
12466 if (!validate_arg (len, INTEGER_TYPE))
12467 return NULL_TREE;
12468 flag = CALL_EXPR_ARG (exp, 2);
12469 if (!validate_arg (flag, INTEGER_TYPE))
12470 return NULL_TREE;
12471 size = CALL_EXPR_ARG (exp, 3);
12472 if (!validate_arg (size, INTEGER_TYPE))
12473 return NULL_TREE;
12474 fmt = CALL_EXPR_ARG (exp, 4);
12475 if (!validate_arg (fmt, POINTER_TYPE))
12476 return NULL_TREE;
12478 if (! host_integerp (size, 1))
12479 return NULL_TREE;
12481 if (! integer_all_onesp (size))
12483 if (! host_integerp (len, 1))
12485 /* If LEN is not constant, try MAXLEN too.
12486 For MAXLEN only allow optimizing into non-_ocs function
12487 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12488 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12489 return NULL_TREE;
12491 else
12492 maxlen = len;
12494 if (tree_int_cst_lt (size, maxlen))
12495 return NULL_TREE;
12498 if (!init_target_chars ())
12499 return NULL_TREE;
12501 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12502 or if format doesn't contain % chars or is "%s". */
12503 if (! integer_zerop (flag))
12505 fmt_str = c_getstr (fmt);
12506 if (fmt_str == NULL)
12507 return NULL_TREE;
12508 if (strchr (fmt_str, target_percent) != NULL
12509 && strcmp (fmt_str, target_percent_s))
12510 return NULL_TREE;
12513 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12514 available. */
12515 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12516 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12517 if (!fn)
12518 return NULL_TREE;
12520 return rewrite_call_expr (loc, exp, 5, fn, 3, dest, len, fmt);
12523 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12524 FMT and ARG are the arguments to the call; we don't fold cases with
12525 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12527 Return NULL_TREE if no simplification was possible, otherwise return the
12528 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12529 code of the function to be simplified. */
12531 static tree
12532 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12533 tree arg, bool ignore,
12534 enum built_in_function fcode)
12536 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12537 const char *fmt_str = NULL;
12539 /* If the return value is used, don't do the transformation. */
12540 if (! ignore)
12541 return NULL_TREE;
12543 /* Verify the required arguments in the original call. */
12544 if (!validate_arg (fmt, POINTER_TYPE))
12545 return NULL_TREE;
12547 /* Check whether the format is a literal string constant. */
12548 fmt_str = c_getstr (fmt);
12549 if (fmt_str == NULL)
12550 return NULL_TREE;
12552 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12554 /* If we're using an unlocked function, assume the other
12555 unlocked functions exist explicitly. */
12556 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12557 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12559 else
12561 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12562 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12565 if (!init_target_chars ())
12566 return NULL_TREE;
12568 if (strcmp (fmt_str, target_percent_s) == 0
12569 || strchr (fmt_str, target_percent) == NULL)
12571 const char *str;
12573 if (strcmp (fmt_str, target_percent_s) == 0)
12575 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12576 return NULL_TREE;
12578 if (!arg || !validate_arg (arg, POINTER_TYPE))
12579 return NULL_TREE;
12581 str = c_getstr (arg);
12582 if (str == NULL)
12583 return NULL_TREE;
12585 else
12587 /* The format specifier doesn't contain any '%' characters. */
12588 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12589 && arg)
12590 return NULL_TREE;
12591 str = fmt_str;
12594 /* If the string was "", printf does nothing. */
12595 if (str[0] == '\0')
12596 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12598 /* If the string has length of 1, call putchar. */
12599 if (str[1] == '\0')
12601 /* Given printf("c"), (where c is any one character,)
12602 convert "c"[0] to an int and pass that to the replacement
12603 function. */
12604 newarg = build_int_cst (NULL_TREE, str[0]);
12605 if (fn_putchar)
12606 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12608 else
12610 /* If the string was "string\n", call puts("string"). */
12611 size_t len = strlen (str);
12612 if ((unsigned char)str[len - 1] == target_newline)
12614 /* Create a NUL-terminated string that's one char shorter
12615 than the original, stripping off the trailing '\n'. */
12616 char *newstr = XALLOCAVEC (char, len);
12617 memcpy (newstr, str, len - 1);
12618 newstr[len - 1] = 0;
12620 newarg = build_string_literal (len, newstr);
12621 if (fn_puts)
12622 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12624 else
12625 /* We'd like to arrange to call fputs(string,stdout) here,
12626 but we need stdout and don't have a way to get it yet. */
12627 return NULL_TREE;
12631 /* The other optimizations can be done only on the non-va_list variants. */
12632 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12633 return NULL_TREE;
12635 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12636 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12638 if (!arg || !validate_arg (arg, POINTER_TYPE))
12639 return NULL_TREE;
12640 if (fn_puts)
12641 call = build_call_expr_loc (loc, fn_puts, 1, arg);
12644 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12645 else if (strcmp (fmt_str, target_percent_c) == 0)
12647 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12648 return NULL_TREE;
12649 if (fn_putchar)
12650 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
12653 if (!call)
12654 return NULL_TREE;
12656 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12659 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12660 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12661 more than 3 arguments, and ARG may be null in the 2-argument case.
12663 Return NULL_TREE if no simplification was possible, otherwise return the
12664 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12665 code of the function to be simplified. */
12667 static tree
12668 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
12669 tree fmt, tree arg, bool ignore,
12670 enum built_in_function fcode)
12672 tree fn_fputc, fn_fputs, call = NULL_TREE;
12673 const char *fmt_str = NULL;
12675 /* If the return value is used, don't do the transformation. */
12676 if (! ignore)
12677 return NULL_TREE;
12679 /* Verify the required arguments in the original call. */
12680 if (!validate_arg (fp, POINTER_TYPE))
12681 return NULL_TREE;
12682 if (!validate_arg (fmt, POINTER_TYPE))
12683 return NULL_TREE;
12685 /* Check whether the format is a literal string constant. */
12686 fmt_str = c_getstr (fmt);
12687 if (fmt_str == NULL)
12688 return NULL_TREE;
12690 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12692 /* If we're using an unlocked function, assume the other
12693 unlocked functions exist explicitly. */
12694 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12695 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12697 else
12699 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12700 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12703 if (!init_target_chars ())
12704 return NULL_TREE;
12706 /* If the format doesn't contain % args or %%, use strcpy. */
12707 if (strchr (fmt_str, target_percent) == NULL)
12709 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12710 && arg)
12711 return NULL_TREE;
12713 /* If the format specifier was "", fprintf does nothing. */
12714 if (fmt_str[0] == '\0')
12716 /* If FP has side-effects, just wait until gimplification is
12717 done. */
12718 if (TREE_SIDE_EFFECTS (fp))
12719 return NULL_TREE;
12721 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12724 /* When "string" doesn't contain %, replace all cases of
12725 fprintf (fp, string) with fputs (string, fp). The fputs
12726 builtin will take care of special cases like length == 1. */
12727 if (fn_fputs)
12728 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
12731 /* The other optimizations can be done only on the non-va_list variants. */
12732 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12733 return NULL_TREE;
12735 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12736 else if (strcmp (fmt_str, target_percent_s) == 0)
12738 if (!arg || !validate_arg (arg, POINTER_TYPE))
12739 return NULL_TREE;
12740 if (fn_fputs)
12741 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
12744 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12745 else if (strcmp (fmt_str, target_percent_c) == 0)
12747 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12748 return NULL_TREE;
12749 if (fn_fputc)
12750 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
12753 if (!call)
12754 return NULL_TREE;
12755 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12758 /* Initialize format string characters in the target charset. */
12760 static bool
12761 init_target_chars (void)
12763 static bool init;
12764 if (!init)
12766 target_newline = lang_hooks.to_target_charset ('\n');
12767 target_percent = lang_hooks.to_target_charset ('%');
12768 target_c = lang_hooks.to_target_charset ('c');
12769 target_s = lang_hooks.to_target_charset ('s');
12770 if (target_newline == 0 || target_percent == 0 || target_c == 0
12771 || target_s == 0)
12772 return false;
12774 target_percent_c[0] = target_percent;
12775 target_percent_c[1] = target_c;
12776 target_percent_c[2] = '\0';
12778 target_percent_s[0] = target_percent;
12779 target_percent_s[1] = target_s;
12780 target_percent_s[2] = '\0';
12782 target_percent_s_newline[0] = target_percent;
12783 target_percent_s_newline[1] = target_s;
12784 target_percent_s_newline[2] = target_newline;
12785 target_percent_s_newline[3] = '\0';
12787 init = true;
12789 return true;
12792 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12793 and no overflow/underflow occurred. INEXACT is true if M was not
12794 exactly calculated. TYPE is the tree type for the result. This
12795 function assumes that you cleared the MPFR flags and then
12796 calculated M to see if anything subsequently set a flag prior to
12797 entering this function. Return NULL_TREE if any checks fail. */
12799 static tree
12800 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12802 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12803 overflow/underflow occurred. If -frounding-math, proceed iff the
12804 result of calling FUNC was exact. */
12805 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12806 && (!flag_rounding_math || !inexact))
12808 REAL_VALUE_TYPE rr;
12810 real_from_mpfr (&rr, m, type, GMP_RNDN);
12811 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12812 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12813 but the mpft_t is not, then we underflowed in the
12814 conversion. */
12815 if (real_isfinite (&rr)
12816 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12818 REAL_VALUE_TYPE rmode;
12820 real_convert (&rmode, TYPE_MODE (type), &rr);
12821 /* Proceed iff the specified mode can hold the value. */
12822 if (real_identical (&rmode, &rr))
12823 return build_real (type, rmode);
12826 return NULL_TREE;
12829 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12830 number and no overflow/underflow occurred. INEXACT is true if M
12831 was not exactly calculated. TYPE is the tree type for the result.
12832 This function assumes that you cleared the MPFR flags and then
12833 calculated M to see if anything subsequently set a flag prior to
12834 entering this function. Return NULL_TREE if any checks fail, if
12835 FORCE_CONVERT is true, then bypass the checks. */
12837 static tree
12838 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
12840 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12841 overflow/underflow occurred. If -frounding-math, proceed iff the
12842 result of calling FUNC was exact. */
12843 if (force_convert
12844 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
12845 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12846 && (!flag_rounding_math || !inexact)))
12848 REAL_VALUE_TYPE re, im;
12850 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
12851 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
12852 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12853 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12854 but the mpft_t is not, then we underflowed in the
12855 conversion. */
12856 if (force_convert
12857 || (real_isfinite (&re) && real_isfinite (&im)
12858 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
12859 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
12861 REAL_VALUE_TYPE re_mode, im_mode;
12863 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
12864 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
12865 /* Proceed iff the specified mode can hold the value. */
12866 if (force_convert
12867 || (real_identical (&re_mode, &re)
12868 && real_identical (&im_mode, &im)))
12869 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
12870 build_real (TREE_TYPE (type), im_mode));
12873 return NULL_TREE;
12876 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12877 FUNC on it and return the resulting value as a tree with type TYPE.
12878 If MIN and/or MAX are not NULL, then the supplied ARG must be
12879 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12880 acceptable values, otherwise they are not. The mpfr precision is
12881 set to the precision of TYPE. We assume that function FUNC returns
12882 zero if the result could be calculated exactly within the requested
12883 precision. */
12885 static tree
12886 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
12887 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
12888 bool inclusive)
12890 tree result = NULL_TREE;
12892 STRIP_NOPS (arg);
12894 /* To proceed, MPFR must exactly represent the target floating point
12895 format, which only happens when the target base equals two. */
12896 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12897 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12899 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12901 if (real_isfinite (ra)
12902 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12903 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12905 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12906 const int prec = fmt->p;
12907 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12908 int inexact;
12909 mpfr_t m;
12911 mpfr_init2 (m, prec);
12912 mpfr_from_real (m, ra, GMP_RNDN);
12913 mpfr_clear_flags ();
12914 inexact = func (m, m, rnd);
12915 result = do_mpfr_ckconv (m, type, inexact);
12916 mpfr_clear (m);
12920 return result;
12923 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12924 FUNC on it and return the resulting value as a tree with type TYPE.
12925 The mpfr precision is set to the precision of TYPE. We assume that
12926 function FUNC returns zero if the result could be calculated
12927 exactly within the requested precision. */
12929 static tree
12930 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12931 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12933 tree result = NULL_TREE;
12935 STRIP_NOPS (arg1);
12936 STRIP_NOPS (arg2);
12938 /* To proceed, MPFR must exactly represent the target floating point
12939 format, which only happens when the target base equals two. */
12940 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12941 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12942 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12944 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12945 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12947 if (real_isfinite (ra1) && real_isfinite (ra2))
12949 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12950 const int prec = fmt->p;
12951 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12952 int inexact;
12953 mpfr_t m1, m2;
12955 mpfr_inits2 (prec, m1, m2, NULL);
12956 mpfr_from_real (m1, ra1, GMP_RNDN);
12957 mpfr_from_real (m2, ra2, GMP_RNDN);
12958 mpfr_clear_flags ();
12959 inexact = func (m1, m1, m2, rnd);
12960 result = do_mpfr_ckconv (m1, type, inexact);
12961 mpfr_clears (m1, m2, NULL);
12965 return result;
12968 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12969 FUNC on it and return the resulting value as a tree with type TYPE.
12970 The mpfr precision is set to the precision of TYPE. We assume that
12971 function FUNC returns zero if the result could be calculated
12972 exactly within the requested precision. */
12974 static tree
12975 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12976 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12978 tree result = NULL_TREE;
12980 STRIP_NOPS (arg1);
12981 STRIP_NOPS (arg2);
12982 STRIP_NOPS (arg3);
12984 /* To proceed, MPFR must exactly represent the target floating point
12985 format, which only happens when the target base equals two. */
12986 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12987 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12988 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12989 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12991 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12992 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12993 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12995 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12997 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12998 const int prec = fmt->p;
12999 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13000 int inexact;
13001 mpfr_t m1, m2, m3;
13003 mpfr_inits2 (prec, m1, m2, m3, NULL);
13004 mpfr_from_real (m1, ra1, GMP_RNDN);
13005 mpfr_from_real (m2, ra2, GMP_RNDN);
13006 mpfr_from_real (m3, ra3, GMP_RNDN);
13007 mpfr_clear_flags ();
13008 inexact = func (m1, m1, m2, m3, rnd);
13009 result = do_mpfr_ckconv (m1, type, inexact);
13010 mpfr_clears (m1, m2, m3, NULL);
13014 return result;
13017 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13018 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13019 If ARG_SINP and ARG_COSP are NULL then the result is returned
13020 as a complex value.
13021 The type is taken from the type of ARG and is used for setting the
13022 precision of the calculation and results. */
13024 static tree
13025 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13027 tree const type = TREE_TYPE (arg);
13028 tree result = NULL_TREE;
13030 STRIP_NOPS (arg);
13032 /* To proceed, MPFR must exactly represent the target floating point
13033 format, which only happens when the target base equals two. */
13034 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13035 && TREE_CODE (arg) == REAL_CST
13036 && !TREE_OVERFLOW (arg))
13038 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13040 if (real_isfinite (ra))
13042 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13043 const int prec = fmt->p;
13044 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13045 tree result_s, result_c;
13046 int inexact;
13047 mpfr_t m, ms, mc;
13049 mpfr_inits2 (prec, m, ms, mc, NULL);
13050 mpfr_from_real (m, ra, GMP_RNDN);
13051 mpfr_clear_flags ();
13052 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13053 result_s = do_mpfr_ckconv (ms, type, inexact);
13054 result_c = do_mpfr_ckconv (mc, type, inexact);
13055 mpfr_clears (m, ms, mc, NULL);
13056 if (result_s && result_c)
13058 /* If we are to return in a complex value do so. */
13059 if (!arg_sinp && !arg_cosp)
13060 return build_complex (build_complex_type (type),
13061 result_c, result_s);
13063 /* Dereference the sin/cos pointer arguments. */
13064 arg_sinp = build_fold_indirect_ref (arg_sinp);
13065 arg_cosp = build_fold_indirect_ref (arg_cosp);
13066 /* Proceed if valid pointer type were passed in. */
13067 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13068 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13070 /* Set the values. */
13071 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13072 result_s);
13073 TREE_SIDE_EFFECTS (result_s) = 1;
13074 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13075 result_c);
13076 TREE_SIDE_EFFECTS (result_c) = 1;
13077 /* Combine the assignments into a compound expr. */
13078 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13079 result_s, result_c));
13084 return result;
13087 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13088 two-argument mpfr order N Bessel function FUNC on them and return
13089 the resulting value as a tree with type TYPE. The mpfr precision
13090 is set to the precision of TYPE. We assume that function FUNC
13091 returns zero if the result could be calculated exactly within the
13092 requested precision. */
13093 static tree
13094 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13095 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13096 const REAL_VALUE_TYPE *min, bool inclusive)
13098 tree result = NULL_TREE;
13100 STRIP_NOPS (arg1);
13101 STRIP_NOPS (arg2);
13103 /* To proceed, MPFR must exactly represent the target floating point
13104 format, which only happens when the target base equals two. */
13105 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13106 && host_integerp (arg1, 0)
13107 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13109 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13110 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13112 if (n == (long)n
13113 && real_isfinite (ra)
13114 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13116 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13117 const int prec = fmt->p;
13118 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13119 int inexact;
13120 mpfr_t m;
13122 mpfr_init2 (m, prec);
13123 mpfr_from_real (m, ra, GMP_RNDN);
13124 mpfr_clear_flags ();
13125 inexact = func (m, n, m, rnd);
13126 result = do_mpfr_ckconv (m, type, inexact);
13127 mpfr_clear (m);
13131 return result;
13134 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13135 the pointer *(ARG_QUO) and return the result. The type is taken
13136 from the type of ARG0 and is used for setting the precision of the
13137 calculation and results. */
13139 static tree
13140 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13142 tree const type = TREE_TYPE (arg0);
13143 tree result = NULL_TREE;
13145 STRIP_NOPS (arg0);
13146 STRIP_NOPS (arg1);
13148 /* To proceed, MPFR must exactly represent the target floating point
13149 format, which only happens when the target base equals two. */
13150 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13151 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13152 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13154 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13155 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13157 if (real_isfinite (ra0) && real_isfinite (ra1))
13159 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13160 const int prec = fmt->p;
13161 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13162 tree result_rem;
13163 long integer_quo;
13164 mpfr_t m0, m1;
13166 mpfr_inits2 (prec, m0, m1, NULL);
13167 mpfr_from_real (m0, ra0, GMP_RNDN);
13168 mpfr_from_real (m1, ra1, GMP_RNDN);
13169 mpfr_clear_flags ();
13170 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13171 /* Remquo is independent of the rounding mode, so pass
13172 inexact=0 to do_mpfr_ckconv(). */
13173 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13174 mpfr_clears (m0, m1, NULL);
13175 if (result_rem)
13177 /* MPFR calculates quo in the host's long so it may
13178 return more bits in quo than the target int can hold
13179 if sizeof(host long) > sizeof(target int). This can
13180 happen even for native compilers in LP64 mode. In
13181 these cases, modulo the quo value with the largest
13182 number that the target int can hold while leaving one
13183 bit for the sign. */
13184 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13185 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13187 /* Dereference the quo pointer argument. */
13188 arg_quo = build_fold_indirect_ref (arg_quo);
13189 /* Proceed iff a valid pointer type was passed in. */
13190 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13192 /* Set the value. */
13193 tree result_quo = fold_build2 (MODIFY_EXPR,
13194 TREE_TYPE (arg_quo), arg_quo,
13195 build_int_cst (NULL, integer_quo));
13196 TREE_SIDE_EFFECTS (result_quo) = 1;
13197 /* Combine the quo assignment with the rem. */
13198 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13199 result_quo, result_rem));
13204 return result;
13207 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13208 resulting value as a tree with type TYPE. The mpfr precision is
13209 set to the precision of TYPE. We assume that this mpfr function
13210 returns zero if the result could be calculated exactly within the
13211 requested precision. In addition, the integer pointer represented
13212 by ARG_SG will be dereferenced and set to the appropriate signgam
13213 (-1,1) value. */
13215 static tree
13216 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13218 tree result = NULL_TREE;
13220 STRIP_NOPS (arg);
13222 /* To proceed, MPFR must exactly represent the target floating point
13223 format, which only happens when the target base equals two. Also
13224 verify ARG is a constant and that ARG_SG is an int pointer. */
13225 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13226 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13227 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13228 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13230 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13232 /* In addition to NaN and Inf, the argument cannot be zero or a
13233 negative integer. */
13234 if (real_isfinite (ra)
13235 && ra->cl != rvc_zero
13236 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13238 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13239 const int prec = fmt->p;
13240 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13241 int inexact, sg;
13242 mpfr_t m;
13243 tree result_lg;
13245 mpfr_init2 (m, prec);
13246 mpfr_from_real (m, ra, GMP_RNDN);
13247 mpfr_clear_flags ();
13248 inexact = mpfr_lgamma (m, &sg, m, rnd);
13249 result_lg = do_mpfr_ckconv (m, type, inexact);
13250 mpfr_clear (m);
13251 if (result_lg)
13253 tree result_sg;
13255 /* Dereference the arg_sg pointer argument. */
13256 arg_sg = build_fold_indirect_ref (arg_sg);
13257 /* Assign the signgam value into *arg_sg. */
13258 result_sg = fold_build2 (MODIFY_EXPR,
13259 TREE_TYPE (arg_sg), arg_sg,
13260 build_int_cst (NULL, sg));
13261 TREE_SIDE_EFFECTS (result_sg) = 1;
13262 /* Combine the signgam assignment with the lgamma result. */
13263 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13264 result_sg, result_lg));
13269 return result;
13272 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13273 function FUNC on it and return the resulting value as a tree with
13274 type TYPE. The mpfr precision is set to the precision of TYPE. We
13275 assume that function FUNC returns zero if the result could be
13276 calculated exactly within the requested precision. */
13278 static tree
13279 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13281 tree result = NULL_TREE;
13283 STRIP_NOPS (arg);
13285 /* To proceed, MPFR must exactly represent the target floating point
13286 format, which only happens when the target base equals two. */
13287 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13288 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13289 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13291 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13292 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13294 if (real_isfinite (re) && real_isfinite (im))
13296 const struct real_format *const fmt =
13297 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13298 const int prec = fmt->p;
13299 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13300 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13301 int inexact;
13302 mpc_t m;
13304 mpc_init2 (m, prec);
13305 mpfr_from_real (mpc_realref(m), re, rnd);
13306 mpfr_from_real (mpc_imagref(m), im, rnd);
13307 mpfr_clear_flags ();
13308 inexact = func (m, m, crnd);
13309 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13310 mpc_clear (m);
13314 return result;
13317 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13318 mpc function FUNC on it and return the resulting value as a tree
13319 with type TYPE. The mpfr precision is set to the precision of
13320 TYPE. We assume that function FUNC returns zero if the result
13321 could be calculated exactly within the requested precision. If
13322 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13323 in the arguments and/or results. */
13325 tree
13326 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13327 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13329 tree result = NULL_TREE;
13331 STRIP_NOPS (arg0);
13332 STRIP_NOPS (arg1);
13334 /* To proceed, MPFR must exactly represent the target floating point
13335 format, which only happens when the target base equals two. */
13336 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13337 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13338 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13339 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13340 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13342 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13343 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13344 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13345 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13347 if (do_nonfinite
13348 || (real_isfinite (re0) && real_isfinite (im0)
13349 && real_isfinite (re1) && real_isfinite (im1)))
13351 const struct real_format *const fmt =
13352 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13353 const int prec = fmt->p;
13354 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13355 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13356 int inexact;
13357 mpc_t m0, m1;
13359 mpc_init2 (m0, prec);
13360 mpc_init2 (m1, prec);
13361 mpfr_from_real (mpc_realref(m0), re0, rnd);
13362 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13363 mpfr_from_real (mpc_realref(m1), re1, rnd);
13364 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13365 mpfr_clear_flags ();
13366 inexact = func (m0, m0, m1, crnd);
13367 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13368 mpc_clear (m0);
13369 mpc_clear (m1);
13373 return result;
13376 /* FIXME tuples.
13377 The functions below provide an alternate interface for folding
13378 builtin function calls presented as GIMPLE_CALL statements rather
13379 than as CALL_EXPRs. The folded result is still expressed as a
13380 tree. There is too much code duplication in the handling of
13381 varargs functions, and a more intrusive re-factoring would permit
13382 better sharing of code between the tree and statement-based
13383 versions of these functions. */
13385 /* Construct a new CALL_EXPR using the tail of the argument list of STMT
13386 along with N new arguments specified as the "..." parameters. SKIP
13387 is the number of arguments in STMT to be omitted. This function is used
13388 to do varargs-to-varargs transformations. */
13390 static tree
13391 gimple_rewrite_call_expr (gimple stmt, int skip, tree fndecl, int n, ...)
13393 int oldnargs = gimple_call_num_args (stmt);
13394 int nargs = oldnargs - skip + n;
13395 tree fntype = TREE_TYPE (fndecl);
13396 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
13397 tree *buffer;
13398 int i, j;
13399 va_list ap;
13400 location_t loc = gimple_location (stmt);
13402 buffer = XALLOCAVEC (tree, nargs);
13403 va_start (ap, n);
13404 for (i = 0; i < n; i++)
13405 buffer[i] = va_arg (ap, tree);
13406 va_end (ap);
13407 for (j = skip; j < oldnargs; j++, i++)
13408 buffer[i] = gimple_call_arg (stmt, j);
13410 return fold (build_call_array_loc (loc, TREE_TYPE (fntype), fn, nargs, buffer));
13413 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13414 a normal call should be emitted rather than expanding the function
13415 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13417 static tree
13418 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13420 tree dest, size, len, fn, fmt, flag;
13421 const char *fmt_str;
13422 int nargs = gimple_call_num_args (stmt);
13424 /* Verify the required arguments in the original call. */
13425 if (nargs < 4)
13426 return NULL_TREE;
13427 dest = gimple_call_arg (stmt, 0);
13428 if (!validate_arg (dest, POINTER_TYPE))
13429 return NULL_TREE;
13430 flag = gimple_call_arg (stmt, 1);
13431 if (!validate_arg (flag, INTEGER_TYPE))
13432 return NULL_TREE;
13433 size = gimple_call_arg (stmt, 2);
13434 if (!validate_arg (size, INTEGER_TYPE))
13435 return NULL_TREE;
13436 fmt = gimple_call_arg (stmt, 3);
13437 if (!validate_arg (fmt, POINTER_TYPE))
13438 return NULL_TREE;
13440 if (! host_integerp (size, 1))
13441 return NULL_TREE;
13443 len = NULL_TREE;
13445 if (!init_target_chars ())
13446 return NULL_TREE;
13448 /* Check whether the format is a literal string constant. */
13449 fmt_str = c_getstr (fmt);
13450 if (fmt_str != NULL)
13452 /* If the format doesn't contain % args or %%, we know the size. */
13453 if (strchr (fmt_str, target_percent) == 0)
13455 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13456 len = build_int_cstu (size_type_node, strlen (fmt_str));
13458 /* If the format is "%s" and first ... argument is a string literal,
13459 we know the size too. */
13460 else if (fcode == BUILT_IN_SPRINTF_CHK
13461 && strcmp (fmt_str, target_percent_s) == 0)
13463 tree arg;
13465 if (nargs == 5)
13467 arg = gimple_call_arg (stmt, 4);
13468 if (validate_arg (arg, POINTER_TYPE))
13470 len = c_strlen (arg, 1);
13471 if (! len || ! host_integerp (len, 1))
13472 len = NULL_TREE;
13478 if (! integer_all_onesp (size))
13480 if (! len || ! tree_int_cst_lt (len, size))
13481 return NULL_TREE;
13484 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13485 or if format doesn't contain % chars or is "%s". */
13486 if (! integer_zerop (flag))
13488 if (fmt_str == NULL)
13489 return NULL_TREE;
13490 if (strchr (fmt_str, target_percent) != NULL
13491 && strcmp (fmt_str, target_percent_s))
13492 return NULL_TREE;
13495 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13496 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
13497 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
13498 if (!fn)
13499 return NULL_TREE;
13501 return gimple_rewrite_call_expr (stmt, 4, fn, 2, dest, fmt);
13504 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13505 a normal call should be emitted rather than expanding the function
13506 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13507 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13508 passed as second argument. */
13510 tree
13511 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13512 enum built_in_function fcode)
13514 tree dest, size, len, fn, fmt, flag;
13515 const char *fmt_str;
13517 /* Verify the required arguments in the original call. */
13518 if (gimple_call_num_args (stmt) < 5)
13519 return NULL_TREE;
13520 dest = gimple_call_arg (stmt, 0);
13521 if (!validate_arg (dest, POINTER_TYPE))
13522 return NULL_TREE;
13523 len = gimple_call_arg (stmt, 1);
13524 if (!validate_arg (len, INTEGER_TYPE))
13525 return NULL_TREE;
13526 flag = gimple_call_arg (stmt, 2);
13527 if (!validate_arg (flag, INTEGER_TYPE))
13528 return NULL_TREE;
13529 size = gimple_call_arg (stmt, 3);
13530 if (!validate_arg (size, INTEGER_TYPE))
13531 return NULL_TREE;
13532 fmt = gimple_call_arg (stmt, 4);
13533 if (!validate_arg (fmt, POINTER_TYPE))
13534 return NULL_TREE;
13536 if (! host_integerp (size, 1))
13537 return NULL_TREE;
13539 if (! integer_all_onesp (size))
13541 if (! host_integerp (len, 1))
13543 /* If LEN is not constant, try MAXLEN too.
13544 For MAXLEN only allow optimizing into non-_ocs function
13545 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13546 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13547 return NULL_TREE;
13549 else
13550 maxlen = len;
13552 if (tree_int_cst_lt (size, maxlen))
13553 return NULL_TREE;
13556 if (!init_target_chars ())
13557 return NULL_TREE;
13559 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13560 or if format doesn't contain % chars or is "%s". */
13561 if (! integer_zerop (flag))
13563 fmt_str = c_getstr (fmt);
13564 if (fmt_str == NULL)
13565 return NULL_TREE;
13566 if (strchr (fmt_str, target_percent) != NULL
13567 && strcmp (fmt_str, target_percent_s))
13568 return NULL_TREE;
13571 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13572 available. */
13573 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
13574 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
13575 if (!fn)
13576 return NULL_TREE;
13578 return gimple_rewrite_call_expr (stmt, 5, fn, 3, dest, len, fmt);
13581 /* Builtins with folding operations that operate on "..." arguments
13582 need special handling; we need to store the arguments in a convenient
13583 data structure before attempting any folding. Fortunately there are
13584 only a few builtins that fall into this category. FNDECL is the
13585 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13586 result of the function call is ignored. */
13588 static tree
13589 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13590 bool ignore ATTRIBUTE_UNUSED)
13592 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13593 tree ret = NULL_TREE;
13595 switch (fcode)
13597 case BUILT_IN_SPRINTF_CHK:
13598 case BUILT_IN_VSPRINTF_CHK:
13599 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13600 break;
13602 case BUILT_IN_SNPRINTF_CHK:
13603 case BUILT_IN_VSNPRINTF_CHK:
13604 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13606 default:
13607 break;
13609 if (ret)
13611 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13612 TREE_NO_WARNING (ret) = 1;
13613 return ret;
13615 return NULL_TREE;
13618 /* A wrapper function for builtin folding that prevents warnings for
13619 "statement without effect" and the like, caused by removing the
13620 call node earlier than the warning is generated. */
13622 tree
13623 fold_call_stmt (gimple stmt, bool ignore)
13625 tree ret = NULL_TREE;
13626 tree fndecl = gimple_call_fndecl (stmt);
13627 location_t loc = gimple_location (stmt);
13628 if (fndecl
13629 && TREE_CODE (fndecl) == FUNCTION_DECL
13630 && DECL_BUILT_IN (fndecl)
13631 && !gimple_call_va_arg_pack_p (stmt))
13633 int nargs = gimple_call_num_args (stmt);
13635 if (avoid_folding_inline_builtin (fndecl))
13636 return NULL_TREE;
13637 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13639 return targetm.fold_builtin (fndecl, nargs,
13640 (nargs > 0
13641 ? gimple_call_arg_ptr (stmt, 0)
13642 : &error_mark_node), ignore);
13644 else
13646 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13648 tree args[MAX_ARGS_TO_FOLD_BUILTIN];
13649 int i;
13650 for (i = 0; i < nargs; i++)
13651 args[i] = gimple_call_arg (stmt, i);
13652 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13654 if (!ret)
13655 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13656 if (ret)
13658 /* Propagate location information from original call to
13659 expansion of builtin. Otherwise things like
13660 maybe_emit_chk_warning, that operate on the expansion
13661 of a builtin, will use the wrong location information. */
13662 if (gimple_has_location (stmt))
13664 tree realret = ret;
13665 if (TREE_CODE (ret) == NOP_EXPR)
13666 realret = TREE_OPERAND (ret, 0);
13667 if (CAN_HAVE_LOCATION_P (realret)
13668 && !EXPR_HAS_LOCATION (realret))
13669 SET_EXPR_LOCATION (realret, loc);
13670 return realret;
13672 return ret;
13676 return NULL_TREE;
13679 /* Look up the function in built_in_decls that corresponds to DECL
13680 and set ASMSPEC as its user assembler name. DECL must be a
13681 function decl that declares a builtin. */
13683 void
13684 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13686 tree builtin;
13687 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13688 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13689 && asmspec != 0);
13691 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13692 set_user_assembler_name (builtin, asmspec);
13693 switch (DECL_FUNCTION_CODE (decl))
13695 case BUILT_IN_MEMCPY:
13696 init_block_move_fn (asmspec);
13697 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13698 break;
13699 case BUILT_IN_MEMSET:
13700 init_block_clear_fn (asmspec);
13701 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13702 break;
13703 case BUILT_IN_MEMMOVE:
13704 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13705 break;
13706 case BUILT_IN_MEMCMP:
13707 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13708 break;
13709 case BUILT_IN_ABORT:
13710 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13711 break;
13712 case BUILT_IN_FFS:
13713 if (INT_TYPE_SIZE < BITS_PER_WORD)
13715 set_user_assembler_libfunc ("ffs", asmspec);
13716 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13717 MODE_INT, 0), "ffs");
13719 break;
13720 default:
13721 break;
13725 /* Return true if DECL is a builtin that expands to a constant or similarly
13726 simple code. */
13727 bool
13728 is_simple_builtin (tree decl)
13730 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13731 switch (DECL_FUNCTION_CODE (decl))
13733 /* Builtins that expand to constants. */
13734 case BUILT_IN_CONSTANT_P:
13735 case BUILT_IN_EXPECT:
13736 case BUILT_IN_OBJECT_SIZE:
13737 case BUILT_IN_UNREACHABLE:
13738 /* Simple register moves or loads from stack. */
13739 case BUILT_IN_RETURN_ADDRESS:
13740 case BUILT_IN_EXTRACT_RETURN_ADDR:
13741 case BUILT_IN_FROB_RETURN_ADDR:
13742 case BUILT_IN_RETURN:
13743 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13744 case BUILT_IN_FRAME_ADDRESS:
13745 case BUILT_IN_VA_END:
13746 case BUILT_IN_STACK_SAVE:
13747 case BUILT_IN_STACK_RESTORE:
13748 /* Exception state returns or moves registers around. */
13749 case BUILT_IN_EH_FILTER:
13750 case BUILT_IN_EH_POINTER:
13751 case BUILT_IN_EH_COPY_VALUES:
13752 return true;
13754 default:
13755 return false;
13758 return false;
13761 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13762 most probably expanded inline into reasonably simple code. This is a
13763 superset of is_simple_builtin. */
13764 bool
13765 is_inexpensive_builtin (tree decl)
13767 if (!decl)
13768 return false;
13769 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13770 return true;
13771 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13772 switch (DECL_FUNCTION_CODE (decl))
13774 case BUILT_IN_ABS:
13775 case BUILT_IN_ALLOCA:
13776 case BUILT_IN_BSWAP32:
13777 case BUILT_IN_BSWAP64:
13778 case BUILT_IN_CLZ:
13779 case BUILT_IN_CLZIMAX:
13780 case BUILT_IN_CLZL:
13781 case BUILT_IN_CLZLL:
13782 case BUILT_IN_CTZ:
13783 case BUILT_IN_CTZIMAX:
13784 case BUILT_IN_CTZL:
13785 case BUILT_IN_CTZLL:
13786 case BUILT_IN_FFS:
13787 case BUILT_IN_FFSIMAX:
13788 case BUILT_IN_FFSL:
13789 case BUILT_IN_FFSLL:
13790 case BUILT_IN_IMAXABS:
13791 case BUILT_IN_FINITE:
13792 case BUILT_IN_FINITEF:
13793 case BUILT_IN_FINITEL:
13794 case BUILT_IN_FINITED32:
13795 case BUILT_IN_FINITED64:
13796 case BUILT_IN_FINITED128:
13797 case BUILT_IN_FPCLASSIFY:
13798 case BUILT_IN_ISFINITE:
13799 case BUILT_IN_ISINF_SIGN:
13800 case BUILT_IN_ISINF:
13801 case BUILT_IN_ISINFF:
13802 case BUILT_IN_ISINFL:
13803 case BUILT_IN_ISINFD32:
13804 case BUILT_IN_ISINFD64:
13805 case BUILT_IN_ISINFD128:
13806 case BUILT_IN_ISNAN:
13807 case BUILT_IN_ISNANF:
13808 case BUILT_IN_ISNANL:
13809 case BUILT_IN_ISNAND32:
13810 case BUILT_IN_ISNAND64:
13811 case BUILT_IN_ISNAND128:
13812 case BUILT_IN_ISNORMAL:
13813 case BUILT_IN_ISGREATER:
13814 case BUILT_IN_ISGREATEREQUAL:
13815 case BUILT_IN_ISLESS:
13816 case BUILT_IN_ISLESSEQUAL:
13817 case BUILT_IN_ISLESSGREATER:
13818 case BUILT_IN_ISUNORDERED:
13819 case BUILT_IN_VA_ARG_PACK:
13820 case BUILT_IN_VA_ARG_PACK_LEN:
13821 case BUILT_IN_VA_COPY:
13822 case BUILT_IN_TRAP:
13823 case BUILT_IN_SAVEREGS:
13824 case BUILT_IN_POPCOUNTL:
13825 case BUILT_IN_POPCOUNTLL:
13826 case BUILT_IN_POPCOUNTIMAX:
13827 case BUILT_IN_POPCOUNT:
13828 case BUILT_IN_PARITYL:
13829 case BUILT_IN_PARITYLL:
13830 case BUILT_IN_PARITYIMAX:
13831 case BUILT_IN_PARITY:
13832 case BUILT_IN_LABS:
13833 case BUILT_IN_LLABS:
13834 case BUILT_IN_PREFETCH:
13835 return true;
13837 default:
13838 return is_simple_builtin (decl);
13841 return false;