1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
24 #include "coretypes.h"
33 #include "hard-reg-set.h"
36 #include "insn-config.h"
42 #include "typeclass.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
50 #include "tree-flow.h"
51 #include "value-prof.h"
52 #include "diagnostic.h"
54 #ifndef SLOW_UNALIGNED_ACCESS
55 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
58 #ifndef PAD_VARARGS_DOWN
59 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
62 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
72 #include "builtins.def"
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 tree built_in_decls[(int) END_BUILTINS];
79 /* Declarations used when constructing the builtin implicitly in the compiler.
80 It may be NULL_TREE when this is invalid (for instance runtime is not
81 required to implement the function call in all cases). */
82 tree implicit_built_in_decls[(int) END_BUILTINS];
84 static const char *c_getstr (tree);
85 static rtx c_readstr (const char *, enum machine_mode);
86 static int target_char_cast (tree, char *);
87 static rtx get_memory_rtx (tree, tree);
88 static int apply_args_size (void);
89 static int apply_result_size (void);
90 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
91 static rtx result_vector (int, rtx);
93 static void expand_builtin_update_setjmp_buf (rtx);
94 static void expand_builtin_prefetch (tree);
95 static rtx expand_builtin_apply_args (void);
96 static rtx expand_builtin_apply_args_1 (void);
97 static rtx expand_builtin_apply (rtx, rtx, rtx);
98 static void expand_builtin_return (rtx);
99 static enum type_class type_to_class (tree);
100 static rtx expand_builtin_classify_type (tree);
101 static void expand_errno_check (tree, rtx);
102 static rtx expand_builtin_mathfn (tree, rtx, rtx);
103 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
104 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
105 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
106 static rtx expand_builtin_sincos (tree);
107 static rtx expand_builtin_cexpi (tree, rtx, rtx);
108 static rtx expand_builtin_int_roundingfn (tree, rtx);
109 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
110 static rtx expand_builtin_args_info (tree);
111 static rtx expand_builtin_next_arg (void);
112 static rtx expand_builtin_va_start (tree);
113 static rtx expand_builtin_va_end (tree);
114 static rtx expand_builtin_va_copy (tree);
115 static rtx expand_builtin_memchr (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
119 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
120 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
123 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx,
127 enum machine_mode, int);
128 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int);
129 static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx,
130 enum machine_mode, int);
131 static rtx expand_builtin_bcopy (tree, int);
132 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
133 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
134 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
136 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
137 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
139 static rtx expand_builtin_bzero (tree);
140 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
141 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
142 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
143 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
144 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
145 static rtx expand_builtin_alloca (tree, rtx);
146 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
147 static rtx expand_builtin_frame_address (tree, tree);
148 static rtx expand_builtin_fputs (tree, rtx, bool);
149 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
150 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
151 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
152 static tree stabilize_va_list (tree, int);
153 static rtx expand_builtin_expect (tree, rtx);
154 static tree fold_builtin_constant_p (tree);
155 static tree fold_builtin_expect (tree, tree);
156 static tree fold_builtin_classify_type (tree);
157 static tree fold_builtin_strlen (tree);
158 static tree fold_builtin_inf (tree, int);
159 static tree fold_builtin_nan (tree, tree, int);
160 static tree rewrite_call_expr (tree, int, tree, int, ...);
161 static bool validate_arg (const_tree, enum tree_code code);
162 static bool integer_valued_real_p (tree);
163 static tree fold_trunc_transparent_mathfn (tree, tree);
164 static bool readonly_data_expr (tree);
165 static rtx expand_builtin_fabs (tree, rtx, rtx);
166 static rtx expand_builtin_signbit (tree, rtx);
167 static tree fold_builtin_sqrt (tree, tree);
168 static tree fold_builtin_cbrt (tree, tree);
169 static tree fold_builtin_pow (tree, tree, tree, tree);
170 static tree fold_builtin_powi (tree, tree, tree, tree);
171 static tree fold_builtin_cos (tree, tree, tree);
172 static tree fold_builtin_cosh (tree, tree, tree);
173 static tree fold_builtin_tan (tree, tree);
174 static tree fold_builtin_trunc (tree, tree);
175 static tree fold_builtin_floor (tree, tree);
176 static tree fold_builtin_ceil (tree, tree);
177 static tree fold_builtin_round (tree, tree);
178 static tree fold_builtin_int_roundingfn (tree, tree);
179 static tree fold_builtin_bitop (tree, tree);
180 static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int);
181 static tree fold_builtin_strchr (tree, tree, tree);
182 static tree fold_builtin_memchr (tree, tree, tree, tree);
183 static tree fold_builtin_memcmp (tree, tree, tree);
184 static tree fold_builtin_strcmp (tree, tree);
185 static tree fold_builtin_strncmp (tree, tree, tree);
186 static tree fold_builtin_signbit (tree, tree);
187 static tree fold_builtin_copysign (tree, tree, tree, tree);
188 static tree fold_builtin_isascii (tree);
189 static tree fold_builtin_toascii (tree);
190 static tree fold_builtin_isdigit (tree);
191 static tree fold_builtin_fabs (tree, tree);
192 static tree fold_builtin_abs (tree, tree);
193 static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code,
195 static tree fold_builtin_n (tree, tree *, int, bool);
196 static tree fold_builtin_0 (tree, bool);
197 static tree fold_builtin_1 (tree, tree, bool);
198 static tree fold_builtin_2 (tree, tree, tree, bool);
199 static tree fold_builtin_3 (tree, tree, tree, tree, bool);
200 static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool);
201 static tree fold_builtin_varargs (tree, tree, bool);
203 static tree fold_builtin_strpbrk (tree, tree, tree);
204 static tree fold_builtin_strstr (tree, tree, tree);
205 static tree fold_builtin_strrchr (tree, tree, tree);
206 static tree fold_builtin_strcat (tree, tree);
207 static tree fold_builtin_strncat (tree, tree, tree);
208 static tree fold_builtin_strspn (tree, tree);
209 static tree fold_builtin_strcspn (tree, tree);
210 static tree fold_builtin_sprintf (tree, tree, tree, int);
212 static rtx expand_builtin_object_size (tree);
213 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
214 enum built_in_function);
215 static void maybe_emit_chk_warning (tree, enum built_in_function);
216 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
217 static void maybe_emit_free_warning (tree);
218 static tree fold_builtin_object_size (tree, tree);
219 static tree fold_builtin_strcat_chk (tree, tree, tree, tree);
220 static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree);
221 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
222 static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function);
223 static tree fold_builtin_fprintf (tree, tree, tree, tree, bool,
224 enum built_in_function);
225 static bool init_target_chars (void);
227 static unsigned HOST_WIDE_INT target_newline;
228 static unsigned HOST_WIDE_INT target_percent;
229 static unsigned HOST_WIDE_INT target_c;
230 static unsigned HOST_WIDE_INT target_s;
231 static char target_percent_c[3];
232 static char target_percent_s[3];
233 static char target_percent_s_newline[4];
234 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
235 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
236 static tree do_mpfr_arg2 (tree, tree, tree,
237 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
238 static tree do_mpfr_arg3 (tree, tree, tree, tree,
239 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
240 static tree do_mpfr_sincos (tree, tree, tree);
241 static tree do_mpfr_bessel_n (tree, tree, tree,
242 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
243 const REAL_VALUE_TYPE *, bool);
244 static tree do_mpfr_remquo (tree, tree, tree);
245 static tree do_mpfr_lgamma_r (tree, tree, tree);
248 is_builtin_name (const char *name)
250 if (strncmp (name, "__builtin_", 10) == 0)
252 if (strncmp (name, "__sync_", 7) == 0)
257 /* Return true if NODE should be considered for inline expansion regardless
258 of the optimization level. This means whenever a function is invoked with
259 its "internal" name, which normally contains the prefix "__builtin". */
262 called_as_built_in (tree node)
264 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
265 we want the name used to call the function, not the name it
267 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
268 return is_builtin_name (name);
271 /* Return the alignment in bits of EXP, an object.
272 Don't return more than MAX_ALIGN no matter what, ALIGN is the inital
273 guessed alignment e.g. from type alignment. */
276 get_object_alignment (tree exp, unsigned int align, unsigned int max_align)
281 if (handled_component_p (exp))
283 HOST_WIDE_INT bitsize, bitpos;
285 enum machine_mode mode;
286 int unsignedp, volatilep;
288 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
289 &mode, &unsignedp, &volatilep, true);
291 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
296 if (TREE_CODE (offset) == PLUS_EXPR)
298 next_offset = TREE_OPERAND (offset, 0);
299 offset = TREE_OPERAND (offset, 1);
303 if (host_integerp (offset, 1))
305 /* Any overflow in calculating offset_bits won't change
308 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
311 inner = MIN (inner, (offset_bits & -offset_bits));
313 else if (TREE_CODE (offset) == MULT_EXPR
314 && host_integerp (TREE_OPERAND (offset, 1), 1))
316 /* Any overflow in calculating offset_factor won't change
318 unsigned offset_factor
319 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
323 inner = MIN (inner, (offset_factor & -offset_factor));
327 inner = MIN (inner, BITS_PER_UNIT);
330 offset = next_offset;
334 align = MIN (inner, DECL_ALIGN (exp));
335 #ifdef CONSTANT_ALIGNMENT
336 else if (CONSTANT_CLASS_P (exp))
337 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
339 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
340 || TREE_CODE (exp) == INDIRECT_REF)
341 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
343 align = MIN (align, inner);
344 return MIN (align, max_align);
347 /* Return the alignment in bits of EXP, a pointer valued expression.
348 But don't return more than MAX_ALIGN no matter what.
349 The alignment returned is, by default, the alignment of the thing that
350 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
352 Otherwise, look at the expression to see if we can do better, i.e., if the
353 expression is actually pointing at an object whose alignment is tighter. */
356 get_pointer_alignment (tree exp, unsigned int max_align)
358 unsigned int align, inner;
360 /* We rely on TER to compute accurate alignment information. */
361 if (!(optimize && flag_tree_ter))
364 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
367 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
368 align = MIN (align, max_align);
372 switch (TREE_CODE (exp))
375 exp = TREE_OPERAND (exp, 0);
376 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
379 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
380 align = MIN (inner, max_align);
383 case POINTER_PLUS_EXPR:
384 /* If sum of pointer + int, restrict our maximum alignment to that
385 imposed by the integer. If not, we can't do any better than
387 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
390 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
391 & (max_align / BITS_PER_UNIT - 1))
395 exp = TREE_OPERAND (exp, 0);
399 /* See what we are pointing at and look at its alignment. */
400 return get_object_alignment (TREE_OPERAND (exp, 0), align, max_align);
408 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
409 way, because it could contain a zero byte in the middle.
410 TREE_STRING_LENGTH is the size of the character array, not the string.
412 ONLY_VALUE should be nonzero if the result is not going to be emitted
413 into the instruction stream and zero if it is going to be expanded.
414 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
415 is returned, otherwise NULL, since
416 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
417 evaluate the side-effects.
419 The value returned is of type `ssizetype'.
421 Unfortunately, string_constant can't access the values of const char
422 arrays with initializers, so neither can we do so here. */
425 c_strlen (tree src, int only_value)
428 HOST_WIDE_INT offset;
433 if (TREE_CODE (src) == COND_EXPR
434 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
438 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
439 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
440 if (tree_int_cst_equal (len1, len2))
444 if (TREE_CODE (src) == COMPOUND_EXPR
445 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
446 return c_strlen (TREE_OPERAND (src, 1), only_value);
448 src = string_constant (src, &offset_node);
452 max = TREE_STRING_LENGTH (src) - 1;
453 ptr = TREE_STRING_POINTER (src);
455 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
457 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
458 compute the offset to the following null if we don't know where to
459 start searching for it. */
462 for (i = 0; i < max; i++)
466 /* We don't know the starting offset, but we do know that the string
467 has no internal zero bytes. We can assume that the offset falls
468 within the bounds of the string; otherwise, the programmer deserves
469 what he gets. Subtract the offset from the length of the string,
470 and return that. This would perhaps not be valid if we were dealing
471 with named arrays in addition to literal string constants. */
473 return size_diffop (size_int (max), offset_node);
476 /* We have a known offset into the string. Start searching there for
477 a null character if we can represent it as a single HOST_WIDE_INT. */
478 if (offset_node == 0)
480 else if (! host_integerp (offset_node, 0))
483 offset = tree_low_cst (offset_node, 0);
485 /* If the offset is known to be out of bounds, warn, and call strlen at
487 if (offset < 0 || offset > max)
489 /* Suppress multiple warnings for propagated constant strings. */
490 if (! TREE_NO_WARNING (src))
492 warning (0, "offset outside bounds of constant string");
493 TREE_NO_WARNING (src) = 1;
498 /* Use strlen to search for the first zero byte. Since any strings
499 constructed with build_string will have nulls appended, we win even
500 if we get handed something like (char[4])"abcd".
502 Since OFFSET is our starting index into the string, no further
503 calculation is needed. */
504 return ssize_int (strlen (ptr + offset));
507 /* Return a char pointer for a C string if it is a string constant
508 or sum of string constant and integer constant. */
515 src = string_constant (src, &offset_node);
519 if (offset_node == 0)
520 return TREE_STRING_POINTER (src);
521 else if (!host_integerp (offset_node, 1)
522 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
525 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
528 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
529 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
532 c_readstr (const char *str, enum machine_mode mode)
538 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
543 for (i = 0; i < GET_MODE_SIZE (mode); i++)
546 if (WORDS_BIG_ENDIAN)
547 j = GET_MODE_SIZE (mode) - i - 1;
548 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
549 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
550 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
552 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
555 ch = (unsigned char) str[i];
556 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
558 return immed_double_const (c[0], c[1], mode);
561 /* Cast a target constant CST to target CHAR and if that value fits into
562 host char type, return zero and put that value into variable pointed to by
566 target_char_cast (tree cst, char *p)
568 unsigned HOST_WIDE_INT val, hostval;
570 if (!host_integerp (cst, 1)
571 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
574 val = tree_low_cst (cst, 1);
575 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
576 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
579 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
580 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
589 /* Similar to save_expr, but assumes that arbitrary code is not executed
590 in between the multiple evaluations. In particular, we assume that a
591 non-addressable local variable will not be modified. */
594 builtin_save_expr (tree exp)
596 if (TREE_ADDRESSABLE (exp) == 0
597 && (TREE_CODE (exp) == PARM_DECL
598 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
601 return save_expr (exp);
604 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
605 times to get the address of either a higher stack frame, or a return
606 address located within it (depending on FNDECL_CODE). */
609 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
613 #ifdef INITIAL_FRAME_ADDRESS_RTX
614 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
618 /* For a zero count with __builtin_return_address, we don't care what
619 frame address we return, because target-specific definitions will
620 override us. Therefore frame pointer elimination is OK, and using
621 the soft frame pointer is OK.
623 For a nonzero count, or a zero count with __builtin_frame_address,
624 we require a stable offset from the current frame pointer to the
625 previous one, so we must use the hard frame pointer, and
626 we must disable frame pointer elimination. */
627 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
628 tem = frame_pointer_rtx;
631 tem = hard_frame_pointer_rtx;
633 /* Tell reload not to eliminate the frame pointer. */
634 crtl->accesses_prior_frames = 1;
638 /* Some machines need special handling before we can access
639 arbitrary frames. For example, on the SPARC, we must first flush
640 all register windows to the stack. */
641 #ifdef SETUP_FRAME_ADDRESSES
643 SETUP_FRAME_ADDRESSES ();
646 /* On the SPARC, the return address is not in the frame, it is in a
647 register. There is no way to access it off of the current frame
648 pointer, but it can be accessed off the previous frame pointer by
649 reading the value from the register window save area. */
650 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
651 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
655 /* Scan back COUNT frames to the specified frame. */
656 for (i = 0; i < count; i++)
658 /* Assume the dynamic chain pointer is in the word that the
659 frame address points to, unless otherwise specified. */
660 #ifdef DYNAMIC_CHAIN_ADDRESS
661 tem = DYNAMIC_CHAIN_ADDRESS (tem);
663 tem = memory_address (Pmode, tem);
664 tem = gen_frame_mem (Pmode, tem);
665 tem = copy_to_reg (tem);
668 /* For __builtin_frame_address, return what we've got. But, on
669 the SPARC for example, we may have to add a bias. */
670 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
671 #ifdef FRAME_ADDR_RTX
672 return FRAME_ADDR_RTX (tem);
677 /* For __builtin_return_address, get the return address from that frame. */
678 #ifdef RETURN_ADDR_RTX
679 tem = RETURN_ADDR_RTX (count, tem);
681 tem = memory_address (Pmode,
682 plus_constant (tem, GET_MODE_SIZE (Pmode)));
683 tem = gen_frame_mem (Pmode, tem);
688 /* Alias set used for setjmp buffer. */
689 static alias_set_type setjmp_alias_set = -1;
691 /* Construct the leading half of a __builtin_setjmp call. Control will
692 return to RECEIVER_LABEL. This is also called directly by the SJLJ
693 exception handling code. */
696 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
698 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
702 if (setjmp_alias_set == -1)
703 setjmp_alias_set = new_alias_set ();
705 buf_addr = convert_memory_address (Pmode, buf_addr);
707 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
709 /* We store the frame pointer and the address of receiver_label in
710 the buffer and use the rest of it for the stack save area, which
711 is machine-dependent. */
713 mem = gen_rtx_MEM (Pmode, buf_addr);
714 set_mem_alias_set (mem, setjmp_alias_set);
715 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
717 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
718 set_mem_alias_set (mem, setjmp_alias_set);
720 emit_move_insn (validize_mem (mem),
721 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
723 stack_save = gen_rtx_MEM (sa_mode,
724 plus_constant (buf_addr,
725 2 * GET_MODE_SIZE (Pmode)));
726 set_mem_alias_set (stack_save, setjmp_alias_set);
727 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
729 /* If there is further processing to do, do it. */
730 #ifdef HAVE_builtin_setjmp_setup
731 if (HAVE_builtin_setjmp_setup)
732 emit_insn (gen_builtin_setjmp_setup (buf_addr));
735 /* Tell optimize_save_area_alloca that extra work is going to
736 need to go on during alloca. */
737 cfun->calls_setjmp = 1;
739 /* We have a nonlocal label. */
740 cfun->has_nonlocal_label = 1;
743 /* Construct the trailing part of a __builtin_setjmp call. This is
744 also called directly by the SJLJ exception handling code. */
747 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
749 /* Clobber the FP when we get here, so we have to make sure it's
750 marked as used by this function. */
751 emit_use (hard_frame_pointer_rtx);
753 /* Mark the static chain as clobbered here so life information
754 doesn't get messed up for it. */
755 emit_clobber (static_chain_rtx);
757 /* Now put in the code to restore the frame pointer, and argument
758 pointer, if needed. */
759 #ifdef HAVE_nonlocal_goto
760 if (! HAVE_nonlocal_goto)
763 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
764 /* This might change the hard frame pointer in ways that aren't
765 apparent to early optimization passes, so force a clobber. */
766 emit_clobber (hard_frame_pointer_rtx);
769 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
770 if (fixed_regs[ARG_POINTER_REGNUM])
772 #ifdef ELIMINABLE_REGS
774 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
776 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
777 if (elim_regs[i].from == ARG_POINTER_REGNUM
778 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
781 if (i == ARRAY_SIZE (elim_regs))
784 /* Now restore our arg pointer from the address at which it
785 was saved in our stack frame. */
786 emit_move_insn (crtl->args.internal_arg_pointer,
787 copy_to_reg (get_arg_pointer_save_area ()));
792 #ifdef HAVE_builtin_setjmp_receiver
793 if (HAVE_builtin_setjmp_receiver)
794 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
797 #ifdef HAVE_nonlocal_goto_receiver
798 if (HAVE_nonlocal_goto_receiver)
799 emit_insn (gen_nonlocal_goto_receiver ());
804 /* We must not allow the code we just generated to be reordered by
805 scheduling. Specifically, the update of the frame pointer must
806 happen immediately, not later. */
807 emit_insn (gen_blockage ());
810 /* __builtin_longjmp is passed a pointer to an array of five words (not
811 all will be used on all machines). It operates similarly to the C
812 library function of the same name, but is more efficient. Much of
813 the code below is copied from the handling of non-local gotos. */
816 expand_builtin_longjmp (rtx buf_addr, rtx value)
818 rtx fp, lab, stack, insn, last;
819 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
821 /* DRAP is needed for stack realign if longjmp is expanded to current
823 if (SUPPORTS_STACK_ALIGNMENT)
824 crtl->need_drap = true;
826 if (setjmp_alias_set == -1)
827 setjmp_alias_set = new_alias_set ();
829 buf_addr = convert_memory_address (Pmode, buf_addr);
831 buf_addr = force_reg (Pmode, buf_addr);
833 /* We used to store value in static_chain_rtx, but that fails if pointers
834 are smaller than integers. We instead require that the user must pass
835 a second argument of 1, because that is what builtin_setjmp will
836 return. This also makes EH slightly more efficient, since we are no
837 longer copying around a value that we don't care about. */
838 gcc_assert (value == const1_rtx);
840 last = get_last_insn ();
841 #ifdef HAVE_builtin_longjmp
842 if (HAVE_builtin_longjmp)
843 emit_insn (gen_builtin_longjmp (buf_addr));
847 fp = gen_rtx_MEM (Pmode, buf_addr);
848 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
849 GET_MODE_SIZE (Pmode)));
851 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
852 2 * GET_MODE_SIZE (Pmode)));
853 set_mem_alias_set (fp, setjmp_alias_set);
854 set_mem_alias_set (lab, setjmp_alias_set);
855 set_mem_alias_set (stack, setjmp_alias_set);
857 /* Pick up FP, label, and SP from the block and jump. This code is
858 from expand_goto in stmt.c; see there for detailed comments. */
859 #ifdef HAVE_nonlocal_goto
860 if (HAVE_nonlocal_goto)
861 /* We have to pass a value to the nonlocal_goto pattern that will
862 get copied into the static_chain pointer, but it does not matter
863 what that value is, because builtin_setjmp does not use it. */
864 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
868 lab = copy_to_reg (lab);
870 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
871 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
873 emit_move_insn (hard_frame_pointer_rtx, fp);
874 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
876 emit_use (hard_frame_pointer_rtx);
877 emit_use (stack_pointer_rtx);
878 emit_indirect_jump (lab);
882 /* Search backwards and mark the jump insn as a non-local goto.
883 Note that this precludes the use of __builtin_longjmp to a
884 __builtin_setjmp target in the same function. However, we've
885 already cautioned the user that these functions are for
886 internal exception handling use only. */
887 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
889 gcc_assert (insn != last);
893 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
896 else if (CALL_P (insn))
901 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
902 and the address of the save area. */
905 expand_builtin_nonlocal_goto (tree exp)
907 tree t_label, t_save_area;
908 rtx r_label, r_save_area, r_fp, r_sp, insn;
910 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
913 t_label = CALL_EXPR_ARG (exp, 0);
914 t_save_area = CALL_EXPR_ARG (exp, 1);
916 r_label = expand_normal (t_label);
917 r_label = convert_memory_address (Pmode, r_label);
918 r_save_area = expand_normal (t_save_area);
919 r_save_area = convert_memory_address (Pmode, r_save_area);
920 /* Copy the address of the save location to a register just in case it was based
921 on the frame pointer. */
922 r_save_area = copy_to_reg (r_save_area);
923 r_fp = gen_rtx_MEM (Pmode, r_save_area);
924 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
925 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
927 crtl->has_nonlocal_goto = 1;
929 #ifdef HAVE_nonlocal_goto
930 /* ??? We no longer need to pass the static chain value, afaik. */
931 if (HAVE_nonlocal_goto)
932 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
936 r_label = copy_to_reg (r_label);
938 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
939 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
941 /* Restore frame pointer for containing function.
942 This sets the actual hard register used for the frame pointer
943 to the location of the function's incoming static chain info.
944 The non-local goto handler will then adjust it to contain the
945 proper value and reload the argument pointer, if needed. */
946 emit_move_insn (hard_frame_pointer_rtx, r_fp);
947 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
949 /* USE of hard_frame_pointer_rtx added for consistency;
950 not clear if really needed. */
951 emit_use (hard_frame_pointer_rtx);
952 emit_use (stack_pointer_rtx);
954 /* If the architecture is using a GP register, we must
955 conservatively assume that the target function makes use of it.
956 The prologue of functions with nonlocal gotos must therefore
957 initialize the GP register to the appropriate value, and we
958 must then make sure that this value is live at the point
959 of the jump. (Note that this doesn't necessarily apply
960 to targets with a nonlocal_goto pattern; they are free
961 to implement it in their own way. Note also that this is
962 a no-op if the GP register is a global invariant.) */
963 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
964 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
965 emit_use (pic_offset_table_rtx);
967 emit_indirect_jump (r_label);
970 /* Search backwards to the jump insn and mark it as a
972 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
976 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
979 else if (CALL_P (insn))
986 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
987 (not all will be used on all machines) that was passed to __builtin_setjmp.
988 It updates the stack pointer in that block to correspond to the current
992 expand_builtin_update_setjmp_buf (rtx buf_addr)
994 enum machine_mode sa_mode = Pmode;
998 #ifdef HAVE_save_stack_nonlocal
999 if (HAVE_save_stack_nonlocal)
1000 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1002 #ifdef STACK_SAVEAREA_MODE
1003 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1007 = gen_rtx_MEM (sa_mode,
1010 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1014 emit_insn (gen_setjmp ());
1017 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1020 /* Expand a call to __builtin_prefetch. For a target that does not support
1021 data prefetch, evaluate the memory address argument in case it has side
1025 expand_builtin_prefetch (tree exp)
1027 tree arg0, arg1, arg2;
1031 if (!validate_arglist (exp, POINTER_TYPE, 0))
1034 arg0 = CALL_EXPR_ARG (exp, 0);
1036 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1037 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1039 nargs = call_expr_nargs (exp);
1041 arg1 = CALL_EXPR_ARG (exp, 1);
1043 arg1 = integer_zero_node;
1045 arg2 = CALL_EXPR_ARG (exp, 2);
1047 arg2 = build_int_cst (NULL_TREE, 3);
1049 /* Argument 0 is an address. */
1050 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1052 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1053 if (TREE_CODE (arg1) != INTEGER_CST)
1055 error ("second argument to %<__builtin_prefetch%> must be a constant");
1056 arg1 = integer_zero_node;
1058 op1 = expand_normal (arg1);
1059 /* Argument 1 must be either zero or one. */
1060 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1062 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1067 /* Argument 2 (locality) must be a compile-time constant int. */
1068 if (TREE_CODE (arg2) != INTEGER_CST)
1070 error ("third argument to %<__builtin_prefetch%> must be a constant");
1071 arg2 = integer_zero_node;
1073 op2 = expand_normal (arg2);
1074 /* Argument 2 must be 0, 1, 2, or 3. */
1075 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1077 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1081 #ifdef HAVE_prefetch
1084 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1086 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1087 || (GET_MODE (op0) != Pmode))
1089 op0 = convert_memory_address (Pmode, op0);
1090 op0 = force_reg (Pmode, op0);
1092 emit_insn (gen_prefetch (op0, op1, op2));
1096 /* Don't do anything with direct references to volatile memory, but
1097 generate code to handle other side effects. */
1098 if (!MEM_P (op0) && side_effects_p (op0))
1102 /* Get a MEM rtx for expression EXP which is the address of an operand
1103 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1104 the maximum length of the block of memory that might be accessed or
1108 get_memory_rtx (tree exp, tree len)
1110 tree orig_exp = exp;
1114 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1115 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1116 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1117 exp = TREE_OPERAND (exp, 0);
1119 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1120 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1122 /* Get an expression we can use to find the attributes to assign to MEM.
1123 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1124 we can. First remove any nops. */
1125 while (CONVERT_EXPR_P (exp)
1126 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1127 exp = TREE_OPERAND (exp, 0);
1130 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1131 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1132 && host_integerp (TREE_OPERAND (exp, 1), 0)
1133 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1134 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1135 else if (TREE_CODE (exp) == ADDR_EXPR)
1136 exp = TREE_OPERAND (exp, 0);
1137 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1138 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1142 /* Honor attributes derived from exp, except for the alias set
1143 (as builtin stringops may alias with anything) and the size
1144 (as stringops may access multiple array elements). */
1147 set_mem_attributes (mem, exp, 0);
1150 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1152 /* Allow the string and memory builtins to overflow from one
1153 field into another, see http://gcc.gnu.org/PR23561.
1154 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1155 memory accessed by the string or memory builtin will fit
1156 within the field. */
1157 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1159 tree mem_expr = MEM_EXPR (mem);
1160 HOST_WIDE_INT offset = -1, length = -1;
1163 while (TREE_CODE (inner) == ARRAY_REF
1164 || CONVERT_EXPR_P (inner)
1165 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1166 || TREE_CODE (inner) == SAVE_EXPR)
1167 inner = TREE_OPERAND (inner, 0);
1169 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1171 if (MEM_OFFSET (mem)
1172 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1173 offset = INTVAL (MEM_OFFSET (mem));
1175 if (offset >= 0 && len && host_integerp (len, 0))
1176 length = tree_low_cst (len, 0);
1178 while (TREE_CODE (inner) == COMPONENT_REF)
1180 tree field = TREE_OPERAND (inner, 1);
1181 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1182 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1184 /* Bitfields are generally not byte-addressable. */
1185 gcc_assert (!DECL_BIT_FIELD (field)
1186 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1187 % BITS_PER_UNIT) == 0
1188 && host_integerp (DECL_SIZE (field), 0)
1189 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1190 % BITS_PER_UNIT) == 0));
1192 /* If we can prove that the memory starting at XEXP (mem, 0) and
1193 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1194 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1195 fields without DECL_SIZE_UNIT like flexible array members. */
1197 && DECL_SIZE_UNIT (field)
1198 && host_integerp (DECL_SIZE_UNIT (field), 0))
1201 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1204 && offset + length <= size)
1209 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1210 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1211 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1219 mem_expr = TREE_OPERAND (mem_expr, 0);
1220 inner = TREE_OPERAND (inner, 0);
1223 if (mem_expr == NULL)
1225 if (mem_expr != MEM_EXPR (mem))
1227 set_mem_expr (mem, mem_expr);
1228 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1231 set_mem_alias_set (mem, 0);
1232 set_mem_size (mem, NULL_RTX);
1238 /* Built-in functions to perform an untyped call and return. */
1240 /* For each register that may be used for calling a function, this
1241 gives a mode used to copy the register's value. VOIDmode indicates
1242 the register is not used for calling a function. If the machine
1243 has register windows, this gives only the outbound registers.
1244 INCOMING_REGNO gives the corresponding inbound register. */
1245 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1247 /* For each register that may be used for returning values, this gives
1248 a mode used to copy the register's value. VOIDmode indicates the
1249 register is not used for returning values. If the machine has
1250 register windows, this gives only the outbound registers.
1251 INCOMING_REGNO gives the corresponding inbound register. */
1252 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1254 /* For each register that may be used for calling a function, this
1255 gives the offset of that register into the block returned by
1256 __builtin_apply_args. 0 indicates that the register is not
1257 used for calling a function. */
1258 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1260 /* Return the size required for the block returned by __builtin_apply_args,
1261 and initialize apply_args_mode. */
1264 apply_args_size (void)
1266 static int size = -1;
1269 enum machine_mode mode;
1271 /* The values computed by this function never change. */
1274 /* The first value is the incoming arg-pointer. */
1275 size = GET_MODE_SIZE (Pmode);
1277 /* The second value is the structure value address unless this is
1278 passed as an "invisible" first argument. */
1279 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1280 size += GET_MODE_SIZE (Pmode);
1282 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1283 if (FUNCTION_ARG_REGNO_P (regno))
1285 mode = reg_raw_mode[regno];
1287 gcc_assert (mode != VOIDmode);
1289 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1290 if (size % align != 0)
1291 size = CEIL (size, align) * align;
1292 apply_args_reg_offset[regno] = size;
1293 size += GET_MODE_SIZE (mode);
1294 apply_args_mode[regno] = mode;
1298 apply_args_mode[regno] = VOIDmode;
1299 apply_args_reg_offset[regno] = 0;
1305 /* Return the size required for the block returned by __builtin_apply,
1306 and initialize apply_result_mode. */
1309 apply_result_size (void)
1311 static int size = -1;
1313 enum machine_mode mode;
1315 /* The values computed by this function never change. */
1320 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1321 if (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;
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;
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. */
1351 result_vector (int savep, rtx result)
1353 int regno, size, align, nelts;
1354 enum machine_mode mode;
1356 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
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. */
1380 expand_builtin_apply_args_1 (void)
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),
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
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. */
1459 temp = expand_builtin_apply_args_1 ();
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
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);
1476 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1477 pop_topmost_sequence ();
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. */
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);
1508 /* Push a new argument block and copy the arguments. Do not allow
1509 the (potential) memcpy call below to interfere with our stack
1511 do_pending_stack_adjust ();
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);
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 (GET_CODE (argsize) == CONST_INT)
1536 dest = plus_constant (dest, -INTVAL (argsize));
1538 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
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. */
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);
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);
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 (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)));
1599 #ifdef HAVE_call_value
1600 if (HAVE_call_value)
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);
1626 /* Find the CALL insn we just emitted, and attach the register usage
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);
1637 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
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. */
1649 expand_builtin_return (rtx result)
1651 int size, align, regno;
1652 enum machine_mode mode;
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)));
1670 /* Restore the return value and note that each value is used. */
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);
1683 call_fusage = get_insns ();
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
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;
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. */
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. */
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;
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)
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];
1860 /* Like mathfn_built_in_1(), but always use the implicit array. */
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
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 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1883 /* If this built-in doesn't throw an exception, set errno directly. */
1884 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1886 #ifdef GEN_ERRNO_RTX
1887 rtx errno_rtx = GEN_ERRNO_RTX;
1890 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1892 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1898 /* Make sure the library call isn't expanded as a tail call. */
1899 CALL_EXPR_TAILCALL (exp) = 0;
1901 /* We can't set errno=EDOM directly; let the library call do it.
1902 Pop the arguments right away in case the call gets deleted. */
1904 expand_call (exp, target, 0);
1909 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1910 Return NULL_RTX if a normal call should be emitted rather than expanding
1911 the function in-line. EXP is the expression that is a call to the builtin
1912 function; if convenient, the result should be placed in TARGET.
1913 SUBTARGET may be used as the target for computing one of EXP's operands. */
1916 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1918 optab builtin_optab;
1919 rtx op0, insns, before_call;
1920 tree fndecl = get_callee_fndecl (exp);
1921 enum machine_mode mode;
1922 bool errno_set = false;
1925 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1928 arg = CALL_EXPR_ARG (exp, 0);
1930 switch (DECL_FUNCTION_CODE (fndecl))
1932 CASE_FLT_FN (BUILT_IN_SQRT):
1933 errno_set = ! tree_expr_nonnegative_p (arg);
1934 builtin_optab = sqrt_optab;
1936 CASE_FLT_FN (BUILT_IN_EXP):
1937 errno_set = true; builtin_optab = exp_optab; break;
1938 CASE_FLT_FN (BUILT_IN_EXP10):
1939 CASE_FLT_FN (BUILT_IN_POW10):
1940 errno_set = true; builtin_optab = exp10_optab; break;
1941 CASE_FLT_FN (BUILT_IN_EXP2):
1942 errno_set = true; builtin_optab = exp2_optab; break;
1943 CASE_FLT_FN (BUILT_IN_EXPM1):
1944 errno_set = true; builtin_optab = expm1_optab; break;
1945 CASE_FLT_FN (BUILT_IN_LOGB):
1946 errno_set = true; builtin_optab = logb_optab; break;
1947 CASE_FLT_FN (BUILT_IN_LOG):
1948 errno_set = true; builtin_optab = log_optab; break;
1949 CASE_FLT_FN (BUILT_IN_LOG10):
1950 errno_set = true; builtin_optab = log10_optab; break;
1951 CASE_FLT_FN (BUILT_IN_LOG2):
1952 errno_set = true; builtin_optab = log2_optab; break;
1953 CASE_FLT_FN (BUILT_IN_LOG1P):
1954 errno_set = true; builtin_optab = log1p_optab; break;
1955 CASE_FLT_FN (BUILT_IN_ASIN):
1956 builtin_optab = asin_optab; break;
1957 CASE_FLT_FN (BUILT_IN_ACOS):
1958 builtin_optab = acos_optab; break;
1959 CASE_FLT_FN (BUILT_IN_TAN):
1960 builtin_optab = tan_optab; break;
1961 CASE_FLT_FN (BUILT_IN_ATAN):
1962 builtin_optab = atan_optab; break;
1963 CASE_FLT_FN (BUILT_IN_FLOOR):
1964 builtin_optab = floor_optab; break;
1965 CASE_FLT_FN (BUILT_IN_CEIL):
1966 builtin_optab = ceil_optab; break;
1967 CASE_FLT_FN (BUILT_IN_TRUNC):
1968 builtin_optab = btrunc_optab; break;
1969 CASE_FLT_FN (BUILT_IN_ROUND):
1970 builtin_optab = round_optab; break;
1971 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1972 builtin_optab = nearbyint_optab;
1973 if (flag_trapping_math)
1975 /* Else fallthrough and expand as rint. */
1976 CASE_FLT_FN (BUILT_IN_RINT):
1977 builtin_optab = rint_optab; break;
1982 /* Make a suitable register to place result in. */
1983 mode = TYPE_MODE (TREE_TYPE (exp));
1985 if (! flag_errno_math || ! HONOR_NANS (mode))
1988 /* Before working hard, check whether the instruction is available. */
1989 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
1991 target = gen_reg_rtx (mode);
1993 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1994 need to expand the argument again. This way, we will not perform
1995 side-effects more the once. */
1996 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
1998 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2002 /* Compute into TARGET.
2003 Set TARGET to wherever the result comes back. */
2004 target = expand_unop (mode, builtin_optab, op0, target, 0);
2009 expand_errno_check (exp, target);
2011 /* Output the entire sequence. */
2012 insns = get_insns ();
2018 /* If we were unable to expand via the builtin, stop the sequence
2019 (without outputting the insns) and call to the library function
2020 with the stabilized argument list. */
2024 before_call = get_last_insn ();
2026 return expand_call (exp, target, target == const0_rtx);
2029 /* Expand a call to the builtin binary math functions (pow and atan2).
2030 Return NULL_RTX if a normal call should be emitted rather than expanding the
2031 function in-line. EXP is the expression that is a call to the builtin
2032 function; if convenient, the result should be placed in TARGET.
2033 SUBTARGET may be used as the target for computing one of EXP's
2037 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2039 optab builtin_optab;
2040 rtx op0, op1, insns;
2041 int op1_type = REAL_TYPE;
2042 tree fndecl = get_callee_fndecl (exp);
2044 enum machine_mode mode;
2045 bool errno_set = true;
2047 switch (DECL_FUNCTION_CODE (fndecl))
2049 CASE_FLT_FN (BUILT_IN_SCALBN):
2050 CASE_FLT_FN (BUILT_IN_SCALBLN):
2051 CASE_FLT_FN (BUILT_IN_LDEXP):
2052 op1_type = INTEGER_TYPE;
2057 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2060 arg0 = CALL_EXPR_ARG (exp, 0);
2061 arg1 = CALL_EXPR_ARG (exp, 1);
2063 switch (DECL_FUNCTION_CODE (fndecl))
2065 CASE_FLT_FN (BUILT_IN_POW):
2066 builtin_optab = pow_optab; break;
2067 CASE_FLT_FN (BUILT_IN_ATAN2):
2068 builtin_optab = atan2_optab; break;
2069 CASE_FLT_FN (BUILT_IN_SCALB):
2070 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2072 builtin_optab = scalb_optab; break;
2073 CASE_FLT_FN (BUILT_IN_SCALBN):
2074 CASE_FLT_FN (BUILT_IN_SCALBLN):
2075 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2077 /* Fall through... */
2078 CASE_FLT_FN (BUILT_IN_LDEXP):
2079 builtin_optab = ldexp_optab; break;
2080 CASE_FLT_FN (BUILT_IN_FMOD):
2081 builtin_optab = fmod_optab; break;
2082 CASE_FLT_FN (BUILT_IN_REMAINDER):
2083 CASE_FLT_FN (BUILT_IN_DREM):
2084 builtin_optab = remainder_optab; break;
2089 /* Make a suitable register to place result in. */
2090 mode = TYPE_MODE (TREE_TYPE (exp));
2092 /* Before working hard, check whether the instruction is available. */
2093 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2096 target = gen_reg_rtx (mode);
2098 if (! flag_errno_math || ! HONOR_NANS (mode))
2101 /* Always stabilize the argument list. */
2102 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2103 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2105 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2106 op1 = expand_normal (arg1);
2110 /* Compute into TARGET.
2111 Set TARGET to wherever the result comes back. */
2112 target = expand_binop (mode, builtin_optab, op0, op1,
2113 target, 0, OPTAB_DIRECT);
2115 /* If we were unable to expand via the builtin, stop the sequence
2116 (without outputting the insns) and call to the library function
2117 with the stabilized argument list. */
2121 return expand_call (exp, target, target == const0_rtx);
2125 expand_errno_check (exp, target);
2127 /* Output the entire sequence. */
2128 insns = get_insns ();
2135 /* Expand a call to the builtin sin and cos math functions.
2136 Return NULL_RTX if a normal call should be emitted rather than expanding the
2137 function in-line. EXP is the expression that is a call to the builtin
2138 function; if convenient, the result should be placed in TARGET.
2139 SUBTARGET may be used as the target for computing one of EXP's
2143 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2145 optab builtin_optab;
2147 tree fndecl = get_callee_fndecl (exp);
2148 enum machine_mode mode;
2151 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2154 arg = CALL_EXPR_ARG (exp, 0);
2156 switch (DECL_FUNCTION_CODE (fndecl))
2158 CASE_FLT_FN (BUILT_IN_SIN):
2159 CASE_FLT_FN (BUILT_IN_COS):
2160 builtin_optab = sincos_optab; break;
2165 /* Make a suitable register to place result in. */
2166 mode = TYPE_MODE (TREE_TYPE (exp));
2168 /* Check if sincos insn is available, otherwise fallback
2169 to sin or cos insn. */
2170 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2171 switch (DECL_FUNCTION_CODE (fndecl))
2173 CASE_FLT_FN (BUILT_IN_SIN):
2174 builtin_optab = sin_optab; break;
2175 CASE_FLT_FN (BUILT_IN_COS):
2176 builtin_optab = cos_optab; break;
2181 /* Before working hard, check whether the instruction is available. */
2182 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2184 target = gen_reg_rtx (mode);
2186 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2187 need to expand the argument again. This way, we will not perform
2188 side-effects more the once. */
2189 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2191 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2195 /* Compute into TARGET.
2196 Set TARGET to wherever the result comes back. */
2197 if (builtin_optab == sincos_optab)
2201 switch (DECL_FUNCTION_CODE (fndecl))
2203 CASE_FLT_FN (BUILT_IN_SIN):
2204 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2206 CASE_FLT_FN (BUILT_IN_COS):
2207 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2212 gcc_assert (result);
2216 target = expand_unop (mode, builtin_optab, op0, target, 0);
2221 /* Output the entire sequence. */
2222 insns = get_insns ();
2228 /* If we were unable to expand via the builtin, stop the sequence
2229 (without outputting the insns) and call to the library function
2230 with the stabilized argument list. */
2234 target = expand_call (exp, target, target == const0_rtx);
2239 /* Expand a call to one of the builtin math functions that operate on
2240 floating point argument and output an integer result (ilogb, isinf,
2242 Return 0 if a normal call should be emitted rather than expanding the
2243 function in-line. EXP is the expression that is a call to the builtin
2244 function; if convenient, the result should be placed in TARGET.
2245 SUBTARGET may be used as the target for computing one of EXP's operands. */
2248 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2250 optab builtin_optab = 0;
2251 enum insn_code icode = CODE_FOR_nothing;
2253 tree fndecl = get_callee_fndecl (exp);
2254 enum machine_mode mode;
2255 bool errno_set = false;
2258 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2261 arg = CALL_EXPR_ARG (exp, 0);
2263 switch (DECL_FUNCTION_CODE (fndecl))
2265 CASE_FLT_FN (BUILT_IN_ILOGB):
2266 errno_set = true; builtin_optab = ilogb_optab; break;
2267 CASE_FLT_FN (BUILT_IN_ISINF):
2268 builtin_optab = isinf_optab; break;
2269 case BUILT_IN_ISNORMAL:
2270 case BUILT_IN_ISFINITE:
2271 CASE_FLT_FN (BUILT_IN_FINITE):
2272 /* These builtins have no optabs (yet). */
2278 /* There's no easy way to detect the case we need to set EDOM. */
2279 if (flag_errno_math && errno_set)
2282 /* Optab mode depends on the mode of the input argument. */
2283 mode = TYPE_MODE (TREE_TYPE (arg));
2286 icode = optab_handler (builtin_optab, mode)->insn_code;
2288 /* Before working hard, check whether the instruction is available. */
2289 if (icode != CODE_FOR_nothing)
2291 /* Make a suitable register to place result in. */
2293 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2294 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2296 gcc_assert (insn_data[icode].operand[0].predicate
2297 (target, GET_MODE (target)));
2299 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2300 need to expand the argument again. This way, we will not perform
2301 side-effects more the once. */
2302 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2304 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2306 if (mode != GET_MODE (op0))
2307 op0 = convert_to_mode (mode, op0, 0);
2309 /* Compute into TARGET.
2310 Set TARGET to wherever the result comes back. */
2311 emit_unop_insn (icode, target, op0, UNKNOWN);
2315 /* If there is no optab, try generic code. */
2316 switch (DECL_FUNCTION_CODE (fndecl))
2320 CASE_FLT_FN (BUILT_IN_ISINF):
2322 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
2323 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
2324 tree const type = TREE_TYPE (arg);
2328 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2329 real_from_string (&r, buf);
2330 result = build_call_expr (isgr_fn, 2,
2331 fold_build1 (ABS_EXPR, type, arg),
2332 build_real (type, r));
2333 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2335 CASE_FLT_FN (BUILT_IN_FINITE):
2336 case BUILT_IN_ISFINITE:
2338 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
2339 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2340 tree const type = TREE_TYPE (arg);
2344 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2345 real_from_string (&r, buf);
2346 result = build_call_expr (isle_fn, 2,
2347 fold_build1 (ABS_EXPR, type, arg),
2348 build_real (type, r));
2349 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2351 case BUILT_IN_ISNORMAL:
2353 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
2354 islessequal(fabs(x),DBL_MAX). */
2355 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2356 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
2357 tree const type = TREE_TYPE (arg);
2358 REAL_VALUE_TYPE rmax, rmin;
2361 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2362 real_from_string (&rmax, buf);
2363 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
2364 real_from_string (&rmin, buf);
2365 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
2366 result = build_call_expr (isle_fn, 2, arg,
2367 build_real (type, rmax));
2368 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
2369 build_call_expr (isge_fn, 2, arg,
2370 build_real (type, rmin)));
2371 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2377 target = expand_call (exp, target, target == const0_rtx);
2382 /* Expand a call to the builtin sincos math function.
2383 Return NULL_RTX if a normal call should be emitted rather than expanding the
2384 function in-line. EXP is the expression that is a call to the builtin
2388 expand_builtin_sincos (tree exp)
2390 rtx op0, op1, op2, target1, target2;
2391 enum machine_mode mode;
2392 tree arg, sinp, cosp;
2395 if (!validate_arglist (exp, REAL_TYPE,
2396 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2399 arg = CALL_EXPR_ARG (exp, 0);
2400 sinp = CALL_EXPR_ARG (exp, 1);
2401 cosp = CALL_EXPR_ARG (exp, 2);
2403 /* Make a suitable register to place result in. */
2404 mode = TYPE_MODE (TREE_TYPE (arg));
2406 /* Check if sincos insn is available, otherwise emit the call. */
2407 if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing)
2410 target1 = gen_reg_rtx (mode);
2411 target2 = gen_reg_rtx (mode);
2413 op0 = expand_normal (arg);
2414 op1 = expand_normal (build_fold_indirect_ref (sinp));
2415 op2 = expand_normal (build_fold_indirect_ref (cosp));
2417 /* Compute into target1 and target2.
2418 Set TARGET to wherever the result comes back. */
2419 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2420 gcc_assert (result);
2422 /* Move target1 and target2 to the memory locations indicated
2424 emit_move_insn (op1, target1);
2425 emit_move_insn (op2, target2);
2430 /* Expand a call to the internal cexpi builtin to the sincos math function.
2431 EXP is the expression that is a call to the builtin function; if convenient,
2432 the result should be placed in TARGET. SUBTARGET may be used as the target
2433 for computing one of EXP's operands. */
2436 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2438 tree fndecl = get_callee_fndecl (exp);
2440 enum machine_mode mode;
2443 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2446 arg = CALL_EXPR_ARG (exp, 0);
2447 type = TREE_TYPE (arg);
2448 mode = TYPE_MODE (TREE_TYPE (arg));
2450 /* Try expanding via a sincos optab, fall back to emitting a libcall
2451 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2452 is only generated from sincos, cexp or if we have either of them. */
2453 if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing)
2455 op1 = gen_reg_rtx (mode);
2456 op2 = gen_reg_rtx (mode);
2458 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2460 /* Compute into op1 and op2. */
2461 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2463 else if (TARGET_HAS_SINCOS)
2465 tree call, fn = NULL_TREE;
2469 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2470 fn = built_in_decls[BUILT_IN_SINCOSF];
2471 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2472 fn = built_in_decls[BUILT_IN_SINCOS];
2473 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2474 fn = built_in_decls[BUILT_IN_SINCOSL];
2478 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2479 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2480 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2481 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2482 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2483 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2485 /* Make sure not to fold the sincos call again. */
2486 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2487 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2488 call, 3, arg, top1, top2));
2492 tree call, fn = NULL_TREE, narg;
2493 tree ctype = build_complex_type (type);
2495 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2496 fn = built_in_decls[BUILT_IN_CEXPF];
2497 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2498 fn = built_in_decls[BUILT_IN_CEXP];
2499 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2500 fn = built_in_decls[BUILT_IN_CEXPL];
2504 /* If we don't have a decl for cexp create one. This is the
2505 friendliest fallback if the user calls __builtin_cexpi
2506 without full target C99 function support. */
2507 if (fn == NULL_TREE)
2510 const char *name = NULL;
2512 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2514 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2516 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2519 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2520 fn = build_fn_decl (name, fntype);
2523 narg = fold_build2 (COMPLEX_EXPR, ctype,
2524 build_real (type, dconst0), arg);
2526 /* Make sure not to fold the cexp call again. */
2527 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2528 return expand_expr (build_call_nary (ctype, call, 1, narg),
2529 target, VOIDmode, EXPAND_NORMAL);
2532 /* Now build the proper return type. */
2533 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2534 make_tree (TREE_TYPE (arg), op2),
2535 make_tree (TREE_TYPE (arg), op1)),
2536 target, VOIDmode, EXPAND_NORMAL);
2539 /* Expand a call to one of the builtin rounding functions gcc defines
2540 as an extension (lfloor and lceil). As these are gcc extensions we
2541 do not need to worry about setting errno to EDOM.
2542 If expanding via optab fails, lower expression to (int)(floor(x)).
2543 EXP is the expression that is a call to the builtin function;
2544 if convenient, the result should be placed in TARGET. */
2547 expand_builtin_int_roundingfn (tree exp, rtx target)
2549 convert_optab builtin_optab;
2550 rtx op0, insns, tmp;
2551 tree fndecl = get_callee_fndecl (exp);
2552 enum built_in_function fallback_fn;
2553 tree fallback_fndecl;
2554 enum machine_mode mode;
2557 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2560 arg = CALL_EXPR_ARG (exp, 0);
2562 switch (DECL_FUNCTION_CODE (fndecl))
2564 CASE_FLT_FN (BUILT_IN_LCEIL):
2565 CASE_FLT_FN (BUILT_IN_LLCEIL):
2566 builtin_optab = lceil_optab;
2567 fallback_fn = BUILT_IN_CEIL;
2570 CASE_FLT_FN (BUILT_IN_LFLOOR):
2571 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2572 builtin_optab = lfloor_optab;
2573 fallback_fn = BUILT_IN_FLOOR;
2580 /* Make a suitable register to place result in. */
2581 mode = TYPE_MODE (TREE_TYPE (exp));
2583 target = gen_reg_rtx (mode);
2585 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2586 need to expand the argument again. This way, we will not perform
2587 side-effects more the once. */
2588 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2590 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2594 /* Compute into TARGET. */
2595 if (expand_sfix_optab (target, op0, builtin_optab))
2597 /* Output the entire sequence. */
2598 insns = get_insns ();
2604 /* If we were unable to expand via the builtin, stop the sequence
2605 (without outputting the insns). */
2608 /* Fall back to floating point rounding optab. */
2609 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2611 /* For non-C99 targets we may end up without a fallback fndecl here
2612 if the user called __builtin_lfloor directly. In this case emit
2613 a call to the floor/ceil variants nevertheless. This should result
2614 in the best user experience for not full C99 targets. */
2615 if (fallback_fndecl == NULL_TREE)
2618 const char *name = NULL;
2620 switch (DECL_FUNCTION_CODE (fndecl))
2622 case BUILT_IN_LCEIL:
2623 case BUILT_IN_LLCEIL:
2626 case BUILT_IN_LCEILF:
2627 case BUILT_IN_LLCEILF:
2630 case BUILT_IN_LCEILL:
2631 case BUILT_IN_LLCEILL:
2634 case BUILT_IN_LFLOOR:
2635 case BUILT_IN_LLFLOOR:
2638 case BUILT_IN_LFLOORF:
2639 case BUILT_IN_LLFLOORF:
2642 case BUILT_IN_LFLOORL:
2643 case BUILT_IN_LLFLOORL:
2650 fntype = build_function_type_list (TREE_TYPE (arg),
2651 TREE_TYPE (arg), NULL_TREE);
2652 fallback_fndecl = build_fn_decl (name, fntype);
2655 exp = build_call_expr (fallback_fndecl, 1, arg);
2657 tmp = expand_normal (exp);
2659 /* Truncate the result of floating point optab to integer
2660 via expand_fix (). */
2661 target = gen_reg_rtx (mode);
2662 expand_fix (target, tmp, 0);
2667 /* Expand a call to one of the builtin math functions doing integer
2669 Return 0 if a normal call should be emitted rather than expanding the
2670 function in-line. EXP is the expression that is a call to the builtin
2671 function; if convenient, the result should be placed in TARGET. */
2674 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2676 convert_optab builtin_optab;
2678 tree fndecl = get_callee_fndecl (exp);
2680 enum machine_mode mode;
2682 /* There's no easy way to detect the case we need to set EDOM. */
2683 if (flag_errno_math)
2686 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2689 arg = CALL_EXPR_ARG (exp, 0);
2691 switch (DECL_FUNCTION_CODE (fndecl))
2693 CASE_FLT_FN (BUILT_IN_LRINT):
2694 CASE_FLT_FN (BUILT_IN_LLRINT):
2695 builtin_optab = lrint_optab; break;
2696 CASE_FLT_FN (BUILT_IN_LROUND):
2697 CASE_FLT_FN (BUILT_IN_LLROUND):
2698 builtin_optab = lround_optab; break;
2703 /* Make a suitable register to place result in. */
2704 mode = TYPE_MODE (TREE_TYPE (exp));
2706 target = gen_reg_rtx (mode);
2708 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2709 need to expand the argument again. This way, we will not perform
2710 side-effects more the once. */
2711 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2713 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2717 if (expand_sfix_optab (target, op0, builtin_optab))
2719 /* Output the entire sequence. */
2720 insns = get_insns ();
2726 /* If we were unable to expand via the builtin, stop the sequence
2727 (without outputting the insns) and call to the library function
2728 with the stabilized argument list. */
2731 target = expand_call (exp, target, target == const0_rtx);
2736 /* To evaluate powi(x,n), the floating point value x raised to the
2737 constant integer exponent n, we use a hybrid algorithm that
2738 combines the "window method" with look-up tables. For an
2739 introduction to exponentiation algorithms and "addition chains",
2740 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2741 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2742 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2743 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2745 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2746 multiplications to inline before calling the system library's pow
2747 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2748 so this default never requires calling pow, powf or powl. */
2750 #ifndef POWI_MAX_MULTS
2751 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2754 /* The size of the "optimal power tree" lookup table. All
2755 exponents less than this value are simply looked up in the
2756 powi_table below. This threshold is also used to size the
2757 cache of pseudo registers that hold intermediate results. */
2758 #define POWI_TABLE_SIZE 256
2760 /* The size, in bits of the window, used in the "window method"
2761 exponentiation algorithm. This is equivalent to a radix of
2762 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2763 #define POWI_WINDOW_SIZE 3
2765 /* The following table is an efficient representation of an
2766 "optimal power tree". For each value, i, the corresponding
2767 value, j, in the table states than an optimal evaluation
2768 sequence for calculating pow(x,i) can be found by evaluating
2769 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2770 100 integers is given in Knuth's "Seminumerical algorithms". */
2772 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2774 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2775 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2776 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2777 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2778 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2779 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2780 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2781 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2782 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2783 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2784 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2785 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2786 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2787 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2788 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2789 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2790 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2791 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2792 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2793 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2794 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2795 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2796 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2797 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2798 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2799 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2800 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2801 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2802 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2803 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2804 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2805 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2809 /* Return the number of multiplications required to calculate
2810 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2811 subroutine of powi_cost. CACHE is an array indicating
2812 which exponents have already been calculated. */
2815 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2817 /* If we've already calculated this exponent, then this evaluation
2818 doesn't require any additional multiplications. */
2823 return powi_lookup_cost (n - powi_table[n], cache)
2824 + powi_lookup_cost (powi_table[n], cache) + 1;
2827 /* Return the number of multiplications required to calculate
2828 powi(x,n) for an arbitrary x, given the exponent N. This
2829 function needs to be kept in sync with expand_powi below. */
2832 powi_cost (HOST_WIDE_INT n)
2834 bool cache[POWI_TABLE_SIZE];
2835 unsigned HOST_WIDE_INT digit;
2836 unsigned HOST_WIDE_INT val;
2842 /* Ignore the reciprocal when calculating the cost. */
2843 val = (n < 0) ? -n : n;
2845 /* Initialize the exponent cache. */
2846 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2851 while (val >= POWI_TABLE_SIZE)
2855 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2856 result += powi_lookup_cost (digit, cache)
2857 + POWI_WINDOW_SIZE + 1;
2858 val >>= POWI_WINDOW_SIZE;
2867 return result + powi_lookup_cost (val, cache);
2870 /* Recursive subroutine of expand_powi. This function takes the array,
2871 CACHE, of already calculated exponents and an exponent N and returns
2872 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2875 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2877 unsigned HOST_WIDE_INT digit;
2881 if (n < POWI_TABLE_SIZE)
2886 target = gen_reg_rtx (mode);
2889 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2890 op1 = expand_powi_1 (mode, powi_table[n], cache);
2894 target = gen_reg_rtx (mode);
2895 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2896 op0 = expand_powi_1 (mode, n - digit, cache);
2897 op1 = expand_powi_1 (mode, digit, cache);
2901 target = gen_reg_rtx (mode);
2902 op0 = expand_powi_1 (mode, n >> 1, cache);
2906 result = expand_mult (mode, op0, op1, target, 0);
2907 if (result != target)
2908 emit_move_insn (target, result);
2912 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2913 floating point operand in mode MODE, and N is the exponent. This
2914 function needs to be kept in sync with powi_cost above. */
2917 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2919 unsigned HOST_WIDE_INT val;
2920 rtx cache[POWI_TABLE_SIZE];
2924 return CONST1_RTX (mode);
2926 val = (n < 0) ? -n : n;
2928 memset (cache, 0, sizeof (cache));
2931 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2933 /* If the original exponent was negative, reciprocate the result. */
2935 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2936 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2941 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2942 a normal call should be emitted rather than expanding the function
2943 in-line. EXP is the expression that is a call to the builtin
2944 function; if convenient, the result should be placed in TARGET. */
2947 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2951 tree type = TREE_TYPE (exp);
2952 REAL_VALUE_TYPE cint, c, c2;
2955 enum machine_mode mode = TYPE_MODE (type);
2957 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2960 arg0 = CALL_EXPR_ARG (exp, 0);
2961 arg1 = CALL_EXPR_ARG (exp, 1);
2963 if (TREE_CODE (arg1) != REAL_CST
2964 || TREE_OVERFLOW (arg1))
2965 return expand_builtin_mathfn_2 (exp, target, subtarget);
2967 /* Handle constant exponents. */
2969 /* For integer valued exponents we can expand to an optimal multiplication
2970 sequence using expand_powi. */
2971 c = TREE_REAL_CST (arg1);
2972 n = real_to_integer (&c);
2973 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2974 if (real_identical (&c, &cint)
2975 && ((n >= -1 && n <= 2)
2976 || (flag_unsafe_math_optimizations
2977 && optimize_insn_for_speed_p ()
2978 && powi_cost (n) <= POWI_MAX_MULTS)))
2980 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2983 op = force_reg (mode, op);
2984 op = expand_powi (op, mode, n);
2989 narg0 = builtin_save_expr (arg0);
2991 /* If the exponent is not integer valued, check if it is half of an integer.
2992 In this case we can expand to sqrt (x) * x**(n/2). */
2993 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2994 if (fn != NULL_TREE)
2996 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2997 n = real_to_integer (&c2);
2998 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2999 if (real_identical (&c2, &cint)
3000 && ((flag_unsafe_math_optimizations
3001 && optimize_insn_for_speed_p ()
3002 && powi_cost (n/2) <= POWI_MAX_MULTS)
3005 tree call_expr = build_call_expr (fn, 1, narg0);
3006 /* Use expand_expr in case the newly built call expression
3007 was folded to a non-call. */
3008 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3011 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3012 op2 = force_reg (mode, op2);
3013 op2 = expand_powi (op2, mode, abs (n / 2));
3014 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3015 0, OPTAB_LIB_WIDEN);
3016 /* If the original exponent was negative, reciprocate the
3019 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3020 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3026 /* Try if the exponent is a third of an integer. In this case
3027 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3028 different from pow (x, 1./3.) due to rounding and behavior
3029 with negative x we need to constrain this transformation to
3030 unsafe math and positive x or finite math. */
3031 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3033 && flag_unsafe_math_optimizations
3034 && (tree_expr_nonnegative_p (arg0)
3035 || !HONOR_NANS (mode)))
3037 REAL_VALUE_TYPE dconst3;
3038 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3039 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3040 real_round (&c2, mode, &c2);
3041 n = real_to_integer (&c2);
3042 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3043 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3044 real_convert (&c2, mode, &c2);
3045 if (real_identical (&c2, &c)
3046 && ((optimize_insn_for_speed_p ()
3047 && powi_cost (n/3) <= POWI_MAX_MULTS)
3050 tree call_expr = build_call_expr (fn, 1,narg0);
3051 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3052 if (abs (n) % 3 == 2)
3053 op = expand_simple_binop (mode, MULT, op, op, op,
3054 0, OPTAB_LIB_WIDEN);
3057 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3058 op2 = force_reg (mode, op2);
3059 op2 = expand_powi (op2, mode, abs (n / 3));
3060 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3061 0, OPTAB_LIB_WIDEN);
3062 /* If the original exponent was negative, reciprocate the
3065 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3066 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3072 /* Fall back to optab expansion. */
3073 return expand_builtin_mathfn_2 (exp, target, subtarget);
3076 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3077 a normal call should be emitted rather than expanding the function
3078 in-line. EXP is the expression that is a call to the builtin
3079 function; if convenient, the result should be placed in TARGET. */
3082 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3086 enum machine_mode mode;
3087 enum machine_mode mode2;
3089 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3092 arg0 = CALL_EXPR_ARG (exp, 0);
3093 arg1 = CALL_EXPR_ARG (exp, 1);
3094 mode = TYPE_MODE (TREE_TYPE (exp));
3096 /* Handle constant power. */
3098 if (TREE_CODE (arg1) == INTEGER_CST
3099 && !TREE_OVERFLOW (arg1))
3101 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3103 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3104 Otherwise, check the number of multiplications required. */
3105 if ((TREE_INT_CST_HIGH (arg1) == 0
3106 || TREE_INT_CST_HIGH (arg1) == -1)
3107 && ((n >= -1 && n <= 2)
3108 || (optimize_insn_for_speed_p ()
3109 && powi_cost (n) <= POWI_MAX_MULTS)))
3111 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3112 op0 = force_reg (mode, op0);
3113 return expand_powi (op0, mode, n);
3117 /* Emit a libcall to libgcc. */
3119 /* Mode of the 2nd argument must match that of an int. */
3120 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3122 if (target == NULL_RTX)
3123 target = gen_reg_rtx (mode);
3125 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3126 if (GET_MODE (op0) != mode)
3127 op0 = convert_to_mode (mode, op0, 0);
3128 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3129 if (GET_MODE (op1) != mode2)
3130 op1 = convert_to_mode (mode2, op1, 0);
3132 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3133 target, LCT_CONST, mode, 2,
3134 op0, mode, op1, mode2);
3139 /* Expand expression EXP which is a call to the strlen builtin. Return
3140 NULL_RTX if we failed the caller should emit a normal call, otherwise
3141 try to get the result in TARGET, if convenient. */
3144 expand_builtin_strlen (tree exp, rtx target,
3145 enum machine_mode target_mode)
3147 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3153 tree src = CALL_EXPR_ARG (exp, 0);
3154 rtx result, src_reg, char_rtx, before_strlen;
3155 enum machine_mode insn_mode = target_mode, char_mode;
3156 enum insn_code icode = CODE_FOR_nothing;
3159 /* If the length can be computed at compile-time, return it. */
3160 len = c_strlen (src, 0);
3162 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3164 /* If the length can be computed at compile-time and is constant
3165 integer, but there are side-effects in src, evaluate
3166 src for side-effects, then return len.
3167 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3168 can be optimized into: i++; x = 3; */
3169 len = c_strlen (src, 1);
3170 if (len && TREE_CODE (len) == INTEGER_CST)
3172 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3173 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3176 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3178 /* If SRC is not a pointer type, don't do this operation inline. */
3182 /* Bail out if we can't compute strlen in the right mode. */
3183 while (insn_mode != VOIDmode)
3185 icode = optab_handler (strlen_optab, insn_mode)->insn_code;
3186 if (icode != CODE_FOR_nothing)
3189 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3191 if (insn_mode == VOIDmode)
3194 /* Make a place to write the result of the instruction. */
3198 && GET_MODE (result) == insn_mode
3199 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3200 result = gen_reg_rtx (insn_mode);
3202 /* Make a place to hold the source address. We will not expand
3203 the actual source until we are sure that the expansion will
3204 not fail -- there are trees that cannot be expanded twice. */
3205 src_reg = gen_reg_rtx (Pmode);
3207 /* Mark the beginning of the strlen sequence so we can emit the
3208 source operand later. */
3209 before_strlen = get_last_insn ();
3211 char_rtx = const0_rtx;
3212 char_mode = insn_data[(int) icode].operand[2].mode;
3213 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3215 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3217 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3218 char_rtx, GEN_INT (align));
3223 /* Now that we are assured of success, expand the source. */
3225 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3227 emit_move_insn (src_reg, pat);
3232 emit_insn_after (pat, before_strlen);
3234 emit_insn_before (pat, get_insns ());
3236 /* Return the value in the proper mode for this function. */
3237 if (GET_MODE (result) == target_mode)
3239 else if (target != 0)
3240 convert_move (target, result, 0);
3242 target = convert_to_mode (target_mode, result, 0);
3248 /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the
3249 caller should emit a normal call, otherwise try to get the result
3250 in TARGET, if convenient (and in mode MODE if that's convenient). */
3253 expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode)
3255 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3257 tree type = TREE_TYPE (exp);
3258 tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0),
3259 CALL_EXPR_ARG (exp, 1), type);
3261 return expand_expr (result, target, mode, EXPAND_NORMAL);
3266 /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the
3267 caller should emit a normal call, otherwise try to get the result
3268 in TARGET, if convenient (and in mode MODE if that's convenient). */
3271 expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode)
3273 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3275 tree type = TREE_TYPE (exp);
3276 tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0),
3277 CALL_EXPR_ARG (exp, 1), type);
3279 return expand_expr (result, target, mode, EXPAND_NORMAL);
3281 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3286 /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the
3287 caller should emit a normal call, otherwise try to get the result
3288 in TARGET, if convenient (and in mode MODE if that's convenient). */
3291 expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode)
3293 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3295 tree type = TREE_TYPE (exp);
3296 tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0),
3297 CALL_EXPR_ARG (exp, 1), type);
3299 return expand_expr (result, target, mode, EXPAND_NORMAL);
3304 /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the
3305 caller should emit a normal call, otherwise try to get the result
3306 in TARGET, if convenient (and in mode MODE if that's convenient). */
3309 expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode)
3311 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3313 tree type = TREE_TYPE (exp);
3314 tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0),
3315 CALL_EXPR_ARG (exp, 1), type);
3317 return expand_expr (result, target, mode, EXPAND_NORMAL);
3322 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3323 bytes from constant string DATA + OFFSET and return it as target
3327 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3328 enum machine_mode mode)
3330 const char *str = (const char *) data;
3332 gcc_assert (offset >= 0
3333 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3334 <= strlen (str) + 1));
3336 return c_readstr (str + offset, mode);
3339 /* Expand a call EXP to the memcpy builtin.
3340 Return NULL_RTX if we failed, the caller should emit a normal call,
3341 otherwise try to get the result in TARGET, if convenient (and in
3342 mode MODE if that's convenient). */
3345 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3347 tree fndecl = get_callee_fndecl (exp);
3349 if (!validate_arglist (exp,
3350 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3354 tree dest = CALL_EXPR_ARG (exp, 0);
3355 tree src = CALL_EXPR_ARG (exp, 1);
3356 tree len = CALL_EXPR_ARG (exp, 2);
3357 const char *src_str;
3358 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3359 unsigned int dest_align
3360 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3361 rtx dest_mem, src_mem, dest_addr, len_rtx;
3362 tree result = fold_builtin_memory_op (dest, src, len,
3363 TREE_TYPE (TREE_TYPE (fndecl)),
3365 HOST_WIDE_INT expected_size = -1;
3366 unsigned int expected_align = 0;
3367 tree_ann_common_t ann;
3371 while (TREE_CODE (result) == COMPOUND_EXPR)
3373 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3375 result = TREE_OPERAND (result, 1);
3377 return expand_expr (result, target, mode, EXPAND_NORMAL);
3380 /* If DEST is not a pointer type, call the normal function. */
3381 if (dest_align == 0)
3384 /* If either SRC is not a pointer type, don't do this
3385 operation in-line. */
3389 ann = tree_common_ann (exp);
3391 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3393 if (expected_align < dest_align)
3394 expected_align = dest_align;
3395 dest_mem = get_memory_rtx (dest, len);
3396 set_mem_align (dest_mem, dest_align);
3397 len_rtx = expand_normal (len);
3398 src_str = c_getstr (src);
3400 /* If SRC is a string constant and block move would be done
3401 by pieces, we can avoid loading the string from memory
3402 and only stored the computed constants. */
3404 && GET_CODE (len_rtx) == CONST_INT
3405 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3406 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3407 CONST_CAST (char *, src_str),
3410 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3411 builtin_memcpy_read_str,
3412 CONST_CAST (char *, src_str),
3413 dest_align, false, 0);
3414 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3415 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3419 src_mem = get_memory_rtx (src, len);
3420 set_mem_align (src_mem, src_align);
3422 /* Copy word part most expediently. */
3423 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3424 CALL_EXPR_TAILCALL (exp)
3425 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3426 expected_align, expected_size);
3430 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3431 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3437 /* Expand a call EXP to the mempcpy builtin.
3438 Return NULL_RTX if we failed; the caller should emit a normal call,
3439 otherwise try to get the result in TARGET, if convenient (and in
3440 mode MODE if that's convenient). If ENDP is 0 return the
3441 destination pointer, if ENDP is 1 return the end pointer ala
3442 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3446 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3448 if (!validate_arglist (exp,
3449 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3453 tree dest = CALL_EXPR_ARG (exp, 0);
3454 tree src = CALL_EXPR_ARG (exp, 1);
3455 tree len = CALL_EXPR_ARG (exp, 2);
3456 return expand_builtin_mempcpy_args (dest, src, len,
3458 target, mode, /*endp=*/ 1);
3462 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3463 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3464 so that this can also be called without constructing an actual CALL_EXPR.
3465 TYPE is the return type of the call. The other arguments and return value
3466 are the same as for expand_builtin_mempcpy. */
3469 expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
3470 rtx target, enum machine_mode mode, int endp)
3472 /* If return value is ignored, transform mempcpy into memcpy. */
3473 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3475 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3476 tree result = build_call_expr (fn, 3, dest, src, len);
3478 while (TREE_CODE (result) == COMPOUND_EXPR)
3480 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3482 result = TREE_OPERAND (result, 1);
3484 return expand_expr (result, target, mode, EXPAND_NORMAL);
3488 const char *src_str;
3489 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3490 unsigned int dest_align
3491 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3492 rtx dest_mem, src_mem, len_rtx;
3493 tree result = fold_builtin_memory_op (dest, src, len, type, false, endp);
3497 while (TREE_CODE (result) == COMPOUND_EXPR)
3499 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3501 result = TREE_OPERAND (result, 1);
3503 return expand_expr (result, target, mode, EXPAND_NORMAL);
3506 /* If either SRC or DEST is not a pointer type, don't do this
3507 operation in-line. */
3508 if (dest_align == 0 || src_align == 0)
3511 /* If LEN is not constant, call the normal function. */
3512 if (! host_integerp (len, 1))
3515 len_rtx = expand_normal (len);
3516 src_str = c_getstr (src);
3518 /* If SRC is a string constant and block move would be done
3519 by pieces, we can avoid loading the string from memory
3520 and only stored the computed constants. */
3522 && GET_CODE (len_rtx) == CONST_INT
3523 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3524 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3525 CONST_CAST (char *, src_str),
3528 dest_mem = get_memory_rtx (dest, len);
3529 set_mem_align (dest_mem, dest_align);
3530 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3531 builtin_memcpy_read_str,
3532 CONST_CAST (char *, src_str),
3533 dest_align, false, endp);
3534 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3535 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3539 if (GET_CODE (len_rtx) == CONST_INT
3540 && can_move_by_pieces (INTVAL (len_rtx),
3541 MIN (dest_align, src_align)))
3543 dest_mem = get_memory_rtx (dest, len);
3544 set_mem_align (dest_mem, dest_align);
3545 src_mem = get_memory_rtx (src, len);
3546 set_mem_align (src_mem, src_align);
3547 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3548 MIN (dest_align, src_align), endp);
3549 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3550 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3558 /* Expand expression EXP, which is a call to the memmove builtin. Return
3559 NULL_RTX if we failed; the caller should emit a normal call. */
3562 expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore)
3564 if (!validate_arglist (exp,
3565 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3569 tree dest = CALL_EXPR_ARG (exp, 0);
3570 tree src = CALL_EXPR_ARG (exp, 1);
3571 tree len = CALL_EXPR_ARG (exp, 2);
3572 return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp),
3573 target, mode, ignore);
3577 /* Helper function to do the actual work for expand_builtin_memmove. The
3578 arguments to the builtin_memmove call DEST, SRC, and LEN are broken out
3579 so that this can also be called without constructing an actual CALL_EXPR.
3580 TYPE is the return type of the call. The other arguments and return value
3581 are the same as for expand_builtin_memmove. */
3584 expand_builtin_memmove_args (tree dest, tree src, tree len,
3585 tree type, rtx target, enum machine_mode mode,
3588 tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3);
3592 STRIP_TYPE_NOPS (result);
3593 while (TREE_CODE (result) == COMPOUND_EXPR)
3595 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3597 result = TREE_OPERAND (result, 1);
3599 return expand_expr (result, target, mode, EXPAND_NORMAL);
3602 /* Otherwise, call the normal function. */
3606 /* Expand expression EXP, which is a call to the bcopy builtin. Return
3607 NULL_RTX if we failed the caller should emit a normal call. */
3610 expand_builtin_bcopy (tree exp, int ignore)
3612 tree type = TREE_TYPE (exp);
3613 tree src, dest, size;
3615 if (!validate_arglist (exp,
3616 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3619 src = CALL_EXPR_ARG (exp, 0);
3620 dest = CALL_EXPR_ARG (exp, 1);
3621 size = CALL_EXPR_ARG (exp, 2);
3623 /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z).
3624 This is done this way so that if it isn't expanded inline, we fall
3625 back to calling bcopy instead of memmove. */
3626 return expand_builtin_memmove_args (dest, src,
3627 fold_convert (sizetype, size),
3628 type, const0_rtx, VOIDmode,
3633 # define HAVE_movstr 0
3634 # define CODE_FOR_movstr CODE_FOR_nothing
3637 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3638 we failed, the caller should emit a normal call, otherwise try to
3639 get the result in TARGET, if convenient. If ENDP is 0 return the
3640 destination pointer, if ENDP is 1 return the end pointer ala
3641 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3645 expand_movstr (tree dest, tree src, rtx target, int endp)
3651 const struct insn_data * data;
3656 dest_mem = get_memory_rtx (dest, NULL);
3657 src_mem = get_memory_rtx (src, NULL);
3660 target = force_reg (Pmode, XEXP (dest_mem, 0));
3661 dest_mem = replace_equiv_address (dest_mem, target);
3662 end = gen_reg_rtx (Pmode);
3666 if (target == 0 || target == const0_rtx)
3668 end = gen_reg_rtx (Pmode);
3676 data = insn_data + CODE_FOR_movstr;
3678 if (data->operand[0].mode != VOIDmode)
3679 end = gen_lowpart (data->operand[0].mode, end);
3681 insn = data->genfun (end, dest_mem, src_mem);
3687 /* movstr is supposed to set end to the address of the NUL
3688 terminator. If the caller requested a mempcpy-like return value,
3690 if (endp == 1 && target != const0_rtx)
3692 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3693 emit_move_insn (target, force_operand (tem, NULL_RTX));
3699 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3700 NULL_RTX if we failed the caller should emit a normal call, otherwise
3701 try to get the result in TARGET, if convenient (and in mode MODE if that's
3705 expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode)
3707 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3709 tree dest = CALL_EXPR_ARG (exp, 0);
3710 tree src = CALL_EXPR_ARG (exp, 1);
3711 return expand_builtin_strcpy_args (fndecl, dest, src, target, mode);
3716 /* Helper function to do the actual work for expand_builtin_strcpy. The
3717 arguments to the builtin_strcpy call DEST and SRC are broken out
3718 so that this can also be called without constructing an actual CALL_EXPR.
3719 The other arguments and return value are the same as for
3720 expand_builtin_strcpy. */
3723 expand_builtin_strcpy_args (tree fndecl, tree dest, tree src,
3724 rtx target, enum machine_mode mode)
3726 tree result = fold_builtin_strcpy (fndecl, dest, src, 0);
3728 return expand_expr (result, target, mode, EXPAND_NORMAL);
3729 return expand_movstr (dest, src, target, /*endp=*/0);
3733 /* Expand a call EXP to the stpcpy builtin.
3734 Return NULL_RTX if we failed the caller should emit a normal call,
3735 otherwise try to get the result in TARGET, if convenient (and in
3736 mode MODE if that's convenient). */
3739 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3743 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3746 dst = CALL_EXPR_ARG (exp, 0);
3747 src = CALL_EXPR_ARG (exp, 1);
3749 /* If return value is ignored, transform stpcpy into strcpy. */
3750 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3752 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3753 tree result = build_call_expr (fn, 2, dst, src);
3755 STRIP_NOPS (result);
3756 while (TREE_CODE (result) == COMPOUND_EXPR)
3758 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3760 result = TREE_OPERAND (result, 1);
3762 return expand_expr (result, target, mode, EXPAND_NORMAL);
3769 /* Ensure we get an actual string whose length can be evaluated at
3770 compile-time, not an expression containing a string. This is
3771 because the latter will potentially produce pessimized code
3772 when used to produce the return value. */
3773 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3774 return expand_movstr (dst, src, target, /*endp=*/2);
3776 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3777 ret = expand_builtin_mempcpy_args (dst, src, lenp1, TREE_TYPE (exp),
3778 target, mode, /*endp=*/2);
3783 if (TREE_CODE (len) == INTEGER_CST)
3785 rtx len_rtx = expand_normal (len);
3787 if (GET_CODE (len_rtx) == CONST_INT)
3789 ret = expand_builtin_strcpy_args (get_callee_fndecl (exp),
3790 dst, src, target, mode);
3796 if (mode != VOIDmode)
3797 target = gen_reg_rtx (mode);
3799 target = gen_reg_rtx (GET_MODE (ret));
3801 if (GET_MODE (target) != GET_MODE (ret))
3802 ret = gen_lowpart (GET_MODE (target), ret);
3804 ret = plus_constant (ret, INTVAL (len_rtx));
3805 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3813 return expand_movstr (dst, src, target, /*endp=*/2);
3817 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3818 bytes from constant string DATA + OFFSET and return it as target
3822 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3823 enum machine_mode mode)
3825 const char *str = (const char *) data;
3827 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3830 return c_readstr (str + offset, mode);
3833 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3834 NULL_RTX if we failed the caller should emit a normal call. */
3837 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3839 tree fndecl = get_callee_fndecl (exp);
3841 if (validate_arglist (exp,
3842 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3844 tree dest = CALL_EXPR_ARG (exp, 0);
3845 tree src = CALL_EXPR_ARG (exp, 1);
3846 tree len = CALL_EXPR_ARG (exp, 2);
3847 tree slen = c_strlen (src, 1);
3848 tree result = fold_builtin_strncpy (fndecl, dest, src, len, slen);
3852 while (TREE_CODE (result) == COMPOUND_EXPR)
3854 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3856 result = TREE_OPERAND (result, 1);
3858 return expand_expr (result, target, mode, EXPAND_NORMAL);
3861 /* We must be passed a constant len and src parameter. */
3862 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3865 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3867 /* We're required to pad with trailing zeros if the requested
3868 len is greater than strlen(s2)+1. In that case try to
3869 use store_by_pieces, if it fails, punt. */
3870 if (tree_int_cst_lt (slen, len))
3872 unsigned int dest_align
3873 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3874 const char *p = c_getstr (src);
3877 if (!p || dest_align == 0 || !host_integerp (len, 1)
3878 || !can_store_by_pieces (tree_low_cst (len, 1),
3879 builtin_strncpy_read_str,
3880 CONST_CAST (char *, p),
3884 dest_mem = get_memory_rtx (dest, len);
3885 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3886 builtin_strncpy_read_str,
3887 CONST_CAST (char *, p), dest_align, false, 0);
3888 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3889 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3896 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3897 bytes from constant string DATA + OFFSET and return it as target
3901 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3902 enum machine_mode mode)
3904 const char *c = (const char *) data;
3905 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3907 memset (p, *c, GET_MODE_SIZE (mode));
3909 return c_readstr (p, mode);
3912 /* Callback routine for store_by_pieces. Return the RTL of a register
3913 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3914 char value given in the RTL register data. For example, if mode is
3915 4 bytes wide, return the RTL for 0x01010101*data. */
3918 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3919 enum machine_mode mode)
3925 size = GET_MODE_SIZE (mode);
3929 p = XALLOCAVEC (char, size);
3930 memset (p, 1, size);
3931 coeff = c_readstr (p, mode);
3933 target = convert_to_mode (mode, (rtx) data, 1);
3934 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3935 return force_reg (mode, target);
3938 /* Expand expression EXP, which is a call to the memset builtin. Return
3939 NULL_RTX if we failed the caller should emit a normal call, otherwise
3940 try to get the result in TARGET, if convenient (and in mode MODE if that's
3944 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3946 if (!validate_arglist (exp,
3947 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3951 tree dest = CALL_EXPR_ARG (exp, 0);
3952 tree val = CALL_EXPR_ARG (exp, 1);
3953 tree len = CALL_EXPR_ARG (exp, 2);
3954 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3958 /* Helper function to do the actual work for expand_builtin_memset. The
3959 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3960 so that this can also be called without constructing an actual CALL_EXPR.
3961 The other arguments and return value are the same as for
3962 expand_builtin_memset. */
3965 expand_builtin_memset_args (tree dest, tree val, tree len,
3966 rtx target, enum machine_mode mode, tree orig_exp)
3969 enum built_in_function fcode;
3971 unsigned int dest_align;
3972 rtx dest_mem, dest_addr, len_rtx;
3973 HOST_WIDE_INT expected_size = -1;
3974 unsigned int expected_align = 0;
3975 tree_ann_common_t ann;
3977 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3979 /* If DEST is not a pointer type, don't do this operation in-line. */
3980 if (dest_align == 0)
3983 ann = tree_common_ann (orig_exp);
3985 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3987 if (expected_align < dest_align)
3988 expected_align = dest_align;
3990 /* If the LEN parameter is zero, return DEST. */
3991 if (integer_zerop (len))
3993 /* Evaluate and ignore VAL in case it has side-effects. */
3994 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3995 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3998 /* Stabilize the arguments in case we fail. */
3999 dest = builtin_save_expr (dest);
4000 val = builtin_save_expr (val);
4001 len = builtin_save_expr (len);
4003 len_rtx = expand_normal (len);
4004 dest_mem = get_memory_rtx (dest, len);
4006 if (TREE_CODE (val) != INTEGER_CST)
4010 val_rtx = expand_normal (val);
4011 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
4014 /* Assume that we can memset by pieces if we can store
4015 * the coefficients by pieces (in the required modes).
4016 * We can't pass builtin_memset_gen_str as that emits RTL. */
4018 if (host_integerp (len, 1)
4019 && can_store_by_pieces (tree_low_cst (len, 1),
4020 builtin_memset_read_str, &c, dest_align,
4023 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
4025 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4026 builtin_memset_gen_str, val_rtx, dest_align,
4029 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4030 dest_align, expected_align,
4034 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4035 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4039 if (target_char_cast (val, &c))
4044 if (host_integerp (len, 1)
4045 && can_store_by_pieces (tree_low_cst (len, 1),
4046 builtin_memset_read_str, &c, dest_align,
4048 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4049 builtin_memset_read_str, &c, dest_align, true, 0);
4050 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
4051 dest_align, expected_align,
4055 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4056 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4060 set_mem_align (dest_mem, dest_align);
4061 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4062 CALL_EXPR_TAILCALL (orig_exp)
4063 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4064 expected_align, expected_size);
4068 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4069 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4075 fndecl = get_callee_fndecl (orig_exp);
4076 fcode = DECL_FUNCTION_CODE (fndecl);
4077 if (fcode == BUILT_IN_MEMSET)
4078 fn = build_call_expr (fndecl, 3, dest, val, len);
4079 else if (fcode == BUILT_IN_BZERO)
4080 fn = build_call_expr (fndecl, 2, dest, len);
4083 if (TREE_CODE (fn) == CALL_EXPR)
4084 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4085 return expand_call (fn, target, target == const0_rtx);
4088 /* Expand expression EXP, which is a call to the bzero builtin. Return
4089 NULL_RTX if we failed the caller should emit a normal call. */
4092 expand_builtin_bzero (tree exp)
4096 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4099 dest = CALL_EXPR_ARG (exp, 0);
4100 size = CALL_EXPR_ARG (exp, 1);
4102 /* New argument list transforming bzero(ptr x, int y) to
4103 memset(ptr x, int 0, size_t y). This is done this way
4104 so that if it isn't expanded inline, we fallback to
4105 calling bzero instead of memset. */
4107 return expand_builtin_memset_args (dest, integer_zero_node,
4108 fold_convert (sizetype, size),
4109 const0_rtx, VOIDmode, exp);
4112 /* Expand a call to the memchr builtin. Return NULL_RTX if we failed the
4113 caller should emit a normal call, otherwise try to get the result
4114 in TARGET, if convenient (and in mode MODE if that's convenient). */
4117 expand_builtin_memchr (tree exp, rtx target, enum machine_mode mode)
4119 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE,
4120 INTEGER_TYPE, VOID_TYPE))
4122 tree type = TREE_TYPE (exp);
4123 tree result = fold_builtin_memchr (CALL_EXPR_ARG (exp, 0),
4124 CALL_EXPR_ARG (exp, 1),
4125 CALL_EXPR_ARG (exp, 2), type);
4127 return expand_expr (result, target, mode, EXPAND_NORMAL);
4132 /* Expand expression EXP, which is a call to the memcmp built-in function.
4133 Return NULL_RTX if we failed and the
4134 caller should emit a normal call, otherwise try to get the result in
4135 TARGET, if convenient (and in mode MODE, if that's convenient). */
4138 expand_builtin_memcmp (tree exp, rtx target, enum machine_mode mode)
4140 if (!validate_arglist (exp,
4141 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4145 tree result = fold_builtin_memcmp (CALL_EXPR_ARG (exp, 0),
4146 CALL_EXPR_ARG (exp, 1),
4147 CALL_EXPR_ARG (exp, 2));
4149 return expand_expr (result, target, mode, EXPAND_NORMAL);
4152 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4154 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4157 tree arg1 = CALL_EXPR_ARG (exp, 0);
4158 tree arg2 = CALL_EXPR_ARG (exp, 1);
4159 tree len = CALL_EXPR_ARG (exp, 2);
4162 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4164 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4165 enum machine_mode insn_mode;
4167 #ifdef HAVE_cmpmemsi
4169 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4172 #ifdef HAVE_cmpstrnsi
4174 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4179 /* If we don't have POINTER_TYPE, call the function. */
4180 if (arg1_align == 0 || arg2_align == 0)
4183 /* Make a place to write the result of the instruction. */
4186 && REG_P (result) && GET_MODE (result) == insn_mode
4187 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4188 result = gen_reg_rtx (insn_mode);
4190 arg1_rtx = get_memory_rtx (arg1, len);
4191 arg2_rtx = get_memory_rtx (arg2, len);
4192 arg3_rtx = expand_normal (fold_convert (sizetype, len));
4194 /* Set MEM_SIZE as appropriate. */
4195 if (GET_CODE (arg3_rtx) == CONST_INT)
4197 set_mem_size (arg1_rtx, arg3_rtx);
4198 set_mem_size (arg2_rtx, arg3_rtx);
4201 #ifdef HAVE_cmpmemsi
4203 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4204 GEN_INT (MIN (arg1_align, arg2_align)));
4207 #ifdef HAVE_cmpstrnsi
4209 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4210 GEN_INT (MIN (arg1_align, arg2_align)));
4218 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4219 TYPE_MODE (integer_type_node), 3,
4220 XEXP (arg1_rtx, 0), Pmode,
4221 XEXP (arg2_rtx, 0), Pmode,
4222 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4223 TYPE_UNSIGNED (sizetype)),
4224 TYPE_MODE (sizetype));
4226 /* Return the value in the proper mode for this function. */
4227 mode = TYPE_MODE (TREE_TYPE (exp));
4228 if (GET_MODE (result) == mode)
4230 else if (target != 0)
4232 convert_move (target, result, 0);
4236 return convert_to_mode (mode, result, 0);
4243 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4244 if we failed the caller should emit a normal call, otherwise try to get
4245 the result in TARGET, if convenient. */
4248 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
4250 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4254 tree result = fold_builtin_strcmp (CALL_EXPR_ARG (exp, 0),
4255 CALL_EXPR_ARG (exp, 1));
4257 return expand_expr (result, target, mode, EXPAND_NORMAL);
4260 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4261 if (cmpstr_optab[SImode] != CODE_FOR_nothing
4262 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
4264 rtx arg1_rtx, arg2_rtx;
4265 rtx result, insn = NULL_RTX;
4267 tree arg1 = CALL_EXPR_ARG (exp, 0);
4268 tree arg2 = CALL_EXPR_ARG (exp, 1);
4271 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4273 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4275 /* If we don't have POINTER_TYPE, call the function. */
4276 if (arg1_align == 0 || arg2_align == 0)
4279 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4280 arg1 = builtin_save_expr (arg1);
4281 arg2 = builtin_save_expr (arg2);
4283 arg1_rtx = get_memory_rtx (arg1, NULL);
4284 arg2_rtx = get_memory_rtx (arg2, NULL);
4286 #ifdef HAVE_cmpstrsi
4287 /* Try to call cmpstrsi. */
4290 enum machine_mode insn_mode
4291 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4293 /* Make a place to write the result of the instruction. */
4296 && REG_P (result) && GET_MODE (result) == insn_mode
4297 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4298 result = gen_reg_rtx (insn_mode);
4300 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4301 GEN_INT (MIN (arg1_align, arg2_align)));
4304 #ifdef HAVE_cmpstrnsi
4305 /* Try to determine at least one length and call cmpstrnsi. */
4306 if (!insn && HAVE_cmpstrnsi)
4311 enum machine_mode insn_mode
4312 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4313 tree len1 = c_strlen (arg1, 1);
4314 tree len2 = c_strlen (arg2, 1);
4317 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4319 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4321 /* If we don't have a constant length for the first, use the length
4322 of the second, if we know it. We don't require a constant for
4323 this case; some cost analysis could be done if both are available
4324 but neither is constant. For now, assume they're equally cheap,
4325 unless one has side effects. If both strings have constant lengths,
4332 else if (TREE_SIDE_EFFECTS (len1))
4334 else if (TREE_SIDE_EFFECTS (len2))
4336 else if (TREE_CODE (len1) != INTEGER_CST)
4338 else if (TREE_CODE (len2) != INTEGER_CST)
4340 else if (tree_int_cst_lt (len1, len2))
4345 /* If both arguments have side effects, we cannot optimize. */
4346 if (!len || TREE_SIDE_EFFECTS (len))
4349 arg3_rtx = expand_normal (len);
4351 /* Make a place to write the result of the instruction. */
4354 && REG_P (result) && GET_MODE (result) == insn_mode
4355 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4356 result = gen_reg_rtx (insn_mode);
4358 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4359 GEN_INT (MIN (arg1_align, arg2_align)));
4367 /* Return the value in the proper mode for this function. */
4368 mode = TYPE_MODE (TREE_TYPE (exp));
4369 if (GET_MODE (result) == mode)
4372 return convert_to_mode (mode, result, 0);
4373 convert_move (target, result, 0);
4377 /* Expand the library call ourselves using a stabilized argument
4378 list to avoid re-evaluating the function's arguments twice. */
4379 #ifdef HAVE_cmpstrnsi
4382 fndecl = get_callee_fndecl (exp);
4383 fn = build_call_expr (fndecl, 2, arg1, arg2);
4384 if (TREE_CODE (fn) == CALL_EXPR)
4385 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4386 return expand_call (fn, target, target == const0_rtx);
4392 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4393 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4394 the result in TARGET, if convenient. */
4397 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4399 if (!validate_arglist (exp,
4400 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4404 tree result = fold_builtin_strncmp (CALL_EXPR_ARG (exp, 0),
4405 CALL_EXPR_ARG (exp, 1),
4406 CALL_EXPR_ARG (exp, 2));
4408 return expand_expr (result, target, mode, EXPAND_NORMAL);
4411 /* If c_strlen can determine an expression for one of the string
4412 lengths, and it doesn't have side effects, then emit cmpstrnsi
4413 using length MIN(strlen(string)+1, arg3). */
4414 #ifdef HAVE_cmpstrnsi
4417 tree len, len1, len2;
4418 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4421 tree arg1 = CALL_EXPR_ARG (exp, 0);
4422 tree arg2 = CALL_EXPR_ARG (exp, 1);
4423 tree arg3 = CALL_EXPR_ARG (exp, 2);
4426 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4428 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4429 enum machine_mode insn_mode
4430 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4432 len1 = c_strlen (arg1, 1);
4433 len2 = c_strlen (arg2, 1);
4436 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4438 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4440 /* If we don't have a constant length for the first, use the length
4441 of the second, if we know it. We don't require a constant for
4442 this case; some cost analysis could be done if both are available
4443 but neither is constant. For now, assume they're equally cheap,
4444 unless one has side effects. If both strings have constant lengths,
4451 else if (TREE_SIDE_EFFECTS (len1))
4453 else if (TREE_SIDE_EFFECTS (len2))
4455 else if (TREE_CODE (len1) != INTEGER_CST)
4457 else if (TREE_CODE (len2) != INTEGER_CST)
4459 else if (tree_int_cst_lt (len1, len2))
4464 /* If both arguments have side effects, we cannot optimize. */
4465 if (!len || TREE_SIDE_EFFECTS (len))
4468 /* The actual new length parameter is MIN(len,arg3). */
4469 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4470 fold_convert (TREE_TYPE (len), arg3));
4472 /* If we don't have POINTER_TYPE, call the function. */
4473 if (arg1_align == 0 || arg2_align == 0)
4476 /* Make a place to write the result of the instruction. */
4479 && REG_P (result) && GET_MODE (result) == insn_mode
4480 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4481 result = gen_reg_rtx (insn_mode);
4483 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4484 arg1 = builtin_save_expr (arg1);
4485 arg2 = builtin_save_expr (arg2);
4486 len = builtin_save_expr (len);
4488 arg1_rtx = get_memory_rtx (arg1, len);
4489 arg2_rtx = get_memory_rtx (arg2, len);
4490 arg3_rtx = expand_normal (len);
4491 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4492 GEN_INT (MIN (arg1_align, arg2_align)));
4497 /* Return the value in the proper mode for this function. */
4498 mode = TYPE_MODE (TREE_TYPE (exp));
4499 if (GET_MODE (result) == mode)
4502 return convert_to_mode (mode, result, 0);
4503 convert_move (target, result, 0);
4507 /* Expand the library call ourselves using a stabilized argument
4508 list to avoid re-evaluating the function's arguments twice. */
4509 fndecl = get_callee_fndecl (exp);
4510 fn = build_call_expr (fndecl, 3, arg1, arg2, len);
4511 if (TREE_CODE (fn) == CALL_EXPR)
4512 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4513 return expand_call (fn, target, target == const0_rtx);
4519 /* Expand expression EXP, which is a call to the strcat builtin.
4520 Return NULL_RTX if we failed the caller should emit a normal call,
4521 otherwise try to get the result in TARGET, if convenient. */
4524 expand_builtin_strcat (tree fndecl, tree exp, rtx target, enum machine_mode mode)
4526 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4530 tree dst = CALL_EXPR_ARG (exp, 0);
4531 tree src = CALL_EXPR_ARG (exp, 1);
4532 const char *p = c_getstr (src);
4534 /* If the string length is zero, return the dst parameter. */
4535 if (p && *p == '\0')
4536 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4538 if (optimize_insn_for_speed_p ())
4540 /* See if we can store by pieces into (dst + strlen(dst)). */
4541 tree newsrc, newdst,
4542 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4545 /* Stabilize the argument list. */
4546 newsrc = builtin_save_expr (src);
4547 dst = builtin_save_expr (dst);
4551 /* Create strlen (dst). */
4552 newdst = build_call_expr (strlen_fn, 1, dst);
4553 /* Create (dst p+ strlen (dst)). */
4555 newdst = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4556 newdst = builtin_save_expr (newdst);
4558 if (!expand_builtin_strcpy_args (fndecl, newdst, newsrc, target, mode))
4560 end_sequence (); /* Stop sequence. */
4564 /* Output the entire sequence. */
4565 insns = get_insns ();
4569 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4576 /* Expand expression EXP, which is a call to the strncat builtin.
4577 Return NULL_RTX if we failed the caller should emit a normal call,
4578 otherwise try to get the result in TARGET, if convenient. */
4581 expand_builtin_strncat (tree exp, rtx target, enum machine_mode mode)
4583 if (validate_arglist (exp,
4584 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4586 tree result = fold_builtin_strncat (CALL_EXPR_ARG (exp, 0),
4587 CALL_EXPR_ARG (exp, 1),
4588 CALL_EXPR_ARG (exp, 2));
4590 return expand_expr (result, target, mode, EXPAND_NORMAL);
4595 /* Expand expression EXP, which is a call to the strspn builtin.
4596 Return NULL_RTX if we failed the caller should emit a normal call,
4597 otherwise try to get the result in TARGET, if convenient. */
4600 expand_builtin_strspn (tree exp, rtx target, enum machine_mode mode)
4602 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4604 tree result = fold_builtin_strspn (CALL_EXPR_ARG (exp, 0),
4605 CALL_EXPR_ARG (exp, 1));
4607 return expand_expr (result, target, mode, EXPAND_NORMAL);
4612 /* Expand expression EXP, which is a call to the strcspn builtin.
4613 Return NULL_RTX if we failed the caller should emit a normal call,
4614 otherwise try to get the result in TARGET, if convenient. */
4617 expand_builtin_strcspn (tree exp, rtx target, enum machine_mode mode)
4619 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4621 tree result = fold_builtin_strcspn (CALL_EXPR_ARG (exp, 0),
4622 CALL_EXPR_ARG (exp, 1));
4624 return expand_expr (result, target, mode, EXPAND_NORMAL);
4629 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4630 if that's convenient. */
4633 expand_builtin_saveregs (void)
4637 /* Don't do __builtin_saveregs more than once in a function.
4638 Save the result of the first call and reuse it. */
4639 if (saveregs_value != 0)
4640 return saveregs_value;
4642 /* When this function is called, it means that registers must be
4643 saved on entry to this function. So we migrate the call to the
4644 first insn of this function. */
4648 /* Do whatever the machine needs done in this case. */
4649 val = targetm.calls.expand_builtin_saveregs ();
4654 saveregs_value = val;
4656 /* Put the insns after the NOTE that starts the function. If this
4657 is inside a start_sequence, make the outer-level insn chain current, so
4658 the code is placed at the start of the function. */
4659 push_topmost_sequence ();
4660 emit_insn_after (seq, entry_of_function ());
4661 pop_topmost_sequence ();
4666 /* __builtin_args_info (N) returns word N of the arg space info
4667 for the current function. The number and meanings of words
4668 is controlled by the definition of CUMULATIVE_ARGS. */
4671 expand_builtin_args_info (tree exp)
4673 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4674 int *word_ptr = (int *) &crtl->args.info;
4676 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4678 if (call_expr_nargs (exp) != 0)
4680 if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0))
4681 error ("argument of %<__builtin_args_info%> must be constant");
4684 HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0);
4686 if (wordnum < 0 || wordnum >= nwords)
4687 error ("argument of %<__builtin_args_info%> out of range");
4689 return GEN_INT (word_ptr[wordnum]);
4693 error ("missing argument in %<__builtin_args_info%>");
4698 /* Expand a call to __builtin_next_arg. */
4701 expand_builtin_next_arg (void)
4703 /* Checking arguments is already done in fold_builtin_next_arg
4704 that must be called before this function. */
4705 return expand_binop (ptr_mode, add_optab,
4706 crtl->args.internal_arg_pointer,
4707 crtl->args.arg_offset_rtx,
4708 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4711 /* Make it easier for the backends by protecting the valist argument
4712 from multiple evaluations. */
4715 stabilize_va_list (tree valist, int needs_lvalue)
4717 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4719 gcc_assert (vatype != NULL_TREE);
4721 if (TREE_CODE (vatype) == ARRAY_TYPE)
4723 if (TREE_SIDE_EFFECTS (valist))
4724 valist = save_expr (valist);
4726 /* For this case, the backends will be expecting a pointer to
4727 vatype, but it's possible we've actually been given an array
4728 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4730 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4732 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4733 valist = build_fold_addr_expr_with_type (valist, p1);
4742 if (! TREE_SIDE_EFFECTS (valist))
4745 pt = build_pointer_type (vatype);
4746 valist = fold_build1 (ADDR_EXPR, pt, valist);
4747 TREE_SIDE_EFFECTS (valist) = 1;
4750 if (TREE_SIDE_EFFECTS (valist))
4751 valist = save_expr (valist);
4752 valist = build_fold_indirect_ref (valist);
4758 /* The "standard" definition of va_list is void*. */
4761 std_build_builtin_va_list (void)
4763 return ptr_type_node;
4766 /* The "standard" abi va_list is va_list_type_node. */
4769 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4771 return va_list_type_node;
4774 /* The "standard" type of va_list is va_list_type_node. */
4777 std_canonical_va_list_type (tree type)
4781 if (INDIRECT_REF_P (type))
4782 type = TREE_TYPE (type);
4783 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4784 type = TREE_TYPE (type);
4785 wtype = va_list_type_node;
4787 /* Treat structure va_list types. */
4788 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4789 htype = TREE_TYPE (htype);
4790 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4792 /* If va_list is an array type, the argument may have decayed
4793 to a pointer type, e.g. by being passed to another function.
4794 In that case, unwrap both types so that we can compare the
4795 underlying records. */
4796 if (TREE_CODE (htype) == ARRAY_TYPE
4797 || POINTER_TYPE_P (htype))
4799 wtype = TREE_TYPE (wtype);
4800 htype = TREE_TYPE (htype);
4803 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4804 return va_list_type_node;
4809 /* The "standard" implementation of va_start: just assign `nextarg' to
4813 std_expand_builtin_va_start (tree valist, rtx nextarg)
4815 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4816 convert_move (va_r, nextarg, 0);
4819 /* Expand EXP, a call to __builtin_va_start. */
4822 expand_builtin_va_start (tree exp)
4827 if (call_expr_nargs (exp) < 2)
4829 error ("too few arguments to function %<va_start%>");
4833 if (fold_builtin_next_arg (exp, true))
4836 nextarg = expand_builtin_next_arg ();
4837 valist = stabilize_va_list (CALL_EXPR_ARG (exp, 0), 1);
4839 if (targetm.expand_builtin_va_start)
4840 targetm.expand_builtin_va_start (valist, nextarg);
4842 std_expand_builtin_va_start (valist, nextarg);
4847 /* The "standard" implementation of va_arg: read the value from the
4848 current (padded) address and increment by the (padded) size. */
4851 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4854 tree addr, t, type_size, rounded_size, valist_tmp;
4855 unsigned HOST_WIDE_INT align, boundary;
4858 #ifdef ARGS_GROW_DOWNWARD
4859 /* All of the alignment and movement below is for args-grow-up machines.
4860 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4861 implement their own specialized gimplify_va_arg_expr routines. */
4865 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4867 type = build_pointer_type (type);
4869 align = PARM_BOUNDARY / BITS_PER_UNIT;
4870 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4872 /* When we align parameter on stack for caller, if the parameter
4873 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4874 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4875 here with caller. */
4876 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4877 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4879 boundary /= BITS_PER_UNIT;
4881 /* Hoist the valist value into a temporary for the moment. */
4882 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4884 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4885 requires greater alignment, we must perform dynamic alignment. */
4886 if (boundary > align
4887 && !integer_zerop (TYPE_SIZE (type)))
4889 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4890 fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist),
4891 valist_tmp, size_int (boundary - 1)));
4892 gimplify_and_add (t, pre_p);
4894 t = fold_convert (sizetype, valist_tmp);
4895 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4896 fold_convert (TREE_TYPE (valist),
4897 fold_build2 (BIT_AND_EXPR, sizetype, t,
4898 size_int (-boundary))));
4899 gimplify_and_add (t, pre_p);
4904 /* If the actual alignment is less than the alignment of the type,
4905 adjust the type accordingly so that we don't assume strict alignment
4906 when dereferencing the pointer. */
4907 boundary *= BITS_PER_UNIT;
4908 if (boundary < TYPE_ALIGN (type))
4910 type = build_variant_type_copy (type);
4911 TYPE_ALIGN (type) = boundary;
4914 /* Compute the rounded size of the type. */
4915 type_size = size_in_bytes (type);
4916 rounded_size = round_up (type_size, align);
4918 /* Reduce rounded_size so it's sharable with the postqueue. */
4919 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4923 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4925 /* Small args are padded downward. */
4926 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4927 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4928 size_binop (MINUS_EXPR, rounded_size, type_size));
4929 addr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr, t);
4932 /* Compute new value for AP. */
4933 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4934 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4935 gimplify_and_add (t, pre_p);
4937 addr = fold_convert (build_pointer_type (type), addr);
4940 addr = build_va_arg_indirect_ref (addr);
4942 return build_va_arg_indirect_ref (addr);
4945 /* Build an indirect-ref expression over the given TREE, which represents a
4946 piece of a va_arg() expansion. */
4948 build_va_arg_indirect_ref (tree addr)
4950 addr = build_fold_indirect_ref (addr);
4952 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4958 /* Return a dummy expression of type TYPE in order to keep going after an
4962 dummy_object (tree type)
4964 tree t = build_int_cst (build_pointer_type (type), 0);
4965 return build1 (INDIRECT_REF, type, t);
4968 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4969 builtin function, but a very special sort of operator. */
4971 enum gimplify_status
4972 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4974 tree promoted_type, have_va_type;
4975 tree valist = TREE_OPERAND (*expr_p, 0);
4976 tree type = TREE_TYPE (*expr_p);
4978 location_t loc = EXPR_HAS_LOCATION (*expr_p) ? EXPR_LOCATION (*expr_p) :
4981 /* Verify that valist is of the proper type. */
4982 have_va_type = TREE_TYPE (valist);
4983 if (have_va_type == error_mark_node)
4985 have_va_type = targetm.canonical_va_list_type (have_va_type);
4987 if (have_va_type == NULL_TREE)
4989 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4993 /* Generate a diagnostic for requesting data of a type that cannot
4994 be passed through `...' due to type promotion at the call site. */
4995 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4998 static bool gave_help;
5001 /* Unfortunately, this is merely undefined, rather than a constraint
5002 violation, so we cannot make this an error. If this call is never
5003 executed, the program is still strictly conforming. */
5004 warned = warning_at (loc, 0,
5005 "%qT is promoted to %qT when passed through %<...%>",
5006 type, promoted_type);
5007 if (!gave_help && warned)
5010 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
5011 promoted_type, type);
5014 /* We can, however, treat "undefined" any way we please.
5015 Call abort to encourage the user to fix the program. */
5017 inform (loc, "if this code is reached, the program will abort");
5018 /* Before the abort, allow the evaluation of the va_list
5019 expression to exit or longjmp. */
5020 gimplify_and_add (valist, pre_p);
5021 t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
5022 gimplify_and_add (t, pre_p);
5024 /* This is dead code, but go ahead and finish so that the
5025 mode of the result comes out right. */
5026 *expr_p = dummy_object (type);
5031 /* Make it easier for the backends by protecting the valist argument
5032 from multiple evaluations. */
5033 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
5035 /* For this case, the backends will be expecting a pointer to
5036 TREE_TYPE (abi), but it's possible we've
5037 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
5039 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5041 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
5042 valist = fold_convert (p1, build_fold_addr_expr (valist));
5045 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
5048 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
5050 if (!targetm.gimplify_va_arg_expr)
5051 /* FIXME: Once most targets are converted we should merely
5052 assert this is non-null. */
5055 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
5060 /* Expand EXP, a call to __builtin_va_end. */
5063 expand_builtin_va_end (tree exp)
5065 tree valist = CALL_EXPR_ARG (exp, 0);
5067 /* Evaluate for side effects, if needed. I hate macros that don't
5069 if (TREE_SIDE_EFFECTS (valist))
5070 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5075 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5076 builtin rather than just as an assignment in stdarg.h because of the
5077 nastiness of array-type va_list types. */
5080 expand_builtin_va_copy (tree exp)
5084 dst = CALL_EXPR_ARG (exp, 0);
5085 src = CALL_EXPR_ARG (exp, 1);
5087 dst = stabilize_va_list (dst, 1);
5088 src = stabilize_va_list (src, 0);
5090 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5092 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5094 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5095 TREE_SIDE_EFFECTS (t) = 1;
5096 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5100 rtx dstb, srcb, size;
5102 /* Evaluate to pointers. */
5103 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5104 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5105 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5106 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5108 dstb = convert_memory_address (Pmode, dstb);
5109 srcb = convert_memory_address (Pmode, srcb);
5111 /* "Dereference" to BLKmode memories. */
5112 dstb = gen_rtx_MEM (BLKmode, dstb);
5113 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5114 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5115 srcb = gen_rtx_MEM (BLKmode, srcb);
5116 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5117 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5120 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5126 /* Expand a call to one of the builtin functions __builtin_frame_address or
5127 __builtin_return_address. */
5130 expand_builtin_frame_address (tree fndecl, tree exp)
5132 /* The argument must be a nonnegative integer constant.
5133 It counts the number of frames to scan up the stack.
5134 The value is the return address saved in that frame. */
5135 if (call_expr_nargs (exp) == 0)
5136 /* Warning about missing arg was already issued. */
5138 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
5140 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5141 error ("invalid argument to %<__builtin_frame_address%>");
5143 error ("invalid argument to %<__builtin_return_address%>");
5149 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
5150 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
5152 /* Some ports cannot access arbitrary stack frames. */
5155 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5156 warning (0, "unsupported argument to %<__builtin_frame_address%>");
5158 warning (0, "unsupported argument to %<__builtin_return_address%>");
5162 /* For __builtin_frame_address, return what we've got. */
5163 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5167 && ! CONSTANT_P (tem))
5168 tem = copy_to_mode_reg (Pmode, tem);
5173 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
5174 we failed and the caller should emit a normal call, otherwise try to get
5175 the result in TARGET, if convenient. */
5178 expand_builtin_alloca (tree exp, rtx target)
5183 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
5184 should always expand to function calls. These can be intercepted
5189 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5192 /* Compute the argument. */
5193 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5195 /* Allocate the desired space. */
5196 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
5197 result = convert_memory_address (ptr_mode, result);
5202 /* Expand a call to a bswap builtin with argument ARG0. MODE
5203 is the mode to expand with. */
5206 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5208 enum machine_mode mode;
5212 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5215 arg = CALL_EXPR_ARG (exp, 0);
5216 mode = TYPE_MODE (TREE_TYPE (arg));
5217 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5219 target = expand_unop (mode, bswap_optab, op0, target, 1);
5221 gcc_assert (target);
5223 return convert_to_mode (mode, target, 0);
5226 /* Expand a call to a unary builtin in EXP.
5227 Return NULL_RTX if a normal call should be emitted rather than expanding the
5228 function in-line. If convenient, the result should be placed in TARGET.
5229 SUBTARGET may be used as the target for computing one of EXP's operands. */
5232 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5233 rtx subtarget, optab op_optab)
5237 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5240 /* Compute the argument. */
5241 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5242 VOIDmode, EXPAND_NORMAL);
5243 /* Compute op, into TARGET if possible.
5244 Set TARGET to wherever the result comes back. */
5245 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5246 op_optab, op0, target, 1);
5247 gcc_assert (target);
5249 return convert_to_mode (target_mode, target, 0);
5252 /* If the string passed to fputs is a constant and is one character
5253 long, we attempt to transform this call into __builtin_fputc(). */
5256 expand_builtin_fputs (tree exp, rtx target, bool unlocked)
5258 /* Verify the arguments in the original call. */
5259 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5261 tree result = fold_builtin_fputs (CALL_EXPR_ARG (exp, 0),
5262 CALL_EXPR_ARG (exp, 1),
5263 (target == const0_rtx),
5264 unlocked, NULL_TREE);
5266 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
5271 /* Expand a call to __builtin_expect. We just return our argument
5272 as the builtin_expect semantic should've been already executed by
5273 tree branch prediction pass. */
5276 expand_builtin_expect (tree exp, rtx target)
5280 if (call_expr_nargs (exp) < 2)
5282 arg = CALL_EXPR_ARG (exp, 0);
5283 c = CALL_EXPR_ARG (exp, 1);
5285 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5286 /* When guessing was done, the hints should be already stripped away. */
5287 gcc_assert (!flag_guess_branch_prob
5288 || optimize == 0 || errorcount || sorrycount);
5293 expand_builtin_trap (void)
5297 emit_insn (gen_trap ());
5300 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5304 /* Expand a call to __builtin_unreachable. We do nothing except emit
5305 a barrier saying that control flow will not pass here.
5307 It is the responsibility of the program being compiled to ensure
5308 that control flow does never reach __builtin_unreachable. */
5310 expand_builtin_unreachable (void)
5315 /* Expand EXP, a call to fabs, fabsf or fabsl.
5316 Return NULL_RTX if a normal call should be emitted rather than expanding
5317 the function inline. If convenient, the result should be placed
5318 in TARGET. SUBTARGET may be used as the target for computing
5322 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5324 enum machine_mode mode;
5328 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5331 arg = CALL_EXPR_ARG (exp, 0);
5332 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5333 mode = TYPE_MODE (TREE_TYPE (arg));
5334 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5335 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5338 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5339 Return NULL is a normal call should be emitted rather than expanding the
5340 function inline. If convenient, the result should be placed in TARGET.
5341 SUBTARGET may be used as the target for computing the operand. */
5344 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5349 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5352 arg = CALL_EXPR_ARG (exp, 0);
5353 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5355 arg = CALL_EXPR_ARG (exp, 1);
5356 op1 = expand_normal (arg);
5358 return expand_copysign (op0, op1, target);
5361 /* Create a new constant string literal and return a char* pointer to it.
5362 The STRING_CST value is the LEN characters at STR. */
5364 build_string_literal (int len, const char *str)
5366 tree t, elem, index, type;
5368 t = build_string (len, str);
5369 elem = build_type_variant (char_type_node, 1, 0);
5370 index = build_index_type (size_int (len - 1));
5371 type = build_array_type (elem, index);
5372 TREE_TYPE (t) = type;
5373 TREE_CONSTANT (t) = 1;
5374 TREE_READONLY (t) = 1;
5375 TREE_STATIC (t) = 1;
5377 type = build_pointer_type (elem);
5378 t = build1 (ADDR_EXPR, type,
5379 build4 (ARRAY_REF, elem,
5380 t, integer_zero_node, NULL_TREE, NULL_TREE));
5384 /* Expand EXP, a call to printf or printf_unlocked.
5385 Return NULL_RTX if a normal call should be emitted rather than transforming
5386 the function inline. If convenient, the result should be placed in
5387 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
5390 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
5393 /* If we're using an unlocked function, assume the other unlocked
5394 functions exist explicitly. */
5395 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
5396 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
5397 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
5398 : implicit_built_in_decls[BUILT_IN_PUTS];
5399 const char *fmt_str;
5402 int nargs = call_expr_nargs (exp);
5404 /* If the return value is used, don't do the transformation. */
5405 if (target != const0_rtx)
5408 /* Verify the required arguments in the original call. */
5411 fmt = CALL_EXPR_ARG (exp, 0);
5412 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5415 /* Check whether the format is a literal string constant. */
5416 fmt_str = c_getstr (fmt);
5417 if (fmt_str == NULL)
5420 if (!init_target_chars ())
5423 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5424 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5427 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1))))
5430 fn = build_call_expr (fn_puts, 1, CALL_EXPR_ARG (exp, 1));
5432 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5433 else if (strcmp (fmt_str, target_percent_c) == 0)
5436 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE)
5439 fn = build_call_expr (fn_putchar, 1, CALL_EXPR_ARG (exp, 1));
5443 /* We can't handle anything else with % args or %% ... yet. */
5444 if (strchr (fmt_str, target_percent))
5450 /* If the format specifier was "", printf does nothing. */
5451 if (fmt_str[0] == '\0')
5453 /* If the format specifier has length of 1, call putchar. */
5454 if (fmt_str[1] == '\0')
5456 /* Given printf("c"), (where c is any one character,)
5457 convert "c"[0] to an int and pass that to the replacement
5459 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5461 fn = build_call_expr (fn_putchar, 1, arg);
5465 /* If the format specifier was "string\n", call puts("string"). */
5466 size_t len = strlen (fmt_str);
5467 if ((unsigned char)fmt_str[len - 1] == target_newline)
5469 /* Create a NUL-terminated string that's one char shorter
5470 than the original, stripping off the trailing '\n'. */
5471 char *newstr = XALLOCAVEC (char, len);
5472 memcpy (newstr, fmt_str, len - 1);
5473 newstr[len - 1] = 0;
5474 arg = build_string_literal (len, newstr);
5476 fn = build_call_expr (fn_puts, 1, arg);
5479 /* We'd like to arrange to call fputs(string,stdout) here,
5480 but we need stdout and don't have a way to get it yet. */
5487 if (TREE_CODE (fn) == CALL_EXPR)
5488 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5489 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5492 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5493 Return NULL_RTX if a normal call should be emitted rather than transforming
5494 the function inline. If convenient, the result should be placed in
5495 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5498 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5501 /* If we're using an unlocked function, assume the other unlocked
5502 functions exist explicitly. */
5503 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5504 : implicit_built_in_decls[BUILT_IN_FPUTC];
5505 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5506 : implicit_built_in_decls[BUILT_IN_FPUTS];
5507 const char *fmt_str;
5510 int nargs = call_expr_nargs (exp);
5512 /* If the return value is used, don't do the transformation. */
5513 if (target != const0_rtx)
5516 /* Verify the required arguments in the original call. */
5519 fp = CALL_EXPR_ARG (exp, 0);
5520 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5522 fmt = CALL_EXPR_ARG (exp, 1);
5523 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5526 /* Check whether the format is a literal string constant. */
5527 fmt_str = c_getstr (fmt);
5528 if (fmt_str == NULL)
5531 if (!init_target_chars ())
5534 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5535 if (strcmp (fmt_str, target_percent_s) == 0)
5538 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2))))
5540 arg = CALL_EXPR_ARG (exp, 2);
5542 fn = build_call_expr (fn_fputs, 2, arg, fp);
5544 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5545 else if (strcmp (fmt_str, target_percent_c) == 0)
5548 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE)
5550 arg = CALL_EXPR_ARG (exp, 2);
5552 fn = build_call_expr (fn_fputc, 2, arg, fp);
5556 /* We can't handle anything else with % args or %% ... yet. */
5557 if (strchr (fmt_str, target_percent))
5563 /* If the format specifier was "", fprintf does nothing. */
5564 if (fmt_str[0] == '\0')
5566 /* Evaluate and ignore FILE* argument for side-effects. */
5567 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5571 /* When "string" doesn't contain %, replace all cases of
5572 fprintf(stream,string) with fputs(string,stream). The fputs
5573 builtin will take care of special cases like length == 1. */
5575 fn = build_call_expr (fn_fputs, 2, fmt, fp);
5580 if (TREE_CODE (fn) == CALL_EXPR)
5581 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5582 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5585 /* Expand a call EXP to sprintf. Return NULL_RTX if
5586 a normal call should be emitted rather than expanding the function
5587 inline. If convenient, the result should be placed in TARGET with
5591 expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode)
5594 const char *fmt_str;
5595 int nargs = call_expr_nargs (exp);
5597 /* Verify the required arguments in the original call. */
5600 dest = CALL_EXPR_ARG (exp, 0);
5601 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5603 fmt = CALL_EXPR_ARG (exp, 0);
5604 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5607 /* Check whether the format is a literal string constant. */
5608 fmt_str = c_getstr (fmt);
5609 if (fmt_str == NULL)
5612 if (!init_target_chars ())
5615 /* If the format doesn't contain % args or %%, use strcpy. */
5616 if (strchr (fmt_str, target_percent) == 0)
5618 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5621 if ((nargs > 2) || ! fn)
5623 expand_expr (build_call_expr (fn, 2, dest, fmt),
5624 const0_rtx, VOIDmode, EXPAND_NORMAL);
5625 if (target == const0_rtx)
5627 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5628 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5630 /* If the format is "%s", use strcpy if the result isn't used. */
5631 else if (strcmp (fmt_str, target_percent_s) == 0)
5634 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5640 arg = CALL_EXPR_ARG (exp, 2);
5641 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5644 if (target != const0_rtx)
5646 len = c_strlen (arg, 1);
5647 if (! len || TREE_CODE (len) != INTEGER_CST)
5653 expand_expr (build_call_expr (fn, 2, dest, arg),
5654 const0_rtx, VOIDmode, EXPAND_NORMAL);
5656 if (target == const0_rtx)
5658 return expand_expr (len, target, mode, EXPAND_NORMAL);
5664 /* Expand a call to either the entry or exit function profiler. */
5667 expand_builtin_profile_func (bool exitp)
5669 rtx this_rtx, which;
5671 this_rtx = DECL_RTL (current_function_decl);
5672 gcc_assert (MEM_P (this_rtx));
5673 this_rtx = XEXP (this_rtx, 0);
5676 which = profile_function_exit_libfunc;
5678 which = profile_function_entry_libfunc;
5680 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5681 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5688 /* Expand a call to __builtin___clear_cache. */
5691 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5693 #ifndef HAVE_clear_cache
5694 #ifdef CLEAR_INSN_CACHE
5695 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5696 does something. Just do the default expansion to a call to
5700 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5701 does nothing. There is no need to call it. Do nothing. */
5703 #endif /* CLEAR_INSN_CACHE */
5705 /* We have a "clear_cache" insn, and it will handle everything. */
5707 rtx begin_rtx, end_rtx;
5708 enum insn_code icode;
5710 /* We must not expand to a library call. If we did, any
5711 fallback library function in libgcc that might contain a call to
5712 __builtin___clear_cache() would recurse infinitely. */
5713 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5715 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5719 if (HAVE_clear_cache)
5721 icode = CODE_FOR_clear_cache;
5723 begin = CALL_EXPR_ARG (exp, 0);
5724 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5725 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5726 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5727 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5729 end = CALL_EXPR_ARG (exp, 1);
5730 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5731 end_rtx = convert_memory_address (Pmode, end_rtx);
5732 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5733 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5735 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5738 #endif /* HAVE_clear_cache */
5741 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5744 round_trampoline_addr (rtx tramp)
5746 rtx temp, addend, mask;
5748 /* If we don't need too much alignment, we'll have been guaranteed
5749 proper alignment by get_trampoline_type. */
5750 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5753 /* Round address up to desired boundary. */
5754 temp = gen_reg_rtx (Pmode);
5755 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5756 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5758 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5759 temp, 0, OPTAB_LIB_WIDEN);
5760 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5761 temp, 0, OPTAB_LIB_WIDEN);
5767 expand_builtin_init_trampoline (tree exp)
5769 tree t_tramp, t_func, t_chain;
5770 rtx r_tramp, r_func, r_chain;
5771 #ifdef TRAMPOLINE_TEMPLATE
5775 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5776 POINTER_TYPE, VOID_TYPE))
5779 t_tramp = CALL_EXPR_ARG (exp, 0);
5780 t_func = CALL_EXPR_ARG (exp, 1);
5781 t_chain = CALL_EXPR_ARG (exp, 2);
5783 r_tramp = expand_normal (t_tramp);
5784 r_func = expand_normal (t_func);
5785 r_chain = expand_normal (t_chain);
5787 /* Generate insns to initialize the trampoline. */
5788 r_tramp = round_trampoline_addr (r_tramp);
5789 #ifdef TRAMPOLINE_TEMPLATE
5790 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5791 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5792 emit_block_move (blktramp, assemble_trampoline_template (),
5793 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5795 trampolines_created = 1;
5796 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5802 expand_builtin_adjust_trampoline (tree exp)
5806 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5809 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5810 tramp = round_trampoline_addr (tramp);
5811 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5812 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5818 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5819 function. The function first checks whether the back end provides
5820 an insn to implement signbit for the respective mode. If not, it
5821 checks whether the floating point format of the value is such that
5822 the sign bit can be extracted. If that is not the case, the
5823 function returns NULL_RTX to indicate that a normal call should be
5824 emitted rather than expanding the function in-line. EXP is the
5825 expression that is a call to the builtin function; if convenient,
5826 the result should be placed in TARGET. */
5828 expand_builtin_signbit (tree exp, rtx target)
5830 const struct real_format *fmt;
5831 enum machine_mode fmode, imode, rmode;
5832 HOST_WIDE_INT hi, lo;
5835 enum insn_code icode;
5838 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5841 arg = CALL_EXPR_ARG (exp, 0);
5842 fmode = TYPE_MODE (TREE_TYPE (arg));
5843 rmode = TYPE_MODE (TREE_TYPE (exp));
5844 fmt = REAL_MODE_FORMAT (fmode);
5846 arg = builtin_save_expr (arg);
5848 /* Expand the argument yielding a RTX expression. */
5849 temp = expand_normal (arg);
5851 /* Check if the back end provides an insn that handles signbit for the
5853 icode = signbit_optab->handlers [(int) fmode].insn_code;
5854 if (icode != CODE_FOR_nothing)
5856 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5857 emit_unop_insn (icode, target, temp, UNKNOWN);
5861 /* For floating point formats without a sign bit, implement signbit
5863 bitpos = fmt->signbit_ro;
5866 /* But we can't do this if the format supports signed zero. */
5867 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5870 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5871 build_real (TREE_TYPE (arg), dconst0));
5872 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5875 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5877 imode = int_mode_for_mode (fmode);
5878 if (imode == BLKmode)
5880 temp = gen_lowpart (imode, temp);
5885 /* Handle targets with different FP word orders. */
5886 if (FLOAT_WORDS_BIG_ENDIAN)
5887 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5889 word = bitpos / BITS_PER_WORD;
5890 temp = operand_subword_force (temp, word, fmode);
5891 bitpos = bitpos % BITS_PER_WORD;
5894 /* Force the intermediate word_mode (or narrower) result into a
5895 register. This avoids attempting to create paradoxical SUBREGs
5896 of floating point modes below. */
5897 temp = force_reg (imode, temp);
5899 /* If the bitpos is within the "result mode" lowpart, the operation
5900 can be implement with a single bitwise AND. Otherwise, we need
5901 a right shift and an AND. */
5903 if (bitpos < GET_MODE_BITSIZE (rmode))
5905 if (bitpos < HOST_BITS_PER_WIDE_INT)
5908 lo = (HOST_WIDE_INT) 1 << bitpos;
5912 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5916 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5917 temp = gen_lowpart (rmode, temp);
5918 temp = expand_binop (rmode, and_optab, temp,
5919 immed_double_const (lo, hi, rmode),
5920 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5924 /* Perform a logical right shift to place the signbit in the least
5925 significant bit, then truncate the result to the desired mode
5926 and mask just this bit. */
5927 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5928 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5929 temp = gen_lowpart (rmode, temp);
5930 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5931 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5937 /* Expand fork or exec calls. TARGET is the desired target of the
5938 call. EXP is the call. FN is the
5939 identificator of the actual function. IGNORE is nonzero if the
5940 value is to be ignored. */
5943 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5948 /* If we are not profiling, just call the function. */
5949 if (!profile_arc_flag)
5952 /* Otherwise call the wrapper. This should be equivalent for the rest of
5953 compiler, so the code does not diverge, and the wrapper may run the
5954 code necessary for keeping the profiling sane. */
5956 switch (DECL_FUNCTION_CODE (fn))
5959 id = get_identifier ("__gcov_fork");
5962 case BUILT_IN_EXECL:
5963 id = get_identifier ("__gcov_execl");
5966 case BUILT_IN_EXECV:
5967 id = get_identifier ("__gcov_execv");
5970 case BUILT_IN_EXECLP:
5971 id = get_identifier ("__gcov_execlp");
5974 case BUILT_IN_EXECLE:
5975 id = get_identifier ("__gcov_execle");
5978 case BUILT_IN_EXECVP:
5979 id = get_identifier ("__gcov_execvp");
5982 case BUILT_IN_EXECVE:
5983 id = get_identifier ("__gcov_execve");
5990 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5991 FUNCTION_DECL, id, TREE_TYPE (fn));
5992 DECL_EXTERNAL (decl) = 1;
5993 TREE_PUBLIC (decl) = 1;
5994 DECL_ARTIFICIAL (decl) = 1;
5995 TREE_NOTHROW (decl) = 1;
5996 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5997 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5998 call = rewrite_call_expr (exp, 0, decl, 0);
5999 return expand_call (call, target, ignore);
6004 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6005 the pointer in these functions is void*, the tree optimizers may remove
6006 casts. The mode computed in expand_builtin isn't reliable either, due
6007 to __sync_bool_compare_and_swap.
6009 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6010 group of builtins. This gives us log2 of the mode size. */
6012 static inline enum machine_mode
6013 get_builtin_sync_mode (int fcode_diff)
6015 /* The size is not negotiable, so ask not to get BLKmode in return
6016 if the target indicates that a smaller size would be better. */
6017 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
6020 /* Expand the memory expression LOC and return the appropriate memory operand
6021 for the builtin_sync operations. */
6024 get_builtin_sync_mem (tree loc, enum machine_mode mode)
6028 addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM);
6030 /* Note that we explicitly do not want any alias information for this
6031 memory, so that we kill all other live memories. Otherwise we don't
6032 satisfy the full barrier semantics of the intrinsic. */
6033 mem = validize_mem (gen_rtx_MEM (mode, addr));
6035 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
6036 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6037 MEM_VOLATILE_P (mem) = 1;
6042 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6043 EXP is the CALL_EXPR. CODE is the rtx code
6044 that corresponds to the arithmetic or logical operation from the name;
6045 an exception here is that NOT actually means NAND. TARGET is an optional
6046 place for us to store the results; AFTER is true if this is the
6047 fetch_and_xxx form. IGNORE is true if we don't actually care about
6048 the result of the operation at all. */
6051 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
6052 enum rtx_code code, bool after,
6053 rtx target, bool ignore)
6056 enum machine_mode old_mode;
6057 location_t loc = EXPR_LOCATION (exp);
6059 if (code == NOT && warn_sync_nand)
6061 tree fndecl = get_callee_fndecl (exp);
6062 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6064 static bool warned_f_a_n, warned_n_a_f;
6068 case BUILT_IN_FETCH_AND_NAND_1:
6069 case BUILT_IN_FETCH_AND_NAND_2:
6070 case BUILT_IN_FETCH_AND_NAND_4:
6071 case BUILT_IN_FETCH_AND_NAND_8:
6072 case BUILT_IN_FETCH_AND_NAND_16:
6077 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
6078 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6079 warned_f_a_n = true;
6082 case BUILT_IN_NAND_AND_FETCH_1:
6083 case BUILT_IN_NAND_AND_FETCH_2:
6084 case BUILT_IN_NAND_AND_FETCH_4:
6085 case BUILT_IN_NAND_AND_FETCH_8:
6086 case BUILT_IN_NAND_AND_FETCH_16:
6091 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
6092 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6093 warned_n_a_f = true;
6101 /* Expand the operands. */
6102 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6104 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6105 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6106 of CONST_INTs, where we know the old_mode only from the call argument. */
6107 old_mode = GET_MODE (val);
6108 if (old_mode == VOIDmode)
6109 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6110 val = convert_modes (mode, old_mode, val, 1);
6113 return expand_sync_operation (mem, val, code);
6115 return expand_sync_fetch_operation (mem, val, code, after, target);
6118 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6119 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6120 true if this is the boolean form. TARGET is a place for us to store the
6121 results; this is NOT optional if IS_BOOL is true. */
6124 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
6125 bool is_bool, rtx target)
6127 rtx old_val, new_val, mem;
6128 enum machine_mode old_mode;
6130 /* Expand the operands. */
6131 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6134 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
6135 mode, EXPAND_NORMAL);
6136 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6137 of CONST_INTs, where we know the old_mode only from the call argument. */
6138 old_mode = GET_MODE (old_val);
6139 if (old_mode == VOIDmode)
6140 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6141 old_val = convert_modes (mode, old_mode, old_val, 1);
6143 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
6144 mode, EXPAND_NORMAL);
6145 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6146 of CONST_INTs, where we know the old_mode only from the call argument. */
6147 old_mode = GET_MODE (new_val);
6148 if (old_mode == VOIDmode)
6149 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
6150 new_val = convert_modes (mode, old_mode, new_val, 1);
6153 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
6155 return expand_val_compare_and_swap (mem, old_val, new_val, target);
6158 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6159 general form is actually an atomic exchange, and some targets only
6160 support a reduced form with the second argument being a constant 1.
6161 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6165 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
6169 enum machine_mode old_mode;
6171 /* Expand the operands. */
6172 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6173 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6174 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6175 of CONST_INTs, where we know the old_mode only from the call argument. */
6176 old_mode = GET_MODE (val);
6177 if (old_mode == VOIDmode)
6178 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6179 val = convert_modes (mode, old_mode, val, 1);
6181 return expand_sync_lock_test_and_set (mem, val, target);
6184 /* Expand the __sync_synchronize intrinsic. */
6187 expand_builtin_synchronize (void)
6191 #ifdef HAVE_memory_barrier
6192 if (HAVE_memory_barrier)
6194 emit_insn (gen_memory_barrier ());
6199 if (synchronize_libfunc != NULL_RTX)
6201 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
6205 /* If no explicit memory barrier instruction is available, create an
6206 empty asm stmt with a memory clobber. */
6207 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
6208 tree_cons (NULL, build_string (6, "memory"), NULL));
6209 ASM_VOLATILE_P (x) = 1;
6210 expand_asm_expr (x);
6213 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6216 expand_builtin_lock_release (enum machine_mode mode, tree exp)
6218 enum insn_code icode;
6220 rtx val = const0_rtx;
6222 /* Expand the operands. */
6223 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6225 /* If there is an explicit operation in the md file, use it. */
6226 icode = sync_lock_release[mode];
6227 if (icode != CODE_FOR_nothing)
6229 if (!insn_data[icode].operand[1].predicate (val, mode))
6230 val = force_reg (mode, val);
6232 insn = GEN_FCN (icode) (mem, val);
6240 /* Otherwise we can implement this operation by emitting a barrier
6241 followed by a store of zero. */
6242 expand_builtin_synchronize ();
6243 emit_move_insn (mem, val);
6246 /* Expand an expression EXP that calls a built-in function,
6247 with result going to TARGET if that's convenient
6248 (and in mode MODE if that's convenient).
6249 SUBTARGET may be used as the target for computing one of EXP's operands.
6250 IGNORE is nonzero if the value is to be ignored. */
6253 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
6256 tree fndecl = get_callee_fndecl (exp);
6257 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6258 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6260 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6261 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6263 /* When not optimizing, generate calls to library functions for a certain
6266 && !called_as_built_in (fndecl)
6267 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
6268 && fcode != BUILT_IN_ALLOCA
6269 && fcode != BUILT_IN_FREE)
6270 return expand_call (exp, target, ignore);
6272 /* The built-in function expanders test for target == const0_rtx
6273 to determine whether the function's result will be ignored. */
6275 target = const0_rtx;
6277 /* If the result of a pure or const built-in function is ignored, and
6278 none of its arguments are volatile, we can avoid expanding the
6279 built-in call and just evaluate the arguments for side-effects. */
6280 if (target == const0_rtx
6281 && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl)))
6283 bool volatilep = false;
6285 call_expr_arg_iterator iter;
6287 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6288 if (TREE_THIS_VOLATILE (arg))
6296 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6297 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6304 CASE_FLT_FN (BUILT_IN_FABS):
6305 target = expand_builtin_fabs (exp, target, subtarget);
6310 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6311 target = expand_builtin_copysign (exp, target, subtarget);
6316 /* Just do a normal library call if we were unable to fold
6318 CASE_FLT_FN (BUILT_IN_CABS):
6321 CASE_FLT_FN (BUILT_IN_EXP):
6322 CASE_FLT_FN (BUILT_IN_EXP10):
6323 CASE_FLT_FN (BUILT_IN_POW10):
6324 CASE_FLT_FN (BUILT_IN_EXP2):
6325 CASE_FLT_FN (BUILT_IN_EXPM1):
6326 CASE_FLT_FN (BUILT_IN_LOGB):
6327 CASE_FLT_FN (BUILT_IN_LOG):
6328 CASE_FLT_FN (BUILT_IN_LOG10):
6329 CASE_FLT_FN (BUILT_IN_LOG2):
6330 CASE_FLT_FN (BUILT_IN_LOG1P):
6331 CASE_FLT_FN (BUILT_IN_TAN):
6332 CASE_FLT_FN (BUILT_IN_ASIN):
6333 CASE_FLT_FN (BUILT_IN_ACOS):
6334 CASE_FLT_FN (BUILT_IN_ATAN):
6335 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6336 because of possible accuracy problems. */
6337 if (! flag_unsafe_math_optimizations)
6339 CASE_FLT_FN (BUILT_IN_SQRT):
6340 CASE_FLT_FN (BUILT_IN_FLOOR):
6341 CASE_FLT_FN (BUILT_IN_CEIL):
6342 CASE_FLT_FN (BUILT_IN_TRUNC):
6343 CASE_FLT_FN (BUILT_IN_ROUND):
6344 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6345 CASE_FLT_FN (BUILT_IN_RINT):
6346 target = expand_builtin_mathfn (exp, target, subtarget);
6351 CASE_FLT_FN (BUILT_IN_ILOGB):
6352 if (! flag_unsafe_math_optimizations)
6354 CASE_FLT_FN (BUILT_IN_ISINF):
6355 CASE_FLT_FN (BUILT_IN_FINITE):
6356 case BUILT_IN_ISFINITE:
6357 case BUILT_IN_ISNORMAL:
6358 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
6363 CASE_FLT_FN (BUILT_IN_LCEIL):
6364 CASE_FLT_FN (BUILT_IN_LLCEIL):
6365 CASE_FLT_FN (BUILT_IN_LFLOOR):
6366 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6367 target = expand_builtin_int_roundingfn (exp, target);
6372 CASE_FLT_FN (BUILT_IN_LRINT):
6373 CASE_FLT_FN (BUILT_IN_LLRINT):
6374 CASE_FLT_FN (BUILT_IN_LROUND):
6375 CASE_FLT_FN (BUILT_IN_LLROUND):
6376 target = expand_builtin_int_roundingfn_2 (exp, target);
6381 CASE_FLT_FN (BUILT_IN_POW):
6382 target = expand_builtin_pow (exp, target, subtarget);
6387 CASE_FLT_FN (BUILT_IN_POWI):
6388 target = expand_builtin_powi (exp, target, subtarget);
6393 CASE_FLT_FN (BUILT_IN_ATAN2):
6394 CASE_FLT_FN (BUILT_IN_LDEXP):
6395 CASE_FLT_FN (BUILT_IN_SCALB):
6396 CASE_FLT_FN (BUILT_IN_SCALBN):
6397 CASE_FLT_FN (BUILT_IN_SCALBLN):
6398 if (! flag_unsafe_math_optimizations)
6401 CASE_FLT_FN (BUILT_IN_FMOD):
6402 CASE_FLT_FN (BUILT_IN_REMAINDER):
6403 CASE_FLT_FN (BUILT_IN_DREM):
6404 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6409 CASE_FLT_FN (BUILT_IN_CEXPI):
6410 target = expand_builtin_cexpi (exp, target, subtarget);
6411 gcc_assert (target);
6414 CASE_FLT_FN (BUILT_IN_SIN):
6415 CASE_FLT_FN (BUILT_IN_COS):
6416 if (! flag_unsafe_math_optimizations)
6418 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6423 CASE_FLT_FN (BUILT_IN_SINCOS):
6424 if (! flag_unsafe_math_optimizations)
6426 target = expand_builtin_sincos (exp);
6431 case BUILT_IN_APPLY_ARGS:
6432 return expand_builtin_apply_args ();
6434 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6435 FUNCTION with a copy of the parameters described by
6436 ARGUMENTS, and ARGSIZE. It returns a block of memory
6437 allocated on the stack into which is stored all the registers
6438 that might possibly be used for returning the result of a
6439 function. ARGUMENTS is the value returned by
6440 __builtin_apply_args. ARGSIZE is the number of bytes of
6441 arguments that must be copied. ??? How should this value be
6442 computed? We'll also need a safe worst case value for varargs
6444 case BUILT_IN_APPLY:
6445 if (!validate_arglist (exp, POINTER_TYPE,
6446 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6447 && !validate_arglist (exp, REFERENCE_TYPE,
6448 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6454 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6455 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6456 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6458 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6461 /* __builtin_return (RESULT) causes the function to return the
6462 value described by RESULT. RESULT is address of the block of
6463 memory returned by __builtin_apply. */
6464 case BUILT_IN_RETURN:
6465 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6466 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6469 case BUILT_IN_SAVEREGS:
6470 return expand_builtin_saveregs ();
6472 case BUILT_IN_ARGS_INFO:
6473 return expand_builtin_args_info (exp);
6475 case BUILT_IN_VA_ARG_PACK:
6476 /* All valid uses of __builtin_va_arg_pack () are removed during
6478 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6481 case BUILT_IN_VA_ARG_PACK_LEN:
6482 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6484 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6487 /* Return the address of the first anonymous stack arg. */
6488 case BUILT_IN_NEXT_ARG:
6489 if (fold_builtin_next_arg (exp, false))
6491 return expand_builtin_next_arg ();
6493 case BUILT_IN_CLEAR_CACHE:
6494 target = expand_builtin___clear_cache (exp);
6499 case BUILT_IN_CLASSIFY_TYPE:
6500 return expand_builtin_classify_type (exp);
6502 case BUILT_IN_CONSTANT_P:
6505 case BUILT_IN_FRAME_ADDRESS:
6506 case BUILT_IN_RETURN_ADDRESS:
6507 return expand_builtin_frame_address (fndecl, exp);
6509 /* Returns the address of the area where the structure is returned.
6511 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6512 if (call_expr_nargs (exp) != 0
6513 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6514 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6517 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6519 case BUILT_IN_ALLOCA:
6520 target = expand_builtin_alloca (exp, target);
6525 case BUILT_IN_STACK_SAVE:
6526 return expand_stack_save ();
6528 case BUILT_IN_STACK_RESTORE:
6529 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6532 case BUILT_IN_BSWAP32:
6533 case BUILT_IN_BSWAP64:
6534 target = expand_builtin_bswap (exp, target, subtarget);
6540 CASE_INT_FN (BUILT_IN_FFS):
6541 case BUILT_IN_FFSIMAX:
6542 target = expand_builtin_unop (target_mode, exp, target,
6543 subtarget, ffs_optab);
6548 CASE_INT_FN (BUILT_IN_CLZ):
6549 case BUILT_IN_CLZIMAX:
6550 target = expand_builtin_unop (target_mode, exp, target,
6551 subtarget, clz_optab);
6556 CASE_INT_FN (BUILT_IN_CTZ):
6557 case BUILT_IN_CTZIMAX:
6558 target = expand_builtin_unop (target_mode, exp, target,
6559 subtarget, ctz_optab);
6564 CASE_INT_FN (BUILT_IN_POPCOUNT):
6565 case BUILT_IN_POPCOUNTIMAX:
6566 target = expand_builtin_unop (target_mode, exp, target,
6567 subtarget, popcount_optab);
6572 CASE_INT_FN (BUILT_IN_PARITY):
6573 case BUILT_IN_PARITYIMAX:
6574 target = expand_builtin_unop (target_mode, exp, target,
6575 subtarget, parity_optab);
6580 case BUILT_IN_STRLEN:
6581 target = expand_builtin_strlen (exp, target, target_mode);
6586 case BUILT_IN_STRCPY:
6587 target = expand_builtin_strcpy (fndecl, exp, target, mode);
6592 case BUILT_IN_STRNCPY:
6593 target = expand_builtin_strncpy (exp, target, mode);
6598 case BUILT_IN_STPCPY:
6599 target = expand_builtin_stpcpy (exp, target, mode);
6604 case BUILT_IN_STRCAT:
6605 target = expand_builtin_strcat (fndecl, exp, target, mode);
6610 case BUILT_IN_STRNCAT:
6611 target = expand_builtin_strncat (exp, target, mode);
6616 case BUILT_IN_STRSPN:
6617 target = expand_builtin_strspn (exp, target, mode);
6622 case BUILT_IN_STRCSPN:
6623 target = expand_builtin_strcspn (exp, target, mode);
6628 case BUILT_IN_STRSTR:
6629 target = expand_builtin_strstr (exp, target, mode);
6634 case BUILT_IN_STRPBRK:
6635 target = expand_builtin_strpbrk (exp, target, mode);
6640 case BUILT_IN_INDEX:
6641 case BUILT_IN_STRCHR:
6642 target = expand_builtin_strchr (exp, target, mode);
6647 case BUILT_IN_RINDEX:
6648 case BUILT_IN_STRRCHR:
6649 target = expand_builtin_strrchr (exp, target, mode);
6654 case BUILT_IN_MEMCPY:
6655 target = expand_builtin_memcpy (exp, target, mode);
6660 case BUILT_IN_MEMPCPY:
6661 target = expand_builtin_mempcpy (exp, target, mode);
6666 case BUILT_IN_MEMMOVE:
6667 target = expand_builtin_memmove (exp, target, mode, ignore);
6672 case BUILT_IN_BCOPY:
6673 target = expand_builtin_bcopy (exp, ignore);
6678 case BUILT_IN_MEMSET:
6679 target = expand_builtin_memset (exp, target, mode);
6684 case BUILT_IN_BZERO:
6685 target = expand_builtin_bzero (exp);
6690 case BUILT_IN_STRCMP:
6691 target = expand_builtin_strcmp (exp, target, mode);
6696 case BUILT_IN_STRNCMP:
6697 target = expand_builtin_strncmp (exp, target, mode);
6702 case BUILT_IN_MEMCHR:
6703 target = expand_builtin_memchr (exp, target, mode);
6709 case BUILT_IN_MEMCMP:
6710 target = expand_builtin_memcmp (exp, target, mode);
6715 case BUILT_IN_SETJMP:
6716 /* This should have been lowered to the builtins below. */
6719 case BUILT_IN_SETJMP_SETUP:
6720 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6721 and the receiver label. */
6722 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6724 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6725 VOIDmode, EXPAND_NORMAL);
6726 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6727 rtx label_r = label_rtx (label);
6729 /* This is copied from the handling of non-local gotos. */
6730 expand_builtin_setjmp_setup (buf_addr, label_r);
6731 nonlocal_goto_handler_labels
6732 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6733 nonlocal_goto_handler_labels);
6734 /* ??? Do not let expand_label treat us as such since we would
6735 not want to be both on the list of non-local labels and on
6736 the list of forced labels. */
6737 FORCED_LABEL (label) = 0;
6742 case BUILT_IN_SETJMP_DISPATCHER:
6743 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6744 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6746 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6747 rtx label_r = label_rtx (label);
6749 /* Remove the dispatcher label from the list of non-local labels
6750 since the receiver labels have been added to it above. */
6751 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6756 case BUILT_IN_SETJMP_RECEIVER:
6757 /* __builtin_setjmp_receiver is passed the receiver label. */
6758 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6760 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6761 rtx label_r = label_rtx (label);
6763 expand_builtin_setjmp_receiver (label_r);
6768 /* __builtin_longjmp is passed a pointer to an array of five words.
6769 It's similar to the C library longjmp function but works with
6770 __builtin_setjmp above. */
6771 case BUILT_IN_LONGJMP:
6772 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6774 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6775 VOIDmode, EXPAND_NORMAL);
6776 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6778 if (value != const1_rtx)
6780 error ("%<__builtin_longjmp%> second argument must be 1");
6784 expand_builtin_longjmp (buf_addr, value);
6789 case BUILT_IN_NONLOCAL_GOTO:
6790 target = expand_builtin_nonlocal_goto (exp);
6795 /* This updates the setjmp buffer that is its argument with the value
6796 of the current stack pointer. */
6797 case BUILT_IN_UPDATE_SETJMP_BUF:
6798 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6801 = expand_normal (CALL_EXPR_ARG (exp, 0));
6803 expand_builtin_update_setjmp_buf (buf_addr);
6809 expand_builtin_trap ();
6812 case BUILT_IN_UNREACHABLE:
6813 expand_builtin_unreachable ();
6816 case BUILT_IN_PRINTF:
6817 target = expand_builtin_printf (exp, target, mode, false);
6822 case BUILT_IN_PRINTF_UNLOCKED:
6823 target = expand_builtin_printf (exp, target, mode, true);
6828 case BUILT_IN_FPUTS:
6829 target = expand_builtin_fputs (exp, target, false);
6833 case BUILT_IN_FPUTS_UNLOCKED:
6834 target = expand_builtin_fputs (exp, target, true);
6839 case BUILT_IN_FPRINTF:
6840 target = expand_builtin_fprintf (exp, target, mode, false);
6845 case BUILT_IN_FPRINTF_UNLOCKED:
6846 target = expand_builtin_fprintf (exp, target, mode, true);
6851 case BUILT_IN_SPRINTF:
6852 target = expand_builtin_sprintf (exp, target, mode);
6857 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6858 case BUILT_IN_SIGNBITD32:
6859 case BUILT_IN_SIGNBITD64:
6860 case BUILT_IN_SIGNBITD128:
6861 target = expand_builtin_signbit (exp, target);
6866 /* Various hooks for the DWARF 2 __throw routine. */
6867 case BUILT_IN_UNWIND_INIT:
6868 expand_builtin_unwind_init ();
6870 case BUILT_IN_DWARF_CFA:
6871 return virtual_cfa_rtx;
6872 #ifdef DWARF2_UNWIND_INFO
6873 case BUILT_IN_DWARF_SP_COLUMN:
6874 return expand_builtin_dwarf_sp_column ();
6875 case BUILT_IN_INIT_DWARF_REG_SIZES:
6876 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6879 case BUILT_IN_FROB_RETURN_ADDR:
6880 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6881 case BUILT_IN_EXTRACT_RETURN_ADDR:
6882 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6883 case BUILT_IN_EH_RETURN:
6884 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6885 CALL_EXPR_ARG (exp, 1));
6887 #ifdef EH_RETURN_DATA_REGNO
6888 case BUILT_IN_EH_RETURN_DATA_REGNO:
6889 return expand_builtin_eh_return_data_regno (exp);
6891 case BUILT_IN_EXTEND_POINTER:
6892 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6894 case BUILT_IN_VA_START:
6895 return expand_builtin_va_start (exp);
6896 case BUILT_IN_VA_END:
6897 return expand_builtin_va_end (exp);
6898 case BUILT_IN_VA_COPY:
6899 return expand_builtin_va_copy (exp);
6900 case BUILT_IN_EXPECT:
6901 return expand_builtin_expect (exp, target);
6902 case BUILT_IN_PREFETCH:
6903 expand_builtin_prefetch (exp);
6906 case BUILT_IN_PROFILE_FUNC_ENTER:
6907 return expand_builtin_profile_func (false);
6908 case BUILT_IN_PROFILE_FUNC_EXIT:
6909 return expand_builtin_profile_func (true);
6911 case BUILT_IN_INIT_TRAMPOLINE:
6912 return expand_builtin_init_trampoline (exp);
6913 case BUILT_IN_ADJUST_TRAMPOLINE:
6914 return expand_builtin_adjust_trampoline (exp);
6917 case BUILT_IN_EXECL:
6918 case BUILT_IN_EXECV:
6919 case BUILT_IN_EXECLP:
6920 case BUILT_IN_EXECLE:
6921 case BUILT_IN_EXECVP:
6922 case BUILT_IN_EXECVE:
6923 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6928 case BUILT_IN_FETCH_AND_ADD_1:
6929 case BUILT_IN_FETCH_AND_ADD_2:
6930 case BUILT_IN_FETCH_AND_ADD_4:
6931 case BUILT_IN_FETCH_AND_ADD_8:
6932 case BUILT_IN_FETCH_AND_ADD_16:
6933 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6934 target = expand_builtin_sync_operation (mode, exp, PLUS,
6935 false, target, ignore);
6940 case BUILT_IN_FETCH_AND_SUB_1:
6941 case BUILT_IN_FETCH_AND_SUB_2:
6942 case BUILT_IN_FETCH_AND_SUB_4:
6943 case BUILT_IN_FETCH_AND_SUB_8:
6944 case BUILT_IN_FETCH_AND_SUB_16:
6945 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6946 target = expand_builtin_sync_operation (mode, exp, MINUS,
6947 false, target, ignore);
6952 case BUILT_IN_FETCH_AND_OR_1:
6953 case BUILT_IN_FETCH_AND_OR_2:
6954 case BUILT_IN_FETCH_AND_OR_4:
6955 case BUILT_IN_FETCH_AND_OR_8:
6956 case BUILT_IN_FETCH_AND_OR_16:
6957 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6958 target = expand_builtin_sync_operation (mode, exp, IOR,
6959 false, target, ignore);
6964 case BUILT_IN_FETCH_AND_AND_1:
6965 case BUILT_IN_FETCH_AND_AND_2:
6966 case BUILT_IN_FETCH_AND_AND_4:
6967 case BUILT_IN_FETCH_AND_AND_8:
6968 case BUILT_IN_FETCH_AND_AND_16:
6969 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6970 target = expand_builtin_sync_operation (mode, exp, AND,
6971 false, target, ignore);
6976 case BUILT_IN_FETCH_AND_XOR_1:
6977 case BUILT_IN_FETCH_AND_XOR_2:
6978 case BUILT_IN_FETCH_AND_XOR_4:
6979 case BUILT_IN_FETCH_AND_XOR_8:
6980 case BUILT_IN_FETCH_AND_XOR_16:
6981 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6982 target = expand_builtin_sync_operation (mode, exp, XOR,
6983 false, target, ignore);
6988 case BUILT_IN_FETCH_AND_NAND_1:
6989 case BUILT_IN_FETCH_AND_NAND_2:
6990 case BUILT_IN_FETCH_AND_NAND_4:
6991 case BUILT_IN_FETCH_AND_NAND_8:
6992 case BUILT_IN_FETCH_AND_NAND_16:
6993 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6994 target = expand_builtin_sync_operation (mode, exp, NOT,
6995 false, target, ignore);
7000 case BUILT_IN_ADD_AND_FETCH_1:
7001 case BUILT_IN_ADD_AND_FETCH_2:
7002 case BUILT_IN_ADD_AND_FETCH_4:
7003 case BUILT_IN_ADD_AND_FETCH_8:
7004 case BUILT_IN_ADD_AND_FETCH_16:
7005 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
7006 target = expand_builtin_sync_operation (mode, exp, PLUS,
7007 true, target, ignore);
7012 case BUILT_IN_SUB_AND_FETCH_1:
7013 case BUILT_IN_SUB_AND_FETCH_2:
7014 case BUILT_IN_SUB_AND_FETCH_4:
7015 case BUILT_IN_SUB_AND_FETCH_8:
7016 case BUILT_IN_SUB_AND_FETCH_16:
7017 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
7018 target = expand_builtin_sync_operation (mode, exp, MINUS,
7019 true, target, ignore);
7024 case BUILT_IN_OR_AND_FETCH_1:
7025 case BUILT_IN_OR_AND_FETCH_2:
7026 case BUILT_IN_OR_AND_FETCH_4:
7027 case BUILT_IN_OR_AND_FETCH_8:
7028 case BUILT_IN_OR_AND_FETCH_16:
7029 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
7030 target = expand_builtin_sync_operation (mode, exp, IOR,
7031 true, target, ignore);
7036 case BUILT_IN_AND_AND_FETCH_1:
7037 case BUILT_IN_AND_AND_FETCH_2:
7038 case BUILT_IN_AND_AND_FETCH_4:
7039 case BUILT_IN_AND_AND_FETCH_8:
7040 case BUILT_IN_AND_AND_FETCH_16:
7041 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
7042 target = expand_builtin_sync_operation (mode, exp, AND,
7043 true, target, ignore);
7048 case BUILT_IN_XOR_AND_FETCH_1:
7049 case BUILT_IN_XOR_AND_FETCH_2:
7050 case BUILT_IN_XOR_AND_FETCH_4:
7051 case BUILT_IN_XOR_AND_FETCH_8:
7052 case BUILT_IN_XOR_AND_FETCH_16:
7053 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
7054 target = expand_builtin_sync_operation (mode, exp, XOR,
7055 true, target, ignore);
7060 case BUILT_IN_NAND_AND_FETCH_1:
7061 case BUILT_IN_NAND_AND_FETCH_2:
7062 case BUILT_IN_NAND_AND_FETCH_4:
7063 case BUILT_IN_NAND_AND_FETCH_8:
7064 case BUILT_IN_NAND_AND_FETCH_16:
7065 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
7066 target = expand_builtin_sync_operation (mode, exp, NOT,
7067 true, target, ignore);
7072 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
7073 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
7074 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
7075 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
7076 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
7077 if (mode == VOIDmode)
7078 mode = TYPE_MODE (boolean_type_node);
7079 if (!target || !register_operand (target, mode))
7080 target = gen_reg_rtx (mode);
7082 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
7083 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7088 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
7089 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
7090 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
7091 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
7092 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
7093 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
7094 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7099 case BUILT_IN_LOCK_TEST_AND_SET_1:
7100 case BUILT_IN_LOCK_TEST_AND_SET_2:
7101 case BUILT_IN_LOCK_TEST_AND_SET_4:
7102 case BUILT_IN_LOCK_TEST_AND_SET_8:
7103 case BUILT_IN_LOCK_TEST_AND_SET_16:
7104 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
7105 target = expand_builtin_lock_test_and_set (mode, exp, target);
7110 case BUILT_IN_LOCK_RELEASE_1:
7111 case BUILT_IN_LOCK_RELEASE_2:
7112 case BUILT_IN_LOCK_RELEASE_4:
7113 case BUILT_IN_LOCK_RELEASE_8:
7114 case BUILT_IN_LOCK_RELEASE_16:
7115 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
7116 expand_builtin_lock_release (mode, exp);
7119 case BUILT_IN_SYNCHRONIZE:
7120 expand_builtin_synchronize ();
7123 case BUILT_IN_OBJECT_SIZE:
7124 return expand_builtin_object_size (exp);
7126 case BUILT_IN_MEMCPY_CHK:
7127 case BUILT_IN_MEMPCPY_CHK:
7128 case BUILT_IN_MEMMOVE_CHK:
7129 case BUILT_IN_MEMSET_CHK:
7130 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7135 case BUILT_IN_STRCPY_CHK:
7136 case BUILT_IN_STPCPY_CHK:
7137 case BUILT_IN_STRNCPY_CHK:
7138 case BUILT_IN_STRCAT_CHK:
7139 case BUILT_IN_STRNCAT_CHK:
7140 case BUILT_IN_SNPRINTF_CHK:
7141 case BUILT_IN_VSNPRINTF_CHK:
7142 maybe_emit_chk_warning (exp, fcode);
7145 case BUILT_IN_SPRINTF_CHK:
7146 case BUILT_IN_VSPRINTF_CHK:
7147 maybe_emit_sprintf_chk_warning (exp, fcode);
7151 maybe_emit_free_warning (exp);
7154 default: /* just do library call, if unknown builtin */
7158 /* The switch statement above can drop through to cause the function
7159 to be called normally. */
7160 return expand_call (exp, target, ignore);
7163 /* Determine whether a tree node represents a call to a built-in
7164 function. If the tree T is a call to a built-in function with
7165 the right number of arguments of the appropriate types, return
7166 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7167 Otherwise the return value is END_BUILTINS. */
7169 enum built_in_function
7170 builtin_mathfn_code (const_tree t)
7172 const_tree fndecl, arg, parmlist;
7173 const_tree argtype, parmtype;
7174 const_call_expr_arg_iterator iter;
7176 if (TREE_CODE (t) != CALL_EXPR
7177 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7178 return END_BUILTINS;
7180 fndecl = get_callee_fndecl (t);
7181 if (fndecl == NULL_TREE
7182 || TREE_CODE (fndecl) != FUNCTION_DECL
7183 || ! DECL_BUILT_IN (fndecl)
7184 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7185 return END_BUILTINS;
7187 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7188 init_const_call_expr_arg_iterator (t, &iter);
7189 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7191 /* If a function doesn't take a variable number of arguments,
7192 the last element in the list will have type `void'. */
7193 parmtype = TREE_VALUE (parmlist);
7194 if (VOID_TYPE_P (parmtype))
7196 if (more_const_call_expr_args_p (&iter))
7197 return END_BUILTINS;
7198 return DECL_FUNCTION_CODE (fndecl);
7201 if (! more_const_call_expr_args_p (&iter))
7202 return END_BUILTINS;
7204 arg = next_const_call_expr_arg (&iter);
7205 argtype = TREE_TYPE (arg);
7207 if (SCALAR_FLOAT_TYPE_P (parmtype))
7209 if (! SCALAR_FLOAT_TYPE_P (argtype))
7210 return END_BUILTINS;
7212 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7214 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7215 return END_BUILTINS;
7217 else if (POINTER_TYPE_P (parmtype))
7219 if (! POINTER_TYPE_P (argtype))
7220 return END_BUILTINS;
7222 else if (INTEGRAL_TYPE_P (parmtype))
7224 if (! INTEGRAL_TYPE_P (argtype))
7225 return END_BUILTINS;
7228 return END_BUILTINS;
7231 /* Variable-length argument list. */
7232 return DECL_FUNCTION_CODE (fndecl);
7235 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7236 evaluate to a constant. */
7239 fold_builtin_constant_p (tree arg)
7241 /* We return 1 for a numeric type that's known to be a constant
7242 value at compile-time or for an aggregate type that's a
7243 literal constant. */
7246 /* If we know this is a constant, emit the constant of one. */
7247 if (CONSTANT_CLASS_P (arg)
7248 || (TREE_CODE (arg) == CONSTRUCTOR
7249 && TREE_CONSTANT (arg)))
7250 return integer_one_node;
7251 if (TREE_CODE (arg) == ADDR_EXPR)
7253 tree op = TREE_OPERAND (arg, 0);
7254 if (TREE_CODE (op) == STRING_CST
7255 || (TREE_CODE (op) == ARRAY_REF
7256 && integer_zerop (TREE_OPERAND (op, 1))
7257 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7258 return integer_one_node;
7261 /* If this expression has side effects, show we don't know it to be a
7262 constant. Likewise if it's a pointer or aggregate type since in
7263 those case we only want literals, since those are only optimized
7264 when generating RTL, not later.
7265 And finally, if we are compiling an initializer, not code, we
7266 need to return a definite result now; there's not going to be any
7267 more optimization done. */
7268 if (TREE_SIDE_EFFECTS (arg)
7269 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7270 || POINTER_TYPE_P (TREE_TYPE (arg))
7272 || folding_initializer)
7273 return integer_zero_node;
7278 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7279 return it as a truthvalue. */
7282 build_builtin_expect_predicate (tree pred, tree expected)
7284 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7286 fn = built_in_decls[BUILT_IN_EXPECT];
7287 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7288 ret_type = TREE_TYPE (TREE_TYPE (fn));
7289 pred_type = TREE_VALUE (arg_types);
7290 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7292 pred = fold_convert (pred_type, pred);
7293 expected = fold_convert (expected_type, expected);
7294 call_expr = build_call_expr (fn, 2, pred, expected);
7296 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7297 build_int_cst (ret_type, 0));
7300 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7301 NULL_TREE if no simplification is possible. */
7304 fold_builtin_expect (tree arg0, tree arg1)
7307 enum tree_code code;
7309 /* If this is a builtin_expect within a builtin_expect keep the
7310 inner one. See through a comparison against a constant. It
7311 might have been added to create a thruthvalue. */
7313 if (COMPARISON_CLASS_P (inner)
7314 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7315 inner = TREE_OPERAND (inner, 0);
7317 if (TREE_CODE (inner) == CALL_EXPR
7318 && (fndecl = get_callee_fndecl (inner))
7319 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7320 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7323 /* Distribute the expected value over short-circuiting operators.
7324 See through the cast from truthvalue_type_node to long. */
7326 while (TREE_CODE (inner) == NOP_EXPR
7327 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
7328 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
7329 inner = TREE_OPERAND (inner, 0);
7331 code = TREE_CODE (inner);
7332 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7334 tree op0 = TREE_OPERAND (inner, 0);
7335 tree op1 = TREE_OPERAND (inner, 1);
7337 op0 = build_builtin_expect_predicate (op0, arg1);
7338 op1 = build_builtin_expect_predicate (op1, arg1);
7339 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7341 return fold_convert (TREE_TYPE (arg0), inner);
7344 /* If the argument isn't invariant then there's nothing else we can do. */
7345 if (!TREE_CONSTANT (arg0))
7348 /* If we expect that a comparison against the argument will fold to
7349 a constant return the constant. In practice, this means a true
7350 constant or the address of a non-weak symbol. */
7353 if (TREE_CODE (inner) == ADDR_EXPR)
7357 inner = TREE_OPERAND (inner, 0);
7359 while (TREE_CODE (inner) == COMPONENT_REF
7360 || TREE_CODE (inner) == ARRAY_REF);
7361 if ((TREE_CODE (inner) == VAR_DECL
7362 || TREE_CODE (inner) == FUNCTION_DECL)
7363 && DECL_WEAK (inner))
7367 /* Otherwise, ARG0 already has the proper type for the return value. */
7371 /* Fold a call to __builtin_classify_type with argument ARG. */
7374 fold_builtin_classify_type (tree arg)
7377 return build_int_cst (NULL_TREE, no_type_class);
7379 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
7382 /* Fold a call to __builtin_strlen with argument ARG. */
7385 fold_builtin_strlen (tree arg)
7387 if (!validate_arg (arg, POINTER_TYPE))
7391 tree len = c_strlen (arg, 0);
7395 /* Convert from the internal "sizetype" type to "size_t". */
7397 len = fold_convert (size_type_node, len);
7405 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7408 fold_builtin_inf (tree type, int warn)
7410 REAL_VALUE_TYPE real;
7412 /* __builtin_inff is intended to be usable to define INFINITY on all
7413 targets. If an infinity is not available, INFINITY expands "to a
7414 positive constant of type float that overflows at translation
7415 time", footnote "In this case, using INFINITY will violate the
7416 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7417 Thus we pedwarn to ensure this constraint violation is
7419 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7420 pedwarn (input_location, 0, "target format does not support infinity");
7423 return build_real (type, real);
7426 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7429 fold_builtin_nan (tree arg, tree type, int quiet)
7431 REAL_VALUE_TYPE real;
7434 if (!validate_arg (arg, POINTER_TYPE))
7436 str = c_getstr (arg);
7440 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7443 return build_real (type, real);
7446 /* Return true if the floating point expression T has an integer value.
7447 We also allow +Inf, -Inf and NaN to be considered integer values. */
7450 integer_valued_real_p (tree t)
7452 switch (TREE_CODE (t))
7459 return integer_valued_real_p (TREE_OPERAND (t, 0));
7464 return integer_valued_real_p (TREE_OPERAND (t, 1));
7471 return integer_valued_real_p (TREE_OPERAND (t, 0))
7472 && integer_valued_real_p (TREE_OPERAND (t, 1));
7475 return integer_valued_real_p (TREE_OPERAND (t, 1))
7476 && integer_valued_real_p (TREE_OPERAND (t, 2));
7479 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7483 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7484 if (TREE_CODE (type) == INTEGER_TYPE)
7486 if (TREE_CODE (type) == REAL_TYPE)
7487 return integer_valued_real_p (TREE_OPERAND (t, 0));
7492 switch (builtin_mathfn_code (t))
7494 CASE_FLT_FN (BUILT_IN_CEIL):
7495 CASE_FLT_FN (BUILT_IN_FLOOR):
7496 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7497 CASE_FLT_FN (BUILT_IN_RINT):
7498 CASE_FLT_FN (BUILT_IN_ROUND):
7499 CASE_FLT_FN (BUILT_IN_TRUNC):
7502 CASE_FLT_FN (BUILT_IN_FMIN):
7503 CASE_FLT_FN (BUILT_IN_FMAX):
7504 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7505 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7518 /* FNDECL is assumed to be a builtin where truncation can be propagated
7519 across (for instance floor((double)f) == (double)floorf (f).
7520 Do the transformation for a call with argument ARG. */
7523 fold_trunc_transparent_mathfn (tree fndecl, tree arg)
7525 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7527 if (!validate_arg (arg, REAL_TYPE))
7530 /* Integer rounding functions are idempotent. */
7531 if (fcode == builtin_mathfn_code (arg))
7534 /* If argument is already integer valued, and we don't need to worry
7535 about setting errno, there's no need to perform rounding. */
7536 if (! flag_errno_math && integer_valued_real_p (arg))
7541 tree arg0 = strip_float_extensions (arg);
7542 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7543 tree newtype = TREE_TYPE (arg0);
7546 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7547 && (decl = mathfn_built_in (newtype, fcode)))
7548 return fold_convert (ftype,
7549 build_call_expr (decl, 1,
7550 fold_convert (newtype, arg0)));
7555 /* FNDECL is assumed to be builtin which can narrow the FP type of
7556 the argument, for instance lround((double)f) -> lroundf (f).
7557 Do the transformation for a call with argument ARG. */
7560 fold_fixed_mathfn (tree fndecl, tree arg)
7562 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7564 if (!validate_arg (arg, REAL_TYPE))
7567 /* If argument is already integer valued, and we don't need to worry
7568 about setting errno, there's no need to perform rounding. */
7569 if (! flag_errno_math && integer_valued_real_p (arg))
7570 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
7574 tree ftype = TREE_TYPE (arg);
7575 tree arg0 = strip_float_extensions (arg);
7576 tree newtype = TREE_TYPE (arg0);
7579 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7580 && (decl = mathfn_built_in (newtype, fcode)))
7581 return build_call_expr (decl, 1, fold_convert (newtype, arg0));
7584 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7585 sizeof (long long) == sizeof (long). */
7586 if (TYPE_PRECISION (long_long_integer_type_node)
7587 == TYPE_PRECISION (long_integer_type_node))
7589 tree newfn = NULL_TREE;
7592 CASE_FLT_FN (BUILT_IN_LLCEIL):
7593 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7596 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7597 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7600 CASE_FLT_FN (BUILT_IN_LLROUND):
7601 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7604 CASE_FLT_FN (BUILT_IN_LLRINT):
7605 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7614 tree newcall = build_call_expr(newfn, 1, arg);
7615 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7622 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7623 return type. Return NULL_TREE if no simplification can be made. */
7626 fold_builtin_cabs (tree arg, tree type, tree fndecl)
7630 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7631 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7634 /* Calculate the result when the argument is a constant. */
7635 if (TREE_CODE (arg) == COMPLEX_CST
7636 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7640 if (TREE_CODE (arg) == COMPLEX_EXPR)
7642 tree real = TREE_OPERAND (arg, 0);
7643 tree imag = TREE_OPERAND (arg, 1);
7645 /* If either part is zero, cabs is fabs of the other. */
7646 if (real_zerop (real))
7647 return fold_build1 (ABS_EXPR, type, imag);
7648 if (real_zerop (imag))
7649 return fold_build1 (ABS_EXPR, type, real);
7651 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7652 if (flag_unsafe_math_optimizations
7653 && operand_equal_p (real, imag, OEP_PURE_SAME))
7655 const REAL_VALUE_TYPE sqrt2_trunc
7656 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7658 return fold_build2 (MULT_EXPR, type,
7659 fold_build1 (ABS_EXPR, type, real),
7660 build_real (type, sqrt2_trunc));
7664 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7665 if (TREE_CODE (arg) == NEGATE_EXPR
7666 || TREE_CODE (arg) == CONJ_EXPR)
7667 return build_call_expr (fndecl, 1, TREE_OPERAND (arg, 0));
7669 /* Don't do this when optimizing for size. */
7670 if (flag_unsafe_math_optimizations
7671 && optimize && optimize_function_for_speed_p (cfun))
7673 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7675 if (sqrtfn != NULL_TREE)
7677 tree rpart, ipart, result;
7679 arg = builtin_save_expr (arg);
7681 rpart = fold_build1 (REALPART_EXPR, type, arg);
7682 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7684 rpart = builtin_save_expr (rpart);
7685 ipart = builtin_save_expr (ipart);
7687 result = fold_build2 (PLUS_EXPR, type,
7688 fold_build2 (MULT_EXPR, type,
7690 fold_build2 (MULT_EXPR, type,
7693 return build_call_expr (sqrtfn, 1, result);
7700 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7701 Return NULL_TREE if no simplification can be made. */
7704 fold_builtin_sqrt (tree arg, tree type)
7707 enum built_in_function fcode;
7710 if (!validate_arg (arg, REAL_TYPE))
7713 /* Calculate the result when the argument is a constant. */
7714 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7717 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7718 fcode = builtin_mathfn_code (arg);
7719 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7721 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7722 arg = fold_build2 (MULT_EXPR, type,
7723 CALL_EXPR_ARG (arg, 0),
7724 build_real (type, dconsthalf));
7725 return build_call_expr (expfn, 1, arg);
7728 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7729 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7731 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7735 tree arg0 = CALL_EXPR_ARG (arg, 0);
7737 /* The inner root was either sqrt or cbrt. */
7738 /* This was a conditional expression but it triggered a bug
7740 REAL_VALUE_TYPE dconstroot;
7741 if (BUILTIN_SQRT_P (fcode))
7742 dconstroot = dconsthalf;
7744 dconstroot = dconst_third ();
7746 /* Adjust for the outer root. */
7747 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7748 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7749 tree_root = build_real (type, dconstroot);
7750 return build_call_expr (powfn, 2, arg0, tree_root);
7754 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7755 if (flag_unsafe_math_optimizations
7756 && (fcode == BUILT_IN_POW
7757 || fcode == BUILT_IN_POWF
7758 || fcode == BUILT_IN_POWL))
7760 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7761 tree arg0 = CALL_EXPR_ARG (arg, 0);
7762 tree arg1 = CALL_EXPR_ARG (arg, 1);
7764 if (!tree_expr_nonnegative_p (arg0))
7765 arg0 = build1 (ABS_EXPR, type, arg0);
7766 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7767 build_real (type, dconsthalf));
7768 return build_call_expr (powfn, 2, arg0, narg1);
7774 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7775 Return NULL_TREE if no simplification can be made. */
7778 fold_builtin_cbrt (tree arg, tree type)
7780 const enum built_in_function fcode = builtin_mathfn_code (arg);
7783 if (!validate_arg (arg, REAL_TYPE))
7786 /* Calculate the result when the argument is a constant. */
7787 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7790 if (flag_unsafe_math_optimizations)
7792 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7793 if (BUILTIN_EXPONENT_P (fcode))
7795 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7796 const REAL_VALUE_TYPE third_trunc =
7797 real_value_truncate (TYPE_MODE (type), dconst_third ());
7798 arg = fold_build2 (MULT_EXPR, type,
7799 CALL_EXPR_ARG (arg, 0),
7800 build_real (type, third_trunc));
7801 return build_call_expr (expfn, 1, arg);
7804 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7805 if (BUILTIN_SQRT_P (fcode))
7807 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7811 tree arg0 = CALL_EXPR_ARG (arg, 0);
7813 REAL_VALUE_TYPE dconstroot = dconst_third ();
7815 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7816 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7817 tree_root = build_real (type, dconstroot);
7818 return build_call_expr (powfn, 2, arg0, tree_root);
7822 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7823 if (BUILTIN_CBRT_P (fcode))
7825 tree arg0 = CALL_EXPR_ARG (arg, 0);
7826 if (tree_expr_nonnegative_p (arg0))
7828 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7833 REAL_VALUE_TYPE dconstroot;
7835 real_arithmetic (&dconstroot, MULT_EXPR,
7836 dconst_third_ptr (), dconst_third_ptr ());
7837 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7838 tree_root = build_real (type, dconstroot);
7839 return build_call_expr (powfn, 2, arg0, tree_root);
7844 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7845 if (fcode == BUILT_IN_POW
7846 || fcode == BUILT_IN_POWF
7847 || fcode == BUILT_IN_POWL)
7849 tree arg00 = CALL_EXPR_ARG (arg, 0);
7850 tree arg01 = CALL_EXPR_ARG (arg, 1);
7851 if (tree_expr_nonnegative_p (arg00))
7853 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7854 const REAL_VALUE_TYPE dconstroot
7855 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7856 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7857 build_real (type, dconstroot));
7858 return build_call_expr (powfn, 2, arg00, narg01);
7865 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7866 TYPE is the type of the return value. Return NULL_TREE if no
7867 simplification can be made. */
7870 fold_builtin_cos (tree arg, tree type, tree fndecl)
7874 if (!validate_arg (arg, REAL_TYPE))
7877 /* Calculate the result when the argument is a constant. */
7878 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7881 /* Optimize cos(-x) into cos (x). */
7882 if ((narg = fold_strip_sign_ops (arg)))
7883 return build_call_expr (fndecl, 1, narg);
7888 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7889 Return NULL_TREE if no simplification can be made. */
7892 fold_builtin_cosh (tree arg, tree type, tree fndecl)
7894 if (validate_arg (arg, REAL_TYPE))
7898 /* Calculate the result when the argument is a constant. */
7899 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7902 /* Optimize cosh(-x) into cosh (x). */
7903 if ((narg = fold_strip_sign_ops (arg)))
7904 return build_call_expr (fndecl, 1, narg);
7910 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7911 argument ARG. TYPE is the type of the return value. Return
7912 NULL_TREE if no simplification can be made. */
7915 fold_builtin_ccos (tree arg, tree type ATTRIBUTE_UNUSED, tree fndecl,
7916 bool hyper ATTRIBUTE_UNUSED)
7918 if (validate_arg (arg, COMPLEX_TYPE)
7919 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7924 /* Calculate the result when the argument is a constant. */
7925 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7929 /* Optimize fn(-x) into fn(x). */
7930 if ((tmp = fold_strip_sign_ops (arg)))
7931 return build_call_expr (fndecl, 1, tmp);
7937 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7938 Return NULL_TREE if no simplification can be made. */
7941 fold_builtin_tan (tree arg, tree type)
7943 enum built_in_function fcode;
7946 if (!validate_arg (arg, REAL_TYPE))
7949 /* Calculate the result when the argument is a constant. */
7950 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7953 /* Optimize tan(atan(x)) = x. */
7954 fcode = builtin_mathfn_code (arg);
7955 if (flag_unsafe_math_optimizations
7956 && (fcode == BUILT_IN_ATAN
7957 || fcode == BUILT_IN_ATANF
7958 || fcode == BUILT_IN_ATANL))
7959 return CALL_EXPR_ARG (arg, 0);
7964 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7965 NULL_TREE if no simplification can be made. */
7968 fold_builtin_sincos (tree arg0, tree arg1, tree arg2)
7973 if (!validate_arg (arg0, REAL_TYPE)
7974 || !validate_arg (arg1, POINTER_TYPE)
7975 || !validate_arg (arg2, POINTER_TYPE))
7978 type = TREE_TYPE (arg0);
7980 /* Calculate the result when the argument is a constant. */
7981 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7984 /* Canonicalize sincos to cexpi. */
7985 if (!TARGET_C99_FUNCTIONS)
7987 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7991 call = build_call_expr (fn, 1, arg0);
7992 call = builtin_save_expr (call);
7994 return build2 (COMPOUND_EXPR, void_type_node,
7995 build2 (MODIFY_EXPR, void_type_node,
7996 build_fold_indirect_ref (arg1),
7997 build1 (IMAGPART_EXPR, type, call)),
7998 build2 (MODIFY_EXPR, void_type_node,
7999 build_fold_indirect_ref (arg2),
8000 build1 (REALPART_EXPR, type, call)));
8003 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8004 NULL_TREE if no simplification can be made. */
8007 fold_builtin_cexp (tree arg0, tree type)
8010 tree realp, imagp, ifn;
8015 if (!validate_arg (arg0, COMPLEX_TYPE)
8016 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8020 /* Calculate the result when the argument is a constant. */
8021 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
8025 rtype = TREE_TYPE (TREE_TYPE (arg0));
8027 /* In case we can figure out the real part of arg0 and it is constant zero
8029 if (!TARGET_C99_FUNCTIONS)
8031 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
8035 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
8036 && real_zerop (realp))
8038 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
8039 return build_call_expr (ifn, 1, narg);
8042 /* In case we can easily decompose real and imaginary parts split cexp
8043 to exp (r) * cexpi (i). */
8044 if (flag_unsafe_math_optimizations
8047 tree rfn, rcall, icall;
8049 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
8053 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
8057 icall = build_call_expr (ifn, 1, imagp);
8058 icall = builtin_save_expr (icall);
8059 rcall = build_call_expr (rfn, 1, realp);
8060 rcall = builtin_save_expr (rcall);
8061 return fold_build2 (COMPLEX_EXPR, type,
8062 fold_build2 (MULT_EXPR, rtype,
8064 fold_build1 (REALPART_EXPR, rtype, icall)),
8065 fold_build2 (MULT_EXPR, rtype,
8067 fold_build1 (IMAGPART_EXPR, rtype, icall)));
8073 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8074 Return NULL_TREE if no simplification can be made. */
8077 fold_builtin_trunc (tree fndecl, tree arg)
8079 if (!validate_arg (arg, REAL_TYPE))
8082 /* Optimize trunc of constant value. */
8083 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8085 REAL_VALUE_TYPE r, x;
8086 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8088 x = TREE_REAL_CST (arg);
8089 real_trunc (&r, TYPE_MODE (type), &x);
8090 return build_real (type, r);
8093 return fold_trunc_transparent_mathfn (fndecl, arg);
8096 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8097 Return NULL_TREE if no simplification can be made. */
8100 fold_builtin_floor (tree fndecl, tree arg)
8102 if (!validate_arg (arg, REAL_TYPE))
8105 /* Optimize floor of constant value. */
8106 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8110 x = TREE_REAL_CST (arg);
8111 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8113 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8116 real_floor (&r, TYPE_MODE (type), &x);
8117 return build_real (type, r);
8121 /* Fold floor (x) where x is nonnegative to trunc (x). */
8122 if (tree_expr_nonnegative_p (arg))
8124 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8126 return build_call_expr (truncfn, 1, arg);
8129 return fold_trunc_transparent_mathfn (fndecl, arg);
8132 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8133 Return NULL_TREE if no simplification can be made. */
8136 fold_builtin_ceil (tree fndecl, tree arg)
8138 if (!validate_arg (arg, REAL_TYPE))
8141 /* Optimize ceil of constant value. */
8142 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8146 x = TREE_REAL_CST (arg);
8147 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8149 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8152 real_ceil (&r, TYPE_MODE (type), &x);
8153 return build_real (type, r);
8157 return fold_trunc_transparent_mathfn (fndecl, arg);
8160 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8161 Return NULL_TREE if no simplification can be made. */
8164 fold_builtin_round (tree fndecl, tree arg)
8166 if (!validate_arg (arg, REAL_TYPE))
8169 /* Optimize round of constant value. */
8170 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8174 x = TREE_REAL_CST (arg);
8175 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8177 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8180 real_round (&r, TYPE_MODE (type), &x);
8181 return build_real (type, r);
8185 return fold_trunc_transparent_mathfn (fndecl, arg);
8188 /* Fold function call to builtin lround, lroundf or lroundl (or the
8189 corresponding long long versions) and other rounding functions. ARG
8190 is the argument to the call. Return NULL_TREE if no simplification
8194 fold_builtin_int_roundingfn (tree fndecl, tree arg)
8196 if (!validate_arg (arg, REAL_TYPE))
8199 /* Optimize lround of constant value. */
8200 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8202 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8204 if (real_isfinite (&x))
8206 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8207 tree ftype = TREE_TYPE (arg);
8208 unsigned HOST_WIDE_INT lo2;
8209 HOST_WIDE_INT hi, lo;
8212 switch (DECL_FUNCTION_CODE (fndecl))
8214 CASE_FLT_FN (BUILT_IN_LFLOOR):
8215 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8216 real_floor (&r, TYPE_MODE (ftype), &x);
8219 CASE_FLT_FN (BUILT_IN_LCEIL):
8220 CASE_FLT_FN (BUILT_IN_LLCEIL):
8221 real_ceil (&r, TYPE_MODE (ftype), &x);
8224 CASE_FLT_FN (BUILT_IN_LROUND):
8225 CASE_FLT_FN (BUILT_IN_LLROUND):
8226 real_round (&r, TYPE_MODE (ftype), &x);
8233 REAL_VALUE_TO_INT (&lo, &hi, r);
8234 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
8235 return build_int_cst_wide (itype, lo2, hi);
8239 switch (DECL_FUNCTION_CODE (fndecl))
8241 CASE_FLT_FN (BUILT_IN_LFLOOR):
8242 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8243 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8244 if (tree_expr_nonnegative_p (arg))
8245 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
8251 return fold_fixed_mathfn (fndecl, arg);
8254 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8255 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8256 the argument to the call. Return NULL_TREE if no simplification can
8260 fold_builtin_bitop (tree fndecl, tree arg)
8262 if (!validate_arg (arg, INTEGER_TYPE))
8265 /* Optimize for constant argument. */
8266 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8268 HOST_WIDE_INT hi, width, result;
8269 unsigned HOST_WIDE_INT lo;
8272 type = TREE_TYPE (arg);
8273 width = TYPE_PRECISION (type);
8274 lo = TREE_INT_CST_LOW (arg);
8276 /* Clear all the bits that are beyond the type's precision. */
8277 if (width > HOST_BITS_PER_WIDE_INT)
8279 hi = TREE_INT_CST_HIGH (arg);
8280 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8281 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
8286 if (width < HOST_BITS_PER_WIDE_INT)
8287 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8290 switch (DECL_FUNCTION_CODE (fndecl))
8292 CASE_INT_FN (BUILT_IN_FFS):
8294 result = exact_log2 (lo & -lo) + 1;
8296 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
8301 CASE_INT_FN (BUILT_IN_CLZ):
8303 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8305 result = width - floor_log2 (lo) - 1;
8306 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8310 CASE_INT_FN (BUILT_IN_CTZ):
8312 result = exact_log2 (lo & -lo);
8314 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
8315 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8319 CASE_INT_FN (BUILT_IN_POPCOUNT):
8322 result++, lo &= lo - 1;
8324 result++, hi &= hi - 1;
8327 CASE_INT_FN (BUILT_IN_PARITY):
8330 result++, lo &= lo - 1;
8332 result++, hi &= hi - 1;
8340 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8346 /* Fold function call to builtin_bswap and the long and long long
8347 variants. Return NULL_TREE if no simplification can be made. */
8349 fold_builtin_bswap (tree fndecl, tree arg)
8351 if (! validate_arg (arg, INTEGER_TYPE))
8354 /* Optimize constant value. */
8355 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8357 HOST_WIDE_INT hi, width, r_hi = 0;
8358 unsigned HOST_WIDE_INT lo, r_lo = 0;
8361 type = TREE_TYPE (arg);
8362 width = TYPE_PRECISION (type);
8363 lo = TREE_INT_CST_LOW (arg);
8364 hi = TREE_INT_CST_HIGH (arg);
8366 switch (DECL_FUNCTION_CODE (fndecl))
8368 case BUILT_IN_BSWAP32:
8369 case BUILT_IN_BSWAP64:
8373 for (s = 0; s < width; s += 8)
8375 int d = width - s - 8;
8376 unsigned HOST_WIDE_INT byte;
8378 if (s < HOST_BITS_PER_WIDE_INT)
8379 byte = (lo >> s) & 0xff;
8381 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8383 if (d < HOST_BITS_PER_WIDE_INT)
8386 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8396 if (width < HOST_BITS_PER_WIDE_INT)
8397 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8399 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8405 /* A subroutine of fold_builtin to fold the various logarithmic
8406 functions. Return NULL_TREE if no simplification can me made.
8407 FUNC is the corresponding MPFR logarithm function. */
8410 fold_builtin_logarithm (tree fndecl, tree arg,
8411 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8413 if (validate_arg (arg, REAL_TYPE))
8415 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8417 const enum built_in_function fcode = builtin_mathfn_code (arg);
8419 /* Calculate the result when the argument is a constant. */
8420 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8423 /* Special case, optimize logN(expN(x)) = x. */
8424 if (flag_unsafe_math_optimizations
8425 && ((func == mpfr_log
8426 && (fcode == BUILT_IN_EXP
8427 || fcode == BUILT_IN_EXPF
8428 || fcode == BUILT_IN_EXPL))
8429 || (func == mpfr_log2
8430 && (fcode == BUILT_IN_EXP2
8431 || fcode == BUILT_IN_EXP2F
8432 || fcode == BUILT_IN_EXP2L))
8433 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8434 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8436 /* Optimize logN(func()) for various exponential functions. We
8437 want to determine the value "x" and the power "exponent" in
8438 order to transform logN(x**exponent) into exponent*logN(x). */
8439 if (flag_unsafe_math_optimizations)
8441 tree exponent = 0, x = 0;
8445 CASE_FLT_FN (BUILT_IN_EXP):
8446 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8447 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8449 exponent = CALL_EXPR_ARG (arg, 0);
8451 CASE_FLT_FN (BUILT_IN_EXP2):
8452 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8453 x = build_real (type, dconst2);
8454 exponent = CALL_EXPR_ARG (arg, 0);
8456 CASE_FLT_FN (BUILT_IN_EXP10):
8457 CASE_FLT_FN (BUILT_IN_POW10):
8458 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8460 REAL_VALUE_TYPE dconst10;
8461 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8462 x = build_real (type, dconst10);
8464 exponent = CALL_EXPR_ARG (arg, 0);
8466 CASE_FLT_FN (BUILT_IN_SQRT):
8467 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8468 x = CALL_EXPR_ARG (arg, 0);
8469 exponent = build_real (type, dconsthalf);
8471 CASE_FLT_FN (BUILT_IN_CBRT):
8472 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8473 x = CALL_EXPR_ARG (arg, 0);
8474 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8477 CASE_FLT_FN (BUILT_IN_POW):
8478 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8479 x = CALL_EXPR_ARG (arg, 0);
8480 exponent = CALL_EXPR_ARG (arg, 1);
8486 /* Now perform the optimization. */
8489 tree logfn = build_call_expr (fndecl, 1, x);
8490 return fold_build2 (MULT_EXPR, type, exponent, logfn);
8498 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8499 NULL_TREE if no simplification can be made. */
8502 fold_builtin_hypot (tree fndecl, tree arg0, tree arg1, tree type)
8504 tree res, narg0, narg1;
8506 if (!validate_arg (arg0, REAL_TYPE)
8507 || !validate_arg (arg1, REAL_TYPE))
8510 /* Calculate the result when the argument is a constant. */
8511 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8514 /* If either argument to hypot has a negate or abs, strip that off.
8515 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8516 narg0 = fold_strip_sign_ops (arg0);
8517 narg1 = fold_strip_sign_ops (arg1);
8520 return build_call_expr (fndecl, 2, narg0 ? narg0 : arg0,
8521 narg1 ? narg1 : arg1);
8524 /* If either argument is zero, hypot is fabs of the other. */
8525 if (real_zerop (arg0))
8526 return fold_build1 (ABS_EXPR, type, arg1);
8527 else if (real_zerop (arg1))
8528 return fold_build1 (ABS_EXPR, type, arg0);
8530 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8531 if (flag_unsafe_math_optimizations
8532 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8534 const REAL_VALUE_TYPE sqrt2_trunc
8535 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8536 return fold_build2 (MULT_EXPR, type,
8537 fold_build1 (ABS_EXPR, type, arg0),
8538 build_real (type, sqrt2_trunc));
8545 /* Fold a builtin function call to pow, powf, or powl. Return
8546 NULL_TREE if no simplification can be made. */
8548 fold_builtin_pow (tree fndecl, tree arg0, tree arg1, tree type)
8552 if (!validate_arg (arg0, REAL_TYPE)
8553 || !validate_arg (arg1, REAL_TYPE))
8556 /* Calculate the result when the argument is a constant. */
8557 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8560 /* Optimize pow(1.0,y) = 1.0. */
8561 if (real_onep (arg0))
8562 return omit_one_operand (type, build_real (type, dconst1), arg1);
8564 if (TREE_CODE (arg1) == REAL_CST
8565 && !TREE_OVERFLOW (arg1))
8567 REAL_VALUE_TYPE cint;
8571 c = TREE_REAL_CST (arg1);
8573 /* Optimize pow(x,0.0) = 1.0. */
8574 if (REAL_VALUES_EQUAL (c, dconst0))
8575 return omit_one_operand (type, build_real (type, dconst1),
8578 /* Optimize pow(x,1.0) = x. */
8579 if (REAL_VALUES_EQUAL (c, dconst1))
8582 /* Optimize pow(x,-1.0) = 1.0/x. */
8583 if (REAL_VALUES_EQUAL (c, dconstm1))
8584 return fold_build2 (RDIV_EXPR, type,
8585 build_real (type, dconst1), arg0);
8587 /* Optimize pow(x,0.5) = sqrt(x). */
8588 if (flag_unsafe_math_optimizations
8589 && REAL_VALUES_EQUAL (c, dconsthalf))
8591 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8593 if (sqrtfn != NULL_TREE)
8594 return build_call_expr (sqrtfn, 1, arg0);
8597 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8598 if (flag_unsafe_math_optimizations)
8600 const REAL_VALUE_TYPE dconstroot
8601 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8603 if (REAL_VALUES_EQUAL (c, dconstroot))
8605 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8606 if (cbrtfn != NULL_TREE)
8607 return build_call_expr (cbrtfn, 1, arg0);
8611 /* Check for an integer exponent. */
8612 n = real_to_integer (&c);
8613 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8614 if (real_identical (&c, &cint))
8616 /* Attempt to evaluate pow at compile-time, unless this should
8617 raise an exception. */
8618 if (TREE_CODE (arg0) == REAL_CST
8619 && !TREE_OVERFLOW (arg0)
8621 || (!flag_trapping_math && !flag_errno_math)
8622 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8627 x = TREE_REAL_CST (arg0);
8628 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8629 if (flag_unsafe_math_optimizations || !inexact)
8630 return build_real (type, x);
8633 /* Strip sign ops from even integer powers. */
8634 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8636 tree narg0 = fold_strip_sign_ops (arg0);
8638 return build_call_expr (fndecl, 2, narg0, arg1);
8643 if (flag_unsafe_math_optimizations)
8645 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8647 /* Optimize pow(expN(x),y) = expN(x*y). */
8648 if (BUILTIN_EXPONENT_P (fcode))
8650 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8651 tree arg = CALL_EXPR_ARG (arg0, 0);
8652 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8653 return build_call_expr (expfn, 1, arg);
8656 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8657 if (BUILTIN_SQRT_P (fcode))
8659 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8660 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8661 build_real (type, dconsthalf));
8662 return build_call_expr (fndecl, 2, narg0, narg1);
8665 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8666 if (BUILTIN_CBRT_P (fcode))
8668 tree arg = CALL_EXPR_ARG (arg0, 0);
8669 if (tree_expr_nonnegative_p (arg))
8671 const REAL_VALUE_TYPE dconstroot
8672 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8673 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8674 build_real (type, dconstroot));
8675 return build_call_expr (fndecl, 2, arg, narg1);
8679 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8680 if (fcode == BUILT_IN_POW
8681 || fcode == BUILT_IN_POWF
8682 || fcode == BUILT_IN_POWL)
8684 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8685 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8686 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8687 return build_call_expr (fndecl, 2, arg00, narg1);
8694 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8695 Return NULL_TREE if no simplification can be made. */
8697 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED,
8698 tree arg0, tree arg1, tree type)
8700 if (!validate_arg (arg0, REAL_TYPE)
8701 || !validate_arg (arg1, INTEGER_TYPE))
8704 /* Optimize pow(1.0,y) = 1.0. */
8705 if (real_onep (arg0))
8706 return omit_one_operand (type, build_real (type, dconst1), arg1);
8708 if (host_integerp (arg1, 0))
8710 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8712 /* Evaluate powi at compile-time. */
8713 if (TREE_CODE (arg0) == REAL_CST
8714 && !TREE_OVERFLOW (arg0))
8717 x = TREE_REAL_CST (arg0);
8718 real_powi (&x, TYPE_MODE (type), &x, c);
8719 return build_real (type, x);
8722 /* Optimize pow(x,0) = 1.0. */
8724 return omit_one_operand (type, build_real (type, dconst1),
8727 /* Optimize pow(x,1) = x. */
8731 /* Optimize pow(x,-1) = 1.0/x. */
8733 return fold_build2 (RDIV_EXPR, type,
8734 build_real (type, dconst1), arg0);
8740 /* A subroutine of fold_builtin to fold the various exponent
8741 functions. Return NULL_TREE if no simplification can be made.
8742 FUNC is the corresponding MPFR exponent function. */
8745 fold_builtin_exponent (tree fndecl, tree arg,
8746 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8748 if (validate_arg (arg, REAL_TYPE))
8750 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8753 /* Calculate the result when the argument is a constant. */
8754 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8757 /* Optimize expN(logN(x)) = x. */
8758 if (flag_unsafe_math_optimizations)
8760 const enum built_in_function fcode = builtin_mathfn_code (arg);
8762 if ((func == mpfr_exp
8763 && (fcode == BUILT_IN_LOG
8764 || fcode == BUILT_IN_LOGF
8765 || fcode == BUILT_IN_LOGL))
8766 || (func == mpfr_exp2
8767 && (fcode == BUILT_IN_LOG2
8768 || fcode == BUILT_IN_LOG2F
8769 || fcode == BUILT_IN_LOG2L))
8770 || (func == mpfr_exp10
8771 && (fcode == BUILT_IN_LOG10
8772 || fcode == BUILT_IN_LOG10F
8773 || fcode == BUILT_IN_LOG10L)))
8774 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8781 /* Return true if VAR is a VAR_DECL or a component thereof. */
8784 var_decl_component_p (tree var)
8787 while (handled_component_p (inner))
8788 inner = TREE_OPERAND (inner, 0);
8789 return SSA_VAR_P (inner);
8792 /* Fold function call to builtin memset. Return
8793 NULL_TREE if no simplification can be made. */
8796 fold_builtin_memset (tree dest, tree c, tree len, tree type, bool ignore)
8798 tree var, ret, etype;
8799 unsigned HOST_WIDE_INT length, cval;
8801 if (! validate_arg (dest, POINTER_TYPE)
8802 || ! validate_arg (c, INTEGER_TYPE)
8803 || ! validate_arg (len, INTEGER_TYPE))
8806 if (! host_integerp (len, 1))
8809 /* If the LEN parameter is zero, return DEST. */
8810 if (integer_zerop (len))
8811 return omit_one_operand (type, dest, c);
8813 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8818 if (TREE_CODE (var) != ADDR_EXPR)
8821 var = TREE_OPERAND (var, 0);
8822 if (TREE_THIS_VOLATILE (var))
8825 etype = TREE_TYPE (var);
8826 if (TREE_CODE (etype) == ARRAY_TYPE)
8827 etype = TREE_TYPE (etype);
8829 if (!INTEGRAL_TYPE_P (etype)
8830 && !POINTER_TYPE_P (etype))
8833 if (! var_decl_component_p (var))
8836 length = tree_low_cst (len, 1);
8837 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8838 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8842 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8845 if (integer_zerop (c))
8849 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8852 cval = tree_low_cst (c, 1);
8856 cval |= (cval << 31) << 1;
8859 ret = build_int_cst_type (etype, cval);
8860 var = build_fold_indirect_ref (fold_convert (build_pointer_type (etype),
8862 ret = build2 (MODIFY_EXPR, etype, var, ret);
8866 return omit_one_operand (type, dest, ret);
8869 /* Fold function call to builtin memset. Return
8870 NULL_TREE if no simplification can be made. */
8873 fold_builtin_bzero (tree dest, tree size, bool ignore)
8875 if (! validate_arg (dest, POINTER_TYPE)
8876 || ! validate_arg (size, INTEGER_TYPE))
8882 /* New argument list transforming bzero(ptr x, int y) to
8883 memset(ptr x, int 0, size_t y). This is done this way
8884 so that if it isn't expanded inline, we fallback to
8885 calling bzero instead of memset. */
8887 return fold_builtin_memset (dest, integer_zero_node,
8888 fold_convert (sizetype, size),
8889 void_type_node, ignore);
8892 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8893 NULL_TREE if no simplification can be made.
8894 If ENDP is 0, return DEST (like memcpy).
8895 If ENDP is 1, return DEST+LEN (like mempcpy).
8896 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8897 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8901 fold_builtin_memory_op (tree dest, tree src, tree len, tree type, bool ignore, int endp)
8903 tree destvar, srcvar, expr;
8905 if (! validate_arg (dest, POINTER_TYPE)
8906 || ! validate_arg (src, POINTER_TYPE)
8907 || ! validate_arg (len, INTEGER_TYPE))
8910 /* If the LEN parameter is zero, return DEST. */
8911 if (integer_zerop (len))
8912 return omit_one_operand (type, dest, src);
8914 /* If SRC and DEST are the same (and not volatile), return
8915 DEST{,+LEN,+LEN-1}. */
8916 if (operand_equal_p (src, dest, 0))
8920 tree srctype, desttype;
8921 int src_align, dest_align;
8925 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8926 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8928 /* Both DEST and SRC must be pointer types.
8929 ??? This is what old code did. Is the testing for pointer types
8932 If either SRC is readonly or length is 1, we can use memcpy. */
8933 if (!dest_align || !src_align)
8935 if (readonly_data_expr (src)
8936 || (host_integerp (len, 1)
8937 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8938 >= tree_low_cst (len, 1))))
8940 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8943 return build_call_expr (fn, 3, dest, src, len);
8946 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8947 srcvar = build_fold_indirect_ref (src);
8948 destvar = build_fold_indirect_ref (dest);
8950 && !TREE_THIS_VOLATILE (srcvar)
8952 && !TREE_THIS_VOLATILE (destvar))
8954 tree src_base, dest_base, fn;
8955 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8956 HOST_WIDE_INT size = -1;
8957 HOST_WIDE_INT maxsize = -1;
8960 if (handled_component_p (src_base))
8961 src_base = get_ref_base_and_extent (src_base, &src_offset,
8963 dest_base = destvar;
8964 if (handled_component_p (dest_base))
8965 dest_base = get_ref_base_and_extent (dest_base, &dest_offset,
8967 if (host_integerp (len, 1))
8969 maxsize = tree_low_cst (len, 1);
8971 > INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT)
8974 maxsize *= BITS_PER_UNIT;
8978 if (SSA_VAR_P (src_base)
8979 && SSA_VAR_P (dest_base))
8981 if (operand_equal_p (src_base, dest_base, 0)
8982 && ranges_overlap_p (src_offset, maxsize,
8983 dest_offset, maxsize))
8986 else if (TREE_CODE (src_base) == INDIRECT_REF
8987 && TREE_CODE (dest_base) == INDIRECT_REF)
8989 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8990 TREE_OPERAND (dest_base, 0), 0)
8991 || ranges_overlap_p (src_offset, maxsize,
8992 dest_offset, maxsize))
8998 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9001 return build_call_expr (fn, 3, dest, src, len);
9006 if (!host_integerp (len, 0))
9009 This logic lose for arguments like (type *)malloc (sizeof (type)),
9010 since we strip the casts of up to VOID return value from malloc.
9011 Perhaps we ought to inherit type from non-VOID argument here? */
9014 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
9015 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
9017 tree tem = TREE_OPERAND (src, 0);
9019 if (tem != TREE_OPERAND (src, 0))
9020 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
9022 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
9024 tree tem = TREE_OPERAND (dest, 0);
9026 if (tem != TREE_OPERAND (dest, 0))
9027 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
9029 srctype = TREE_TYPE (TREE_TYPE (src));
9031 && TREE_CODE (srctype) == ARRAY_TYPE
9032 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
9034 srctype = TREE_TYPE (srctype);
9036 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
9038 desttype = TREE_TYPE (TREE_TYPE (dest));
9040 && TREE_CODE (desttype) == ARRAY_TYPE
9041 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
9043 desttype = TREE_TYPE (desttype);
9045 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
9047 if (!srctype || !desttype
9048 || !TYPE_SIZE_UNIT (srctype)
9049 || !TYPE_SIZE_UNIT (desttype)
9050 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
9051 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
9052 || TYPE_VOLATILE (srctype)
9053 || TYPE_VOLATILE (desttype))
9056 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
9057 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
9058 if (dest_align < (int) TYPE_ALIGN (desttype)
9059 || src_align < (int) TYPE_ALIGN (srctype))
9063 dest = builtin_save_expr (dest);
9066 if (tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
9068 srcvar = build_fold_indirect_ref (src);
9069 if (TREE_THIS_VOLATILE (srcvar))
9071 else if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
9073 /* With memcpy, it is possible to bypass aliasing rules, so without
9074 this check i.e. execute/20060930-2.c would be misoptimized,
9075 because it use conflicting alias set to hold argument for the
9076 memcpy call. This check is probably unnecessary with
9077 -fno-strict-aliasing. Similarly for destvar. See also
9079 else if (!var_decl_component_p (srcvar))
9083 destvar = NULL_TREE;
9084 if (tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
9086 destvar = build_fold_indirect_ref (dest);
9087 if (TREE_THIS_VOLATILE (destvar))
9089 else if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
9090 destvar = NULL_TREE;
9091 else if (!var_decl_component_p (destvar))
9092 destvar = NULL_TREE;
9095 if (srcvar == NULL_TREE && destvar == NULL_TREE)
9098 if (srcvar == NULL_TREE)
9101 if (TREE_ADDRESSABLE (TREE_TYPE (destvar)))
9104 srctype = build_qualified_type (desttype, 0);
9105 if (src_align < (int) TYPE_ALIGN (srctype))
9107 if (AGGREGATE_TYPE_P (srctype)
9108 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (srctype), src_align))
9111 srctype = build_variant_type_copy (srctype);
9112 TYPE_ALIGN (srctype) = src_align;
9113 TYPE_USER_ALIGN (srctype) = 1;
9114 TYPE_PACKED (srctype) = 1;
9116 srcptype = build_pointer_type_for_mode (srctype, ptr_mode, true);
9117 src = fold_convert (srcptype, src);
9118 srcvar = build_fold_indirect_ref (src);
9120 else if (destvar == NULL_TREE)
9123 if (TREE_ADDRESSABLE (TREE_TYPE (srcvar)))
9126 desttype = build_qualified_type (srctype, 0);
9127 if (dest_align < (int) TYPE_ALIGN (desttype))
9129 if (AGGREGATE_TYPE_P (desttype)
9130 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (desttype), dest_align))
9133 desttype = build_variant_type_copy (desttype);
9134 TYPE_ALIGN (desttype) = dest_align;
9135 TYPE_USER_ALIGN (desttype) = 1;
9136 TYPE_PACKED (desttype) = 1;
9138 destptype = build_pointer_type_for_mode (desttype, ptr_mode, true);
9139 dest = fold_convert (destptype, dest);
9140 destvar = build_fold_indirect_ref (dest);
9143 if (srctype == desttype
9144 || (gimple_in_ssa_p (cfun)
9145 && useless_type_conversion_p (desttype, srctype)))
9147 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
9148 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
9149 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
9150 || POINTER_TYPE_P (TREE_TYPE (destvar))))
9151 expr = fold_convert (TREE_TYPE (destvar), srcvar);
9153 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
9154 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
9160 if (endp == 0 || endp == 3)
9161 return omit_one_operand (type, dest, expr);
9167 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
9170 len = fold_convert (sizetype, len);
9171 dest = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
9172 dest = fold_convert (type, dest);
9174 dest = omit_one_operand (type, dest, expr);
9178 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9179 If LEN is not NULL, it represents the length of the string to be
9180 copied. Return NULL_TREE if no simplification can be made. */
9183 fold_builtin_strcpy (tree fndecl, tree dest, tree src, tree len)
9187 if (!validate_arg (dest, POINTER_TYPE)
9188 || !validate_arg (src, POINTER_TYPE))
9191 /* If SRC and DEST are the same (and not volatile), return DEST. */
9192 if (operand_equal_p (src, dest, 0))
9193 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
9195 if (optimize_function_for_size_p (cfun))
9198 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9204 len = c_strlen (src, 1);
9205 if (! len || TREE_SIDE_EFFECTS (len))
9209 len = size_binop (PLUS_EXPR, len, ssize_int (1));
9210 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9211 build_call_expr (fn, 3, dest, src, len));
9214 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9215 If SLEN is not NULL, it represents the length of the source string.
9216 Return NULL_TREE if no simplification can be made. */
9219 fold_builtin_strncpy (tree fndecl, tree dest, tree src, tree len, tree slen)
9223 if (!validate_arg (dest, POINTER_TYPE)
9224 || !validate_arg (src, POINTER_TYPE)
9225 || !validate_arg (len, INTEGER_TYPE))
9228 /* If the LEN parameter is zero, return DEST. */
9229 if (integer_zerop (len))
9230 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9232 /* We can't compare slen with len as constants below if len is not a
9234 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9238 slen = c_strlen (src, 1);
9240 /* Now, we must be passed a constant src ptr parameter. */
9241 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9244 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
9246 /* We do not support simplification of this case, though we do
9247 support it when expanding trees into RTL. */
9248 /* FIXME: generate a call to __builtin_memset. */
9249 if (tree_int_cst_lt (slen, len))
9252 /* OK transform into builtin memcpy. */
9253 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9256 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9257 build_call_expr (fn, 3, dest, src, len));
9260 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9261 arguments to the call, and TYPE is its return type.
9262 Return NULL_TREE if no simplification can be made. */
9265 fold_builtin_memchr (tree arg1, tree arg2, tree len, tree type)
9267 if (!validate_arg (arg1, POINTER_TYPE)
9268 || !validate_arg (arg2, INTEGER_TYPE)
9269 || !validate_arg (len, INTEGER_TYPE))
9275 if (TREE_CODE (arg2) != INTEGER_CST
9276 || !host_integerp (len, 1))
9279 p1 = c_getstr (arg1);
9280 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9286 if (target_char_cast (arg2, &c))
9289 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
9292 return build_int_cst (TREE_TYPE (arg1), 0);
9294 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
9296 return fold_convert (type, tem);
9302 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9303 Return NULL_TREE if no simplification can be made. */
9306 fold_builtin_memcmp (tree arg1, tree arg2, tree len)
9308 const char *p1, *p2;
9310 if (!validate_arg (arg1, POINTER_TYPE)
9311 || !validate_arg (arg2, POINTER_TYPE)
9312 || !validate_arg (len, INTEGER_TYPE))
9315 /* If the LEN parameter is zero, return zero. */
9316 if (integer_zerop (len))
9317 return omit_two_operands (integer_type_node, integer_zero_node,
9320 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9321 if (operand_equal_p (arg1, arg2, 0))
9322 return omit_one_operand (integer_type_node, integer_zero_node, len);
9324 p1 = c_getstr (arg1);
9325 p2 = c_getstr (arg2);
9327 /* If all arguments are constant, and the value of len is not greater
9328 than the lengths of arg1 and arg2, evaluate at compile-time. */
9329 if (host_integerp (len, 1) && p1 && p2
9330 && compare_tree_int (len, strlen (p1) + 1) <= 0
9331 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9333 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9336 return integer_one_node;
9338 return integer_minus_one_node;
9340 return integer_zero_node;
9343 /* If len parameter is one, return an expression corresponding to
9344 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9345 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9347 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9348 tree cst_uchar_ptr_node
9349 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9351 tree ind1 = fold_convert (integer_type_node,
9352 build1 (INDIRECT_REF, cst_uchar_node,
9353 fold_convert (cst_uchar_ptr_node,
9355 tree ind2 = fold_convert (integer_type_node,
9356 build1 (INDIRECT_REF, cst_uchar_node,
9357 fold_convert (cst_uchar_ptr_node,
9359 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9365 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9366 Return NULL_TREE if no simplification can be made. */
9369 fold_builtin_strcmp (tree arg1, tree arg2)
9371 const char *p1, *p2;
9373 if (!validate_arg (arg1, POINTER_TYPE)
9374 || !validate_arg (arg2, POINTER_TYPE))
9377 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9378 if (operand_equal_p (arg1, arg2, 0))
9379 return integer_zero_node;
9381 p1 = c_getstr (arg1);
9382 p2 = c_getstr (arg2);
9386 const int i = strcmp (p1, p2);
9388 return integer_minus_one_node;
9390 return integer_one_node;
9392 return integer_zero_node;
9395 /* If the second arg is "", return *(const unsigned char*)arg1. */
9396 if (p2 && *p2 == '\0')
9398 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9399 tree cst_uchar_ptr_node
9400 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9402 return fold_convert (integer_type_node,
9403 build1 (INDIRECT_REF, cst_uchar_node,
9404 fold_convert (cst_uchar_ptr_node,
9408 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9409 if (p1 && *p1 == '\0')
9411 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9412 tree cst_uchar_ptr_node
9413 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9415 tree temp = fold_convert (integer_type_node,
9416 build1 (INDIRECT_REF, cst_uchar_node,
9417 fold_convert (cst_uchar_ptr_node,
9419 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9425 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9426 Return NULL_TREE if no simplification can be made. */
9429 fold_builtin_strncmp (tree arg1, tree arg2, tree len)
9431 const char *p1, *p2;
9433 if (!validate_arg (arg1, POINTER_TYPE)
9434 || !validate_arg (arg2, POINTER_TYPE)
9435 || !validate_arg (len, INTEGER_TYPE))
9438 /* If the LEN parameter is zero, return zero. */
9439 if (integer_zerop (len))
9440 return omit_two_operands (integer_type_node, integer_zero_node,
9443 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9444 if (operand_equal_p (arg1, arg2, 0))
9445 return omit_one_operand (integer_type_node, integer_zero_node, len);
9447 p1 = c_getstr (arg1);
9448 p2 = c_getstr (arg2);
9450 if (host_integerp (len, 1) && p1 && p2)
9452 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9454 return integer_one_node;
9456 return integer_minus_one_node;
9458 return integer_zero_node;
9461 /* If the second arg is "", and the length is greater than zero,
9462 return *(const unsigned char*)arg1. */
9463 if (p2 && *p2 == '\0'
9464 && TREE_CODE (len) == INTEGER_CST
9465 && tree_int_cst_sgn (len) == 1)
9467 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9468 tree cst_uchar_ptr_node
9469 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9471 return fold_convert (integer_type_node,
9472 build1 (INDIRECT_REF, cst_uchar_node,
9473 fold_convert (cst_uchar_ptr_node,
9477 /* If the first arg is "", and the length is greater than zero,
9478 return -*(const unsigned char*)arg2. */
9479 if (p1 && *p1 == '\0'
9480 && TREE_CODE (len) == INTEGER_CST
9481 && tree_int_cst_sgn (len) == 1)
9483 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9484 tree cst_uchar_ptr_node
9485 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9487 tree temp = fold_convert (integer_type_node,
9488 build1 (INDIRECT_REF, cst_uchar_node,
9489 fold_convert (cst_uchar_ptr_node,
9491 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9494 /* If len parameter is one, return an expression corresponding to
9495 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9496 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9498 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9499 tree cst_uchar_ptr_node
9500 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9502 tree ind1 = fold_convert (integer_type_node,
9503 build1 (INDIRECT_REF, cst_uchar_node,
9504 fold_convert (cst_uchar_ptr_node,
9506 tree ind2 = fold_convert (integer_type_node,
9507 build1 (INDIRECT_REF, cst_uchar_node,
9508 fold_convert (cst_uchar_ptr_node,
9510 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9516 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9517 ARG. Return NULL_TREE if no simplification can be made. */
9520 fold_builtin_signbit (tree arg, tree type)
9524 if (!validate_arg (arg, REAL_TYPE))
9527 /* If ARG is a compile-time constant, determine the result. */
9528 if (TREE_CODE (arg) == REAL_CST
9529 && !TREE_OVERFLOW (arg))
9533 c = TREE_REAL_CST (arg);
9534 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9535 return fold_convert (type, temp);
9538 /* If ARG is non-negative, the result is always zero. */
9539 if (tree_expr_nonnegative_p (arg))
9540 return omit_one_operand (type, integer_zero_node, arg);
9542 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9543 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9544 return fold_build2 (LT_EXPR, type, arg,
9545 build_real (TREE_TYPE (arg), dconst0));
9550 /* Fold function call to builtin copysign, copysignf or copysignl with
9551 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9555 fold_builtin_copysign (tree fndecl, tree arg1, tree arg2, tree type)
9559 if (!validate_arg (arg1, REAL_TYPE)
9560 || !validate_arg (arg2, REAL_TYPE))
9563 /* copysign(X,X) is X. */
9564 if (operand_equal_p (arg1, arg2, 0))
9565 return fold_convert (type, arg1);
9567 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9568 if (TREE_CODE (arg1) == REAL_CST
9569 && TREE_CODE (arg2) == REAL_CST
9570 && !TREE_OVERFLOW (arg1)
9571 && !TREE_OVERFLOW (arg2))
9573 REAL_VALUE_TYPE c1, c2;
9575 c1 = TREE_REAL_CST (arg1);
9576 c2 = TREE_REAL_CST (arg2);
9577 /* c1.sign := c2.sign. */
9578 real_copysign (&c1, &c2);
9579 return build_real (type, c1);
9582 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9583 Remember to evaluate Y for side-effects. */
9584 if (tree_expr_nonnegative_p (arg2))
9585 return omit_one_operand (type,
9586 fold_build1 (ABS_EXPR, type, arg1),
9589 /* Strip sign changing operations for the first argument. */
9590 tem = fold_strip_sign_ops (arg1);
9592 return build_call_expr (fndecl, 2, tem, arg2);
9597 /* Fold a call to builtin isascii with argument ARG. */
9600 fold_builtin_isascii (tree arg)
9602 if (!validate_arg (arg, INTEGER_TYPE))
9606 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9607 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
9608 build_int_cst (NULL_TREE,
9609 ~ (unsigned HOST_WIDE_INT) 0x7f));
9610 return fold_build2 (EQ_EXPR, integer_type_node,
9611 arg, integer_zero_node);
9615 /* Fold a call to builtin toascii with argument ARG. */
9618 fold_builtin_toascii (tree arg)
9620 if (!validate_arg (arg, INTEGER_TYPE))
9623 /* Transform toascii(c) -> (c & 0x7f). */
9624 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9625 build_int_cst (NULL_TREE, 0x7f));
9628 /* Fold a call to builtin isdigit with argument ARG. */
9631 fold_builtin_isdigit (tree arg)
9633 if (!validate_arg (arg, INTEGER_TYPE))
9637 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9638 /* According to the C standard, isdigit is unaffected by locale.
9639 However, it definitely is affected by the target character set. */
9640 unsigned HOST_WIDE_INT target_digit0
9641 = lang_hooks.to_target_charset ('0');
9643 if (target_digit0 == 0)
9646 arg = fold_convert (unsigned_type_node, arg);
9647 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
9648 build_int_cst (unsigned_type_node, target_digit0));
9649 return fold_build2 (LE_EXPR, integer_type_node, arg,
9650 build_int_cst (unsigned_type_node, 9));
9654 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9657 fold_builtin_fabs (tree arg, tree type)
9659 if (!validate_arg (arg, REAL_TYPE))
9662 arg = fold_convert (type, arg);
9663 if (TREE_CODE (arg) == REAL_CST)
9664 return fold_abs_const (arg, type);
9665 return fold_build1 (ABS_EXPR, type, arg);
9668 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9671 fold_builtin_abs (tree arg, tree type)
9673 if (!validate_arg (arg, INTEGER_TYPE))
9676 arg = fold_convert (type, arg);
9677 if (TREE_CODE (arg) == INTEGER_CST)
9678 return fold_abs_const (arg, type);
9679 return fold_build1 (ABS_EXPR, type, arg);
9682 /* Fold a call to builtin fmin or fmax. */
9685 fold_builtin_fmin_fmax (tree arg0, tree arg1, tree type, bool max)
9687 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9689 /* Calculate the result when the argument is a constant. */
9690 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9695 /* If either argument is NaN, return the other one. Avoid the
9696 transformation if we get (and honor) a signalling NaN. Using
9697 omit_one_operand() ensures we create a non-lvalue. */
9698 if (TREE_CODE (arg0) == REAL_CST
9699 && real_isnan (&TREE_REAL_CST (arg0))
9700 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9701 || ! TREE_REAL_CST (arg0).signalling))
9702 return omit_one_operand (type, arg1, arg0);
9703 if (TREE_CODE (arg1) == REAL_CST
9704 && real_isnan (&TREE_REAL_CST (arg1))
9705 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9706 || ! TREE_REAL_CST (arg1).signalling))
9707 return omit_one_operand (type, arg0, arg1);
9709 /* Transform fmin/fmax(x,x) -> x. */
9710 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9711 return omit_one_operand (type, arg0, arg1);
9713 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9714 functions to return the numeric arg if the other one is NaN.
9715 These tree codes don't honor that, so only transform if
9716 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9717 handled, so we don't have to worry about it either. */
9718 if (flag_finite_math_only)
9719 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9720 fold_convert (type, arg0),
9721 fold_convert (type, arg1));
9726 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9729 fold_builtin_carg (tree arg, tree type)
9731 if (validate_arg (arg, COMPLEX_TYPE)
9732 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9734 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9738 tree new_arg = builtin_save_expr (arg);
9739 tree r_arg = fold_build1 (REALPART_EXPR, type, new_arg);
9740 tree i_arg = fold_build1 (IMAGPART_EXPR, type, new_arg);
9741 return build_call_expr (atan2_fn, 2, i_arg, r_arg);
9748 /* Fold a call to builtin logb/ilogb. */
9751 fold_builtin_logb (tree arg, tree rettype)
9753 if (! validate_arg (arg, REAL_TYPE))
9758 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9760 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9766 /* If arg is Inf or NaN and we're logb, return it. */
9767 if (TREE_CODE (rettype) == REAL_TYPE)
9768 return fold_convert (rettype, arg);
9769 /* Fall through... */
9771 /* Zero may set errno and/or raise an exception for logb, also
9772 for ilogb we don't know FP_ILOGB0. */
9775 /* For normal numbers, proceed iff radix == 2. In GCC,
9776 normalized significands are in the range [0.5, 1.0). We
9777 want the exponent as if they were [1.0, 2.0) so get the
9778 exponent and subtract 1. */
9779 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9780 return fold_convert (rettype, build_int_cst (NULL_TREE,
9781 REAL_EXP (value)-1));
9789 /* Fold a call to builtin significand, if radix == 2. */
9792 fold_builtin_significand (tree arg, tree rettype)
9794 if (! validate_arg (arg, REAL_TYPE))
9799 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9801 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9808 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9809 return fold_convert (rettype, arg);
9811 /* For normal numbers, proceed iff radix == 2. */
9812 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9814 REAL_VALUE_TYPE result = *value;
9815 /* In GCC, normalized significands are in the range [0.5,
9816 1.0). We want them to be [1.0, 2.0) so set the
9818 SET_REAL_EXP (&result, 1);
9819 return build_real (rettype, result);
9828 /* Fold a call to builtin frexp, we can assume the base is 2. */
9831 fold_builtin_frexp (tree arg0, tree arg1, tree rettype)
9833 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9838 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9841 arg1 = build_fold_indirect_ref (arg1);
9843 /* Proceed if a valid pointer type was passed in. */
9844 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9846 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9852 /* For +-0, return (*exp = 0, +-0). */
9853 exp = integer_zero_node;
9858 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9859 return omit_one_operand (rettype, arg0, arg1);
9862 /* Since the frexp function always expects base 2, and in
9863 GCC normalized significands are already in the range
9864 [0.5, 1.0), we have exactly what frexp wants. */
9865 REAL_VALUE_TYPE frac_rvt = *value;
9866 SET_REAL_EXP (&frac_rvt, 0);
9867 frac = build_real (rettype, frac_rvt);
9868 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9875 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9876 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, exp);
9877 TREE_SIDE_EFFECTS (arg1) = 1;
9878 return fold_build2 (COMPOUND_EXPR, rettype, arg1, frac);
9884 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9885 then we can assume the base is two. If it's false, then we have to
9886 check the mode of the TYPE parameter in certain cases. */
9889 fold_builtin_load_exponent (tree arg0, tree arg1, tree type, bool ldexp)
9891 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9896 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9897 if (real_zerop (arg0) || integer_zerop (arg1)
9898 || (TREE_CODE (arg0) == REAL_CST
9899 && !real_isfinite (&TREE_REAL_CST (arg0))))
9900 return omit_one_operand (type, arg0, arg1);
9902 /* If both arguments are constant, then try to evaluate it. */
9903 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9904 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9905 && host_integerp (arg1, 0))
9907 /* Bound the maximum adjustment to twice the range of the
9908 mode's valid exponents. Use abs to ensure the range is
9909 positive as a sanity check. */
9910 const long max_exp_adj = 2 *
9911 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9912 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9914 /* Get the user-requested adjustment. */
9915 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9917 /* The requested adjustment must be inside this range. This
9918 is a preliminary cap to avoid things like overflow, we
9919 may still fail to compute the result for other reasons. */
9920 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9922 REAL_VALUE_TYPE initial_result;
9924 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9926 /* Ensure we didn't overflow. */
9927 if (! real_isinf (&initial_result))
9929 const REAL_VALUE_TYPE trunc_result
9930 = real_value_truncate (TYPE_MODE (type), initial_result);
9932 /* Only proceed if the target mode can hold the
9934 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9935 return build_real (type, trunc_result);
9944 /* Fold a call to builtin modf. */
9947 fold_builtin_modf (tree arg0, tree arg1, tree rettype)
9949 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9954 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9957 arg1 = build_fold_indirect_ref (arg1);
9959 /* Proceed if a valid pointer type was passed in. */
9960 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9962 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9963 REAL_VALUE_TYPE trunc, frac;
9969 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9970 trunc = frac = *value;
9973 /* For +-Inf, return (*arg1 = arg0, +-0). */
9975 frac.sign = value->sign;
9979 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9980 real_trunc (&trunc, VOIDmode, value);
9981 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9982 /* If the original number was negative and already
9983 integral, then the fractional part is -0.0. */
9984 if (value->sign && frac.cl == rvc_zero)
9985 frac.sign = value->sign;
9989 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9990 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1,
9991 build_real (rettype, trunc));
9992 TREE_SIDE_EFFECTS (arg1) = 1;
9993 return fold_build2 (COMPOUND_EXPR, rettype, arg1,
9994 build_real (rettype, frac));
10000 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10001 ARG is the argument for the call. */
10004 fold_builtin_classify (tree fndecl, tree arg, int builtin_index)
10006 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10009 if (!validate_arg (arg, REAL_TYPE))
10012 switch (builtin_index)
10014 case BUILT_IN_ISINF:
10015 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10016 return omit_one_operand (type, integer_zero_node, arg);
10018 if (TREE_CODE (arg) == REAL_CST)
10020 r = TREE_REAL_CST (arg);
10021 if (real_isinf (&r))
10022 return real_compare (GT_EXPR, &r, &dconst0)
10023 ? integer_one_node : integer_minus_one_node;
10025 return integer_zero_node;
10030 case BUILT_IN_ISINF_SIGN:
10032 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10033 /* In a boolean context, GCC will fold the inner COND_EXPR to
10034 1. So e.g. "if (isinf_sign(x))" would be folded to just
10035 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10036 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10037 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
10038 tree tmp = NULL_TREE;
10040 arg = builtin_save_expr (arg);
10042 if (signbit_fn && isinf_fn)
10044 tree signbit_call = build_call_expr (signbit_fn, 1, arg);
10045 tree isinf_call = build_call_expr (isinf_fn, 1, arg);
10047 signbit_call = fold_build2 (NE_EXPR, integer_type_node,
10048 signbit_call, integer_zero_node);
10049 isinf_call = fold_build2 (NE_EXPR, integer_type_node,
10050 isinf_call, integer_zero_node);
10052 tmp = fold_build3 (COND_EXPR, integer_type_node, signbit_call,
10053 integer_minus_one_node, integer_one_node);
10054 tmp = fold_build3 (COND_EXPR, integer_type_node, isinf_call, tmp,
10055 integer_zero_node);
10061 case BUILT_IN_ISFINITE:
10062 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10063 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10064 return omit_one_operand (type, integer_one_node, arg);
10066 if (TREE_CODE (arg) == REAL_CST)
10068 r = TREE_REAL_CST (arg);
10069 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10074 case BUILT_IN_ISNAN:
10075 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10076 return omit_one_operand (type, integer_zero_node, arg);
10078 if (TREE_CODE (arg) == REAL_CST)
10080 r = TREE_REAL_CST (arg);
10081 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10084 arg = builtin_save_expr (arg);
10085 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
10088 gcc_unreachable ();
10092 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10093 This builtin will generate code to return the appropriate floating
10094 point classification depending on the value of the floating point
10095 number passed in. The possible return values must be supplied as
10096 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10097 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10098 one floating point argument which is "type generic". */
10101 fold_builtin_fpclassify (tree exp)
10103 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10104 arg, type, res, tmp;
10105 enum machine_mode mode;
10109 /* Verify the required arguments in the original call. */
10110 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10111 INTEGER_TYPE, INTEGER_TYPE,
10112 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10115 fp_nan = CALL_EXPR_ARG (exp, 0);
10116 fp_infinite = CALL_EXPR_ARG (exp, 1);
10117 fp_normal = CALL_EXPR_ARG (exp, 2);
10118 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10119 fp_zero = CALL_EXPR_ARG (exp, 4);
10120 arg = CALL_EXPR_ARG (exp, 5);
10121 type = TREE_TYPE (arg);
10122 mode = TYPE_MODE (type);
10123 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
10125 /* fpclassify(x) ->
10126 isnan(x) ? FP_NAN :
10127 (fabs(x) == Inf ? FP_INFINITE :
10128 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10129 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10131 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
10132 build_real (type, dconst0));
10133 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_zero, fp_subnormal);
10135 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10136 real_from_string (&r, buf);
10137 tmp = fold_build2 (GE_EXPR, integer_type_node, arg, build_real (type, r));
10138 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_normal, res);
10140 if (HONOR_INFINITIES (mode))
10143 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
10144 build_real (type, r));
10145 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_infinite, res);
10148 if (HONOR_NANS (mode))
10150 tmp = fold_build2 (ORDERED_EXPR, integer_type_node, arg, arg);
10151 res = fold_build3 (COND_EXPR, integer_type_node, tmp, res, fp_nan);
10157 /* Fold a call to an unordered comparison function such as
10158 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10159 being called and ARG0 and ARG1 are the arguments for the call.
10160 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10161 the opposite of the desired result. UNORDERED_CODE is used
10162 for modes that can hold NaNs and ORDERED_CODE is used for
10166 fold_builtin_unordered_cmp (tree fndecl, tree arg0, tree arg1,
10167 enum tree_code unordered_code,
10168 enum tree_code ordered_code)
10170 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10171 enum tree_code code;
10173 enum tree_code code0, code1;
10174 tree cmp_type = NULL_TREE;
10176 type0 = TREE_TYPE (arg0);
10177 type1 = TREE_TYPE (arg1);
10179 code0 = TREE_CODE (type0);
10180 code1 = TREE_CODE (type1);
10182 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10183 /* Choose the wider of two real types. */
10184 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10186 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10188 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10191 arg0 = fold_convert (cmp_type, arg0);
10192 arg1 = fold_convert (cmp_type, arg1);
10194 if (unordered_code == UNORDERED_EXPR)
10196 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10197 return omit_two_operands (type, integer_zero_node, arg0, arg1);
10198 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
10201 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10203 return fold_build1 (TRUTH_NOT_EXPR, type,
10204 fold_build2 (code, type, arg0, arg1));
10207 /* Fold a call to built-in function FNDECL with 0 arguments.
10208 IGNORE is true if the result of the function call is ignored. This
10209 function returns NULL_TREE if no simplification was possible. */
10212 fold_builtin_0 (tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10214 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10215 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10218 CASE_FLT_FN (BUILT_IN_INF):
10219 case BUILT_IN_INFD32:
10220 case BUILT_IN_INFD64:
10221 case BUILT_IN_INFD128:
10222 return fold_builtin_inf (type, true);
10224 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10225 return fold_builtin_inf (type, false);
10227 case BUILT_IN_CLASSIFY_TYPE:
10228 return fold_builtin_classify_type (NULL_TREE);
10236 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10237 IGNORE is true if the result of the function call is ignored. This
10238 function returns NULL_TREE if no simplification was possible. */
10241 fold_builtin_1 (tree fndecl, tree arg0, bool ignore)
10243 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10244 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10248 case BUILT_IN_CONSTANT_P:
10250 tree val = fold_builtin_constant_p (arg0);
10252 /* Gimplification will pull the CALL_EXPR for the builtin out of
10253 an if condition. When not optimizing, we'll not CSE it back.
10254 To avoid link error types of regressions, return false now. */
10255 if (!val && !optimize)
10256 val = integer_zero_node;
10261 case BUILT_IN_CLASSIFY_TYPE:
10262 return fold_builtin_classify_type (arg0);
10264 case BUILT_IN_STRLEN:
10265 return fold_builtin_strlen (arg0);
10267 CASE_FLT_FN (BUILT_IN_FABS):
10268 return fold_builtin_fabs (arg0, type);
10271 case BUILT_IN_LABS:
10272 case BUILT_IN_LLABS:
10273 case BUILT_IN_IMAXABS:
10274 return fold_builtin_abs (arg0, type);
10276 CASE_FLT_FN (BUILT_IN_CONJ):
10277 if (validate_arg (arg0, COMPLEX_TYPE)
10278 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10279 return fold_build1 (CONJ_EXPR, type, arg0);
10282 CASE_FLT_FN (BUILT_IN_CREAL):
10283 if (validate_arg (arg0, COMPLEX_TYPE)
10284 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10285 return non_lvalue (fold_build1 (REALPART_EXPR, type, arg0));;
10288 CASE_FLT_FN (BUILT_IN_CIMAG):
10289 if (validate_arg (arg0, COMPLEX_TYPE))
10290 return non_lvalue (fold_build1 (IMAGPART_EXPR, type, arg0));
10293 CASE_FLT_FN (BUILT_IN_CCOS):
10294 return fold_builtin_ccos(arg0, type, fndecl, /*hyper=*/ false);
10296 CASE_FLT_FN (BUILT_IN_CCOSH):
10297 return fold_builtin_ccos(arg0, type, fndecl, /*hyper=*/ true);
10300 CASE_FLT_FN (BUILT_IN_CSIN):
10301 if (validate_arg (arg0, COMPLEX_TYPE)
10302 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10303 return do_mpc_arg1 (arg0, type, mpc_sin);
10306 CASE_FLT_FN (BUILT_IN_CSINH):
10307 if (validate_arg (arg0, COMPLEX_TYPE)
10308 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10309 return do_mpc_arg1 (arg0, type, mpc_sinh);
10312 CASE_FLT_FN (BUILT_IN_CTAN):
10313 if (validate_arg (arg0, COMPLEX_TYPE)
10314 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10315 return do_mpc_arg1 (arg0, type, mpc_tan);
10318 CASE_FLT_FN (BUILT_IN_CTANH):
10319 if (validate_arg (arg0, COMPLEX_TYPE)
10320 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10321 return do_mpc_arg1 (arg0, type, mpc_tanh);
10324 CASE_FLT_FN (BUILT_IN_CLOG):
10325 if (validate_arg (arg0, COMPLEX_TYPE)
10326 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10327 return do_mpc_arg1 (arg0, type, mpc_log);
10330 CASE_FLT_FN (BUILT_IN_CSQRT):
10331 if (validate_arg (arg0, COMPLEX_TYPE)
10332 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10333 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10337 CASE_FLT_FN (BUILT_IN_CABS):
10338 return fold_builtin_cabs (arg0, type, fndecl);
10340 CASE_FLT_FN (BUILT_IN_CARG):
10341 return fold_builtin_carg (arg0, type);
10343 CASE_FLT_FN (BUILT_IN_SQRT):
10344 return fold_builtin_sqrt (arg0, type);
10346 CASE_FLT_FN (BUILT_IN_CBRT):
10347 return fold_builtin_cbrt (arg0, type);
10349 CASE_FLT_FN (BUILT_IN_ASIN):
10350 if (validate_arg (arg0, REAL_TYPE))
10351 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10352 &dconstm1, &dconst1, true);
10355 CASE_FLT_FN (BUILT_IN_ACOS):
10356 if (validate_arg (arg0, REAL_TYPE))
10357 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10358 &dconstm1, &dconst1, true);
10361 CASE_FLT_FN (BUILT_IN_ATAN):
10362 if (validate_arg (arg0, REAL_TYPE))
10363 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10366 CASE_FLT_FN (BUILT_IN_ASINH):
10367 if (validate_arg (arg0, REAL_TYPE))
10368 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10371 CASE_FLT_FN (BUILT_IN_ACOSH):
10372 if (validate_arg (arg0, REAL_TYPE))
10373 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10374 &dconst1, NULL, true);
10377 CASE_FLT_FN (BUILT_IN_ATANH):
10378 if (validate_arg (arg0, REAL_TYPE))
10379 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10380 &dconstm1, &dconst1, false);
10383 CASE_FLT_FN (BUILT_IN_SIN):
10384 if (validate_arg (arg0, REAL_TYPE))
10385 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10388 CASE_FLT_FN (BUILT_IN_COS):
10389 return fold_builtin_cos (arg0, type, fndecl);
10391 CASE_FLT_FN (BUILT_IN_TAN):
10392 return fold_builtin_tan (arg0, type);
10394 CASE_FLT_FN (BUILT_IN_CEXP):
10395 return fold_builtin_cexp (arg0, type);
10397 CASE_FLT_FN (BUILT_IN_CEXPI):
10398 if (validate_arg (arg0, REAL_TYPE))
10399 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10402 CASE_FLT_FN (BUILT_IN_SINH):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10407 CASE_FLT_FN (BUILT_IN_COSH):
10408 return fold_builtin_cosh (arg0, type, fndecl);
10410 CASE_FLT_FN (BUILT_IN_TANH):
10411 if (validate_arg (arg0, REAL_TYPE))
10412 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10415 CASE_FLT_FN (BUILT_IN_ERF):
10416 if (validate_arg (arg0, REAL_TYPE))
10417 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10420 CASE_FLT_FN (BUILT_IN_ERFC):
10421 if (validate_arg (arg0, REAL_TYPE))
10422 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10425 CASE_FLT_FN (BUILT_IN_TGAMMA):
10426 if (validate_arg (arg0, REAL_TYPE))
10427 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10430 CASE_FLT_FN (BUILT_IN_EXP):
10431 return fold_builtin_exponent (fndecl, arg0, mpfr_exp);
10433 CASE_FLT_FN (BUILT_IN_EXP2):
10434 return fold_builtin_exponent (fndecl, arg0, mpfr_exp2);
10436 CASE_FLT_FN (BUILT_IN_EXP10):
10437 CASE_FLT_FN (BUILT_IN_POW10):
10438 return fold_builtin_exponent (fndecl, arg0, mpfr_exp10);
10440 CASE_FLT_FN (BUILT_IN_EXPM1):
10441 if (validate_arg (arg0, REAL_TYPE))
10442 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10445 CASE_FLT_FN (BUILT_IN_LOG):
10446 return fold_builtin_logarithm (fndecl, arg0, mpfr_log);
10448 CASE_FLT_FN (BUILT_IN_LOG2):
10449 return fold_builtin_logarithm (fndecl, arg0, mpfr_log2);
10451 CASE_FLT_FN (BUILT_IN_LOG10):
10452 return fold_builtin_logarithm (fndecl, arg0, mpfr_log10);
10454 CASE_FLT_FN (BUILT_IN_LOG1P):
10455 if (validate_arg (arg0, REAL_TYPE))
10456 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10457 &dconstm1, NULL, false);
10460 CASE_FLT_FN (BUILT_IN_J0):
10461 if (validate_arg (arg0, REAL_TYPE))
10462 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10466 CASE_FLT_FN (BUILT_IN_J1):
10467 if (validate_arg (arg0, REAL_TYPE))
10468 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10472 CASE_FLT_FN (BUILT_IN_Y0):
10473 if (validate_arg (arg0, REAL_TYPE))
10474 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10475 &dconst0, NULL, false);
10478 CASE_FLT_FN (BUILT_IN_Y1):
10479 if (validate_arg (arg0, REAL_TYPE))
10480 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10481 &dconst0, NULL, false);
10484 CASE_FLT_FN (BUILT_IN_NAN):
10485 case BUILT_IN_NAND32:
10486 case BUILT_IN_NAND64:
10487 case BUILT_IN_NAND128:
10488 return fold_builtin_nan (arg0, type, true);
10490 CASE_FLT_FN (BUILT_IN_NANS):
10491 return fold_builtin_nan (arg0, type, false);
10493 CASE_FLT_FN (BUILT_IN_FLOOR):
10494 return fold_builtin_floor (fndecl, arg0);
10496 CASE_FLT_FN (BUILT_IN_CEIL):
10497 return fold_builtin_ceil (fndecl, arg0);
10499 CASE_FLT_FN (BUILT_IN_TRUNC):
10500 return fold_builtin_trunc (fndecl, arg0);
10502 CASE_FLT_FN (BUILT_IN_ROUND):
10503 return fold_builtin_round (fndecl, arg0);
10505 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10506 CASE_FLT_FN (BUILT_IN_RINT):
10507 return fold_trunc_transparent_mathfn (fndecl, arg0);
10509 CASE_FLT_FN (BUILT_IN_LCEIL):
10510 CASE_FLT_FN (BUILT_IN_LLCEIL):
10511 CASE_FLT_FN (BUILT_IN_LFLOOR):
10512 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10513 CASE_FLT_FN (BUILT_IN_LROUND):
10514 CASE_FLT_FN (BUILT_IN_LLROUND):
10515 return fold_builtin_int_roundingfn (fndecl, arg0);
10517 CASE_FLT_FN (BUILT_IN_LRINT):
10518 CASE_FLT_FN (BUILT_IN_LLRINT):
10519 return fold_fixed_mathfn (fndecl, arg0);
10521 case BUILT_IN_BSWAP32:
10522 case BUILT_IN_BSWAP64:
10523 return fold_builtin_bswap (fndecl, arg0);
10525 CASE_INT_FN (BUILT_IN_FFS):
10526 CASE_INT_FN (BUILT_IN_CLZ):
10527 CASE_INT_FN (BUILT_IN_CTZ):
10528 CASE_INT_FN (BUILT_IN_POPCOUNT):
10529 CASE_INT_FN (BUILT_IN_PARITY):
10530 return fold_builtin_bitop (fndecl, arg0);
10532 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10533 return fold_builtin_signbit (arg0, type);
10535 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10536 return fold_builtin_significand (arg0, type);
10538 CASE_FLT_FN (BUILT_IN_ILOGB):
10539 CASE_FLT_FN (BUILT_IN_LOGB):
10540 return fold_builtin_logb (arg0, type);
10542 case BUILT_IN_ISASCII:
10543 return fold_builtin_isascii (arg0);
10545 case BUILT_IN_TOASCII:
10546 return fold_builtin_toascii (arg0);
10548 case BUILT_IN_ISDIGIT:
10549 return fold_builtin_isdigit (arg0);
10551 CASE_FLT_FN (BUILT_IN_FINITE):
10552 case BUILT_IN_FINITED32:
10553 case BUILT_IN_FINITED64:
10554 case BUILT_IN_FINITED128:
10555 case BUILT_IN_ISFINITE:
10556 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISFINITE);
10558 CASE_FLT_FN (BUILT_IN_ISINF):
10559 case BUILT_IN_ISINFD32:
10560 case BUILT_IN_ISINFD64:
10561 case BUILT_IN_ISINFD128:
10562 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF);
10564 case BUILT_IN_ISINF_SIGN:
10565 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF_SIGN);
10567 CASE_FLT_FN (BUILT_IN_ISNAN):
10568 case BUILT_IN_ISNAND32:
10569 case BUILT_IN_ISNAND64:
10570 case BUILT_IN_ISNAND128:
10571 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISNAN);
10573 case BUILT_IN_PRINTF:
10574 case BUILT_IN_PRINTF_UNLOCKED:
10575 case BUILT_IN_VPRINTF:
10576 return fold_builtin_printf (fndecl, arg0, NULL_TREE, ignore, fcode);
10586 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10587 IGNORE is true if the result of the function call is ignored. This
10588 function returns NULL_TREE if no simplification was possible. */
10591 fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore)
10593 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10594 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10598 CASE_FLT_FN (BUILT_IN_JN):
10599 if (validate_arg (arg0, INTEGER_TYPE)
10600 && validate_arg (arg1, REAL_TYPE))
10601 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10604 CASE_FLT_FN (BUILT_IN_YN):
10605 if (validate_arg (arg0, INTEGER_TYPE)
10606 && validate_arg (arg1, REAL_TYPE))
10607 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10611 CASE_FLT_FN (BUILT_IN_DREM):
10612 CASE_FLT_FN (BUILT_IN_REMAINDER):
10613 if (validate_arg (arg0, REAL_TYPE)
10614 && validate_arg(arg1, REAL_TYPE))
10615 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10618 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10619 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10620 if (validate_arg (arg0, REAL_TYPE)
10621 && validate_arg(arg1, POINTER_TYPE))
10622 return do_mpfr_lgamma_r (arg0, arg1, type);
10625 CASE_FLT_FN (BUILT_IN_ATAN2):
10626 if (validate_arg (arg0, REAL_TYPE)
10627 && validate_arg(arg1, REAL_TYPE))
10628 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10631 CASE_FLT_FN (BUILT_IN_FDIM):
10632 if (validate_arg (arg0, REAL_TYPE)
10633 && validate_arg(arg1, REAL_TYPE))
10634 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10637 CASE_FLT_FN (BUILT_IN_HYPOT):
10638 return fold_builtin_hypot (fndecl, arg0, arg1, type);
10640 CASE_FLT_FN (BUILT_IN_LDEXP):
10641 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/true);
10642 CASE_FLT_FN (BUILT_IN_SCALBN):
10643 CASE_FLT_FN (BUILT_IN_SCALBLN):
10644 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/false);
10646 CASE_FLT_FN (BUILT_IN_FREXP):
10647 return fold_builtin_frexp (arg0, arg1, type);
10649 CASE_FLT_FN (BUILT_IN_MODF):
10650 return fold_builtin_modf (arg0, arg1, type);
10652 case BUILT_IN_BZERO:
10653 return fold_builtin_bzero (arg0, arg1, ignore);
10655 case BUILT_IN_FPUTS:
10656 return fold_builtin_fputs (arg0, arg1, ignore, false, NULL_TREE);
10658 case BUILT_IN_FPUTS_UNLOCKED:
10659 return fold_builtin_fputs (arg0, arg1, ignore, true, NULL_TREE);
10661 case BUILT_IN_STRSTR:
10662 return fold_builtin_strstr (arg0, arg1, type);
10664 case BUILT_IN_STRCAT:
10665 return fold_builtin_strcat (arg0, arg1);
10667 case BUILT_IN_STRSPN:
10668 return fold_builtin_strspn (arg0, arg1);
10670 case BUILT_IN_STRCSPN:
10671 return fold_builtin_strcspn (arg0, arg1);
10673 case BUILT_IN_STRCHR:
10674 case BUILT_IN_INDEX:
10675 return fold_builtin_strchr (arg0, arg1, type);
10677 case BUILT_IN_STRRCHR:
10678 case BUILT_IN_RINDEX:
10679 return fold_builtin_strrchr (arg0, arg1, type);
10681 case BUILT_IN_STRCPY:
10682 return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE);
10684 case BUILT_IN_STPCPY:
10687 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10691 return build_call_expr (fn, 2, arg0, arg1);
10695 case BUILT_IN_STRCMP:
10696 return fold_builtin_strcmp (arg0, arg1);
10698 case BUILT_IN_STRPBRK:
10699 return fold_builtin_strpbrk (arg0, arg1, type);
10701 case BUILT_IN_EXPECT:
10702 return fold_builtin_expect (arg0, arg1);
10704 CASE_FLT_FN (BUILT_IN_POW):
10705 return fold_builtin_pow (fndecl, arg0, arg1, type);
10707 CASE_FLT_FN (BUILT_IN_POWI):
10708 return fold_builtin_powi (fndecl, arg0, arg1, type);
10710 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10711 return fold_builtin_copysign (fndecl, arg0, arg1, type);
10713 CASE_FLT_FN (BUILT_IN_FMIN):
10714 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/false);
10716 CASE_FLT_FN (BUILT_IN_FMAX):
10717 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/true);
10719 case BUILT_IN_ISGREATER:
10720 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLE_EXPR, LE_EXPR);
10721 case BUILT_IN_ISGREATEREQUAL:
10722 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLT_EXPR, LT_EXPR);
10723 case BUILT_IN_ISLESS:
10724 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGE_EXPR, GE_EXPR);
10725 case BUILT_IN_ISLESSEQUAL:
10726 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGT_EXPR, GT_EXPR);
10727 case BUILT_IN_ISLESSGREATER:
10728 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10729 case BUILT_IN_ISUNORDERED:
10730 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNORDERED_EXPR,
10733 /* We do the folding for va_start in the expander. */
10734 case BUILT_IN_VA_START:
10737 case BUILT_IN_SPRINTF:
10738 return fold_builtin_sprintf (arg0, arg1, NULL_TREE, ignore);
10740 case BUILT_IN_OBJECT_SIZE:
10741 return fold_builtin_object_size (arg0, arg1);
10743 case BUILT_IN_PRINTF:
10744 case BUILT_IN_PRINTF_UNLOCKED:
10745 case BUILT_IN_VPRINTF:
10746 return fold_builtin_printf (fndecl, arg0, arg1, ignore, fcode);
10748 case BUILT_IN_PRINTF_CHK:
10749 case BUILT_IN_VPRINTF_CHK:
10750 if (!validate_arg (arg0, INTEGER_TYPE)
10751 || TREE_SIDE_EFFECTS (arg0))
10754 return fold_builtin_printf (fndecl, arg1, NULL_TREE, ignore, fcode);
10757 case BUILT_IN_FPRINTF:
10758 case BUILT_IN_FPRINTF_UNLOCKED:
10759 case BUILT_IN_VFPRINTF:
10760 return fold_builtin_fprintf (fndecl, arg0, arg1, NULL_TREE,
10769 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10770 and ARG2. IGNORE is true if the result of the function call is ignored.
10771 This function returns NULL_TREE if no simplification was possible. */
10774 fold_builtin_3 (tree fndecl, tree arg0, tree arg1, tree arg2, bool ignore)
10776 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10777 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10781 CASE_FLT_FN (BUILT_IN_SINCOS):
10782 return fold_builtin_sincos (arg0, arg1, arg2);
10784 CASE_FLT_FN (BUILT_IN_FMA):
10785 if (validate_arg (arg0, REAL_TYPE)
10786 && validate_arg(arg1, REAL_TYPE)
10787 && validate_arg(arg2, REAL_TYPE))
10788 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10791 CASE_FLT_FN (BUILT_IN_REMQUO):
10792 if (validate_arg (arg0, REAL_TYPE)
10793 && validate_arg(arg1, REAL_TYPE)
10794 && validate_arg(arg2, POINTER_TYPE))
10795 return do_mpfr_remquo (arg0, arg1, arg2);
10798 case BUILT_IN_MEMSET:
10799 return fold_builtin_memset (arg0, arg1, arg2, type, ignore);
10801 case BUILT_IN_BCOPY:
10802 return fold_builtin_memory_op (arg1, arg0, arg2, void_type_node, true, /*endp=*/3);
10804 case BUILT_IN_MEMCPY:
10805 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/0);
10807 case BUILT_IN_MEMPCPY:
10808 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/1);
10810 case BUILT_IN_MEMMOVE:
10811 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/3);
10813 case BUILT_IN_STRNCAT:
10814 return fold_builtin_strncat (arg0, arg1, arg2);
10816 case BUILT_IN_STRNCPY:
10817 return fold_builtin_strncpy (fndecl, arg0, arg1, arg2, NULL_TREE);
10819 case BUILT_IN_STRNCMP:
10820 return fold_builtin_strncmp (arg0, arg1, arg2);
10822 case BUILT_IN_MEMCHR:
10823 return fold_builtin_memchr (arg0, arg1, arg2, type);
10825 case BUILT_IN_BCMP:
10826 case BUILT_IN_MEMCMP:
10827 return fold_builtin_memcmp (arg0, arg1, arg2);;
10829 case BUILT_IN_SPRINTF:
10830 return fold_builtin_sprintf (arg0, arg1, arg2, ignore);
10832 case BUILT_IN_STRCPY_CHK:
10833 case BUILT_IN_STPCPY_CHK:
10834 return fold_builtin_stxcpy_chk (fndecl, arg0, arg1, arg2, NULL_TREE,
10837 case BUILT_IN_STRCAT_CHK:
10838 return fold_builtin_strcat_chk (fndecl, arg0, arg1, arg2);
10840 case BUILT_IN_PRINTF_CHK:
10841 case BUILT_IN_VPRINTF_CHK:
10842 if (!validate_arg (arg0, INTEGER_TYPE)
10843 || TREE_SIDE_EFFECTS (arg0))
10846 return fold_builtin_printf (fndecl, arg1, arg2, ignore, fcode);
10849 case BUILT_IN_FPRINTF:
10850 case BUILT_IN_FPRINTF_UNLOCKED:
10851 case BUILT_IN_VFPRINTF:
10852 return fold_builtin_fprintf (fndecl, arg0, arg1, arg2, ignore, fcode);
10854 case BUILT_IN_FPRINTF_CHK:
10855 case BUILT_IN_VFPRINTF_CHK:
10856 if (!validate_arg (arg1, INTEGER_TYPE)
10857 || TREE_SIDE_EFFECTS (arg1))
10860 return fold_builtin_fprintf (fndecl, arg0, arg2, NULL_TREE,
10869 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10870 ARG2, and ARG3. IGNORE is true if the result of the function call is
10871 ignored. This function returns NULL_TREE if no simplification was
10875 fold_builtin_4 (tree fndecl, tree arg0, tree arg1, tree arg2, tree arg3,
10878 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10882 case BUILT_IN_MEMCPY_CHK:
10883 case BUILT_IN_MEMPCPY_CHK:
10884 case BUILT_IN_MEMMOVE_CHK:
10885 case BUILT_IN_MEMSET_CHK:
10886 return fold_builtin_memory_chk (fndecl, arg0, arg1, arg2, arg3,
10888 DECL_FUNCTION_CODE (fndecl));
10890 case BUILT_IN_STRNCPY_CHK:
10891 return fold_builtin_strncpy_chk (arg0, arg1, arg2, arg3, NULL_TREE);
10893 case BUILT_IN_STRNCAT_CHK:
10894 return fold_builtin_strncat_chk (fndecl, arg0, arg1, arg2, arg3);
10896 case BUILT_IN_FPRINTF_CHK:
10897 case BUILT_IN_VFPRINTF_CHK:
10898 if (!validate_arg (arg1, INTEGER_TYPE)
10899 || TREE_SIDE_EFFECTS (arg1))
10902 return fold_builtin_fprintf (fndecl, arg0, arg2, arg3,
10912 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10913 arguments, where NARGS <= 4. IGNORE is true if the result of the
10914 function call is ignored. This function returns NULL_TREE if no
10915 simplification was possible. Note that this only folds builtins with
10916 fixed argument patterns. Foldings that do varargs-to-varargs
10917 transformations, or that match calls with more than 4 arguments,
10918 need to be handled with fold_builtin_varargs instead. */
10920 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10923 fold_builtin_n (tree fndecl, tree *args, int nargs, bool ignore)
10925 tree ret = NULL_TREE;
10930 ret = fold_builtin_0 (fndecl, ignore);
10933 ret = fold_builtin_1 (fndecl, args[0], ignore);
10936 ret = fold_builtin_2 (fndecl, args[0], args[1], ignore);
10939 ret = fold_builtin_3 (fndecl, args[0], args[1], args[2], ignore);
10942 ret = fold_builtin_4 (fndecl, args[0], args[1], args[2], args[3],
10950 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10951 TREE_NO_WARNING (ret) = 1;
10957 /* Builtins with folding operations that operate on "..." arguments
10958 need special handling; we need to store the arguments in a convenient
10959 data structure before attempting any folding. Fortunately there are
10960 only a few builtins that fall into this category. FNDECL is the
10961 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10962 result of the function call is ignored. */
10965 fold_builtin_varargs (tree fndecl, tree exp, bool ignore ATTRIBUTE_UNUSED)
10967 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10968 tree ret = NULL_TREE;
10972 case BUILT_IN_SPRINTF_CHK:
10973 case BUILT_IN_VSPRINTF_CHK:
10974 ret = fold_builtin_sprintf_chk (exp, fcode);
10977 case BUILT_IN_SNPRINTF_CHK:
10978 case BUILT_IN_VSNPRINTF_CHK:
10979 ret = fold_builtin_snprintf_chk (exp, NULL_TREE, fcode);
10982 case BUILT_IN_FPCLASSIFY:
10983 ret = fold_builtin_fpclassify (exp);
10991 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10992 TREE_NO_WARNING (ret) = 1;
10998 /* Return true if FNDECL shouldn't be folded right now.
10999 If a built-in function has an inline attribute always_inline
11000 wrapper, defer folding it after always_inline functions have
11001 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11002 might not be performed. */
11005 avoid_folding_inline_builtin (tree fndecl)
11007 return (DECL_DECLARED_INLINE_P (fndecl)
11008 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11010 && !cfun->always_inline_functions_inlined
11011 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11014 /* A wrapper function for builtin folding that prevents warnings for
11015 "statement without effect" and the like, caused by removing the
11016 call node earlier than the warning is generated. */
11019 fold_call_expr (tree exp, bool ignore)
11021 tree ret = NULL_TREE;
11022 tree fndecl = get_callee_fndecl (exp);
11024 && TREE_CODE (fndecl) == FUNCTION_DECL
11025 && DECL_BUILT_IN (fndecl)
11026 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11027 yet. Defer folding until we see all the arguments
11028 (after inlining). */
11029 && !CALL_EXPR_VA_ARG_PACK (exp))
11031 int nargs = call_expr_nargs (exp);
11033 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11034 instead last argument is __builtin_va_arg_pack (). Defer folding
11035 even in that case, until arguments are finalized. */
11036 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11038 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11040 && TREE_CODE (fndecl2) == FUNCTION_DECL
11041 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11042 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11046 if (avoid_folding_inline_builtin (fndecl))
11049 /* FIXME: Don't use a list in this interface. */
11050 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11051 return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore);
11054 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11056 tree *args = CALL_EXPR_ARGP (exp);
11057 ret = fold_builtin_n (fndecl, args, nargs, ignore);
11060 ret = fold_builtin_varargs (fndecl, exp, ignore);
11063 /* Propagate location information from original call to
11064 expansion of builtin. Otherwise things like
11065 maybe_emit_chk_warning, that operate on the expansion
11066 of a builtin, will use the wrong location information. */
11067 if (CAN_HAVE_LOCATION_P (exp) && EXPR_HAS_LOCATION (exp))
11069 tree realret = ret;
11070 if (TREE_CODE (ret) == NOP_EXPR)
11071 realret = TREE_OPERAND (ret, 0);
11072 if (CAN_HAVE_LOCATION_P (realret)
11073 && !EXPR_HAS_LOCATION (realret))
11074 SET_EXPR_LOCATION (realret, EXPR_LOCATION (exp));
11083 /* Conveniently construct a function call expression. FNDECL names the
11084 function to be called and ARGLIST is a TREE_LIST of arguments. */
11087 build_function_call_expr (tree fndecl, tree arglist)
11089 tree fntype = TREE_TYPE (fndecl);
11090 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11091 int n = list_length (arglist);
11092 tree *argarray = (tree *) alloca (n * sizeof (tree));
11095 for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist))
11096 argarray[i] = TREE_VALUE (arglist);
11097 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
11100 /* Conveniently construct a function call expression. FNDECL names the
11101 function to be called, N is the number of arguments, and the "..."
11102 parameters are the argument expressions. */
11105 build_call_expr (tree fndecl, int n, ...)
11108 tree fntype = TREE_TYPE (fndecl);
11109 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11110 tree *argarray = (tree *) alloca (n * sizeof (tree));
11114 for (i = 0; i < n; i++)
11115 argarray[i] = va_arg (ap, tree);
11117 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
11120 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11121 N arguments are passed in the array ARGARRAY. */
11124 fold_builtin_call_array (tree type,
11129 tree ret = NULL_TREE;
11133 if (TREE_CODE (fn) == ADDR_EXPR)
11135 tree fndecl = TREE_OPERAND (fn, 0);
11136 if (TREE_CODE (fndecl) == FUNCTION_DECL
11137 && DECL_BUILT_IN (fndecl))
11139 /* If last argument is __builtin_va_arg_pack (), arguments to this
11140 function are not finalized yet. Defer folding until they are. */
11141 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11143 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11145 && TREE_CODE (fndecl2) == FUNCTION_DECL
11146 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11147 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11148 return build_call_array (type, fn, n, argarray);
11150 if (avoid_folding_inline_builtin (fndecl))
11151 return build_call_array (type, fn, n, argarray);
11152 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11154 tree arglist = NULL_TREE;
11155 for (i = n - 1; i >= 0; i--)
11156 arglist = tree_cons (NULL_TREE, argarray[i], arglist);
11157 ret = targetm.fold_builtin (fndecl, arglist, false);
11160 return build_call_array (type, fn, n, argarray);
11162 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11164 /* First try the transformations that don't require consing up
11166 ret = fold_builtin_n (fndecl, argarray, n, false);
11171 /* If we got this far, we need to build an exp. */
11172 exp = build_call_array (type, fn, n, argarray);
11173 ret = fold_builtin_varargs (fndecl, exp, false);
11174 return ret ? ret : exp;
11178 return build_call_array (type, fn, n, argarray);
11181 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11182 along with N new arguments specified as the "..." parameters. SKIP
11183 is the number of arguments in EXP to be omitted. This function is used
11184 to do varargs-to-varargs transformations. */
11187 rewrite_call_expr (tree exp, int skip, tree fndecl, int n, ...)
11189 int oldnargs = call_expr_nargs (exp);
11190 int nargs = oldnargs - skip + n;
11191 tree fntype = TREE_TYPE (fndecl);
11192 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11200 buffer = XALLOCAVEC (tree, nargs);
11202 for (i = 0; i < n; i++)
11203 buffer[i] = va_arg (ap, tree);
11205 for (j = skip; j < oldnargs; j++, i++)
11206 buffer[i] = CALL_EXPR_ARG (exp, j);
11209 buffer = CALL_EXPR_ARGP (exp) + skip;
11211 return fold (build_call_array (TREE_TYPE (exp), fn, nargs, buffer));
11214 /* Validate a single argument ARG against a tree code CODE representing
11218 validate_arg (const_tree arg, enum tree_code code)
11222 else if (code == POINTER_TYPE)
11223 return POINTER_TYPE_P (TREE_TYPE (arg));
11224 else if (code == INTEGER_TYPE)
11225 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11226 return code == TREE_CODE (TREE_TYPE (arg));
11229 /* This function validates the types of a function call argument list
11230 against a specified list of tree_codes. If the last specifier is a 0,
11231 that represents an ellipses, otherwise the last specifier must be a
11234 This is the GIMPLE version of validate_arglist. Eventually we want to
11235 completely convert builtins.c to work from GIMPLEs and the tree based
11236 validate_arglist will then be removed. */
11239 validate_gimple_arglist (const_gimple call, ...)
11241 enum tree_code code;
11247 va_start (ap, call);
11252 code = (enum tree_code) va_arg (ap, int);
11256 /* This signifies an ellipses, any further arguments are all ok. */
11260 /* This signifies an endlink, if no arguments remain, return
11261 true, otherwise return false. */
11262 res = (i == gimple_call_num_args (call));
11265 /* If no parameters remain or the parameter's code does not
11266 match the specified code, return false. Otherwise continue
11267 checking any remaining arguments. */
11268 arg = gimple_call_arg (call, i++);
11269 if (!validate_arg (arg, code))
11276 /* We need gotos here since we can only have one VA_CLOSE in a
11284 /* This function validates the types of a function call argument list
11285 against a specified list of tree_codes. If the last specifier is a 0,
11286 that represents an ellipses, otherwise the last specifier must be a
11290 validate_arglist (const_tree callexpr, ...)
11292 enum tree_code code;
11295 const_call_expr_arg_iterator iter;
11298 va_start (ap, callexpr);
11299 init_const_call_expr_arg_iterator (callexpr, &iter);
11303 code = (enum tree_code) va_arg (ap, int);
11307 /* This signifies an ellipses, any further arguments are all ok. */
11311 /* This signifies an endlink, if no arguments remain, return
11312 true, otherwise return false. */
11313 res = !more_const_call_expr_args_p (&iter);
11316 /* If no parameters remain or the parameter's code does not
11317 match the specified code, return false. Otherwise continue
11318 checking any remaining arguments. */
11319 arg = next_const_call_expr_arg (&iter);
11320 if (!validate_arg (arg, code))
11327 /* We need gotos here since we can only have one VA_CLOSE in a
11335 /* Default target-specific builtin expander that does nothing. */
11338 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11339 rtx target ATTRIBUTE_UNUSED,
11340 rtx subtarget ATTRIBUTE_UNUSED,
11341 enum machine_mode mode ATTRIBUTE_UNUSED,
11342 int ignore ATTRIBUTE_UNUSED)
11347 /* Returns true is EXP represents data that would potentially reside
11348 in a readonly section. */
11351 readonly_data_expr (tree exp)
11355 if (TREE_CODE (exp) != ADDR_EXPR)
11358 exp = get_base_address (TREE_OPERAND (exp, 0));
11362 /* Make sure we call decl_readonly_section only for trees it
11363 can handle (since it returns true for everything it doesn't
11365 if (TREE_CODE (exp) == STRING_CST
11366 || TREE_CODE (exp) == CONSTRUCTOR
11367 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11368 return decl_readonly_section (exp, 0);
11373 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11374 to the call, and TYPE is its return type.
11376 Return NULL_TREE if no simplification was possible, otherwise return the
11377 simplified form of the call as a tree.
11379 The simplified form may be a constant or other expression which
11380 computes the same value, but in a more efficient manner (including
11381 calls to other builtin functions).
11383 The call may contain arguments which need to be evaluated, but
11384 which are not useful to determine the result of the call. In
11385 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11386 COMPOUND_EXPR will be an argument which must be evaluated.
11387 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11388 COMPOUND_EXPR in the chain will contain the tree for the simplified
11389 form of the builtin function call. */
11392 fold_builtin_strstr (tree s1, tree s2, tree type)
11394 if (!validate_arg (s1, POINTER_TYPE)
11395 || !validate_arg (s2, POINTER_TYPE))
11400 const char *p1, *p2;
11402 p2 = c_getstr (s2);
11406 p1 = c_getstr (s1);
11409 const char *r = strstr (p1, p2);
11413 return build_int_cst (TREE_TYPE (s1), 0);
11415 /* Return an offset into the constant string argument. */
11416 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11417 s1, size_int (r - p1));
11418 return fold_convert (type, tem);
11421 /* The argument is const char *, and the result is char *, so we need
11422 a type conversion here to avoid a warning. */
11424 return fold_convert (type, s1);
11429 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11433 /* New argument list transforming strstr(s1, s2) to
11434 strchr(s1, s2[0]). */
11435 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11439 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11440 the call, and TYPE is its return type.
11442 Return NULL_TREE if no simplification was possible, otherwise return the
11443 simplified form of the call as a tree.
11445 The simplified form may be a constant or other expression which
11446 computes the same value, but in a more efficient manner (including
11447 calls to other builtin functions).
11449 The call may contain arguments which need to be evaluated, but
11450 which are not useful to determine the result of the call. In
11451 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11452 COMPOUND_EXPR will be an argument which must be evaluated.
11453 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11454 COMPOUND_EXPR in the chain will contain the tree for the simplified
11455 form of the builtin function call. */
11458 fold_builtin_strchr (tree s1, tree s2, tree type)
11460 if (!validate_arg (s1, POINTER_TYPE)
11461 || !validate_arg (s2, INTEGER_TYPE))
11467 if (TREE_CODE (s2) != INTEGER_CST)
11470 p1 = c_getstr (s1);
11477 if (target_char_cast (s2, &c))
11480 r = strchr (p1, c);
11483 return build_int_cst (TREE_TYPE (s1), 0);
11485 /* Return an offset into the constant string argument. */
11486 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11487 s1, size_int (r - p1));
11488 return fold_convert (type, tem);
11494 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11495 the call, and TYPE is its return type.
11497 Return NULL_TREE if no simplification was possible, otherwise return the
11498 simplified form of the call as a tree.
11500 The simplified form may be a constant or other expression which
11501 computes the same value, but in a more efficient manner (including
11502 calls to other builtin functions).
11504 The call may contain arguments which need to be evaluated, but
11505 which are not useful to determine the result of the call. In
11506 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11507 COMPOUND_EXPR will be an argument which must be evaluated.
11508 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11509 COMPOUND_EXPR in the chain will contain the tree for the simplified
11510 form of the builtin function call. */
11513 fold_builtin_strrchr (tree s1, tree s2, tree type)
11515 if (!validate_arg (s1, POINTER_TYPE)
11516 || !validate_arg (s2, INTEGER_TYPE))
11523 if (TREE_CODE (s2) != INTEGER_CST)
11526 p1 = c_getstr (s1);
11533 if (target_char_cast (s2, &c))
11536 r = strrchr (p1, c);
11539 return build_int_cst (TREE_TYPE (s1), 0);
11541 /* Return an offset into the constant string argument. */
11542 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11543 s1, size_int (r - p1));
11544 return fold_convert (type, tem);
11547 if (! integer_zerop (s2))
11550 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11554 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11555 return build_call_expr (fn, 2, s1, s2);
11559 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11560 to the call, and TYPE is its return type.
11562 Return NULL_TREE if no simplification was possible, otherwise return the
11563 simplified form of the call as a tree.
11565 The simplified form may be a constant or other expression which
11566 computes the same value, but in a more efficient manner (including
11567 calls to other builtin functions).
11569 The call may contain arguments which need to be evaluated, but
11570 which are not useful to determine the result of the call. In
11571 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11572 COMPOUND_EXPR will be an argument which must be evaluated.
11573 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11574 COMPOUND_EXPR in the chain will contain the tree for the simplified
11575 form of the builtin function call. */
11578 fold_builtin_strpbrk (tree s1, tree s2, tree type)
11580 if (!validate_arg (s1, POINTER_TYPE)
11581 || !validate_arg (s2, POINTER_TYPE))
11586 const char *p1, *p2;
11588 p2 = c_getstr (s2);
11592 p1 = c_getstr (s1);
11595 const char *r = strpbrk (p1, p2);
11599 return build_int_cst (TREE_TYPE (s1), 0);
11601 /* Return an offset into the constant string argument. */
11602 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11603 s1, size_int (r - p1));
11604 return fold_convert (type, tem);
11608 /* strpbrk(x, "") == NULL.
11609 Evaluate and ignore s1 in case it had side-effects. */
11610 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
11613 return NULL_TREE; /* Really call strpbrk. */
11615 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11619 /* New argument list transforming strpbrk(s1, s2) to
11620 strchr(s1, s2[0]). */
11621 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11625 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11628 Return NULL_TREE if no simplification was possible, otherwise return the
11629 simplified form of the call as a tree.
11631 The simplified form may be a constant or other expression which
11632 computes the same value, but in a more efficient manner (including
11633 calls to other builtin functions).
11635 The call may contain arguments which need to be evaluated, but
11636 which are not useful to determine the result of the call. In
11637 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11638 COMPOUND_EXPR will be an argument which must be evaluated.
11639 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11640 COMPOUND_EXPR in the chain will contain the tree for the simplified
11641 form of the builtin function call. */
11644 fold_builtin_strcat (tree dst, tree src)
11646 if (!validate_arg (dst, POINTER_TYPE)
11647 || !validate_arg (src, POINTER_TYPE))
11651 const char *p = c_getstr (src);
11653 /* If the string length is zero, return the dst parameter. */
11654 if (p && *p == '\0')
11661 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11662 arguments to the call.
11664 Return NULL_TREE if no simplification was possible, otherwise return the
11665 simplified form of the call as a tree.
11667 The simplified form may be a constant or other expression which
11668 computes the same value, but in a more efficient manner (including
11669 calls to other builtin functions).
11671 The call may contain arguments which need to be evaluated, but
11672 which are not useful to determine the result of the call. In
11673 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11674 COMPOUND_EXPR will be an argument which must be evaluated.
11675 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11676 COMPOUND_EXPR in the chain will contain the tree for the simplified
11677 form of the builtin function call. */
11680 fold_builtin_strncat (tree dst, tree src, tree len)
11682 if (!validate_arg (dst, POINTER_TYPE)
11683 || !validate_arg (src, POINTER_TYPE)
11684 || !validate_arg (len, INTEGER_TYPE))
11688 const char *p = c_getstr (src);
11690 /* If the requested length is zero, or the src parameter string
11691 length is zero, return the dst parameter. */
11692 if (integer_zerop (len) || (p && *p == '\0'))
11693 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
11695 /* If the requested len is greater than or equal to the string
11696 length, call strcat. */
11697 if (TREE_CODE (len) == INTEGER_CST && p
11698 && compare_tree_int (len, strlen (p)) >= 0)
11700 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11702 /* If the replacement _DECL isn't initialized, don't do the
11707 return build_call_expr (fn, 2, dst, src);
11713 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11716 Return NULL_TREE if no simplification was possible, otherwise return the
11717 simplified form of the call as a tree.
11719 The simplified form may be a constant or other expression which
11720 computes the same value, but in a more efficient manner (including
11721 calls to other builtin functions).
11723 The call may contain arguments which need to be evaluated, but
11724 which are not useful to determine the result of the call. In
11725 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11726 COMPOUND_EXPR will be an argument which must be evaluated.
11727 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11728 COMPOUND_EXPR in the chain will contain the tree for the simplified
11729 form of the builtin function call. */
11732 fold_builtin_strspn (tree s1, tree s2)
11734 if (!validate_arg (s1, POINTER_TYPE)
11735 || !validate_arg (s2, POINTER_TYPE))
11739 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11741 /* If both arguments are constants, evaluate at compile-time. */
11744 const size_t r = strspn (p1, p2);
11745 return size_int (r);
11748 /* If either argument is "", return NULL_TREE. */
11749 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11750 /* Evaluate and ignore both arguments in case either one has
11752 return omit_two_operands (size_type_node, size_zero_node,
11758 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11761 Return NULL_TREE if no simplification was possible, otherwise return the
11762 simplified form of the call as a tree.
11764 The simplified form may be a constant or other expression which
11765 computes the same value, but in a more efficient manner (including
11766 calls to other builtin functions).
11768 The call may contain arguments which need to be evaluated, but
11769 which are not useful to determine the result of the call. In
11770 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11771 COMPOUND_EXPR will be an argument which must be evaluated.
11772 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11773 COMPOUND_EXPR in the chain will contain the tree for the simplified
11774 form of the builtin function call. */
11777 fold_builtin_strcspn (tree s1, tree s2)
11779 if (!validate_arg (s1, POINTER_TYPE)
11780 || !validate_arg (s2, POINTER_TYPE))
11784 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11786 /* If both arguments are constants, evaluate at compile-time. */
11789 const size_t r = strcspn (p1, p2);
11790 return size_int (r);
11793 /* If the first argument is "", return NULL_TREE. */
11794 if (p1 && *p1 == '\0')
11796 /* Evaluate and ignore argument s2 in case it has
11798 return omit_one_operand (size_type_node,
11799 size_zero_node, s2);
11802 /* If the second argument is "", return __builtin_strlen(s1). */
11803 if (p2 && *p2 == '\0')
11805 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11807 /* If the replacement _DECL isn't initialized, don't do the
11812 return build_call_expr (fn, 1, s1);
11818 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11819 to the call. IGNORE is true if the value returned
11820 by the builtin will be ignored. UNLOCKED is true is true if this
11821 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11822 the known length of the string. Return NULL_TREE if no simplification
11826 fold_builtin_fputs (tree arg0, tree arg1, bool ignore, bool unlocked, tree len)
11828 /* If we're using an unlocked function, assume the other unlocked
11829 functions exist explicitly. */
11830 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11831 : implicit_built_in_decls[BUILT_IN_FPUTC];
11832 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11833 : implicit_built_in_decls[BUILT_IN_FWRITE];
11835 /* If the return value is used, don't do the transformation. */
11839 /* Verify the arguments in the original call. */
11840 if (!validate_arg (arg0, POINTER_TYPE)
11841 || !validate_arg (arg1, POINTER_TYPE))
11845 len = c_strlen (arg0, 0);
11847 /* Get the length of the string passed to fputs. If the length
11848 can't be determined, punt. */
11850 || TREE_CODE (len) != INTEGER_CST)
11853 switch (compare_tree_int (len, 1))
11855 case -1: /* length is 0, delete the call entirely . */
11856 return omit_one_operand (integer_type_node, integer_zero_node, arg1);;
11858 case 0: /* length is 1, call fputc. */
11860 const char *p = c_getstr (arg0);
11865 return build_call_expr (fn_fputc, 2,
11866 build_int_cst (NULL_TREE, p[0]), arg1);
11872 case 1: /* length is greater than 1, call fwrite. */
11874 /* If optimizing for size keep fputs. */
11875 if (optimize_function_for_size_p (cfun))
11877 /* New argument list transforming fputs(string, stream) to
11878 fwrite(string, 1, len, stream). */
11880 return build_call_expr (fn_fwrite, 4, arg0, size_one_node, len, arg1);
11885 gcc_unreachable ();
11890 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11891 produced. False otherwise. This is done so that we don't output the error
11892 or warning twice or three times. */
11895 fold_builtin_next_arg (tree exp, bool va_start_p)
11897 tree fntype = TREE_TYPE (current_function_decl);
11898 int nargs = call_expr_nargs (exp);
11901 if (TYPE_ARG_TYPES (fntype) == 0
11902 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11903 == void_type_node))
11905 error ("%<va_start%> used in function with fixed args");
11911 if (va_start_p && (nargs != 2))
11913 error ("wrong number of arguments to function %<va_start%>");
11916 arg = CALL_EXPR_ARG (exp, 1);
11918 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11919 when we checked the arguments and if needed issued a warning. */
11924 /* Evidently an out of date version of <stdarg.h>; can't validate
11925 va_start's second argument, but can still work as intended. */
11926 warning (0, "%<__builtin_next_arg%> called without an argument");
11929 else if (nargs > 1)
11931 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11934 arg = CALL_EXPR_ARG (exp, 0);
11937 if (TREE_CODE (arg) == SSA_NAME)
11938 arg = SSA_NAME_VAR (arg);
11940 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11941 or __builtin_next_arg (0) the first time we see it, after checking
11942 the arguments and if needed issuing a warning. */
11943 if (!integer_zerop (arg))
11945 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11947 /* Strip off all nops for the sake of the comparison. This
11948 is not quite the same as STRIP_NOPS. It does more.
11949 We must also strip off INDIRECT_EXPR for C++ reference
11951 while (CONVERT_EXPR_P (arg)
11952 || TREE_CODE (arg) == INDIRECT_REF)
11953 arg = TREE_OPERAND (arg, 0);
11954 if (arg != last_parm)
11956 /* FIXME: Sometimes with the tree optimizers we can get the
11957 not the last argument even though the user used the last
11958 argument. We just warn and set the arg to be the last
11959 argument so that we will get wrong-code because of
11961 warning (0, "second parameter of %<va_start%> not last named argument");
11964 /* Undefined by C99 7.15.1.4p4 (va_start):
11965 "If the parameter parmN is declared with the register storage
11966 class, with a function or array type, or with a type that is
11967 not compatible with the type that results after application of
11968 the default argument promotions, the behavior is undefined."
11970 else if (DECL_REGISTER (arg))
11971 warning (0, "undefined behaviour when second parameter of "
11972 "%<va_start%> is declared with %<register%> storage");
11974 /* We want to verify the second parameter just once before the tree
11975 optimizers are run and then avoid keeping it in the tree,
11976 as otherwise we could warn even for correct code like:
11977 void foo (int i, ...)
11978 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11980 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11982 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11988 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11989 ORIG may be null if this is a 2-argument call. We don't attempt to
11990 simplify calls with more than 3 arguments.
11992 Return NULL_TREE if no simplification was possible, otherwise return the
11993 simplified form of the call as a tree. If IGNORED is true, it means that
11994 the caller does not use the returned value of the function. */
11997 fold_builtin_sprintf (tree dest, tree fmt, tree orig, int ignored)
12000 const char *fmt_str = NULL;
12002 /* Verify the required arguments in the original call. We deal with two
12003 types of sprintf() calls: 'sprintf (str, fmt)' and
12004 'sprintf (dest, "%s", orig)'. */
12005 if (!validate_arg (dest, POINTER_TYPE)
12006 || !validate_arg (fmt, POINTER_TYPE))
12008 if (orig && !validate_arg (orig, POINTER_TYPE))
12011 /* Check whether the format is a literal string constant. */
12012 fmt_str = c_getstr (fmt);
12013 if (fmt_str == NULL)
12017 retval = NULL_TREE;
12019 if (!init_target_chars ())
12022 /* If the format doesn't contain % args or %%, use strcpy. */
12023 if (strchr (fmt_str, target_percent) == NULL)
12025 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
12030 /* Don't optimize sprintf (buf, "abc", ptr++). */
12034 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12035 'format' is known to contain no % formats. */
12036 call = build_call_expr (fn, 2, dest, fmt);
12038 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
12041 /* If the format is "%s", use strcpy if the result isn't used. */
12042 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12045 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
12050 /* Don't crash on sprintf (str1, "%s"). */
12054 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12057 retval = c_strlen (orig, 1);
12058 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12061 call = build_call_expr (fn, 2, dest, orig);
12064 if (call && retval)
12066 retval = fold_convert
12067 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
12069 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12075 /* Expand a call EXP to __builtin_object_size. */
12078 expand_builtin_object_size (tree exp)
12081 int object_size_type;
12082 tree fndecl = get_callee_fndecl (exp);
12084 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12086 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12088 expand_builtin_trap ();
12092 ost = CALL_EXPR_ARG (exp, 1);
12095 if (TREE_CODE (ost) != INTEGER_CST
12096 || tree_int_cst_sgn (ost) < 0
12097 || compare_tree_int (ost, 3) > 0)
12099 error ("%Klast argument of %D is not integer constant between 0 and 3",
12101 expand_builtin_trap ();
12105 object_size_type = tree_low_cst (ost, 0);
12107 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12110 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12111 FCODE is the BUILT_IN_* to use.
12112 Return NULL_RTX if we failed; the caller should emit a normal call,
12113 otherwise try to get the result in TARGET, if convenient (and in
12114 mode MODE if that's convenient). */
12117 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12118 enum built_in_function fcode)
12120 tree dest, src, len, size;
12122 if (!validate_arglist (exp,
12124 fcode == BUILT_IN_MEMSET_CHK
12125 ? INTEGER_TYPE : POINTER_TYPE,
12126 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12129 dest = CALL_EXPR_ARG (exp, 0);
12130 src = CALL_EXPR_ARG (exp, 1);
12131 len = CALL_EXPR_ARG (exp, 2);
12132 size = CALL_EXPR_ARG (exp, 3);
12134 if (! host_integerp (size, 1))
12137 if (host_integerp (len, 1) || integer_all_onesp (size))
12141 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12143 warning_at (tree_nonartificial_location (exp),
12144 0, "%Kcall to %D will always overflow destination buffer",
12145 exp, get_callee_fndecl (exp));
12150 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12151 mem{cpy,pcpy,move,set} is available. */
12154 case BUILT_IN_MEMCPY_CHK:
12155 fn = built_in_decls[BUILT_IN_MEMCPY];
12157 case BUILT_IN_MEMPCPY_CHK:
12158 fn = built_in_decls[BUILT_IN_MEMPCPY];
12160 case BUILT_IN_MEMMOVE_CHK:
12161 fn = built_in_decls[BUILT_IN_MEMMOVE];
12163 case BUILT_IN_MEMSET_CHK:
12164 fn = built_in_decls[BUILT_IN_MEMSET];
12173 fn = build_call_expr (fn, 3, dest, src, len);
12174 STRIP_TYPE_NOPS (fn);
12175 while (TREE_CODE (fn) == COMPOUND_EXPR)
12177 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
12179 fn = TREE_OPERAND (fn, 1);
12181 if (TREE_CODE (fn) == CALL_EXPR)
12182 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12183 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12185 else if (fcode == BUILT_IN_MEMSET_CHK)
12189 unsigned int dest_align
12190 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12192 /* If DEST is not a pointer type, call the normal function. */
12193 if (dest_align == 0)
12196 /* If SRC and DEST are the same (and not volatile), do nothing. */
12197 if (operand_equal_p (src, dest, 0))
12201 if (fcode != BUILT_IN_MEMPCPY_CHK)
12203 /* Evaluate and ignore LEN in case it has side-effects. */
12204 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12205 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12208 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12209 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12212 /* __memmove_chk special case. */
12213 if (fcode == BUILT_IN_MEMMOVE_CHK)
12215 unsigned int src_align
12216 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12218 if (src_align == 0)
12221 /* If src is categorized for a readonly section we can use
12222 normal __memcpy_chk. */
12223 if (readonly_data_expr (src))
12225 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12228 fn = build_call_expr (fn, 4, dest, src, len, size);
12229 STRIP_TYPE_NOPS (fn);
12230 while (TREE_CODE (fn) == COMPOUND_EXPR)
12232 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
12234 fn = TREE_OPERAND (fn, 1);
12236 if (TREE_CODE (fn) == CALL_EXPR)
12237 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12238 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12245 /* Emit warning if a buffer overflow is detected at compile time. */
12248 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12252 location_t loc = tree_nonartificial_location (exp);
12256 case BUILT_IN_STRCPY_CHK:
12257 case BUILT_IN_STPCPY_CHK:
12258 /* For __strcat_chk the warning will be emitted only if overflowing
12259 by at least strlen (dest) + 1 bytes. */
12260 case BUILT_IN_STRCAT_CHK:
12261 len = CALL_EXPR_ARG (exp, 1);
12262 size = CALL_EXPR_ARG (exp, 2);
12265 case BUILT_IN_STRNCAT_CHK:
12266 case BUILT_IN_STRNCPY_CHK:
12267 len = CALL_EXPR_ARG (exp, 2);
12268 size = CALL_EXPR_ARG (exp, 3);
12270 case BUILT_IN_SNPRINTF_CHK:
12271 case BUILT_IN_VSNPRINTF_CHK:
12272 len = CALL_EXPR_ARG (exp, 1);
12273 size = CALL_EXPR_ARG (exp, 3);
12276 gcc_unreachable ();
12282 if (! host_integerp (size, 1) || integer_all_onesp (size))
12287 len = c_strlen (len, 1);
12288 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12291 else if (fcode == BUILT_IN_STRNCAT_CHK)
12293 tree src = CALL_EXPR_ARG (exp, 1);
12294 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12296 src = c_strlen (src, 1);
12297 if (! src || ! host_integerp (src, 1))
12299 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12300 exp, get_callee_fndecl (exp));
12303 else if (tree_int_cst_lt (src, size))
12306 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12309 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12310 exp, get_callee_fndecl (exp));
12313 /* Emit warning if a buffer overflow is detected at compile time
12314 in __sprintf_chk/__vsprintf_chk calls. */
12317 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12319 tree dest, size, len, fmt, flag;
12320 const char *fmt_str;
12321 int nargs = call_expr_nargs (exp);
12323 /* Verify the required arguments in the original call. */
12327 dest = CALL_EXPR_ARG (exp, 0);
12328 flag = CALL_EXPR_ARG (exp, 1);
12329 size = CALL_EXPR_ARG (exp, 2);
12330 fmt = CALL_EXPR_ARG (exp, 3);
12332 if (! host_integerp (size, 1) || integer_all_onesp (size))
12335 /* Check whether the format is a literal string constant. */
12336 fmt_str = c_getstr (fmt);
12337 if (fmt_str == NULL)
12340 if (!init_target_chars ())
12343 /* If the format doesn't contain % args or %%, we know its size. */
12344 if (strchr (fmt_str, target_percent) == 0)
12345 len = build_int_cstu (size_type_node, strlen (fmt_str));
12346 /* If the format is "%s" and first ... argument is a string literal,
12348 else if (fcode == BUILT_IN_SPRINTF_CHK
12349 && strcmp (fmt_str, target_percent_s) == 0)
12355 arg = CALL_EXPR_ARG (exp, 4);
12356 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12359 len = c_strlen (arg, 1);
12360 if (!len || ! host_integerp (len, 1))
12366 if (! tree_int_cst_lt (len, size))
12367 warning_at (tree_nonartificial_location (exp),
12368 0, "%Kcall to %D will always overflow destination buffer",
12369 exp, get_callee_fndecl (exp));
12372 /* Emit warning if a free is called with address of a variable. */
12375 maybe_emit_free_warning (tree exp)
12377 tree arg = CALL_EXPR_ARG (exp, 0);
12380 if (TREE_CODE (arg) != ADDR_EXPR)
12383 arg = get_base_address (TREE_OPERAND (arg, 0));
12384 if (arg == NULL || INDIRECT_REF_P (arg))
12387 if (SSA_VAR_P (arg))
12388 warning_at (tree_nonartificial_location (exp),
12389 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12391 warning_at (tree_nonartificial_location (exp),
12392 0, "%Kattempt to free a non-heap object", exp);
12395 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12399 fold_builtin_object_size (tree ptr, tree ost)
12401 tree ret = NULL_TREE;
12402 int object_size_type;
12404 if (!validate_arg (ptr, POINTER_TYPE)
12405 || !validate_arg (ost, INTEGER_TYPE))
12410 if (TREE_CODE (ost) != INTEGER_CST
12411 || tree_int_cst_sgn (ost) < 0
12412 || compare_tree_int (ost, 3) > 0)
12415 object_size_type = tree_low_cst (ost, 0);
12417 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12418 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12419 and (size_t) 0 for types 2 and 3. */
12420 if (TREE_SIDE_EFFECTS (ptr))
12421 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12423 if (TREE_CODE (ptr) == ADDR_EXPR)
12424 ret = build_int_cstu (size_type_node,
12425 compute_builtin_object_size (ptr, object_size_type));
12427 else if (TREE_CODE (ptr) == SSA_NAME)
12429 unsigned HOST_WIDE_INT bytes;
12431 /* If object size is not known yet, delay folding until
12432 later. Maybe subsequent passes will help determining
12434 bytes = compute_builtin_object_size (ptr, object_size_type);
12435 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
12437 ret = build_int_cstu (size_type_node, bytes);
12442 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
12443 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
12444 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
12451 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12452 DEST, SRC, LEN, and SIZE are the arguments to the call.
12453 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12454 code of the builtin. If MAXLEN is not NULL, it is maximum length
12455 passed as third argument. */
12458 fold_builtin_memory_chk (tree fndecl,
12459 tree dest, tree src, tree len, tree size,
12460 tree maxlen, bool ignore,
12461 enum built_in_function fcode)
12465 if (!validate_arg (dest, POINTER_TYPE)
12466 || !validate_arg (src,
12467 (fcode == BUILT_IN_MEMSET_CHK
12468 ? INTEGER_TYPE : POINTER_TYPE))
12469 || !validate_arg (len, INTEGER_TYPE)
12470 || !validate_arg (size, INTEGER_TYPE))
12473 /* If SRC and DEST are the same (and not volatile), return DEST
12474 (resp. DEST+LEN for __mempcpy_chk). */
12475 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12477 if (fcode != BUILT_IN_MEMPCPY_CHK)
12478 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12481 tree temp = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12482 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
12486 if (! host_integerp (size, 1))
12489 if (! integer_all_onesp (size))
12491 if (! host_integerp (len, 1))
12493 /* If LEN is not constant, try MAXLEN too.
12494 For MAXLEN only allow optimizing into non-_ocs function
12495 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12496 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12498 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12500 /* (void) __mempcpy_chk () can be optimized into
12501 (void) __memcpy_chk (). */
12502 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12506 return build_call_expr (fn, 4, dest, src, len, size);
12514 if (tree_int_cst_lt (size, maxlen))
12519 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12520 mem{cpy,pcpy,move,set} is available. */
12523 case BUILT_IN_MEMCPY_CHK:
12524 fn = built_in_decls[BUILT_IN_MEMCPY];
12526 case BUILT_IN_MEMPCPY_CHK:
12527 fn = built_in_decls[BUILT_IN_MEMPCPY];
12529 case BUILT_IN_MEMMOVE_CHK:
12530 fn = built_in_decls[BUILT_IN_MEMMOVE];
12532 case BUILT_IN_MEMSET_CHK:
12533 fn = built_in_decls[BUILT_IN_MEMSET];
12542 return build_call_expr (fn, 3, dest, src, len);
12545 /* Fold a call to the __st[rp]cpy_chk builtin.
12546 DEST, SRC, and SIZE are the arguments to the call.
12547 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12548 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12549 strings passed as second argument. */
12552 fold_builtin_stxcpy_chk (tree fndecl, tree dest, tree src, tree size,
12553 tree maxlen, bool ignore,
12554 enum built_in_function fcode)
12558 if (!validate_arg (dest, POINTER_TYPE)
12559 || !validate_arg (src, POINTER_TYPE)
12560 || !validate_arg (size, INTEGER_TYPE))
12563 /* If SRC and DEST are the same (and not volatile), return DEST. */
12564 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12565 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
12567 if (! host_integerp (size, 1))
12570 if (! integer_all_onesp (size))
12572 len = c_strlen (src, 1);
12573 if (! len || ! host_integerp (len, 1))
12575 /* If LEN is not constant, try MAXLEN too.
12576 For MAXLEN only allow optimizing into non-_ocs function
12577 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12578 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12580 if (fcode == BUILT_IN_STPCPY_CHK)
12585 /* If return value of __stpcpy_chk is ignored,
12586 optimize into __strcpy_chk. */
12587 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12591 return build_call_expr (fn, 3, dest, src, size);
12594 if (! len || TREE_SIDE_EFFECTS (len))
12597 /* If c_strlen returned something, but not a constant,
12598 transform __strcpy_chk into __memcpy_chk. */
12599 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12603 len = size_binop (PLUS_EXPR, len, ssize_int (1));
12604 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
12605 build_call_expr (fn, 4,
12606 dest, src, len, size));
12612 if (! tree_int_cst_lt (maxlen, size))
12616 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12617 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12618 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12622 return build_call_expr (fn, 2, dest, src);
12625 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12626 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12627 length passed as third argument. */
12630 fold_builtin_strncpy_chk (tree dest, tree src, tree len, tree size,
12635 if (!validate_arg (dest, POINTER_TYPE)
12636 || !validate_arg (src, POINTER_TYPE)
12637 || !validate_arg (len, INTEGER_TYPE)
12638 || !validate_arg (size, INTEGER_TYPE))
12641 if (! host_integerp (size, 1))
12644 if (! integer_all_onesp (size))
12646 if (! host_integerp (len, 1))
12648 /* If LEN is not constant, try MAXLEN too.
12649 For MAXLEN only allow optimizing into non-_ocs function
12650 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12651 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12657 if (tree_int_cst_lt (size, maxlen))
12661 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12662 fn = built_in_decls[BUILT_IN_STRNCPY];
12666 return build_call_expr (fn, 3, dest, src, len);
12669 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12670 are the arguments to the call. */
12673 fold_builtin_strcat_chk (tree fndecl, tree dest, tree src, tree size)
12678 if (!validate_arg (dest, POINTER_TYPE)
12679 || !validate_arg (src, POINTER_TYPE)
12680 || !validate_arg (size, INTEGER_TYPE))
12683 p = c_getstr (src);
12684 /* If the SRC parameter is "", return DEST. */
12685 if (p && *p == '\0')
12686 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12688 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12691 /* If __builtin_strcat_chk is used, assume strcat is available. */
12692 fn = built_in_decls[BUILT_IN_STRCAT];
12696 return build_call_expr (fn, 2, dest, src);
12699 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12703 fold_builtin_strncat_chk (tree fndecl,
12704 tree dest, tree src, tree len, tree size)
12709 if (!validate_arg (dest, POINTER_TYPE)
12710 || !validate_arg (src, POINTER_TYPE)
12711 || !validate_arg (size, INTEGER_TYPE)
12712 || !validate_arg (size, INTEGER_TYPE))
12715 p = c_getstr (src);
12716 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12717 if (p && *p == '\0')
12718 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12719 else if (integer_zerop (len))
12720 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12722 if (! host_integerp (size, 1))
12725 if (! integer_all_onesp (size))
12727 tree src_len = c_strlen (src, 1);
12729 && host_integerp (src_len, 1)
12730 && host_integerp (len, 1)
12731 && ! tree_int_cst_lt (len, src_len))
12733 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12734 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12738 return build_call_expr (fn, 3, dest, src, size);
12743 /* If __builtin_strncat_chk is used, assume strncat is available. */
12744 fn = built_in_decls[BUILT_IN_STRNCAT];
12748 return build_call_expr (fn, 3, dest, src, len);
12751 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12752 a normal call should be emitted rather than expanding the function
12753 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12756 fold_builtin_sprintf_chk (tree exp, enum built_in_function fcode)
12758 tree dest, size, len, fn, fmt, flag;
12759 const char *fmt_str;
12760 int nargs = call_expr_nargs (exp);
12762 /* Verify the required arguments in the original call. */
12765 dest = CALL_EXPR_ARG (exp, 0);
12766 if (!validate_arg (dest, POINTER_TYPE))
12768 flag = CALL_EXPR_ARG (exp, 1);
12769 if (!validate_arg (flag, INTEGER_TYPE))
12771 size = CALL_EXPR_ARG (exp, 2);
12772 if (!validate_arg (size, INTEGER_TYPE))
12774 fmt = CALL_EXPR_ARG (exp, 3);
12775 if (!validate_arg (fmt, POINTER_TYPE))
12778 if (! host_integerp (size, 1))
12783 if (!init_target_chars ())
12786 /* Check whether the format is a literal string constant. */
12787 fmt_str = c_getstr (fmt);
12788 if (fmt_str != NULL)
12790 /* If the format doesn't contain % args or %%, we know the size. */
12791 if (strchr (fmt_str, target_percent) == 0)
12793 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12794 len = build_int_cstu (size_type_node, strlen (fmt_str));
12796 /* If the format is "%s" and first ... argument is a string literal,
12797 we know the size too. */
12798 else if (fcode == BUILT_IN_SPRINTF_CHK
12799 && strcmp (fmt_str, target_percent_s) == 0)
12805 arg = CALL_EXPR_ARG (exp, 4);
12806 if (validate_arg (arg, POINTER_TYPE))
12808 len = c_strlen (arg, 1);
12809 if (! len || ! host_integerp (len, 1))
12816 if (! integer_all_onesp (size))
12818 if (! len || ! tree_int_cst_lt (len, size))
12822 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12823 or if format doesn't contain % chars or is "%s". */
12824 if (! integer_zerop (flag))
12826 if (fmt_str == NULL)
12828 if (strchr (fmt_str, target_percent) != NULL
12829 && strcmp (fmt_str, target_percent_s))
12833 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12834 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12835 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12839 return rewrite_call_expr (exp, 4, fn, 2, dest, fmt);
12842 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12843 a normal call should be emitted rather than expanding the function
12844 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12845 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12846 passed as second argument. */
12849 fold_builtin_snprintf_chk (tree exp, tree maxlen,
12850 enum built_in_function fcode)
12852 tree dest, size, len, fn, fmt, flag;
12853 const char *fmt_str;
12855 /* Verify the required arguments in the original call. */
12856 if (call_expr_nargs (exp) < 5)
12858 dest = CALL_EXPR_ARG (exp, 0);
12859 if (!validate_arg (dest, POINTER_TYPE))
12861 len = CALL_EXPR_ARG (exp, 1);
12862 if (!validate_arg (len, INTEGER_TYPE))
12864 flag = CALL_EXPR_ARG (exp, 2);
12865 if (!validate_arg (flag, INTEGER_TYPE))
12867 size = CALL_EXPR_ARG (exp, 3);
12868 if (!validate_arg (size, INTEGER_TYPE))
12870 fmt = CALL_EXPR_ARG (exp, 4);
12871 if (!validate_arg (fmt, POINTER_TYPE))
12874 if (! host_integerp (size, 1))
12877 if (! integer_all_onesp (size))
12879 if (! host_integerp (len, 1))
12881 /* If LEN is not constant, try MAXLEN too.
12882 For MAXLEN only allow optimizing into non-_ocs function
12883 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12884 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12890 if (tree_int_cst_lt (size, maxlen))
12894 if (!init_target_chars ())
12897 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12898 or if format doesn't contain % chars or is "%s". */
12899 if (! integer_zerop (flag))
12901 fmt_str = c_getstr (fmt);
12902 if (fmt_str == NULL)
12904 if (strchr (fmt_str, target_percent) != NULL
12905 && strcmp (fmt_str, target_percent_s))
12909 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12911 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12912 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12916 return rewrite_call_expr (exp, 5, fn, 3, dest, len, fmt);
12919 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12920 FMT and ARG are the arguments to the call; we don't fold cases with
12921 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12923 Return NULL_TREE if no simplification was possible, otherwise return the
12924 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12925 code of the function to be simplified. */
12928 fold_builtin_printf (tree fndecl, tree fmt, tree arg, bool ignore,
12929 enum built_in_function fcode)
12931 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12932 const char *fmt_str = NULL;
12934 /* If the return value is used, don't do the transformation. */
12938 /* Verify the required arguments in the original call. */
12939 if (!validate_arg (fmt, POINTER_TYPE))
12942 /* Check whether the format is a literal string constant. */
12943 fmt_str = c_getstr (fmt);
12944 if (fmt_str == NULL)
12947 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12949 /* If we're using an unlocked function, assume the other
12950 unlocked functions exist explicitly. */
12951 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12952 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12956 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12957 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12960 if (!init_target_chars ())
12963 if (strcmp (fmt_str, target_percent_s) == 0
12964 || strchr (fmt_str, target_percent) == NULL)
12968 if (strcmp (fmt_str, target_percent_s) == 0)
12970 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12973 if (!arg || !validate_arg (arg, POINTER_TYPE))
12976 str = c_getstr (arg);
12982 /* The format specifier doesn't contain any '%' characters. */
12983 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12989 /* If the string was "", printf does nothing. */
12990 if (str[0] == '\0')
12991 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12993 /* If the string has length of 1, call putchar. */
12994 if (str[1] == '\0')
12996 /* Given printf("c"), (where c is any one character,)
12997 convert "c"[0] to an int and pass that to the replacement
12999 newarg = build_int_cst (NULL_TREE, str[0]);
13001 call = build_call_expr (fn_putchar, 1, newarg);
13005 /* If the string was "string\n", call puts("string"). */
13006 size_t len = strlen (str);
13007 if ((unsigned char)str[len - 1] == target_newline)
13009 /* Create a NUL-terminated string that's one char shorter
13010 than the original, stripping off the trailing '\n'. */
13011 char *newstr = XALLOCAVEC (char, len);
13012 memcpy (newstr, str, len - 1);
13013 newstr[len - 1] = 0;
13015 newarg = build_string_literal (len, newstr);
13017 call = build_call_expr (fn_puts, 1, newarg);
13020 /* We'd like to arrange to call fputs(string,stdout) here,
13021 but we need stdout and don't have a way to get it yet. */
13026 /* The other optimizations can be done only on the non-va_list variants. */
13027 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13030 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13031 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13033 if (!arg || !validate_arg (arg, POINTER_TYPE))
13036 call = build_call_expr (fn_puts, 1, arg);
13039 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13040 else if (strcmp (fmt_str, target_percent_c) == 0)
13042 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13045 call = build_call_expr (fn_putchar, 1, arg);
13051 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
13054 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13055 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13056 more than 3 arguments, and ARG may be null in the 2-argument case.
13058 Return NULL_TREE if no simplification was possible, otherwise return the
13059 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13060 code of the function to be simplified. */
13063 fold_builtin_fprintf (tree fndecl, tree fp, tree fmt, tree arg, bool ignore,
13064 enum built_in_function fcode)
13066 tree fn_fputc, fn_fputs, call = NULL_TREE;
13067 const char *fmt_str = NULL;
13069 /* If the return value is used, don't do the transformation. */
13073 /* Verify the required arguments in the original call. */
13074 if (!validate_arg (fp, POINTER_TYPE))
13076 if (!validate_arg (fmt, POINTER_TYPE))
13079 /* Check whether the format is a literal string constant. */
13080 fmt_str = c_getstr (fmt);
13081 if (fmt_str == NULL)
13084 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13086 /* If we're using an unlocked function, assume the other
13087 unlocked functions exist explicitly. */
13088 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
13089 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
13093 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
13094 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
13097 if (!init_target_chars ())
13100 /* If the format doesn't contain % args or %%, use strcpy. */
13101 if (strchr (fmt_str, target_percent) == NULL)
13103 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13107 /* If the format specifier was "", fprintf does nothing. */
13108 if (fmt_str[0] == '\0')
13110 /* If FP has side-effects, just wait until gimplification is
13112 if (TREE_SIDE_EFFECTS (fp))
13115 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13118 /* When "string" doesn't contain %, replace all cases of
13119 fprintf (fp, string) with fputs (string, fp). The fputs
13120 builtin will take care of special cases like length == 1. */
13122 call = build_call_expr (fn_fputs, 2, fmt, fp);
13125 /* The other optimizations can be done only on the non-va_list variants. */
13126 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13129 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13130 else if (strcmp (fmt_str, target_percent_s) == 0)
13132 if (!arg || !validate_arg (arg, POINTER_TYPE))
13135 call = build_call_expr (fn_fputs, 2, arg, fp);
13138 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13139 else if (strcmp (fmt_str, target_percent_c) == 0)
13141 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13144 call = build_call_expr (fn_fputc, 2, arg, fp);
13149 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
13152 /* Initialize format string characters in the target charset. */
13155 init_target_chars (void)
13160 target_newline = lang_hooks.to_target_charset ('\n');
13161 target_percent = lang_hooks.to_target_charset ('%');
13162 target_c = lang_hooks.to_target_charset ('c');
13163 target_s = lang_hooks.to_target_charset ('s');
13164 if (target_newline == 0 || target_percent == 0 || target_c == 0
13168 target_percent_c[0] = target_percent;
13169 target_percent_c[1] = target_c;
13170 target_percent_c[2] = '\0';
13172 target_percent_s[0] = target_percent;
13173 target_percent_s[1] = target_s;
13174 target_percent_s[2] = '\0';
13176 target_percent_s_newline[0] = target_percent;
13177 target_percent_s_newline[1] = target_s;
13178 target_percent_s_newline[2] = target_newline;
13179 target_percent_s_newline[3] = '\0';
13186 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13187 and no overflow/underflow occurred. INEXACT is true if M was not
13188 exactly calculated. TYPE is the tree type for the result. This
13189 function assumes that you cleared the MPFR flags and then
13190 calculated M to see if anything subsequently set a flag prior to
13191 entering this function. Return NULL_TREE if any checks fail. */
13194 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13196 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13197 overflow/underflow occurred. If -frounding-math, proceed iff the
13198 result of calling FUNC was exact. */
13199 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13200 && (!flag_rounding_math || !inexact))
13202 REAL_VALUE_TYPE rr;
13204 real_from_mpfr (&rr, m, type, GMP_RNDN);
13205 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13206 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13207 but the mpft_t is not, then we underflowed in the
13209 if (real_isfinite (&rr)
13210 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13212 REAL_VALUE_TYPE rmode;
13214 real_convert (&rmode, TYPE_MODE (type), &rr);
13215 /* Proceed iff the specified mode can hold the value. */
13216 if (real_identical (&rmode, &rr))
13217 return build_real (type, rmode);
13224 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13225 number and no overflow/underflow occurred. INEXACT is true if M
13226 was not exactly calculated. TYPE is the tree type for the result.
13227 This function assumes that you cleared the MPFR flags and then
13228 calculated M to see if anything subsequently set a flag prior to
13229 entering this function. Return NULL_TREE if any checks fail. */
13232 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact)
13234 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13235 overflow/underflow occurred. If -frounding-math, proceed iff the
13236 result of calling FUNC was exact. */
13237 if (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13238 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13239 && (!flag_rounding_math || !inexact))
13241 REAL_VALUE_TYPE re, im;
13243 real_from_mpfr (&re, mpc_realref (m), type, GMP_RNDN);
13244 real_from_mpfr (&im, mpc_imagref (m), type, GMP_RNDN);
13245 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13246 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13247 but the mpft_t is not, then we underflowed in the
13249 if (real_isfinite (&re) && real_isfinite (&im)
13250 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13251 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0))
13253 REAL_VALUE_TYPE re_mode, im_mode;
13255 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13256 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13257 /* Proceed iff the specified mode can hold the value. */
13258 if (real_identical (&re_mode, &re) && real_identical (&im_mode, &im))
13259 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13260 build_real (TREE_TYPE (type), im_mode));
13265 #endif /* HAVE_mpc */
13267 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13268 FUNC on it and return the resulting value as a tree with type TYPE.
13269 If MIN and/or MAX are not NULL, then the supplied ARG must be
13270 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13271 acceptable values, otherwise they are not. The mpfr precision is
13272 set to the precision of TYPE. We assume that function FUNC returns
13273 zero if the result could be calculated exactly within the requested
13277 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13278 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13281 tree result = NULL_TREE;
13285 /* To proceed, MPFR must exactly represent the target floating point
13286 format, which only happens when the target base equals two. */
13287 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13288 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13290 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13292 if (real_isfinite (ra)
13293 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13294 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13296 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13297 const int prec = fmt->p;
13298 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13302 mpfr_init2 (m, prec);
13303 mpfr_from_real (m, ra, GMP_RNDN);
13304 mpfr_clear_flags ();
13305 inexact = func (m, m, rnd);
13306 result = do_mpfr_ckconv (m, type, inexact);
13314 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13315 FUNC on it and return the resulting value as a tree with type TYPE.
13316 The mpfr precision is set to the precision of TYPE. We assume that
13317 function FUNC returns zero if the result could be calculated
13318 exactly within the requested precision. */
13321 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13322 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13324 tree result = NULL_TREE;
13329 /* To proceed, MPFR must exactly represent the target floating point
13330 format, which only happens when the target base equals two. */
13331 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13332 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13333 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13335 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13336 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13338 if (real_isfinite (ra1) && real_isfinite (ra2))
13340 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13341 const int prec = fmt->p;
13342 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13346 mpfr_inits2 (prec, m1, m2, NULL);
13347 mpfr_from_real (m1, ra1, GMP_RNDN);
13348 mpfr_from_real (m2, ra2, GMP_RNDN);
13349 mpfr_clear_flags ();
13350 inexact = func (m1, m1, m2, rnd);
13351 result = do_mpfr_ckconv (m1, type, inexact);
13352 mpfr_clears (m1, m2, NULL);
13359 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13360 FUNC on it and return the resulting value as a tree with type TYPE.
13361 The mpfr precision is set to the precision of TYPE. We assume that
13362 function FUNC returns zero if the result could be calculated
13363 exactly within the requested precision. */
13366 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13367 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13369 tree result = NULL_TREE;
13375 /* To proceed, MPFR must exactly represent the target floating point
13376 format, which only happens when the target base equals two. */
13377 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13378 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13379 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13380 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13382 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13383 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13384 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13386 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13388 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13389 const int prec = fmt->p;
13390 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13394 mpfr_inits2 (prec, m1, m2, m3, NULL);
13395 mpfr_from_real (m1, ra1, GMP_RNDN);
13396 mpfr_from_real (m2, ra2, GMP_RNDN);
13397 mpfr_from_real (m3, ra3, GMP_RNDN);
13398 mpfr_clear_flags ();
13399 inexact = func (m1, m1, m2, m3, rnd);
13400 result = do_mpfr_ckconv (m1, type, inexact);
13401 mpfr_clears (m1, m2, m3, NULL);
13408 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13409 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13410 If ARG_SINP and ARG_COSP are NULL then the result is returned
13411 as a complex value.
13412 The type is taken from the type of ARG and is used for setting the
13413 precision of the calculation and results. */
13416 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13418 tree const type = TREE_TYPE (arg);
13419 tree result = NULL_TREE;
13423 /* To proceed, MPFR must exactly represent the target floating point
13424 format, which only happens when the target base equals two. */
13425 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13426 && TREE_CODE (arg) == REAL_CST
13427 && !TREE_OVERFLOW (arg))
13429 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13431 if (real_isfinite (ra))
13433 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13434 const int prec = fmt->p;
13435 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13436 tree result_s, result_c;
13440 mpfr_inits2 (prec, m, ms, mc, NULL);
13441 mpfr_from_real (m, ra, GMP_RNDN);
13442 mpfr_clear_flags ();
13443 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13444 result_s = do_mpfr_ckconv (ms, type, inexact);
13445 result_c = do_mpfr_ckconv (mc, type, inexact);
13446 mpfr_clears (m, ms, mc, NULL);
13447 if (result_s && result_c)
13449 /* If we are to return in a complex value do so. */
13450 if (!arg_sinp && !arg_cosp)
13451 return build_complex (build_complex_type (type),
13452 result_c, result_s);
13454 /* Dereference the sin/cos pointer arguments. */
13455 arg_sinp = build_fold_indirect_ref (arg_sinp);
13456 arg_cosp = build_fold_indirect_ref (arg_cosp);
13457 /* Proceed if valid pointer type were passed in. */
13458 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13459 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13461 /* Set the values. */
13462 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13464 TREE_SIDE_EFFECTS (result_s) = 1;
13465 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13467 TREE_SIDE_EFFECTS (result_c) = 1;
13468 /* Combine the assignments into a compound expr. */
13469 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13470 result_s, result_c));
13478 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13479 two-argument mpfr order N Bessel function FUNC on them and return
13480 the resulting value as a tree with type TYPE. The mpfr precision
13481 is set to the precision of TYPE. We assume that function FUNC
13482 returns zero if the result could be calculated exactly within the
13483 requested precision. */
13485 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13486 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13487 const REAL_VALUE_TYPE *min, bool inclusive)
13489 tree result = NULL_TREE;
13494 /* To proceed, MPFR must exactly represent the target floating point
13495 format, which only happens when the target base equals two. */
13496 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13497 && host_integerp (arg1, 0)
13498 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13500 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13501 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13504 && real_isfinite (ra)
13505 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13507 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13508 const int prec = fmt->p;
13509 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13513 mpfr_init2 (m, prec);
13514 mpfr_from_real (m, ra, GMP_RNDN);
13515 mpfr_clear_flags ();
13516 inexact = func (m, n, m, rnd);
13517 result = do_mpfr_ckconv (m, type, inexact);
13525 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13526 the pointer *(ARG_QUO) and return the result. The type is taken
13527 from the type of ARG0 and is used for setting the precision of the
13528 calculation and results. */
13531 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13533 tree const type = TREE_TYPE (arg0);
13534 tree result = NULL_TREE;
13539 /* To proceed, MPFR must exactly represent the target floating point
13540 format, which only happens when the target base equals two. */
13541 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13542 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13543 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13545 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13546 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13548 if (real_isfinite (ra0) && real_isfinite (ra1))
13550 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13551 const int prec = fmt->p;
13552 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13557 mpfr_inits2 (prec, m0, m1, NULL);
13558 mpfr_from_real (m0, ra0, GMP_RNDN);
13559 mpfr_from_real (m1, ra1, GMP_RNDN);
13560 mpfr_clear_flags ();
13561 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13562 /* Remquo is independent of the rounding mode, so pass
13563 inexact=0 to do_mpfr_ckconv(). */
13564 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13565 mpfr_clears (m0, m1, NULL);
13568 /* MPFR calculates quo in the host's long so it may
13569 return more bits in quo than the target int can hold
13570 if sizeof(host long) > sizeof(target int). This can
13571 happen even for native compilers in LP64 mode. In
13572 these cases, modulo the quo value with the largest
13573 number that the target int can hold while leaving one
13574 bit for the sign. */
13575 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13576 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13578 /* Dereference the quo pointer argument. */
13579 arg_quo = build_fold_indirect_ref (arg_quo);
13580 /* Proceed iff a valid pointer type was passed in. */
13581 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13583 /* Set the value. */
13584 tree result_quo = fold_build2 (MODIFY_EXPR,
13585 TREE_TYPE (arg_quo), arg_quo,
13586 build_int_cst (NULL, integer_quo));
13587 TREE_SIDE_EFFECTS (result_quo) = 1;
13588 /* Combine the quo assignment with the rem. */
13589 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13590 result_quo, result_rem));
13598 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13599 resulting value as a tree with type TYPE. The mpfr precision is
13600 set to the precision of TYPE. We assume that this mpfr function
13601 returns zero if the result could be calculated exactly within the
13602 requested precision. In addition, the integer pointer represented
13603 by ARG_SG will be dereferenced and set to the appropriate signgam
13607 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13609 tree result = NULL_TREE;
13613 /* To proceed, MPFR must exactly represent the target floating point
13614 format, which only happens when the target base equals two. Also
13615 verify ARG is a constant and that ARG_SG is an int pointer. */
13616 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13617 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13618 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13619 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13621 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13623 /* In addition to NaN and Inf, the argument cannot be zero or a
13624 negative integer. */
13625 if (real_isfinite (ra)
13626 && ra->cl != rvc_zero
13627 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13629 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13630 const int prec = fmt->p;
13631 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13636 mpfr_init2 (m, prec);
13637 mpfr_from_real (m, ra, GMP_RNDN);
13638 mpfr_clear_flags ();
13639 inexact = mpfr_lgamma (m, &sg, m, rnd);
13640 result_lg = do_mpfr_ckconv (m, type, inexact);
13646 /* Dereference the arg_sg pointer argument. */
13647 arg_sg = build_fold_indirect_ref (arg_sg);
13648 /* Assign the signgam value into *arg_sg. */
13649 result_sg = fold_build2 (MODIFY_EXPR,
13650 TREE_TYPE (arg_sg), arg_sg,
13651 build_int_cst (NULL, sg));
13652 TREE_SIDE_EFFECTS (result_sg) = 1;
13653 /* Combine the signgam assignment with the lgamma result. */
13654 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13655 result_sg, result_lg));
13664 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13665 function FUNC on it and return the resulting value as a tree with
13666 type TYPE. The mpfr precision is set to the precision of TYPE. We
13667 assume that function FUNC returns zero if the result could be
13668 calculated exactly within the requested precision. */
13671 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13673 tree result = NULL_TREE;
13677 /* To proceed, MPFR must exactly represent the target floating point
13678 format, which only happens when the target base equals two. */
13679 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13680 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13681 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13683 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13684 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13686 if (real_isfinite (re) && real_isfinite (im))
13688 const struct real_format *const fmt =
13689 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13690 const int prec = fmt->p;
13691 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13692 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13696 mpc_init2 (m, prec);
13697 mpfr_from_real (mpc_realref(m), re, rnd);
13698 mpfr_from_real (mpc_imagref(m), im, rnd);
13699 mpfr_clear_flags ();
13700 inexact = func (m, m, crnd);
13701 result = do_mpc_ckconv (m, type, inexact);
13708 #endif /* HAVE_mpc */
13711 The functions below provide an alternate interface for folding
13712 builtin function calls presented as GIMPLE_CALL statements rather
13713 than as CALL_EXPRs. The folded result is still expressed as a
13714 tree. There is too much code duplication in the handling of
13715 varargs functions, and a more intrusive re-factoring would permit
13716 better sharing of code between the tree and statement-based
13717 versions of these functions. */
13719 /* Construct a new CALL_EXPR using the tail of the argument list of STMT
13720 along with N new arguments specified as the "..." parameters. SKIP
13721 is the number of arguments in STMT to be omitted. This function is used
13722 to do varargs-to-varargs transformations. */
13725 gimple_rewrite_call_expr (gimple stmt, int skip, tree fndecl, int n, ...)
13727 int oldnargs = gimple_call_num_args (stmt);
13728 int nargs = oldnargs - skip + n;
13729 tree fntype = TREE_TYPE (fndecl);
13730 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
13735 buffer = XALLOCAVEC (tree, nargs);
13737 for (i = 0; i < n; i++)
13738 buffer[i] = va_arg (ap, tree);
13740 for (j = skip; j < oldnargs; j++, i++)
13741 buffer[i] = gimple_call_arg (stmt, j);
13743 return fold (build_call_array (TREE_TYPE (fntype), fn, nargs, buffer));
13746 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13747 a normal call should be emitted rather than expanding the function
13748 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13751 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13753 tree dest, size, len, fn, fmt, flag;
13754 const char *fmt_str;
13755 int nargs = gimple_call_num_args (stmt);
13757 /* Verify the required arguments in the original call. */
13760 dest = gimple_call_arg (stmt, 0);
13761 if (!validate_arg (dest, POINTER_TYPE))
13763 flag = gimple_call_arg (stmt, 1);
13764 if (!validate_arg (flag, INTEGER_TYPE))
13766 size = gimple_call_arg (stmt, 2);
13767 if (!validate_arg (size, INTEGER_TYPE))
13769 fmt = gimple_call_arg (stmt, 3);
13770 if (!validate_arg (fmt, POINTER_TYPE))
13773 if (! host_integerp (size, 1))
13778 if (!init_target_chars ())
13781 /* Check whether the format is a literal string constant. */
13782 fmt_str = c_getstr (fmt);
13783 if (fmt_str != NULL)
13785 /* If the format doesn't contain % args or %%, we know the size. */
13786 if (strchr (fmt_str, target_percent) == 0)
13788 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13789 len = build_int_cstu (size_type_node, strlen (fmt_str));
13791 /* If the format is "%s" and first ... argument is a string literal,
13792 we know the size too. */
13793 else if (fcode == BUILT_IN_SPRINTF_CHK
13794 && strcmp (fmt_str, target_percent_s) == 0)
13800 arg = gimple_call_arg (stmt, 4);
13801 if (validate_arg (arg, POINTER_TYPE))
13803 len = c_strlen (arg, 1);
13804 if (! len || ! host_integerp (len, 1))
13811 if (! integer_all_onesp (size))
13813 if (! len || ! tree_int_cst_lt (len, size))
13817 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13818 or if format doesn't contain % chars or is "%s". */
13819 if (! integer_zerop (flag))
13821 if (fmt_str == NULL)
13823 if (strchr (fmt_str, target_percent) != NULL
13824 && strcmp (fmt_str, target_percent_s))
13828 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13829 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
13830 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
13834 return gimple_rewrite_call_expr (stmt, 4, fn, 2, dest, fmt);
13837 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13838 a normal call should be emitted rather than expanding the function
13839 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13840 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13841 passed as second argument. */
13844 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13845 enum built_in_function fcode)
13847 tree dest, size, len, fn, fmt, flag;
13848 const char *fmt_str;
13850 /* Verify the required arguments in the original call. */
13851 if (gimple_call_num_args (stmt) < 5)
13853 dest = gimple_call_arg (stmt, 0);
13854 if (!validate_arg (dest, POINTER_TYPE))
13856 len = gimple_call_arg (stmt, 1);
13857 if (!validate_arg (len, INTEGER_TYPE))
13859 flag = gimple_call_arg (stmt, 2);
13860 if (!validate_arg (flag, INTEGER_TYPE))
13862 size = gimple_call_arg (stmt, 3);
13863 if (!validate_arg (size, INTEGER_TYPE))
13865 fmt = gimple_call_arg (stmt, 4);
13866 if (!validate_arg (fmt, POINTER_TYPE))
13869 if (! host_integerp (size, 1))
13872 if (! integer_all_onesp (size))
13874 if (! host_integerp (len, 1))
13876 /* If LEN is not constant, try MAXLEN too.
13877 For MAXLEN only allow optimizing into non-_ocs function
13878 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13879 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13885 if (tree_int_cst_lt (size, maxlen))
13889 if (!init_target_chars ())
13892 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13893 or if format doesn't contain % chars or is "%s". */
13894 if (! integer_zerop (flag))
13896 fmt_str = c_getstr (fmt);
13897 if (fmt_str == NULL)
13899 if (strchr (fmt_str, target_percent) != NULL
13900 && strcmp (fmt_str, target_percent_s))
13904 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13906 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
13907 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
13911 return gimple_rewrite_call_expr (stmt, 5, fn, 3, dest, len, fmt);
13914 /* Builtins with folding operations that operate on "..." arguments
13915 need special handling; we need to store the arguments in a convenient
13916 data structure before attempting any folding. Fortunately there are
13917 only a few builtins that fall into this category. FNDECL is the
13918 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13919 result of the function call is ignored. */
13922 gimple_fold_builtin_varargs (tree fndecl, gimple stmt, bool ignore ATTRIBUTE_UNUSED)
13924 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13925 tree ret = NULL_TREE;
13929 case BUILT_IN_SPRINTF_CHK:
13930 case BUILT_IN_VSPRINTF_CHK:
13931 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13934 case BUILT_IN_SNPRINTF_CHK:
13935 case BUILT_IN_VSNPRINTF_CHK:
13936 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13943 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13944 TREE_NO_WARNING (ret) = 1;
13950 /* A wrapper function for builtin folding that prevents warnings for
13951 "statement without effect" and the like, caused by removing the
13952 call node earlier than the warning is generated. */
13955 fold_call_stmt (gimple stmt, bool ignore)
13957 tree ret = NULL_TREE;
13958 tree fndecl = gimple_call_fndecl (stmt);
13960 && TREE_CODE (fndecl) == FUNCTION_DECL
13961 && DECL_BUILT_IN (fndecl)
13962 && !gimple_call_va_arg_pack_p (stmt))
13964 int nargs = gimple_call_num_args (stmt);
13966 if (avoid_folding_inline_builtin (fndecl))
13968 /* FIXME: Don't use a list in this interface. */
13969 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13971 tree arglist = NULL_TREE;
13973 for (i = nargs - 1; i >= 0; i--)
13974 arglist = tree_cons (NULL_TREE, gimple_call_arg (stmt, i), arglist);
13975 return targetm.fold_builtin (fndecl, arglist, ignore);
13979 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13981 tree args[MAX_ARGS_TO_FOLD_BUILTIN];
13983 for (i = 0; i < nargs; i++)
13984 args[i] = gimple_call_arg (stmt, i);
13985 ret = fold_builtin_n (fndecl, args, nargs, ignore);
13988 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13991 /* Propagate location information from original call to
13992 expansion of builtin. Otherwise things like
13993 maybe_emit_chk_warning, that operate on the expansion
13994 of a builtin, will use the wrong location information. */
13995 if (gimple_has_location (stmt))
13997 tree realret = ret;
13998 if (TREE_CODE (ret) == NOP_EXPR)
13999 realret = TREE_OPERAND (ret, 0);
14000 if (CAN_HAVE_LOCATION_P (realret)
14001 && !EXPR_HAS_LOCATION (realret))
14002 SET_EXPR_LOCATION (realret, gimple_location (stmt));