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));
64 static tree do_mpc_arg2 (tree, tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t));
68 /* Define the names of the builtin function types and codes. */
69 const char *const built_in_class_names[4]
70 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
72 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
73 const char * built_in_names[(int) END_BUILTINS] =
75 #include "builtins.def"
79 /* Setup an array of _DECL trees, make sure each element is
80 initialized to NULL_TREE. */
81 tree built_in_decls[(int) END_BUILTINS];
82 /* Declarations used when constructing the builtin implicitly in the compiler.
83 It may be NULL_TREE when this is invalid (for instance runtime is not
84 required to implement the function call in all cases). */
85 tree implicit_built_in_decls[(int) END_BUILTINS];
87 static const char *c_getstr (tree);
88 static rtx c_readstr (const char *, enum machine_mode);
89 static int target_char_cast (tree, char *);
90 static rtx get_memory_rtx (tree, tree);
91 static int apply_args_size (void);
92 static int apply_result_size (void);
93 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
94 static rtx result_vector (int, rtx);
96 static void expand_builtin_update_setjmp_buf (rtx);
97 static void expand_builtin_prefetch (tree);
98 static rtx expand_builtin_apply_args (void);
99 static rtx expand_builtin_apply_args_1 (void);
100 static rtx expand_builtin_apply (rtx, rtx, rtx);
101 static void expand_builtin_return (rtx);
102 static enum type_class type_to_class (tree);
103 static rtx expand_builtin_classify_type (tree);
104 static void expand_errno_check (tree, rtx);
105 static rtx expand_builtin_mathfn (tree, rtx, rtx);
106 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
107 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
108 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
109 static rtx expand_builtin_sincos (tree);
110 static rtx expand_builtin_cexpi (tree, rtx, rtx);
111 static rtx expand_builtin_int_roundingfn (tree, rtx);
112 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
113 static rtx expand_builtin_args_info (tree);
114 static rtx expand_builtin_next_arg (void);
115 static rtx expand_builtin_va_start (tree);
116 static rtx expand_builtin_va_end (tree);
117 static rtx expand_builtin_va_copy (tree);
118 static rtx expand_builtin_memchr (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
120 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
122 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
124 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx,
130 enum machine_mode, int);
131 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int);
132 static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx,
133 enum machine_mode, int);
134 static rtx expand_builtin_bcopy (tree, int);
135 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
136 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
137 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
139 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
140 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
141 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
142 static rtx expand_builtin_bzero (tree);
143 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
144 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
145 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
146 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
147 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
148 static rtx expand_builtin_alloca (tree, rtx);
149 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
150 static rtx expand_builtin_frame_address (tree, tree);
151 static rtx expand_builtin_fputs (tree, rtx, bool);
152 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
153 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
154 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
155 static tree stabilize_va_list (tree, int);
156 static rtx expand_builtin_expect (tree, rtx);
157 static tree fold_builtin_constant_p (tree);
158 static tree fold_builtin_expect (tree, tree);
159 static tree fold_builtin_classify_type (tree);
160 static tree fold_builtin_strlen (tree);
161 static tree fold_builtin_inf (tree, int);
162 static tree fold_builtin_nan (tree, tree, int);
163 static tree rewrite_call_expr (tree, int, tree, int, ...);
164 static bool validate_arg (const_tree, enum tree_code code);
165 static bool integer_valued_real_p (tree);
166 static tree fold_trunc_transparent_mathfn (tree, tree);
167 static bool readonly_data_expr (tree);
168 static rtx expand_builtin_fabs (tree, rtx, rtx);
169 static rtx expand_builtin_signbit (tree, rtx);
170 static tree fold_builtin_sqrt (tree, tree);
171 static tree fold_builtin_cbrt (tree, tree);
172 static tree fold_builtin_pow (tree, tree, tree, tree);
173 static tree fold_builtin_powi (tree, tree, tree, tree);
174 static tree fold_builtin_cos (tree, tree, tree);
175 static tree fold_builtin_cosh (tree, tree, tree);
176 static tree fold_builtin_tan (tree, tree);
177 static tree fold_builtin_trunc (tree, tree);
178 static tree fold_builtin_floor (tree, tree);
179 static tree fold_builtin_ceil (tree, tree);
180 static tree fold_builtin_round (tree, tree);
181 static tree fold_builtin_int_roundingfn (tree, tree);
182 static tree fold_builtin_bitop (tree, tree);
183 static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int);
184 static tree fold_builtin_strchr (tree, tree, tree);
185 static tree fold_builtin_memchr (tree, tree, tree, tree);
186 static tree fold_builtin_memcmp (tree, tree, tree);
187 static tree fold_builtin_strcmp (tree, tree);
188 static tree fold_builtin_strncmp (tree, tree, tree);
189 static tree fold_builtin_signbit (tree, tree);
190 static tree fold_builtin_copysign (tree, tree, tree, tree);
191 static tree fold_builtin_isascii (tree);
192 static tree fold_builtin_toascii (tree);
193 static tree fold_builtin_isdigit (tree);
194 static tree fold_builtin_fabs (tree, tree);
195 static tree fold_builtin_abs (tree, tree);
196 static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code,
198 static tree fold_builtin_n (tree, tree *, int, bool);
199 static tree fold_builtin_0 (tree, bool);
200 static tree fold_builtin_1 (tree, tree, bool);
201 static tree fold_builtin_2 (tree, tree, tree, bool);
202 static tree fold_builtin_3 (tree, tree, tree, tree, bool);
203 static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool);
204 static tree fold_builtin_varargs (tree, tree, bool);
206 static tree fold_builtin_strpbrk (tree, tree, tree);
207 static tree fold_builtin_strstr (tree, tree, tree);
208 static tree fold_builtin_strrchr (tree, tree, tree);
209 static tree fold_builtin_strcat (tree, tree);
210 static tree fold_builtin_strncat (tree, tree, tree);
211 static tree fold_builtin_strspn (tree, tree);
212 static tree fold_builtin_strcspn (tree, tree);
213 static tree fold_builtin_sprintf (tree, tree, tree, int);
215 static rtx expand_builtin_object_size (tree);
216 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
217 enum built_in_function);
218 static void maybe_emit_chk_warning (tree, enum built_in_function);
219 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
220 static void maybe_emit_free_warning (tree);
221 static tree fold_builtin_object_size (tree, tree);
222 static tree fold_builtin_strcat_chk (tree, tree, tree, tree);
223 static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree);
224 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
225 static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function);
226 static tree fold_builtin_fprintf (tree, tree, tree, tree, bool,
227 enum built_in_function);
228 static bool init_target_chars (void);
230 static unsigned HOST_WIDE_INT target_newline;
231 static unsigned HOST_WIDE_INT target_percent;
232 static unsigned HOST_WIDE_INT target_c;
233 static unsigned HOST_WIDE_INT target_s;
234 static char target_percent_c[3];
235 static char target_percent_s[3];
236 static char target_percent_s_newline[4];
237 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
238 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
239 static tree do_mpfr_arg2 (tree, tree, tree,
240 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
241 static tree do_mpfr_arg3 (tree, tree, tree, tree,
242 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
243 static tree do_mpfr_sincos (tree, tree, tree);
244 static tree do_mpfr_bessel_n (tree, tree, tree,
245 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
246 const REAL_VALUE_TYPE *, bool);
247 static tree do_mpfr_remquo (tree, tree, tree);
248 static tree do_mpfr_lgamma_r (tree, tree, tree);
251 is_builtin_name (const char *name)
253 if (strncmp (name, "__builtin_", 10) == 0)
255 if (strncmp (name, "__sync_", 7) == 0)
260 /* Return true if NODE should be considered for inline expansion regardless
261 of the optimization level. This means whenever a function is invoked with
262 its "internal" name, which normally contains the prefix "__builtin". */
265 called_as_built_in (tree node)
267 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
268 we want the name used to call the function, not the name it
270 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
271 return is_builtin_name (name);
274 /* Return the alignment in bits of EXP, an object.
275 Don't return more than MAX_ALIGN no matter what, ALIGN is the inital
276 guessed alignment e.g. from type alignment. */
279 get_object_alignment (tree exp, unsigned int align, unsigned int max_align)
284 if (handled_component_p (exp))
286 HOST_WIDE_INT bitsize, bitpos;
288 enum machine_mode mode;
289 int unsignedp, volatilep;
291 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
292 &mode, &unsignedp, &volatilep, true);
294 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
299 if (TREE_CODE (offset) == PLUS_EXPR)
301 next_offset = TREE_OPERAND (offset, 0);
302 offset = TREE_OPERAND (offset, 1);
306 if (host_integerp (offset, 1))
308 /* Any overflow in calculating offset_bits won't change
311 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
314 inner = MIN (inner, (offset_bits & -offset_bits));
316 else if (TREE_CODE (offset) == MULT_EXPR
317 && host_integerp (TREE_OPERAND (offset, 1), 1))
319 /* Any overflow in calculating offset_factor won't change
321 unsigned offset_factor
322 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
326 inner = MIN (inner, (offset_factor & -offset_factor));
330 inner = MIN (inner, BITS_PER_UNIT);
333 offset = next_offset;
337 align = MIN (inner, DECL_ALIGN (exp));
338 #ifdef CONSTANT_ALIGNMENT
339 else if (CONSTANT_CLASS_P (exp))
340 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
342 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
343 || TREE_CODE (exp) == INDIRECT_REF)
344 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
346 align = MIN (align, inner);
347 return MIN (align, max_align);
350 /* Returns true iff we can trust that alignment information has been
351 calculated properly. */
354 can_trust_pointer_alignment (void)
356 /* We rely on TER to compute accurate alignment information. */
357 return (optimize && flag_tree_ter);
360 /* Return the alignment in bits of EXP, a pointer valued expression.
361 But don't return more than MAX_ALIGN no matter what.
362 The alignment returned is, by default, the alignment of the thing that
363 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
365 Otherwise, look at the expression to see if we can do better, i.e., if the
366 expression is actually pointing at an object whose alignment is tighter. */
369 get_pointer_alignment (tree exp, unsigned int max_align)
371 unsigned int align, inner;
373 if (!can_trust_pointer_alignment ())
376 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
379 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
380 align = MIN (align, max_align);
384 switch (TREE_CODE (exp))
387 exp = TREE_OPERAND (exp, 0);
388 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
391 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
392 align = MIN (inner, max_align);
395 case POINTER_PLUS_EXPR:
396 /* If sum of pointer + int, restrict our maximum alignment to that
397 imposed by the integer. If not, we can't do any better than
399 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
402 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
403 & (max_align / BITS_PER_UNIT - 1))
407 exp = TREE_OPERAND (exp, 0);
411 /* See what we are pointing at and look at its alignment. */
412 return get_object_alignment (TREE_OPERAND (exp, 0), align, max_align);
420 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
421 way, because it could contain a zero byte in the middle.
422 TREE_STRING_LENGTH is the size of the character array, not the string.
424 ONLY_VALUE should be nonzero if the result is not going to be emitted
425 into the instruction stream and zero if it is going to be expanded.
426 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
427 is returned, otherwise NULL, since
428 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
429 evaluate the side-effects.
431 The value returned is of type `ssizetype'.
433 Unfortunately, string_constant can't access the values of const char
434 arrays with initializers, so neither can we do so here. */
437 c_strlen (tree src, int only_value)
440 HOST_WIDE_INT offset;
445 if (TREE_CODE (src) == COND_EXPR
446 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
450 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
451 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
452 if (tree_int_cst_equal (len1, len2))
456 if (TREE_CODE (src) == COMPOUND_EXPR
457 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
458 return c_strlen (TREE_OPERAND (src, 1), only_value);
460 src = string_constant (src, &offset_node);
464 max = TREE_STRING_LENGTH (src) - 1;
465 ptr = TREE_STRING_POINTER (src);
467 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
469 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
470 compute the offset to the following null if we don't know where to
471 start searching for it. */
474 for (i = 0; i < max; i++)
478 /* We don't know the starting offset, but we do know that the string
479 has no internal zero bytes. We can assume that the offset falls
480 within the bounds of the string; otherwise, the programmer deserves
481 what he gets. Subtract the offset from the length of the string,
482 and return that. This would perhaps not be valid if we were dealing
483 with named arrays in addition to literal string constants. */
485 return size_diffop (size_int (max), offset_node);
488 /* We have a known offset into the string. Start searching there for
489 a null character if we can represent it as a single HOST_WIDE_INT. */
490 if (offset_node == 0)
492 else if (! host_integerp (offset_node, 0))
495 offset = tree_low_cst (offset_node, 0);
497 /* If the offset is known to be out of bounds, warn, and call strlen at
499 if (offset < 0 || offset > max)
501 /* Suppress multiple warnings for propagated constant strings. */
502 if (! TREE_NO_WARNING (src))
504 warning (0, "offset outside bounds of constant string");
505 TREE_NO_WARNING (src) = 1;
510 /* Use strlen to search for the first zero byte. Since any strings
511 constructed with build_string will have nulls appended, we win even
512 if we get handed something like (char[4])"abcd".
514 Since OFFSET is our starting index into the string, no further
515 calculation is needed. */
516 return ssize_int (strlen (ptr + offset));
519 /* Return a char pointer for a C string if it is a string constant
520 or sum of string constant and integer constant. */
527 src = string_constant (src, &offset_node);
531 if (offset_node == 0)
532 return TREE_STRING_POINTER (src);
533 else if (!host_integerp (offset_node, 1)
534 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
537 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
540 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
541 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
544 c_readstr (const char *str, enum machine_mode mode)
550 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
555 for (i = 0; i < GET_MODE_SIZE (mode); i++)
558 if (WORDS_BIG_ENDIAN)
559 j = GET_MODE_SIZE (mode) - i - 1;
560 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
561 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
562 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
564 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
567 ch = (unsigned char) str[i];
568 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
570 return immed_double_const (c[0], c[1], mode);
573 /* Cast a target constant CST to target CHAR and if that value fits into
574 host char type, return zero and put that value into variable pointed to by
578 target_char_cast (tree cst, char *p)
580 unsigned HOST_WIDE_INT val, hostval;
582 if (!host_integerp (cst, 1)
583 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
586 val = tree_low_cst (cst, 1);
587 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
588 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
591 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
592 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
601 /* Similar to save_expr, but assumes that arbitrary code is not executed
602 in between the multiple evaluations. In particular, we assume that a
603 non-addressable local variable will not be modified. */
606 builtin_save_expr (tree exp)
608 if (TREE_ADDRESSABLE (exp) == 0
609 && (TREE_CODE (exp) == PARM_DECL
610 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
613 return save_expr (exp);
616 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
617 times to get the address of either a higher stack frame, or a return
618 address located within it (depending on FNDECL_CODE). */
621 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
625 #ifdef INITIAL_FRAME_ADDRESS_RTX
626 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
630 /* For a zero count with __builtin_return_address, we don't care what
631 frame address we return, because target-specific definitions will
632 override us. Therefore frame pointer elimination is OK, and using
633 the soft frame pointer is OK.
635 For a nonzero count, or a zero count with __builtin_frame_address,
636 we require a stable offset from the current frame pointer to the
637 previous one, so we must use the hard frame pointer, and
638 we must disable frame pointer elimination. */
639 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
640 tem = frame_pointer_rtx;
643 tem = hard_frame_pointer_rtx;
645 /* Tell reload not to eliminate the frame pointer. */
646 crtl->accesses_prior_frames = 1;
650 /* Some machines need special handling before we can access
651 arbitrary frames. For example, on the SPARC, we must first flush
652 all register windows to the stack. */
653 #ifdef SETUP_FRAME_ADDRESSES
655 SETUP_FRAME_ADDRESSES ();
658 /* On the SPARC, the return address is not in the frame, it is in a
659 register. There is no way to access it off of the current frame
660 pointer, but it can be accessed off the previous frame pointer by
661 reading the value from the register window save area. */
662 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
663 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
667 /* Scan back COUNT frames to the specified frame. */
668 for (i = 0; i < count; i++)
670 /* Assume the dynamic chain pointer is in the word that the
671 frame address points to, unless otherwise specified. */
672 #ifdef DYNAMIC_CHAIN_ADDRESS
673 tem = DYNAMIC_CHAIN_ADDRESS (tem);
675 tem = memory_address (Pmode, tem);
676 tem = gen_frame_mem (Pmode, tem);
677 tem = copy_to_reg (tem);
680 /* For __builtin_frame_address, return what we've got. But, on
681 the SPARC for example, we may have to add a bias. */
682 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
683 #ifdef FRAME_ADDR_RTX
684 return FRAME_ADDR_RTX (tem);
689 /* For __builtin_return_address, get the return address from that frame. */
690 #ifdef RETURN_ADDR_RTX
691 tem = RETURN_ADDR_RTX (count, tem);
693 tem = memory_address (Pmode,
694 plus_constant (tem, GET_MODE_SIZE (Pmode)));
695 tem = gen_frame_mem (Pmode, tem);
700 /* Alias set used for setjmp buffer. */
701 static alias_set_type setjmp_alias_set = -1;
703 /* Construct the leading half of a __builtin_setjmp call. Control will
704 return to RECEIVER_LABEL. This is also called directly by the SJLJ
705 exception handling code. */
708 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
710 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
714 if (setjmp_alias_set == -1)
715 setjmp_alias_set = new_alias_set ();
717 buf_addr = convert_memory_address (Pmode, buf_addr);
719 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
721 /* We store the frame pointer and the address of receiver_label in
722 the buffer and use the rest of it for the stack save area, which
723 is machine-dependent. */
725 mem = gen_rtx_MEM (Pmode, buf_addr);
726 set_mem_alias_set (mem, setjmp_alias_set);
727 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
729 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
730 set_mem_alias_set (mem, setjmp_alias_set);
732 emit_move_insn (validize_mem (mem),
733 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
735 stack_save = gen_rtx_MEM (sa_mode,
736 plus_constant (buf_addr,
737 2 * GET_MODE_SIZE (Pmode)));
738 set_mem_alias_set (stack_save, setjmp_alias_set);
739 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
741 /* If there is further processing to do, do it. */
742 #ifdef HAVE_builtin_setjmp_setup
743 if (HAVE_builtin_setjmp_setup)
744 emit_insn (gen_builtin_setjmp_setup (buf_addr));
747 /* Tell optimize_save_area_alloca that extra work is going to
748 need to go on during alloca. */
749 cfun->calls_setjmp = 1;
751 /* We have a nonlocal label. */
752 cfun->has_nonlocal_label = 1;
755 /* Construct the trailing part of a __builtin_setjmp call. This is
756 also called directly by the SJLJ exception handling code. */
759 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
761 /* Clobber the FP when we get here, so we have to make sure it's
762 marked as used by this function. */
763 emit_use (hard_frame_pointer_rtx);
765 /* Mark the static chain as clobbered here so life information
766 doesn't get messed up for it. */
767 emit_clobber (static_chain_rtx);
769 /* Now put in the code to restore the frame pointer, and argument
770 pointer, if needed. */
771 #ifdef HAVE_nonlocal_goto
772 if (! HAVE_nonlocal_goto)
775 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
776 /* This might change the hard frame pointer in ways that aren't
777 apparent to early optimization passes, so force a clobber. */
778 emit_clobber (hard_frame_pointer_rtx);
781 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
782 if (fixed_regs[ARG_POINTER_REGNUM])
784 #ifdef ELIMINABLE_REGS
786 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
788 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
789 if (elim_regs[i].from == ARG_POINTER_REGNUM
790 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
793 if (i == ARRAY_SIZE (elim_regs))
796 /* Now restore our arg pointer from the address at which it
797 was saved in our stack frame. */
798 emit_move_insn (crtl->args.internal_arg_pointer,
799 copy_to_reg (get_arg_pointer_save_area ()));
804 #ifdef HAVE_builtin_setjmp_receiver
805 if (HAVE_builtin_setjmp_receiver)
806 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
809 #ifdef HAVE_nonlocal_goto_receiver
810 if (HAVE_nonlocal_goto_receiver)
811 emit_insn (gen_nonlocal_goto_receiver ());
816 /* We must not allow the code we just generated to be reordered by
817 scheduling. Specifically, the update of the frame pointer must
818 happen immediately, not later. */
819 emit_insn (gen_blockage ());
822 /* __builtin_longjmp is passed a pointer to an array of five words (not
823 all will be used on all machines). It operates similarly to the C
824 library function of the same name, but is more efficient. Much of
825 the code below is copied from the handling of non-local gotos. */
828 expand_builtin_longjmp (rtx buf_addr, rtx value)
830 rtx fp, lab, stack, insn, last;
831 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
833 /* DRAP is needed for stack realign if longjmp is expanded to current
835 if (SUPPORTS_STACK_ALIGNMENT)
836 crtl->need_drap = true;
838 if (setjmp_alias_set == -1)
839 setjmp_alias_set = new_alias_set ();
841 buf_addr = convert_memory_address (Pmode, buf_addr);
843 buf_addr = force_reg (Pmode, buf_addr);
845 /* We used to store value in static_chain_rtx, but that fails if pointers
846 are smaller than integers. We instead require that the user must pass
847 a second argument of 1, because that is what builtin_setjmp will
848 return. This also makes EH slightly more efficient, since we are no
849 longer copying around a value that we don't care about. */
850 gcc_assert (value == const1_rtx);
852 last = get_last_insn ();
853 #ifdef HAVE_builtin_longjmp
854 if (HAVE_builtin_longjmp)
855 emit_insn (gen_builtin_longjmp (buf_addr));
859 fp = gen_rtx_MEM (Pmode, buf_addr);
860 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
861 GET_MODE_SIZE (Pmode)));
863 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
864 2 * GET_MODE_SIZE (Pmode)));
865 set_mem_alias_set (fp, setjmp_alias_set);
866 set_mem_alias_set (lab, setjmp_alias_set);
867 set_mem_alias_set (stack, setjmp_alias_set);
869 /* Pick up FP, label, and SP from the block and jump. This code is
870 from expand_goto in stmt.c; see there for detailed comments. */
871 #ifdef HAVE_nonlocal_goto
872 if (HAVE_nonlocal_goto)
873 /* We have to pass a value to the nonlocal_goto pattern that will
874 get copied into the static_chain pointer, but it does not matter
875 what that value is, because builtin_setjmp does not use it. */
876 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
880 lab = copy_to_reg (lab);
882 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
883 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
885 emit_move_insn (hard_frame_pointer_rtx, fp);
886 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
888 emit_use (hard_frame_pointer_rtx);
889 emit_use (stack_pointer_rtx);
890 emit_indirect_jump (lab);
894 /* Search backwards and mark the jump insn as a non-local goto.
895 Note that this precludes the use of __builtin_longjmp to a
896 __builtin_setjmp target in the same function. However, we've
897 already cautioned the user that these functions are for
898 internal exception handling use only. */
899 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
901 gcc_assert (insn != last);
905 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
908 else if (CALL_P (insn))
913 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
914 and the address of the save area. */
917 expand_builtin_nonlocal_goto (tree exp)
919 tree t_label, t_save_area;
920 rtx r_label, r_save_area, r_fp, r_sp, insn;
922 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
925 t_label = CALL_EXPR_ARG (exp, 0);
926 t_save_area = CALL_EXPR_ARG (exp, 1);
928 r_label = expand_normal (t_label);
929 r_label = convert_memory_address (Pmode, r_label);
930 r_save_area = expand_normal (t_save_area);
931 r_save_area = convert_memory_address (Pmode, r_save_area);
932 /* Copy the address of the save location to a register just in case it was based
933 on the frame pointer. */
934 r_save_area = copy_to_reg (r_save_area);
935 r_fp = gen_rtx_MEM (Pmode, r_save_area);
936 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
937 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
939 crtl->has_nonlocal_goto = 1;
941 #ifdef HAVE_nonlocal_goto
942 /* ??? We no longer need to pass the static chain value, afaik. */
943 if (HAVE_nonlocal_goto)
944 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
948 r_label = copy_to_reg (r_label);
950 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
951 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
953 /* Restore frame pointer for containing function.
954 This sets the actual hard register used for the frame pointer
955 to the location of the function's incoming static chain info.
956 The non-local goto handler will then adjust it to contain the
957 proper value and reload the argument pointer, if needed. */
958 emit_move_insn (hard_frame_pointer_rtx, r_fp);
959 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
961 /* USE of hard_frame_pointer_rtx added for consistency;
962 not clear if really needed. */
963 emit_use (hard_frame_pointer_rtx);
964 emit_use (stack_pointer_rtx);
966 /* If the architecture is using a GP register, we must
967 conservatively assume that the target function makes use of it.
968 The prologue of functions with nonlocal gotos must therefore
969 initialize the GP register to the appropriate value, and we
970 must then make sure that this value is live at the point
971 of the jump. (Note that this doesn't necessarily apply
972 to targets with a nonlocal_goto pattern; they are free
973 to implement it in their own way. Note also that this is
974 a no-op if the GP register is a global invariant.) */
975 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
976 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
977 emit_use (pic_offset_table_rtx);
979 emit_indirect_jump (r_label);
982 /* Search backwards to the jump insn and mark it as a
984 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
988 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
991 else if (CALL_P (insn))
998 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
999 (not all will be used on all machines) that was passed to __builtin_setjmp.
1000 It updates the stack pointer in that block to correspond to the current
1004 expand_builtin_update_setjmp_buf (rtx buf_addr)
1006 enum machine_mode sa_mode = Pmode;
1010 #ifdef HAVE_save_stack_nonlocal
1011 if (HAVE_save_stack_nonlocal)
1012 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1014 #ifdef STACK_SAVEAREA_MODE
1015 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1019 = gen_rtx_MEM (sa_mode,
1022 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1026 emit_insn (gen_setjmp ());
1029 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1032 /* Expand a call to __builtin_prefetch. For a target that does not support
1033 data prefetch, evaluate the memory address argument in case it has side
1037 expand_builtin_prefetch (tree exp)
1039 tree arg0, arg1, arg2;
1043 if (!validate_arglist (exp, POINTER_TYPE, 0))
1046 arg0 = CALL_EXPR_ARG (exp, 0);
1048 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1049 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1051 nargs = call_expr_nargs (exp);
1053 arg1 = CALL_EXPR_ARG (exp, 1);
1055 arg1 = integer_zero_node;
1057 arg2 = CALL_EXPR_ARG (exp, 2);
1059 arg2 = build_int_cst (NULL_TREE, 3);
1061 /* Argument 0 is an address. */
1062 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1064 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1065 if (TREE_CODE (arg1) != INTEGER_CST)
1067 error ("second argument to %<__builtin_prefetch%> must be a constant");
1068 arg1 = integer_zero_node;
1070 op1 = expand_normal (arg1);
1071 /* Argument 1 must be either zero or one. */
1072 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1074 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1079 /* Argument 2 (locality) must be a compile-time constant int. */
1080 if (TREE_CODE (arg2) != INTEGER_CST)
1082 error ("third argument to %<__builtin_prefetch%> must be a constant");
1083 arg2 = integer_zero_node;
1085 op2 = expand_normal (arg2);
1086 /* Argument 2 must be 0, 1, 2, or 3. */
1087 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1089 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1093 #ifdef HAVE_prefetch
1096 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1098 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1099 || (GET_MODE (op0) != Pmode))
1101 op0 = convert_memory_address (Pmode, op0);
1102 op0 = force_reg (Pmode, op0);
1104 emit_insn (gen_prefetch (op0, op1, op2));
1108 /* Don't do anything with direct references to volatile memory, but
1109 generate code to handle other side effects. */
1110 if (!MEM_P (op0) && side_effects_p (op0))
1114 /* Get a MEM rtx for expression EXP which is the address of an operand
1115 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1116 the maximum length of the block of memory that might be accessed or
1120 get_memory_rtx (tree exp, tree len)
1122 tree orig_exp = exp;
1126 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1127 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1128 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1129 exp = TREE_OPERAND (exp, 0);
1131 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1132 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1134 /* Get an expression we can use to find the attributes to assign to MEM.
1135 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1136 we can. First remove any nops. */
1137 while (CONVERT_EXPR_P (exp)
1138 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1139 exp = TREE_OPERAND (exp, 0);
1142 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1143 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1144 && host_integerp (TREE_OPERAND (exp, 1), 0)
1145 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1146 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1147 else if (TREE_CODE (exp) == ADDR_EXPR)
1148 exp = TREE_OPERAND (exp, 0);
1149 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1150 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1154 /* Honor attributes derived from exp, except for the alias set
1155 (as builtin stringops may alias with anything) and the size
1156 (as stringops may access multiple array elements). */
1159 set_mem_attributes (mem, exp, 0);
1162 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1164 /* Allow the string and memory builtins to overflow from one
1165 field into another, see http://gcc.gnu.org/PR23561.
1166 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1167 memory accessed by the string or memory builtin will fit
1168 within the field. */
1169 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1171 tree mem_expr = MEM_EXPR (mem);
1172 HOST_WIDE_INT offset = -1, length = -1;
1175 while (TREE_CODE (inner) == ARRAY_REF
1176 || CONVERT_EXPR_P (inner)
1177 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1178 || TREE_CODE (inner) == SAVE_EXPR)
1179 inner = TREE_OPERAND (inner, 0);
1181 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1183 if (MEM_OFFSET (mem)
1184 && CONST_INT_P (MEM_OFFSET (mem)))
1185 offset = INTVAL (MEM_OFFSET (mem));
1187 if (offset >= 0 && len && host_integerp (len, 0))
1188 length = tree_low_cst (len, 0);
1190 while (TREE_CODE (inner) == COMPONENT_REF)
1192 tree field = TREE_OPERAND (inner, 1);
1193 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1194 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1196 /* Bitfields are generally not byte-addressable. */
1197 gcc_assert (!DECL_BIT_FIELD (field)
1198 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1199 % BITS_PER_UNIT) == 0
1200 && host_integerp (DECL_SIZE (field), 0)
1201 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1202 % BITS_PER_UNIT) == 0));
1204 /* If we can prove that the memory starting at XEXP (mem, 0) and
1205 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1206 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1207 fields without DECL_SIZE_UNIT like flexible array members. */
1209 && DECL_SIZE_UNIT (field)
1210 && host_integerp (DECL_SIZE_UNIT (field), 0))
1213 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1216 && offset + length <= size)
1221 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1222 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1223 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1231 mem_expr = TREE_OPERAND (mem_expr, 0);
1232 inner = TREE_OPERAND (inner, 0);
1235 if (mem_expr == NULL)
1237 if (mem_expr != MEM_EXPR (mem))
1239 set_mem_expr (mem, mem_expr);
1240 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1243 set_mem_alias_set (mem, 0);
1244 set_mem_size (mem, NULL_RTX);
1250 /* Built-in functions to perform an untyped call and return. */
1252 /* For each register that may be used for calling a function, this
1253 gives a mode used to copy the register's value. VOIDmode indicates
1254 the register is not used for calling a function. If the machine
1255 has register windows, this gives only the outbound registers.
1256 INCOMING_REGNO gives the corresponding inbound register. */
1257 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1259 /* For each register that may be used for returning values, this gives
1260 a mode used to copy the register's value. VOIDmode indicates the
1261 register is not used for returning values. If the machine has
1262 register windows, this gives only the outbound registers.
1263 INCOMING_REGNO gives the corresponding inbound register. */
1264 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1266 /* For each register that may be used for calling a function, this
1267 gives the offset of that register into the block returned by
1268 __builtin_apply_args. 0 indicates that the register is not
1269 used for calling a function. */
1270 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1272 /* Return the size required for the block returned by __builtin_apply_args,
1273 and initialize apply_args_mode. */
1276 apply_args_size (void)
1278 static int size = -1;
1281 enum machine_mode mode;
1283 /* The values computed by this function never change. */
1286 /* The first value is the incoming arg-pointer. */
1287 size = GET_MODE_SIZE (Pmode);
1289 /* The second value is the structure value address unless this is
1290 passed as an "invisible" first argument. */
1291 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1292 size += GET_MODE_SIZE (Pmode);
1294 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1295 if (FUNCTION_ARG_REGNO_P (regno))
1297 mode = reg_raw_mode[regno];
1299 gcc_assert (mode != VOIDmode);
1301 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1302 if (size % align != 0)
1303 size = CEIL (size, align) * align;
1304 apply_args_reg_offset[regno] = size;
1305 size += GET_MODE_SIZE (mode);
1306 apply_args_mode[regno] = mode;
1310 apply_args_mode[regno] = VOIDmode;
1311 apply_args_reg_offset[regno] = 0;
1317 /* Return the size required for the block returned by __builtin_apply,
1318 and initialize apply_result_mode. */
1321 apply_result_size (void)
1323 static int size = -1;
1325 enum machine_mode mode;
1327 /* The values computed by this function never change. */
1332 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1333 if (FUNCTION_VALUE_REGNO_P (regno))
1335 mode = reg_raw_mode[regno];
1337 gcc_assert (mode != VOIDmode);
1339 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1340 if (size % align != 0)
1341 size = CEIL (size, align) * align;
1342 size += GET_MODE_SIZE (mode);
1343 apply_result_mode[regno] = mode;
1346 apply_result_mode[regno] = VOIDmode;
1348 /* Allow targets that use untyped_call and untyped_return to override
1349 the size so that machine-specific information can be stored here. */
1350 #ifdef APPLY_RESULT_SIZE
1351 size = APPLY_RESULT_SIZE;
1357 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1358 /* Create a vector describing the result block RESULT. If SAVEP is true,
1359 the result block is used to save the values; otherwise it is used to
1360 restore the values. */
1363 result_vector (int savep, rtx result)
1365 int regno, size, align, nelts;
1366 enum machine_mode mode;
1368 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1371 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1372 if ((mode = apply_result_mode[regno]) != VOIDmode)
1374 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1375 if (size % align != 0)
1376 size = CEIL (size, align) * align;
1377 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1378 mem = adjust_address (result, mode, size);
1379 savevec[nelts++] = (savep
1380 ? gen_rtx_SET (VOIDmode, mem, reg)
1381 : gen_rtx_SET (VOIDmode, reg, mem));
1382 size += GET_MODE_SIZE (mode);
1384 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1386 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1388 /* Save the state required to perform an untyped call with the same
1389 arguments as were passed to the current function. */
1392 expand_builtin_apply_args_1 (void)
1395 int size, align, regno;
1396 enum machine_mode mode;
1397 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1399 /* Create a block where the arg-pointer, structure value address,
1400 and argument registers can be saved. */
1401 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1403 /* Walk past the arg-pointer and structure value address. */
1404 size = GET_MODE_SIZE (Pmode);
1405 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1406 size += GET_MODE_SIZE (Pmode);
1408 /* Save each register used in calling a function to the block. */
1409 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1410 if ((mode = apply_args_mode[regno]) != VOIDmode)
1412 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1413 if (size % align != 0)
1414 size = CEIL (size, align) * align;
1416 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1418 emit_move_insn (adjust_address (registers, mode, size), tem);
1419 size += GET_MODE_SIZE (mode);
1422 /* Save the arg pointer to the block. */
1423 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1424 #ifdef STACK_GROWS_DOWNWARD
1425 /* We need the pointer as the caller actually passed them to us, not
1426 as we might have pretended they were passed. Make sure it's a valid
1427 operand, as emit_move_insn isn't expected to handle a PLUS. */
1429 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1432 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1434 size = GET_MODE_SIZE (Pmode);
1436 /* Save the structure value address unless this is passed as an
1437 "invisible" first argument. */
1438 if (struct_incoming_value)
1440 emit_move_insn (adjust_address (registers, Pmode, size),
1441 copy_to_reg (struct_incoming_value));
1442 size += GET_MODE_SIZE (Pmode);
1445 /* Return the address of the block. */
1446 return copy_addr_to_reg (XEXP (registers, 0));
1449 /* __builtin_apply_args returns block of memory allocated on
1450 the stack into which is stored the arg pointer, structure
1451 value address, static chain, and all the registers that might
1452 possibly be used in performing a function call. The code is
1453 moved to the start of the function so the incoming values are
1457 expand_builtin_apply_args (void)
1459 /* Don't do __builtin_apply_args more than once in a function.
1460 Save the result of the first call and reuse it. */
1461 if (apply_args_value != 0)
1462 return apply_args_value;
1464 /* When this function is called, it means that registers must be
1465 saved on entry to this function. So we migrate the
1466 call to the first insn of this function. */
1471 temp = expand_builtin_apply_args_1 ();
1475 apply_args_value = temp;
1477 /* Put the insns after the NOTE that starts the function.
1478 If this is inside a start_sequence, make the outer-level insn
1479 chain current, so the code is placed at the start of the
1480 function. If internal_arg_pointer is a non-virtual pseudo,
1481 it needs to be placed after the function that initializes
1483 push_topmost_sequence ();
1484 if (REG_P (crtl->args.internal_arg_pointer)
1485 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1486 emit_insn_before (seq, parm_birth_insn);
1488 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1489 pop_topmost_sequence ();
1494 /* Perform an untyped call and save the state required to perform an
1495 untyped return of whatever value was returned by the given function. */
1498 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1500 int size, align, regno;
1501 enum machine_mode mode;
1502 rtx incoming_args, result, reg, dest, src, call_insn;
1503 rtx old_stack_level = 0;
1504 rtx call_fusage = 0;
1505 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1507 arguments = convert_memory_address (Pmode, arguments);
1509 /* Create a block where the return registers can be saved. */
1510 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1512 /* Fetch the arg pointer from the ARGUMENTS block. */
1513 incoming_args = gen_reg_rtx (Pmode);
1514 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1515 #ifndef STACK_GROWS_DOWNWARD
1516 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1517 incoming_args, 0, OPTAB_LIB_WIDEN);
1520 /* Push a new argument block and copy the arguments. Do not allow
1521 the (potential) memcpy call below to interfere with our stack
1523 do_pending_stack_adjust ();
1526 /* Save the stack with nonlocal if available. */
1527 #ifdef HAVE_save_stack_nonlocal
1528 if (HAVE_save_stack_nonlocal)
1529 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1532 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1534 /* Allocate a block of memory onto the stack and copy the memory
1535 arguments to the outgoing arguments address. */
1536 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1538 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1539 may have already set current_function_calls_alloca to true.
1540 current_function_calls_alloca won't be set if argsize is zero,
1541 so we have to guarantee need_drap is true here. */
1542 if (SUPPORTS_STACK_ALIGNMENT)
1543 crtl->need_drap = true;
1545 dest = virtual_outgoing_args_rtx;
1546 #ifndef STACK_GROWS_DOWNWARD
1547 if (CONST_INT_P (argsize))
1548 dest = plus_constant (dest, -INTVAL (argsize));
1550 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1552 dest = gen_rtx_MEM (BLKmode, dest);
1553 set_mem_align (dest, PARM_BOUNDARY);
1554 src = gen_rtx_MEM (BLKmode, incoming_args);
1555 set_mem_align (src, PARM_BOUNDARY);
1556 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1558 /* Refer to the argument block. */
1560 arguments = gen_rtx_MEM (BLKmode, arguments);
1561 set_mem_align (arguments, PARM_BOUNDARY);
1563 /* Walk past the arg-pointer and structure value address. */
1564 size = GET_MODE_SIZE (Pmode);
1566 size += GET_MODE_SIZE (Pmode);
1568 /* Restore each of the registers previously saved. Make USE insns
1569 for each of these registers for use in making the call. */
1570 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1571 if ((mode = apply_args_mode[regno]) != VOIDmode)
1573 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1574 if (size % align != 0)
1575 size = CEIL (size, align) * align;
1576 reg = gen_rtx_REG (mode, regno);
1577 emit_move_insn (reg, adjust_address (arguments, mode, size));
1578 use_reg (&call_fusage, reg);
1579 size += GET_MODE_SIZE (mode);
1582 /* Restore the structure value address unless this is passed as an
1583 "invisible" first argument. */
1584 size = GET_MODE_SIZE (Pmode);
1587 rtx value = gen_reg_rtx (Pmode);
1588 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1589 emit_move_insn (struct_value, value);
1590 if (REG_P (struct_value))
1591 use_reg (&call_fusage, struct_value);
1592 size += GET_MODE_SIZE (Pmode);
1595 /* All arguments and registers used for the call are set up by now! */
1596 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1598 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1599 and we don't want to load it into a register as an optimization,
1600 because prepare_call_address already did it if it should be done. */
1601 if (GET_CODE (function) != SYMBOL_REF)
1602 function = memory_address (FUNCTION_MODE, function);
1604 /* Generate the actual call instruction and save the return value. */
1605 #ifdef HAVE_untyped_call
1606 if (HAVE_untyped_call)
1607 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1608 result, result_vector (1, result)));
1611 #ifdef HAVE_call_value
1612 if (HAVE_call_value)
1616 /* Locate the unique return register. It is not possible to
1617 express a call that sets more than one return register using
1618 call_value; use untyped_call for that. In fact, untyped_call
1619 only needs to save the return registers in the given block. */
1620 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1621 if ((mode = apply_result_mode[regno]) != VOIDmode)
1623 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1625 valreg = gen_rtx_REG (mode, regno);
1628 emit_call_insn (GEN_CALL_VALUE (valreg,
1629 gen_rtx_MEM (FUNCTION_MODE, function),
1630 const0_rtx, NULL_RTX, const0_rtx));
1632 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1638 /* Find the CALL insn we just emitted, and attach the register usage
1640 call_insn = last_call_insn ();
1641 add_function_usage_to (call_insn, call_fusage);
1643 /* Restore the stack. */
1644 #ifdef HAVE_save_stack_nonlocal
1645 if (HAVE_save_stack_nonlocal)
1646 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1649 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1653 /* Return the address of the result block. */
1654 result = copy_addr_to_reg (XEXP (result, 0));
1655 return convert_memory_address (ptr_mode, result);
1658 /* Perform an untyped return. */
1661 expand_builtin_return (rtx result)
1663 int size, align, regno;
1664 enum machine_mode mode;
1666 rtx call_fusage = 0;
1668 result = convert_memory_address (Pmode, result);
1670 apply_result_size ();
1671 result = gen_rtx_MEM (BLKmode, result);
1673 #ifdef HAVE_untyped_return
1674 if (HAVE_untyped_return)
1676 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1682 /* Restore the return value and note that each value is used. */
1684 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1685 if ((mode = apply_result_mode[regno]) != VOIDmode)
1687 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1688 if (size % align != 0)
1689 size = CEIL (size, align) * align;
1690 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1691 emit_move_insn (reg, adjust_address (result, mode, size));
1693 push_to_sequence (call_fusage);
1695 call_fusage = get_insns ();
1697 size += GET_MODE_SIZE (mode);
1700 /* Put the USE insns before the return. */
1701 emit_insn (call_fusage);
1703 /* Return whatever values was restored by jumping directly to the end
1705 expand_naked_return ();
1708 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1710 static enum type_class
1711 type_to_class (tree type)
1713 switch (TREE_CODE (type))
1715 case VOID_TYPE: return void_type_class;
1716 case INTEGER_TYPE: return integer_type_class;
1717 case ENUMERAL_TYPE: return enumeral_type_class;
1718 case BOOLEAN_TYPE: return boolean_type_class;
1719 case POINTER_TYPE: return pointer_type_class;
1720 case REFERENCE_TYPE: return reference_type_class;
1721 case OFFSET_TYPE: return offset_type_class;
1722 case REAL_TYPE: return real_type_class;
1723 case COMPLEX_TYPE: return complex_type_class;
1724 case FUNCTION_TYPE: return function_type_class;
1725 case METHOD_TYPE: return method_type_class;
1726 case RECORD_TYPE: return record_type_class;
1728 case QUAL_UNION_TYPE: return union_type_class;
1729 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1730 ? string_type_class : array_type_class);
1731 case LANG_TYPE: return lang_type_class;
1732 default: return no_type_class;
1736 /* Expand a call EXP to __builtin_classify_type. */
1739 expand_builtin_classify_type (tree exp)
1741 if (call_expr_nargs (exp))
1742 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1743 return GEN_INT (no_type_class);
1746 /* This helper macro, meant to be used in mathfn_built_in below,
1747 determines which among a set of three builtin math functions is
1748 appropriate for a given type mode. The `F' and `L' cases are
1749 automatically generated from the `double' case. */
1750 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1751 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1752 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1753 fcodel = BUILT_IN_MATHFN##L ; break;
1754 /* Similar to above, but appends _R after any F/L suffix. */
1755 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1756 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1757 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1758 fcodel = BUILT_IN_MATHFN##L_R ; break;
1760 /* Return mathematic function equivalent to FN but operating directly
1761 on TYPE, if available. If IMPLICIT is true find the function in
1762 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1763 can't do the conversion, return zero. */
1766 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1768 tree const *const fn_arr
1769 = implicit ? implicit_built_in_decls : built_in_decls;
1770 enum built_in_function fcode, fcodef, fcodel;
1774 CASE_MATHFN (BUILT_IN_ACOS)
1775 CASE_MATHFN (BUILT_IN_ACOSH)
1776 CASE_MATHFN (BUILT_IN_ASIN)
1777 CASE_MATHFN (BUILT_IN_ASINH)
1778 CASE_MATHFN (BUILT_IN_ATAN)
1779 CASE_MATHFN (BUILT_IN_ATAN2)
1780 CASE_MATHFN (BUILT_IN_ATANH)
1781 CASE_MATHFN (BUILT_IN_CBRT)
1782 CASE_MATHFN (BUILT_IN_CEIL)
1783 CASE_MATHFN (BUILT_IN_CEXPI)
1784 CASE_MATHFN (BUILT_IN_COPYSIGN)
1785 CASE_MATHFN (BUILT_IN_COS)
1786 CASE_MATHFN (BUILT_IN_COSH)
1787 CASE_MATHFN (BUILT_IN_DREM)
1788 CASE_MATHFN (BUILT_IN_ERF)
1789 CASE_MATHFN (BUILT_IN_ERFC)
1790 CASE_MATHFN (BUILT_IN_EXP)
1791 CASE_MATHFN (BUILT_IN_EXP10)
1792 CASE_MATHFN (BUILT_IN_EXP2)
1793 CASE_MATHFN (BUILT_IN_EXPM1)
1794 CASE_MATHFN (BUILT_IN_FABS)
1795 CASE_MATHFN (BUILT_IN_FDIM)
1796 CASE_MATHFN (BUILT_IN_FLOOR)
1797 CASE_MATHFN (BUILT_IN_FMA)
1798 CASE_MATHFN (BUILT_IN_FMAX)
1799 CASE_MATHFN (BUILT_IN_FMIN)
1800 CASE_MATHFN (BUILT_IN_FMOD)
1801 CASE_MATHFN (BUILT_IN_FREXP)
1802 CASE_MATHFN (BUILT_IN_GAMMA)
1803 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1804 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1805 CASE_MATHFN (BUILT_IN_HYPOT)
1806 CASE_MATHFN (BUILT_IN_ILOGB)
1807 CASE_MATHFN (BUILT_IN_INF)
1808 CASE_MATHFN (BUILT_IN_ISINF)
1809 CASE_MATHFN (BUILT_IN_J0)
1810 CASE_MATHFN (BUILT_IN_J1)
1811 CASE_MATHFN (BUILT_IN_JN)
1812 CASE_MATHFN (BUILT_IN_LCEIL)
1813 CASE_MATHFN (BUILT_IN_LDEXP)
1814 CASE_MATHFN (BUILT_IN_LFLOOR)
1815 CASE_MATHFN (BUILT_IN_LGAMMA)
1816 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1817 CASE_MATHFN (BUILT_IN_LLCEIL)
1818 CASE_MATHFN (BUILT_IN_LLFLOOR)
1819 CASE_MATHFN (BUILT_IN_LLRINT)
1820 CASE_MATHFN (BUILT_IN_LLROUND)
1821 CASE_MATHFN (BUILT_IN_LOG)
1822 CASE_MATHFN (BUILT_IN_LOG10)
1823 CASE_MATHFN (BUILT_IN_LOG1P)
1824 CASE_MATHFN (BUILT_IN_LOG2)
1825 CASE_MATHFN (BUILT_IN_LOGB)
1826 CASE_MATHFN (BUILT_IN_LRINT)
1827 CASE_MATHFN (BUILT_IN_LROUND)
1828 CASE_MATHFN (BUILT_IN_MODF)
1829 CASE_MATHFN (BUILT_IN_NAN)
1830 CASE_MATHFN (BUILT_IN_NANS)
1831 CASE_MATHFN (BUILT_IN_NEARBYINT)
1832 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1833 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1834 CASE_MATHFN (BUILT_IN_POW)
1835 CASE_MATHFN (BUILT_IN_POWI)
1836 CASE_MATHFN (BUILT_IN_POW10)
1837 CASE_MATHFN (BUILT_IN_REMAINDER)
1838 CASE_MATHFN (BUILT_IN_REMQUO)
1839 CASE_MATHFN (BUILT_IN_RINT)
1840 CASE_MATHFN (BUILT_IN_ROUND)
1841 CASE_MATHFN (BUILT_IN_SCALB)
1842 CASE_MATHFN (BUILT_IN_SCALBLN)
1843 CASE_MATHFN (BUILT_IN_SCALBN)
1844 CASE_MATHFN (BUILT_IN_SIGNBIT)
1845 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1846 CASE_MATHFN (BUILT_IN_SIN)
1847 CASE_MATHFN (BUILT_IN_SINCOS)
1848 CASE_MATHFN (BUILT_IN_SINH)
1849 CASE_MATHFN (BUILT_IN_SQRT)
1850 CASE_MATHFN (BUILT_IN_TAN)
1851 CASE_MATHFN (BUILT_IN_TANH)
1852 CASE_MATHFN (BUILT_IN_TGAMMA)
1853 CASE_MATHFN (BUILT_IN_TRUNC)
1854 CASE_MATHFN (BUILT_IN_Y0)
1855 CASE_MATHFN (BUILT_IN_Y1)
1856 CASE_MATHFN (BUILT_IN_YN)
1862 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1863 return fn_arr[fcode];
1864 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1865 return fn_arr[fcodef];
1866 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1867 return fn_arr[fcodel];
1872 /* Like mathfn_built_in_1(), but always use the implicit array. */
1875 mathfn_built_in (tree type, enum built_in_function fn)
1877 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1880 /* If errno must be maintained, expand the RTL to check if the result,
1881 TARGET, of a built-in function call, EXP, is NaN, and if so set
1885 expand_errno_check (tree exp, rtx target)
1887 rtx lab = gen_label_rtx ();
1889 /* Test the result; if it is NaN, set errno=EDOM because
1890 the argument was not in the domain. */
1891 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1892 NULL_RTX, NULL_RTX, lab);
1895 /* If this built-in doesn't throw an exception, set errno directly. */
1896 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1898 #ifdef GEN_ERRNO_RTX
1899 rtx errno_rtx = GEN_ERRNO_RTX;
1902 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1904 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1910 /* Make sure the library call isn't expanded as a tail call. */
1911 CALL_EXPR_TAILCALL (exp) = 0;
1913 /* We can't set errno=EDOM directly; let the library call do it.
1914 Pop the arguments right away in case the call gets deleted. */
1916 expand_call (exp, target, 0);
1921 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1922 Return NULL_RTX if a normal call should be emitted rather than expanding
1923 the function in-line. EXP is the expression that is a call to the builtin
1924 function; if convenient, the result should be placed in TARGET.
1925 SUBTARGET may be used as the target for computing one of EXP's operands. */
1928 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1930 optab builtin_optab;
1931 rtx op0, insns, before_call;
1932 tree fndecl = get_callee_fndecl (exp);
1933 enum machine_mode mode;
1934 bool errno_set = false;
1937 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1940 arg = CALL_EXPR_ARG (exp, 0);
1942 switch (DECL_FUNCTION_CODE (fndecl))
1944 CASE_FLT_FN (BUILT_IN_SQRT):
1945 errno_set = ! tree_expr_nonnegative_p (arg);
1946 builtin_optab = sqrt_optab;
1948 CASE_FLT_FN (BUILT_IN_EXP):
1949 errno_set = true; builtin_optab = exp_optab; break;
1950 CASE_FLT_FN (BUILT_IN_EXP10):
1951 CASE_FLT_FN (BUILT_IN_POW10):
1952 errno_set = true; builtin_optab = exp10_optab; break;
1953 CASE_FLT_FN (BUILT_IN_EXP2):
1954 errno_set = true; builtin_optab = exp2_optab; break;
1955 CASE_FLT_FN (BUILT_IN_EXPM1):
1956 errno_set = true; builtin_optab = expm1_optab; break;
1957 CASE_FLT_FN (BUILT_IN_LOGB):
1958 errno_set = true; builtin_optab = logb_optab; break;
1959 CASE_FLT_FN (BUILT_IN_LOG):
1960 errno_set = true; builtin_optab = log_optab; break;
1961 CASE_FLT_FN (BUILT_IN_LOG10):
1962 errno_set = true; builtin_optab = log10_optab; break;
1963 CASE_FLT_FN (BUILT_IN_LOG2):
1964 errno_set = true; builtin_optab = log2_optab; break;
1965 CASE_FLT_FN (BUILT_IN_LOG1P):
1966 errno_set = true; builtin_optab = log1p_optab; break;
1967 CASE_FLT_FN (BUILT_IN_ASIN):
1968 builtin_optab = asin_optab; break;
1969 CASE_FLT_FN (BUILT_IN_ACOS):
1970 builtin_optab = acos_optab; break;
1971 CASE_FLT_FN (BUILT_IN_TAN):
1972 builtin_optab = tan_optab; break;
1973 CASE_FLT_FN (BUILT_IN_ATAN):
1974 builtin_optab = atan_optab; break;
1975 CASE_FLT_FN (BUILT_IN_FLOOR):
1976 builtin_optab = floor_optab; break;
1977 CASE_FLT_FN (BUILT_IN_CEIL):
1978 builtin_optab = ceil_optab; break;
1979 CASE_FLT_FN (BUILT_IN_TRUNC):
1980 builtin_optab = btrunc_optab; break;
1981 CASE_FLT_FN (BUILT_IN_ROUND):
1982 builtin_optab = round_optab; break;
1983 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1984 builtin_optab = nearbyint_optab;
1985 if (flag_trapping_math)
1987 /* Else fallthrough and expand as rint. */
1988 CASE_FLT_FN (BUILT_IN_RINT):
1989 builtin_optab = rint_optab; break;
1990 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
1991 builtin_optab = significand_optab; break;
1996 /* Make a suitable register to place result in. */
1997 mode = TYPE_MODE (TREE_TYPE (exp));
1999 if (! flag_errno_math || ! HONOR_NANS (mode))
2002 /* Before working hard, check whether the instruction is available. */
2003 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2005 target = gen_reg_rtx (mode);
2007 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2008 need to expand the argument again. This way, we will not perform
2009 side-effects more the once. */
2010 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2012 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2016 /* Compute into TARGET.
2017 Set TARGET to wherever the result comes back. */
2018 target = expand_unop (mode, builtin_optab, op0, target, 0);
2023 expand_errno_check (exp, target);
2025 /* Output the entire sequence. */
2026 insns = get_insns ();
2032 /* If we were unable to expand via the builtin, stop the sequence
2033 (without outputting the insns) and call to the library function
2034 with the stabilized argument list. */
2038 before_call = get_last_insn ();
2040 return expand_call (exp, target, target == const0_rtx);
2043 /* Expand a call to the builtin binary math functions (pow and atan2).
2044 Return NULL_RTX if a normal call should be emitted rather than expanding the
2045 function in-line. EXP is the expression that is a call to the builtin
2046 function; if convenient, the result should be placed in TARGET.
2047 SUBTARGET may be used as the target for computing one of EXP's
2051 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2053 optab builtin_optab;
2054 rtx op0, op1, insns;
2055 int op1_type = REAL_TYPE;
2056 tree fndecl = get_callee_fndecl (exp);
2058 enum machine_mode mode;
2059 bool errno_set = true;
2061 switch (DECL_FUNCTION_CODE (fndecl))
2063 CASE_FLT_FN (BUILT_IN_SCALBN):
2064 CASE_FLT_FN (BUILT_IN_SCALBLN):
2065 CASE_FLT_FN (BUILT_IN_LDEXP):
2066 op1_type = INTEGER_TYPE;
2071 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2074 arg0 = CALL_EXPR_ARG (exp, 0);
2075 arg1 = CALL_EXPR_ARG (exp, 1);
2077 switch (DECL_FUNCTION_CODE (fndecl))
2079 CASE_FLT_FN (BUILT_IN_POW):
2080 builtin_optab = pow_optab; break;
2081 CASE_FLT_FN (BUILT_IN_ATAN2):
2082 builtin_optab = atan2_optab; break;
2083 CASE_FLT_FN (BUILT_IN_SCALB):
2084 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2086 builtin_optab = scalb_optab; break;
2087 CASE_FLT_FN (BUILT_IN_SCALBN):
2088 CASE_FLT_FN (BUILT_IN_SCALBLN):
2089 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2091 /* Fall through... */
2092 CASE_FLT_FN (BUILT_IN_LDEXP):
2093 builtin_optab = ldexp_optab; break;
2094 CASE_FLT_FN (BUILT_IN_FMOD):
2095 builtin_optab = fmod_optab; break;
2096 CASE_FLT_FN (BUILT_IN_REMAINDER):
2097 CASE_FLT_FN (BUILT_IN_DREM):
2098 builtin_optab = remainder_optab; break;
2103 /* Make a suitable register to place result in. */
2104 mode = TYPE_MODE (TREE_TYPE (exp));
2106 /* Before working hard, check whether the instruction is available. */
2107 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2110 target = gen_reg_rtx (mode);
2112 if (! flag_errno_math || ! HONOR_NANS (mode))
2115 /* Always stabilize the argument list. */
2116 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2117 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2119 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2120 op1 = expand_normal (arg1);
2124 /* Compute into TARGET.
2125 Set TARGET to wherever the result comes back. */
2126 target = expand_binop (mode, builtin_optab, op0, op1,
2127 target, 0, OPTAB_DIRECT);
2129 /* If we were unable to expand via the builtin, stop the sequence
2130 (without outputting the insns) and call to the library function
2131 with the stabilized argument list. */
2135 return expand_call (exp, target, target == const0_rtx);
2139 expand_errno_check (exp, target);
2141 /* Output the entire sequence. */
2142 insns = get_insns ();
2149 /* Expand a call to the builtin sin and cos math functions.
2150 Return NULL_RTX if a normal call should be emitted rather than expanding the
2151 function in-line. EXP is the expression that is a call to the builtin
2152 function; if convenient, the result should be placed in TARGET.
2153 SUBTARGET may be used as the target for computing one of EXP's
2157 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2159 optab builtin_optab;
2161 tree fndecl = get_callee_fndecl (exp);
2162 enum machine_mode mode;
2165 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2168 arg = CALL_EXPR_ARG (exp, 0);
2170 switch (DECL_FUNCTION_CODE (fndecl))
2172 CASE_FLT_FN (BUILT_IN_SIN):
2173 CASE_FLT_FN (BUILT_IN_COS):
2174 builtin_optab = sincos_optab; break;
2179 /* Make a suitable register to place result in. */
2180 mode = TYPE_MODE (TREE_TYPE (exp));
2182 /* Check if sincos insn is available, otherwise fallback
2183 to sin or cos insn. */
2184 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2185 switch (DECL_FUNCTION_CODE (fndecl))
2187 CASE_FLT_FN (BUILT_IN_SIN):
2188 builtin_optab = sin_optab; break;
2189 CASE_FLT_FN (BUILT_IN_COS):
2190 builtin_optab = cos_optab; break;
2195 /* Before working hard, check whether the instruction is available. */
2196 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2198 target = gen_reg_rtx (mode);
2200 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2201 need to expand the argument again. This way, we will not perform
2202 side-effects more the once. */
2203 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2205 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2209 /* Compute into TARGET.
2210 Set TARGET to wherever the result comes back. */
2211 if (builtin_optab == sincos_optab)
2215 switch (DECL_FUNCTION_CODE (fndecl))
2217 CASE_FLT_FN (BUILT_IN_SIN):
2218 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2220 CASE_FLT_FN (BUILT_IN_COS):
2221 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2226 gcc_assert (result);
2230 target = expand_unop (mode, builtin_optab, op0, target, 0);
2235 /* Output the entire sequence. */
2236 insns = get_insns ();
2242 /* If we were unable to expand via the builtin, stop the sequence
2243 (without outputting the insns) and call to the library function
2244 with the stabilized argument list. */
2248 target = expand_call (exp, target, target == const0_rtx);
2253 /* Expand a call to one of the builtin math functions that operate on
2254 floating point argument and output an integer result (ilogb, isinf,
2256 Return 0 if a normal call should be emitted rather than expanding the
2257 function in-line. EXP is the expression that is a call to the builtin
2258 function; if convenient, the result should be placed in TARGET.
2259 SUBTARGET may be used as the target for computing one of EXP's operands. */
2262 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2264 optab builtin_optab = 0;
2265 enum insn_code icode = CODE_FOR_nothing;
2267 tree fndecl = get_callee_fndecl (exp);
2268 enum machine_mode mode;
2269 bool errno_set = false;
2272 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2275 arg = CALL_EXPR_ARG (exp, 0);
2277 switch (DECL_FUNCTION_CODE (fndecl))
2279 CASE_FLT_FN (BUILT_IN_ILOGB):
2280 errno_set = true; builtin_optab = ilogb_optab; break;
2281 CASE_FLT_FN (BUILT_IN_ISINF):
2282 builtin_optab = isinf_optab; break;
2283 case BUILT_IN_ISNORMAL:
2284 case BUILT_IN_ISFINITE:
2285 CASE_FLT_FN (BUILT_IN_FINITE):
2286 /* These builtins have no optabs (yet). */
2292 /* There's no easy way to detect the case we need to set EDOM. */
2293 if (flag_errno_math && errno_set)
2296 /* Optab mode depends on the mode of the input argument. */
2297 mode = TYPE_MODE (TREE_TYPE (arg));
2300 icode = optab_handler (builtin_optab, mode)->insn_code;
2302 /* Before working hard, check whether the instruction is available. */
2303 if (icode != CODE_FOR_nothing)
2305 /* Make a suitable register to place result in. */
2307 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2308 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2310 gcc_assert (insn_data[icode].operand[0].predicate
2311 (target, GET_MODE (target)));
2313 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2314 need to expand the argument again. This way, we will not perform
2315 side-effects more the once. */
2316 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2318 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2320 if (mode != GET_MODE (op0))
2321 op0 = convert_to_mode (mode, op0, 0);
2323 /* Compute into TARGET.
2324 Set TARGET to wherever the result comes back. */
2325 emit_unop_insn (icode, target, op0, UNKNOWN);
2329 /* If there is no optab, try generic code. */
2330 switch (DECL_FUNCTION_CODE (fndecl))
2334 CASE_FLT_FN (BUILT_IN_ISINF):
2336 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
2337 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
2338 tree const type = TREE_TYPE (arg);
2342 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2343 real_from_string (&r, buf);
2344 result = build_call_expr (isgr_fn, 2,
2345 fold_build1 (ABS_EXPR, type, arg),
2346 build_real (type, r));
2347 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2349 CASE_FLT_FN (BUILT_IN_FINITE):
2350 case BUILT_IN_ISFINITE:
2352 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
2353 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2354 tree const type = TREE_TYPE (arg);
2358 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2359 real_from_string (&r, buf);
2360 result = build_call_expr (isle_fn, 2,
2361 fold_build1 (ABS_EXPR, type, arg),
2362 build_real (type, r));
2363 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2365 case BUILT_IN_ISNORMAL:
2367 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
2368 islessequal(fabs(x),DBL_MAX). */
2369 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2370 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
2371 tree const type = TREE_TYPE (arg);
2372 REAL_VALUE_TYPE rmax, rmin;
2375 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2376 real_from_string (&rmax, buf);
2377 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
2378 real_from_string (&rmin, buf);
2379 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
2380 result = build_call_expr (isle_fn, 2, arg,
2381 build_real (type, rmax));
2382 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
2383 build_call_expr (isge_fn, 2, arg,
2384 build_real (type, rmin)));
2385 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2391 target = expand_call (exp, target, target == const0_rtx);
2396 /* Expand a call to the builtin sincos math function.
2397 Return NULL_RTX if a normal call should be emitted rather than expanding the
2398 function in-line. EXP is the expression that is a call to the builtin
2402 expand_builtin_sincos (tree exp)
2404 rtx op0, op1, op2, target1, target2;
2405 enum machine_mode mode;
2406 tree arg, sinp, cosp;
2409 if (!validate_arglist (exp, REAL_TYPE,
2410 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2413 arg = CALL_EXPR_ARG (exp, 0);
2414 sinp = CALL_EXPR_ARG (exp, 1);
2415 cosp = CALL_EXPR_ARG (exp, 2);
2417 /* Make a suitable register to place result in. */
2418 mode = TYPE_MODE (TREE_TYPE (arg));
2420 /* Check if sincos insn is available, otherwise emit the call. */
2421 if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing)
2424 target1 = gen_reg_rtx (mode);
2425 target2 = gen_reg_rtx (mode);
2427 op0 = expand_normal (arg);
2428 op1 = expand_normal (build_fold_indirect_ref (sinp));
2429 op2 = expand_normal (build_fold_indirect_ref (cosp));
2431 /* Compute into target1 and target2.
2432 Set TARGET to wherever the result comes back. */
2433 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2434 gcc_assert (result);
2436 /* Move target1 and target2 to the memory locations indicated
2438 emit_move_insn (op1, target1);
2439 emit_move_insn (op2, target2);
2444 /* Expand a call to the internal cexpi builtin to the sincos math function.
2445 EXP is the expression that is a call to the builtin function; if convenient,
2446 the result should be placed in TARGET. SUBTARGET may be used as the target
2447 for computing one of EXP's operands. */
2450 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2452 tree fndecl = get_callee_fndecl (exp);
2454 enum machine_mode mode;
2457 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2460 arg = CALL_EXPR_ARG (exp, 0);
2461 type = TREE_TYPE (arg);
2462 mode = TYPE_MODE (TREE_TYPE (arg));
2464 /* Try expanding via a sincos optab, fall back to emitting a libcall
2465 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2466 is only generated from sincos, cexp or if we have either of them. */
2467 if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing)
2469 op1 = gen_reg_rtx (mode);
2470 op2 = gen_reg_rtx (mode);
2472 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2474 /* Compute into op1 and op2. */
2475 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2477 else if (TARGET_HAS_SINCOS)
2479 tree call, fn = NULL_TREE;
2483 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2484 fn = built_in_decls[BUILT_IN_SINCOSF];
2485 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2486 fn = built_in_decls[BUILT_IN_SINCOS];
2487 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2488 fn = built_in_decls[BUILT_IN_SINCOSL];
2492 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2493 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2494 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2495 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2496 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2497 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2499 /* Make sure not to fold the sincos call again. */
2500 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2501 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2502 call, 3, arg, top1, top2));
2506 tree call, fn = NULL_TREE, narg;
2507 tree ctype = build_complex_type (type);
2509 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2510 fn = built_in_decls[BUILT_IN_CEXPF];
2511 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2512 fn = built_in_decls[BUILT_IN_CEXP];
2513 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2514 fn = built_in_decls[BUILT_IN_CEXPL];
2518 /* If we don't have a decl for cexp create one. This is the
2519 friendliest fallback if the user calls __builtin_cexpi
2520 without full target C99 function support. */
2521 if (fn == NULL_TREE)
2524 const char *name = NULL;
2526 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2528 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2530 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2533 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2534 fn = build_fn_decl (name, fntype);
2537 narg = fold_build2 (COMPLEX_EXPR, ctype,
2538 build_real (type, dconst0), arg);
2540 /* Make sure not to fold the cexp call again. */
2541 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2542 return expand_expr (build_call_nary (ctype, call, 1, narg),
2543 target, VOIDmode, EXPAND_NORMAL);
2546 /* Now build the proper return type. */
2547 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2548 make_tree (TREE_TYPE (arg), op2),
2549 make_tree (TREE_TYPE (arg), op1)),
2550 target, VOIDmode, EXPAND_NORMAL);
2553 /* Expand a call to one of the builtin rounding functions gcc defines
2554 as an extension (lfloor and lceil). As these are gcc extensions we
2555 do not need to worry about setting errno to EDOM.
2556 If expanding via optab fails, lower expression to (int)(floor(x)).
2557 EXP is the expression that is a call to the builtin function;
2558 if convenient, the result should be placed in TARGET. */
2561 expand_builtin_int_roundingfn (tree exp, rtx target)
2563 convert_optab builtin_optab;
2564 rtx op0, insns, tmp;
2565 tree fndecl = get_callee_fndecl (exp);
2566 enum built_in_function fallback_fn;
2567 tree fallback_fndecl;
2568 enum machine_mode mode;
2571 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2574 arg = CALL_EXPR_ARG (exp, 0);
2576 switch (DECL_FUNCTION_CODE (fndecl))
2578 CASE_FLT_FN (BUILT_IN_LCEIL):
2579 CASE_FLT_FN (BUILT_IN_LLCEIL):
2580 builtin_optab = lceil_optab;
2581 fallback_fn = BUILT_IN_CEIL;
2584 CASE_FLT_FN (BUILT_IN_LFLOOR):
2585 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2586 builtin_optab = lfloor_optab;
2587 fallback_fn = BUILT_IN_FLOOR;
2594 /* Make a suitable register to place result in. */
2595 mode = TYPE_MODE (TREE_TYPE (exp));
2597 target = gen_reg_rtx (mode);
2599 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2600 need to expand the argument again. This way, we will not perform
2601 side-effects more the once. */
2602 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2604 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2608 /* Compute into TARGET. */
2609 if (expand_sfix_optab (target, op0, builtin_optab))
2611 /* Output the entire sequence. */
2612 insns = get_insns ();
2618 /* If we were unable to expand via the builtin, stop the sequence
2619 (without outputting the insns). */
2622 /* Fall back to floating point rounding optab. */
2623 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2625 /* For non-C99 targets we may end up without a fallback fndecl here
2626 if the user called __builtin_lfloor directly. In this case emit
2627 a call to the floor/ceil variants nevertheless. This should result
2628 in the best user experience for not full C99 targets. */
2629 if (fallback_fndecl == NULL_TREE)
2632 const char *name = NULL;
2634 switch (DECL_FUNCTION_CODE (fndecl))
2636 case BUILT_IN_LCEIL:
2637 case BUILT_IN_LLCEIL:
2640 case BUILT_IN_LCEILF:
2641 case BUILT_IN_LLCEILF:
2644 case BUILT_IN_LCEILL:
2645 case BUILT_IN_LLCEILL:
2648 case BUILT_IN_LFLOOR:
2649 case BUILT_IN_LLFLOOR:
2652 case BUILT_IN_LFLOORF:
2653 case BUILT_IN_LLFLOORF:
2656 case BUILT_IN_LFLOORL:
2657 case BUILT_IN_LLFLOORL:
2664 fntype = build_function_type_list (TREE_TYPE (arg),
2665 TREE_TYPE (arg), NULL_TREE);
2666 fallback_fndecl = build_fn_decl (name, fntype);
2669 exp = build_call_expr (fallback_fndecl, 1, arg);
2671 tmp = expand_normal (exp);
2673 /* Truncate the result of floating point optab to integer
2674 via expand_fix (). */
2675 target = gen_reg_rtx (mode);
2676 expand_fix (target, tmp, 0);
2681 /* Expand a call to one of the builtin math functions doing integer
2683 Return 0 if a normal call should be emitted rather than expanding the
2684 function in-line. EXP is the expression that is a call to the builtin
2685 function; if convenient, the result should be placed in TARGET. */
2688 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2690 convert_optab builtin_optab;
2692 tree fndecl = get_callee_fndecl (exp);
2694 enum machine_mode mode;
2696 /* There's no easy way to detect the case we need to set EDOM. */
2697 if (flag_errno_math)
2700 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2703 arg = CALL_EXPR_ARG (exp, 0);
2705 switch (DECL_FUNCTION_CODE (fndecl))
2707 CASE_FLT_FN (BUILT_IN_LRINT):
2708 CASE_FLT_FN (BUILT_IN_LLRINT):
2709 builtin_optab = lrint_optab; break;
2710 CASE_FLT_FN (BUILT_IN_LROUND):
2711 CASE_FLT_FN (BUILT_IN_LLROUND):
2712 builtin_optab = lround_optab; break;
2717 /* Make a suitable register to place result in. */
2718 mode = TYPE_MODE (TREE_TYPE (exp));
2720 target = gen_reg_rtx (mode);
2722 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2723 need to expand the argument again. This way, we will not perform
2724 side-effects more the once. */
2725 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2727 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2731 if (expand_sfix_optab (target, op0, builtin_optab))
2733 /* Output the entire sequence. */
2734 insns = get_insns ();
2740 /* If we were unable to expand via the builtin, stop the sequence
2741 (without outputting the insns) and call to the library function
2742 with the stabilized argument list. */
2745 target = expand_call (exp, target, target == const0_rtx);
2750 /* To evaluate powi(x,n), the floating point value x raised to the
2751 constant integer exponent n, we use a hybrid algorithm that
2752 combines the "window method" with look-up tables. For an
2753 introduction to exponentiation algorithms and "addition chains",
2754 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2755 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2756 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2757 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2759 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2760 multiplications to inline before calling the system library's pow
2761 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2762 so this default never requires calling pow, powf or powl. */
2764 #ifndef POWI_MAX_MULTS
2765 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2768 /* The size of the "optimal power tree" lookup table. All
2769 exponents less than this value are simply looked up in the
2770 powi_table below. This threshold is also used to size the
2771 cache of pseudo registers that hold intermediate results. */
2772 #define POWI_TABLE_SIZE 256
2774 /* The size, in bits of the window, used in the "window method"
2775 exponentiation algorithm. This is equivalent to a radix of
2776 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2777 #define POWI_WINDOW_SIZE 3
2779 /* The following table is an efficient representation of an
2780 "optimal power tree". For each value, i, the corresponding
2781 value, j, in the table states than an optimal evaluation
2782 sequence for calculating pow(x,i) can be found by evaluating
2783 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2784 100 integers is given in Knuth's "Seminumerical algorithms". */
2786 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2788 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2789 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2790 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2791 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2792 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2793 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2794 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2795 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2796 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2797 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2798 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2799 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2800 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2801 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2802 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2803 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2804 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2805 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2806 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2807 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2808 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2809 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2810 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2811 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2812 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2813 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2814 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2815 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2816 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2817 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2818 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2819 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2823 /* Return the number of multiplications required to calculate
2824 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2825 subroutine of powi_cost. CACHE is an array indicating
2826 which exponents have already been calculated. */
2829 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2831 /* If we've already calculated this exponent, then this evaluation
2832 doesn't require any additional multiplications. */
2837 return powi_lookup_cost (n - powi_table[n], cache)
2838 + powi_lookup_cost (powi_table[n], cache) + 1;
2841 /* Return the number of multiplications required to calculate
2842 powi(x,n) for an arbitrary x, given the exponent N. This
2843 function needs to be kept in sync with expand_powi below. */
2846 powi_cost (HOST_WIDE_INT n)
2848 bool cache[POWI_TABLE_SIZE];
2849 unsigned HOST_WIDE_INT digit;
2850 unsigned HOST_WIDE_INT val;
2856 /* Ignore the reciprocal when calculating the cost. */
2857 val = (n < 0) ? -n : n;
2859 /* Initialize the exponent cache. */
2860 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2865 while (val >= POWI_TABLE_SIZE)
2869 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2870 result += powi_lookup_cost (digit, cache)
2871 + POWI_WINDOW_SIZE + 1;
2872 val >>= POWI_WINDOW_SIZE;
2881 return result + powi_lookup_cost (val, cache);
2884 /* Recursive subroutine of expand_powi. This function takes the array,
2885 CACHE, of already calculated exponents and an exponent N and returns
2886 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2889 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2891 unsigned HOST_WIDE_INT digit;
2895 if (n < POWI_TABLE_SIZE)
2900 target = gen_reg_rtx (mode);
2903 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2904 op1 = expand_powi_1 (mode, powi_table[n], cache);
2908 target = gen_reg_rtx (mode);
2909 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2910 op0 = expand_powi_1 (mode, n - digit, cache);
2911 op1 = expand_powi_1 (mode, digit, cache);
2915 target = gen_reg_rtx (mode);
2916 op0 = expand_powi_1 (mode, n >> 1, cache);
2920 result = expand_mult (mode, op0, op1, target, 0);
2921 if (result != target)
2922 emit_move_insn (target, result);
2926 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2927 floating point operand in mode MODE, and N is the exponent. This
2928 function needs to be kept in sync with powi_cost above. */
2931 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2933 unsigned HOST_WIDE_INT val;
2934 rtx cache[POWI_TABLE_SIZE];
2938 return CONST1_RTX (mode);
2940 val = (n < 0) ? -n : n;
2942 memset (cache, 0, sizeof (cache));
2945 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2947 /* If the original exponent was negative, reciprocate the result. */
2949 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2950 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2955 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2956 a normal call should be emitted rather than expanding the function
2957 in-line. EXP is the expression that is a call to the builtin
2958 function; if convenient, the result should be placed in TARGET. */
2961 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2965 tree type = TREE_TYPE (exp);
2966 REAL_VALUE_TYPE cint, c, c2;
2969 enum machine_mode mode = TYPE_MODE (type);
2971 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2974 arg0 = CALL_EXPR_ARG (exp, 0);
2975 arg1 = CALL_EXPR_ARG (exp, 1);
2977 if (TREE_CODE (arg1) != REAL_CST
2978 || TREE_OVERFLOW (arg1))
2979 return expand_builtin_mathfn_2 (exp, target, subtarget);
2981 /* Handle constant exponents. */
2983 /* For integer valued exponents we can expand to an optimal multiplication
2984 sequence using expand_powi. */
2985 c = TREE_REAL_CST (arg1);
2986 n = real_to_integer (&c);
2987 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2988 if (real_identical (&c, &cint)
2989 && ((n >= -1 && n <= 2)
2990 || (flag_unsafe_math_optimizations
2991 && optimize_insn_for_speed_p ()
2992 && powi_cost (n) <= POWI_MAX_MULTS)))
2994 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2997 op = force_reg (mode, op);
2998 op = expand_powi (op, mode, n);
3003 narg0 = builtin_save_expr (arg0);
3005 /* If the exponent is not integer valued, check if it is half of an integer.
3006 In this case we can expand to sqrt (x) * x**(n/2). */
3007 fn = mathfn_built_in (type, BUILT_IN_SQRT);
3008 if (fn != NULL_TREE)
3010 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3011 n = real_to_integer (&c2);
3012 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3013 if (real_identical (&c2, &cint)
3014 && ((flag_unsafe_math_optimizations
3015 && optimize_insn_for_speed_p ()
3016 && powi_cost (n/2) <= POWI_MAX_MULTS)
3019 tree call_expr = build_call_expr (fn, 1, narg0);
3020 /* Use expand_expr in case the newly built call expression
3021 was folded to a non-call. */
3022 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3025 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3026 op2 = force_reg (mode, op2);
3027 op2 = expand_powi (op2, mode, abs (n / 2));
3028 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3029 0, OPTAB_LIB_WIDEN);
3030 /* If the original exponent was negative, reciprocate the
3033 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3034 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3040 /* Try if the exponent is a third of an integer. In this case
3041 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3042 different from pow (x, 1./3.) due to rounding and behavior
3043 with negative x we need to constrain this transformation to
3044 unsafe math and positive x or finite math. */
3045 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3047 && flag_unsafe_math_optimizations
3048 && (tree_expr_nonnegative_p (arg0)
3049 || !HONOR_NANS (mode)))
3051 REAL_VALUE_TYPE dconst3;
3052 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3053 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3054 real_round (&c2, mode, &c2);
3055 n = real_to_integer (&c2);
3056 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3057 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3058 real_convert (&c2, mode, &c2);
3059 if (real_identical (&c2, &c)
3060 && ((optimize_insn_for_speed_p ()
3061 && powi_cost (n/3) <= POWI_MAX_MULTS)
3064 tree call_expr = build_call_expr (fn, 1,narg0);
3065 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3066 if (abs (n) % 3 == 2)
3067 op = expand_simple_binop (mode, MULT, op, op, op,
3068 0, OPTAB_LIB_WIDEN);
3071 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3072 op2 = force_reg (mode, op2);
3073 op2 = expand_powi (op2, mode, abs (n / 3));
3074 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3075 0, OPTAB_LIB_WIDEN);
3076 /* If the original exponent was negative, reciprocate the
3079 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3080 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3086 /* Fall back to optab expansion. */
3087 return expand_builtin_mathfn_2 (exp, target, subtarget);
3090 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3091 a normal call should be emitted rather than expanding the function
3092 in-line. EXP is the expression that is a call to the builtin
3093 function; if convenient, the result should be placed in TARGET. */
3096 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3100 enum machine_mode mode;
3101 enum machine_mode mode2;
3103 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3106 arg0 = CALL_EXPR_ARG (exp, 0);
3107 arg1 = CALL_EXPR_ARG (exp, 1);
3108 mode = TYPE_MODE (TREE_TYPE (exp));
3110 /* Handle constant power. */
3112 if (TREE_CODE (arg1) == INTEGER_CST
3113 && !TREE_OVERFLOW (arg1))
3115 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3117 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3118 Otherwise, check the number of multiplications required. */
3119 if ((TREE_INT_CST_HIGH (arg1) == 0
3120 || TREE_INT_CST_HIGH (arg1) == -1)
3121 && ((n >= -1 && n <= 2)
3122 || (optimize_insn_for_speed_p ()
3123 && powi_cost (n) <= POWI_MAX_MULTS)))
3125 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3126 op0 = force_reg (mode, op0);
3127 return expand_powi (op0, mode, n);
3131 /* Emit a libcall to libgcc. */
3133 /* Mode of the 2nd argument must match that of an int. */
3134 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3136 if (target == NULL_RTX)
3137 target = gen_reg_rtx (mode);
3139 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3140 if (GET_MODE (op0) != mode)
3141 op0 = convert_to_mode (mode, op0, 0);
3142 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3143 if (GET_MODE (op1) != mode2)
3144 op1 = convert_to_mode (mode2, op1, 0);
3146 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3147 target, LCT_CONST, mode, 2,
3148 op0, mode, op1, mode2);
3153 /* Expand expression EXP which is a call to the strlen builtin. Return
3154 NULL_RTX if we failed the caller should emit a normal call, otherwise
3155 try to get the result in TARGET, if convenient. */
3158 expand_builtin_strlen (tree exp, rtx target,
3159 enum machine_mode target_mode)
3161 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3167 tree src = CALL_EXPR_ARG (exp, 0);
3168 rtx result, src_reg, char_rtx, before_strlen;
3169 enum machine_mode insn_mode = target_mode, char_mode;
3170 enum insn_code icode = CODE_FOR_nothing;
3173 /* If the length can be computed at compile-time, return it. */
3174 len = c_strlen (src, 0);
3176 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3178 /* If the length can be computed at compile-time and is constant
3179 integer, but there are side-effects in src, evaluate
3180 src for side-effects, then return len.
3181 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3182 can be optimized into: i++; x = 3; */
3183 len = c_strlen (src, 1);
3184 if (len && TREE_CODE (len) == INTEGER_CST)
3186 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3187 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3190 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3192 /* If SRC is not a pointer type, don't do this operation inline. */
3196 /* Bail out if we can't compute strlen in the right mode. */
3197 while (insn_mode != VOIDmode)
3199 icode = optab_handler (strlen_optab, insn_mode)->insn_code;
3200 if (icode != CODE_FOR_nothing)
3203 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3205 if (insn_mode == VOIDmode)
3208 /* Make a place to write the result of the instruction. */
3212 && GET_MODE (result) == insn_mode
3213 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3214 result = gen_reg_rtx (insn_mode);
3216 /* Make a place to hold the source address. We will not expand
3217 the actual source until we are sure that the expansion will
3218 not fail -- there are trees that cannot be expanded twice. */
3219 src_reg = gen_reg_rtx (Pmode);
3221 /* Mark the beginning of the strlen sequence so we can emit the
3222 source operand later. */
3223 before_strlen = get_last_insn ();
3225 char_rtx = const0_rtx;
3226 char_mode = insn_data[(int) icode].operand[2].mode;
3227 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3229 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3231 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3232 char_rtx, GEN_INT (align));
3237 /* Now that we are assured of success, expand the source. */
3239 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3241 emit_move_insn (src_reg, pat);
3246 emit_insn_after (pat, before_strlen);
3248 emit_insn_before (pat, get_insns ());
3250 /* Return the value in the proper mode for this function. */
3251 if (GET_MODE (result) == target_mode)
3253 else if (target != 0)
3254 convert_move (target, result, 0);
3256 target = convert_to_mode (target_mode, result, 0);
3262 /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the
3263 caller should emit a normal call, otherwise try to get the result
3264 in TARGET, if convenient (and in mode MODE if that's convenient). */
3267 expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode)
3269 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3271 tree type = TREE_TYPE (exp);
3272 tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0),
3273 CALL_EXPR_ARG (exp, 1), type);
3275 return expand_expr (result, target, mode, EXPAND_NORMAL);
3280 /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the
3281 caller should emit a normal call, otherwise try to get the result
3282 in TARGET, if convenient (and in mode MODE if that's convenient). */
3285 expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode)
3287 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3289 tree type = TREE_TYPE (exp);
3290 tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0),
3291 CALL_EXPR_ARG (exp, 1), type);
3293 return expand_expr (result, target, mode, EXPAND_NORMAL);
3295 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3300 /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the
3301 caller should emit a normal call, otherwise try to get the result
3302 in TARGET, if convenient (and in mode MODE if that's convenient). */
3305 expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode)
3307 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3309 tree type = TREE_TYPE (exp);
3310 tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0),
3311 CALL_EXPR_ARG (exp, 1), type);
3313 return expand_expr (result, target, mode, EXPAND_NORMAL);
3318 /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the
3319 caller should emit a normal call, otherwise try to get the result
3320 in TARGET, if convenient (and in mode MODE if that's convenient). */
3323 expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode)
3325 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3327 tree type = TREE_TYPE (exp);
3328 tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0),
3329 CALL_EXPR_ARG (exp, 1), type);
3331 return expand_expr (result, target, mode, EXPAND_NORMAL);
3336 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3337 bytes from constant string DATA + OFFSET and return it as target
3341 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3342 enum machine_mode mode)
3344 const char *str = (const char *) data;
3346 gcc_assert (offset >= 0
3347 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3348 <= strlen (str) + 1));
3350 return c_readstr (str + offset, mode);
3353 /* Expand a call EXP to the memcpy builtin.
3354 Return NULL_RTX if we failed, the caller should emit a normal call,
3355 otherwise try to get the result in TARGET, if convenient (and in
3356 mode MODE if that's convenient). */
3359 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3361 tree fndecl = get_callee_fndecl (exp);
3363 if (!validate_arglist (exp,
3364 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3368 tree dest = CALL_EXPR_ARG (exp, 0);
3369 tree src = CALL_EXPR_ARG (exp, 1);
3370 tree len = CALL_EXPR_ARG (exp, 2);
3371 const char *src_str;
3372 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3373 unsigned int dest_align
3374 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3375 rtx dest_mem, src_mem, dest_addr, len_rtx;
3376 tree result = fold_builtin_memory_op (dest, src, len,
3377 TREE_TYPE (TREE_TYPE (fndecl)),
3379 HOST_WIDE_INT expected_size = -1;
3380 unsigned int expected_align = 0;
3381 tree_ann_common_t ann;
3385 while (TREE_CODE (result) == COMPOUND_EXPR)
3387 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3389 result = TREE_OPERAND (result, 1);
3391 return expand_expr (result, target, mode, EXPAND_NORMAL);
3394 /* If DEST is not a pointer type, call the normal function. */
3395 if (dest_align == 0)
3398 /* If either SRC is not a pointer type, don't do this
3399 operation in-line. */
3403 ann = tree_common_ann (exp);
3405 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3407 if (expected_align < dest_align)
3408 expected_align = dest_align;
3409 dest_mem = get_memory_rtx (dest, len);
3410 set_mem_align (dest_mem, dest_align);
3411 len_rtx = expand_normal (len);
3412 src_str = c_getstr (src);
3414 /* If SRC is a string constant and block move would be done
3415 by pieces, we can avoid loading the string from memory
3416 and only stored the computed constants. */
3418 && CONST_INT_P (len_rtx)
3419 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3420 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3421 CONST_CAST (char *, src_str),
3424 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3425 builtin_memcpy_read_str,
3426 CONST_CAST (char *, src_str),
3427 dest_align, false, 0);
3428 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3429 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3433 src_mem = get_memory_rtx (src, len);
3434 set_mem_align (src_mem, src_align);
3436 /* Copy word part most expediently. */
3437 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3438 CALL_EXPR_TAILCALL (exp)
3439 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3440 expected_align, expected_size);
3444 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3445 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3451 /* Expand a call EXP to the mempcpy builtin.
3452 Return NULL_RTX if we failed; the caller should emit a normal call,
3453 otherwise try to get the result in TARGET, if convenient (and in
3454 mode MODE if that's convenient). If ENDP is 0 return the
3455 destination pointer, if ENDP is 1 return the end pointer ala
3456 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3460 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3462 if (!validate_arglist (exp,
3463 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3467 tree dest = CALL_EXPR_ARG (exp, 0);
3468 tree src = CALL_EXPR_ARG (exp, 1);
3469 tree len = CALL_EXPR_ARG (exp, 2);
3470 return expand_builtin_mempcpy_args (dest, src, len,
3472 target, mode, /*endp=*/ 1);
3476 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3477 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3478 so that this can also be called without constructing an actual CALL_EXPR.
3479 TYPE is the return type of the call. The other arguments and return value
3480 are the same as for expand_builtin_mempcpy. */
3483 expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
3484 rtx target, enum machine_mode mode, int endp)
3486 /* If return value is ignored, transform mempcpy into memcpy. */
3487 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3489 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3490 tree result = build_call_expr (fn, 3, dest, src, len);
3492 while (TREE_CODE (result) == COMPOUND_EXPR)
3494 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3496 result = TREE_OPERAND (result, 1);
3498 return expand_expr (result, target, mode, EXPAND_NORMAL);
3502 const char *src_str;
3503 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3504 unsigned int dest_align
3505 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3506 rtx dest_mem, src_mem, len_rtx;
3507 tree result = fold_builtin_memory_op (dest, src, len, type, false, endp);
3511 while (TREE_CODE (result) == COMPOUND_EXPR)
3513 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3515 result = TREE_OPERAND (result, 1);
3517 return expand_expr (result, target, mode, EXPAND_NORMAL);
3520 /* If either SRC or DEST is not a pointer type, don't do this
3521 operation in-line. */
3522 if (dest_align == 0 || src_align == 0)
3525 /* If LEN is not constant, call the normal function. */
3526 if (! host_integerp (len, 1))
3529 len_rtx = expand_normal (len);
3530 src_str = c_getstr (src);
3532 /* If SRC is a string constant and block move would be done
3533 by pieces, we can avoid loading the string from memory
3534 and only stored the computed constants. */
3536 && CONST_INT_P (len_rtx)
3537 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3538 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3539 CONST_CAST (char *, src_str),
3542 dest_mem = get_memory_rtx (dest, len);
3543 set_mem_align (dest_mem, dest_align);
3544 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3545 builtin_memcpy_read_str,
3546 CONST_CAST (char *, src_str),
3547 dest_align, false, endp);
3548 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3549 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3553 if (CONST_INT_P (len_rtx)
3554 && can_move_by_pieces (INTVAL (len_rtx),
3555 MIN (dest_align, src_align)))
3557 dest_mem = get_memory_rtx (dest, len);
3558 set_mem_align (dest_mem, dest_align);
3559 src_mem = get_memory_rtx (src, len);
3560 set_mem_align (src_mem, src_align);
3561 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3562 MIN (dest_align, src_align), endp);
3563 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3564 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3572 /* Expand expression EXP, which is a call to the memmove builtin. Return
3573 NULL_RTX if we failed; the caller should emit a normal call. */
3576 expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore)
3578 if (!validate_arglist (exp,
3579 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3583 tree dest = CALL_EXPR_ARG (exp, 0);
3584 tree src = CALL_EXPR_ARG (exp, 1);
3585 tree len = CALL_EXPR_ARG (exp, 2);
3586 return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp),
3587 target, mode, ignore);
3591 /* Helper function to do the actual work for expand_builtin_memmove. The
3592 arguments to the builtin_memmove call DEST, SRC, and LEN are broken out
3593 so that this can also be called without constructing an actual CALL_EXPR.
3594 TYPE is the return type of the call. The other arguments and return value
3595 are the same as for expand_builtin_memmove. */
3598 expand_builtin_memmove_args (tree dest, tree src, tree len,
3599 tree type, rtx target, enum machine_mode mode,
3602 tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3);
3606 STRIP_TYPE_NOPS (result);
3607 while (TREE_CODE (result) == COMPOUND_EXPR)
3609 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3611 result = TREE_OPERAND (result, 1);
3613 return expand_expr (result, target, mode, EXPAND_NORMAL);
3616 /* Otherwise, call the normal function. */
3620 /* Expand expression EXP, which is a call to the bcopy builtin. Return
3621 NULL_RTX if we failed the caller should emit a normal call. */
3624 expand_builtin_bcopy (tree exp, int ignore)
3626 tree type = TREE_TYPE (exp);
3627 tree src, dest, size;
3629 if (!validate_arglist (exp,
3630 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3633 src = CALL_EXPR_ARG (exp, 0);
3634 dest = CALL_EXPR_ARG (exp, 1);
3635 size = CALL_EXPR_ARG (exp, 2);
3637 /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z).
3638 This is done this way so that if it isn't expanded inline, we fall
3639 back to calling bcopy instead of memmove. */
3640 return expand_builtin_memmove_args (dest, src,
3641 fold_convert (sizetype, size),
3642 type, const0_rtx, VOIDmode,
3647 # define HAVE_movstr 0
3648 # define CODE_FOR_movstr CODE_FOR_nothing
3651 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3652 we failed, the caller should emit a normal call, otherwise try to
3653 get the result in TARGET, if convenient. If ENDP is 0 return the
3654 destination pointer, if ENDP is 1 return the end pointer ala
3655 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3659 expand_movstr (tree dest, tree src, rtx target, int endp)
3665 const struct insn_data * data;
3670 dest_mem = get_memory_rtx (dest, NULL);
3671 src_mem = get_memory_rtx (src, NULL);
3674 target = force_reg (Pmode, XEXP (dest_mem, 0));
3675 dest_mem = replace_equiv_address (dest_mem, target);
3676 end = gen_reg_rtx (Pmode);
3680 if (target == 0 || target == const0_rtx)
3682 end = gen_reg_rtx (Pmode);
3690 data = insn_data + CODE_FOR_movstr;
3692 if (data->operand[0].mode != VOIDmode)
3693 end = gen_lowpart (data->operand[0].mode, end);
3695 insn = data->genfun (end, dest_mem, src_mem);
3701 /* movstr is supposed to set end to the address of the NUL
3702 terminator. If the caller requested a mempcpy-like return value,
3704 if (endp == 1 && target != const0_rtx)
3706 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3707 emit_move_insn (target, force_operand (tem, NULL_RTX));
3713 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3714 NULL_RTX if we failed the caller should emit a normal call, otherwise
3715 try to get the result in TARGET, if convenient (and in mode MODE if that's
3719 expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode)
3721 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3723 tree dest = CALL_EXPR_ARG (exp, 0);
3724 tree src = CALL_EXPR_ARG (exp, 1);
3725 return expand_builtin_strcpy_args (fndecl, dest, src, target, mode);
3730 /* Helper function to do the actual work for expand_builtin_strcpy. The
3731 arguments to the builtin_strcpy call DEST and SRC are broken out
3732 so that this can also be called without constructing an actual CALL_EXPR.
3733 The other arguments and return value are the same as for
3734 expand_builtin_strcpy. */
3737 expand_builtin_strcpy_args (tree fndecl, tree dest, tree src,
3738 rtx target, enum machine_mode mode)
3740 tree result = fold_builtin_strcpy (fndecl, dest, src, 0);
3742 return expand_expr (result, target, mode, EXPAND_NORMAL);
3743 return expand_movstr (dest, src, target, /*endp=*/0);
3747 /* Expand a call EXP to the stpcpy builtin.
3748 Return NULL_RTX if we failed the caller should emit a normal call,
3749 otherwise try to get the result in TARGET, if convenient (and in
3750 mode MODE if that's convenient). */
3753 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3757 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3760 dst = CALL_EXPR_ARG (exp, 0);
3761 src = CALL_EXPR_ARG (exp, 1);
3763 /* If return value is ignored, transform stpcpy into strcpy. */
3764 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3766 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3767 tree result = build_call_expr (fn, 2, dst, src);
3769 STRIP_NOPS (result);
3770 while (TREE_CODE (result) == COMPOUND_EXPR)
3772 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3774 result = TREE_OPERAND (result, 1);
3776 return expand_expr (result, target, mode, EXPAND_NORMAL);
3783 /* Ensure we get an actual string whose length can be evaluated at
3784 compile-time, not an expression containing a string. This is
3785 because the latter will potentially produce pessimized code
3786 when used to produce the return value. */
3787 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3788 return expand_movstr (dst, src, target, /*endp=*/2);
3790 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3791 ret = expand_builtin_mempcpy_args (dst, src, lenp1, TREE_TYPE (exp),
3792 target, mode, /*endp=*/2);
3797 if (TREE_CODE (len) == INTEGER_CST)
3799 rtx len_rtx = expand_normal (len);
3801 if (CONST_INT_P (len_rtx))
3803 ret = expand_builtin_strcpy_args (get_callee_fndecl (exp),
3804 dst, src, target, mode);
3810 if (mode != VOIDmode)
3811 target = gen_reg_rtx (mode);
3813 target = gen_reg_rtx (GET_MODE (ret));
3815 if (GET_MODE (target) != GET_MODE (ret))
3816 ret = gen_lowpart (GET_MODE (target), ret);
3818 ret = plus_constant (ret, INTVAL (len_rtx));
3819 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3827 return expand_movstr (dst, src, target, /*endp=*/2);
3831 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3832 bytes from constant string DATA + OFFSET and return it as target
3836 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3837 enum machine_mode mode)
3839 const char *str = (const char *) data;
3841 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3844 return c_readstr (str + offset, mode);
3847 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3848 NULL_RTX if we failed the caller should emit a normal call. */
3851 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3853 tree fndecl = get_callee_fndecl (exp);
3855 if (validate_arglist (exp,
3856 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3858 tree dest = CALL_EXPR_ARG (exp, 0);
3859 tree src = CALL_EXPR_ARG (exp, 1);
3860 tree len = CALL_EXPR_ARG (exp, 2);
3861 tree slen = c_strlen (src, 1);
3862 tree result = fold_builtin_strncpy (fndecl, dest, src, len, slen);
3866 while (TREE_CODE (result) == COMPOUND_EXPR)
3868 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3870 result = TREE_OPERAND (result, 1);
3872 return expand_expr (result, target, mode, EXPAND_NORMAL);
3875 /* We must be passed a constant len and src parameter. */
3876 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3879 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3881 /* We're required to pad with trailing zeros if the requested
3882 len is greater than strlen(s2)+1. In that case try to
3883 use store_by_pieces, if it fails, punt. */
3884 if (tree_int_cst_lt (slen, len))
3886 unsigned int dest_align
3887 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3888 const char *p = c_getstr (src);
3891 if (!p || dest_align == 0 || !host_integerp (len, 1)
3892 || !can_store_by_pieces (tree_low_cst (len, 1),
3893 builtin_strncpy_read_str,
3894 CONST_CAST (char *, p),
3898 dest_mem = get_memory_rtx (dest, len);
3899 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3900 builtin_strncpy_read_str,
3901 CONST_CAST (char *, p), dest_align, false, 0);
3902 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3903 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3910 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3911 bytes from constant string DATA + OFFSET and return it as target
3915 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3916 enum machine_mode mode)
3918 const char *c = (const char *) data;
3919 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3921 memset (p, *c, GET_MODE_SIZE (mode));
3923 return c_readstr (p, mode);
3926 /* Callback routine for store_by_pieces. Return the RTL of a register
3927 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3928 char value given in the RTL register data. For example, if mode is
3929 4 bytes wide, return the RTL for 0x01010101*data. */
3932 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3933 enum machine_mode mode)
3939 size = GET_MODE_SIZE (mode);
3943 p = XALLOCAVEC (char, size);
3944 memset (p, 1, size);
3945 coeff = c_readstr (p, mode);
3947 target = convert_to_mode (mode, (rtx) data, 1);
3948 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3949 return force_reg (mode, target);
3952 /* Expand expression EXP, which is a call to the memset builtin. Return
3953 NULL_RTX if we failed the caller should emit a normal call, otherwise
3954 try to get the result in TARGET, if convenient (and in mode MODE if that's
3958 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3960 if (!validate_arglist (exp,
3961 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3965 tree dest = CALL_EXPR_ARG (exp, 0);
3966 tree val = CALL_EXPR_ARG (exp, 1);
3967 tree len = CALL_EXPR_ARG (exp, 2);
3968 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3972 /* Helper function to do the actual work for expand_builtin_memset. The
3973 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3974 so that this can also be called without constructing an actual CALL_EXPR.
3975 The other arguments and return value are the same as for
3976 expand_builtin_memset. */
3979 expand_builtin_memset_args (tree dest, tree val, tree len,
3980 rtx target, enum machine_mode mode, tree orig_exp)
3983 enum built_in_function fcode;
3985 unsigned int dest_align;
3986 rtx dest_mem, dest_addr, len_rtx;
3987 HOST_WIDE_INT expected_size = -1;
3988 unsigned int expected_align = 0;
3989 tree_ann_common_t ann;
3991 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3993 /* If DEST is not a pointer type, don't do this operation in-line. */
3994 if (dest_align == 0)
3997 ann = tree_common_ann (orig_exp);
3999 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
4001 if (expected_align < dest_align)
4002 expected_align = dest_align;
4004 /* If the LEN parameter is zero, return DEST. */
4005 if (integer_zerop (len))
4007 /* Evaluate and ignore VAL in case it has side-effects. */
4008 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4009 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4012 /* Stabilize the arguments in case we fail. */
4013 dest = builtin_save_expr (dest);
4014 val = builtin_save_expr (val);
4015 len = builtin_save_expr (len);
4017 len_rtx = expand_normal (len);
4018 dest_mem = get_memory_rtx (dest, len);
4020 if (TREE_CODE (val) != INTEGER_CST)
4024 val_rtx = expand_normal (val);
4025 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
4028 /* Assume that we can memset by pieces if we can store
4029 * the coefficients by pieces (in the required modes).
4030 * We can't pass builtin_memset_gen_str as that emits RTL. */
4032 if (host_integerp (len, 1)
4033 && can_store_by_pieces (tree_low_cst (len, 1),
4034 builtin_memset_read_str, &c, dest_align,
4037 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
4039 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4040 builtin_memset_gen_str, val_rtx, dest_align,
4043 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4044 dest_align, expected_align,
4048 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4049 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4053 if (target_char_cast (val, &c))
4058 if (host_integerp (len, 1)
4059 && can_store_by_pieces (tree_low_cst (len, 1),
4060 builtin_memset_read_str, &c, dest_align,
4062 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4063 builtin_memset_read_str, &c, dest_align, true, 0);
4064 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
4065 dest_align, expected_align,
4069 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4070 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4074 set_mem_align (dest_mem, dest_align);
4075 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4076 CALL_EXPR_TAILCALL (orig_exp)
4077 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4078 expected_align, expected_size);
4082 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4083 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4089 fndecl = get_callee_fndecl (orig_exp);
4090 fcode = DECL_FUNCTION_CODE (fndecl);
4091 if (fcode == BUILT_IN_MEMSET)
4092 fn = build_call_expr (fndecl, 3, dest, val, len);
4093 else if (fcode == BUILT_IN_BZERO)
4094 fn = build_call_expr (fndecl, 2, dest, len);
4097 if (TREE_CODE (fn) == CALL_EXPR)
4098 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4099 return expand_call (fn, target, target == const0_rtx);
4102 /* Expand expression EXP, which is a call to the bzero builtin. Return
4103 NULL_RTX if we failed the caller should emit a normal call. */
4106 expand_builtin_bzero (tree exp)
4110 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4113 dest = CALL_EXPR_ARG (exp, 0);
4114 size = CALL_EXPR_ARG (exp, 1);
4116 /* New argument list transforming bzero(ptr x, int y) to
4117 memset(ptr x, int 0, size_t y). This is done this way
4118 so that if it isn't expanded inline, we fallback to
4119 calling bzero instead of memset. */
4121 return expand_builtin_memset_args (dest, integer_zero_node,
4122 fold_convert (sizetype, size),
4123 const0_rtx, VOIDmode, exp);
4126 /* Expand a call to the memchr builtin. Return NULL_RTX if we failed the
4127 caller should emit a normal call, otherwise try to get the result
4128 in TARGET, if convenient (and in mode MODE if that's convenient). */
4131 expand_builtin_memchr (tree exp, rtx target, enum machine_mode mode)
4133 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE,
4134 INTEGER_TYPE, VOID_TYPE))
4136 tree type = TREE_TYPE (exp);
4137 tree result = fold_builtin_memchr (CALL_EXPR_ARG (exp, 0),
4138 CALL_EXPR_ARG (exp, 1),
4139 CALL_EXPR_ARG (exp, 2), type);
4141 return expand_expr (result, target, mode, EXPAND_NORMAL);
4146 /* Expand expression EXP, which is a call to the memcmp built-in function.
4147 Return NULL_RTX if we failed and the
4148 caller should emit a normal call, otherwise try to get the result in
4149 TARGET, if convenient (and in mode MODE, if that's convenient). */
4152 expand_builtin_memcmp (tree exp, rtx target, enum machine_mode mode)
4154 if (!validate_arglist (exp,
4155 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4159 tree result = fold_builtin_memcmp (CALL_EXPR_ARG (exp, 0),
4160 CALL_EXPR_ARG (exp, 1),
4161 CALL_EXPR_ARG (exp, 2));
4163 return expand_expr (result, target, mode, EXPAND_NORMAL);
4166 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4168 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4171 tree arg1 = CALL_EXPR_ARG (exp, 0);
4172 tree arg2 = CALL_EXPR_ARG (exp, 1);
4173 tree len = CALL_EXPR_ARG (exp, 2);
4176 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4178 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4179 enum machine_mode insn_mode;
4181 #ifdef HAVE_cmpmemsi
4183 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4186 #ifdef HAVE_cmpstrnsi
4188 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4193 /* If we don't have POINTER_TYPE, call the function. */
4194 if (arg1_align == 0 || arg2_align == 0)
4197 /* Make a place to write the result of the instruction. */
4200 && REG_P (result) && GET_MODE (result) == insn_mode
4201 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4202 result = gen_reg_rtx (insn_mode);
4204 arg1_rtx = get_memory_rtx (arg1, len);
4205 arg2_rtx = get_memory_rtx (arg2, len);
4206 arg3_rtx = expand_normal (fold_convert (sizetype, len));
4208 /* Set MEM_SIZE as appropriate. */
4209 if (CONST_INT_P (arg3_rtx))
4211 set_mem_size (arg1_rtx, arg3_rtx);
4212 set_mem_size (arg2_rtx, arg3_rtx);
4215 #ifdef HAVE_cmpmemsi
4217 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4218 GEN_INT (MIN (arg1_align, arg2_align)));
4221 #ifdef HAVE_cmpstrnsi
4223 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4224 GEN_INT (MIN (arg1_align, arg2_align)));
4232 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4233 TYPE_MODE (integer_type_node), 3,
4234 XEXP (arg1_rtx, 0), Pmode,
4235 XEXP (arg2_rtx, 0), Pmode,
4236 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4237 TYPE_UNSIGNED (sizetype)),
4238 TYPE_MODE (sizetype));
4240 /* Return the value in the proper mode for this function. */
4241 mode = TYPE_MODE (TREE_TYPE (exp));
4242 if (GET_MODE (result) == mode)
4244 else if (target != 0)
4246 convert_move (target, result, 0);
4250 return convert_to_mode (mode, result, 0);
4257 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4258 if we failed the caller should emit a normal call, otherwise try to get
4259 the result in TARGET, if convenient. */
4262 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
4264 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4268 tree result = fold_builtin_strcmp (CALL_EXPR_ARG (exp, 0),
4269 CALL_EXPR_ARG (exp, 1));
4271 return expand_expr (result, target, mode, EXPAND_NORMAL);
4274 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4275 if (cmpstr_optab[SImode] != CODE_FOR_nothing
4276 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
4278 rtx arg1_rtx, arg2_rtx;
4279 rtx result, insn = NULL_RTX;
4281 tree arg1 = CALL_EXPR_ARG (exp, 0);
4282 tree arg2 = CALL_EXPR_ARG (exp, 1);
4285 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4287 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4289 /* If we don't have POINTER_TYPE, call the function. */
4290 if (arg1_align == 0 || arg2_align == 0)
4293 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4294 arg1 = builtin_save_expr (arg1);
4295 arg2 = builtin_save_expr (arg2);
4297 arg1_rtx = get_memory_rtx (arg1, NULL);
4298 arg2_rtx = get_memory_rtx (arg2, NULL);
4300 #ifdef HAVE_cmpstrsi
4301 /* Try to call cmpstrsi. */
4304 enum machine_mode insn_mode
4305 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4307 /* Make a place to write the result of the instruction. */
4310 && REG_P (result) && GET_MODE (result) == insn_mode
4311 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4312 result = gen_reg_rtx (insn_mode);
4314 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4315 GEN_INT (MIN (arg1_align, arg2_align)));
4318 #ifdef HAVE_cmpstrnsi
4319 /* Try to determine at least one length and call cmpstrnsi. */
4320 if (!insn && HAVE_cmpstrnsi)
4325 enum machine_mode insn_mode
4326 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4327 tree len1 = c_strlen (arg1, 1);
4328 tree len2 = c_strlen (arg2, 1);
4331 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4333 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4335 /* If we don't have a constant length for the first, use the length
4336 of the second, if we know it. We don't require a constant for
4337 this case; some cost analysis could be done if both are available
4338 but neither is constant. For now, assume they're equally cheap,
4339 unless one has side effects. If both strings have constant lengths,
4346 else if (TREE_SIDE_EFFECTS (len1))
4348 else if (TREE_SIDE_EFFECTS (len2))
4350 else if (TREE_CODE (len1) != INTEGER_CST)
4352 else if (TREE_CODE (len2) != INTEGER_CST)
4354 else if (tree_int_cst_lt (len1, len2))
4359 /* If both arguments have side effects, we cannot optimize. */
4360 if (!len || TREE_SIDE_EFFECTS (len))
4363 arg3_rtx = expand_normal (len);
4365 /* Make a place to write the result of the instruction. */
4368 && REG_P (result) && GET_MODE (result) == insn_mode
4369 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4370 result = gen_reg_rtx (insn_mode);
4372 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4373 GEN_INT (MIN (arg1_align, arg2_align)));
4381 /* Return the value in the proper mode for this function. */
4382 mode = TYPE_MODE (TREE_TYPE (exp));
4383 if (GET_MODE (result) == mode)
4386 return convert_to_mode (mode, result, 0);
4387 convert_move (target, result, 0);
4391 /* Expand the library call ourselves using a stabilized argument
4392 list to avoid re-evaluating the function's arguments twice. */
4393 #ifdef HAVE_cmpstrnsi
4396 fndecl = get_callee_fndecl (exp);
4397 fn = build_call_expr (fndecl, 2, arg1, arg2);
4398 if (TREE_CODE (fn) == CALL_EXPR)
4399 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4400 return expand_call (fn, target, target == const0_rtx);
4406 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4407 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4408 the result in TARGET, if convenient. */
4411 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4413 if (!validate_arglist (exp,
4414 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4418 tree result = fold_builtin_strncmp (CALL_EXPR_ARG (exp, 0),
4419 CALL_EXPR_ARG (exp, 1),
4420 CALL_EXPR_ARG (exp, 2));
4422 return expand_expr (result, target, mode, EXPAND_NORMAL);
4425 /* If c_strlen can determine an expression for one of the string
4426 lengths, and it doesn't have side effects, then emit cmpstrnsi
4427 using length MIN(strlen(string)+1, arg3). */
4428 #ifdef HAVE_cmpstrnsi
4431 tree len, len1, len2;
4432 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4435 tree arg1 = CALL_EXPR_ARG (exp, 0);
4436 tree arg2 = CALL_EXPR_ARG (exp, 1);
4437 tree arg3 = CALL_EXPR_ARG (exp, 2);
4440 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4442 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4443 enum machine_mode insn_mode
4444 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4446 len1 = c_strlen (arg1, 1);
4447 len2 = c_strlen (arg2, 1);
4450 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4452 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4454 /* If we don't have a constant length for the first, use the length
4455 of the second, if we know it. We don't require a constant for
4456 this case; some cost analysis could be done if both are available
4457 but neither is constant. For now, assume they're equally cheap,
4458 unless one has side effects. If both strings have constant lengths,
4465 else if (TREE_SIDE_EFFECTS (len1))
4467 else if (TREE_SIDE_EFFECTS (len2))
4469 else if (TREE_CODE (len1) != INTEGER_CST)
4471 else if (TREE_CODE (len2) != INTEGER_CST)
4473 else if (tree_int_cst_lt (len1, len2))
4478 /* If both arguments have side effects, we cannot optimize. */
4479 if (!len || TREE_SIDE_EFFECTS (len))
4482 /* The actual new length parameter is MIN(len,arg3). */
4483 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4484 fold_convert (TREE_TYPE (len), arg3));
4486 /* If we don't have POINTER_TYPE, call the function. */
4487 if (arg1_align == 0 || arg2_align == 0)
4490 /* Make a place to write the result of the instruction. */
4493 && REG_P (result) && GET_MODE (result) == insn_mode
4494 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4495 result = gen_reg_rtx (insn_mode);
4497 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4498 arg1 = builtin_save_expr (arg1);
4499 arg2 = builtin_save_expr (arg2);
4500 len = builtin_save_expr (len);
4502 arg1_rtx = get_memory_rtx (arg1, len);
4503 arg2_rtx = get_memory_rtx (arg2, len);
4504 arg3_rtx = expand_normal (len);
4505 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4506 GEN_INT (MIN (arg1_align, arg2_align)));
4511 /* Return the value in the proper mode for this function. */
4512 mode = TYPE_MODE (TREE_TYPE (exp));
4513 if (GET_MODE (result) == mode)
4516 return convert_to_mode (mode, result, 0);
4517 convert_move (target, result, 0);
4521 /* Expand the library call ourselves using a stabilized argument
4522 list to avoid re-evaluating the function's arguments twice. */
4523 fndecl = get_callee_fndecl (exp);
4524 fn = build_call_expr (fndecl, 3, arg1, arg2, len);
4525 if (TREE_CODE (fn) == CALL_EXPR)
4526 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4527 return expand_call (fn, target, target == const0_rtx);
4533 /* Expand expression EXP, which is a call to the strcat builtin.
4534 Return NULL_RTX if we failed the caller should emit a normal call,
4535 otherwise try to get the result in TARGET, if convenient. */
4538 expand_builtin_strcat (tree fndecl, tree exp, rtx target, enum machine_mode mode)
4540 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4544 tree dst = CALL_EXPR_ARG (exp, 0);
4545 tree src = CALL_EXPR_ARG (exp, 1);
4546 const char *p = c_getstr (src);
4548 /* If the string length is zero, return the dst parameter. */
4549 if (p && *p == '\0')
4550 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4552 if (optimize_insn_for_speed_p ())
4554 /* See if we can store by pieces into (dst + strlen(dst)). */
4555 tree newsrc, newdst,
4556 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4559 /* Stabilize the argument list. */
4560 newsrc = builtin_save_expr (src);
4561 dst = builtin_save_expr (dst);
4565 /* Create strlen (dst). */
4566 newdst = build_call_expr (strlen_fn, 1, dst);
4567 /* Create (dst p+ strlen (dst)). */
4569 newdst = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4570 newdst = builtin_save_expr (newdst);
4572 if (!expand_builtin_strcpy_args (fndecl, newdst, newsrc, target, mode))
4574 end_sequence (); /* Stop sequence. */
4578 /* Output the entire sequence. */
4579 insns = get_insns ();
4583 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4590 /* Expand expression EXP, which is a call to the strncat builtin.
4591 Return NULL_RTX if we failed the caller should emit a normal call,
4592 otherwise try to get the result in TARGET, if convenient. */
4595 expand_builtin_strncat (tree exp, rtx target, enum machine_mode mode)
4597 if (validate_arglist (exp,
4598 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4600 tree result = fold_builtin_strncat (CALL_EXPR_ARG (exp, 0),
4601 CALL_EXPR_ARG (exp, 1),
4602 CALL_EXPR_ARG (exp, 2));
4604 return expand_expr (result, target, mode, EXPAND_NORMAL);
4609 /* Expand expression EXP, which is a call to the strspn builtin.
4610 Return NULL_RTX if we failed the caller should emit a normal call,
4611 otherwise try to get the result in TARGET, if convenient. */
4614 expand_builtin_strspn (tree exp, rtx target, enum machine_mode mode)
4616 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4618 tree result = fold_builtin_strspn (CALL_EXPR_ARG (exp, 0),
4619 CALL_EXPR_ARG (exp, 1));
4621 return expand_expr (result, target, mode, EXPAND_NORMAL);
4626 /* Expand expression EXP, which is a call to the strcspn builtin.
4627 Return NULL_RTX if we failed the caller should emit a normal call,
4628 otherwise try to get the result in TARGET, if convenient. */
4631 expand_builtin_strcspn (tree exp, rtx target, enum machine_mode mode)
4633 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4635 tree result = fold_builtin_strcspn (CALL_EXPR_ARG (exp, 0),
4636 CALL_EXPR_ARG (exp, 1));
4638 return expand_expr (result, target, mode, EXPAND_NORMAL);
4643 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4644 if that's convenient. */
4647 expand_builtin_saveregs (void)
4651 /* Don't do __builtin_saveregs more than once in a function.
4652 Save the result of the first call and reuse it. */
4653 if (saveregs_value != 0)
4654 return saveregs_value;
4656 /* When this function is called, it means that registers must be
4657 saved on entry to this function. So we migrate the call to the
4658 first insn of this function. */
4662 /* Do whatever the machine needs done in this case. */
4663 val = targetm.calls.expand_builtin_saveregs ();
4668 saveregs_value = val;
4670 /* Put the insns after the NOTE that starts the function. If this
4671 is inside a start_sequence, make the outer-level insn chain current, so
4672 the code is placed at the start of the function. */
4673 push_topmost_sequence ();
4674 emit_insn_after (seq, entry_of_function ());
4675 pop_topmost_sequence ();
4680 /* __builtin_args_info (N) returns word N of the arg space info
4681 for the current function. The number and meanings of words
4682 is controlled by the definition of CUMULATIVE_ARGS. */
4685 expand_builtin_args_info (tree exp)
4687 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4688 int *word_ptr = (int *) &crtl->args.info;
4690 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4692 if (call_expr_nargs (exp) != 0)
4694 if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0))
4695 error ("argument of %<__builtin_args_info%> must be constant");
4698 HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0);
4700 if (wordnum < 0 || wordnum >= nwords)
4701 error ("argument of %<__builtin_args_info%> out of range");
4703 return GEN_INT (word_ptr[wordnum]);
4707 error ("missing argument in %<__builtin_args_info%>");
4712 /* Expand a call to __builtin_next_arg. */
4715 expand_builtin_next_arg (void)
4717 /* Checking arguments is already done in fold_builtin_next_arg
4718 that must be called before this function. */
4719 return expand_binop (ptr_mode, add_optab,
4720 crtl->args.internal_arg_pointer,
4721 crtl->args.arg_offset_rtx,
4722 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4725 /* Make it easier for the backends by protecting the valist argument
4726 from multiple evaluations. */
4729 stabilize_va_list (tree valist, int needs_lvalue)
4731 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4733 gcc_assert (vatype != NULL_TREE);
4735 if (TREE_CODE (vatype) == ARRAY_TYPE)
4737 if (TREE_SIDE_EFFECTS (valist))
4738 valist = save_expr (valist);
4740 /* For this case, the backends will be expecting a pointer to
4741 vatype, but it's possible we've actually been given an array
4742 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4744 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4746 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4747 valist = build_fold_addr_expr_with_type (valist, p1);
4756 if (! TREE_SIDE_EFFECTS (valist))
4759 pt = build_pointer_type (vatype);
4760 valist = fold_build1 (ADDR_EXPR, pt, valist);
4761 TREE_SIDE_EFFECTS (valist) = 1;
4764 if (TREE_SIDE_EFFECTS (valist))
4765 valist = save_expr (valist);
4766 valist = build_fold_indirect_ref (valist);
4772 /* The "standard" definition of va_list is void*. */
4775 std_build_builtin_va_list (void)
4777 return ptr_type_node;
4780 /* The "standard" abi va_list is va_list_type_node. */
4783 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4785 return va_list_type_node;
4788 /* The "standard" type of va_list is va_list_type_node. */
4791 std_canonical_va_list_type (tree type)
4795 if (INDIRECT_REF_P (type))
4796 type = TREE_TYPE (type);
4797 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4798 type = TREE_TYPE (type);
4799 wtype = va_list_type_node;
4801 /* Treat structure va_list types. */
4802 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4803 htype = TREE_TYPE (htype);
4804 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4806 /* If va_list is an array type, the argument may have decayed
4807 to a pointer type, e.g. by being passed to another function.
4808 In that case, unwrap both types so that we can compare the
4809 underlying records. */
4810 if (TREE_CODE (htype) == ARRAY_TYPE
4811 || POINTER_TYPE_P (htype))
4813 wtype = TREE_TYPE (wtype);
4814 htype = TREE_TYPE (htype);
4817 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4818 return va_list_type_node;
4823 /* The "standard" implementation of va_start: just assign `nextarg' to
4827 std_expand_builtin_va_start (tree valist, rtx nextarg)
4829 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4830 convert_move (va_r, nextarg, 0);
4833 /* Expand EXP, a call to __builtin_va_start. */
4836 expand_builtin_va_start (tree exp)
4841 if (call_expr_nargs (exp) < 2)
4843 error ("too few arguments to function %<va_start%>");
4847 if (fold_builtin_next_arg (exp, true))
4850 nextarg = expand_builtin_next_arg ();
4851 valist = stabilize_va_list (CALL_EXPR_ARG (exp, 0), 1);
4853 if (targetm.expand_builtin_va_start)
4854 targetm.expand_builtin_va_start (valist, nextarg);
4856 std_expand_builtin_va_start (valist, nextarg);
4861 /* The "standard" implementation of va_arg: read the value from the
4862 current (padded) address and increment by the (padded) size. */
4865 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4868 tree addr, t, type_size, rounded_size, valist_tmp;
4869 unsigned HOST_WIDE_INT align, boundary;
4872 #ifdef ARGS_GROW_DOWNWARD
4873 /* All of the alignment and movement below is for args-grow-up machines.
4874 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4875 implement their own specialized gimplify_va_arg_expr routines. */
4879 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4881 type = build_pointer_type (type);
4883 align = PARM_BOUNDARY / BITS_PER_UNIT;
4884 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4886 /* When we align parameter on stack for caller, if the parameter
4887 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4888 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4889 here with caller. */
4890 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4891 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4893 boundary /= BITS_PER_UNIT;
4895 /* Hoist the valist value into a temporary for the moment. */
4896 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4898 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4899 requires greater alignment, we must perform dynamic alignment. */
4900 if (boundary > align
4901 && !integer_zerop (TYPE_SIZE (type)))
4903 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4904 fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist),
4905 valist_tmp, size_int (boundary - 1)));
4906 gimplify_and_add (t, pre_p);
4908 t = fold_convert (sizetype, valist_tmp);
4909 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4910 fold_convert (TREE_TYPE (valist),
4911 fold_build2 (BIT_AND_EXPR, sizetype, t,
4912 size_int (-boundary))));
4913 gimplify_and_add (t, pre_p);
4918 /* If the actual alignment is less than the alignment of the type,
4919 adjust the type accordingly so that we don't assume strict alignment
4920 when dereferencing the pointer. */
4921 boundary *= BITS_PER_UNIT;
4922 if (boundary < TYPE_ALIGN (type))
4924 type = build_variant_type_copy (type);
4925 TYPE_ALIGN (type) = boundary;
4928 /* Compute the rounded size of the type. */
4929 type_size = size_in_bytes (type);
4930 rounded_size = round_up (type_size, align);
4932 /* Reduce rounded_size so it's sharable with the postqueue. */
4933 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4937 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4939 /* Small args are padded downward. */
4940 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4941 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4942 size_binop (MINUS_EXPR, rounded_size, type_size));
4943 addr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr, t);
4946 /* Compute new value for AP. */
4947 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4948 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4949 gimplify_and_add (t, pre_p);
4951 addr = fold_convert (build_pointer_type (type), addr);
4954 addr = build_va_arg_indirect_ref (addr);
4956 return build_va_arg_indirect_ref (addr);
4959 /* Build an indirect-ref expression over the given TREE, which represents a
4960 piece of a va_arg() expansion. */
4962 build_va_arg_indirect_ref (tree addr)
4964 addr = build_fold_indirect_ref (addr);
4966 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4972 /* Return a dummy expression of type TYPE in order to keep going after an
4976 dummy_object (tree type)
4978 tree t = build_int_cst (build_pointer_type (type), 0);
4979 return build1 (INDIRECT_REF, type, t);
4982 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4983 builtin function, but a very special sort of operator. */
4985 enum gimplify_status
4986 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4988 tree promoted_type, have_va_type;
4989 tree valist = TREE_OPERAND (*expr_p, 0);
4990 tree type = TREE_TYPE (*expr_p);
4992 location_t loc = EXPR_HAS_LOCATION (*expr_p) ? EXPR_LOCATION (*expr_p) :
4995 /* Verify that valist is of the proper type. */
4996 have_va_type = TREE_TYPE (valist);
4997 if (have_va_type == error_mark_node)
4999 have_va_type = targetm.canonical_va_list_type (have_va_type);
5001 if (have_va_type == NULL_TREE)
5003 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
5007 /* Generate a diagnostic for requesting data of a type that cannot
5008 be passed through `...' due to type promotion at the call site. */
5009 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
5012 static bool gave_help;
5015 /* Unfortunately, this is merely undefined, rather than a constraint
5016 violation, so we cannot make this an error. If this call is never
5017 executed, the program is still strictly conforming. */
5018 warned = warning_at (loc, 0,
5019 "%qT is promoted to %qT when passed through %<...%>",
5020 type, promoted_type);
5021 if (!gave_help && warned)
5024 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
5025 promoted_type, type);
5028 /* We can, however, treat "undefined" any way we please.
5029 Call abort to encourage the user to fix the program. */
5031 inform (loc, "if this code is reached, the program will abort");
5032 /* Before the abort, allow the evaluation of the va_list
5033 expression to exit or longjmp. */
5034 gimplify_and_add (valist, pre_p);
5035 t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
5036 gimplify_and_add (t, pre_p);
5038 /* This is dead code, but go ahead and finish so that the
5039 mode of the result comes out right. */
5040 *expr_p = dummy_object (type);
5045 /* Make it easier for the backends by protecting the valist argument
5046 from multiple evaluations. */
5047 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
5049 /* For this case, the backends will be expecting a pointer to
5050 TREE_TYPE (abi), but it's possible we've
5051 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
5053 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5055 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
5056 valist = fold_convert (p1, build_fold_addr_expr (valist));
5059 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
5062 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
5064 if (!targetm.gimplify_va_arg_expr)
5065 /* FIXME: Once most targets are converted we should merely
5066 assert this is non-null. */
5069 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
5074 /* Expand EXP, a call to __builtin_va_end. */
5077 expand_builtin_va_end (tree exp)
5079 tree valist = CALL_EXPR_ARG (exp, 0);
5081 /* Evaluate for side effects, if needed. I hate macros that don't
5083 if (TREE_SIDE_EFFECTS (valist))
5084 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5089 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5090 builtin rather than just as an assignment in stdarg.h because of the
5091 nastiness of array-type va_list types. */
5094 expand_builtin_va_copy (tree exp)
5098 dst = CALL_EXPR_ARG (exp, 0);
5099 src = CALL_EXPR_ARG (exp, 1);
5101 dst = stabilize_va_list (dst, 1);
5102 src = stabilize_va_list (src, 0);
5104 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5106 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5108 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5109 TREE_SIDE_EFFECTS (t) = 1;
5110 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5114 rtx dstb, srcb, size;
5116 /* Evaluate to pointers. */
5117 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5118 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5119 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5120 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5122 dstb = convert_memory_address (Pmode, dstb);
5123 srcb = convert_memory_address (Pmode, srcb);
5125 /* "Dereference" to BLKmode memories. */
5126 dstb = gen_rtx_MEM (BLKmode, dstb);
5127 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5128 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5129 srcb = gen_rtx_MEM (BLKmode, srcb);
5130 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5131 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5134 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5140 /* Expand a call to one of the builtin functions __builtin_frame_address or
5141 __builtin_return_address. */
5144 expand_builtin_frame_address (tree fndecl, tree exp)
5146 /* The argument must be a nonnegative integer constant.
5147 It counts the number of frames to scan up the stack.
5148 The value is the return address saved in that frame. */
5149 if (call_expr_nargs (exp) == 0)
5150 /* Warning about missing arg was already issued. */
5152 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
5154 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5155 error ("invalid argument to %<__builtin_frame_address%>");
5157 error ("invalid argument to %<__builtin_return_address%>");
5163 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
5164 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
5166 /* Some ports cannot access arbitrary stack frames. */
5169 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5170 warning (0, "unsupported argument to %<__builtin_frame_address%>");
5172 warning (0, "unsupported argument to %<__builtin_return_address%>");
5176 /* For __builtin_frame_address, return what we've got. */
5177 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5181 && ! CONSTANT_P (tem))
5182 tem = copy_to_mode_reg (Pmode, tem);
5187 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
5188 we failed and the caller should emit a normal call, otherwise try to get
5189 the result in TARGET, if convenient. */
5192 expand_builtin_alloca (tree exp, rtx target)
5197 /* Emit normal call if marked not-inlineable. */
5198 if (CALL_CANNOT_INLINE_P (exp))
5201 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5204 /* Compute the argument. */
5205 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5207 /* Allocate the desired space. */
5208 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
5209 result = convert_memory_address (ptr_mode, result);
5214 /* Expand a call to a bswap builtin with argument ARG0. MODE
5215 is the mode to expand with. */
5218 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5220 enum machine_mode mode;
5224 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5227 arg = CALL_EXPR_ARG (exp, 0);
5228 mode = TYPE_MODE (TREE_TYPE (arg));
5229 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5231 target = expand_unop (mode, bswap_optab, op0, target, 1);
5233 gcc_assert (target);
5235 return convert_to_mode (mode, target, 0);
5238 /* Expand a call to a unary builtin in EXP.
5239 Return NULL_RTX if a normal call should be emitted rather than expanding the
5240 function in-line. If convenient, the result should be placed in TARGET.
5241 SUBTARGET may be used as the target for computing one of EXP's operands. */
5244 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5245 rtx subtarget, optab op_optab)
5249 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5252 /* Compute the argument. */
5253 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5254 VOIDmode, EXPAND_NORMAL);
5255 /* Compute op, into TARGET if possible.
5256 Set TARGET to wherever the result comes back. */
5257 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5258 op_optab, op0, target, 1);
5259 gcc_assert (target);
5261 return convert_to_mode (target_mode, target, 0);
5264 /* If the string passed to fputs is a constant and is one character
5265 long, we attempt to transform this call into __builtin_fputc(). */
5268 expand_builtin_fputs (tree exp, rtx target, bool unlocked)
5270 /* Verify the arguments in the original call. */
5271 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5273 tree result = fold_builtin_fputs (CALL_EXPR_ARG (exp, 0),
5274 CALL_EXPR_ARG (exp, 1),
5275 (target == const0_rtx),
5276 unlocked, NULL_TREE);
5278 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
5283 /* Expand a call to __builtin_expect. We just return our argument
5284 as the builtin_expect semantic should've been already executed by
5285 tree branch prediction pass. */
5288 expand_builtin_expect (tree exp, rtx target)
5292 if (call_expr_nargs (exp) < 2)
5294 arg = CALL_EXPR_ARG (exp, 0);
5295 c = CALL_EXPR_ARG (exp, 1);
5297 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5298 /* When guessing was done, the hints should be already stripped away. */
5299 gcc_assert (!flag_guess_branch_prob
5300 || optimize == 0 || errorcount || sorrycount);
5305 expand_builtin_trap (void)
5309 emit_insn (gen_trap ());
5312 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5316 /* Expand a call to __builtin_unreachable. We do nothing except emit
5317 a barrier saying that control flow will not pass here.
5319 It is the responsibility of the program being compiled to ensure
5320 that control flow does never reach __builtin_unreachable. */
5322 expand_builtin_unreachable (void)
5327 /* Expand EXP, a call to fabs, fabsf or fabsl.
5328 Return NULL_RTX if a normal call should be emitted rather than expanding
5329 the function inline. If convenient, the result should be placed
5330 in TARGET. SUBTARGET may be used as the target for computing
5334 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5336 enum machine_mode mode;
5340 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5343 arg = CALL_EXPR_ARG (exp, 0);
5344 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5345 mode = TYPE_MODE (TREE_TYPE (arg));
5346 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5347 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5350 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5351 Return NULL is a normal call should be emitted rather than expanding the
5352 function inline. If convenient, the result should be placed in TARGET.
5353 SUBTARGET may be used as the target for computing the operand. */
5356 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5361 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5364 arg = CALL_EXPR_ARG (exp, 0);
5365 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5367 arg = CALL_EXPR_ARG (exp, 1);
5368 op1 = expand_normal (arg);
5370 return expand_copysign (op0, op1, target);
5373 /* Create a new constant string literal and return a char* pointer to it.
5374 The STRING_CST value is the LEN characters at STR. */
5376 build_string_literal (int len, const char *str)
5378 tree t, elem, index, type;
5380 t = build_string (len, str);
5381 elem = build_type_variant (char_type_node, 1, 0);
5382 index = build_index_type (size_int (len - 1));
5383 type = build_array_type (elem, index);
5384 TREE_TYPE (t) = type;
5385 TREE_CONSTANT (t) = 1;
5386 TREE_READONLY (t) = 1;
5387 TREE_STATIC (t) = 1;
5389 type = build_pointer_type (elem);
5390 t = build1 (ADDR_EXPR, type,
5391 build4 (ARRAY_REF, elem,
5392 t, integer_zero_node, NULL_TREE, NULL_TREE));
5396 /* Expand EXP, a call to printf or printf_unlocked.
5397 Return NULL_RTX if a normal call should be emitted rather than transforming
5398 the function inline. If convenient, the result should be placed in
5399 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
5402 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
5405 /* If we're using an unlocked function, assume the other unlocked
5406 functions exist explicitly. */
5407 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
5408 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
5409 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
5410 : implicit_built_in_decls[BUILT_IN_PUTS];
5411 const char *fmt_str;
5414 int nargs = call_expr_nargs (exp);
5416 /* If the return value is used, don't do the transformation. */
5417 if (target != const0_rtx)
5420 /* Verify the required arguments in the original call. */
5423 fmt = CALL_EXPR_ARG (exp, 0);
5424 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5427 /* Check whether the format is a literal string constant. */
5428 fmt_str = c_getstr (fmt);
5429 if (fmt_str == NULL)
5432 if (!init_target_chars ())
5435 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5436 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5439 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1))))
5442 fn = build_call_expr (fn_puts, 1, CALL_EXPR_ARG (exp, 1));
5444 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5445 else if (strcmp (fmt_str, target_percent_c) == 0)
5448 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE)
5451 fn = build_call_expr (fn_putchar, 1, CALL_EXPR_ARG (exp, 1));
5455 /* We can't handle anything else with % args or %% ... yet. */
5456 if (strchr (fmt_str, target_percent))
5462 /* If the format specifier was "", printf does nothing. */
5463 if (fmt_str[0] == '\0')
5465 /* If the format specifier has length of 1, call putchar. */
5466 if (fmt_str[1] == '\0')
5468 /* Given printf("c"), (where c is any one character,)
5469 convert "c"[0] to an int and pass that to the replacement
5471 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5473 fn = build_call_expr (fn_putchar, 1, arg);
5477 /* If the format specifier was "string\n", call puts("string"). */
5478 size_t len = strlen (fmt_str);
5479 if ((unsigned char)fmt_str[len - 1] == target_newline)
5481 /* Create a NUL-terminated string that's one char shorter
5482 than the original, stripping off the trailing '\n'. */
5483 char *newstr = XALLOCAVEC (char, len);
5484 memcpy (newstr, fmt_str, len - 1);
5485 newstr[len - 1] = 0;
5486 arg = build_string_literal (len, newstr);
5488 fn = build_call_expr (fn_puts, 1, arg);
5491 /* We'd like to arrange to call fputs(string,stdout) here,
5492 but we need stdout and don't have a way to get it yet. */
5499 if (TREE_CODE (fn) == CALL_EXPR)
5500 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5501 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5504 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5505 Return NULL_RTX if a normal call should be emitted rather than transforming
5506 the function inline. If convenient, the result should be placed in
5507 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5510 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5513 /* If we're using an unlocked function, assume the other unlocked
5514 functions exist explicitly. */
5515 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5516 : implicit_built_in_decls[BUILT_IN_FPUTC];
5517 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5518 : implicit_built_in_decls[BUILT_IN_FPUTS];
5519 const char *fmt_str;
5522 int nargs = call_expr_nargs (exp);
5524 /* If the return value is used, don't do the transformation. */
5525 if (target != const0_rtx)
5528 /* Verify the required arguments in the original call. */
5531 fp = CALL_EXPR_ARG (exp, 0);
5532 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5534 fmt = CALL_EXPR_ARG (exp, 1);
5535 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5538 /* Check whether the format is a literal string constant. */
5539 fmt_str = c_getstr (fmt);
5540 if (fmt_str == NULL)
5543 if (!init_target_chars ())
5546 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5547 if (strcmp (fmt_str, target_percent_s) == 0)
5550 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2))))
5552 arg = CALL_EXPR_ARG (exp, 2);
5554 fn = build_call_expr (fn_fputs, 2, arg, fp);
5556 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5557 else if (strcmp (fmt_str, target_percent_c) == 0)
5560 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE)
5562 arg = CALL_EXPR_ARG (exp, 2);
5564 fn = build_call_expr (fn_fputc, 2, arg, fp);
5568 /* We can't handle anything else with % args or %% ... yet. */
5569 if (strchr (fmt_str, target_percent))
5575 /* If the format specifier was "", fprintf does nothing. */
5576 if (fmt_str[0] == '\0')
5578 /* Evaluate and ignore FILE* argument for side-effects. */
5579 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5583 /* When "string" doesn't contain %, replace all cases of
5584 fprintf(stream,string) with fputs(string,stream). The fputs
5585 builtin will take care of special cases like length == 1. */
5587 fn = build_call_expr (fn_fputs, 2, fmt, fp);
5592 if (TREE_CODE (fn) == CALL_EXPR)
5593 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5594 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5597 /* Expand a call EXP to sprintf. Return NULL_RTX if
5598 a normal call should be emitted rather than expanding the function
5599 inline. If convenient, the result should be placed in TARGET with
5603 expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode)
5606 const char *fmt_str;
5607 int nargs = call_expr_nargs (exp);
5609 /* Verify the required arguments in the original call. */
5612 dest = CALL_EXPR_ARG (exp, 0);
5613 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5615 fmt = CALL_EXPR_ARG (exp, 0);
5616 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5619 /* Check whether the format is a literal string constant. */
5620 fmt_str = c_getstr (fmt);
5621 if (fmt_str == NULL)
5624 if (!init_target_chars ())
5627 /* If the format doesn't contain % args or %%, use strcpy. */
5628 if (strchr (fmt_str, target_percent) == 0)
5630 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5633 if ((nargs > 2) || ! fn)
5635 expand_expr (build_call_expr (fn, 2, dest, fmt),
5636 const0_rtx, VOIDmode, EXPAND_NORMAL);
5637 if (target == const0_rtx)
5639 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5640 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5642 /* If the format is "%s", use strcpy if the result isn't used. */
5643 else if (strcmp (fmt_str, target_percent_s) == 0)
5646 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5652 arg = CALL_EXPR_ARG (exp, 2);
5653 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5656 if (target != const0_rtx)
5658 len = c_strlen (arg, 1);
5659 if (! len || TREE_CODE (len) != INTEGER_CST)
5665 expand_expr (build_call_expr (fn, 2, dest, arg),
5666 const0_rtx, VOIDmode, EXPAND_NORMAL);
5668 if (target == const0_rtx)
5670 return expand_expr (len, target, mode, EXPAND_NORMAL);
5676 /* Expand a call to either the entry or exit function profiler. */
5679 expand_builtin_profile_func (bool exitp)
5681 rtx this_rtx, which;
5683 this_rtx = DECL_RTL (current_function_decl);
5684 gcc_assert (MEM_P (this_rtx));
5685 this_rtx = XEXP (this_rtx, 0);
5688 which = profile_function_exit_libfunc;
5690 which = profile_function_entry_libfunc;
5692 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5693 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5700 /* Expand a call to __builtin___clear_cache. */
5703 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5705 #ifndef HAVE_clear_cache
5706 #ifdef CLEAR_INSN_CACHE
5707 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5708 does something. Just do the default expansion to a call to
5712 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5713 does nothing. There is no need to call it. Do nothing. */
5715 #endif /* CLEAR_INSN_CACHE */
5717 /* We have a "clear_cache" insn, and it will handle everything. */
5719 rtx begin_rtx, end_rtx;
5720 enum insn_code icode;
5722 /* We must not expand to a library call. If we did, any
5723 fallback library function in libgcc that might contain a call to
5724 __builtin___clear_cache() would recurse infinitely. */
5725 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5727 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5731 if (HAVE_clear_cache)
5733 icode = CODE_FOR_clear_cache;
5735 begin = CALL_EXPR_ARG (exp, 0);
5736 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5737 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5738 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5739 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5741 end = CALL_EXPR_ARG (exp, 1);
5742 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5743 end_rtx = convert_memory_address (Pmode, end_rtx);
5744 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5745 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5747 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5750 #endif /* HAVE_clear_cache */
5753 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5756 round_trampoline_addr (rtx tramp)
5758 rtx temp, addend, mask;
5760 /* If we don't need too much alignment, we'll have been guaranteed
5761 proper alignment by get_trampoline_type. */
5762 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5765 /* Round address up to desired boundary. */
5766 temp = gen_reg_rtx (Pmode);
5767 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5768 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5770 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5771 temp, 0, OPTAB_LIB_WIDEN);
5772 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5773 temp, 0, OPTAB_LIB_WIDEN);
5779 expand_builtin_init_trampoline (tree exp)
5781 tree t_tramp, t_func, t_chain;
5782 rtx r_tramp, r_func, r_chain;
5783 #ifdef TRAMPOLINE_TEMPLATE
5787 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5788 POINTER_TYPE, VOID_TYPE))
5791 t_tramp = CALL_EXPR_ARG (exp, 0);
5792 t_func = CALL_EXPR_ARG (exp, 1);
5793 t_chain = CALL_EXPR_ARG (exp, 2);
5795 r_tramp = expand_normal (t_tramp);
5796 r_func = expand_normal (t_func);
5797 r_chain = expand_normal (t_chain);
5799 /* Generate insns to initialize the trampoline. */
5800 r_tramp = round_trampoline_addr (r_tramp);
5801 #ifdef TRAMPOLINE_TEMPLATE
5802 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5803 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5804 emit_block_move (blktramp, assemble_trampoline_template (),
5805 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5807 trampolines_created = 1;
5808 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5814 expand_builtin_adjust_trampoline (tree exp)
5818 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5821 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5822 tramp = round_trampoline_addr (tramp);
5823 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5824 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5830 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5831 function. The function first checks whether the back end provides
5832 an insn to implement signbit for the respective mode. If not, it
5833 checks whether the floating point format of the value is such that
5834 the sign bit can be extracted. If that is not the case, the
5835 function returns NULL_RTX to indicate that a normal call should be
5836 emitted rather than expanding the function in-line. EXP is the
5837 expression that is a call to the builtin function; if convenient,
5838 the result should be placed in TARGET. */
5840 expand_builtin_signbit (tree exp, rtx target)
5842 const struct real_format *fmt;
5843 enum machine_mode fmode, imode, rmode;
5844 HOST_WIDE_INT hi, lo;
5847 enum insn_code icode;
5850 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5853 arg = CALL_EXPR_ARG (exp, 0);
5854 fmode = TYPE_MODE (TREE_TYPE (arg));
5855 rmode = TYPE_MODE (TREE_TYPE (exp));
5856 fmt = REAL_MODE_FORMAT (fmode);
5858 arg = builtin_save_expr (arg);
5860 /* Expand the argument yielding a RTX expression. */
5861 temp = expand_normal (arg);
5863 /* Check if the back end provides an insn that handles signbit for the
5865 icode = signbit_optab->handlers [(int) fmode].insn_code;
5866 if (icode != CODE_FOR_nothing)
5868 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5869 emit_unop_insn (icode, target, temp, UNKNOWN);
5873 /* For floating point formats without a sign bit, implement signbit
5875 bitpos = fmt->signbit_ro;
5878 /* But we can't do this if the format supports signed zero. */
5879 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5882 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5883 build_real (TREE_TYPE (arg), dconst0));
5884 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5887 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5889 imode = int_mode_for_mode (fmode);
5890 if (imode == BLKmode)
5892 temp = gen_lowpart (imode, temp);
5897 /* Handle targets with different FP word orders. */
5898 if (FLOAT_WORDS_BIG_ENDIAN)
5899 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5901 word = bitpos / BITS_PER_WORD;
5902 temp = operand_subword_force (temp, word, fmode);
5903 bitpos = bitpos % BITS_PER_WORD;
5906 /* Force the intermediate word_mode (or narrower) result into a
5907 register. This avoids attempting to create paradoxical SUBREGs
5908 of floating point modes below. */
5909 temp = force_reg (imode, temp);
5911 /* If the bitpos is within the "result mode" lowpart, the operation
5912 can be implement with a single bitwise AND. Otherwise, we need
5913 a right shift and an AND. */
5915 if (bitpos < GET_MODE_BITSIZE (rmode))
5917 if (bitpos < HOST_BITS_PER_WIDE_INT)
5920 lo = (HOST_WIDE_INT) 1 << bitpos;
5924 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5928 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5929 temp = gen_lowpart (rmode, temp);
5930 temp = expand_binop (rmode, and_optab, temp,
5931 immed_double_const (lo, hi, rmode),
5932 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5936 /* Perform a logical right shift to place the signbit in the least
5937 significant bit, then truncate the result to the desired mode
5938 and mask just this bit. */
5939 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5940 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5941 temp = gen_lowpart (rmode, temp);
5942 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5943 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5949 /* Expand fork or exec calls. TARGET is the desired target of the
5950 call. EXP is the call. FN is the
5951 identificator of the actual function. IGNORE is nonzero if the
5952 value is to be ignored. */
5955 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5960 /* If we are not profiling, just call the function. */
5961 if (!profile_arc_flag)
5964 /* Otherwise call the wrapper. This should be equivalent for the rest of
5965 compiler, so the code does not diverge, and the wrapper may run the
5966 code necessary for keeping the profiling sane. */
5968 switch (DECL_FUNCTION_CODE (fn))
5971 id = get_identifier ("__gcov_fork");
5974 case BUILT_IN_EXECL:
5975 id = get_identifier ("__gcov_execl");
5978 case BUILT_IN_EXECV:
5979 id = get_identifier ("__gcov_execv");
5982 case BUILT_IN_EXECLP:
5983 id = get_identifier ("__gcov_execlp");
5986 case BUILT_IN_EXECLE:
5987 id = get_identifier ("__gcov_execle");
5990 case BUILT_IN_EXECVP:
5991 id = get_identifier ("__gcov_execvp");
5994 case BUILT_IN_EXECVE:
5995 id = get_identifier ("__gcov_execve");
6002 decl = build_decl (DECL_SOURCE_LOCATION (fn),
6003 FUNCTION_DECL, id, TREE_TYPE (fn));
6004 DECL_EXTERNAL (decl) = 1;
6005 TREE_PUBLIC (decl) = 1;
6006 DECL_ARTIFICIAL (decl) = 1;
6007 TREE_NOTHROW (decl) = 1;
6008 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6009 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6010 call = rewrite_call_expr (exp, 0, decl, 0);
6011 return expand_call (call, target, ignore);
6016 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6017 the pointer in these functions is void*, the tree optimizers may remove
6018 casts. The mode computed in expand_builtin isn't reliable either, due
6019 to __sync_bool_compare_and_swap.
6021 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6022 group of builtins. This gives us log2 of the mode size. */
6024 static inline enum machine_mode
6025 get_builtin_sync_mode (int fcode_diff)
6027 /* The size is not negotiable, so ask not to get BLKmode in return
6028 if the target indicates that a smaller size would be better. */
6029 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
6032 /* Expand the memory expression LOC and return the appropriate memory operand
6033 for the builtin_sync operations. */
6036 get_builtin_sync_mem (tree loc, enum machine_mode mode)
6040 addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM);
6042 /* Note that we explicitly do not want any alias information for this
6043 memory, so that we kill all other live memories. Otherwise we don't
6044 satisfy the full barrier semantics of the intrinsic. */
6045 mem = validize_mem (gen_rtx_MEM (mode, addr));
6047 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
6048 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6049 MEM_VOLATILE_P (mem) = 1;
6054 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6055 EXP is the CALL_EXPR. CODE is the rtx code
6056 that corresponds to the arithmetic or logical operation from the name;
6057 an exception here is that NOT actually means NAND. TARGET is an optional
6058 place for us to store the results; AFTER is true if this is the
6059 fetch_and_xxx form. IGNORE is true if we don't actually care about
6060 the result of the operation at all. */
6063 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
6064 enum rtx_code code, bool after,
6065 rtx target, bool ignore)
6068 enum machine_mode old_mode;
6069 location_t loc = EXPR_LOCATION (exp);
6071 if (code == NOT && warn_sync_nand)
6073 tree fndecl = get_callee_fndecl (exp);
6074 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6076 static bool warned_f_a_n, warned_n_a_f;
6080 case BUILT_IN_FETCH_AND_NAND_1:
6081 case BUILT_IN_FETCH_AND_NAND_2:
6082 case BUILT_IN_FETCH_AND_NAND_4:
6083 case BUILT_IN_FETCH_AND_NAND_8:
6084 case BUILT_IN_FETCH_AND_NAND_16:
6089 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
6090 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6091 warned_f_a_n = true;
6094 case BUILT_IN_NAND_AND_FETCH_1:
6095 case BUILT_IN_NAND_AND_FETCH_2:
6096 case BUILT_IN_NAND_AND_FETCH_4:
6097 case BUILT_IN_NAND_AND_FETCH_8:
6098 case BUILT_IN_NAND_AND_FETCH_16:
6103 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
6104 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6105 warned_n_a_f = true;
6113 /* Expand the operands. */
6114 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6116 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6117 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6118 of CONST_INTs, where we know the old_mode only from the call argument. */
6119 old_mode = GET_MODE (val);
6120 if (old_mode == VOIDmode)
6121 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6122 val = convert_modes (mode, old_mode, val, 1);
6125 return expand_sync_operation (mem, val, code);
6127 return expand_sync_fetch_operation (mem, val, code, after, target);
6130 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6131 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6132 true if this is the boolean form. TARGET is a place for us to store the
6133 results; this is NOT optional if IS_BOOL is true. */
6136 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
6137 bool is_bool, rtx target)
6139 rtx old_val, new_val, mem;
6140 enum machine_mode old_mode;
6142 /* Expand the operands. */
6143 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6146 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
6147 mode, EXPAND_NORMAL);
6148 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6149 of CONST_INTs, where we know the old_mode only from the call argument. */
6150 old_mode = GET_MODE (old_val);
6151 if (old_mode == VOIDmode)
6152 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6153 old_val = convert_modes (mode, old_mode, old_val, 1);
6155 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
6156 mode, EXPAND_NORMAL);
6157 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6158 of CONST_INTs, where we know the old_mode only from the call argument. */
6159 old_mode = GET_MODE (new_val);
6160 if (old_mode == VOIDmode)
6161 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
6162 new_val = convert_modes (mode, old_mode, new_val, 1);
6165 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
6167 return expand_val_compare_and_swap (mem, old_val, new_val, target);
6170 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6171 general form is actually an atomic exchange, and some targets only
6172 support a reduced form with the second argument being a constant 1.
6173 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6177 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
6181 enum machine_mode old_mode;
6183 /* Expand the operands. */
6184 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6185 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6186 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6187 of CONST_INTs, where we know the old_mode only from the call argument. */
6188 old_mode = GET_MODE (val);
6189 if (old_mode == VOIDmode)
6190 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6191 val = convert_modes (mode, old_mode, val, 1);
6193 return expand_sync_lock_test_and_set (mem, val, target);
6196 /* Expand the __sync_synchronize intrinsic. */
6199 expand_builtin_synchronize (void)
6203 #ifdef HAVE_memory_barrier
6204 if (HAVE_memory_barrier)
6206 emit_insn (gen_memory_barrier ());
6211 if (synchronize_libfunc != NULL_RTX)
6213 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
6217 /* If no explicit memory barrier instruction is available, create an
6218 empty asm stmt with a memory clobber. */
6219 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
6220 tree_cons (NULL, build_string (6, "memory"), NULL));
6221 ASM_VOLATILE_P (x) = 1;
6222 expand_asm_expr (x);
6225 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6228 expand_builtin_lock_release (enum machine_mode mode, tree exp)
6230 enum insn_code icode;
6232 rtx val = const0_rtx;
6234 /* Expand the operands. */
6235 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6237 /* If there is an explicit operation in the md file, use it. */
6238 icode = sync_lock_release[mode];
6239 if (icode != CODE_FOR_nothing)
6241 if (!insn_data[icode].operand[1].predicate (val, mode))
6242 val = force_reg (mode, val);
6244 insn = GEN_FCN (icode) (mem, val);
6252 /* Otherwise we can implement this operation by emitting a barrier
6253 followed by a store of zero. */
6254 expand_builtin_synchronize ();
6255 emit_move_insn (mem, val);
6258 /* Expand an expression EXP that calls a built-in function,
6259 with result going to TARGET if that's convenient
6260 (and in mode MODE if that's convenient).
6261 SUBTARGET may be used as the target for computing one of EXP's operands.
6262 IGNORE is nonzero if the value is to be ignored. */
6265 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
6268 tree fndecl = get_callee_fndecl (exp);
6269 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6270 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6272 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6273 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6275 /* When not optimizing, generate calls to library functions for a certain
6278 && !called_as_built_in (fndecl)
6279 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
6280 && fcode != BUILT_IN_ALLOCA
6281 && fcode != BUILT_IN_FREE)
6282 return expand_call (exp, target, ignore);
6284 /* The built-in function expanders test for target == const0_rtx
6285 to determine whether the function's result will be ignored. */
6287 target = const0_rtx;
6289 /* If the result of a pure or const built-in function is ignored, and
6290 none of its arguments are volatile, we can avoid expanding the
6291 built-in call and just evaluate the arguments for side-effects. */
6292 if (target == const0_rtx
6293 && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl)))
6295 bool volatilep = false;
6297 call_expr_arg_iterator iter;
6299 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6300 if (TREE_THIS_VOLATILE (arg))
6308 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6309 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6316 CASE_FLT_FN (BUILT_IN_FABS):
6317 target = expand_builtin_fabs (exp, target, subtarget);
6322 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6323 target = expand_builtin_copysign (exp, target, subtarget);
6328 /* Just do a normal library call if we were unable to fold
6330 CASE_FLT_FN (BUILT_IN_CABS):
6333 CASE_FLT_FN (BUILT_IN_EXP):
6334 CASE_FLT_FN (BUILT_IN_EXP10):
6335 CASE_FLT_FN (BUILT_IN_POW10):
6336 CASE_FLT_FN (BUILT_IN_EXP2):
6337 CASE_FLT_FN (BUILT_IN_EXPM1):
6338 CASE_FLT_FN (BUILT_IN_LOGB):
6339 CASE_FLT_FN (BUILT_IN_LOG):
6340 CASE_FLT_FN (BUILT_IN_LOG10):
6341 CASE_FLT_FN (BUILT_IN_LOG2):
6342 CASE_FLT_FN (BUILT_IN_LOG1P):
6343 CASE_FLT_FN (BUILT_IN_TAN):
6344 CASE_FLT_FN (BUILT_IN_ASIN):
6345 CASE_FLT_FN (BUILT_IN_ACOS):
6346 CASE_FLT_FN (BUILT_IN_ATAN):
6347 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
6348 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6349 because of possible accuracy problems. */
6350 if (! flag_unsafe_math_optimizations)
6352 CASE_FLT_FN (BUILT_IN_SQRT):
6353 CASE_FLT_FN (BUILT_IN_FLOOR):
6354 CASE_FLT_FN (BUILT_IN_CEIL):
6355 CASE_FLT_FN (BUILT_IN_TRUNC):
6356 CASE_FLT_FN (BUILT_IN_ROUND):
6357 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6358 CASE_FLT_FN (BUILT_IN_RINT):
6359 target = expand_builtin_mathfn (exp, target, subtarget);
6364 CASE_FLT_FN (BUILT_IN_ILOGB):
6365 if (! flag_unsafe_math_optimizations)
6367 CASE_FLT_FN (BUILT_IN_ISINF):
6368 CASE_FLT_FN (BUILT_IN_FINITE):
6369 case BUILT_IN_ISFINITE:
6370 case BUILT_IN_ISNORMAL:
6371 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
6376 CASE_FLT_FN (BUILT_IN_LCEIL):
6377 CASE_FLT_FN (BUILT_IN_LLCEIL):
6378 CASE_FLT_FN (BUILT_IN_LFLOOR):
6379 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6380 target = expand_builtin_int_roundingfn (exp, target);
6385 CASE_FLT_FN (BUILT_IN_LRINT):
6386 CASE_FLT_FN (BUILT_IN_LLRINT):
6387 CASE_FLT_FN (BUILT_IN_LROUND):
6388 CASE_FLT_FN (BUILT_IN_LLROUND):
6389 target = expand_builtin_int_roundingfn_2 (exp, target);
6394 CASE_FLT_FN (BUILT_IN_POW):
6395 target = expand_builtin_pow (exp, target, subtarget);
6400 CASE_FLT_FN (BUILT_IN_POWI):
6401 target = expand_builtin_powi (exp, target, subtarget);
6406 CASE_FLT_FN (BUILT_IN_ATAN2):
6407 CASE_FLT_FN (BUILT_IN_LDEXP):
6408 CASE_FLT_FN (BUILT_IN_SCALB):
6409 CASE_FLT_FN (BUILT_IN_SCALBN):
6410 CASE_FLT_FN (BUILT_IN_SCALBLN):
6411 if (! flag_unsafe_math_optimizations)
6414 CASE_FLT_FN (BUILT_IN_FMOD):
6415 CASE_FLT_FN (BUILT_IN_REMAINDER):
6416 CASE_FLT_FN (BUILT_IN_DREM):
6417 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6422 CASE_FLT_FN (BUILT_IN_CEXPI):
6423 target = expand_builtin_cexpi (exp, target, subtarget);
6424 gcc_assert (target);
6427 CASE_FLT_FN (BUILT_IN_SIN):
6428 CASE_FLT_FN (BUILT_IN_COS):
6429 if (! flag_unsafe_math_optimizations)
6431 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6436 CASE_FLT_FN (BUILT_IN_SINCOS):
6437 if (! flag_unsafe_math_optimizations)
6439 target = expand_builtin_sincos (exp);
6444 case BUILT_IN_APPLY_ARGS:
6445 return expand_builtin_apply_args ();
6447 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6448 FUNCTION with a copy of the parameters described by
6449 ARGUMENTS, and ARGSIZE. It returns a block of memory
6450 allocated on the stack into which is stored all the registers
6451 that might possibly be used for returning the result of a
6452 function. ARGUMENTS is the value returned by
6453 __builtin_apply_args. ARGSIZE is the number of bytes of
6454 arguments that must be copied. ??? How should this value be
6455 computed? We'll also need a safe worst case value for varargs
6457 case BUILT_IN_APPLY:
6458 if (!validate_arglist (exp, POINTER_TYPE,
6459 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6460 && !validate_arglist (exp, REFERENCE_TYPE,
6461 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6467 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6468 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6469 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6471 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6474 /* __builtin_return (RESULT) causes the function to return the
6475 value described by RESULT. RESULT is address of the block of
6476 memory returned by __builtin_apply. */
6477 case BUILT_IN_RETURN:
6478 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6479 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6482 case BUILT_IN_SAVEREGS:
6483 return expand_builtin_saveregs ();
6485 case BUILT_IN_ARGS_INFO:
6486 return expand_builtin_args_info (exp);
6488 case BUILT_IN_VA_ARG_PACK:
6489 /* All valid uses of __builtin_va_arg_pack () are removed during
6491 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6494 case BUILT_IN_VA_ARG_PACK_LEN:
6495 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6497 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6500 /* Return the address of the first anonymous stack arg. */
6501 case BUILT_IN_NEXT_ARG:
6502 if (fold_builtin_next_arg (exp, false))
6504 return expand_builtin_next_arg ();
6506 case BUILT_IN_CLEAR_CACHE:
6507 target = expand_builtin___clear_cache (exp);
6512 case BUILT_IN_CLASSIFY_TYPE:
6513 return expand_builtin_classify_type (exp);
6515 case BUILT_IN_CONSTANT_P:
6518 case BUILT_IN_FRAME_ADDRESS:
6519 case BUILT_IN_RETURN_ADDRESS:
6520 return expand_builtin_frame_address (fndecl, exp);
6522 /* Returns the address of the area where the structure is returned.
6524 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6525 if (call_expr_nargs (exp) != 0
6526 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6527 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6530 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6532 case BUILT_IN_ALLOCA:
6533 target = expand_builtin_alloca (exp, target);
6538 case BUILT_IN_STACK_SAVE:
6539 return expand_stack_save ();
6541 case BUILT_IN_STACK_RESTORE:
6542 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6545 case BUILT_IN_BSWAP32:
6546 case BUILT_IN_BSWAP64:
6547 target = expand_builtin_bswap (exp, target, subtarget);
6553 CASE_INT_FN (BUILT_IN_FFS):
6554 case BUILT_IN_FFSIMAX:
6555 target = expand_builtin_unop (target_mode, exp, target,
6556 subtarget, ffs_optab);
6561 CASE_INT_FN (BUILT_IN_CLZ):
6562 case BUILT_IN_CLZIMAX:
6563 target = expand_builtin_unop (target_mode, exp, target,
6564 subtarget, clz_optab);
6569 CASE_INT_FN (BUILT_IN_CTZ):
6570 case BUILT_IN_CTZIMAX:
6571 target = expand_builtin_unop (target_mode, exp, target,
6572 subtarget, ctz_optab);
6577 CASE_INT_FN (BUILT_IN_POPCOUNT):
6578 case BUILT_IN_POPCOUNTIMAX:
6579 target = expand_builtin_unop (target_mode, exp, target,
6580 subtarget, popcount_optab);
6585 CASE_INT_FN (BUILT_IN_PARITY):
6586 case BUILT_IN_PARITYIMAX:
6587 target = expand_builtin_unop (target_mode, exp, target,
6588 subtarget, parity_optab);
6593 case BUILT_IN_STRLEN:
6594 target = expand_builtin_strlen (exp, target, target_mode);
6599 case BUILT_IN_STRCPY:
6600 target = expand_builtin_strcpy (fndecl, exp, target, mode);
6605 case BUILT_IN_STRNCPY:
6606 target = expand_builtin_strncpy (exp, target, mode);
6611 case BUILT_IN_STPCPY:
6612 target = expand_builtin_stpcpy (exp, target, mode);
6617 case BUILT_IN_STRCAT:
6618 target = expand_builtin_strcat (fndecl, exp, target, mode);
6623 case BUILT_IN_STRNCAT:
6624 target = expand_builtin_strncat (exp, target, mode);
6629 case BUILT_IN_STRSPN:
6630 target = expand_builtin_strspn (exp, target, mode);
6635 case BUILT_IN_STRCSPN:
6636 target = expand_builtin_strcspn (exp, target, mode);
6641 case BUILT_IN_STRSTR:
6642 target = expand_builtin_strstr (exp, target, mode);
6647 case BUILT_IN_STRPBRK:
6648 target = expand_builtin_strpbrk (exp, target, mode);
6653 case BUILT_IN_INDEX:
6654 case BUILT_IN_STRCHR:
6655 target = expand_builtin_strchr (exp, target, mode);
6660 case BUILT_IN_RINDEX:
6661 case BUILT_IN_STRRCHR:
6662 target = expand_builtin_strrchr (exp, target, mode);
6667 case BUILT_IN_MEMCPY:
6668 target = expand_builtin_memcpy (exp, target, mode);
6673 case BUILT_IN_MEMPCPY:
6674 target = expand_builtin_mempcpy (exp, target, mode);
6679 case BUILT_IN_MEMMOVE:
6680 target = expand_builtin_memmove (exp, target, mode, ignore);
6685 case BUILT_IN_BCOPY:
6686 target = expand_builtin_bcopy (exp, ignore);
6691 case BUILT_IN_MEMSET:
6692 target = expand_builtin_memset (exp, target, mode);
6697 case BUILT_IN_BZERO:
6698 target = expand_builtin_bzero (exp);
6703 case BUILT_IN_STRCMP:
6704 target = expand_builtin_strcmp (exp, target, mode);
6709 case BUILT_IN_STRNCMP:
6710 target = expand_builtin_strncmp (exp, target, mode);
6715 case BUILT_IN_MEMCHR:
6716 target = expand_builtin_memchr (exp, target, mode);
6722 case BUILT_IN_MEMCMP:
6723 target = expand_builtin_memcmp (exp, target, mode);
6728 case BUILT_IN_SETJMP:
6729 /* This should have been lowered to the builtins below. */
6732 case BUILT_IN_SETJMP_SETUP:
6733 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6734 and the receiver label. */
6735 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6737 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6738 VOIDmode, EXPAND_NORMAL);
6739 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6740 rtx label_r = label_rtx (label);
6742 /* This is copied from the handling of non-local gotos. */
6743 expand_builtin_setjmp_setup (buf_addr, label_r);
6744 nonlocal_goto_handler_labels
6745 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6746 nonlocal_goto_handler_labels);
6747 /* ??? Do not let expand_label treat us as such since we would
6748 not want to be both on the list of non-local labels and on
6749 the list of forced labels. */
6750 FORCED_LABEL (label) = 0;
6755 case BUILT_IN_SETJMP_DISPATCHER:
6756 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6757 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6759 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6760 rtx label_r = label_rtx (label);
6762 /* Remove the dispatcher label from the list of non-local labels
6763 since the receiver labels have been added to it above. */
6764 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6769 case BUILT_IN_SETJMP_RECEIVER:
6770 /* __builtin_setjmp_receiver is passed the receiver label. */
6771 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6773 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6774 rtx label_r = label_rtx (label);
6776 expand_builtin_setjmp_receiver (label_r);
6781 /* __builtin_longjmp is passed a pointer to an array of five words.
6782 It's similar to the C library longjmp function but works with
6783 __builtin_setjmp above. */
6784 case BUILT_IN_LONGJMP:
6785 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6787 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6788 VOIDmode, EXPAND_NORMAL);
6789 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6791 if (value != const1_rtx)
6793 error ("%<__builtin_longjmp%> second argument must be 1");
6797 expand_builtin_longjmp (buf_addr, value);
6802 case BUILT_IN_NONLOCAL_GOTO:
6803 target = expand_builtin_nonlocal_goto (exp);
6808 /* This updates the setjmp buffer that is its argument with the value
6809 of the current stack pointer. */
6810 case BUILT_IN_UPDATE_SETJMP_BUF:
6811 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6814 = expand_normal (CALL_EXPR_ARG (exp, 0));
6816 expand_builtin_update_setjmp_buf (buf_addr);
6822 expand_builtin_trap ();
6825 case BUILT_IN_UNREACHABLE:
6826 expand_builtin_unreachable ();
6829 case BUILT_IN_PRINTF:
6830 target = expand_builtin_printf (exp, target, mode, false);
6835 case BUILT_IN_PRINTF_UNLOCKED:
6836 target = expand_builtin_printf (exp, target, mode, true);
6841 case BUILT_IN_FPUTS:
6842 target = expand_builtin_fputs (exp, target, false);
6846 case BUILT_IN_FPUTS_UNLOCKED:
6847 target = expand_builtin_fputs (exp, target, true);
6852 case BUILT_IN_FPRINTF:
6853 target = expand_builtin_fprintf (exp, target, mode, false);
6858 case BUILT_IN_FPRINTF_UNLOCKED:
6859 target = expand_builtin_fprintf (exp, target, mode, true);
6864 case BUILT_IN_SPRINTF:
6865 target = expand_builtin_sprintf (exp, target, mode);
6870 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6871 case BUILT_IN_SIGNBITD32:
6872 case BUILT_IN_SIGNBITD64:
6873 case BUILT_IN_SIGNBITD128:
6874 target = expand_builtin_signbit (exp, target);
6879 /* Various hooks for the DWARF 2 __throw routine. */
6880 case BUILT_IN_UNWIND_INIT:
6881 expand_builtin_unwind_init ();
6883 case BUILT_IN_DWARF_CFA:
6884 return virtual_cfa_rtx;
6885 #ifdef DWARF2_UNWIND_INFO
6886 case BUILT_IN_DWARF_SP_COLUMN:
6887 return expand_builtin_dwarf_sp_column ();
6888 case BUILT_IN_INIT_DWARF_REG_SIZES:
6889 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6892 case BUILT_IN_FROB_RETURN_ADDR:
6893 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6894 case BUILT_IN_EXTRACT_RETURN_ADDR:
6895 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6896 case BUILT_IN_EH_RETURN:
6897 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6898 CALL_EXPR_ARG (exp, 1));
6900 #ifdef EH_RETURN_DATA_REGNO
6901 case BUILT_IN_EH_RETURN_DATA_REGNO:
6902 return expand_builtin_eh_return_data_regno (exp);
6904 case BUILT_IN_EXTEND_POINTER:
6905 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6907 case BUILT_IN_VA_START:
6908 return expand_builtin_va_start (exp);
6909 case BUILT_IN_VA_END:
6910 return expand_builtin_va_end (exp);
6911 case BUILT_IN_VA_COPY:
6912 return expand_builtin_va_copy (exp);
6913 case BUILT_IN_EXPECT:
6914 return expand_builtin_expect (exp, target);
6915 case BUILT_IN_PREFETCH:
6916 expand_builtin_prefetch (exp);
6919 case BUILT_IN_PROFILE_FUNC_ENTER:
6920 return expand_builtin_profile_func (false);
6921 case BUILT_IN_PROFILE_FUNC_EXIT:
6922 return expand_builtin_profile_func (true);
6924 case BUILT_IN_INIT_TRAMPOLINE:
6925 return expand_builtin_init_trampoline (exp);
6926 case BUILT_IN_ADJUST_TRAMPOLINE:
6927 return expand_builtin_adjust_trampoline (exp);
6930 case BUILT_IN_EXECL:
6931 case BUILT_IN_EXECV:
6932 case BUILT_IN_EXECLP:
6933 case BUILT_IN_EXECLE:
6934 case BUILT_IN_EXECVP:
6935 case BUILT_IN_EXECVE:
6936 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6941 case BUILT_IN_FETCH_AND_ADD_1:
6942 case BUILT_IN_FETCH_AND_ADD_2:
6943 case BUILT_IN_FETCH_AND_ADD_4:
6944 case BUILT_IN_FETCH_AND_ADD_8:
6945 case BUILT_IN_FETCH_AND_ADD_16:
6946 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6947 target = expand_builtin_sync_operation (mode, exp, PLUS,
6948 false, target, ignore);
6953 case BUILT_IN_FETCH_AND_SUB_1:
6954 case BUILT_IN_FETCH_AND_SUB_2:
6955 case BUILT_IN_FETCH_AND_SUB_4:
6956 case BUILT_IN_FETCH_AND_SUB_8:
6957 case BUILT_IN_FETCH_AND_SUB_16:
6958 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6959 target = expand_builtin_sync_operation (mode, exp, MINUS,
6960 false, target, ignore);
6965 case BUILT_IN_FETCH_AND_OR_1:
6966 case BUILT_IN_FETCH_AND_OR_2:
6967 case BUILT_IN_FETCH_AND_OR_4:
6968 case BUILT_IN_FETCH_AND_OR_8:
6969 case BUILT_IN_FETCH_AND_OR_16:
6970 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6971 target = expand_builtin_sync_operation (mode, exp, IOR,
6972 false, target, ignore);
6977 case BUILT_IN_FETCH_AND_AND_1:
6978 case BUILT_IN_FETCH_AND_AND_2:
6979 case BUILT_IN_FETCH_AND_AND_4:
6980 case BUILT_IN_FETCH_AND_AND_8:
6981 case BUILT_IN_FETCH_AND_AND_16:
6982 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6983 target = expand_builtin_sync_operation (mode, exp, AND,
6984 false, target, ignore);
6989 case BUILT_IN_FETCH_AND_XOR_1:
6990 case BUILT_IN_FETCH_AND_XOR_2:
6991 case BUILT_IN_FETCH_AND_XOR_4:
6992 case BUILT_IN_FETCH_AND_XOR_8:
6993 case BUILT_IN_FETCH_AND_XOR_16:
6994 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6995 target = expand_builtin_sync_operation (mode, exp, XOR,
6996 false, target, ignore);
7001 case BUILT_IN_FETCH_AND_NAND_1:
7002 case BUILT_IN_FETCH_AND_NAND_2:
7003 case BUILT_IN_FETCH_AND_NAND_4:
7004 case BUILT_IN_FETCH_AND_NAND_8:
7005 case BUILT_IN_FETCH_AND_NAND_16:
7006 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
7007 target = expand_builtin_sync_operation (mode, exp, NOT,
7008 false, target, ignore);
7013 case BUILT_IN_ADD_AND_FETCH_1:
7014 case BUILT_IN_ADD_AND_FETCH_2:
7015 case BUILT_IN_ADD_AND_FETCH_4:
7016 case BUILT_IN_ADD_AND_FETCH_8:
7017 case BUILT_IN_ADD_AND_FETCH_16:
7018 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
7019 target = expand_builtin_sync_operation (mode, exp, PLUS,
7020 true, target, ignore);
7025 case BUILT_IN_SUB_AND_FETCH_1:
7026 case BUILT_IN_SUB_AND_FETCH_2:
7027 case BUILT_IN_SUB_AND_FETCH_4:
7028 case BUILT_IN_SUB_AND_FETCH_8:
7029 case BUILT_IN_SUB_AND_FETCH_16:
7030 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
7031 target = expand_builtin_sync_operation (mode, exp, MINUS,
7032 true, target, ignore);
7037 case BUILT_IN_OR_AND_FETCH_1:
7038 case BUILT_IN_OR_AND_FETCH_2:
7039 case BUILT_IN_OR_AND_FETCH_4:
7040 case BUILT_IN_OR_AND_FETCH_8:
7041 case BUILT_IN_OR_AND_FETCH_16:
7042 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
7043 target = expand_builtin_sync_operation (mode, exp, IOR,
7044 true, target, ignore);
7049 case BUILT_IN_AND_AND_FETCH_1:
7050 case BUILT_IN_AND_AND_FETCH_2:
7051 case BUILT_IN_AND_AND_FETCH_4:
7052 case BUILT_IN_AND_AND_FETCH_8:
7053 case BUILT_IN_AND_AND_FETCH_16:
7054 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
7055 target = expand_builtin_sync_operation (mode, exp, AND,
7056 true, target, ignore);
7061 case BUILT_IN_XOR_AND_FETCH_1:
7062 case BUILT_IN_XOR_AND_FETCH_2:
7063 case BUILT_IN_XOR_AND_FETCH_4:
7064 case BUILT_IN_XOR_AND_FETCH_8:
7065 case BUILT_IN_XOR_AND_FETCH_16:
7066 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
7067 target = expand_builtin_sync_operation (mode, exp, XOR,
7068 true, target, ignore);
7073 case BUILT_IN_NAND_AND_FETCH_1:
7074 case BUILT_IN_NAND_AND_FETCH_2:
7075 case BUILT_IN_NAND_AND_FETCH_4:
7076 case BUILT_IN_NAND_AND_FETCH_8:
7077 case BUILT_IN_NAND_AND_FETCH_16:
7078 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
7079 target = expand_builtin_sync_operation (mode, exp, NOT,
7080 true, target, ignore);
7085 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
7086 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
7087 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
7088 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
7089 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
7090 if (mode == VOIDmode)
7091 mode = TYPE_MODE (boolean_type_node);
7092 if (!target || !register_operand (target, mode))
7093 target = gen_reg_rtx (mode);
7095 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
7096 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7101 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
7102 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
7103 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
7104 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
7105 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
7106 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
7107 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7112 case BUILT_IN_LOCK_TEST_AND_SET_1:
7113 case BUILT_IN_LOCK_TEST_AND_SET_2:
7114 case BUILT_IN_LOCK_TEST_AND_SET_4:
7115 case BUILT_IN_LOCK_TEST_AND_SET_8:
7116 case BUILT_IN_LOCK_TEST_AND_SET_16:
7117 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
7118 target = expand_builtin_lock_test_and_set (mode, exp, target);
7123 case BUILT_IN_LOCK_RELEASE_1:
7124 case BUILT_IN_LOCK_RELEASE_2:
7125 case BUILT_IN_LOCK_RELEASE_4:
7126 case BUILT_IN_LOCK_RELEASE_8:
7127 case BUILT_IN_LOCK_RELEASE_16:
7128 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
7129 expand_builtin_lock_release (mode, exp);
7132 case BUILT_IN_SYNCHRONIZE:
7133 expand_builtin_synchronize ();
7136 case BUILT_IN_OBJECT_SIZE:
7137 return expand_builtin_object_size (exp);
7139 case BUILT_IN_MEMCPY_CHK:
7140 case BUILT_IN_MEMPCPY_CHK:
7141 case BUILT_IN_MEMMOVE_CHK:
7142 case BUILT_IN_MEMSET_CHK:
7143 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7148 case BUILT_IN_STRCPY_CHK:
7149 case BUILT_IN_STPCPY_CHK:
7150 case BUILT_IN_STRNCPY_CHK:
7151 case BUILT_IN_STRCAT_CHK:
7152 case BUILT_IN_STRNCAT_CHK:
7153 case BUILT_IN_SNPRINTF_CHK:
7154 case BUILT_IN_VSNPRINTF_CHK:
7155 maybe_emit_chk_warning (exp, fcode);
7158 case BUILT_IN_SPRINTF_CHK:
7159 case BUILT_IN_VSPRINTF_CHK:
7160 maybe_emit_sprintf_chk_warning (exp, fcode);
7164 maybe_emit_free_warning (exp);
7167 default: /* just do library call, if unknown builtin */
7171 /* The switch statement above can drop through to cause the function
7172 to be called normally. */
7173 return expand_call (exp, target, ignore);
7176 /* Determine whether a tree node represents a call to a built-in
7177 function. If the tree T is a call to a built-in function with
7178 the right number of arguments of the appropriate types, return
7179 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7180 Otherwise the return value is END_BUILTINS. */
7182 enum built_in_function
7183 builtin_mathfn_code (const_tree t)
7185 const_tree fndecl, arg, parmlist;
7186 const_tree argtype, parmtype;
7187 const_call_expr_arg_iterator iter;
7189 if (TREE_CODE (t) != CALL_EXPR
7190 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7191 return END_BUILTINS;
7193 fndecl = get_callee_fndecl (t);
7194 if (fndecl == NULL_TREE
7195 || TREE_CODE (fndecl) != FUNCTION_DECL
7196 || ! DECL_BUILT_IN (fndecl)
7197 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7198 return END_BUILTINS;
7200 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7201 init_const_call_expr_arg_iterator (t, &iter);
7202 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7204 /* If a function doesn't take a variable number of arguments,
7205 the last element in the list will have type `void'. */
7206 parmtype = TREE_VALUE (parmlist);
7207 if (VOID_TYPE_P (parmtype))
7209 if (more_const_call_expr_args_p (&iter))
7210 return END_BUILTINS;
7211 return DECL_FUNCTION_CODE (fndecl);
7214 if (! more_const_call_expr_args_p (&iter))
7215 return END_BUILTINS;
7217 arg = next_const_call_expr_arg (&iter);
7218 argtype = TREE_TYPE (arg);
7220 if (SCALAR_FLOAT_TYPE_P (parmtype))
7222 if (! SCALAR_FLOAT_TYPE_P (argtype))
7223 return END_BUILTINS;
7225 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7227 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7228 return END_BUILTINS;
7230 else if (POINTER_TYPE_P (parmtype))
7232 if (! POINTER_TYPE_P (argtype))
7233 return END_BUILTINS;
7235 else if (INTEGRAL_TYPE_P (parmtype))
7237 if (! INTEGRAL_TYPE_P (argtype))
7238 return END_BUILTINS;
7241 return END_BUILTINS;
7244 /* Variable-length argument list. */
7245 return DECL_FUNCTION_CODE (fndecl);
7248 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7249 evaluate to a constant. */
7252 fold_builtin_constant_p (tree arg)
7254 /* We return 1 for a numeric type that's known to be a constant
7255 value at compile-time or for an aggregate type that's a
7256 literal constant. */
7259 /* If we know this is a constant, emit the constant of one. */
7260 if (CONSTANT_CLASS_P (arg)
7261 || (TREE_CODE (arg) == CONSTRUCTOR
7262 && TREE_CONSTANT (arg)))
7263 return integer_one_node;
7264 if (TREE_CODE (arg) == ADDR_EXPR)
7266 tree op = TREE_OPERAND (arg, 0);
7267 if (TREE_CODE (op) == STRING_CST
7268 || (TREE_CODE (op) == ARRAY_REF
7269 && integer_zerop (TREE_OPERAND (op, 1))
7270 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7271 return integer_one_node;
7274 /* If this expression has side effects, show we don't know it to be a
7275 constant. Likewise if it's a pointer or aggregate type since in
7276 those case we only want literals, since those are only optimized
7277 when generating RTL, not later.
7278 And finally, if we are compiling an initializer, not code, we
7279 need to return a definite result now; there's not going to be any
7280 more optimization done. */
7281 if (TREE_SIDE_EFFECTS (arg)
7282 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7283 || POINTER_TYPE_P (TREE_TYPE (arg))
7285 || folding_initializer)
7286 return integer_zero_node;
7291 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7292 return it as a truthvalue. */
7295 build_builtin_expect_predicate (tree pred, tree expected)
7297 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7299 fn = built_in_decls[BUILT_IN_EXPECT];
7300 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7301 ret_type = TREE_TYPE (TREE_TYPE (fn));
7302 pred_type = TREE_VALUE (arg_types);
7303 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7305 pred = fold_convert (pred_type, pred);
7306 expected = fold_convert (expected_type, expected);
7307 call_expr = build_call_expr (fn, 2, pred, expected);
7309 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7310 build_int_cst (ret_type, 0));
7313 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7314 NULL_TREE if no simplification is possible. */
7317 fold_builtin_expect (tree arg0, tree arg1)
7320 enum tree_code code;
7322 /* If this is a builtin_expect within a builtin_expect keep the
7323 inner one. See through a comparison against a constant. It
7324 might have been added to create a thruthvalue. */
7326 if (COMPARISON_CLASS_P (inner)
7327 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7328 inner = TREE_OPERAND (inner, 0);
7330 if (TREE_CODE (inner) == CALL_EXPR
7331 && (fndecl = get_callee_fndecl (inner))
7332 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7333 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7336 /* Distribute the expected value over short-circuiting operators.
7337 See through the cast from truthvalue_type_node to long. */
7339 while (TREE_CODE (inner) == NOP_EXPR
7340 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
7341 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
7342 inner = TREE_OPERAND (inner, 0);
7344 code = TREE_CODE (inner);
7345 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7347 tree op0 = TREE_OPERAND (inner, 0);
7348 tree op1 = TREE_OPERAND (inner, 1);
7350 op0 = build_builtin_expect_predicate (op0, arg1);
7351 op1 = build_builtin_expect_predicate (op1, arg1);
7352 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7354 return fold_convert (TREE_TYPE (arg0), inner);
7357 /* If the argument isn't invariant then there's nothing else we can do. */
7358 if (!TREE_CONSTANT (arg0))
7361 /* If we expect that a comparison against the argument will fold to
7362 a constant return the constant. In practice, this means a true
7363 constant or the address of a non-weak symbol. */
7366 if (TREE_CODE (inner) == ADDR_EXPR)
7370 inner = TREE_OPERAND (inner, 0);
7372 while (TREE_CODE (inner) == COMPONENT_REF
7373 || TREE_CODE (inner) == ARRAY_REF);
7374 if ((TREE_CODE (inner) == VAR_DECL
7375 || TREE_CODE (inner) == FUNCTION_DECL)
7376 && DECL_WEAK (inner))
7380 /* Otherwise, ARG0 already has the proper type for the return value. */
7384 /* Fold a call to __builtin_classify_type with argument ARG. */
7387 fold_builtin_classify_type (tree arg)
7390 return build_int_cst (NULL_TREE, no_type_class);
7392 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
7395 /* Fold a call to __builtin_strlen with argument ARG. */
7398 fold_builtin_strlen (tree arg)
7400 if (!validate_arg (arg, POINTER_TYPE))
7404 tree len = c_strlen (arg, 0);
7408 /* Convert from the internal "sizetype" type to "size_t". */
7410 len = fold_convert (size_type_node, len);
7418 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7421 fold_builtin_inf (tree type, int warn)
7423 REAL_VALUE_TYPE real;
7425 /* __builtin_inff is intended to be usable to define INFINITY on all
7426 targets. If an infinity is not available, INFINITY expands "to a
7427 positive constant of type float that overflows at translation
7428 time", footnote "In this case, using INFINITY will violate the
7429 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7430 Thus we pedwarn to ensure this constraint violation is
7432 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7433 pedwarn (input_location, 0, "target format does not support infinity");
7436 return build_real (type, real);
7439 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7442 fold_builtin_nan (tree arg, tree type, int quiet)
7444 REAL_VALUE_TYPE real;
7447 if (!validate_arg (arg, POINTER_TYPE))
7449 str = c_getstr (arg);
7453 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7456 return build_real (type, real);
7459 /* Return true if the floating point expression T has an integer value.
7460 We also allow +Inf, -Inf and NaN to be considered integer values. */
7463 integer_valued_real_p (tree t)
7465 switch (TREE_CODE (t))
7472 return integer_valued_real_p (TREE_OPERAND (t, 0));
7477 return integer_valued_real_p (TREE_OPERAND (t, 1));
7484 return integer_valued_real_p (TREE_OPERAND (t, 0))
7485 && integer_valued_real_p (TREE_OPERAND (t, 1));
7488 return integer_valued_real_p (TREE_OPERAND (t, 1))
7489 && integer_valued_real_p (TREE_OPERAND (t, 2));
7492 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7496 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7497 if (TREE_CODE (type) == INTEGER_TYPE)
7499 if (TREE_CODE (type) == REAL_TYPE)
7500 return integer_valued_real_p (TREE_OPERAND (t, 0));
7505 switch (builtin_mathfn_code (t))
7507 CASE_FLT_FN (BUILT_IN_CEIL):
7508 CASE_FLT_FN (BUILT_IN_FLOOR):
7509 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7510 CASE_FLT_FN (BUILT_IN_RINT):
7511 CASE_FLT_FN (BUILT_IN_ROUND):
7512 CASE_FLT_FN (BUILT_IN_TRUNC):
7515 CASE_FLT_FN (BUILT_IN_FMIN):
7516 CASE_FLT_FN (BUILT_IN_FMAX):
7517 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7518 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7531 /* FNDECL is assumed to be a builtin where truncation can be propagated
7532 across (for instance floor((double)f) == (double)floorf (f).
7533 Do the transformation for a call with argument ARG. */
7536 fold_trunc_transparent_mathfn (tree fndecl, tree arg)
7538 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7540 if (!validate_arg (arg, REAL_TYPE))
7543 /* Integer rounding functions are idempotent. */
7544 if (fcode == builtin_mathfn_code (arg))
7547 /* If argument is already integer valued, and we don't need to worry
7548 about setting errno, there's no need to perform rounding. */
7549 if (! flag_errno_math && integer_valued_real_p (arg))
7554 tree arg0 = strip_float_extensions (arg);
7555 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7556 tree newtype = TREE_TYPE (arg0);
7559 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7560 && (decl = mathfn_built_in (newtype, fcode)))
7561 return fold_convert (ftype,
7562 build_call_expr (decl, 1,
7563 fold_convert (newtype, arg0)));
7568 /* FNDECL is assumed to be builtin which can narrow the FP type of
7569 the argument, for instance lround((double)f) -> lroundf (f).
7570 Do the transformation for a call with argument ARG. */
7573 fold_fixed_mathfn (tree fndecl, tree arg)
7575 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7577 if (!validate_arg (arg, REAL_TYPE))
7580 /* If argument is already integer valued, and we don't need to worry
7581 about setting errno, there's no need to perform rounding. */
7582 if (! flag_errno_math && integer_valued_real_p (arg))
7583 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
7587 tree ftype = TREE_TYPE (arg);
7588 tree arg0 = strip_float_extensions (arg);
7589 tree newtype = TREE_TYPE (arg0);
7592 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7593 && (decl = mathfn_built_in (newtype, fcode)))
7594 return build_call_expr (decl, 1, fold_convert (newtype, arg0));
7597 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7598 sizeof (long long) == sizeof (long). */
7599 if (TYPE_PRECISION (long_long_integer_type_node)
7600 == TYPE_PRECISION (long_integer_type_node))
7602 tree newfn = NULL_TREE;
7605 CASE_FLT_FN (BUILT_IN_LLCEIL):
7606 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7609 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7610 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7613 CASE_FLT_FN (BUILT_IN_LLROUND):
7614 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7617 CASE_FLT_FN (BUILT_IN_LLRINT):
7618 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7627 tree newcall = build_call_expr(newfn, 1, arg);
7628 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7635 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7636 return type. Return NULL_TREE if no simplification can be made. */
7639 fold_builtin_cabs (tree arg, tree type, tree fndecl)
7643 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7644 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7647 /* Calculate the result when the argument is a constant. */
7648 if (TREE_CODE (arg) == COMPLEX_CST
7649 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7653 if (TREE_CODE (arg) == COMPLEX_EXPR)
7655 tree real = TREE_OPERAND (arg, 0);
7656 tree imag = TREE_OPERAND (arg, 1);
7658 /* If either part is zero, cabs is fabs of the other. */
7659 if (real_zerop (real))
7660 return fold_build1 (ABS_EXPR, type, imag);
7661 if (real_zerop (imag))
7662 return fold_build1 (ABS_EXPR, type, real);
7664 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7665 if (flag_unsafe_math_optimizations
7666 && operand_equal_p (real, imag, OEP_PURE_SAME))
7668 const REAL_VALUE_TYPE sqrt2_trunc
7669 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7671 return fold_build2 (MULT_EXPR, type,
7672 fold_build1 (ABS_EXPR, type, real),
7673 build_real (type, sqrt2_trunc));
7677 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7678 if (TREE_CODE (arg) == NEGATE_EXPR
7679 || TREE_CODE (arg) == CONJ_EXPR)
7680 return build_call_expr (fndecl, 1, TREE_OPERAND (arg, 0));
7682 /* Don't do this when optimizing for size. */
7683 if (flag_unsafe_math_optimizations
7684 && optimize && optimize_function_for_speed_p (cfun))
7686 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7688 if (sqrtfn != NULL_TREE)
7690 tree rpart, ipart, result;
7692 arg = builtin_save_expr (arg);
7694 rpart = fold_build1 (REALPART_EXPR, type, arg);
7695 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7697 rpart = builtin_save_expr (rpart);
7698 ipart = builtin_save_expr (ipart);
7700 result = fold_build2 (PLUS_EXPR, type,
7701 fold_build2 (MULT_EXPR, type,
7703 fold_build2 (MULT_EXPR, type,
7706 return build_call_expr (sqrtfn, 1, result);
7713 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7714 Return NULL_TREE if no simplification can be made. */
7717 fold_builtin_sqrt (tree arg, tree type)
7720 enum built_in_function fcode;
7723 if (!validate_arg (arg, REAL_TYPE))
7726 /* Calculate the result when the argument is a constant. */
7727 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7730 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7731 fcode = builtin_mathfn_code (arg);
7732 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7734 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7735 arg = fold_build2 (MULT_EXPR, type,
7736 CALL_EXPR_ARG (arg, 0),
7737 build_real (type, dconsthalf));
7738 return build_call_expr (expfn, 1, arg);
7741 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7742 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7744 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7748 tree arg0 = CALL_EXPR_ARG (arg, 0);
7750 /* The inner root was either sqrt or cbrt. */
7751 /* This was a conditional expression but it triggered a bug
7753 REAL_VALUE_TYPE dconstroot;
7754 if (BUILTIN_SQRT_P (fcode))
7755 dconstroot = dconsthalf;
7757 dconstroot = dconst_third ();
7759 /* Adjust for the outer root. */
7760 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7761 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7762 tree_root = build_real (type, dconstroot);
7763 return build_call_expr (powfn, 2, arg0, tree_root);
7767 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7768 if (flag_unsafe_math_optimizations
7769 && (fcode == BUILT_IN_POW
7770 || fcode == BUILT_IN_POWF
7771 || fcode == BUILT_IN_POWL))
7773 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7774 tree arg0 = CALL_EXPR_ARG (arg, 0);
7775 tree arg1 = CALL_EXPR_ARG (arg, 1);
7777 if (!tree_expr_nonnegative_p (arg0))
7778 arg0 = build1 (ABS_EXPR, type, arg0);
7779 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7780 build_real (type, dconsthalf));
7781 return build_call_expr (powfn, 2, arg0, narg1);
7787 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7788 Return NULL_TREE if no simplification can be made. */
7791 fold_builtin_cbrt (tree arg, tree type)
7793 const enum built_in_function fcode = builtin_mathfn_code (arg);
7796 if (!validate_arg (arg, REAL_TYPE))
7799 /* Calculate the result when the argument is a constant. */
7800 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7803 if (flag_unsafe_math_optimizations)
7805 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7806 if (BUILTIN_EXPONENT_P (fcode))
7808 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7809 const REAL_VALUE_TYPE third_trunc =
7810 real_value_truncate (TYPE_MODE (type), dconst_third ());
7811 arg = fold_build2 (MULT_EXPR, type,
7812 CALL_EXPR_ARG (arg, 0),
7813 build_real (type, third_trunc));
7814 return build_call_expr (expfn, 1, arg);
7817 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7818 if (BUILTIN_SQRT_P (fcode))
7820 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7824 tree arg0 = CALL_EXPR_ARG (arg, 0);
7826 REAL_VALUE_TYPE dconstroot = dconst_third ();
7828 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7829 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7830 tree_root = build_real (type, dconstroot);
7831 return build_call_expr (powfn, 2, arg0, tree_root);
7835 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7836 if (BUILTIN_CBRT_P (fcode))
7838 tree arg0 = CALL_EXPR_ARG (arg, 0);
7839 if (tree_expr_nonnegative_p (arg0))
7841 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7846 REAL_VALUE_TYPE dconstroot;
7848 real_arithmetic (&dconstroot, MULT_EXPR,
7849 dconst_third_ptr (), dconst_third_ptr ());
7850 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7851 tree_root = build_real (type, dconstroot);
7852 return build_call_expr (powfn, 2, arg0, tree_root);
7857 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7858 if (fcode == BUILT_IN_POW
7859 || fcode == BUILT_IN_POWF
7860 || fcode == BUILT_IN_POWL)
7862 tree arg00 = CALL_EXPR_ARG (arg, 0);
7863 tree arg01 = CALL_EXPR_ARG (arg, 1);
7864 if (tree_expr_nonnegative_p (arg00))
7866 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7867 const REAL_VALUE_TYPE dconstroot
7868 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7869 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7870 build_real (type, dconstroot));
7871 return build_call_expr (powfn, 2, arg00, narg01);
7878 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7879 TYPE is the type of the return value. Return NULL_TREE if no
7880 simplification can be made. */
7883 fold_builtin_cos (tree arg, tree type, tree fndecl)
7887 if (!validate_arg (arg, REAL_TYPE))
7890 /* Calculate the result when the argument is a constant. */
7891 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7894 /* Optimize cos(-x) into cos (x). */
7895 if ((narg = fold_strip_sign_ops (arg)))
7896 return build_call_expr (fndecl, 1, narg);
7901 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7902 Return NULL_TREE if no simplification can be made. */
7905 fold_builtin_cosh (tree arg, tree type, tree fndecl)
7907 if (validate_arg (arg, REAL_TYPE))
7911 /* Calculate the result when the argument is a constant. */
7912 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7915 /* Optimize cosh(-x) into cosh (x). */
7916 if ((narg = fold_strip_sign_ops (arg)))
7917 return build_call_expr (fndecl, 1, narg);
7923 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7924 argument ARG. TYPE is the type of the return value. Return
7925 NULL_TREE if no simplification can be made. */
7928 fold_builtin_ccos (tree arg, tree type ATTRIBUTE_UNUSED, tree fndecl,
7929 bool hyper ATTRIBUTE_UNUSED)
7931 if (validate_arg (arg, COMPLEX_TYPE)
7932 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7937 /* Calculate the result when the argument is a constant. */
7938 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7942 /* Optimize fn(-x) into fn(x). */
7943 if ((tmp = fold_strip_sign_ops (arg)))
7944 return build_call_expr (fndecl, 1, tmp);
7950 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7951 Return NULL_TREE if no simplification can be made. */
7954 fold_builtin_tan (tree arg, tree type)
7956 enum built_in_function fcode;
7959 if (!validate_arg (arg, REAL_TYPE))
7962 /* Calculate the result when the argument is a constant. */
7963 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7966 /* Optimize tan(atan(x)) = x. */
7967 fcode = builtin_mathfn_code (arg);
7968 if (flag_unsafe_math_optimizations
7969 && (fcode == BUILT_IN_ATAN
7970 || fcode == BUILT_IN_ATANF
7971 || fcode == BUILT_IN_ATANL))
7972 return CALL_EXPR_ARG (arg, 0);
7977 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7978 NULL_TREE if no simplification can be made. */
7981 fold_builtin_sincos (tree arg0, tree arg1, tree arg2)
7986 if (!validate_arg (arg0, REAL_TYPE)
7987 || !validate_arg (arg1, POINTER_TYPE)
7988 || !validate_arg (arg2, POINTER_TYPE))
7991 type = TREE_TYPE (arg0);
7993 /* Calculate the result when the argument is a constant. */
7994 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7997 /* Canonicalize sincos to cexpi. */
7998 if (!TARGET_C99_FUNCTIONS)
8000 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
8004 call = build_call_expr (fn, 1, arg0);
8005 call = builtin_save_expr (call);
8007 return build2 (COMPOUND_EXPR, void_type_node,
8008 build2 (MODIFY_EXPR, void_type_node,
8009 build_fold_indirect_ref (arg1),
8010 build1 (IMAGPART_EXPR, type, call)),
8011 build2 (MODIFY_EXPR, void_type_node,
8012 build_fold_indirect_ref (arg2),
8013 build1 (REALPART_EXPR, type, call)));
8016 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8017 NULL_TREE if no simplification can be made. */
8020 fold_builtin_cexp (tree arg0, tree type)
8023 tree realp, imagp, ifn;
8028 if (!validate_arg (arg0, COMPLEX_TYPE)
8029 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8033 /* Calculate the result when the argument is a constant. */
8034 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
8038 rtype = TREE_TYPE (TREE_TYPE (arg0));
8040 /* In case we can figure out the real part of arg0 and it is constant zero
8042 if (!TARGET_C99_FUNCTIONS)
8044 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
8048 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
8049 && real_zerop (realp))
8051 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
8052 return build_call_expr (ifn, 1, narg);
8055 /* In case we can easily decompose real and imaginary parts split cexp
8056 to exp (r) * cexpi (i). */
8057 if (flag_unsafe_math_optimizations
8060 tree rfn, rcall, icall;
8062 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
8066 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
8070 icall = build_call_expr (ifn, 1, imagp);
8071 icall = builtin_save_expr (icall);
8072 rcall = build_call_expr (rfn, 1, realp);
8073 rcall = builtin_save_expr (rcall);
8074 return fold_build2 (COMPLEX_EXPR, type,
8075 fold_build2 (MULT_EXPR, rtype,
8077 fold_build1 (REALPART_EXPR, rtype, icall)),
8078 fold_build2 (MULT_EXPR, rtype,
8080 fold_build1 (IMAGPART_EXPR, rtype, icall)));
8086 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8087 Return NULL_TREE if no simplification can be made. */
8090 fold_builtin_trunc (tree fndecl, tree arg)
8092 if (!validate_arg (arg, REAL_TYPE))
8095 /* Optimize trunc of constant value. */
8096 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8098 REAL_VALUE_TYPE r, x;
8099 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8101 x = TREE_REAL_CST (arg);
8102 real_trunc (&r, TYPE_MODE (type), &x);
8103 return build_real (type, r);
8106 return fold_trunc_transparent_mathfn (fndecl, arg);
8109 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8110 Return NULL_TREE if no simplification can be made. */
8113 fold_builtin_floor (tree fndecl, tree arg)
8115 if (!validate_arg (arg, REAL_TYPE))
8118 /* Optimize floor of constant value. */
8119 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8123 x = TREE_REAL_CST (arg);
8124 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8126 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8129 real_floor (&r, TYPE_MODE (type), &x);
8130 return build_real (type, r);
8134 /* Fold floor (x) where x is nonnegative to trunc (x). */
8135 if (tree_expr_nonnegative_p (arg))
8137 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8139 return build_call_expr (truncfn, 1, arg);
8142 return fold_trunc_transparent_mathfn (fndecl, arg);
8145 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8146 Return NULL_TREE if no simplification can be made. */
8149 fold_builtin_ceil (tree fndecl, tree arg)
8151 if (!validate_arg (arg, REAL_TYPE))
8154 /* Optimize ceil of constant value. */
8155 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8159 x = TREE_REAL_CST (arg);
8160 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8162 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8165 real_ceil (&r, TYPE_MODE (type), &x);
8166 return build_real (type, r);
8170 return fold_trunc_transparent_mathfn (fndecl, arg);
8173 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8174 Return NULL_TREE if no simplification can be made. */
8177 fold_builtin_round (tree fndecl, tree arg)
8179 if (!validate_arg (arg, REAL_TYPE))
8182 /* Optimize round of constant value. */
8183 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8187 x = TREE_REAL_CST (arg);
8188 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8190 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8193 real_round (&r, TYPE_MODE (type), &x);
8194 return build_real (type, r);
8198 return fold_trunc_transparent_mathfn (fndecl, arg);
8201 /* Fold function call to builtin lround, lroundf or lroundl (or the
8202 corresponding long long versions) and other rounding functions. ARG
8203 is the argument to the call. Return NULL_TREE if no simplification
8207 fold_builtin_int_roundingfn (tree fndecl, tree arg)
8209 if (!validate_arg (arg, REAL_TYPE))
8212 /* Optimize lround of constant value. */
8213 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8215 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8217 if (real_isfinite (&x))
8219 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8220 tree ftype = TREE_TYPE (arg);
8221 unsigned HOST_WIDE_INT lo2;
8222 HOST_WIDE_INT hi, lo;
8225 switch (DECL_FUNCTION_CODE (fndecl))
8227 CASE_FLT_FN (BUILT_IN_LFLOOR):
8228 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8229 real_floor (&r, TYPE_MODE (ftype), &x);
8232 CASE_FLT_FN (BUILT_IN_LCEIL):
8233 CASE_FLT_FN (BUILT_IN_LLCEIL):
8234 real_ceil (&r, TYPE_MODE (ftype), &x);
8237 CASE_FLT_FN (BUILT_IN_LROUND):
8238 CASE_FLT_FN (BUILT_IN_LLROUND):
8239 real_round (&r, TYPE_MODE (ftype), &x);
8246 REAL_VALUE_TO_INT (&lo, &hi, r);
8247 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
8248 return build_int_cst_wide (itype, lo2, hi);
8252 switch (DECL_FUNCTION_CODE (fndecl))
8254 CASE_FLT_FN (BUILT_IN_LFLOOR):
8255 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8256 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8257 if (tree_expr_nonnegative_p (arg))
8258 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
8264 return fold_fixed_mathfn (fndecl, arg);
8267 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8268 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8269 the argument to the call. Return NULL_TREE if no simplification can
8273 fold_builtin_bitop (tree fndecl, tree arg)
8275 if (!validate_arg (arg, INTEGER_TYPE))
8278 /* Optimize for constant argument. */
8279 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8281 HOST_WIDE_INT hi, width, result;
8282 unsigned HOST_WIDE_INT lo;
8285 type = TREE_TYPE (arg);
8286 width = TYPE_PRECISION (type);
8287 lo = TREE_INT_CST_LOW (arg);
8289 /* Clear all the bits that are beyond the type's precision. */
8290 if (width > HOST_BITS_PER_WIDE_INT)
8292 hi = TREE_INT_CST_HIGH (arg);
8293 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8294 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
8299 if (width < HOST_BITS_PER_WIDE_INT)
8300 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8303 switch (DECL_FUNCTION_CODE (fndecl))
8305 CASE_INT_FN (BUILT_IN_FFS):
8307 result = exact_log2 (lo & -lo) + 1;
8309 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
8314 CASE_INT_FN (BUILT_IN_CLZ):
8316 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8318 result = width - floor_log2 (lo) - 1;
8319 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8323 CASE_INT_FN (BUILT_IN_CTZ):
8325 result = exact_log2 (lo & -lo);
8327 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
8328 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8332 CASE_INT_FN (BUILT_IN_POPCOUNT):
8335 result++, lo &= lo - 1;
8337 result++, hi &= hi - 1;
8340 CASE_INT_FN (BUILT_IN_PARITY):
8343 result++, lo &= lo - 1;
8345 result++, hi &= hi - 1;
8353 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8359 /* Fold function call to builtin_bswap and the long and long long
8360 variants. Return NULL_TREE if no simplification can be made. */
8362 fold_builtin_bswap (tree fndecl, tree arg)
8364 if (! validate_arg (arg, INTEGER_TYPE))
8367 /* Optimize constant value. */
8368 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8370 HOST_WIDE_INT hi, width, r_hi = 0;
8371 unsigned HOST_WIDE_INT lo, r_lo = 0;
8374 type = TREE_TYPE (arg);
8375 width = TYPE_PRECISION (type);
8376 lo = TREE_INT_CST_LOW (arg);
8377 hi = TREE_INT_CST_HIGH (arg);
8379 switch (DECL_FUNCTION_CODE (fndecl))
8381 case BUILT_IN_BSWAP32:
8382 case BUILT_IN_BSWAP64:
8386 for (s = 0; s < width; s += 8)
8388 int d = width - s - 8;
8389 unsigned HOST_WIDE_INT byte;
8391 if (s < HOST_BITS_PER_WIDE_INT)
8392 byte = (lo >> s) & 0xff;
8394 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8396 if (d < HOST_BITS_PER_WIDE_INT)
8399 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8409 if (width < HOST_BITS_PER_WIDE_INT)
8410 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8412 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8418 /* A subroutine of fold_builtin to fold the various logarithmic
8419 functions. Return NULL_TREE if no simplification can me made.
8420 FUNC is the corresponding MPFR logarithm function. */
8423 fold_builtin_logarithm (tree fndecl, tree arg,
8424 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8426 if (validate_arg (arg, REAL_TYPE))
8428 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8430 const enum built_in_function fcode = builtin_mathfn_code (arg);
8432 /* Calculate the result when the argument is a constant. */
8433 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8436 /* Special case, optimize logN(expN(x)) = x. */
8437 if (flag_unsafe_math_optimizations
8438 && ((func == mpfr_log
8439 && (fcode == BUILT_IN_EXP
8440 || fcode == BUILT_IN_EXPF
8441 || fcode == BUILT_IN_EXPL))
8442 || (func == mpfr_log2
8443 && (fcode == BUILT_IN_EXP2
8444 || fcode == BUILT_IN_EXP2F
8445 || fcode == BUILT_IN_EXP2L))
8446 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8447 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8449 /* Optimize logN(func()) for various exponential functions. We
8450 want to determine the value "x" and the power "exponent" in
8451 order to transform logN(x**exponent) into exponent*logN(x). */
8452 if (flag_unsafe_math_optimizations)
8454 tree exponent = 0, x = 0;
8458 CASE_FLT_FN (BUILT_IN_EXP):
8459 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8460 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8462 exponent = CALL_EXPR_ARG (arg, 0);
8464 CASE_FLT_FN (BUILT_IN_EXP2):
8465 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8466 x = build_real (type, dconst2);
8467 exponent = CALL_EXPR_ARG (arg, 0);
8469 CASE_FLT_FN (BUILT_IN_EXP10):
8470 CASE_FLT_FN (BUILT_IN_POW10):
8471 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8473 REAL_VALUE_TYPE dconst10;
8474 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8475 x = build_real (type, dconst10);
8477 exponent = CALL_EXPR_ARG (arg, 0);
8479 CASE_FLT_FN (BUILT_IN_SQRT):
8480 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8481 x = CALL_EXPR_ARG (arg, 0);
8482 exponent = build_real (type, dconsthalf);
8484 CASE_FLT_FN (BUILT_IN_CBRT):
8485 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8486 x = CALL_EXPR_ARG (arg, 0);
8487 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8490 CASE_FLT_FN (BUILT_IN_POW):
8491 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8492 x = CALL_EXPR_ARG (arg, 0);
8493 exponent = CALL_EXPR_ARG (arg, 1);
8499 /* Now perform the optimization. */
8502 tree logfn = build_call_expr (fndecl, 1, x);
8503 return fold_build2 (MULT_EXPR, type, exponent, logfn);
8511 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8512 NULL_TREE if no simplification can be made. */
8515 fold_builtin_hypot (tree fndecl, tree arg0, tree arg1, tree type)
8517 tree res, narg0, narg1;
8519 if (!validate_arg (arg0, REAL_TYPE)
8520 || !validate_arg (arg1, REAL_TYPE))
8523 /* Calculate the result when the argument is a constant. */
8524 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8527 /* If either argument to hypot has a negate or abs, strip that off.
8528 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8529 narg0 = fold_strip_sign_ops (arg0);
8530 narg1 = fold_strip_sign_ops (arg1);
8533 return build_call_expr (fndecl, 2, narg0 ? narg0 : arg0,
8534 narg1 ? narg1 : arg1);
8537 /* If either argument is zero, hypot is fabs of the other. */
8538 if (real_zerop (arg0))
8539 return fold_build1 (ABS_EXPR, type, arg1);
8540 else if (real_zerop (arg1))
8541 return fold_build1 (ABS_EXPR, type, arg0);
8543 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8544 if (flag_unsafe_math_optimizations
8545 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8547 const REAL_VALUE_TYPE sqrt2_trunc
8548 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8549 return fold_build2 (MULT_EXPR, type,
8550 fold_build1 (ABS_EXPR, type, arg0),
8551 build_real (type, sqrt2_trunc));
8558 /* Fold a builtin function call to pow, powf, or powl. Return
8559 NULL_TREE if no simplification can be made. */
8561 fold_builtin_pow (tree fndecl, tree arg0, tree arg1, tree type)
8565 if (!validate_arg (arg0, REAL_TYPE)
8566 || !validate_arg (arg1, REAL_TYPE))
8569 /* Calculate the result when the argument is a constant. */
8570 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8573 /* Optimize pow(1.0,y) = 1.0. */
8574 if (real_onep (arg0))
8575 return omit_one_operand (type, build_real (type, dconst1), arg1);
8577 if (TREE_CODE (arg1) == REAL_CST
8578 && !TREE_OVERFLOW (arg1))
8580 REAL_VALUE_TYPE cint;
8584 c = TREE_REAL_CST (arg1);
8586 /* Optimize pow(x,0.0) = 1.0. */
8587 if (REAL_VALUES_EQUAL (c, dconst0))
8588 return omit_one_operand (type, build_real (type, dconst1),
8591 /* Optimize pow(x,1.0) = x. */
8592 if (REAL_VALUES_EQUAL (c, dconst1))
8595 /* Optimize pow(x,-1.0) = 1.0/x. */
8596 if (REAL_VALUES_EQUAL (c, dconstm1))
8597 return fold_build2 (RDIV_EXPR, type,
8598 build_real (type, dconst1), arg0);
8600 /* Optimize pow(x,0.5) = sqrt(x). */
8601 if (flag_unsafe_math_optimizations
8602 && REAL_VALUES_EQUAL (c, dconsthalf))
8604 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8606 if (sqrtfn != NULL_TREE)
8607 return build_call_expr (sqrtfn, 1, arg0);
8610 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8611 if (flag_unsafe_math_optimizations)
8613 const REAL_VALUE_TYPE dconstroot
8614 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8616 if (REAL_VALUES_EQUAL (c, dconstroot))
8618 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8619 if (cbrtfn != NULL_TREE)
8620 return build_call_expr (cbrtfn, 1, arg0);
8624 /* Check for an integer exponent. */
8625 n = real_to_integer (&c);
8626 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8627 if (real_identical (&c, &cint))
8629 /* Attempt to evaluate pow at compile-time, unless this should
8630 raise an exception. */
8631 if (TREE_CODE (arg0) == REAL_CST
8632 && !TREE_OVERFLOW (arg0)
8634 || (!flag_trapping_math && !flag_errno_math)
8635 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8640 x = TREE_REAL_CST (arg0);
8641 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8642 if (flag_unsafe_math_optimizations || !inexact)
8643 return build_real (type, x);
8646 /* Strip sign ops from even integer powers. */
8647 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8649 tree narg0 = fold_strip_sign_ops (arg0);
8651 return build_call_expr (fndecl, 2, narg0, arg1);
8656 if (flag_unsafe_math_optimizations)
8658 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8660 /* Optimize pow(expN(x),y) = expN(x*y). */
8661 if (BUILTIN_EXPONENT_P (fcode))
8663 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8664 tree arg = CALL_EXPR_ARG (arg0, 0);
8665 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8666 return build_call_expr (expfn, 1, arg);
8669 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8670 if (BUILTIN_SQRT_P (fcode))
8672 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8673 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8674 build_real (type, dconsthalf));
8675 return build_call_expr (fndecl, 2, narg0, narg1);
8678 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8679 if (BUILTIN_CBRT_P (fcode))
8681 tree arg = CALL_EXPR_ARG (arg0, 0);
8682 if (tree_expr_nonnegative_p (arg))
8684 const REAL_VALUE_TYPE dconstroot
8685 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8686 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8687 build_real (type, dconstroot));
8688 return build_call_expr (fndecl, 2, arg, narg1);
8692 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8693 if (fcode == BUILT_IN_POW
8694 || fcode == BUILT_IN_POWF
8695 || fcode == BUILT_IN_POWL)
8697 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8698 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8699 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8700 return build_call_expr (fndecl, 2, arg00, narg1);
8707 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8708 Return NULL_TREE if no simplification can be made. */
8710 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED,
8711 tree arg0, tree arg1, tree type)
8713 if (!validate_arg (arg0, REAL_TYPE)
8714 || !validate_arg (arg1, INTEGER_TYPE))
8717 /* Optimize pow(1.0,y) = 1.0. */
8718 if (real_onep (arg0))
8719 return omit_one_operand (type, build_real (type, dconst1), arg1);
8721 if (host_integerp (arg1, 0))
8723 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8725 /* Evaluate powi at compile-time. */
8726 if (TREE_CODE (arg0) == REAL_CST
8727 && !TREE_OVERFLOW (arg0))
8730 x = TREE_REAL_CST (arg0);
8731 real_powi (&x, TYPE_MODE (type), &x, c);
8732 return build_real (type, x);
8735 /* Optimize pow(x,0) = 1.0. */
8737 return omit_one_operand (type, build_real (type, dconst1),
8740 /* Optimize pow(x,1) = x. */
8744 /* Optimize pow(x,-1) = 1.0/x. */
8746 return fold_build2 (RDIV_EXPR, type,
8747 build_real (type, dconst1), arg0);
8753 /* A subroutine of fold_builtin to fold the various exponent
8754 functions. Return NULL_TREE if no simplification can be made.
8755 FUNC is the corresponding MPFR exponent function. */
8758 fold_builtin_exponent (tree fndecl, tree arg,
8759 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8761 if (validate_arg (arg, REAL_TYPE))
8763 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8766 /* Calculate the result when the argument is a constant. */
8767 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8770 /* Optimize expN(logN(x)) = x. */
8771 if (flag_unsafe_math_optimizations)
8773 const enum built_in_function fcode = builtin_mathfn_code (arg);
8775 if ((func == mpfr_exp
8776 && (fcode == BUILT_IN_LOG
8777 || fcode == BUILT_IN_LOGF
8778 || fcode == BUILT_IN_LOGL))
8779 || (func == mpfr_exp2
8780 && (fcode == BUILT_IN_LOG2
8781 || fcode == BUILT_IN_LOG2F
8782 || fcode == BUILT_IN_LOG2L))
8783 || (func == mpfr_exp10
8784 && (fcode == BUILT_IN_LOG10
8785 || fcode == BUILT_IN_LOG10F
8786 || fcode == BUILT_IN_LOG10L)))
8787 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8794 /* Return true if VAR is a VAR_DECL or a component thereof. */
8797 var_decl_component_p (tree var)
8800 while (handled_component_p (inner))
8801 inner = TREE_OPERAND (inner, 0);
8802 return SSA_VAR_P (inner);
8805 /* Fold function call to builtin memset. Return
8806 NULL_TREE if no simplification can be made. */
8809 fold_builtin_memset (tree dest, tree c, tree len, tree type, bool ignore)
8811 tree var, ret, etype;
8812 unsigned HOST_WIDE_INT length, cval;
8814 if (! validate_arg (dest, POINTER_TYPE)
8815 || ! validate_arg (c, INTEGER_TYPE)
8816 || ! validate_arg (len, INTEGER_TYPE))
8819 if (! host_integerp (len, 1))
8822 /* If the LEN parameter is zero, return DEST. */
8823 if (integer_zerop (len))
8824 return omit_one_operand (type, dest, c);
8826 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8831 if (TREE_CODE (var) != ADDR_EXPR)
8834 var = TREE_OPERAND (var, 0);
8835 if (TREE_THIS_VOLATILE (var))
8838 etype = TREE_TYPE (var);
8839 if (TREE_CODE (etype) == ARRAY_TYPE)
8840 etype = TREE_TYPE (etype);
8842 if (!INTEGRAL_TYPE_P (etype)
8843 && !POINTER_TYPE_P (etype))
8846 if (! var_decl_component_p (var))
8849 length = tree_low_cst (len, 1);
8850 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8851 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8855 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8858 if (integer_zerop (c))
8862 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8865 cval = tree_low_cst (c, 1);
8869 cval |= (cval << 31) << 1;
8872 ret = build_int_cst_type (etype, cval);
8873 var = build_fold_indirect_ref (fold_convert (build_pointer_type (etype),
8875 ret = build2 (MODIFY_EXPR, etype, var, ret);
8879 return omit_one_operand (type, dest, ret);
8882 /* Fold function call to builtin memset. Return
8883 NULL_TREE if no simplification can be made. */
8886 fold_builtin_bzero (tree dest, tree size, bool ignore)
8888 if (! validate_arg (dest, POINTER_TYPE)
8889 || ! validate_arg (size, INTEGER_TYPE))
8895 /* New argument list transforming bzero(ptr x, int y) to
8896 memset(ptr x, int 0, size_t y). This is done this way
8897 so that if it isn't expanded inline, we fallback to
8898 calling bzero instead of memset. */
8900 return fold_builtin_memset (dest, integer_zero_node,
8901 fold_convert (sizetype, size),
8902 void_type_node, ignore);
8905 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8906 NULL_TREE if no simplification can be made.
8907 If ENDP is 0, return DEST (like memcpy).
8908 If ENDP is 1, return DEST+LEN (like mempcpy).
8909 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8910 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8914 fold_builtin_memory_op (tree dest, tree src, tree len, tree type, bool ignore, int endp)
8916 tree destvar, srcvar, expr;
8918 if (! validate_arg (dest, POINTER_TYPE)
8919 || ! validate_arg (src, POINTER_TYPE)
8920 || ! validate_arg (len, INTEGER_TYPE))
8923 /* If the LEN parameter is zero, return DEST. */
8924 if (integer_zerop (len))
8925 return omit_one_operand (type, dest, src);
8927 /* If SRC and DEST are the same (and not volatile), return
8928 DEST{,+LEN,+LEN-1}. */
8929 if (operand_equal_p (src, dest, 0))
8933 tree srctype, desttype;
8934 int src_align, dest_align;
8938 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8939 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8941 /* Both DEST and SRC must be pointer types.
8942 ??? This is what old code did. Is the testing for pointer types
8945 If either SRC is readonly or length is 1, we can use memcpy. */
8946 if (!dest_align || !src_align)
8948 if (readonly_data_expr (src)
8949 || (host_integerp (len, 1)
8950 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8951 >= tree_low_cst (len, 1))))
8953 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8956 return build_call_expr (fn, 3, dest, src, len);
8959 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8960 srcvar = build_fold_indirect_ref (src);
8961 destvar = build_fold_indirect_ref (dest);
8963 && !TREE_THIS_VOLATILE (srcvar)
8965 && !TREE_THIS_VOLATILE (destvar))
8967 tree src_base, dest_base, fn;
8968 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8969 HOST_WIDE_INT size = -1;
8970 HOST_WIDE_INT maxsize = -1;
8973 if (handled_component_p (src_base))
8974 src_base = get_ref_base_and_extent (src_base, &src_offset,
8976 dest_base = destvar;
8977 if (handled_component_p (dest_base))
8978 dest_base = get_ref_base_and_extent (dest_base, &dest_offset,
8980 if (host_integerp (len, 1))
8982 maxsize = tree_low_cst (len, 1);
8984 > INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT)
8987 maxsize *= BITS_PER_UNIT;
8991 if (SSA_VAR_P (src_base)
8992 && SSA_VAR_P (dest_base))
8994 if (operand_equal_p (src_base, dest_base, 0)
8995 && ranges_overlap_p (src_offset, maxsize,
8996 dest_offset, maxsize))
8999 else if (TREE_CODE (src_base) == INDIRECT_REF
9000 && TREE_CODE (dest_base) == INDIRECT_REF)
9002 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
9003 TREE_OPERAND (dest_base, 0), 0)
9004 || ranges_overlap_p (src_offset, maxsize,
9005 dest_offset, maxsize))
9011 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9014 return build_call_expr (fn, 3, dest, src, len);
9019 if (!host_integerp (len, 0))
9022 This logic lose for arguments like (type *)malloc (sizeof (type)),
9023 since we strip the casts of up to VOID return value from malloc.
9024 Perhaps we ought to inherit type from non-VOID argument here? */
9027 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
9028 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
9030 tree tem = TREE_OPERAND (src, 0);
9032 if (tem != TREE_OPERAND (src, 0))
9033 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
9035 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
9037 tree tem = TREE_OPERAND (dest, 0);
9039 if (tem != TREE_OPERAND (dest, 0))
9040 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
9042 srctype = TREE_TYPE (TREE_TYPE (src));
9044 && TREE_CODE (srctype) == ARRAY_TYPE
9045 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
9047 srctype = TREE_TYPE (srctype);
9049 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
9051 desttype = TREE_TYPE (TREE_TYPE (dest));
9053 && TREE_CODE (desttype) == ARRAY_TYPE
9054 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
9056 desttype = TREE_TYPE (desttype);
9058 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
9060 if (!srctype || !desttype
9061 || !TYPE_SIZE_UNIT (srctype)
9062 || !TYPE_SIZE_UNIT (desttype)
9063 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
9064 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
9065 || TYPE_VOLATILE (srctype)
9066 || TYPE_VOLATILE (desttype))
9069 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
9070 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
9071 if (dest_align < (int) TYPE_ALIGN (desttype)
9072 || src_align < (int) TYPE_ALIGN (srctype))
9076 dest = builtin_save_expr (dest);
9079 if (tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
9081 srcvar = build_fold_indirect_ref (src);
9082 if (TREE_THIS_VOLATILE (srcvar))
9084 else if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
9086 /* With memcpy, it is possible to bypass aliasing rules, so without
9087 this check i.e. execute/20060930-2.c would be misoptimized,
9088 because it use conflicting alias set to hold argument for the
9089 memcpy call. This check is probably unnecessary with
9090 -fno-strict-aliasing. Similarly for destvar. See also
9092 else if (!var_decl_component_p (srcvar))
9096 destvar = NULL_TREE;
9097 if (tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
9099 destvar = build_fold_indirect_ref (dest);
9100 if (TREE_THIS_VOLATILE (destvar))
9102 else if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
9103 destvar = NULL_TREE;
9104 else if (!var_decl_component_p (destvar))
9105 destvar = NULL_TREE;
9108 if (srcvar == NULL_TREE && destvar == NULL_TREE)
9111 if (srcvar == NULL_TREE)
9114 if (TREE_ADDRESSABLE (TREE_TYPE (destvar)))
9117 srctype = build_qualified_type (desttype, 0);
9118 if (src_align < (int) TYPE_ALIGN (srctype))
9120 if (AGGREGATE_TYPE_P (srctype)
9121 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (srctype), src_align))
9124 srctype = build_variant_type_copy (srctype);
9125 TYPE_ALIGN (srctype) = src_align;
9126 TYPE_USER_ALIGN (srctype) = 1;
9127 TYPE_PACKED (srctype) = 1;
9129 srcptype = build_pointer_type_for_mode (srctype, ptr_mode, true);
9130 src = fold_convert (srcptype, src);
9131 srcvar = build_fold_indirect_ref (src);
9133 else if (destvar == NULL_TREE)
9136 if (TREE_ADDRESSABLE (TREE_TYPE (srcvar)))
9139 desttype = build_qualified_type (srctype, 0);
9140 if (dest_align < (int) TYPE_ALIGN (desttype))
9142 if (AGGREGATE_TYPE_P (desttype)
9143 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (desttype), dest_align))
9146 desttype = build_variant_type_copy (desttype);
9147 TYPE_ALIGN (desttype) = dest_align;
9148 TYPE_USER_ALIGN (desttype) = 1;
9149 TYPE_PACKED (desttype) = 1;
9151 destptype = build_pointer_type_for_mode (desttype, ptr_mode, true);
9152 dest = fold_convert (destptype, dest);
9153 destvar = build_fold_indirect_ref (dest);
9156 if (srctype == desttype
9157 || (gimple_in_ssa_p (cfun)
9158 && useless_type_conversion_p (desttype, srctype)))
9160 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
9161 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
9162 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
9163 || POINTER_TYPE_P (TREE_TYPE (destvar))))
9164 expr = fold_convert (TREE_TYPE (destvar), srcvar);
9166 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
9167 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
9173 if (endp == 0 || endp == 3)
9174 return omit_one_operand (type, dest, expr);
9180 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
9183 len = fold_convert (sizetype, len);
9184 dest = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
9185 dest = fold_convert (type, dest);
9187 dest = omit_one_operand (type, dest, expr);
9191 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9192 If LEN is not NULL, it represents the length of the string to be
9193 copied. Return NULL_TREE if no simplification can be made. */
9196 fold_builtin_strcpy (tree fndecl, tree dest, tree src, tree len)
9200 if (!validate_arg (dest, POINTER_TYPE)
9201 || !validate_arg (src, POINTER_TYPE))
9204 /* If SRC and DEST are the same (and not volatile), return DEST. */
9205 if (operand_equal_p (src, dest, 0))
9206 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
9208 if (optimize_function_for_size_p (cfun))
9211 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9217 len = c_strlen (src, 1);
9218 if (! len || TREE_SIDE_EFFECTS (len))
9222 len = size_binop (PLUS_EXPR, len, ssize_int (1));
9223 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9224 build_call_expr (fn, 3, dest, src, len));
9227 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9228 If SLEN is not NULL, it represents the length of the source string.
9229 Return NULL_TREE if no simplification can be made. */
9232 fold_builtin_strncpy (tree fndecl, tree dest, tree src, tree len, tree slen)
9236 if (!validate_arg (dest, POINTER_TYPE)
9237 || !validate_arg (src, POINTER_TYPE)
9238 || !validate_arg (len, INTEGER_TYPE))
9241 /* If the LEN parameter is zero, return DEST. */
9242 if (integer_zerop (len))
9243 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9245 /* We can't compare slen with len as constants below if len is not a
9247 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9251 slen = c_strlen (src, 1);
9253 /* Now, we must be passed a constant src ptr parameter. */
9254 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9257 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
9259 /* We do not support simplification of this case, though we do
9260 support it when expanding trees into RTL. */
9261 /* FIXME: generate a call to __builtin_memset. */
9262 if (tree_int_cst_lt (slen, len))
9265 /* OK transform into builtin memcpy. */
9266 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9269 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9270 build_call_expr (fn, 3, dest, src, len));
9273 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9274 arguments to the call, and TYPE is its return type.
9275 Return NULL_TREE if no simplification can be made. */
9278 fold_builtin_memchr (tree arg1, tree arg2, tree len, tree type)
9280 if (!validate_arg (arg1, POINTER_TYPE)
9281 || !validate_arg (arg2, INTEGER_TYPE)
9282 || !validate_arg (len, INTEGER_TYPE))
9288 if (TREE_CODE (arg2) != INTEGER_CST
9289 || !host_integerp (len, 1))
9292 p1 = c_getstr (arg1);
9293 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9299 if (target_char_cast (arg2, &c))
9302 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
9305 return build_int_cst (TREE_TYPE (arg1), 0);
9307 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
9309 return fold_convert (type, tem);
9315 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9316 Return NULL_TREE if no simplification can be made. */
9319 fold_builtin_memcmp (tree arg1, tree arg2, tree len)
9321 const char *p1, *p2;
9323 if (!validate_arg (arg1, POINTER_TYPE)
9324 || !validate_arg (arg2, POINTER_TYPE)
9325 || !validate_arg (len, INTEGER_TYPE))
9328 /* If the LEN parameter is zero, return zero. */
9329 if (integer_zerop (len))
9330 return omit_two_operands (integer_type_node, integer_zero_node,
9333 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9334 if (operand_equal_p (arg1, arg2, 0))
9335 return omit_one_operand (integer_type_node, integer_zero_node, len);
9337 p1 = c_getstr (arg1);
9338 p2 = c_getstr (arg2);
9340 /* If all arguments are constant, and the value of len is not greater
9341 than the lengths of arg1 and arg2, evaluate at compile-time. */
9342 if (host_integerp (len, 1) && p1 && p2
9343 && compare_tree_int (len, strlen (p1) + 1) <= 0
9344 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9346 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9349 return integer_one_node;
9351 return integer_minus_one_node;
9353 return integer_zero_node;
9356 /* If len parameter is one, return an expression corresponding to
9357 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9358 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9360 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9361 tree cst_uchar_ptr_node
9362 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9364 tree ind1 = fold_convert (integer_type_node,
9365 build1 (INDIRECT_REF, cst_uchar_node,
9366 fold_convert (cst_uchar_ptr_node,
9368 tree ind2 = fold_convert (integer_type_node,
9369 build1 (INDIRECT_REF, cst_uchar_node,
9370 fold_convert (cst_uchar_ptr_node,
9372 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9378 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9379 Return NULL_TREE if no simplification can be made. */
9382 fold_builtin_strcmp (tree arg1, tree arg2)
9384 const char *p1, *p2;
9386 if (!validate_arg (arg1, POINTER_TYPE)
9387 || !validate_arg (arg2, POINTER_TYPE))
9390 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9391 if (operand_equal_p (arg1, arg2, 0))
9392 return integer_zero_node;
9394 p1 = c_getstr (arg1);
9395 p2 = c_getstr (arg2);
9399 const int i = strcmp (p1, p2);
9401 return integer_minus_one_node;
9403 return integer_one_node;
9405 return integer_zero_node;
9408 /* If the second arg is "", return *(const unsigned char*)arg1. */
9409 if (p2 && *p2 == '\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 return fold_convert (integer_type_node,
9416 build1 (INDIRECT_REF, cst_uchar_node,
9417 fold_convert (cst_uchar_ptr_node,
9421 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9422 if (p1 && *p1 == '\0')
9424 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9425 tree cst_uchar_ptr_node
9426 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9428 tree temp = fold_convert (integer_type_node,
9429 build1 (INDIRECT_REF, cst_uchar_node,
9430 fold_convert (cst_uchar_ptr_node,
9432 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9438 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9439 Return NULL_TREE if no simplification can be made. */
9442 fold_builtin_strncmp (tree arg1, tree arg2, tree len)
9444 const char *p1, *p2;
9446 if (!validate_arg (arg1, POINTER_TYPE)
9447 || !validate_arg (arg2, POINTER_TYPE)
9448 || !validate_arg (len, INTEGER_TYPE))
9451 /* If the LEN parameter is zero, return zero. */
9452 if (integer_zerop (len))
9453 return omit_two_operands (integer_type_node, integer_zero_node,
9456 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9457 if (operand_equal_p (arg1, arg2, 0))
9458 return omit_one_operand (integer_type_node, integer_zero_node, len);
9460 p1 = c_getstr (arg1);
9461 p2 = c_getstr (arg2);
9463 if (host_integerp (len, 1) && p1 && p2)
9465 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9467 return integer_one_node;
9469 return integer_minus_one_node;
9471 return integer_zero_node;
9474 /* If the second arg is "", and the length is greater than zero,
9475 return *(const unsigned char*)arg1. */
9476 if (p2 && *p2 == '\0'
9477 && TREE_CODE (len) == INTEGER_CST
9478 && tree_int_cst_sgn (len) == 1)
9480 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9481 tree cst_uchar_ptr_node
9482 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9484 return fold_convert (integer_type_node,
9485 build1 (INDIRECT_REF, cst_uchar_node,
9486 fold_convert (cst_uchar_ptr_node,
9490 /* If the first arg is "", and the length is greater than zero,
9491 return -*(const unsigned char*)arg2. */
9492 if (p1 && *p1 == '\0'
9493 && TREE_CODE (len) == INTEGER_CST
9494 && tree_int_cst_sgn (len) == 1)
9496 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9497 tree cst_uchar_ptr_node
9498 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9500 tree temp = fold_convert (integer_type_node,
9501 build1 (INDIRECT_REF, cst_uchar_node,
9502 fold_convert (cst_uchar_ptr_node,
9504 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9507 /* If len parameter is one, return an expression corresponding to
9508 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9509 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9511 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9512 tree cst_uchar_ptr_node
9513 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9515 tree ind1 = fold_convert (integer_type_node,
9516 build1 (INDIRECT_REF, cst_uchar_node,
9517 fold_convert (cst_uchar_ptr_node,
9519 tree ind2 = fold_convert (integer_type_node,
9520 build1 (INDIRECT_REF, cst_uchar_node,
9521 fold_convert (cst_uchar_ptr_node,
9523 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9529 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9530 ARG. Return NULL_TREE if no simplification can be made. */
9533 fold_builtin_signbit (tree arg, tree type)
9537 if (!validate_arg (arg, REAL_TYPE))
9540 /* If ARG is a compile-time constant, determine the result. */
9541 if (TREE_CODE (arg) == REAL_CST
9542 && !TREE_OVERFLOW (arg))
9546 c = TREE_REAL_CST (arg);
9547 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9548 return fold_convert (type, temp);
9551 /* If ARG is non-negative, the result is always zero. */
9552 if (tree_expr_nonnegative_p (arg))
9553 return omit_one_operand (type, integer_zero_node, arg);
9555 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9556 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9557 return fold_build2 (LT_EXPR, type, arg,
9558 build_real (TREE_TYPE (arg), dconst0));
9563 /* Fold function call to builtin copysign, copysignf or copysignl with
9564 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9568 fold_builtin_copysign (tree fndecl, tree arg1, tree arg2, tree type)
9572 if (!validate_arg (arg1, REAL_TYPE)
9573 || !validate_arg (arg2, REAL_TYPE))
9576 /* copysign(X,X) is X. */
9577 if (operand_equal_p (arg1, arg2, 0))
9578 return fold_convert (type, arg1);
9580 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9581 if (TREE_CODE (arg1) == REAL_CST
9582 && TREE_CODE (arg2) == REAL_CST
9583 && !TREE_OVERFLOW (arg1)
9584 && !TREE_OVERFLOW (arg2))
9586 REAL_VALUE_TYPE c1, c2;
9588 c1 = TREE_REAL_CST (arg1);
9589 c2 = TREE_REAL_CST (arg2);
9590 /* c1.sign := c2.sign. */
9591 real_copysign (&c1, &c2);
9592 return build_real (type, c1);
9595 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9596 Remember to evaluate Y for side-effects. */
9597 if (tree_expr_nonnegative_p (arg2))
9598 return omit_one_operand (type,
9599 fold_build1 (ABS_EXPR, type, arg1),
9602 /* Strip sign changing operations for the first argument. */
9603 tem = fold_strip_sign_ops (arg1);
9605 return build_call_expr (fndecl, 2, tem, arg2);
9610 /* Fold a call to builtin isascii with argument ARG. */
9613 fold_builtin_isascii (tree arg)
9615 if (!validate_arg (arg, INTEGER_TYPE))
9619 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9620 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
9621 build_int_cst (NULL_TREE,
9622 ~ (unsigned HOST_WIDE_INT) 0x7f));
9623 return fold_build2 (EQ_EXPR, integer_type_node,
9624 arg, integer_zero_node);
9628 /* Fold a call to builtin toascii with argument ARG. */
9631 fold_builtin_toascii (tree arg)
9633 if (!validate_arg (arg, INTEGER_TYPE))
9636 /* Transform toascii(c) -> (c & 0x7f). */
9637 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9638 build_int_cst (NULL_TREE, 0x7f));
9641 /* Fold a call to builtin isdigit with argument ARG. */
9644 fold_builtin_isdigit (tree arg)
9646 if (!validate_arg (arg, INTEGER_TYPE))
9650 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9651 /* According to the C standard, isdigit is unaffected by locale.
9652 However, it definitely is affected by the target character set. */
9653 unsigned HOST_WIDE_INT target_digit0
9654 = lang_hooks.to_target_charset ('0');
9656 if (target_digit0 == 0)
9659 arg = fold_convert (unsigned_type_node, arg);
9660 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
9661 build_int_cst (unsigned_type_node, target_digit0));
9662 return fold_build2 (LE_EXPR, integer_type_node, arg,
9663 build_int_cst (unsigned_type_node, 9));
9667 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9670 fold_builtin_fabs (tree arg, tree type)
9672 if (!validate_arg (arg, REAL_TYPE))
9675 arg = fold_convert (type, arg);
9676 if (TREE_CODE (arg) == REAL_CST)
9677 return fold_abs_const (arg, type);
9678 return fold_build1 (ABS_EXPR, type, arg);
9681 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9684 fold_builtin_abs (tree arg, tree type)
9686 if (!validate_arg (arg, INTEGER_TYPE))
9689 arg = fold_convert (type, arg);
9690 if (TREE_CODE (arg) == INTEGER_CST)
9691 return fold_abs_const (arg, type);
9692 return fold_build1 (ABS_EXPR, type, arg);
9695 /* Fold a call to builtin fmin or fmax. */
9698 fold_builtin_fmin_fmax (tree arg0, tree arg1, tree type, bool max)
9700 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9702 /* Calculate the result when the argument is a constant. */
9703 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9708 /* If either argument is NaN, return the other one. Avoid the
9709 transformation if we get (and honor) a signalling NaN. Using
9710 omit_one_operand() ensures we create a non-lvalue. */
9711 if (TREE_CODE (arg0) == REAL_CST
9712 && real_isnan (&TREE_REAL_CST (arg0))
9713 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9714 || ! TREE_REAL_CST (arg0).signalling))
9715 return omit_one_operand (type, arg1, arg0);
9716 if (TREE_CODE (arg1) == REAL_CST
9717 && real_isnan (&TREE_REAL_CST (arg1))
9718 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9719 || ! TREE_REAL_CST (arg1).signalling))
9720 return omit_one_operand (type, arg0, arg1);
9722 /* Transform fmin/fmax(x,x) -> x. */
9723 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9724 return omit_one_operand (type, arg0, arg1);
9726 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9727 functions to return the numeric arg if the other one is NaN.
9728 These tree codes don't honor that, so only transform if
9729 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9730 handled, so we don't have to worry about it either. */
9731 if (flag_finite_math_only)
9732 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9733 fold_convert (type, arg0),
9734 fold_convert (type, arg1));
9739 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9742 fold_builtin_carg (tree arg, tree type)
9744 if (validate_arg (arg, COMPLEX_TYPE)
9745 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9747 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9751 tree new_arg = builtin_save_expr (arg);
9752 tree r_arg = fold_build1 (REALPART_EXPR, type, new_arg);
9753 tree i_arg = fold_build1 (IMAGPART_EXPR, type, new_arg);
9754 return build_call_expr (atan2_fn, 2, i_arg, r_arg);
9761 /* Fold a call to builtin logb/ilogb. */
9764 fold_builtin_logb (tree arg, tree rettype)
9766 if (! validate_arg (arg, REAL_TYPE))
9771 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9773 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9779 /* If arg is Inf or NaN and we're logb, return it. */
9780 if (TREE_CODE (rettype) == REAL_TYPE)
9781 return fold_convert (rettype, arg);
9782 /* Fall through... */
9784 /* Zero may set errno and/or raise an exception for logb, also
9785 for ilogb we don't know FP_ILOGB0. */
9788 /* For normal numbers, proceed iff radix == 2. In GCC,
9789 normalized significands are in the range [0.5, 1.0). We
9790 want the exponent as if they were [1.0, 2.0) so get the
9791 exponent and subtract 1. */
9792 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9793 return fold_convert (rettype, build_int_cst (NULL_TREE,
9794 REAL_EXP (value)-1));
9802 /* Fold a call to builtin significand, if radix == 2. */
9805 fold_builtin_significand (tree arg, tree rettype)
9807 if (! validate_arg (arg, REAL_TYPE))
9812 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9814 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9821 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9822 return fold_convert (rettype, arg);
9824 /* For normal numbers, proceed iff radix == 2. */
9825 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9827 REAL_VALUE_TYPE result = *value;
9828 /* In GCC, normalized significands are in the range [0.5,
9829 1.0). We want them to be [1.0, 2.0) so set the
9831 SET_REAL_EXP (&result, 1);
9832 return build_real (rettype, result);
9841 /* Fold a call to builtin frexp, we can assume the base is 2. */
9844 fold_builtin_frexp (tree arg0, tree arg1, tree rettype)
9846 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9851 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9854 arg1 = build_fold_indirect_ref (arg1);
9856 /* Proceed if a valid pointer type was passed in. */
9857 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9859 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9865 /* For +-0, return (*exp = 0, +-0). */
9866 exp = integer_zero_node;
9871 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9872 return omit_one_operand (rettype, arg0, arg1);
9875 /* Since the frexp function always expects base 2, and in
9876 GCC normalized significands are already in the range
9877 [0.5, 1.0), we have exactly what frexp wants. */
9878 REAL_VALUE_TYPE frac_rvt = *value;
9879 SET_REAL_EXP (&frac_rvt, 0);
9880 frac = build_real (rettype, frac_rvt);
9881 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9888 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9889 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, exp);
9890 TREE_SIDE_EFFECTS (arg1) = 1;
9891 return fold_build2 (COMPOUND_EXPR, rettype, arg1, frac);
9897 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9898 then we can assume the base is two. If it's false, then we have to
9899 check the mode of the TYPE parameter in certain cases. */
9902 fold_builtin_load_exponent (tree arg0, tree arg1, tree type, bool ldexp)
9904 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9909 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9910 if (real_zerop (arg0) || integer_zerop (arg1)
9911 || (TREE_CODE (arg0) == REAL_CST
9912 && !real_isfinite (&TREE_REAL_CST (arg0))))
9913 return omit_one_operand (type, arg0, arg1);
9915 /* If both arguments are constant, then try to evaluate it. */
9916 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9917 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9918 && host_integerp (arg1, 0))
9920 /* Bound the maximum adjustment to twice the range of the
9921 mode's valid exponents. Use abs to ensure the range is
9922 positive as a sanity check. */
9923 const long max_exp_adj = 2 *
9924 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9925 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9927 /* Get the user-requested adjustment. */
9928 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9930 /* The requested adjustment must be inside this range. This
9931 is a preliminary cap to avoid things like overflow, we
9932 may still fail to compute the result for other reasons. */
9933 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9935 REAL_VALUE_TYPE initial_result;
9937 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9939 /* Ensure we didn't overflow. */
9940 if (! real_isinf (&initial_result))
9942 const REAL_VALUE_TYPE trunc_result
9943 = real_value_truncate (TYPE_MODE (type), initial_result);
9945 /* Only proceed if the target mode can hold the
9947 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9948 return build_real (type, trunc_result);
9957 /* Fold a call to builtin modf. */
9960 fold_builtin_modf (tree arg0, tree arg1, tree rettype)
9962 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9967 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9970 arg1 = build_fold_indirect_ref (arg1);
9972 /* Proceed if a valid pointer type was passed in. */
9973 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9975 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9976 REAL_VALUE_TYPE trunc, frac;
9982 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9983 trunc = frac = *value;
9986 /* For +-Inf, return (*arg1 = arg0, +-0). */
9988 frac.sign = value->sign;
9992 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9993 real_trunc (&trunc, VOIDmode, value);
9994 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9995 /* If the original number was negative and already
9996 integral, then the fractional part is -0.0. */
9997 if (value->sign && frac.cl == rvc_zero)
9998 frac.sign = value->sign;
10002 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
10003 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1,
10004 build_real (rettype, trunc));
10005 TREE_SIDE_EFFECTS (arg1) = 1;
10006 return fold_build2 (COMPOUND_EXPR, rettype, arg1,
10007 build_real (rettype, frac));
10013 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10014 ARG is the argument for the call. */
10017 fold_builtin_classify (tree fndecl, tree arg, int builtin_index)
10019 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10022 if (!validate_arg (arg, REAL_TYPE))
10025 switch (builtin_index)
10027 case BUILT_IN_ISINF:
10028 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10029 return omit_one_operand (type, integer_zero_node, arg);
10031 if (TREE_CODE (arg) == REAL_CST)
10033 r = TREE_REAL_CST (arg);
10034 if (real_isinf (&r))
10035 return real_compare (GT_EXPR, &r, &dconst0)
10036 ? integer_one_node : integer_minus_one_node;
10038 return integer_zero_node;
10043 case BUILT_IN_ISINF_SIGN:
10045 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10046 /* In a boolean context, GCC will fold the inner COND_EXPR to
10047 1. So e.g. "if (isinf_sign(x))" would be folded to just
10048 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10049 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10050 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
10051 tree tmp = NULL_TREE;
10053 arg = builtin_save_expr (arg);
10055 if (signbit_fn && isinf_fn)
10057 tree signbit_call = build_call_expr (signbit_fn, 1, arg);
10058 tree isinf_call = build_call_expr (isinf_fn, 1, arg);
10060 signbit_call = fold_build2 (NE_EXPR, integer_type_node,
10061 signbit_call, integer_zero_node);
10062 isinf_call = fold_build2 (NE_EXPR, integer_type_node,
10063 isinf_call, integer_zero_node);
10065 tmp = fold_build3 (COND_EXPR, integer_type_node, signbit_call,
10066 integer_minus_one_node, integer_one_node);
10067 tmp = fold_build3 (COND_EXPR, integer_type_node, isinf_call, tmp,
10068 integer_zero_node);
10074 case BUILT_IN_ISFINITE:
10075 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10076 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10077 return omit_one_operand (type, integer_one_node, arg);
10079 if (TREE_CODE (arg) == REAL_CST)
10081 r = TREE_REAL_CST (arg);
10082 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10087 case BUILT_IN_ISNAN:
10088 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10089 return omit_one_operand (type, integer_zero_node, arg);
10091 if (TREE_CODE (arg) == REAL_CST)
10093 r = TREE_REAL_CST (arg);
10094 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10097 arg = builtin_save_expr (arg);
10098 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
10101 gcc_unreachable ();
10105 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10106 This builtin will generate code to return the appropriate floating
10107 point classification depending on the value of the floating point
10108 number passed in. The possible return values must be supplied as
10109 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10110 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10111 one floating point argument which is "type generic". */
10114 fold_builtin_fpclassify (tree exp)
10116 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10117 arg, type, res, tmp;
10118 enum machine_mode mode;
10122 /* Verify the required arguments in the original call. */
10123 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10124 INTEGER_TYPE, INTEGER_TYPE,
10125 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10128 fp_nan = CALL_EXPR_ARG (exp, 0);
10129 fp_infinite = CALL_EXPR_ARG (exp, 1);
10130 fp_normal = CALL_EXPR_ARG (exp, 2);
10131 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10132 fp_zero = CALL_EXPR_ARG (exp, 4);
10133 arg = CALL_EXPR_ARG (exp, 5);
10134 type = TREE_TYPE (arg);
10135 mode = TYPE_MODE (type);
10136 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
10138 /* fpclassify(x) ->
10139 isnan(x) ? FP_NAN :
10140 (fabs(x) == Inf ? FP_INFINITE :
10141 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10142 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10144 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
10145 build_real (type, dconst0));
10146 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_zero, fp_subnormal);
10148 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10149 real_from_string (&r, buf);
10150 tmp = fold_build2 (GE_EXPR, integer_type_node, arg, build_real (type, r));
10151 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_normal, res);
10153 if (HONOR_INFINITIES (mode))
10156 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
10157 build_real (type, r));
10158 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_infinite, res);
10161 if (HONOR_NANS (mode))
10163 tmp = fold_build2 (ORDERED_EXPR, integer_type_node, arg, arg);
10164 res = fold_build3 (COND_EXPR, integer_type_node, tmp, res, fp_nan);
10170 /* Fold a call to an unordered comparison function such as
10171 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10172 being called and ARG0 and ARG1 are the arguments for the call.
10173 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10174 the opposite of the desired result. UNORDERED_CODE is used
10175 for modes that can hold NaNs and ORDERED_CODE is used for
10179 fold_builtin_unordered_cmp (tree fndecl, tree arg0, tree arg1,
10180 enum tree_code unordered_code,
10181 enum tree_code ordered_code)
10183 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10184 enum tree_code code;
10186 enum tree_code code0, code1;
10187 tree cmp_type = NULL_TREE;
10189 type0 = TREE_TYPE (arg0);
10190 type1 = TREE_TYPE (arg1);
10192 code0 = TREE_CODE (type0);
10193 code1 = TREE_CODE (type1);
10195 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10196 /* Choose the wider of two real types. */
10197 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10199 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10201 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10204 arg0 = fold_convert (cmp_type, arg0);
10205 arg1 = fold_convert (cmp_type, arg1);
10207 if (unordered_code == UNORDERED_EXPR)
10209 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10210 return omit_two_operands (type, integer_zero_node, arg0, arg1);
10211 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
10214 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10216 return fold_build1 (TRUTH_NOT_EXPR, type,
10217 fold_build2 (code, type, arg0, arg1));
10220 /* Fold a call to built-in function FNDECL with 0 arguments.
10221 IGNORE is true if the result of the function call is ignored. This
10222 function returns NULL_TREE if no simplification was possible. */
10225 fold_builtin_0 (tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10227 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10228 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10231 CASE_FLT_FN (BUILT_IN_INF):
10232 case BUILT_IN_INFD32:
10233 case BUILT_IN_INFD64:
10234 case BUILT_IN_INFD128:
10235 return fold_builtin_inf (type, true);
10237 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10238 return fold_builtin_inf (type, false);
10240 case BUILT_IN_CLASSIFY_TYPE:
10241 return fold_builtin_classify_type (NULL_TREE);
10249 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10250 IGNORE is true if the result of the function call is ignored. This
10251 function returns NULL_TREE if no simplification was possible. */
10254 fold_builtin_1 (tree fndecl, tree arg0, bool ignore)
10256 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10257 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10261 case BUILT_IN_CONSTANT_P:
10263 tree val = fold_builtin_constant_p (arg0);
10265 /* Gimplification will pull the CALL_EXPR for the builtin out of
10266 an if condition. When not optimizing, we'll not CSE it back.
10267 To avoid link error types of regressions, return false now. */
10268 if (!val && !optimize)
10269 val = integer_zero_node;
10274 case BUILT_IN_CLASSIFY_TYPE:
10275 return fold_builtin_classify_type (arg0);
10277 case BUILT_IN_STRLEN:
10278 return fold_builtin_strlen (arg0);
10280 CASE_FLT_FN (BUILT_IN_FABS):
10281 return fold_builtin_fabs (arg0, type);
10284 case BUILT_IN_LABS:
10285 case BUILT_IN_LLABS:
10286 case BUILT_IN_IMAXABS:
10287 return fold_builtin_abs (arg0, type);
10289 CASE_FLT_FN (BUILT_IN_CONJ):
10290 if (validate_arg (arg0, COMPLEX_TYPE)
10291 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10292 return fold_build1 (CONJ_EXPR, type, arg0);
10295 CASE_FLT_FN (BUILT_IN_CREAL):
10296 if (validate_arg (arg0, COMPLEX_TYPE)
10297 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10298 return non_lvalue (fold_build1 (REALPART_EXPR, type, arg0));;
10301 CASE_FLT_FN (BUILT_IN_CIMAG):
10302 if (validate_arg (arg0, COMPLEX_TYPE))
10303 return non_lvalue (fold_build1 (IMAGPART_EXPR, type, arg0));
10306 CASE_FLT_FN (BUILT_IN_CCOS):
10307 return fold_builtin_ccos(arg0, type, fndecl, /*hyper=*/ false);
10309 CASE_FLT_FN (BUILT_IN_CCOSH):
10310 return fold_builtin_ccos(arg0, type, fndecl, /*hyper=*/ true);
10313 CASE_FLT_FN (BUILT_IN_CSIN):
10314 if (validate_arg (arg0, COMPLEX_TYPE)
10315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10316 return do_mpc_arg1 (arg0, type, mpc_sin);
10319 CASE_FLT_FN (BUILT_IN_CSINH):
10320 if (validate_arg (arg0, COMPLEX_TYPE)
10321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10322 return do_mpc_arg1 (arg0, type, mpc_sinh);
10325 CASE_FLT_FN (BUILT_IN_CTAN):
10326 if (validate_arg (arg0, COMPLEX_TYPE)
10327 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10328 return do_mpc_arg1 (arg0, type, mpc_tan);
10331 CASE_FLT_FN (BUILT_IN_CTANH):
10332 if (validate_arg (arg0, COMPLEX_TYPE)
10333 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10334 return do_mpc_arg1 (arg0, type, mpc_tanh);
10337 CASE_FLT_FN (BUILT_IN_CLOG):
10338 if (validate_arg (arg0, COMPLEX_TYPE)
10339 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10340 return do_mpc_arg1 (arg0, type, mpc_log);
10343 CASE_FLT_FN (BUILT_IN_CSQRT):
10344 if (validate_arg (arg0, COMPLEX_TYPE)
10345 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10346 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10350 CASE_FLT_FN (BUILT_IN_CABS):
10351 return fold_builtin_cabs (arg0, type, fndecl);
10353 CASE_FLT_FN (BUILT_IN_CARG):
10354 return fold_builtin_carg (arg0, type);
10356 CASE_FLT_FN (BUILT_IN_SQRT):
10357 return fold_builtin_sqrt (arg0, type);
10359 CASE_FLT_FN (BUILT_IN_CBRT):
10360 return fold_builtin_cbrt (arg0, type);
10362 CASE_FLT_FN (BUILT_IN_ASIN):
10363 if (validate_arg (arg0, REAL_TYPE))
10364 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10365 &dconstm1, &dconst1, true);
10368 CASE_FLT_FN (BUILT_IN_ACOS):
10369 if (validate_arg (arg0, REAL_TYPE))
10370 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10371 &dconstm1, &dconst1, true);
10374 CASE_FLT_FN (BUILT_IN_ATAN):
10375 if (validate_arg (arg0, REAL_TYPE))
10376 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10379 CASE_FLT_FN (BUILT_IN_ASINH):
10380 if (validate_arg (arg0, REAL_TYPE))
10381 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10384 CASE_FLT_FN (BUILT_IN_ACOSH):
10385 if (validate_arg (arg0, REAL_TYPE))
10386 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10387 &dconst1, NULL, true);
10390 CASE_FLT_FN (BUILT_IN_ATANH):
10391 if (validate_arg (arg0, REAL_TYPE))
10392 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10393 &dconstm1, &dconst1, false);
10396 CASE_FLT_FN (BUILT_IN_SIN):
10397 if (validate_arg (arg0, REAL_TYPE))
10398 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10401 CASE_FLT_FN (BUILT_IN_COS):
10402 return fold_builtin_cos (arg0, type, fndecl);
10404 CASE_FLT_FN (BUILT_IN_TAN):
10405 return fold_builtin_tan (arg0, type);
10407 CASE_FLT_FN (BUILT_IN_CEXP):
10408 return fold_builtin_cexp (arg0, type);
10410 CASE_FLT_FN (BUILT_IN_CEXPI):
10411 if (validate_arg (arg0, REAL_TYPE))
10412 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10415 CASE_FLT_FN (BUILT_IN_SINH):
10416 if (validate_arg (arg0, REAL_TYPE))
10417 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10420 CASE_FLT_FN (BUILT_IN_COSH):
10421 return fold_builtin_cosh (arg0, type, fndecl);
10423 CASE_FLT_FN (BUILT_IN_TANH):
10424 if (validate_arg (arg0, REAL_TYPE))
10425 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10428 CASE_FLT_FN (BUILT_IN_ERF):
10429 if (validate_arg (arg0, REAL_TYPE))
10430 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10433 CASE_FLT_FN (BUILT_IN_ERFC):
10434 if (validate_arg (arg0, REAL_TYPE))
10435 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10438 CASE_FLT_FN (BUILT_IN_TGAMMA):
10439 if (validate_arg (arg0, REAL_TYPE))
10440 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10443 CASE_FLT_FN (BUILT_IN_EXP):
10444 return fold_builtin_exponent (fndecl, arg0, mpfr_exp);
10446 CASE_FLT_FN (BUILT_IN_EXP2):
10447 return fold_builtin_exponent (fndecl, arg0, mpfr_exp2);
10449 CASE_FLT_FN (BUILT_IN_EXP10):
10450 CASE_FLT_FN (BUILT_IN_POW10):
10451 return fold_builtin_exponent (fndecl, arg0, mpfr_exp10);
10453 CASE_FLT_FN (BUILT_IN_EXPM1):
10454 if (validate_arg (arg0, REAL_TYPE))
10455 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10458 CASE_FLT_FN (BUILT_IN_LOG):
10459 return fold_builtin_logarithm (fndecl, arg0, mpfr_log);
10461 CASE_FLT_FN (BUILT_IN_LOG2):
10462 return fold_builtin_logarithm (fndecl, arg0, mpfr_log2);
10464 CASE_FLT_FN (BUILT_IN_LOG10):
10465 return fold_builtin_logarithm (fndecl, arg0, mpfr_log10);
10467 CASE_FLT_FN (BUILT_IN_LOG1P):
10468 if (validate_arg (arg0, REAL_TYPE))
10469 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10470 &dconstm1, NULL, false);
10473 CASE_FLT_FN (BUILT_IN_J0):
10474 if (validate_arg (arg0, REAL_TYPE))
10475 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10479 CASE_FLT_FN (BUILT_IN_J1):
10480 if (validate_arg (arg0, REAL_TYPE))
10481 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10485 CASE_FLT_FN (BUILT_IN_Y0):
10486 if (validate_arg (arg0, REAL_TYPE))
10487 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10488 &dconst0, NULL, false);
10491 CASE_FLT_FN (BUILT_IN_Y1):
10492 if (validate_arg (arg0, REAL_TYPE))
10493 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10494 &dconst0, NULL, false);
10497 CASE_FLT_FN (BUILT_IN_NAN):
10498 case BUILT_IN_NAND32:
10499 case BUILT_IN_NAND64:
10500 case BUILT_IN_NAND128:
10501 return fold_builtin_nan (arg0, type, true);
10503 CASE_FLT_FN (BUILT_IN_NANS):
10504 return fold_builtin_nan (arg0, type, false);
10506 CASE_FLT_FN (BUILT_IN_FLOOR):
10507 return fold_builtin_floor (fndecl, arg0);
10509 CASE_FLT_FN (BUILT_IN_CEIL):
10510 return fold_builtin_ceil (fndecl, arg0);
10512 CASE_FLT_FN (BUILT_IN_TRUNC):
10513 return fold_builtin_trunc (fndecl, arg0);
10515 CASE_FLT_FN (BUILT_IN_ROUND):
10516 return fold_builtin_round (fndecl, arg0);
10518 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10519 CASE_FLT_FN (BUILT_IN_RINT):
10520 return fold_trunc_transparent_mathfn (fndecl, arg0);
10522 CASE_FLT_FN (BUILT_IN_LCEIL):
10523 CASE_FLT_FN (BUILT_IN_LLCEIL):
10524 CASE_FLT_FN (BUILT_IN_LFLOOR):
10525 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10526 CASE_FLT_FN (BUILT_IN_LROUND):
10527 CASE_FLT_FN (BUILT_IN_LLROUND):
10528 return fold_builtin_int_roundingfn (fndecl, arg0);
10530 CASE_FLT_FN (BUILT_IN_LRINT):
10531 CASE_FLT_FN (BUILT_IN_LLRINT):
10532 return fold_fixed_mathfn (fndecl, arg0);
10534 case BUILT_IN_BSWAP32:
10535 case BUILT_IN_BSWAP64:
10536 return fold_builtin_bswap (fndecl, arg0);
10538 CASE_INT_FN (BUILT_IN_FFS):
10539 CASE_INT_FN (BUILT_IN_CLZ):
10540 CASE_INT_FN (BUILT_IN_CTZ):
10541 CASE_INT_FN (BUILT_IN_POPCOUNT):
10542 CASE_INT_FN (BUILT_IN_PARITY):
10543 return fold_builtin_bitop (fndecl, arg0);
10545 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10546 return fold_builtin_signbit (arg0, type);
10548 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10549 return fold_builtin_significand (arg0, type);
10551 CASE_FLT_FN (BUILT_IN_ILOGB):
10552 CASE_FLT_FN (BUILT_IN_LOGB):
10553 return fold_builtin_logb (arg0, type);
10555 case BUILT_IN_ISASCII:
10556 return fold_builtin_isascii (arg0);
10558 case BUILT_IN_TOASCII:
10559 return fold_builtin_toascii (arg0);
10561 case BUILT_IN_ISDIGIT:
10562 return fold_builtin_isdigit (arg0);
10564 CASE_FLT_FN (BUILT_IN_FINITE):
10565 case BUILT_IN_FINITED32:
10566 case BUILT_IN_FINITED64:
10567 case BUILT_IN_FINITED128:
10568 case BUILT_IN_ISFINITE:
10569 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISFINITE);
10571 CASE_FLT_FN (BUILT_IN_ISINF):
10572 case BUILT_IN_ISINFD32:
10573 case BUILT_IN_ISINFD64:
10574 case BUILT_IN_ISINFD128:
10575 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF);
10577 case BUILT_IN_ISINF_SIGN:
10578 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF_SIGN);
10580 CASE_FLT_FN (BUILT_IN_ISNAN):
10581 case BUILT_IN_ISNAND32:
10582 case BUILT_IN_ISNAND64:
10583 case BUILT_IN_ISNAND128:
10584 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISNAN);
10586 case BUILT_IN_PRINTF:
10587 case BUILT_IN_PRINTF_UNLOCKED:
10588 case BUILT_IN_VPRINTF:
10589 return fold_builtin_printf (fndecl, arg0, NULL_TREE, ignore, fcode);
10599 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10600 IGNORE is true if the result of the function call is ignored. This
10601 function returns NULL_TREE if no simplification was possible. */
10604 fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore)
10606 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10607 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10611 CASE_FLT_FN (BUILT_IN_JN):
10612 if (validate_arg (arg0, INTEGER_TYPE)
10613 && validate_arg (arg1, REAL_TYPE))
10614 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10617 CASE_FLT_FN (BUILT_IN_YN):
10618 if (validate_arg (arg0, INTEGER_TYPE)
10619 && validate_arg (arg1, REAL_TYPE))
10620 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10624 CASE_FLT_FN (BUILT_IN_DREM):
10625 CASE_FLT_FN (BUILT_IN_REMAINDER):
10626 if (validate_arg (arg0, REAL_TYPE)
10627 && validate_arg(arg1, REAL_TYPE))
10628 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10631 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10632 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10633 if (validate_arg (arg0, REAL_TYPE)
10634 && validate_arg(arg1, POINTER_TYPE))
10635 return do_mpfr_lgamma_r (arg0, arg1, type);
10638 CASE_FLT_FN (BUILT_IN_ATAN2):
10639 if (validate_arg (arg0, REAL_TYPE)
10640 && validate_arg(arg1, REAL_TYPE))
10641 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10644 CASE_FLT_FN (BUILT_IN_FDIM):
10645 if (validate_arg (arg0, REAL_TYPE)
10646 && validate_arg(arg1, REAL_TYPE))
10647 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10650 CASE_FLT_FN (BUILT_IN_HYPOT):
10651 return fold_builtin_hypot (fndecl, arg0, arg1, type);
10653 #ifdef HAVE_mpc_pow
10654 CASE_FLT_FN (BUILT_IN_CPOW):
10655 if (validate_arg (arg0, COMPLEX_TYPE)
10656 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10657 && validate_arg (arg1, COMPLEX_TYPE)
10658 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10659 return do_mpc_arg2 (arg0, arg1, type, mpc_pow);
10663 CASE_FLT_FN (BUILT_IN_LDEXP):
10664 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/true);
10665 CASE_FLT_FN (BUILT_IN_SCALBN):
10666 CASE_FLT_FN (BUILT_IN_SCALBLN):
10667 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/false);
10669 CASE_FLT_FN (BUILT_IN_FREXP):
10670 return fold_builtin_frexp (arg0, arg1, type);
10672 CASE_FLT_FN (BUILT_IN_MODF):
10673 return fold_builtin_modf (arg0, arg1, type);
10675 case BUILT_IN_BZERO:
10676 return fold_builtin_bzero (arg0, arg1, ignore);
10678 case BUILT_IN_FPUTS:
10679 return fold_builtin_fputs (arg0, arg1, ignore, false, NULL_TREE);
10681 case BUILT_IN_FPUTS_UNLOCKED:
10682 return fold_builtin_fputs (arg0, arg1, ignore, true, NULL_TREE);
10684 case BUILT_IN_STRSTR:
10685 return fold_builtin_strstr (arg0, arg1, type);
10687 case BUILT_IN_STRCAT:
10688 return fold_builtin_strcat (arg0, arg1);
10690 case BUILT_IN_STRSPN:
10691 return fold_builtin_strspn (arg0, arg1);
10693 case BUILT_IN_STRCSPN:
10694 return fold_builtin_strcspn (arg0, arg1);
10696 case BUILT_IN_STRCHR:
10697 case BUILT_IN_INDEX:
10698 return fold_builtin_strchr (arg0, arg1, type);
10700 case BUILT_IN_STRRCHR:
10701 case BUILT_IN_RINDEX:
10702 return fold_builtin_strrchr (arg0, arg1, type);
10704 case BUILT_IN_STRCPY:
10705 return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE);
10707 case BUILT_IN_STPCPY:
10710 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10714 return build_call_expr (fn, 2, arg0, arg1);
10718 case BUILT_IN_STRCMP:
10719 return fold_builtin_strcmp (arg0, arg1);
10721 case BUILT_IN_STRPBRK:
10722 return fold_builtin_strpbrk (arg0, arg1, type);
10724 case BUILT_IN_EXPECT:
10725 return fold_builtin_expect (arg0, arg1);
10727 CASE_FLT_FN (BUILT_IN_POW):
10728 return fold_builtin_pow (fndecl, arg0, arg1, type);
10730 CASE_FLT_FN (BUILT_IN_POWI):
10731 return fold_builtin_powi (fndecl, arg0, arg1, type);
10733 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10734 return fold_builtin_copysign (fndecl, arg0, arg1, type);
10736 CASE_FLT_FN (BUILT_IN_FMIN):
10737 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/false);
10739 CASE_FLT_FN (BUILT_IN_FMAX):
10740 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/true);
10742 case BUILT_IN_ISGREATER:
10743 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLE_EXPR, LE_EXPR);
10744 case BUILT_IN_ISGREATEREQUAL:
10745 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLT_EXPR, LT_EXPR);
10746 case BUILT_IN_ISLESS:
10747 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGE_EXPR, GE_EXPR);
10748 case BUILT_IN_ISLESSEQUAL:
10749 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGT_EXPR, GT_EXPR);
10750 case BUILT_IN_ISLESSGREATER:
10751 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10752 case BUILT_IN_ISUNORDERED:
10753 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNORDERED_EXPR,
10756 /* We do the folding for va_start in the expander. */
10757 case BUILT_IN_VA_START:
10760 case BUILT_IN_SPRINTF:
10761 return fold_builtin_sprintf (arg0, arg1, NULL_TREE, ignore);
10763 case BUILT_IN_OBJECT_SIZE:
10764 return fold_builtin_object_size (arg0, arg1);
10766 case BUILT_IN_PRINTF:
10767 case BUILT_IN_PRINTF_UNLOCKED:
10768 case BUILT_IN_VPRINTF:
10769 return fold_builtin_printf (fndecl, arg0, arg1, ignore, fcode);
10771 case BUILT_IN_PRINTF_CHK:
10772 case BUILT_IN_VPRINTF_CHK:
10773 if (!validate_arg (arg0, INTEGER_TYPE)
10774 || TREE_SIDE_EFFECTS (arg0))
10777 return fold_builtin_printf (fndecl, arg1, NULL_TREE, ignore, fcode);
10780 case BUILT_IN_FPRINTF:
10781 case BUILT_IN_FPRINTF_UNLOCKED:
10782 case BUILT_IN_VFPRINTF:
10783 return fold_builtin_fprintf (fndecl, arg0, arg1, NULL_TREE,
10792 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10793 and ARG2. IGNORE is true if the result of the function call is ignored.
10794 This function returns NULL_TREE if no simplification was possible. */
10797 fold_builtin_3 (tree fndecl, tree arg0, tree arg1, tree arg2, bool ignore)
10799 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10800 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10804 CASE_FLT_FN (BUILT_IN_SINCOS):
10805 return fold_builtin_sincos (arg0, arg1, arg2);
10807 CASE_FLT_FN (BUILT_IN_FMA):
10808 if (validate_arg (arg0, REAL_TYPE)
10809 && validate_arg(arg1, REAL_TYPE)
10810 && validate_arg(arg2, REAL_TYPE))
10811 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10814 CASE_FLT_FN (BUILT_IN_REMQUO):
10815 if (validate_arg (arg0, REAL_TYPE)
10816 && validate_arg(arg1, REAL_TYPE)
10817 && validate_arg(arg2, POINTER_TYPE))
10818 return do_mpfr_remquo (arg0, arg1, arg2);
10821 case BUILT_IN_MEMSET:
10822 return fold_builtin_memset (arg0, arg1, arg2, type, ignore);
10824 case BUILT_IN_BCOPY:
10825 return fold_builtin_memory_op (arg1, arg0, arg2, void_type_node, true, /*endp=*/3);
10827 case BUILT_IN_MEMCPY:
10828 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/0);
10830 case BUILT_IN_MEMPCPY:
10831 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/1);
10833 case BUILT_IN_MEMMOVE:
10834 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/3);
10836 case BUILT_IN_STRNCAT:
10837 return fold_builtin_strncat (arg0, arg1, arg2);
10839 case BUILT_IN_STRNCPY:
10840 return fold_builtin_strncpy (fndecl, arg0, arg1, arg2, NULL_TREE);
10842 case BUILT_IN_STRNCMP:
10843 return fold_builtin_strncmp (arg0, arg1, arg2);
10845 case BUILT_IN_MEMCHR:
10846 return fold_builtin_memchr (arg0, arg1, arg2, type);
10848 case BUILT_IN_BCMP:
10849 case BUILT_IN_MEMCMP:
10850 return fold_builtin_memcmp (arg0, arg1, arg2);;
10852 case BUILT_IN_SPRINTF:
10853 return fold_builtin_sprintf (arg0, arg1, arg2, ignore);
10855 case BUILT_IN_STRCPY_CHK:
10856 case BUILT_IN_STPCPY_CHK:
10857 return fold_builtin_stxcpy_chk (fndecl, arg0, arg1, arg2, NULL_TREE,
10860 case BUILT_IN_STRCAT_CHK:
10861 return fold_builtin_strcat_chk (fndecl, arg0, arg1, arg2);
10863 case BUILT_IN_PRINTF_CHK:
10864 case BUILT_IN_VPRINTF_CHK:
10865 if (!validate_arg (arg0, INTEGER_TYPE)
10866 || TREE_SIDE_EFFECTS (arg0))
10869 return fold_builtin_printf (fndecl, arg1, arg2, ignore, fcode);
10872 case BUILT_IN_FPRINTF:
10873 case BUILT_IN_FPRINTF_UNLOCKED:
10874 case BUILT_IN_VFPRINTF:
10875 return fold_builtin_fprintf (fndecl, arg0, arg1, arg2, ignore, fcode);
10877 case BUILT_IN_FPRINTF_CHK:
10878 case BUILT_IN_VFPRINTF_CHK:
10879 if (!validate_arg (arg1, INTEGER_TYPE)
10880 || TREE_SIDE_EFFECTS (arg1))
10883 return fold_builtin_fprintf (fndecl, arg0, arg2, NULL_TREE,
10892 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10893 ARG2, and ARG3. IGNORE is true if the result of the function call is
10894 ignored. This function returns NULL_TREE if no simplification was
10898 fold_builtin_4 (tree fndecl, tree arg0, tree arg1, tree arg2, tree arg3,
10901 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10905 case BUILT_IN_MEMCPY_CHK:
10906 case BUILT_IN_MEMPCPY_CHK:
10907 case BUILT_IN_MEMMOVE_CHK:
10908 case BUILT_IN_MEMSET_CHK:
10909 return fold_builtin_memory_chk (fndecl, arg0, arg1, arg2, arg3,
10911 DECL_FUNCTION_CODE (fndecl));
10913 case BUILT_IN_STRNCPY_CHK:
10914 return fold_builtin_strncpy_chk (arg0, arg1, arg2, arg3, NULL_TREE);
10916 case BUILT_IN_STRNCAT_CHK:
10917 return fold_builtin_strncat_chk (fndecl, arg0, arg1, arg2, arg3);
10919 case BUILT_IN_FPRINTF_CHK:
10920 case BUILT_IN_VFPRINTF_CHK:
10921 if (!validate_arg (arg1, INTEGER_TYPE)
10922 || TREE_SIDE_EFFECTS (arg1))
10925 return fold_builtin_fprintf (fndecl, arg0, arg2, arg3,
10935 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10936 arguments, where NARGS <= 4. IGNORE is true if the result of the
10937 function call is ignored. This function returns NULL_TREE if no
10938 simplification was possible. Note that this only folds builtins with
10939 fixed argument patterns. Foldings that do varargs-to-varargs
10940 transformations, or that match calls with more than 4 arguments,
10941 need to be handled with fold_builtin_varargs instead. */
10943 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10946 fold_builtin_n (tree fndecl, tree *args, int nargs, bool ignore)
10948 tree ret = NULL_TREE;
10953 ret = fold_builtin_0 (fndecl, ignore);
10956 ret = fold_builtin_1 (fndecl, args[0], ignore);
10959 ret = fold_builtin_2 (fndecl, args[0], args[1], ignore);
10962 ret = fold_builtin_3 (fndecl, args[0], args[1], args[2], ignore);
10965 ret = fold_builtin_4 (fndecl, args[0], args[1], args[2], args[3],
10973 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10974 TREE_NO_WARNING (ret) = 1;
10980 /* Builtins with folding operations that operate on "..." arguments
10981 need special handling; we need to store the arguments in a convenient
10982 data structure before attempting any folding. Fortunately there are
10983 only a few builtins that fall into this category. FNDECL is the
10984 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10985 result of the function call is ignored. */
10988 fold_builtin_varargs (tree fndecl, tree exp, bool ignore ATTRIBUTE_UNUSED)
10990 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10991 tree ret = NULL_TREE;
10995 case BUILT_IN_SPRINTF_CHK:
10996 case BUILT_IN_VSPRINTF_CHK:
10997 ret = fold_builtin_sprintf_chk (exp, fcode);
11000 case BUILT_IN_SNPRINTF_CHK:
11001 case BUILT_IN_VSNPRINTF_CHK:
11002 ret = fold_builtin_snprintf_chk (exp, NULL_TREE, fcode);
11005 case BUILT_IN_FPCLASSIFY:
11006 ret = fold_builtin_fpclassify (exp);
11014 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11015 TREE_NO_WARNING (ret) = 1;
11021 /* Return true if FNDECL shouldn't be folded right now.
11022 If a built-in function has an inline attribute always_inline
11023 wrapper, defer folding it after always_inline functions have
11024 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11025 might not be performed. */
11028 avoid_folding_inline_builtin (tree fndecl)
11030 return (DECL_DECLARED_INLINE_P (fndecl)
11031 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11033 && !cfun->always_inline_functions_inlined
11034 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11037 /* A wrapper function for builtin folding that prevents warnings for
11038 "statement without effect" and the like, caused by removing the
11039 call node earlier than the warning is generated. */
11042 fold_call_expr (tree exp, bool ignore)
11044 tree ret = NULL_TREE;
11045 tree fndecl = get_callee_fndecl (exp);
11047 && TREE_CODE (fndecl) == FUNCTION_DECL
11048 && DECL_BUILT_IN (fndecl)
11049 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11050 yet. Defer folding until we see all the arguments
11051 (after inlining). */
11052 && !CALL_EXPR_VA_ARG_PACK (exp))
11054 int nargs = call_expr_nargs (exp);
11056 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11057 instead last argument is __builtin_va_arg_pack (). Defer folding
11058 even in that case, until arguments are finalized. */
11059 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11061 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11063 && TREE_CODE (fndecl2) == FUNCTION_DECL
11064 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11065 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11069 if (avoid_folding_inline_builtin (fndecl))
11072 /* FIXME: Don't use a list in this interface. */
11073 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11074 return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore);
11077 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11079 tree *args = CALL_EXPR_ARGP (exp);
11080 ret = fold_builtin_n (fndecl, args, nargs, ignore);
11083 ret = fold_builtin_varargs (fndecl, exp, ignore);
11086 /* Propagate location information from original call to
11087 expansion of builtin. Otherwise things like
11088 maybe_emit_chk_warning, that operate on the expansion
11089 of a builtin, will use the wrong location information. */
11090 if (CAN_HAVE_LOCATION_P (exp) && EXPR_HAS_LOCATION (exp))
11092 tree realret = ret;
11093 if (TREE_CODE (ret) == NOP_EXPR)
11094 realret = TREE_OPERAND (ret, 0);
11095 if (CAN_HAVE_LOCATION_P (realret)
11096 && !EXPR_HAS_LOCATION (realret))
11097 SET_EXPR_LOCATION (realret, EXPR_LOCATION (exp));
11106 /* Conveniently construct a function call expression. FNDECL names the
11107 function to be called and ARGLIST is a TREE_LIST of arguments. */
11110 build_function_call_expr (tree fndecl, tree arglist)
11112 tree fntype = TREE_TYPE (fndecl);
11113 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11114 int n = list_length (arglist);
11115 tree *argarray = (tree *) alloca (n * sizeof (tree));
11118 for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist))
11119 argarray[i] = TREE_VALUE (arglist);
11120 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
11123 /* Conveniently construct a function call expression. FNDECL names the
11124 function to be called, N is the number of arguments, and the "..."
11125 parameters are the argument expressions. */
11128 build_call_expr (tree fndecl, int n, ...)
11131 tree fntype = TREE_TYPE (fndecl);
11132 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11133 tree *argarray = (tree *) alloca (n * sizeof (tree));
11137 for (i = 0; i < n; i++)
11138 argarray[i] = va_arg (ap, tree);
11140 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
11143 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11144 N arguments are passed in the array ARGARRAY. */
11147 fold_builtin_call_array (tree type,
11152 tree ret = NULL_TREE;
11156 if (TREE_CODE (fn) == ADDR_EXPR)
11158 tree fndecl = TREE_OPERAND (fn, 0);
11159 if (TREE_CODE (fndecl) == FUNCTION_DECL
11160 && DECL_BUILT_IN (fndecl))
11162 /* If last argument is __builtin_va_arg_pack (), arguments to this
11163 function are not finalized yet. Defer folding until they are. */
11164 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11166 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11168 && TREE_CODE (fndecl2) == FUNCTION_DECL
11169 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11170 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11171 return build_call_array (type, fn, n, argarray);
11173 if (avoid_folding_inline_builtin (fndecl))
11174 return build_call_array (type, fn, n, argarray);
11175 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11177 tree arglist = NULL_TREE;
11178 for (i = n - 1; i >= 0; i--)
11179 arglist = tree_cons (NULL_TREE, argarray[i], arglist);
11180 ret = targetm.fold_builtin (fndecl, arglist, false);
11183 return build_call_array (type, fn, n, argarray);
11185 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11187 /* First try the transformations that don't require consing up
11189 ret = fold_builtin_n (fndecl, argarray, n, false);
11194 /* If we got this far, we need to build an exp. */
11195 exp = build_call_array (type, fn, n, argarray);
11196 ret = fold_builtin_varargs (fndecl, exp, false);
11197 return ret ? ret : exp;
11201 return build_call_array (type, fn, n, argarray);
11204 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11205 along with N new arguments specified as the "..." parameters. SKIP
11206 is the number of arguments in EXP to be omitted. This function is used
11207 to do varargs-to-varargs transformations. */
11210 rewrite_call_expr (tree exp, int skip, tree fndecl, int n, ...)
11212 int oldnargs = call_expr_nargs (exp);
11213 int nargs = oldnargs - skip + n;
11214 tree fntype = TREE_TYPE (fndecl);
11215 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11223 buffer = XALLOCAVEC (tree, nargs);
11225 for (i = 0; i < n; i++)
11226 buffer[i] = va_arg (ap, tree);
11228 for (j = skip; j < oldnargs; j++, i++)
11229 buffer[i] = CALL_EXPR_ARG (exp, j);
11232 buffer = CALL_EXPR_ARGP (exp) + skip;
11234 return fold (build_call_array (TREE_TYPE (exp), fn, nargs, buffer));
11237 /* Validate a single argument ARG against a tree code CODE representing
11241 validate_arg (const_tree arg, enum tree_code code)
11245 else if (code == POINTER_TYPE)
11246 return POINTER_TYPE_P (TREE_TYPE (arg));
11247 else if (code == INTEGER_TYPE)
11248 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11249 return code == TREE_CODE (TREE_TYPE (arg));
11252 /* This function validates the types of a function call argument list
11253 against a specified list of tree_codes. If the last specifier is a 0,
11254 that represents an ellipses, otherwise the last specifier must be a
11257 This is the GIMPLE version of validate_arglist. Eventually we want to
11258 completely convert builtins.c to work from GIMPLEs and the tree based
11259 validate_arglist will then be removed. */
11262 validate_gimple_arglist (const_gimple call, ...)
11264 enum tree_code code;
11270 va_start (ap, call);
11275 code = (enum tree_code) va_arg (ap, int);
11279 /* This signifies an ellipses, any further arguments are all ok. */
11283 /* This signifies an endlink, if no arguments remain, return
11284 true, otherwise return false. */
11285 res = (i == gimple_call_num_args (call));
11288 /* If no parameters remain or the parameter's code does not
11289 match the specified code, return false. Otherwise continue
11290 checking any remaining arguments. */
11291 arg = gimple_call_arg (call, i++);
11292 if (!validate_arg (arg, code))
11299 /* We need gotos here since we can only have one VA_CLOSE in a
11307 /* This function validates the types of a function call argument list
11308 against a specified list of tree_codes. If the last specifier is a 0,
11309 that represents an ellipses, otherwise the last specifier must be a
11313 validate_arglist (const_tree callexpr, ...)
11315 enum tree_code code;
11318 const_call_expr_arg_iterator iter;
11321 va_start (ap, callexpr);
11322 init_const_call_expr_arg_iterator (callexpr, &iter);
11326 code = (enum tree_code) va_arg (ap, int);
11330 /* This signifies an ellipses, any further arguments are all ok. */
11334 /* This signifies an endlink, if no arguments remain, return
11335 true, otherwise return false. */
11336 res = !more_const_call_expr_args_p (&iter);
11339 /* If no parameters remain or the parameter's code does not
11340 match the specified code, return false. Otherwise continue
11341 checking any remaining arguments. */
11342 arg = next_const_call_expr_arg (&iter);
11343 if (!validate_arg (arg, code))
11350 /* We need gotos here since we can only have one VA_CLOSE in a
11358 /* Default target-specific builtin expander that does nothing. */
11361 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11362 rtx target ATTRIBUTE_UNUSED,
11363 rtx subtarget ATTRIBUTE_UNUSED,
11364 enum machine_mode mode ATTRIBUTE_UNUSED,
11365 int ignore ATTRIBUTE_UNUSED)
11370 /* Returns true is EXP represents data that would potentially reside
11371 in a readonly section. */
11374 readonly_data_expr (tree exp)
11378 if (TREE_CODE (exp) != ADDR_EXPR)
11381 exp = get_base_address (TREE_OPERAND (exp, 0));
11385 /* Make sure we call decl_readonly_section only for trees it
11386 can handle (since it returns true for everything it doesn't
11388 if (TREE_CODE (exp) == STRING_CST
11389 || TREE_CODE (exp) == CONSTRUCTOR
11390 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11391 return decl_readonly_section (exp, 0);
11396 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11397 to the call, and TYPE is its return type.
11399 Return NULL_TREE if no simplification was possible, otherwise return the
11400 simplified form of the call as a tree.
11402 The simplified form may be a constant or other expression which
11403 computes the same value, but in a more efficient manner (including
11404 calls to other builtin functions).
11406 The call may contain arguments which need to be evaluated, but
11407 which are not useful to determine the result of the call. In
11408 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11409 COMPOUND_EXPR will be an argument which must be evaluated.
11410 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11411 COMPOUND_EXPR in the chain will contain the tree for the simplified
11412 form of the builtin function call. */
11415 fold_builtin_strstr (tree s1, tree s2, tree type)
11417 if (!validate_arg (s1, POINTER_TYPE)
11418 || !validate_arg (s2, POINTER_TYPE))
11423 const char *p1, *p2;
11425 p2 = c_getstr (s2);
11429 p1 = c_getstr (s1);
11432 const char *r = strstr (p1, p2);
11436 return build_int_cst (TREE_TYPE (s1), 0);
11438 /* Return an offset into the constant string argument. */
11439 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11440 s1, size_int (r - p1));
11441 return fold_convert (type, tem);
11444 /* The argument is const char *, and the result is char *, so we need
11445 a type conversion here to avoid a warning. */
11447 return fold_convert (type, s1);
11452 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11456 /* New argument list transforming strstr(s1, s2) to
11457 strchr(s1, s2[0]). */
11458 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11462 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11463 the call, and TYPE is its return type.
11465 Return NULL_TREE if no simplification was possible, otherwise return the
11466 simplified form of the call as a tree.
11468 The simplified form may be a constant or other expression which
11469 computes the same value, but in a more efficient manner (including
11470 calls to other builtin functions).
11472 The call may contain arguments which need to be evaluated, but
11473 which are not useful to determine the result of the call. In
11474 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11475 COMPOUND_EXPR will be an argument which must be evaluated.
11476 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11477 COMPOUND_EXPR in the chain will contain the tree for the simplified
11478 form of the builtin function call. */
11481 fold_builtin_strchr (tree s1, tree s2, tree type)
11483 if (!validate_arg (s1, POINTER_TYPE)
11484 || !validate_arg (s2, INTEGER_TYPE))
11490 if (TREE_CODE (s2) != INTEGER_CST)
11493 p1 = c_getstr (s1);
11500 if (target_char_cast (s2, &c))
11503 r = strchr (p1, c);
11506 return build_int_cst (TREE_TYPE (s1), 0);
11508 /* Return an offset into the constant string argument. */
11509 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11510 s1, size_int (r - p1));
11511 return fold_convert (type, tem);
11517 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11518 the call, and TYPE is its return type.
11520 Return NULL_TREE if no simplification was possible, otherwise return the
11521 simplified form of the call as a tree.
11523 The simplified form may be a constant or other expression which
11524 computes the same value, but in a more efficient manner (including
11525 calls to other builtin functions).
11527 The call may contain arguments which need to be evaluated, but
11528 which are not useful to determine the result of the call. In
11529 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11530 COMPOUND_EXPR will be an argument which must be evaluated.
11531 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11532 COMPOUND_EXPR in the chain will contain the tree for the simplified
11533 form of the builtin function call. */
11536 fold_builtin_strrchr (tree s1, tree s2, tree type)
11538 if (!validate_arg (s1, POINTER_TYPE)
11539 || !validate_arg (s2, INTEGER_TYPE))
11546 if (TREE_CODE (s2) != INTEGER_CST)
11549 p1 = c_getstr (s1);
11556 if (target_char_cast (s2, &c))
11559 r = strrchr (p1, c);
11562 return build_int_cst (TREE_TYPE (s1), 0);
11564 /* Return an offset into the constant string argument. */
11565 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11566 s1, size_int (r - p1));
11567 return fold_convert (type, tem);
11570 if (! integer_zerop (s2))
11573 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11577 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11578 return build_call_expr (fn, 2, s1, s2);
11582 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11583 to the call, and TYPE is its return type.
11585 Return NULL_TREE if no simplification was possible, otherwise return the
11586 simplified form of the call as a tree.
11588 The simplified form may be a constant or other expression which
11589 computes the same value, but in a more efficient manner (including
11590 calls to other builtin functions).
11592 The call may contain arguments which need to be evaluated, but
11593 which are not useful to determine the result of the call. In
11594 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11595 COMPOUND_EXPR will be an argument which must be evaluated.
11596 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11597 COMPOUND_EXPR in the chain will contain the tree for the simplified
11598 form of the builtin function call. */
11601 fold_builtin_strpbrk (tree s1, tree s2, tree type)
11603 if (!validate_arg (s1, POINTER_TYPE)
11604 || !validate_arg (s2, POINTER_TYPE))
11609 const char *p1, *p2;
11611 p2 = c_getstr (s2);
11615 p1 = c_getstr (s1);
11618 const char *r = strpbrk (p1, p2);
11622 return build_int_cst (TREE_TYPE (s1), 0);
11624 /* Return an offset into the constant string argument. */
11625 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11626 s1, size_int (r - p1));
11627 return fold_convert (type, tem);
11631 /* strpbrk(x, "") == NULL.
11632 Evaluate and ignore s1 in case it had side-effects. */
11633 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
11636 return NULL_TREE; /* Really call strpbrk. */
11638 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11642 /* New argument list transforming strpbrk(s1, s2) to
11643 strchr(s1, s2[0]). */
11644 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11648 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11651 Return NULL_TREE if no simplification was possible, otherwise return the
11652 simplified form of the call as a tree.
11654 The simplified form may be a constant or other expression which
11655 computes the same value, but in a more efficient manner (including
11656 calls to other builtin functions).
11658 The call may contain arguments which need to be evaluated, but
11659 which are not useful to determine the result of the call. In
11660 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11661 COMPOUND_EXPR will be an argument which must be evaluated.
11662 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11663 COMPOUND_EXPR in the chain will contain the tree for the simplified
11664 form of the builtin function call. */
11667 fold_builtin_strcat (tree dst, tree src)
11669 if (!validate_arg (dst, POINTER_TYPE)
11670 || !validate_arg (src, POINTER_TYPE))
11674 const char *p = c_getstr (src);
11676 /* If the string length is zero, return the dst parameter. */
11677 if (p && *p == '\0')
11684 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11685 arguments to the call.
11687 Return NULL_TREE if no simplification was possible, otherwise return the
11688 simplified form of the call as a tree.
11690 The simplified form may be a constant or other expression which
11691 computes the same value, but in a more efficient manner (including
11692 calls to other builtin functions).
11694 The call may contain arguments which need to be evaluated, but
11695 which are not useful to determine the result of the call. In
11696 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11697 COMPOUND_EXPR will be an argument which must be evaluated.
11698 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11699 COMPOUND_EXPR in the chain will contain the tree for the simplified
11700 form of the builtin function call. */
11703 fold_builtin_strncat (tree dst, tree src, tree len)
11705 if (!validate_arg (dst, POINTER_TYPE)
11706 || !validate_arg (src, POINTER_TYPE)
11707 || !validate_arg (len, INTEGER_TYPE))
11711 const char *p = c_getstr (src);
11713 /* If the requested length is zero, or the src parameter string
11714 length is zero, return the dst parameter. */
11715 if (integer_zerop (len) || (p && *p == '\0'))
11716 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
11718 /* If the requested len is greater than or equal to the string
11719 length, call strcat. */
11720 if (TREE_CODE (len) == INTEGER_CST && p
11721 && compare_tree_int (len, strlen (p)) >= 0)
11723 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11725 /* If the replacement _DECL isn't initialized, don't do the
11730 return build_call_expr (fn, 2, dst, src);
11736 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11739 Return NULL_TREE if no simplification was possible, otherwise return the
11740 simplified form of the call as a tree.
11742 The simplified form may be a constant or other expression which
11743 computes the same value, but in a more efficient manner (including
11744 calls to other builtin functions).
11746 The call may contain arguments which need to be evaluated, but
11747 which are not useful to determine the result of the call. In
11748 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11749 COMPOUND_EXPR will be an argument which must be evaluated.
11750 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11751 COMPOUND_EXPR in the chain will contain the tree for the simplified
11752 form of the builtin function call. */
11755 fold_builtin_strspn (tree s1, tree s2)
11757 if (!validate_arg (s1, POINTER_TYPE)
11758 || !validate_arg (s2, POINTER_TYPE))
11762 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11764 /* If both arguments are constants, evaluate at compile-time. */
11767 const size_t r = strspn (p1, p2);
11768 return size_int (r);
11771 /* If either argument is "", return NULL_TREE. */
11772 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11773 /* Evaluate and ignore both arguments in case either one has
11775 return omit_two_operands (size_type_node, size_zero_node,
11781 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11784 Return NULL_TREE if no simplification was possible, otherwise return the
11785 simplified form of the call as a tree.
11787 The simplified form may be a constant or other expression which
11788 computes the same value, but in a more efficient manner (including
11789 calls to other builtin functions).
11791 The call may contain arguments which need to be evaluated, but
11792 which are not useful to determine the result of the call. In
11793 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11794 COMPOUND_EXPR will be an argument which must be evaluated.
11795 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11796 COMPOUND_EXPR in the chain will contain the tree for the simplified
11797 form of the builtin function call. */
11800 fold_builtin_strcspn (tree s1, tree s2)
11802 if (!validate_arg (s1, POINTER_TYPE)
11803 || !validate_arg (s2, POINTER_TYPE))
11807 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11809 /* If both arguments are constants, evaluate at compile-time. */
11812 const size_t r = strcspn (p1, p2);
11813 return size_int (r);
11816 /* If the first argument is "", return NULL_TREE. */
11817 if (p1 && *p1 == '\0')
11819 /* Evaluate and ignore argument s2 in case it has
11821 return omit_one_operand (size_type_node,
11822 size_zero_node, s2);
11825 /* If the second argument is "", return __builtin_strlen(s1). */
11826 if (p2 && *p2 == '\0')
11828 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11830 /* If the replacement _DECL isn't initialized, don't do the
11835 return build_call_expr (fn, 1, s1);
11841 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11842 to the call. IGNORE is true if the value returned
11843 by the builtin will be ignored. UNLOCKED is true is true if this
11844 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11845 the known length of the string. Return NULL_TREE if no simplification
11849 fold_builtin_fputs (tree arg0, tree arg1, bool ignore, bool unlocked, tree len)
11851 /* If we're using an unlocked function, assume the other unlocked
11852 functions exist explicitly. */
11853 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11854 : implicit_built_in_decls[BUILT_IN_FPUTC];
11855 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11856 : implicit_built_in_decls[BUILT_IN_FWRITE];
11858 /* If the return value is used, don't do the transformation. */
11862 /* Verify the arguments in the original call. */
11863 if (!validate_arg (arg0, POINTER_TYPE)
11864 || !validate_arg (arg1, POINTER_TYPE))
11868 len = c_strlen (arg0, 0);
11870 /* Get the length of the string passed to fputs. If the length
11871 can't be determined, punt. */
11873 || TREE_CODE (len) != INTEGER_CST)
11876 switch (compare_tree_int (len, 1))
11878 case -1: /* length is 0, delete the call entirely . */
11879 return omit_one_operand (integer_type_node, integer_zero_node, arg1);;
11881 case 0: /* length is 1, call fputc. */
11883 const char *p = c_getstr (arg0);
11888 return build_call_expr (fn_fputc, 2,
11889 build_int_cst (NULL_TREE, p[0]), arg1);
11895 case 1: /* length is greater than 1, call fwrite. */
11897 /* If optimizing for size keep fputs. */
11898 if (optimize_function_for_size_p (cfun))
11900 /* New argument list transforming fputs(string, stream) to
11901 fwrite(string, 1, len, stream). */
11903 return build_call_expr (fn_fwrite, 4, arg0, size_one_node, len, arg1);
11908 gcc_unreachable ();
11913 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11914 produced. False otherwise. This is done so that we don't output the error
11915 or warning twice or three times. */
11918 fold_builtin_next_arg (tree exp, bool va_start_p)
11920 tree fntype = TREE_TYPE (current_function_decl);
11921 int nargs = call_expr_nargs (exp);
11924 if (TYPE_ARG_TYPES (fntype) == 0
11925 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11926 == void_type_node))
11928 error ("%<va_start%> used in function with fixed args");
11934 if (va_start_p && (nargs != 2))
11936 error ("wrong number of arguments to function %<va_start%>");
11939 arg = CALL_EXPR_ARG (exp, 1);
11941 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11942 when we checked the arguments and if needed issued a warning. */
11947 /* Evidently an out of date version of <stdarg.h>; can't validate
11948 va_start's second argument, but can still work as intended. */
11949 warning (0, "%<__builtin_next_arg%> called without an argument");
11952 else if (nargs > 1)
11954 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11957 arg = CALL_EXPR_ARG (exp, 0);
11960 if (TREE_CODE (arg) == SSA_NAME)
11961 arg = SSA_NAME_VAR (arg);
11963 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11964 or __builtin_next_arg (0) the first time we see it, after checking
11965 the arguments and if needed issuing a warning. */
11966 if (!integer_zerop (arg))
11968 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11970 /* Strip off all nops for the sake of the comparison. This
11971 is not quite the same as STRIP_NOPS. It does more.
11972 We must also strip off INDIRECT_EXPR for C++ reference
11974 while (CONVERT_EXPR_P (arg)
11975 || TREE_CODE (arg) == INDIRECT_REF)
11976 arg = TREE_OPERAND (arg, 0);
11977 if (arg != last_parm)
11979 /* FIXME: Sometimes with the tree optimizers we can get the
11980 not the last argument even though the user used the last
11981 argument. We just warn and set the arg to be the last
11982 argument so that we will get wrong-code because of
11984 warning (0, "second parameter of %<va_start%> not last named argument");
11987 /* Undefined by C99 7.15.1.4p4 (va_start):
11988 "If the parameter parmN is declared with the register storage
11989 class, with a function or array type, or with a type that is
11990 not compatible with the type that results after application of
11991 the default argument promotions, the behavior is undefined."
11993 else if (DECL_REGISTER (arg))
11994 warning (0, "undefined behaviour when second parameter of "
11995 "%<va_start%> is declared with %<register%> storage");
11997 /* We want to verify the second parameter just once before the tree
11998 optimizers are run and then avoid keeping it in the tree,
11999 as otherwise we could warn even for correct code like:
12000 void foo (int i, ...)
12001 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12003 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12005 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12011 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12012 ORIG may be null if this is a 2-argument call. We don't attempt to
12013 simplify calls with more than 3 arguments.
12015 Return NULL_TREE if no simplification was possible, otherwise return the
12016 simplified form of the call as a tree. If IGNORED is true, it means that
12017 the caller does not use the returned value of the function. */
12020 fold_builtin_sprintf (tree dest, tree fmt, tree orig, int ignored)
12023 const char *fmt_str = NULL;
12025 /* Verify the required arguments in the original call. We deal with two
12026 types of sprintf() calls: 'sprintf (str, fmt)' and
12027 'sprintf (dest, "%s", orig)'. */
12028 if (!validate_arg (dest, POINTER_TYPE)
12029 || !validate_arg (fmt, POINTER_TYPE))
12031 if (orig && !validate_arg (orig, POINTER_TYPE))
12034 /* Check whether the format is a literal string constant. */
12035 fmt_str = c_getstr (fmt);
12036 if (fmt_str == NULL)
12040 retval = NULL_TREE;
12042 if (!init_target_chars ())
12045 /* If the format doesn't contain % args or %%, use strcpy. */
12046 if (strchr (fmt_str, target_percent) == NULL)
12048 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
12053 /* Don't optimize sprintf (buf, "abc", ptr++). */
12057 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12058 'format' is known to contain no % formats. */
12059 call = build_call_expr (fn, 2, dest, fmt);
12061 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
12064 /* If the format is "%s", use strcpy if the result isn't used. */
12065 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12068 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
12073 /* Don't crash on sprintf (str1, "%s"). */
12077 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12080 retval = c_strlen (orig, 1);
12081 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12084 call = build_call_expr (fn, 2, dest, orig);
12087 if (call && retval)
12089 retval = fold_convert
12090 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
12092 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12098 /* Expand a call EXP to __builtin_object_size. */
12101 expand_builtin_object_size (tree exp)
12104 int object_size_type;
12105 tree fndecl = get_callee_fndecl (exp);
12107 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12109 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12111 expand_builtin_trap ();
12115 ost = CALL_EXPR_ARG (exp, 1);
12118 if (TREE_CODE (ost) != INTEGER_CST
12119 || tree_int_cst_sgn (ost) < 0
12120 || compare_tree_int (ost, 3) > 0)
12122 error ("%Klast argument of %D is not integer constant between 0 and 3",
12124 expand_builtin_trap ();
12128 object_size_type = tree_low_cst (ost, 0);
12130 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12133 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12134 FCODE is the BUILT_IN_* to use.
12135 Return NULL_RTX if we failed; the caller should emit a normal call,
12136 otherwise try to get the result in TARGET, if convenient (and in
12137 mode MODE if that's convenient). */
12140 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12141 enum built_in_function fcode)
12143 tree dest, src, len, size;
12145 if (!validate_arglist (exp,
12147 fcode == BUILT_IN_MEMSET_CHK
12148 ? INTEGER_TYPE : POINTER_TYPE,
12149 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12152 dest = CALL_EXPR_ARG (exp, 0);
12153 src = CALL_EXPR_ARG (exp, 1);
12154 len = CALL_EXPR_ARG (exp, 2);
12155 size = CALL_EXPR_ARG (exp, 3);
12157 if (! host_integerp (size, 1))
12160 if (host_integerp (len, 1) || integer_all_onesp (size))
12164 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12166 warning_at (tree_nonartificial_location (exp),
12167 0, "%Kcall to %D will always overflow destination buffer",
12168 exp, get_callee_fndecl (exp));
12173 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12174 mem{cpy,pcpy,move,set} is available. */
12177 case BUILT_IN_MEMCPY_CHK:
12178 fn = built_in_decls[BUILT_IN_MEMCPY];
12180 case BUILT_IN_MEMPCPY_CHK:
12181 fn = built_in_decls[BUILT_IN_MEMPCPY];
12183 case BUILT_IN_MEMMOVE_CHK:
12184 fn = built_in_decls[BUILT_IN_MEMMOVE];
12186 case BUILT_IN_MEMSET_CHK:
12187 fn = built_in_decls[BUILT_IN_MEMSET];
12196 fn = build_call_expr (fn, 3, dest, src, len);
12197 STRIP_TYPE_NOPS (fn);
12198 while (TREE_CODE (fn) == COMPOUND_EXPR)
12200 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
12202 fn = TREE_OPERAND (fn, 1);
12204 if (TREE_CODE (fn) == CALL_EXPR)
12205 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12206 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12208 else if (fcode == BUILT_IN_MEMSET_CHK)
12212 unsigned int dest_align
12213 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12215 /* If DEST is not a pointer type, call the normal function. */
12216 if (dest_align == 0)
12219 /* If SRC and DEST are the same (and not volatile), do nothing. */
12220 if (operand_equal_p (src, dest, 0))
12224 if (fcode != BUILT_IN_MEMPCPY_CHK)
12226 /* Evaluate and ignore LEN in case it has side-effects. */
12227 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12228 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12231 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12232 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12235 /* __memmove_chk special case. */
12236 if (fcode == BUILT_IN_MEMMOVE_CHK)
12238 unsigned int src_align
12239 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12241 if (src_align == 0)
12244 /* If src is categorized for a readonly section we can use
12245 normal __memcpy_chk. */
12246 if (readonly_data_expr (src))
12248 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12251 fn = build_call_expr (fn, 4, dest, src, len, size);
12252 STRIP_TYPE_NOPS (fn);
12253 while (TREE_CODE (fn) == COMPOUND_EXPR)
12255 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
12257 fn = TREE_OPERAND (fn, 1);
12259 if (TREE_CODE (fn) == CALL_EXPR)
12260 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12261 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12268 /* Emit warning if a buffer overflow is detected at compile time. */
12271 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12275 location_t loc = tree_nonartificial_location (exp);
12279 case BUILT_IN_STRCPY_CHK:
12280 case BUILT_IN_STPCPY_CHK:
12281 /* For __strcat_chk the warning will be emitted only if overflowing
12282 by at least strlen (dest) + 1 bytes. */
12283 case BUILT_IN_STRCAT_CHK:
12284 len = CALL_EXPR_ARG (exp, 1);
12285 size = CALL_EXPR_ARG (exp, 2);
12288 case BUILT_IN_STRNCAT_CHK:
12289 case BUILT_IN_STRNCPY_CHK:
12290 len = CALL_EXPR_ARG (exp, 2);
12291 size = CALL_EXPR_ARG (exp, 3);
12293 case BUILT_IN_SNPRINTF_CHK:
12294 case BUILT_IN_VSNPRINTF_CHK:
12295 len = CALL_EXPR_ARG (exp, 1);
12296 size = CALL_EXPR_ARG (exp, 3);
12299 gcc_unreachable ();
12305 if (! host_integerp (size, 1) || integer_all_onesp (size))
12310 len = c_strlen (len, 1);
12311 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12314 else if (fcode == BUILT_IN_STRNCAT_CHK)
12316 tree src = CALL_EXPR_ARG (exp, 1);
12317 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12319 src = c_strlen (src, 1);
12320 if (! src || ! host_integerp (src, 1))
12322 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12323 exp, get_callee_fndecl (exp));
12326 else if (tree_int_cst_lt (src, size))
12329 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12332 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12333 exp, get_callee_fndecl (exp));
12336 /* Emit warning if a buffer overflow is detected at compile time
12337 in __sprintf_chk/__vsprintf_chk calls. */
12340 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12342 tree dest, size, len, fmt, flag;
12343 const char *fmt_str;
12344 int nargs = call_expr_nargs (exp);
12346 /* Verify the required arguments in the original call. */
12350 dest = CALL_EXPR_ARG (exp, 0);
12351 flag = CALL_EXPR_ARG (exp, 1);
12352 size = CALL_EXPR_ARG (exp, 2);
12353 fmt = CALL_EXPR_ARG (exp, 3);
12355 if (! host_integerp (size, 1) || integer_all_onesp (size))
12358 /* Check whether the format is a literal string constant. */
12359 fmt_str = c_getstr (fmt);
12360 if (fmt_str == NULL)
12363 if (!init_target_chars ())
12366 /* If the format doesn't contain % args or %%, we know its size. */
12367 if (strchr (fmt_str, target_percent) == 0)
12368 len = build_int_cstu (size_type_node, strlen (fmt_str));
12369 /* If the format is "%s" and first ... argument is a string literal,
12371 else if (fcode == BUILT_IN_SPRINTF_CHK
12372 && strcmp (fmt_str, target_percent_s) == 0)
12378 arg = CALL_EXPR_ARG (exp, 4);
12379 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12382 len = c_strlen (arg, 1);
12383 if (!len || ! host_integerp (len, 1))
12389 if (! tree_int_cst_lt (len, size))
12390 warning_at (tree_nonartificial_location (exp),
12391 0, "%Kcall to %D will always overflow destination buffer",
12392 exp, get_callee_fndecl (exp));
12395 /* Emit warning if a free is called with address of a variable. */
12398 maybe_emit_free_warning (tree exp)
12400 tree arg = CALL_EXPR_ARG (exp, 0);
12403 if (TREE_CODE (arg) != ADDR_EXPR)
12406 arg = get_base_address (TREE_OPERAND (arg, 0));
12407 if (arg == NULL || INDIRECT_REF_P (arg))
12410 if (SSA_VAR_P (arg))
12411 warning_at (tree_nonartificial_location (exp),
12412 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12414 warning_at (tree_nonartificial_location (exp),
12415 0, "%Kattempt to free a non-heap object", exp);
12418 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12422 fold_builtin_object_size (tree ptr, tree ost)
12424 tree ret = NULL_TREE;
12425 int object_size_type;
12427 if (!validate_arg (ptr, POINTER_TYPE)
12428 || !validate_arg (ost, INTEGER_TYPE))
12433 if (TREE_CODE (ost) != INTEGER_CST
12434 || tree_int_cst_sgn (ost) < 0
12435 || compare_tree_int (ost, 3) > 0)
12438 object_size_type = tree_low_cst (ost, 0);
12440 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12441 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12442 and (size_t) 0 for types 2 and 3. */
12443 if (TREE_SIDE_EFFECTS (ptr))
12444 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12446 if (TREE_CODE (ptr) == ADDR_EXPR)
12447 ret = build_int_cstu (size_type_node,
12448 compute_builtin_object_size (ptr, object_size_type));
12450 else if (TREE_CODE (ptr) == SSA_NAME)
12452 unsigned HOST_WIDE_INT bytes;
12454 /* If object size is not known yet, delay folding until
12455 later. Maybe subsequent passes will help determining
12457 bytes = compute_builtin_object_size (ptr, object_size_type);
12458 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
12460 ret = build_int_cstu (size_type_node, bytes);
12465 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
12466 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
12467 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
12474 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12475 DEST, SRC, LEN, and SIZE are the arguments to the call.
12476 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12477 code of the builtin. If MAXLEN is not NULL, it is maximum length
12478 passed as third argument. */
12481 fold_builtin_memory_chk (tree fndecl,
12482 tree dest, tree src, tree len, tree size,
12483 tree maxlen, bool ignore,
12484 enum built_in_function fcode)
12488 if (!validate_arg (dest, POINTER_TYPE)
12489 || !validate_arg (src,
12490 (fcode == BUILT_IN_MEMSET_CHK
12491 ? INTEGER_TYPE : POINTER_TYPE))
12492 || !validate_arg (len, INTEGER_TYPE)
12493 || !validate_arg (size, INTEGER_TYPE))
12496 /* If SRC and DEST are the same (and not volatile), return DEST
12497 (resp. DEST+LEN for __mempcpy_chk). */
12498 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12500 if (fcode != BUILT_IN_MEMPCPY_CHK)
12501 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12504 tree temp = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12505 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
12509 if (! host_integerp (size, 1))
12512 if (! integer_all_onesp (size))
12514 if (! host_integerp (len, 1))
12516 /* If LEN is not constant, try MAXLEN too.
12517 For MAXLEN only allow optimizing into non-_ocs function
12518 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12519 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12521 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12523 /* (void) __mempcpy_chk () can be optimized into
12524 (void) __memcpy_chk (). */
12525 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12529 return build_call_expr (fn, 4, dest, src, len, size);
12537 if (tree_int_cst_lt (size, maxlen))
12542 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12543 mem{cpy,pcpy,move,set} is available. */
12546 case BUILT_IN_MEMCPY_CHK:
12547 fn = built_in_decls[BUILT_IN_MEMCPY];
12549 case BUILT_IN_MEMPCPY_CHK:
12550 fn = built_in_decls[BUILT_IN_MEMPCPY];
12552 case BUILT_IN_MEMMOVE_CHK:
12553 fn = built_in_decls[BUILT_IN_MEMMOVE];
12555 case BUILT_IN_MEMSET_CHK:
12556 fn = built_in_decls[BUILT_IN_MEMSET];
12565 return build_call_expr (fn, 3, dest, src, len);
12568 /* Fold a call to the __st[rp]cpy_chk builtin.
12569 DEST, SRC, and SIZE are the arguments to the call.
12570 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12571 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12572 strings passed as second argument. */
12575 fold_builtin_stxcpy_chk (tree fndecl, tree dest, tree src, tree size,
12576 tree maxlen, bool ignore,
12577 enum built_in_function fcode)
12581 if (!validate_arg (dest, POINTER_TYPE)
12582 || !validate_arg (src, POINTER_TYPE)
12583 || !validate_arg (size, INTEGER_TYPE))
12586 /* If SRC and DEST are the same (and not volatile), return DEST. */
12587 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12588 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
12590 if (! host_integerp (size, 1))
12593 if (! integer_all_onesp (size))
12595 len = c_strlen (src, 1);
12596 if (! len || ! host_integerp (len, 1))
12598 /* If LEN is not constant, try MAXLEN too.
12599 For MAXLEN only allow optimizing into non-_ocs function
12600 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12601 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12603 if (fcode == BUILT_IN_STPCPY_CHK)
12608 /* If return value of __stpcpy_chk is ignored,
12609 optimize into __strcpy_chk. */
12610 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12614 return build_call_expr (fn, 3, dest, src, size);
12617 if (! len || TREE_SIDE_EFFECTS (len))
12620 /* If c_strlen returned something, but not a constant,
12621 transform __strcpy_chk into __memcpy_chk. */
12622 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12626 len = size_binop (PLUS_EXPR, len, ssize_int (1));
12627 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
12628 build_call_expr (fn, 4,
12629 dest, src, len, size));
12635 if (! tree_int_cst_lt (maxlen, size))
12639 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12640 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12641 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12645 return build_call_expr (fn, 2, dest, src);
12648 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12649 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12650 length passed as third argument. */
12653 fold_builtin_strncpy_chk (tree dest, tree src, tree len, tree size,
12658 if (!validate_arg (dest, POINTER_TYPE)
12659 || !validate_arg (src, POINTER_TYPE)
12660 || !validate_arg (len, INTEGER_TYPE)
12661 || !validate_arg (size, INTEGER_TYPE))
12664 if (! host_integerp (size, 1))
12667 if (! integer_all_onesp (size))
12669 if (! host_integerp (len, 1))
12671 /* If LEN is not constant, try MAXLEN too.
12672 For MAXLEN only allow optimizing into non-_ocs function
12673 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12674 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12680 if (tree_int_cst_lt (size, maxlen))
12684 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12685 fn = built_in_decls[BUILT_IN_STRNCPY];
12689 return build_call_expr (fn, 3, dest, src, len);
12692 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12693 are the arguments to the call. */
12696 fold_builtin_strcat_chk (tree fndecl, tree dest, tree src, tree size)
12701 if (!validate_arg (dest, POINTER_TYPE)
12702 || !validate_arg (src, POINTER_TYPE)
12703 || !validate_arg (size, INTEGER_TYPE))
12706 p = c_getstr (src);
12707 /* If the SRC parameter is "", return DEST. */
12708 if (p && *p == '\0')
12709 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12711 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12714 /* If __builtin_strcat_chk is used, assume strcat is available. */
12715 fn = built_in_decls[BUILT_IN_STRCAT];
12719 return build_call_expr (fn, 2, dest, src);
12722 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12726 fold_builtin_strncat_chk (tree fndecl,
12727 tree dest, tree src, tree len, tree size)
12732 if (!validate_arg (dest, POINTER_TYPE)
12733 || !validate_arg (src, POINTER_TYPE)
12734 || !validate_arg (size, INTEGER_TYPE)
12735 || !validate_arg (size, INTEGER_TYPE))
12738 p = c_getstr (src);
12739 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12740 if (p && *p == '\0')
12741 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12742 else if (integer_zerop (len))
12743 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12745 if (! host_integerp (size, 1))
12748 if (! integer_all_onesp (size))
12750 tree src_len = c_strlen (src, 1);
12752 && host_integerp (src_len, 1)
12753 && host_integerp (len, 1)
12754 && ! tree_int_cst_lt (len, src_len))
12756 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12757 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12761 return build_call_expr (fn, 3, dest, src, size);
12766 /* If __builtin_strncat_chk is used, assume strncat is available. */
12767 fn = built_in_decls[BUILT_IN_STRNCAT];
12771 return build_call_expr (fn, 3, dest, src, len);
12774 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12775 a normal call should be emitted rather than expanding the function
12776 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12779 fold_builtin_sprintf_chk (tree exp, enum built_in_function fcode)
12781 tree dest, size, len, fn, fmt, flag;
12782 const char *fmt_str;
12783 int nargs = call_expr_nargs (exp);
12785 /* Verify the required arguments in the original call. */
12788 dest = CALL_EXPR_ARG (exp, 0);
12789 if (!validate_arg (dest, POINTER_TYPE))
12791 flag = CALL_EXPR_ARG (exp, 1);
12792 if (!validate_arg (flag, INTEGER_TYPE))
12794 size = CALL_EXPR_ARG (exp, 2);
12795 if (!validate_arg (size, INTEGER_TYPE))
12797 fmt = CALL_EXPR_ARG (exp, 3);
12798 if (!validate_arg (fmt, POINTER_TYPE))
12801 if (! host_integerp (size, 1))
12806 if (!init_target_chars ())
12809 /* Check whether the format is a literal string constant. */
12810 fmt_str = c_getstr (fmt);
12811 if (fmt_str != NULL)
12813 /* If the format doesn't contain % args or %%, we know the size. */
12814 if (strchr (fmt_str, target_percent) == 0)
12816 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12817 len = build_int_cstu (size_type_node, strlen (fmt_str));
12819 /* If the format is "%s" and first ... argument is a string literal,
12820 we know the size too. */
12821 else if (fcode == BUILT_IN_SPRINTF_CHK
12822 && strcmp (fmt_str, target_percent_s) == 0)
12828 arg = CALL_EXPR_ARG (exp, 4);
12829 if (validate_arg (arg, POINTER_TYPE))
12831 len = c_strlen (arg, 1);
12832 if (! len || ! host_integerp (len, 1))
12839 if (! integer_all_onesp (size))
12841 if (! len || ! tree_int_cst_lt (len, size))
12845 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12846 or if format doesn't contain % chars or is "%s". */
12847 if (! integer_zerop (flag))
12849 if (fmt_str == NULL)
12851 if (strchr (fmt_str, target_percent) != NULL
12852 && strcmp (fmt_str, target_percent_s))
12856 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12857 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12858 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12862 return rewrite_call_expr (exp, 4, fn, 2, dest, fmt);
12865 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12866 a normal call should be emitted rather than expanding the function
12867 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12868 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12869 passed as second argument. */
12872 fold_builtin_snprintf_chk (tree exp, tree maxlen,
12873 enum built_in_function fcode)
12875 tree dest, size, len, fn, fmt, flag;
12876 const char *fmt_str;
12878 /* Verify the required arguments in the original call. */
12879 if (call_expr_nargs (exp) < 5)
12881 dest = CALL_EXPR_ARG (exp, 0);
12882 if (!validate_arg (dest, POINTER_TYPE))
12884 len = CALL_EXPR_ARG (exp, 1);
12885 if (!validate_arg (len, INTEGER_TYPE))
12887 flag = CALL_EXPR_ARG (exp, 2);
12888 if (!validate_arg (flag, INTEGER_TYPE))
12890 size = CALL_EXPR_ARG (exp, 3);
12891 if (!validate_arg (size, INTEGER_TYPE))
12893 fmt = CALL_EXPR_ARG (exp, 4);
12894 if (!validate_arg (fmt, POINTER_TYPE))
12897 if (! host_integerp (size, 1))
12900 if (! integer_all_onesp (size))
12902 if (! host_integerp (len, 1))
12904 /* If LEN is not constant, try MAXLEN too.
12905 For MAXLEN only allow optimizing into non-_ocs function
12906 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12907 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12913 if (tree_int_cst_lt (size, maxlen))
12917 if (!init_target_chars ())
12920 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12921 or if format doesn't contain % chars or is "%s". */
12922 if (! integer_zerop (flag))
12924 fmt_str = c_getstr (fmt);
12925 if (fmt_str == NULL)
12927 if (strchr (fmt_str, target_percent) != NULL
12928 && strcmp (fmt_str, target_percent_s))
12932 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12934 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12935 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12939 return rewrite_call_expr (exp, 5, fn, 3, dest, len, fmt);
12942 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12943 FMT and ARG are the arguments to the call; we don't fold cases with
12944 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12946 Return NULL_TREE if no simplification was possible, otherwise return the
12947 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12948 code of the function to be simplified. */
12951 fold_builtin_printf (tree fndecl, tree fmt, tree arg, bool ignore,
12952 enum built_in_function fcode)
12954 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12955 const char *fmt_str = NULL;
12957 /* If the return value is used, don't do the transformation. */
12961 /* Verify the required arguments in the original call. */
12962 if (!validate_arg (fmt, POINTER_TYPE))
12965 /* Check whether the format is a literal string constant. */
12966 fmt_str = c_getstr (fmt);
12967 if (fmt_str == NULL)
12970 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12972 /* If we're using an unlocked function, assume the other
12973 unlocked functions exist explicitly. */
12974 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12975 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12979 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12980 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12983 if (!init_target_chars ())
12986 if (strcmp (fmt_str, target_percent_s) == 0
12987 || strchr (fmt_str, target_percent) == NULL)
12991 if (strcmp (fmt_str, target_percent_s) == 0)
12993 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12996 if (!arg || !validate_arg (arg, POINTER_TYPE))
12999 str = c_getstr (arg);
13005 /* The format specifier doesn't contain any '%' characters. */
13006 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13012 /* If the string was "", printf does nothing. */
13013 if (str[0] == '\0')
13014 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13016 /* If the string has length of 1, call putchar. */
13017 if (str[1] == '\0')
13019 /* Given printf("c"), (where c is any one character,)
13020 convert "c"[0] to an int and pass that to the replacement
13022 newarg = build_int_cst (NULL_TREE, str[0]);
13024 call = build_call_expr (fn_putchar, 1, newarg);
13028 /* If the string was "string\n", call puts("string"). */
13029 size_t len = strlen (str);
13030 if ((unsigned char)str[len - 1] == target_newline)
13032 /* Create a NUL-terminated string that's one char shorter
13033 than the original, stripping off the trailing '\n'. */
13034 char *newstr = XALLOCAVEC (char, len);
13035 memcpy (newstr, str, len - 1);
13036 newstr[len - 1] = 0;
13038 newarg = build_string_literal (len, newstr);
13040 call = build_call_expr (fn_puts, 1, newarg);
13043 /* We'd like to arrange to call fputs(string,stdout) here,
13044 but we need stdout and don't have a way to get it yet. */
13049 /* The other optimizations can be done only on the non-va_list variants. */
13050 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13053 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13054 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13056 if (!arg || !validate_arg (arg, POINTER_TYPE))
13059 call = build_call_expr (fn_puts, 1, arg);
13062 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13063 else if (strcmp (fmt_str, target_percent_c) == 0)
13065 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13068 call = build_call_expr (fn_putchar, 1, arg);
13074 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
13077 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13078 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13079 more than 3 arguments, and ARG may be null in the 2-argument case.
13081 Return NULL_TREE if no simplification was possible, otherwise return the
13082 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13083 code of the function to be simplified. */
13086 fold_builtin_fprintf (tree fndecl, tree fp, tree fmt, tree arg, bool ignore,
13087 enum built_in_function fcode)
13089 tree fn_fputc, fn_fputs, call = NULL_TREE;
13090 const char *fmt_str = NULL;
13092 /* If the return value is used, don't do the transformation. */
13096 /* Verify the required arguments in the original call. */
13097 if (!validate_arg (fp, POINTER_TYPE))
13099 if (!validate_arg (fmt, POINTER_TYPE))
13102 /* Check whether the format is a literal string constant. */
13103 fmt_str = c_getstr (fmt);
13104 if (fmt_str == NULL)
13107 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13109 /* If we're using an unlocked function, assume the other
13110 unlocked functions exist explicitly. */
13111 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
13112 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
13116 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
13117 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
13120 if (!init_target_chars ())
13123 /* If the format doesn't contain % args or %%, use strcpy. */
13124 if (strchr (fmt_str, target_percent) == NULL)
13126 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13130 /* If the format specifier was "", fprintf does nothing. */
13131 if (fmt_str[0] == '\0')
13133 /* If FP has side-effects, just wait until gimplification is
13135 if (TREE_SIDE_EFFECTS (fp))
13138 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13141 /* When "string" doesn't contain %, replace all cases of
13142 fprintf (fp, string) with fputs (string, fp). The fputs
13143 builtin will take care of special cases like length == 1. */
13145 call = build_call_expr (fn_fputs, 2, fmt, fp);
13148 /* The other optimizations can be done only on the non-va_list variants. */
13149 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13152 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13153 else if (strcmp (fmt_str, target_percent_s) == 0)
13155 if (!arg || !validate_arg (arg, POINTER_TYPE))
13158 call = build_call_expr (fn_fputs, 2, arg, fp);
13161 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13162 else if (strcmp (fmt_str, target_percent_c) == 0)
13164 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13167 call = build_call_expr (fn_fputc, 2, arg, fp);
13172 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
13175 /* Initialize format string characters in the target charset. */
13178 init_target_chars (void)
13183 target_newline = lang_hooks.to_target_charset ('\n');
13184 target_percent = lang_hooks.to_target_charset ('%');
13185 target_c = lang_hooks.to_target_charset ('c');
13186 target_s = lang_hooks.to_target_charset ('s');
13187 if (target_newline == 0 || target_percent == 0 || target_c == 0
13191 target_percent_c[0] = target_percent;
13192 target_percent_c[1] = target_c;
13193 target_percent_c[2] = '\0';
13195 target_percent_s[0] = target_percent;
13196 target_percent_s[1] = target_s;
13197 target_percent_s[2] = '\0';
13199 target_percent_s_newline[0] = target_percent;
13200 target_percent_s_newline[1] = target_s;
13201 target_percent_s_newline[2] = target_newline;
13202 target_percent_s_newline[3] = '\0';
13209 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13210 and no overflow/underflow occurred. INEXACT is true if M was not
13211 exactly calculated. TYPE is the tree type for the result. This
13212 function assumes that you cleared the MPFR flags and then
13213 calculated M to see if anything subsequently set a flag prior to
13214 entering this function. Return NULL_TREE if any checks fail. */
13217 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13219 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13220 overflow/underflow occurred. If -frounding-math, proceed iff the
13221 result of calling FUNC was exact. */
13222 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13223 && (!flag_rounding_math || !inexact))
13225 REAL_VALUE_TYPE rr;
13227 real_from_mpfr (&rr, m, type, GMP_RNDN);
13228 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13229 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13230 but the mpft_t is not, then we underflowed in the
13232 if (real_isfinite (&rr)
13233 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13235 REAL_VALUE_TYPE rmode;
13237 real_convert (&rmode, TYPE_MODE (type), &rr);
13238 /* Proceed iff the specified mode can hold the value. */
13239 if (real_identical (&rmode, &rr))
13240 return build_real (type, rmode);
13247 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13248 number and no overflow/underflow occurred. INEXACT is true if M
13249 was not exactly calculated. TYPE is the tree type for the result.
13250 This function assumes that you cleared the MPFR flags and then
13251 calculated M to see if anything subsequently set a flag prior to
13252 entering this function. Return NULL_TREE if any checks fail. */
13255 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact)
13257 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13258 overflow/underflow occurred. If -frounding-math, proceed iff the
13259 result of calling FUNC was exact. */
13260 if (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13261 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13262 && (!flag_rounding_math || !inexact))
13264 REAL_VALUE_TYPE re, im;
13266 real_from_mpfr (&re, mpc_realref (m), type, GMP_RNDN);
13267 real_from_mpfr (&im, mpc_imagref (m), type, GMP_RNDN);
13268 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13269 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13270 but the mpft_t is not, then we underflowed in the
13272 if (real_isfinite (&re) && real_isfinite (&im)
13273 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13274 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0))
13276 REAL_VALUE_TYPE re_mode, im_mode;
13278 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13279 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13280 /* Proceed iff the specified mode can hold the value. */
13281 if (real_identical (&re_mode, &re) && real_identical (&im_mode, &im))
13282 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13283 build_real (TREE_TYPE (type), im_mode));
13288 #endif /* HAVE_mpc */
13290 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13291 FUNC on it and return the resulting value as a tree with type TYPE.
13292 If MIN and/or MAX are not NULL, then the supplied ARG must be
13293 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13294 acceptable values, otherwise they are not. The mpfr precision is
13295 set to the precision of TYPE. We assume that function FUNC returns
13296 zero if the result could be calculated exactly within the requested
13300 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13301 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13304 tree result = NULL_TREE;
13308 /* To proceed, MPFR must exactly represent the target floating point
13309 format, which only happens when the target base equals two. */
13310 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13311 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13313 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13315 if (real_isfinite (ra)
13316 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13317 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13319 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13320 const int prec = fmt->p;
13321 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13325 mpfr_init2 (m, prec);
13326 mpfr_from_real (m, ra, GMP_RNDN);
13327 mpfr_clear_flags ();
13328 inexact = func (m, m, rnd);
13329 result = do_mpfr_ckconv (m, type, inexact);
13337 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13338 FUNC on it and return the resulting value as a tree with type TYPE.
13339 The mpfr precision is set to the precision of TYPE. We assume that
13340 function FUNC returns zero if the result could be calculated
13341 exactly within the requested precision. */
13344 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13345 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13347 tree result = NULL_TREE;
13352 /* To proceed, MPFR must exactly represent the target floating point
13353 format, which only happens when the target base equals two. */
13354 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13355 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13356 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13358 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13359 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13361 if (real_isfinite (ra1) && real_isfinite (ra2))
13363 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13364 const int prec = fmt->p;
13365 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13369 mpfr_inits2 (prec, m1, m2, NULL);
13370 mpfr_from_real (m1, ra1, GMP_RNDN);
13371 mpfr_from_real (m2, ra2, GMP_RNDN);
13372 mpfr_clear_flags ();
13373 inexact = func (m1, m1, m2, rnd);
13374 result = do_mpfr_ckconv (m1, type, inexact);
13375 mpfr_clears (m1, m2, NULL);
13382 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13383 FUNC on it and return the resulting value as a tree with type TYPE.
13384 The mpfr precision is set to the precision of TYPE. We assume that
13385 function FUNC returns zero if the result could be calculated
13386 exactly within the requested precision. */
13389 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13390 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13392 tree result = NULL_TREE;
13398 /* To proceed, MPFR must exactly represent the target floating point
13399 format, which only happens when the target base equals two. */
13400 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13401 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13402 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13403 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13405 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13406 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13407 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13409 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13411 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13412 const int prec = fmt->p;
13413 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13417 mpfr_inits2 (prec, m1, m2, m3, NULL);
13418 mpfr_from_real (m1, ra1, GMP_RNDN);
13419 mpfr_from_real (m2, ra2, GMP_RNDN);
13420 mpfr_from_real (m3, ra3, GMP_RNDN);
13421 mpfr_clear_flags ();
13422 inexact = func (m1, m1, m2, m3, rnd);
13423 result = do_mpfr_ckconv (m1, type, inexact);
13424 mpfr_clears (m1, m2, m3, NULL);
13431 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13432 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13433 If ARG_SINP and ARG_COSP are NULL then the result is returned
13434 as a complex value.
13435 The type is taken from the type of ARG and is used for setting the
13436 precision of the calculation and results. */
13439 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13441 tree const type = TREE_TYPE (arg);
13442 tree result = NULL_TREE;
13446 /* To proceed, MPFR must exactly represent the target floating point
13447 format, which only happens when the target base equals two. */
13448 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13449 && TREE_CODE (arg) == REAL_CST
13450 && !TREE_OVERFLOW (arg))
13452 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13454 if (real_isfinite (ra))
13456 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13457 const int prec = fmt->p;
13458 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13459 tree result_s, result_c;
13463 mpfr_inits2 (prec, m, ms, mc, NULL);
13464 mpfr_from_real (m, ra, GMP_RNDN);
13465 mpfr_clear_flags ();
13466 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13467 result_s = do_mpfr_ckconv (ms, type, inexact);
13468 result_c = do_mpfr_ckconv (mc, type, inexact);
13469 mpfr_clears (m, ms, mc, NULL);
13470 if (result_s && result_c)
13472 /* If we are to return in a complex value do so. */
13473 if (!arg_sinp && !arg_cosp)
13474 return build_complex (build_complex_type (type),
13475 result_c, result_s);
13477 /* Dereference the sin/cos pointer arguments. */
13478 arg_sinp = build_fold_indirect_ref (arg_sinp);
13479 arg_cosp = build_fold_indirect_ref (arg_cosp);
13480 /* Proceed if valid pointer type were passed in. */
13481 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13482 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13484 /* Set the values. */
13485 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13487 TREE_SIDE_EFFECTS (result_s) = 1;
13488 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13490 TREE_SIDE_EFFECTS (result_c) = 1;
13491 /* Combine the assignments into a compound expr. */
13492 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13493 result_s, result_c));
13501 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13502 two-argument mpfr order N Bessel function FUNC on them and return
13503 the resulting value as a tree with type TYPE. The mpfr precision
13504 is set to the precision of TYPE. We assume that function FUNC
13505 returns zero if the result could be calculated exactly within the
13506 requested precision. */
13508 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13509 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13510 const REAL_VALUE_TYPE *min, bool inclusive)
13512 tree result = NULL_TREE;
13517 /* To proceed, MPFR must exactly represent the target floating point
13518 format, which only happens when the target base equals two. */
13519 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13520 && host_integerp (arg1, 0)
13521 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13523 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13524 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13527 && real_isfinite (ra)
13528 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13530 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13531 const int prec = fmt->p;
13532 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13536 mpfr_init2 (m, prec);
13537 mpfr_from_real (m, ra, GMP_RNDN);
13538 mpfr_clear_flags ();
13539 inexact = func (m, n, m, rnd);
13540 result = do_mpfr_ckconv (m, type, inexact);
13548 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13549 the pointer *(ARG_QUO) and return the result. The type is taken
13550 from the type of ARG0 and is used for setting the precision of the
13551 calculation and results. */
13554 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13556 tree const type = TREE_TYPE (arg0);
13557 tree result = NULL_TREE;
13562 /* To proceed, MPFR must exactly represent the target floating point
13563 format, which only happens when the target base equals two. */
13564 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13565 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13566 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13568 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13569 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13571 if (real_isfinite (ra0) && real_isfinite (ra1))
13573 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13574 const int prec = fmt->p;
13575 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13580 mpfr_inits2 (prec, m0, m1, NULL);
13581 mpfr_from_real (m0, ra0, GMP_RNDN);
13582 mpfr_from_real (m1, ra1, GMP_RNDN);
13583 mpfr_clear_flags ();
13584 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13585 /* Remquo is independent of the rounding mode, so pass
13586 inexact=0 to do_mpfr_ckconv(). */
13587 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13588 mpfr_clears (m0, m1, NULL);
13591 /* MPFR calculates quo in the host's long so it may
13592 return more bits in quo than the target int can hold
13593 if sizeof(host long) > sizeof(target int). This can
13594 happen even for native compilers in LP64 mode. In
13595 these cases, modulo the quo value with the largest
13596 number that the target int can hold while leaving one
13597 bit for the sign. */
13598 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13599 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13601 /* Dereference the quo pointer argument. */
13602 arg_quo = build_fold_indirect_ref (arg_quo);
13603 /* Proceed iff a valid pointer type was passed in. */
13604 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13606 /* Set the value. */
13607 tree result_quo = fold_build2 (MODIFY_EXPR,
13608 TREE_TYPE (arg_quo), arg_quo,
13609 build_int_cst (NULL, integer_quo));
13610 TREE_SIDE_EFFECTS (result_quo) = 1;
13611 /* Combine the quo assignment with the rem. */
13612 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13613 result_quo, result_rem));
13621 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13622 resulting value as a tree with type TYPE. The mpfr precision is
13623 set to the precision of TYPE. We assume that this mpfr function
13624 returns zero if the result could be calculated exactly within the
13625 requested precision. In addition, the integer pointer represented
13626 by ARG_SG will be dereferenced and set to the appropriate signgam
13630 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13632 tree result = NULL_TREE;
13636 /* To proceed, MPFR must exactly represent the target floating point
13637 format, which only happens when the target base equals two. Also
13638 verify ARG is a constant and that ARG_SG is an int pointer. */
13639 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13640 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13641 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13642 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13644 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13646 /* In addition to NaN and Inf, the argument cannot be zero or a
13647 negative integer. */
13648 if (real_isfinite (ra)
13649 && ra->cl != rvc_zero
13650 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13652 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13653 const int prec = fmt->p;
13654 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13659 mpfr_init2 (m, prec);
13660 mpfr_from_real (m, ra, GMP_RNDN);
13661 mpfr_clear_flags ();
13662 inexact = mpfr_lgamma (m, &sg, m, rnd);
13663 result_lg = do_mpfr_ckconv (m, type, inexact);
13669 /* Dereference the arg_sg pointer argument. */
13670 arg_sg = build_fold_indirect_ref (arg_sg);
13671 /* Assign the signgam value into *arg_sg. */
13672 result_sg = fold_build2 (MODIFY_EXPR,
13673 TREE_TYPE (arg_sg), arg_sg,
13674 build_int_cst (NULL, sg));
13675 TREE_SIDE_EFFECTS (result_sg) = 1;
13676 /* Combine the signgam assignment with the lgamma result. */
13677 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13678 result_sg, result_lg));
13687 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13688 function FUNC on it and return the resulting value as a tree with
13689 type TYPE. The mpfr precision is set to the precision of TYPE. We
13690 assume that function FUNC returns zero if the result could be
13691 calculated exactly within the requested precision. */
13694 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13696 tree result = NULL_TREE;
13700 /* To proceed, MPFR must exactly represent the target floating point
13701 format, which only happens when the target base equals two. */
13702 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13703 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13704 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13706 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13707 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13709 if (real_isfinite (re) && real_isfinite (im))
13711 const struct real_format *const fmt =
13712 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13713 const int prec = fmt->p;
13714 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13715 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13719 mpc_init2 (m, prec);
13720 mpfr_from_real (mpc_realref(m), re, rnd);
13721 mpfr_from_real (mpc_imagref(m), im, rnd);
13722 mpfr_clear_flags ();
13723 inexact = func (m, m, crnd);
13724 result = do_mpc_ckconv (m, type, inexact);
13732 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13733 mpc function FUNC on it and return the resulting value as a tree
13734 with type TYPE. The mpfr precision is set to the precision of
13735 TYPE. We assume that function FUNC returns zero if the result
13736 could be calculated exactly within the requested precision. */
13738 #ifdef HAVE_mpc_pow
13740 do_mpc_arg2 (tree arg0, tree arg1, tree type,
13741 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13743 tree result = NULL_TREE;
13748 /* To proceed, MPFR must exactly represent the target floating point
13749 format, which only happens when the target base equals two. */
13750 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13751 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13752 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13753 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13754 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13756 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13757 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13758 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13759 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13761 if (real_isfinite (re0) && real_isfinite (im0)
13762 && real_isfinite (re1) && real_isfinite (im1))
13764 const struct real_format *const fmt =
13765 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13766 const int prec = fmt->p;
13767 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13768 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13772 mpc_init2 (m0, prec);
13773 mpc_init2 (m1, prec);
13774 mpfr_from_real (mpc_realref(m0), re0, rnd);
13775 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13776 mpfr_from_real (mpc_realref(m1), re1, rnd);
13777 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13778 mpfr_clear_flags ();
13779 inexact = func (m0, m0, m1, crnd);
13780 result = do_mpc_ckconv (m0, type, inexact);
13789 #endif /* HAVE_mpc */
13792 The functions below provide an alternate interface for folding
13793 builtin function calls presented as GIMPLE_CALL statements rather
13794 than as CALL_EXPRs. The folded result is still expressed as a
13795 tree. There is too much code duplication in the handling of
13796 varargs functions, and a more intrusive re-factoring would permit
13797 better sharing of code between the tree and statement-based
13798 versions of these functions. */
13800 /* Construct a new CALL_EXPR using the tail of the argument list of STMT
13801 along with N new arguments specified as the "..." parameters. SKIP
13802 is the number of arguments in STMT to be omitted. This function is used
13803 to do varargs-to-varargs transformations. */
13806 gimple_rewrite_call_expr (gimple stmt, int skip, tree fndecl, int n, ...)
13808 int oldnargs = gimple_call_num_args (stmt);
13809 int nargs = oldnargs - skip + n;
13810 tree fntype = TREE_TYPE (fndecl);
13811 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
13816 buffer = XALLOCAVEC (tree, nargs);
13818 for (i = 0; i < n; i++)
13819 buffer[i] = va_arg (ap, tree);
13821 for (j = skip; j < oldnargs; j++, i++)
13822 buffer[i] = gimple_call_arg (stmt, j);
13824 return fold (build_call_array (TREE_TYPE (fntype), fn, nargs, buffer));
13827 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13828 a normal call should be emitted rather than expanding the function
13829 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13832 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13834 tree dest, size, len, fn, fmt, flag;
13835 const char *fmt_str;
13836 int nargs = gimple_call_num_args (stmt);
13838 /* Verify the required arguments in the original call. */
13841 dest = gimple_call_arg (stmt, 0);
13842 if (!validate_arg (dest, POINTER_TYPE))
13844 flag = gimple_call_arg (stmt, 1);
13845 if (!validate_arg (flag, INTEGER_TYPE))
13847 size = gimple_call_arg (stmt, 2);
13848 if (!validate_arg (size, INTEGER_TYPE))
13850 fmt = gimple_call_arg (stmt, 3);
13851 if (!validate_arg (fmt, POINTER_TYPE))
13854 if (! host_integerp (size, 1))
13859 if (!init_target_chars ())
13862 /* Check whether the format is a literal string constant. */
13863 fmt_str = c_getstr (fmt);
13864 if (fmt_str != NULL)
13866 /* If the format doesn't contain % args or %%, we know the size. */
13867 if (strchr (fmt_str, target_percent) == 0)
13869 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13870 len = build_int_cstu (size_type_node, strlen (fmt_str));
13872 /* If the format is "%s" and first ... argument is a string literal,
13873 we know the size too. */
13874 else if (fcode == BUILT_IN_SPRINTF_CHK
13875 && strcmp (fmt_str, target_percent_s) == 0)
13881 arg = gimple_call_arg (stmt, 4);
13882 if (validate_arg (arg, POINTER_TYPE))
13884 len = c_strlen (arg, 1);
13885 if (! len || ! host_integerp (len, 1))
13892 if (! integer_all_onesp (size))
13894 if (! len || ! tree_int_cst_lt (len, size))
13898 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13899 or if format doesn't contain % chars or is "%s". */
13900 if (! integer_zerop (flag))
13902 if (fmt_str == NULL)
13904 if (strchr (fmt_str, target_percent) != NULL
13905 && strcmp (fmt_str, target_percent_s))
13909 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13910 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
13911 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
13915 return gimple_rewrite_call_expr (stmt, 4, fn, 2, dest, fmt);
13918 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13919 a normal call should be emitted rather than expanding the function
13920 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13921 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13922 passed as second argument. */
13925 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13926 enum built_in_function fcode)
13928 tree dest, size, len, fn, fmt, flag;
13929 const char *fmt_str;
13931 /* Verify the required arguments in the original call. */
13932 if (gimple_call_num_args (stmt) < 5)
13934 dest = gimple_call_arg (stmt, 0);
13935 if (!validate_arg (dest, POINTER_TYPE))
13937 len = gimple_call_arg (stmt, 1);
13938 if (!validate_arg (len, INTEGER_TYPE))
13940 flag = gimple_call_arg (stmt, 2);
13941 if (!validate_arg (flag, INTEGER_TYPE))
13943 size = gimple_call_arg (stmt, 3);
13944 if (!validate_arg (size, INTEGER_TYPE))
13946 fmt = gimple_call_arg (stmt, 4);
13947 if (!validate_arg (fmt, POINTER_TYPE))
13950 if (! host_integerp (size, 1))
13953 if (! integer_all_onesp (size))
13955 if (! host_integerp (len, 1))
13957 /* If LEN is not constant, try MAXLEN too.
13958 For MAXLEN only allow optimizing into non-_ocs function
13959 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13960 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13966 if (tree_int_cst_lt (size, maxlen))
13970 if (!init_target_chars ())
13973 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13974 or if format doesn't contain % chars or is "%s". */
13975 if (! integer_zerop (flag))
13977 fmt_str = c_getstr (fmt);
13978 if (fmt_str == NULL)
13980 if (strchr (fmt_str, target_percent) != NULL
13981 && strcmp (fmt_str, target_percent_s))
13985 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13987 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
13988 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
13992 return gimple_rewrite_call_expr (stmt, 5, fn, 3, dest, len, fmt);
13995 /* Builtins with folding operations that operate on "..." arguments
13996 need special handling; we need to store the arguments in a convenient
13997 data structure before attempting any folding. Fortunately there are
13998 only a few builtins that fall into this category. FNDECL is the
13999 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14000 result of the function call is ignored. */
14003 gimple_fold_builtin_varargs (tree fndecl, gimple stmt, bool ignore ATTRIBUTE_UNUSED)
14005 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14006 tree ret = NULL_TREE;
14010 case BUILT_IN_SPRINTF_CHK:
14011 case BUILT_IN_VSPRINTF_CHK:
14012 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14015 case BUILT_IN_SNPRINTF_CHK:
14016 case BUILT_IN_VSNPRINTF_CHK:
14017 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14024 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14025 TREE_NO_WARNING (ret) = 1;
14031 /* A wrapper function for builtin folding that prevents warnings for
14032 "statement without effect" and the like, caused by removing the
14033 call node earlier than the warning is generated. */
14036 fold_call_stmt (gimple stmt, bool ignore)
14038 tree ret = NULL_TREE;
14039 tree fndecl = gimple_call_fndecl (stmt);
14041 && TREE_CODE (fndecl) == FUNCTION_DECL
14042 && DECL_BUILT_IN (fndecl)
14043 && !gimple_call_va_arg_pack_p (stmt))
14045 int nargs = gimple_call_num_args (stmt);
14047 if (avoid_folding_inline_builtin (fndecl))
14049 /* FIXME: Don't use a list in this interface. */
14050 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14052 tree arglist = NULL_TREE;
14054 for (i = nargs - 1; i >= 0; i--)
14055 arglist = tree_cons (NULL_TREE, gimple_call_arg (stmt, i), arglist);
14056 return targetm.fold_builtin (fndecl, arglist, ignore);
14060 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14062 tree args[MAX_ARGS_TO_FOLD_BUILTIN];
14064 for (i = 0; i < nargs; i++)
14065 args[i] = gimple_call_arg (stmt, i);
14066 ret = fold_builtin_n (fndecl, args, nargs, ignore);
14069 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14072 /* Propagate location information from original call to
14073 expansion of builtin. Otherwise things like
14074 maybe_emit_chk_warning, that operate on the expansion
14075 of a builtin, will use the wrong location information. */
14076 if (gimple_has_location (stmt))
14078 tree realret = ret;
14079 if (TREE_CODE (ret) == NOP_EXPR)
14080 realret = TREE_OPERAND (ret, 0);
14081 if (CAN_HAVE_LOCATION_P (realret)
14082 && !EXPR_HAS_LOCATION (realret))
14083 SET_EXPR_LOCATION (realret, gimple_location (stmt));