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 /* Define the names of the builtin function types and codes. */
63 const char *const built_in_class_names[4]
64 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
66 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
67 const char * built_in_names[(int) END_BUILTINS] =
69 #include "builtins.def"
73 /* Setup an array of _DECL trees, make sure each element is
74 initialized to NULL_TREE. */
75 tree built_in_decls[(int) END_BUILTINS];
76 /* Declarations used when constructing the builtin implicitly in the compiler.
77 It may be NULL_TREE when this is invalid (for instance runtime is not
78 required to implement the function call in all cases). */
79 tree implicit_built_in_decls[(int) END_BUILTINS];
81 static const char *c_getstr (tree);
82 static rtx c_readstr (const char *, enum machine_mode);
83 static int target_char_cast (tree, char *);
84 static rtx get_memory_rtx (tree, tree);
85 static int apply_args_size (void);
86 static int apply_result_size (void);
87 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
88 static rtx result_vector (int, rtx);
90 static void expand_builtin_update_setjmp_buf (rtx);
91 static void expand_builtin_prefetch (tree);
92 static rtx expand_builtin_apply_args (void);
93 static rtx expand_builtin_apply_args_1 (void);
94 static rtx expand_builtin_apply (rtx, rtx, rtx);
95 static void expand_builtin_return (rtx);
96 static enum type_class type_to_class (tree);
97 static rtx expand_builtin_classify_type (tree);
98 static void expand_errno_check (tree, rtx);
99 static rtx expand_builtin_mathfn (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_args_info (tree);
108 static rtx expand_builtin_next_arg (void);
109 static rtx expand_builtin_va_start (tree);
110 static rtx expand_builtin_va_end (tree);
111 static rtx expand_builtin_va_copy (tree);
112 static rtx expand_builtin_memchr (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
116 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
117 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
118 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
120 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
123 static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx,
124 enum machine_mode, int);
125 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int);
126 static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx,
127 enum machine_mode, int);
128 static rtx expand_builtin_bcopy (tree, int);
129 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
133 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
134 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
136 static rtx expand_builtin_bzero (tree);
137 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
139 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
140 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
141 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
142 static rtx expand_builtin_alloca (tree, rtx);
143 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
144 static rtx expand_builtin_frame_address (tree, tree);
145 static rtx expand_builtin_fputs (tree, rtx, bool);
146 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
147 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
148 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
149 static tree stabilize_va_list (tree, int);
150 static rtx expand_builtin_expect (tree, rtx);
151 static tree fold_builtin_constant_p (tree);
152 static tree fold_builtin_expect (tree, tree);
153 static tree fold_builtin_classify_type (tree);
154 static tree fold_builtin_strlen (tree);
155 static tree fold_builtin_inf (tree, int);
156 static tree fold_builtin_nan (tree, tree, int);
157 static tree rewrite_call_expr (tree, int, tree, int, ...);
158 static bool validate_arg (const_tree, enum tree_code code);
159 static bool integer_valued_real_p (tree);
160 static tree fold_trunc_transparent_mathfn (tree, tree);
161 static bool readonly_data_expr (tree);
162 static rtx expand_builtin_fabs (tree, rtx, rtx);
163 static rtx expand_builtin_signbit (tree, rtx);
164 static tree fold_builtin_sqrt (tree, tree);
165 static tree fold_builtin_cbrt (tree, tree);
166 static tree fold_builtin_pow (tree, tree, tree, tree);
167 static tree fold_builtin_powi (tree, tree, tree, tree);
168 static tree fold_builtin_cos (tree, tree, tree);
169 static tree fold_builtin_cosh (tree, tree, tree);
170 static tree fold_builtin_tan (tree, tree);
171 static tree fold_builtin_trunc (tree, tree);
172 static tree fold_builtin_floor (tree, tree);
173 static tree fold_builtin_ceil (tree, tree);
174 static tree fold_builtin_round (tree, tree);
175 static tree fold_builtin_int_roundingfn (tree, tree);
176 static tree fold_builtin_bitop (tree, tree);
177 static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int);
178 static tree fold_builtin_strchr (tree, tree, tree);
179 static tree fold_builtin_memchr (tree, tree, tree, tree);
180 static tree fold_builtin_memcmp (tree, tree, tree);
181 static tree fold_builtin_strcmp (tree, tree);
182 static tree fold_builtin_strncmp (tree, tree, tree);
183 static tree fold_builtin_signbit (tree, tree);
184 static tree fold_builtin_copysign (tree, tree, tree, tree);
185 static tree fold_builtin_isascii (tree);
186 static tree fold_builtin_toascii (tree);
187 static tree fold_builtin_isdigit (tree);
188 static tree fold_builtin_fabs (tree, tree);
189 static tree fold_builtin_abs (tree, tree);
190 static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code,
192 static tree fold_builtin_n (tree, tree *, int, bool);
193 static tree fold_builtin_0 (tree, bool);
194 static tree fold_builtin_1 (tree, tree, bool);
195 static tree fold_builtin_2 (tree, tree, tree, bool);
196 static tree fold_builtin_3 (tree, tree, tree, tree, bool);
197 static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool);
198 static tree fold_builtin_varargs (tree, tree, bool);
200 static tree fold_builtin_strpbrk (tree, tree, tree);
201 static tree fold_builtin_strstr (tree, tree, tree);
202 static tree fold_builtin_strrchr (tree, tree, tree);
203 static tree fold_builtin_strcat (tree, tree);
204 static tree fold_builtin_strncat (tree, tree, tree);
205 static tree fold_builtin_strspn (tree, tree);
206 static tree fold_builtin_strcspn (tree, tree);
207 static tree fold_builtin_sprintf (tree, tree, tree, int);
209 static rtx expand_builtin_object_size (tree);
210 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
211 enum built_in_function);
212 static void maybe_emit_chk_warning (tree, enum built_in_function);
213 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
214 static void maybe_emit_free_warning (tree);
215 static tree fold_builtin_object_size (tree, tree);
216 static tree fold_builtin_strcat_chk (tree, tree, tree, tree);
217 static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree);
218 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
219 static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function);
220 static tree fold_builtin_fprintf (tree, tree, tree, tree, bool,
221 enum built_in_function);
222 static bool init_target_chars (void);
224 static unsigned HOST_WIDE_INT target_newline;
225 static unsigned HOST_WIDE_INT target_percent;
226 static unsigned HOST_WIDE_INT target_c;
227 static unsigned HOST_WIDE_INT target_s;
228 static char target_percent_c[3];
229 static char target_percent_s[3];
230 static char target_percent_s_newline[4];
231 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
232 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
233 static tree do_mpfr_arg2 (tree, tree, tree,
234 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
235 static tree do_mpfr_arg3 (tree, tree, tree, tree,
236 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
237 static tree do_mpfr_sincos (tree, tree, tree);
238 static tree do_mpfr_bessel_n (tree, tree, tree,
239 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
240 const REAL_VALUE_TYPE *, bool);
241 static tree do_mpfr_remquo (tree, tree, tree);
242 static tree do_mpfr_lgamma_r (tree, tree, tree);
244 /* Return true if NODE should be considered for inline expansion regardless
245 of the optimization level. This means whenever a function is invoked with
246 its "internal" name, which normally contains the prefix "__builtin". */
248 static bool called_as_built_in (tree node)
250 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
251 if (strncmp (name, "__builtin_", 10) == 0)
253 if (strncmp (name, "__sync_", 7) == 0)
258 /* Return the alignment in bits of EXP, an object.
259 Don't return more than MAX_ALIGN no matter what, ALIGN is the inital
260 guessed alignment e.g. from type alignment. */
263 get_object_alignment (tree exp, unsigned int align, unsigned int max_align)
268 if (handled_component_p (exp))
270 HOST_WIDE_INT bitsize, bitpos;
272 enum machine_mode mode;
273 int unsignedp, volatilep;
275 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
276 &mode, &unsignedp, &volatilep, true);
278 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
283 if (TREE_CODE (offset) == PLUS_EXPR)
285 next_offset = TREE_OPERAND (offset, 0);
286 offset = TREE_OPERAND (offset, 1);
290 if (host_integerp (offset, 1))
292 /* Any overflow in calculating offset_bits won't change
295 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
298 inner = MIN (inner, (offset_bits & -offset_bits));
300 else if (TREE_CODE (offset) == MULT_EXPR
301 && host_integerp (TREE_OPERAND (offset, 1), 1))
303 /* Any overflow in calculating offset_factor won't change
305 unsigned offset_factor
306 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
310 inner = MIN (inner, (offset_factor & -offset_factor));
314 inner = MIN (inner, BITS_PER_UNIT);
317 offset = next_offset;
321 align = MIN (inner, DECL_ALIGN (exp));
322 #ifdef CONSTANT_ALIGNMENT
323 else if (CONSTANT_CLASS_P (exp))
324 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
326 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
327 || TREE_CODE (exp) == INDIRECT_REF)
328 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
330 align = MIN (align, inner);
331 return MIN (align, max_align);
334 /* Return the alignment in bits of EXP, a pointer valued expression.
335 But don't return more than MAX_ALIGN no matter what.
336 The alignment returned is, by default, the alignment of the thing that
337 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
339 Otherwise, look at the expression to see if we can do better, i.e., if the
340 expression is actually pointing at an object whose alignment is tighter. */
343 get_pointer_alignment (tree exp, unsigned int max_align)
345 unsigned int align, inner;
347 /* We rely on TER to compute accurate alignment information. */
348 if (!(optimize && flag_tree_ter))
351 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
354 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
355 align = MIN (align, max_align);
359 switch (TREE_CODE (exp))
362 exp = TREE_OPERAND (exp, 0);
363 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
366 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
367 align = MIN (inner, max_align);
370 case POINTER_PLUS_EXPR:
371 /* If sum of pointer + int, restrict our maximum alignment to that
372 imposed by the integer. If not, we can't do any better than
374 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
377 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
378 & (max_align / BITS_PER_UNIT - 1))
382 exp = TREE_OPERAND (exp, 0);
386 /* See what we are pointing at and look at its alignment. */
387 return get_object_alignment (TREE_OPERAND (exp, 0), align, max_align);
395 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
396 way, because it could contain a zero byte in the middle.
397 TREE_STRING_LENGTH is the size of the character array, not the string.
399 ONLY_VALUE should be nonzero if the result is not going to be emitted
400 into the instruction stream and zero if it is going to be expanded.
401 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
402 is returned, otherwise NULL, since
403 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
404 evaluate the side-effects.
406 The value returned is of type `ssizetype'.
408 Unfortunately, string_constant can't access the values of const char
409 arrays with initializers, so neither can we do so here. */
412 c_strlen (tree src, int only_value)
415 HOST_WIDE_INT offset;
420 if (TREE_CODE (src) == COND_EXPR
421 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
425 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
426 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
427 if (tree_int_cst_equal (len1, len2))
431 if (TREE_CODE (src) == COMPOUND_EXPR
432 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
433 return c_strlen (TREE_OPERAND (src, 1), only_value);
435 src = string_constant (src, &offset_node);
439 max = TREE_STRING_LENGTH (src) - 1;
440 ptr = TREE_STRING_POINTER (src);
442 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
444 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
445 compute the offset to the following null if we don't know where to
446 start searching for it. */
449 for (i = 0; i < max; i++)
453 /* We don't know the starting offset, but we do know that the string
454 has no internal zero bytes. We can assume that the offset falls
455 within the bounds of the string; otherwise, the programmer deserves
456 what he gets. Subtract the offset from the length of the string,
457 and return that. This would perhaps not be valid if we were dealing
458 with named arrays in addition to literal string constants. */
460 return size_diffop (size_int (max), offset_node);
463 /* We have a known offset into the string. Start searching there for
464 a null character if we can represent it as a single HOST_WIDE_INT. */
465 if (offset_node == 0)
467 else if (! host_integerp (offset_node, 0))
470 offset = tree_low_cst (offset_node, 0);
472 /* If the offset is known to be out of bounds, warn, and call strlen at
474 if (offset < 0 || offset > max)
476 /* Suppress multiple warnings for propagated constant strings. */
477 if (! TREE_NO_WARNING (src))
479 warning (0, "offset outside bounds of constant string");
480 TREE_NO_WARNING (src) = 1;
485 /* Use strlen to search for the first zero byte. Since any strings
486 constructed with build_string will have nulls appended, we win even
487 if we get handed something like (char[4])"abcd".
489 Since OFFSET is our starting index into the string, no further
490 calculation is needed. */
491 return ssize_int (strlen (ptr + offset));
494 /* Return a char pointer for a C string if it is a string constant
495 or sum of string constant and integer constant. */
502 src = string_constant (src, &offset_node);
506 if (offset_node == 0)
507 return TREE_STRING_POINTER (src);
508 else if (!host_integerp (offset_node, 1)
509 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
512 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
515 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
516 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
519 c_readstr (const char *str, enum machine_mode mode)
525 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
530 for (i = 0; i < GET_MODE_SIZE (mode); i++)
533 if (WORDS_BIG_ENDIAN)
534 j = GET_MODE_SIZE (mode) - i - 1;
535 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
536 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
537 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
539 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
542 ch = (unsigned char) str[i];
543 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
545 return immed_double_const (c[0], c[1], mode);
548 /* Cast a target constant CST to target CHAR and if that value fits into
549 host char type, return zero and put that value into variable pointed to by
553 target_char_cast (tree cst, char *p)
555 unsigned HOST_WIDE_INT val, hostval;
557 if (!host_integerp (cst, 1)
558 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
561 val = tree_low_cst (cst, 1);
562 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
563 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
566 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
567 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
576 /* Similar to save_expr, but assumes that arbitrary code is not executed
577 in between the multiple evaluations. In particular, we assume that a
578 non-addressable local variable will not be modified. */
581 builtin_save_expr (tree exp)
583 if (TREE_ADDRESSABLE (exp) == 0
584 && (TREE_CODE (exp) == PARM_DECL
585 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
588 return save_expr (exp);
591 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
592 times to get the address of either a higher stack frame, or a return
593 address located within it (depending on FNDECL_CODE). */
596 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
600 #ifdef INITIAL_FRAME_ADDRESS_RTX
601 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
605 /* For a zero count with __builtin_return_address, we don't care what
606 frame address we return, because target-specific definitions will
607 override us. Therefore frame pointer elimination is OK, and using
608 the soft frame pointer is OK.
610 For a nonzero count, or a zero count with __builtin_frame_address,
611 we require a stable offset from the current frame pointer to the
612 previous one, so we must use the hard frame pointer, and
613 we must disable frame pointer elimination. */
614 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
615 tem = frame_pointer_rtx;
618 tem = hard_frame_pointer_rtx;
620 /* Tell reload not to eliminate the frame pointer. */
621 crtl->accesses_prior_frames = 1;
625 /* Some machines need special handling before we can access
626 arbitrary frames. For example, on the SPARC, we must first flush
627 all register windows to the stack. */
628 #ifdef SETUP_FRAME_ADDRESSES
630 SETUP_FRAME_ADDRESSES ();
633 /* On the SPARC, the return address is not in the frame, it is in a
634 register. There is no way to access it off of the current frame
635 pointer, but it can be accessed off the previous frame pointer by
636 reading the value from the register window save area. */
637 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
638 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
642 /* Scan back COUNT frames to the specified frame. */
643 for (i = 0; i < count; i++)
645 /* Assume the dynamic chain pointer is in the word that the
646 frame address points to, unless otherwise specified. */
647 #ifdef DYNAMIC_CHAIN_ADDRESS
648 tem = DYNAMIC_CHAIN_ADDRESS (tem);
650 tem = memory_address (Pmode, tem);
651 tem = gen_frame_mem (Pmode, tem);
652 tem = copy_to_reg (tem);
655 /* For __builtin_frame_address, return what we've got. But, on
656 the SPARC for example, we may have to add a bias. */
657 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
658 #ifdef FRAME_ADDR_RTX
659 return FRAME_ADDR_RTX (tem);
664 /* For __builtin_return_address, get the return address from that frame. */
665 #ifdef RETURN_ADDR_RTX
666 tem = RETURN_ADDR_RTX (count, tem);
668 tem = memory_address (Pmode,
669 plus_constant (tem, GET_MODE_SIZE (Pmode)));
670 tem = gen_frame_mem (Pmode, tem);
675 /* Alias set used for setjmp buffer. */
676 static alias_set_type setjmp_alias_set = -1;
678 /* Construct the leading half of a __builtin_setjmp call. Control will
679 return to RECEIVER_LABEL. This is also called directly by the SJLJ
680 exception handling code. */
683 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
685 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
689 if (setjmp_alias_set == -1)
690 setjmp_alias_set = new_alias_set ();
692 buf_addr = convert_memory_address (Pmode, buf_addr);
694 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
696 /* We store the frame pointer and the address of receiver_label in
697 the buffer and use the rest of it for the stack save area, which
698 is machine-dependent. */
700 mem = gen_rtx_MEM (Pmode, buf_addr);
701 set_mem_alias_set (mem, setjmp_alias_set);
702 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
704 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
705 set_mem_alias_set (mem, setjmp_alias_set);
707 emit_move_insn (validize_mem (mem),
708 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
710 stack_save = gen_rtx_MEM (sa_mode,
711 plus_constant (buf_addr,
712 2 * GET_MODE_SIZE (Pmode)));
713 set_mem_alias_set (stack_save, setjmp_alias_set);
714 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
716 /* If there is further processing to do, do it. */
717 #ifdef HAVE_builtin_setjmp_setup
718 if (HAVE_builtin_setjmp_setup)
719 emit_insn (gen_builtin_setjmp_setup (buf_addr));
722 /* Tell optimize_save_area_alloca that extra work is going to
723 need to go on during alloca. */
724 cfun->calls_setjmp = 1;
726 /* We have a nonlocal label. */
727 cfun->has_nonlocal_label = 1;
730 /* Construct the trailing part of a __builtin_setjmp call. This is
731 also called directly by the SJLJ exception handling code. */
734 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
736 /* Clobber the FP when we get here, so we have to make sure it's
737 marked as used by this function. */
738 emit_use (hard_frame_pointer_rtx);
740 /* Mark the static chain as clobbered here so life information
741 doesn't get messed up for it. */
742 emit_clobber (static_chain_rtx);
744 /* Now put in the code to restore the frame pointer, and argument
745 pointer, if needed. */
746 #ifdef HAVE_nonlocal_goto
747 if (! HAVE_nonlocal_goto)
750 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
751 /* This might change the hard frame pointer in ways that aren't
752 apparent to early optimization passes, so force a clobber. */
753 emit_clobber (hard_frame_pointer_rtx);
756 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
757 if (fixed_regs[ARG_POINTER_REGNUM])
759 #ifdef ELIMINABLE_REGS
761 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
763 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
764 if (elim_regs[i].from == ARG_POINTER_REGNUM
765 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
768 if (i == ARRAY_SIZE (elim_regs))
771 /* Now restore our arg pointer from the address at which it
772 was saved in our stack frame. */
773 emit_move_insn (crtl->args.internal_arg_pointer,
774 copy_to_reg (get_arg_pointer_save_area ()));
779 #ifdef HAVE_builtin_setjmp_receiver
780 if (HAVE_builtin_setjmp_receiver)
781 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
784 #ifdef HAVE_nonlocal_goto_receiver
785 if (HAVE_nonlocal_goto_receiver)
786 emit_insn (gen_nonlocal_goto_receiver ());
791 /* We must not allow the code we just generated to be reordered by
792 scheduling. Specifically, the update of the frame pointer must
793 happen immediately, not later. */
794 emit_insn (gen_blockage ());
797 /* __builtin_longjmp is passed a pointer to an array of five words (not
798 all will be used on all machines). It operates similarly to the C
799 library function of the same name, but is more efficient. Much of
800 the code below is copied from the handling of non-local gotos. */
803 expand_builtin_longjmp (rtx buf_addr, rtx value)
805 rtx fp, lab, stack, insn, last;
806 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
808 /* DRAP is needed for stack realign if longjmp is expanded to current
810 if (SUPPORTS_STACK_ALIGNMENT)
811 crtl->need_drap = true;
813 if (setjmp_alias_set == -1)
814 setjmp_alias_set = new_alias_set ();
816 buf_addr = convert_memory_address (Pmode, buf_addr);
818 buf_addr = force_reg (Pmode, buf_addr);
820 /* We used to store value in static_chain_rtx, but that fails if pointers
821 are smaller than integers. We instead require that the user must pass
822 a second argument of 1, because that is what builtin_setjmp will
823 return. This also makes EH slightly more efficient, since we are no
824 longer copying around a value that we don't care about. */
825 gcc_assert (value == const1_rtx);
827 last = get_last_insn ();
828 #ifdef HAVE_builtin_longjmp
829 if (HAVE_builtin_longjmp)
830 emit_insn (gen_builtin_longjmp (buf_addr));
834 fp = gen_rtx_MEM (Pmode, buf_addr);
835 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
836 GET_MODE_SIZE (Pmode)));
838 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
839 2 * GET_MODE_SIZE (Pmode)));
840 set_mem_alias_set (fp, setjmp_alias_set);
841 set_mem_alias_set (lab, setjmp_alias_set);
842 set_mem_alias_set (stack, setjmp_alias_set);
844 /* Pick up FP, label, and SP from the block and jump. This code is
845 from expand_goto in stmt.c; see there for detailed comments. */
846 #ifdef HAVE_nonlocal_goto
847 if (HAVE_nonlocal_goto)
848 /* We have to pass a value to the nonlocal_goto pattern that will
849 get copied into the static_chain pointer, but it does not matter
850 what that value is, because builtin_setjmp does not use it. */
851 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
855 lab = copy_to_reg (lab);
857 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
858 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
860 emit_move_insn (hard_frame_pointer_rtx, fp);
861 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
863 emit_use (hard_frame_pointer_rtx);
864 emit_use (stack_pointer_rtx);
865 emit_indirect_jump (lab);
869 /* Search backwards and mark the jump insn as a non-local goto.
870 Note that this precludes the use of __builtin_longjmp to a
871 __builtin_setjmp target in the same function. However, we've
872 already cautioned the user that these functions are for
873 internal exception handling use only. */
874 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
876 gcc_assert (insn != last);
880 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
883 else if (CALL_P (insn))
888 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
889 and the address of the save area. */
892 expand_builtin_nonlocal_goto (tree exp)
894 tree t_label, t_save_area;
895 rtx r_label, r_save_area, r_fp, r_sp, insn;
897 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
900 t_label = CALL_EXPR_ARG (exp, 0);
901 t_save_area = CALL_EXPR_ARG (exp, 1);
903 r_label = expand_normal (t_label);
904 r_label = convert_memory_address (Pmode, r_label);
905 r_save_area = expand_normal (t_save_area);
906 r_save_area = convert_memory_address (Pmode, r_save_area);
907 /* Copy the address of the save location to a register just in case it was based
908 on the frame pointer. */
909 r_save_area = copy_to_reg (r_save_area);
910 r_fp = gen_rtx_MEM (Pmode, r_save_area);
911 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
912 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
914 crtl->has_nonlocal_goto = 1;
916 #ifdef HAVE_nonlocal_goto
917 /* ??? We no longer need to pass the static chain value, afaik. */
918 if (HAVE_nonlocal_goto)
919 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
923 r_label = copy_to_reg (r_label);
925 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
926 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
928 /* Restore frame pointer for containing function.
929 This sets the actual hard register used for the frame pointer
930 to the location of the function's incoming static chain info.
931 The non-local goto handler will then adjust it to contain the
932 proper value and reload the argument pointer, if needed. */
933 emit_move_insn (hard_frame_pointer_rtx, r_fp);
934 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
936 /* USE of hard_frame_pointer_rtx added for consistency;
937 not clear if really needed. */
938 emit_use (hard_frame_pointer_rtx);
939 emit_use (stack_pointer_rtx);
941 /* If the architecture is using a GP register, we must
942 conservatively assume that the target function makes use of it.
943 The prologue of functions with nonlocal gotos must therefore
944 initialize the GP register to the appropriate value, and we
945 must then make sure that this value is live at the point
946 of the jump. (Note that this doesn't necessarily apply
947 to targets with a nonlocal_goto pattern; they are free
948 to implement it in their own way. Note also that this is
949 a no-op if the GP register is a global invariant.) */
950 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
951 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
952 emit_use (pic_offset_table_rtx);
954 emit_indirect_jump (r_label);
957 /* Search backwards to the jump insn and mark it as a
959 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
963 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
966 else if (CALL_P (insn))
973 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
974 (not all will be used on all machines) that was passed to __builtin_setjmp.
975 It updates the stack pointer in that block to correspond to the current
979 expand_builtin_update_setjmp_buf (rtx buf_addr)
981 enum machine_mode sa_mode = Pmode;
985 #ifdef HAVE_save_stack_nonlocal
986 if (HAVE_save_stack_nonlocal)
987 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
989 #ifdef STACK_SAVEAREA_MODE
990 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
994 = gen_rtx_MEM (sa_mode,
997 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1001 emit_insn (gen_setjmp ());
1004 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1007 /* Expand a call to __builtin_prefetch. For a target that does not support
1008 data prefetch, evaluate the memory address argument in case it has side
1012 expand_builtin_prefetch (tree exp)
1014 tree arg0, arg1, arg2;
1018 if (!validate_arglist (exp, POINTER_TYPE, 0))
1021 arg0 = CALL_EXPR_ARG (exp, 0);
1023 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1024 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1026 nargs = call_expr_nargs (exp);
1028 arg1 = CALL_EXPR_ARG (exp, 1);
1030 arg1 = integer_zero_node;
1032 arg2 = CALL_EXPR_ARG (exp, 2);
1034 arg2 = build_int_cst (NULL_TREE, 3);
1036 /* Argument 0 is an address. */
1037 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1039 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1040 if (TREE_CODE (arg1) != INTEGER_CST)
1042 error ("second argument to %<__builtin_prefetch%> must be a constant");
1043 arg1 = integer_zero_node;
1045 op1 = expand_normal (arg1);
1046 /* Argument 1 must be either zero or one. */
1047 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1049 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1054 /* Argument 2 (locality) must be a compile-time constant int. */
1055 if (TREE_CODE (arg2) != INTEGER_CST)
1057 error ("third argument to %<__builtin_prefetch%> must be a constant");
1058 arg2 = integer_zero_node;
1060 op2 = expand_normal (arg2);
1061 /* Argument 2 must be 0, 1, 2, or 3. */
1062 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1064 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1068 #ifdef HAVE_prefetch
1071 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1073 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1074 || (GET_MODE (op0) != Pmode))
1076 op0 = convert_memory_address (Pmode, op0);
1077 op0 = force_reg (Pmode, op0);
1079 emit_insn (gen_prefetch (op0, op1, op2));
1083 /* Don't do anything with direct references to volatile memory, but
1084 generate code to handle other side effects. */
1085 if (!MEM_P (op0) && side_effects_p (op0))
1089 /* Get a MEM rtx for expression EXP which is the address of an operand
1090 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1091 the maximum length of the block of memory that might be accessed or
1095 get_memory_rtx (tree exp, tree len)
1097 tree orig_exp = exp;
1101 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1102 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1103 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1104 exp = TREE_OPERAND (exp, 0);
1106 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1107 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1109 /* Get an expression we can use to find the attributes to assign to MEM.
1110 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1111 we can. First remove any nops. */
1112 while (CONVERT_EXPR_P (exp)
1113 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1114 exp = TREE_OPERAND (exp, 0);
1117 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1118 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1119 && host_integerp (TREE_OPERAND (exp, 1), 0)
1120 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1121 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1122 else if (TREE_CODE (exp) == ADDR_EXPR)
1123 exp = TREE_OPERAND (exp, 0);
1124 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1125 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1129 /* Honor attributes derived from exp, except for the alias set
1130 (as builtin stringops may alias with anything) and the size
1131 (as stringops may access multiple array elements). */
1134 set_mem_attributes (mem, exp, 0);
1137 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1139 /* Allow the string and memory builtins to overflow from one
1140 field into another, see http://gcc.gnu.org/PR23561.
1141 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1142 memory accessed by the string or memory builtin will fit
1143 within the field. */
1144 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1146 tree mem_expr = MEM_EXPR (mem);
1147 HOST_WIDE_INT offset = -1, length = -1;
1150 while (TREE_CODE (inner) == ARRAY_REF
1151 || CONVERT_EXPR_P (inner)
1152 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1153 || TREE_CODE (inner) == SAVE_EXPR)
1154 inner = TREE_OPERAND (inner, 0);
1156 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1158 if (MEM_OFFSET (mem)
1159 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1160 offset = INTVAL (MEM_OFFSET (mem));
1162 if (offset >= 0 && len && host_integerp (len, 0))
1163 length = tree_low_cst (len, 0);
1165 while (TREE_CODE (inner) == COMPONENT_REF)
1167 tree field = TREE_OPERAND (inner, 1);
1168 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1169 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1171 /* Bitfields are generally not byte-addressable. */
1172 gcc_assert (!DECL_BIT_FIELD (field)
1173 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1174 % BITS_PER_UNIT) == 0
1175 && host_integerp (DECL_SIZE (field), 0)
1176 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1177 % BITS_PER_UNIT) == 0));
1179 /* If we can prove that the memory starting at XEXP (mem, 0) and
1180 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1181 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1182 fields without DECL_SIZE_UNIT like flexible array members. */
1184 && DECL_SIZE_UNIT (field)
1185 && host_integerp (DECL_SIZE_UNIT (field), 0))
1188 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1191 && offset + length <= size)
1196 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1197 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1198 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1206 mem_expr = TREE_OPERAND (mem_expr, 0);
1207 inner = TREE_OPERAND (inner, 0);
1210 if (mem_expr == NULL)
1212 if (mem_expr != MEM_EXPR (mem))
1214 set_mem_expr (mem, mem_expr);
1215 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1218 set_mem_alias_set (mem, 0);
1219 set_mem_size (mem, NULL_RTX);
1225 /* Built-in functions to perform an untyped call and return. */
1227 /* For each register that may be used for calling a function, this
1228 gives a mode used to copy the register's value. VOIDmode indicates
1229 the register is not used for calling a function. If the machine
1230 has register windows, this gives only the outbound registers.
1231 INCOMING_REGNO gives the corresponding inbound register. */
1232 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1234 /* For each register that may be used for returning values, this gives
1235 a mode used to copy the register's value. VOIDmode indicates the
1236 register is not used for returning values. If the machine has
1237 register windows, this gives only the outbound registers.
1238 INCOMING_REGNO gives the corresponding inbound register. */
1239 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1241 /* For each register that may be used for calling a function, this
1242 gives the offset of that register into the block returned by
1243 __builtin_apply_args. 0 indicates that the register is not
1244 used for calling a function. */
1245 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1247 /* Return the size required for the block returned by __builtin_apply_args,
1248 and initialize apply_args_mode. */
1251 apply_args_size (void)
1253 static int size = -1;
1256 enum machine_mode mode;
1258 /* The values computed by this function never change. */
1261 /* The first value is the incoming arg-pointer. */
1262 size = GET_MODE_SIZE (Pmode);
1264 /* The second value is the structure value address unless this is
1265 passed as an "invisible" first argument. */
1266 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1267 size += GET_MODE_SIZE (Pmode);
1269 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1270 if (FUNCTION_ARG_REGNO_P (regno))
1272 mode = reg_raw_mode[regno];
1274 gcc_assert (mode != VOIDmode);
1276 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1277 if (size % align != 0)
1278 size = CEIL (size, align) * align;
1279 apply_args_reg_offset[regno] = size;
1280 size += GET_MODE_SIZE (mode);
1281 apply_args_mode[regno] = mode;
1285 apply_args_mode[regno] = VOIDmode;
1286 apply_args_reg_offset[regno] = 0;
1292 /* Return the size required for the block returned by __builtin_apply,
1293 and initialize apply_result_mode. */
1296 apply_result_size (void)
1298 static int size = -1;
1300 enum machine_mode mode;
1302 /* The values computed by this function never change. */
1307 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1308 if (FUNCTION_VALUE_REGNO_P (regno))
1310 mode = reg_raw_mode[regno];
1312 gcc_assert (mode != VOIDmode);
1314 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1315 if (size % align != 0)
1316 size = CEIL (size, align) * align;
1317 size += GET_MODE_SIZE (mode);
1318 apply_result_mode[regno] = mode;
1321 apply_result_mode[regno] = VOIDmode;
1323 /* Allow targets that use untyped_call and untyped_return to override
1324 the size so that machine-specific information can be stored here. */
1325 #ifdef APPLY_RESULT_SIZE
1326 size = APPLY_RESULT_SIZE;
1332 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1333 /* Create a vector describing the result block RESULT. If SAVEP is true,
1334 the result block is used to save the values; otherwise it is used to
1335 restore the values. */
1338 result_vector (int savep, rtx result)
1340 int regno, size, align, nelts;
1341 enum machine_mode mode;
1343 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1346 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1347 if ((mode = apply_result_mode[regno]) != VOIDmode)
1349 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1350 if (size % align != 0)
1351 size = CEIL (size, align) * align;
1352 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1353 mem = adjust_address (result, mode, size);
1354 savevec[nelts++] = (savep
1355 ? gen_rtx_SET (VOIDmode, mem, reg)
1356 : gen_rtx_SET (VOIDmode, reg, mem));
1357 size += GET_MODE_SIZE (mode);
1359 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1361 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1363 /* Save the state required to perform an untyped call with the same
1364 arguments as were passed to the current function. */
1367 expand_builtin_apply_args_1 (void)
1370 int size, align, regno;
1371 enum machine_mode mode;
1372 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1374 /* Create a block where the arg-pointer, structure value address,
1375 and argument registers can be saved. */
1376 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1378 /* Walk past the arg-pointer and structure value address. */
1379 size = GET_MODE_SIZE (Pmode);
1380 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1381 size += GET_MODE_SIZE (Pmode);
1383 /* Save each register used in calling a function to the block. */
1384 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1385 if ((mode = apply_args_mode[regno]) != VOIDmode)
1387 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1388 if (size % align != 0)
1389 size = CEIL (size, align) * align;
1391 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1393 emit_move_insn (adjust_address (registers, mode, size), tem);
1394 size += GET_MODE_SIZE (mode);
1397 /* Save the arg pointer to the block. */
1398 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1399 #ifdef STACK_GROWS_DOWNWARD
1400 /* We need the pointer as the caller actually passed them to us, not
1401 as we might have pretended they were passed. Make sure it's a valid
1402 operand, as emit_move_insn isn't expected to handle a PLUS. */
1404 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1407 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1409 size = GET_MODE_SIZE (Pmode);
1411 /* Save the structure value address unless this is passed as an
1412 "invisible" first argument. */
1413 if (struct_incoming_value)
1415 emit_move_insn (adjust_address (registers, Pmode, size),
1416 copy_to_reg (struct_incoming_value));
1417 size += GET_MODE_SIZE (Pmode);
1420 /* Return the address of the block. */
1421 return copy_addr_to_reg (XEXP (registers, 0));
1424 /* __builtin_apply_args returns block of memory allocated on
1425 the stack into which is stored the arg pointer, structure
1426 value address, static chain, and all the registers that might
1427 possibly be used in performing a function call. The code is
1428 moved to the start of the function so the incoming values are
1432 expand_builtin_apply_args (void)
1434 /* Don't do __builtin_apply_args more than once in a function.
1435 Save the result of the first call and reuse it. */
1436 if (apply_args_value != 0)
1437 return apply_args_value;
1439 /* When this function is called, it means that registers must be
1440 saved on entry to this function. So we migrate the
1441 call to the first insn of this function. */
1446 temp = expand_builtin_apply_args_1 ();
1450 apply_args_value = temp;
1452 /* Put the insns after the NOTE that starts the function.
1453 If this is inside a start_sequence, make the outer-level insn
1454 chain current, so the code is placed at the start of the
1455 function. If internal_arg_pointer is a non-virtual pseudo,
1456 it needs to be placed after the function that initializes
1458 push_topmost_sequence ();
1459 if (REG_P (crtl->args.internal_arg_pointer)
1460 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1461 emit_insn_before (seq, parm_birth_insn);
1463 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1464 pop_topmost_sequence ();
1469 /* Perform an untyped call and save the state required to perform an
1470 untyped return of whatever value was returned by the given function. */
1473 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1475 int size, align, regno;
1476 enum machine_mode mode;
1477 rtx incoming_args, result, reg, dest, src, call_insn;
1478 rtx old_stack_level = 0;
1479 rtx call_fusage = 0;
1480 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1482 arguments = convert_memory_address (Pmode, arguments);
1484 /* Create a block where the return registers can be saved. */
1485 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1487 /* Fetch the arg pointer from the ARGUMENTS block. */
1488 incoming_args = gen_reg_rtx (Pmode);
1489 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1490 #ifndef STACK_GROWS_DOWNWARD
1491 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1492 incoming_args, 0, OPTAB_LIB_WIDEN);
1495 /* Push a new argument block and copy the arguments. Do not allow
1496 the (potential) memcpy call below to interfere with our stack
1498 do_pending_stack_adjust ();
1501 /* Save the stack with nonlocal if available. */
1502 #ifdef HAVE_save_stack_nonlocal
1503 if (HAVE_save_stack_nonlocal)
1504 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1507 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1509 /* Allocate a block of memory onto the stack and copy the memory
1510 arguments to the outgoing arguments address. */
1511 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1513 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1514 may have already set current_function_calls_alloca to true.
1515 current_function_calls_alloca won't be set if argsize is zero,
1516 so we have to guarantee need_drap is true here. */
1517 if (SUPPORTS_STACK_ALIGNMENT)
1518 crtl->need_drap = true;
1520 dest = virtual_outgoing_args_rtx;
1521 #ifndef STACK_GROWS_DOWNWARD
1522 if (GET_CODE (argsize) == CONST_INT)
1523 dest = plus_constant (dest, -INTVAL (argsize));
1525 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1527 dest = gen_rtx_MEM (BLKmode, dest);
1528 set_mem_align (dest, PARM_BOUNDARY);
1529 src = gen_rtx_MEM (BLKmode, incoming_args);
1530 set_mem_align (src, PARM_BOUNDARY);
1531 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1533 /* Refer to the argument block. */
1535 arguments = gen_rtx_MEM (BLKmode, arguments);
1536 set_mem_align (arguments, PARM_BOUNDARY);
1538 /* Walk past the arg-pointer and structure value address. */
1539 size = GET_MODE_SIZE (Pmode);
1541 size += GET_MODE_SIZE (Pmode);
1543 /* Restore each of the registers previously saved. Make USE insns
1544 for each of these registers for use in making the call. */
1545 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1546 if ((mode = apply_args_mode[regno]) != VOIDmode)
1548 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1549 if (size % align != 0)
1550 size = CEIL (size, align) * align;
1551 reg = gen_rtx_REG (mode, regno);
1552 emit_move_insn (reg, adjust_address (arguments, mode, size));
1553 use_reg (&call_fusage, reg);
1554 size += GET_MODE_SIZE (mode);
1557 /* Restore the structure value address unless this is passed as an
1558 "invisible" first argument. */
1559 size = GET_MODE_SIZE (Pmode);
1562 rtx value = gen_reg_rtx (Pmode);
1563 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1564 emit_move_insn (struct_value, value);
1565 if (REG_P (struct_value))
1566 use_reg (&call_fusage, struct_value);
1567 size += GET_MODE_SIZE (Pmode);
1570 /* All arguments and registers used for the call are set up by now! */
1571 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1573 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1574 and we don't want to load it into a register as an optimization,
1575 because prepare_call_address already did it if it should be done. */
1576 if (GET_CODE (function) != SYMBOL_REF)
1577 function = memory_address (FUNCTION_MODE, function);
1579 /* Generate the actual call instruction and save the return value. */
1580 #ifdef HAVE_untyped_call
1581 if (HAVE_untyped_call)
1582 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1583 result, result_vector (1, result)));
1586 #ifdef HAVE_call_value
1587 if (HAVE_call_value)
1591 /* Locate the unique return register. It is not possible to
1592 express a call that sets more than one return register using
1593 call_value; use untyped_call for that. In fact, untyped_call
1594 only needs to save the return registers in the given block. */
1595 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1596 if ((mode = apply_result_mode[regno]) != VOIDmode)
1598 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1600 valreg = gen_rtx_REG (mode, regno);
1603 emit_call_insn (GEN_CALL_VALUE (valreg,
1604 gen_rtx_MEM (FUNCTION_MODE, function),
1605 const0_rtx, NULL_RTX, const0_rtx));
1607 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1613 /* Find the CALL insn we just emitted, and attach the register usage
1615 call_insn = last_call_insn ();
1616 add_function_usage_to (call_insn, call_fusage);
1618 /* Restore the stack. */
1619 #ifdef HAVE_save_stack_nonlocal
1620 if (HAVE_save_stack_nonlocal)
1621 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1624 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1628 /* Return the address of the result block. */
1629 result = copy_addr_to_reg (XEXP (result, 0));
1630 return convert_memory_address (ptr_mode, result);
1633 /* Perform an untyped return. */
1636 expand_builtin_return (rtx result)
1638 int size, align, regno;
1639 enum machine_mode mode;
1641 rtx call_fusage = 0;
1643 result = convert_memory_address (Pmode, result);
1645 apply_result_size ();
1646 result = gen_rtx_MEM (BLKmode, result);
1648 #ifdef HAVE_untyped_return
1649 if (HAVE_untyped_return)
1651 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1657 /* Restore the return value and note that each value is used. */
1659 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1660 if ((mode = apply_result_mode[regno]) != VOIDmode)
1662 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1663 if (size % align != 0)
1664 size = CEIL (size, align) * align;
1665 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1666 emit_move_insn (reg, adjust_address (result, mode, size));
1668 push_to_sequence (call_fusage);
1670 call_fusage = get_insns ();
1672 size += GET_MODE_SIZE (mode);
1675 /* Put the USE insns before the return. */
1676 emit_insn (call_fusage);
1678 /* Return whatever values was restored by jumping directly to the end
1680 expand_naked_return ();
1683 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1685 static enum type_class
1686 type_to_class (tree type)
1688 switch (TREE_CODE (type))
1690 case VOID_TYPE: return void_type_class;
1691 case INTEGER_TYPE: return integer_type_class;
1692 case ENUMERAL_TYPE: return enumeral_type_class;
1693 case BOOLEAN_TYPE: return boolean_type_class;
1694 case POINTER_TYPE: return pointer_type_class;
1695 case REFERENCE_TYPE: return reference_type_class;
1696 case OFFSET_TYPE: return offset_type_class;
1697 case REAL_TYPE: return real_type_class;
1698 case COMPLEX_TYPE: return complex_type_class;
1699 case FUNCTION_TYPE: return function_type_class;
1700 case METHOD_TYPE: return method_type_class;
1701 case RECORD_TYPE: return record_type_class;
1703 case QUAL_UNION_TYPE: return union_type_class;
1704 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1705 ? string_type_class : array_type_class);
1706 case LANG_TYPE: return lang_type_class;
1707 default: return no_type_class;
1711 /* Expand a call EXP to __builtin_classify_type. */
1714 expand_builtin_classify_type (tree exp)
1716 if (call_expr_nargs (exp))
1717 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1718 return GEN_INT (no_type_class);
1721 /* This helper macro, meant to be used in mathfn_built_in below,
1722 determines which among a set of three builtin math functions is
1723 appropriate for a given type mode. The `F' and `L' cases are
1724 automatically generated from the `double' case. */
1725 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1726 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1727 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1728 fcodel = BUILT_IN_MATHFN##L ; break;
1729 /* Similar to above, but appends _R after any F/L suffix. */
1730 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1731 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1732 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1733 fcodel = BUILT_IN_MATHFN##L_R ; break;
1735 /* Return mathematic function equivalent to FN but operating directly
1736 on TYPE, if available. If IMPLICIT is true find the function in
1737 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1738 can't do the conversion, return zero. */
1741 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1743 tree const *const fn_arr
1744 = implicit ? implicit_built_in_decls : built_in_decls;
1745 enum built_in_function fcode, fcodef, fcodel;
1749 CASE_MATHFN (BUILT_IN_ACOS)
1750 CASE_MATHFN (BUILT_IN_ACOSH)
1751 CASE_MATHFN (BUILT_IN_ASIN)
1752 CASE_MATHFN (BUILT_IN_ASINH)
1753 CASE_MATHFN (BUILT_IN_ATAN)
1754 CASE_MATHFN (BUILT_IN_ATAN2)
1755 CASE_MATHFN (BUILT_IN_ATANH)
1756 CASE_MATHFN (BUILT_IN_CBRT)
1757 CASE_MATHFN (BUILT_IN_CEIL)
1758 CASE_MATHFN (BUILT_IN_CEXPI)
1759 CASE_MATHFN (BUILT_IN_COPYSIGN)
1760 CASE_MATHFN (BUILT_IN_COS)
1761 CASE_MATHFN (BUILT_IN_COSH)
1762 CASE_MATHFN (BUILT_IN_DREM)
1763 CASE_MATHFN (BUILT_IN_ERF)
1764 CASE_MATHFN (BUILT_IN_ERFC)
1765 CASE_MATHFN (BUILT_IN_EXP)
1766 CASE_MATHFN (BUILT_IN_EXP10)
1767 CASE_MATHFN (BUILT_IN_EXP2)
1768 CASE_MATHFN (BUILT_IN_EXPM1)
1769 CASE_MATHFN (BUILT_IN_FABS)
1770 CASE_MATHFN (BUILT_IN_FDIM)
1771 CASE_MATHFN (BUILT_IN_FLOOR)
1772 CASE_MATHFN (BUILT_IN_FMA)
1773 CASE_MATHFN (BUILT_IN_FMAX)
1774 CASE_MATHFN (BUILT_IN_FMIN)
1775 CASE_MATHFN (BUILT_IN_FMOD)
1776 CASE_MATHFN (BUILT_IN_FREXP)
1777 CASE_MATHFN (BUILT_IN_GAMMA)
1778 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1779 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1780 CASE_MATHFN (BUILT_IN_HYPOT)
1781 CASE_MATHFN (BUILT_IN_ILOGB)
1782 CASE_MATHFN (BUILT_IN_INF)
1783 CASE_MATHFN (BUILT_IN_ISINF)
1784 CASE_MATHFN (BUILT_IN_J0)
1785 CASE_MATHFN (BUILT_IN_J1)
1786 CASE_MATHFN (BUILT_IN_JN)
1787 CASE_MATHFN (BUILT_IN_LCEIL)
1788 CASE_MATHFN (BUILT_IN_LDEXP)
1789 CASE_MATHFN (BUILT_IN_LFLOOR)
1790 CASE_MATHFN (BUILT_IN_LGAMMA)
1791 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1792 CASE_MATHFN (BUILT_IN_LLCEIL)
1793 CASE_MATHFN (BUILT_IN_LLFLOOR)
1794 CASE_MATHFN (BUILT_IN_LLRINT)
1795 CASE_MATHFN (BUILT_IN_LLROUND)
1796 CASE_MATHFN (BUILT_IN_LOG)
1797 CASE_MATHFN (BUILT_IN_LOG10)
1798 CASE_MATHFN (BUILT_IN_LOG1P)
1799 CASE_MATHFN (BUILT_IN_LOG2)
1800 CASE_MATHFN (BUILT_IN_LOGB)
1801 CASE_MATHFN (BUILT_IN_LRINT)
1802 CASE_MATHFN (BUILT_IN_LROUND)
1803 CASE_MATHFN (BUILT_IN_MODF)
1804 CASE_MATHFN (BUILT_IN_NAN)
1805 CASE_MATHFN (BUILT_IN_NANS)
1806 CASE_MATHFN (BUILT_IN_NEARBYINT)
1807 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1808 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1809 CASE_MATHFN (BUILT_IN_POW)
1810 CASE_MATHFN (BUILT_IN_POWI)
1811 CASE_MATHFN (BUILT_IN_POW10)
1812 CASE_MATHFN (BUILT_IN_REMAINDER)
1813 CASE_MATHFN (BUILT_IN_REMQUO)
1814 CASE_MATHFN (BUILT_IN_RINT)
1815 CASE_MATHFN (BUILT_IN_ROUND)
1816 CASE_MATHFN (BUILT_IN_SCALB)
1817 CASE_MATHFN (BUILT_IN_SCALBLN)
1818 CASE_MATHFN (BUILT_IN_SCALBN)
1819 CASE_MATHFN (BUILT_IN_SIGNBIT)
1820 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1821 CASE_MATHFN (BUILT_IN_SIN)
1822 CASE_MATHFN (BUILT_IN_SINCOS)
1823 CASE_MATHFN (BUILT_IN_SINH)
1824 CASE_MATHFN (BUILT_IN_SQRT)
1825 CASE_MATHFN (BUILT_IN_TAN)
1826 CASE_MATHFN (BUILT_IN_TANH)
1827 CASE_MATHFN (BUILT_IN_TGAMMA)
1828 CASE_MATHFN (BUILT_IN_TRUNC)
1829 CASE_MATHFN (BUILT_IN_Y0)
1830 CASE_MATHFN (BUILT_IN_Y1)
1831 CASE_MATHFN (BUILT_IN_YN)
1837 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1838 return fn_arr[fcode];
1839 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1840 return fn_arr[fcodef];
1841 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1842 return fn_arr[fcodel];
1847 /* Like mathfn_built_in_1(), but always use the implicit array. */
1850 mathfn_built_in (tree type, enum built_in_function fn)
1852 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1855 /* If errno must be maintained, expand the RTL to check if the result,
1856 TARGET, of a built-in function call, EXP, is NaN, and if so set
1860 expand_errno_check (tree exp, rtx target)
1862 rtx lab = gen_label_rtx ();
1864 /* Test the result; if it is NaN, set errno=EDOM because
1865 the argument was not in the domain. */
1866 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1870 /* If this built-in doesn't throw an exception, set errno directly. */
1871 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1873 #ifdef GEN_ERRNO_RTX
1874 rtx errno_rtx = GEN_ERRNO_RTX;
1877 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1879 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1885 /* Make sure the library call isn't expanded as a tail call. */
1886 CALL_EXPR_TAILCALL (exp) = 0;
1888 /* We can't set errno=EDOM directly; let the library call do it.
1889 Pop the arguments right away in case the call gets deleted. */
1891 expand_call (exp, target, 0);
1896 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1897 Return NULL_RTX if a normal call should be emitted rather than expanding
1898 the function in-line. EXP is the expression that is a call to the builtin
1899 function; if convenient, the result should be placed in TARGET.
1900 SUBTARGET may be used as the target for computing one of EXP's operands. */
1903 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1905 optab builtin_optab;
1906 rtx op0, insns, before_call;
1907 tree fndecl = get_callee_fndecl (exp);
1908 enum machine_mode mode;
1909 bool errno_set = false;
1912 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1915 arg = CALL_EXPR_ARG (exp, 0);
1917 switch (DECL_FUNCTION_CODE (fndecl))
1919 CASE_FLT_FN (BUILT_IN_SQRT):
1920 errno_set = ! tree_expr_nonnegative_p (arg);
1921 builtin_optab = sqrt_optab;
1923 CASE_FLT_FN (BUILT_IN_EXP):
1924 errno_set = true; builtin_optab = exp_optab; break;
1925 CASE_FLT_FN (BUILT_IN_EXP10):
1926 CASE_FLT_FN (BUILT_IN_POW10):
1927 errno_set = true; builtin_optab = exp10_optab; break;
1928 CASE_FLT_FN (BUILT_IN_EXP2):
1929 errno_set = true; builtin_optab = exp2_optab; break;
1930 CASE_FLT_FN (BUILT_IN_EXPM1):
1931 errno_set = true; builtin_optab = expm1_optab; break;
1932 CASE_FLT_FN (BUILT_IN_LOGB):
1933 errno_set = true; builtin_optab = logb_optab; break;
1934 CASE_FLT_FN (BUILT_IN_LOG):
1935 errno_set = true; builtin_optab = log_optab; break;
1936 CASE_FLT_FN (BUILT_IN_LOG10):
1937 errno_set = true; builtin_optab = log10_optab; break;
1938 CASE_FLT_FN (BUILT_IN_LOG2):
1939 errno_set = true; builtin_optab = log2_optab; break;
1940 CASE_FLT_FN (BUILT_IN_LOG1P):
1941 errno_set = true; builtin_optab = log1p_optab; break;
1942 CASE_FLT_FN (BUILT_IN_ASIN):
1943 builtin_optab = asin_optab; break;
1944 CASE_FLT_FN (BUILT_IN_ACOS):
1945 builtin_optab = acos_optab; break;
1946 CASE_FLT_FN (BUILT_IN_TAN):
1947 builtin_optab = tan_optab; break;
1948 CASE_FLT_FN (BUILT_IN_ATAN):
1949 builtin_optab = atan_optab; break;
1950 CASE_FLT_FN (BUILT_IN_FLOOR):
1951 builtin_optab = floor_optab; break;
1952 CASE_FLT_FN (BUILT_IN_CEIL):
1953 builtin_optab = ceil_optab; break;
1954 CASE_FLT_FN (BUILT_IN_TRUNC):
1955 builtin_optab = btrunc_optab; break;
1956 CASE_FLT_FN (BUILT_IN_ROUND):
1957 builtin_optab = round_optab; break;
1958 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1959 builtin_optab = nearbyint_optab;
1960 if (flag_trapping_math)
1962 /* Else fallthrough and expand as rint. */
1963 CASE_FLT_FN (BUILT_IN_RINT):
1964 builtin_optab = rint_optab; break;
1969 /* Make a suitable register to place result in. */
1970 mode = TYPE_MODE (TREE_TYPE (exp));
1972 if (! flag_errno_math || ! HONOR_NANS (mode))
1975 /* Before working hard, check whether the instruction is available. */
1976 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
1978 target = gen_reg_rtx (mode);
1980 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1981 need to expand the argument again. This way, we will not perform
1982 side-effects more the once. */
1983 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
1985 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
1989 /* Compute into TARGET.
1990 Set TARGET to wherever the result comes back. */
1991 target = expand_unop (mode, builtin_optab, op0, target, 0);
1996 expand_errno_check (exp, target);
1998 /* Output the entire sequence. */
1999 insns = get_insns ();
2005 /* If we were unable to expand via the builtin, stop the sequence
2006 (without outputting the insns) and call to the library function
2007 with the stabilized argument list. */
2011 before_call = get_last_insn ();
2013 return expand_call (exp, target, target == const0_rtx);
2016 /* Expand a call to the builtin binary math functions (pow and atan2).
2017 Return NULL_RTX if a normal call should be emitted rather than expanding the
2018 function in-line. EXP is the expression that is a call to the builtin
2019 function; if convenient, the result should be placed in TARGET.
2020 SUBTARGET may be used as the target for computing one of EXP's
2024 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2026 optab builtin_optab;
2027 rtx op0, op1, insns;
2028 int op1_type = REAL_TYPE;
2029 tree fndecl = get_callee_fndecl (exp);
2031 enum machine_mode mode;
2032 bool errno_set = true;
2034 switch (DECL_FUNCTION_CODE (fndecl))
2036 CASE_FLT_FN (BUILT_IN_SCALBN):
2037 CASE_FLT_FN (BUILT_IN_SCALBLN):
2038 CASE_FLT_FN (BUILT_IN_LDEXP):
2039 op1_type = INTEGER_TYPE;
2044 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2047 arg0 = CALL_EXPR_ARG (exp, 0);
2048 arg1 = CALL_EXPR_ARG (exp, 1);
2050 switch (DECL_FUNCTION_CODE (fndecl))
2052 CASE_FLT_FN (BUILT_IN_POW):
2053 builtin_optab = pow_optab; break;
2054 CASE_FLT_FN (BUILT_IN_ATAN2):
2055 builtin_optab = atan2_optab; break;
2056 CASE_FLT_FN (BUILT_IN_SCALB):
2057 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2059 builtin_optab = scalb_optab; break;
2060 CASE_FLT_FN (BUILT_IN_SCALBN):
2061 CASE_FLT_FN (BUILT_IN_SCALBLN):
2062 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2064 /* Fall through... */
2065 CASE_FLT_FN (BUILT_IN_LDEXP):
2066 builtin_optab = ldexp_optab; break;
2067 CASE_FLT_FN (BUILT_IN_FMOD):
2068 builtin_optab = fmod_optab; break;
2069 CASE_FLT_FN (BUILT_IN_REMAINDER):
2070 CASE_FLT_FN (BUILT_IN_DREM):
2071 builtin_optab = remainder_optab; break;
2076 /* Make a suitable register to place result in. */
2077 mode = TYPE_MODE (TREE_TYPE (exp));
2079 /* Before working hard, check whether the instruction is available. */
2080 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2083 target = gen_reg_rtx (mode);
2085 if (! flag_errno_math || ! HONOR_NANS (mode))
2088 /* Always stabilize the argument list. */
2089 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2090 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2092 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2093 op1 = expand_normal (arg1);
2097 /* Compute into TARGET.
2098 Set TARGET to wherever the result comes back. */
2099 target = expand_binop (mode, builtin_optab, op0, op1,
2100 target, 0, OPTAB_DIRECT);
2102 /* If we were unable to expand via the builtin, stop the sequence
2103 (without outputting the insns) and call to the library function
2104 with the stabilized argument list. */
2108 return expand_call (exp, target, target == const0_rtx);
2112 expand_errno_check (exp, target);
2114 /* Output the entire sequence. */
2115 insns = get_insns ();
2122 /* Expand a call to the builtin sin and cos math functions.
2123 Return NULL_RTX if a normal call should be emitted rather than expanding the
2124 function in-line. EXP is the expression that is a call to the builtin
2125 function; if convenient, the result should be placed in TARGET.
2126 SUBTARGET may be used as the target for computing one of EXP's
2130 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2132 optab builtin_optab;
2134 tree fndecl = get_callee_fndecl (exp);
2135 enum machine_mode mode;
2138 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2141 arg = CALL_EXPR_ARG (exp, 0);
2143 switch (DECL_FUNCTION_CODE (fndecl))
2145 CASE_FLT_FN (BUILT_IN_SIN):
2146 CASE_FLT_FN (BUILT_IN_COS):
2147 builtin_optab = sincos_optab; break;
2152 /* Make a suitable register to place result in. */
2153 mode = TYPE_MODE (TREE_TYPE (exp));
2155 /* Check if sincos insn is available, otherwise fallback
2156 to sin or cos insn. */
2157 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2158 switch (DECL_FUNCTION_CODE (fndecl))
2160 CASE_FLT_FN (BUILT_IN_SIN):
2161 builtin_optab = sin_optab; break;
2162 CASE_FLT_FN (BUILT_IN_COS):
2163 builtin_optab = cos_optab; break;
2168 /* Before working hard, check whether the instruction is available. */
2169 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2171 target = gen_reg_rtx (mode);
2173 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2174 need to expand the argument again. This way, we will not perform
2175 side-effects more the once. */
2176 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2178 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2182 /* Compute into TARGET.
2183 Set TARGET to wherever the result comes back. */
2184 if (builtin_optab == sincos_optab)
2188 switch (DECL_FUNCTION_CODE (fndecl))
2190 CASE_FLT_FN (BUILT_IN_SIN):
2191 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2193 CASE_FLT_FN (BUILT_IN_COS):
2194 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2199 gcc_assert (result);
2203 target = expand_unop (mode, builtin_optab, op0, target, 0);
2208 /* Output the entire sequence. */
2209 insns = get_insns ();
2215 /* If we were unable to expand via the builtin, stop the sequence
2216 (without outputting the insns) and call to the library function
2217 with the stabilized argument list. */
2221 target = expand_call (exp, target, target == const0_rtx);
2226 /* Expand a call to one of the builtin math functions that operate on
2227 floating point argument and output an integer result (ilogb, isinf,
2229 Return 0 if a normal call should be emitted rather than expanding the
2230 function in-line. EXP is the expression that is a call to the builtin
2231 function; if convenient, the result should be placed in TARGET.
2232 SUBTARGET may be used as the target for computing one of EXP's operands. */
2235 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2237 optab builtin_optab = 0;
2238 enum insn_code icode = CODE_FOR_nothing;
2240 tree fndecl = get_callee_fndecl (exp);
2241 enum machine_mode mode;
2242 bool errno_set = false;
2245 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2248 arg = CALL_EXPR_ARG (exp, 0);
2250 switch (DECL_FUNCTION_CODE (fndecl))
2252 CASE_FLT_FN (BUILT_IN_ILOGB):
2253 errno_set = true; builtin_optab = ilogb_optab; break;
2254 CASE_FLT_FN (BUILT_IN_ISINF):
2255 builtin_optab = isinf_optab; break;
2256 case BUILT_IN_ISNORMAL:
2257 case BUILT_IN_ISFINITE:
2258 CASE_FLT_FN (BUILT_IN_FINITE):
2259 /* These builtins have no optabs (yet). */
2265 /* There's no easy way to detect the case we need to set EDOM. */
2266 if (flag_errno_math && errno_set)
2269 /* Optab mode depends on the mode of the input argument. */
2270 mode = TYPE_MODE (TREE_TYPE (arg));
2273 icode = optab_handler (builtin_optab, mode)->insn_code;
2275 /* Before working hard, check whether the instruction is available. */
2276 if (icode != CODE_FOR_nothing)
2278 /* Make a suitable register to place result in. */
2280 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2281 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2283 gcc_assert (insn_data[icode].operand[0].predicate
2284 (target, GET_MODE (target)));
2286 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2287 need to expand the argument again. This way, we will not perform
2288 side-effects more the once. */
2289 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2291 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2293 if (mode != GET_MODE (op0))
2294 op0 = convert_to_mode (mode, op0, 0);
2296 /* Compute into TARGET.
2297 Set TARGET to wherever the result comes back. */
2298 emit_unop_insn (icode, target, op0, UNKNOWN);
2302 /* If there is no optab, try generic code. */
2303 switch (DECL_FUNCTION_CODE (fndecl))
2307 CASE_FLT_FN (BUILT_IN_ISINF):
2309 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
2310 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
2311 tree const type = TREE_TYPE (arg);
2315 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2316 real_from_string (&r, buf);
2317 result = build_call_expr (isgr_fn, 2,
2318 fold_build1 (ABS_EXPR, type, arg),
2319 build_real (type, r));
2320 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2322 CASE_FLT_FN (BUILT_IN_FINITE):
2323 case BUILT_IN_ISFINITE:
2325 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
2326 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2327 tree const type = TREE_TYPE (arg);
2331 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2332 real_from_string (&r, buf);
2333 result = build_call_expr (isle_fn, 2,
2334 fold_build1 (ABS_EXPR, type, arg),
2335 build_real (type, r));
2336 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2338 case BUILT_IN_ISNORMAL:
2340 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
2341 islessequal(fabs(x),DBL_MAX). */
2342 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2343 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
2344 tree const type = TREE_TYPE (arg);
2345 REAL_VALUE_TYPE rmax, rmin;
2348 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2349 real_from_string (&rmax, buf);
2350 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
2351 real_from_string (&rmin, buf);
2352 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
2353 result = build_call_expr (isle_fn, 2, arg,
2354 build_real (type, rmax));
2355 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
2356 build_call_expr (isge_fn, 2, arg,
2357 build_real (type, rmin)));
2358 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2364 target = expand_call (exp, target, target == const0_rtx);
2369 /* Expand a call to the builtin sincos math function.
2370 Return NULL_RTX if a normal call should be emitted rather than expanding the
2371 function in-line. EXP is the expression that is a call to the builtin
2375 expand_builtin_sincos (tree exp)
2377 rtx op0, op1, op2, target1, target2;
2378 enum machine_mode mode;
2379 tree arg, sinp, cosp;
2382 if (!validate_arglist (exp, REAL_TYPE,
2383 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2386 arg = CALL_EXPR_ARG (exp, 0);
2387 sinp = CALL_EXPR_ARG (exp, 1);
2388 cosp = CALL_EXPR_ARG (exp, 2);
2390 /* Make a suitable register to place result in. */
2391 mode = TYPE_MODE (TREE_TYPE (arg));
2393 /* Check if sincos insn is available, otherwise emit the call. */
2394 if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing)
2397 target1 = gen_reg_rtx (mode);
2398 target2 = gen_reg_rtx (mode);
2400 op0 = expand_normal (arg);
2401 op1 = expand_normal (build_fold_indirect_ref (sinp));
2402 op2 = expand_normal (build_fold_indirect_ref (cosp));
2404 /* Compute into target1 and target2.
2405 Set TARGET to wherever the result comes back. */
2406 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2407 gcc_assert (result);
2409 /* Move target1 and target2 to the memory locations indicated
2411 emit_move_insn (op1, target1);
2412 emit_move_insn (op2, target2);
2417 /* Expand a call to the internal cexpi builtin to the sincos math function.
2418 EXP is the expression that is a call to the builtin function; if convenient,
2419 the result should be placed in TARGET. SUBTARGET may be used as the target
2420 for computing one of EXP's operands. */
2423 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2425 tree fndecl = get_callee_fndecl (exp);
2427 enum machine_mode mode;
2430 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2433 arg = CALL_EXPR_ARG (exp, 0);
2434 type = TREE_TYPE (arg);
2435 mode = TYPE_MODE (TREE_TYPE (arg));
2437 /* Try expanding via a sincos optab, fall back to emitting a libcall
2438 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2439 is only generated from sincos, cexp or if we have either of them. */
2440 if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing)
2442 op1 = gen_reg_rtx (mode);
2443 op2 = gen_reg_rtx (mode);
2445 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2447 /* Compute into op1 and op2. */
2448 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2450 else if (TARGET_HAS_SINCOS)
2452 tree call, fn = NULL_TREE;
2456 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2457 fn = built_in_decls[BUILT_IN_SINCOSF];
2458 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2459 fn = built_in_decls[BUILT_IN_SINCOS];
2460 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2461 fn = built_in_decls[BUILT_IN_SINCOSL];
2465 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2466 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2467 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2468 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2469 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2470 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2472 /* Make sure not to fold the sincos call again. */
2473 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2474 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2475 call, 3, arg, top1, top2));
2479 tree call, fn = NULL_TREE, narg;
2480 tree ctype = build_complex_type (type);
2482 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2483 fn = built_in_decls[BUILT_IN_CEXPF];
2484 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2485 fn = built_in_decls[BUILT_IN_CEXP];
2486 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2487 fn = built_in_decls[BUILT_IN_CEXPL];
2491 /* If we don't have a decl for cexp create one. This is the
2492 friendliest fallback if the user calls __builtin_cexpi
2493 without full target C99 function support. */
2494 if (fn == NULL_TREE)
2497 const char *name = NULL;
2499 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2501 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2503 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2506 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2507 fn = build_fn_decl (name, fntype);
2510 narg = fold_build2 (COMPLEX_EXPR, ctype,
2511 build_real (type, dconst0), arg);
2513 /* Make sure not to fold the cexp call again. */
2514 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2515 return expand_expr (build_call_nary (ctype, call, 1, narg),
2516 target, VOIDmode, EXPAND_NORMAL);
2519 /* Now build the proper return type. */
2520 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2521 make_tree (TREE_TYPE (arg), op2),
2522 make_tree (TREE_TYPE (arg), op1)),
2523 target, VOIDmode, EXPAND_NORMAL);
2526 /* Expand a call to one of the builtin rounding functions gcc defines
2527 as an extension (lfloor and lceil). As these are gcc extensions we
2528 do not need to worry about setting errno to EDOM.
2529 If expanding via optab fails, lower expression to (int)(floor(x)).
2530 EXP is the expression that is a call to the builtin function;
2531 if convenient, the result should be placed in TARGET. */
2534 expand_builtin_int_roundingfn (tree exp, rtx target)
2536 convert_optab builtin_optab;
2537 rtx op0, insns, tmp;
2538 tree fndecl = get_callee_fndecl (exp);
2539 enum built_in_function fallback_fn;
2540 tree fallback_fndecl;
2541 enum machine_mode mode;
2544 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2547 arg = CALL_EXPR_ARG (exp, 0);
2549 switch (DECL_FUNCTION_CODE (fndecl))
2551 CASE_FLT_FN (BUILT_IN_LCEIL):
2552 CASE_FLT_FN (BUILT_IN_LLCEIL):
2553 builtin_optab = lceil_optab;
2554 fallback_fn = BUILT_IN_CEIL;
2557 CASE_FLT_FN (BUILT_IN_LFLOOR):
2558 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2559 builtin_optab = lfloor_optab;
2560 fallback_fn = BUILT_IN_FLOOR;
2567 /* Make a suitable register to place result in. */
2568 mode = TYPE_MODE (TREE_TYPE (exp));
2570 target = gen_reg_rtx (mode);
2572 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2573 need to expand the argument again. This way, we will not perform
2574 side-effects more the once. */
2575 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2577 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2581 /* Compute into TARGET. */
2582 if (expand_sfix_optab (target, op0, builtin_optab))
2584 /* Output the entire sequence. */
2585 insns = get_insns ();
2591 /* If we were unable to expand via the builtin, stop the sequence
2592 (without outputting the insns). */
2595 /* Fall back to floating point rounding optab. */
2596 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2598 /* For non-C99 targets we may end up without a fallback fndecl here
2599 if the user called __builtin_lfloor directly. In this case emit
2600 a call to the floor/ceil variants nevertheless. This should result
2601 in the best user experience for not full C99 targets. */
2602 if (fallback_fndecl == NULL_TREE)
2605 const char *name = NULL;
2607 switch (DECL_FUNCTION_CODE (fndecl))
2609 case BUILT_IN_LCEIL:
2610 case BUILT_IN_LLCEIL:
2613 case BUILT_IN_LCEILF:
2614 case BUILT_IN_LLCEILF:
2617 case BUILT_IN_LCEILL:
2618 case BUILT_IN_LLCEILL:
2621 case BUILT_IN_LFLOOR:
2622 case BUILT_IN_LLFLOOR:
2625 case BUILT_IN_LFLOORF:
2626 case BUILT_IN_LLFLOORF:
2629 case BUILT_IN_LFLOORL:
2630 case BUILT_IN_LLFLOORL:
2637 fntype = build_function_type_list (TREE_TYPE (arg),
2638 TREE_TYPE (arg), NULL_TREE);
2639 fallback_fndecl = build_fn_decl (name, fntype);
2642 exp = build_call_expr (fallback_fndecl, 1, arg);
2644 tmp = expand_normal (exp);
2646 /* Truncate the result of floating point optab to integer
2647 via expand_fix (). */
2648 target = gen_reg_rtx (mode);
2649 expand_fix (target, tmp, 0);
2654 /* Expand a call to one of the builtin math functions doing integer
2656 Return 0 if a normal call should be emitted rather than expanding the
2657 function in-line. EXP is the expression that is a call to the builtin
2658 function; if convenient, the result should be placed in TARGET. */
2661 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2663 convert_optab builtin_optab;
2665 tree fndecl = get_callee_fndecl (exp);
2667 enum machine_mode mode;
2669 /* There's no easy way to detect the case we need to set EDOM. */
2670 if (flag_errno_math)
2673 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2676 arg = CALL_EXPR_ARG (exp, 0);
2678 switch (DECL_FUNCTION_CODE (fndecl))
2680 CASE_FLT_FN (BUILT_IN_LRINT):
2681 CASE_FLT_FN (BUILT_IN_LLRINT):
2682 builtin_optab = lrint_optab; break;
2683 CASE_FLT_FN (BUILT_IN_LROUND):
2684 CASE_FLT_FN (BUILT_IN_LLROUND):
2685 builtin_optab = lround_optab; break;
2690 /* Make a suitable register to place result in. */
2691 mode = TYPE_MODE (TREE_TYPE (exp));
2693 target = gen_reg_rtx (mode);
2695 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2696 need to expand the argument again. This way, we will not perform
2697 side-effects more the once. */
2698 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2700 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2704 if (expand_sfix_optab (target, op0, builtin_optab))
2706 /* Output the entire sequence. */
2707 insns = get_insns ();
2713 /* If we were unable to expand via the builtin, stop the sequence
2714 (without outputting the insns) and call to the library function
2715 with the stabilized argument list. */
2718 target = expand_call (exp, target, target == const0_rtx);
2723 /* To evaluate powi(x,n), the floating point value x raised to the
2724 constant integer exponent n, we use a hybrid algorithm that
2725 combines the "window method" with look-up tables. For an
2726 introduction to exponentiation algorithms and "addition chains",
2727 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2728 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2729 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2730 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2732 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2733 multiplications to inline before calling the system library's pow
2734 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2735 so this default never requires calling pow, powf or powl. */
2737 #ifndef POWI_MAX_MULTS
2738 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2741 /* The size of the "optimal power tree" lookup table. All
2742 exponents less than this value are simply looked up in the
2743 powi_table below. This threshold is also used to size the
2744 cache of pseudo registers that hold intermediate results. */
2745 #define POWI_TABLE_SIZE 256
2747 /* The size, in bits of the window, used in the "window method"
2748 exponentiation algorithm. This is equivalent to a radix of
2749 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2750 #define POWI_WINDOW_SIZE 3
2752 /* The following table is an efficient representation of an
2753 "optimal power tree". For each value, i, the corresponding
2754 value, j, in the table states than an optimal evaluation
2755 sequence for calculating pow(x,i) can be found by evaluating
2756 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2757 100 integers is given in Knuth's "Seminumerical algorithms". */
2759 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2761 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2762 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2763 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2764 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2765 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2766 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2767 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2768 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2769 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2770 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2771 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2772 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2773 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2774 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2775 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2776 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2777 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2778 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2779 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2780 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2781 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2782 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2783 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2784 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2785 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2786 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2787 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2788 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2789 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2790 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2791 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2792 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2796 /* Return the number of multiplications required to calculate
2797 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2798 subroutine of powi_cost. CACHE is an array indicating
2799 which exponents have already been calculated. */
2802 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2804 /* If we've already calculated this exponent, then this evaluation
2805 doesn't require any additional multiplications. */
2810 return powi_lookup_cost (n - powi_table[n], cache)
2811 + powi_lookup_cost (powi_table[n], cache) + 1;
2814 /* Return the number of multiplications required to calculate
2815 powi(x,n) for an arbitrary x, given the exponent N. This
2816 function needs to be kept in sync with expand_powi below. */
2819 powi_cost (HOST_WIDE_INT n)
2821 bool cache[POWI_TABLE_SIZE];
2822 unsigned HOST_WIDE_INT digit;
2823 unsigned HOST_WIDE_INT val;
2829 /* Ignore the reciprocal when calculating the cost. */
2830 val = (n < 0) ? -n : n;
2832 /* Initialize the exponent cache. */
2833 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2838 while (val >= POWI_TABLE_SIZE)
2842 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2843 result += powi_lookup_cost (digit, cache)
2844 + POWI_WINDOW_SIZE + 1;
2845 val >>= POWI_WINDOW_SIZE;
2854 return result + powi_lookup_cost (val, cache);
2857 /* Recursive subroutine of expand_powi. This function takes the array,
2858 CACHE, of already calculated exponents and an exponent N and returns
2859 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2862 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2864 unsigned HOST_WIDE_INT digit;
2868 if (n < POWI_TABLE_SIZE)
2873 target = gen_reg_rtx (mode);
2876 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2877 op1 = expand_powi_1 (mode, powi_table[n], cache);
2881 target = gen_reg_rtx (mode);
2882 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2883 op0 = expand_powi_1 (mode, n - digit, cache);
2884 op1 = expand_powi_1 (mode, digit, cache);
2888 target = gen_reg_rtx (mode);
2889 op0 = expand_powi_1 (mode, n >> 1, cache);
2893 result = expand_mult (mode, op0, op1, target, 0);
2894 if (result != target)
2895 emit_move_insn (target, result);
2899 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2900 floating point operand in mode MODE, and N is the exponent. This
2901 function needs to be kept in sync with powi_cost above. */
2904 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2906 unsigned HOST_WIDE_INT val;
2907 rtx cache[POWI_TABLE_SIZE];
2911 return CONST1_RTX (mode);
2913 val = (n < 0) ? -n : n;
2915 memset (cache, 0, sizeof (cache));
2918 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2920 /* If the original exponent was negative, reciprocate the result. */
2922 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2923 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2928 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2929 a normal call should be emitted rather than expanding the function
2930 in-line. EXP is the expression that is a call to the builtin
2931 function; if convenient, the result should be placed in TARGET. */
2934 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2938 tree type = TREE_TYPE (exp);
2939 REAL_VALUE_TYPE cint, c, c2;
2942 enum machine_mode mode = TYPE_MODE (type);
2944 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2947 arg0 = CALL_EXPR_ARG (exp, 0);
2948 arg1 = CALL_EXPR_ARG (exp, 1);
2950 if (TREE_CODE (arg1) != REAL_CST
2951 || TREE_OVERFLOW (arg1))
2952 return expand_builtin_mathfn_2 (exp, target, subtarget);
2954 /* Handle constant exponents. */
2956 /* For integer valued exponents we can expand to an optimal multiplication
2957 sequence using expand_powi. */
2958 c = TREE_REAL_CST (arg1);
2959 n = real_to_integer (&c);
2960 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2961 if (real_identical (&c, &cint)
2962 && ((n >= -1 && n <= 2)
2963 || (flag_unsafe_math_optimizations
2964 && optimize_insn_for_speed_p ()
2965 && powi_cost (n) <= POWI_MAX_MULTS)))
2967 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2970 op = force_reg (mode, op);
2971 op = expand_powi (op, mode, n);
2976 narg0 = builtin_save_expr (arg0);
2978 /* If the exponent is not integer valued, check if it is half of an integer.
2979 In this case we can expand to sqrt (x) * x**(n/2). */
2980 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2981 if (fn != NULL_TREE)
2983 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2984 n = real_to_integer (&c2);
2985 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2986 if (real_identical (&c2, &cint)
2987 && ((flag_unsafe_math_optimizations
2988 && optimize_insn_for_speed_p ()
2989 && powi_cost (n/2) <= POWI_MAX_MULTS)
2992 tree call_expr = build_call_expr (fn, 1, narg0);
2993 /* Use expand_expr in case the newly built call expression
2994 was folded to a non-call. */
2995 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
2998 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
2999 op2 = force_reg (mode, op2);
3000 op2 = expand_powi (op2, mode, abs (n / 2));
3001 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3002 0, OPTAB_LIB_WIDEN);
3003 /* If the original exponent was negative, reciprocate the
3006 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3007 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3013 /* Try if the exponent is a third of an integer. In this case
3014 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3015 different from pow (x, 1./3.) due to rounding and behavior
3016 with negative x we need to constrain this transformation to
3017 unsafe math and positive x or finite math. */
3018 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3020 && flag_unsafe_math_optimizations
3021 && (tree_expr_nonnegative_p (arg0)
3022 || !HONOR_NANS (mode)))
3024 REAL_VALUE_TYPE dconst3;
3025 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3026 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3027 real_round (&c2, mode, &c2);
3028 n = real_to_integer (&c2);
3029 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3030 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3031 real_convert (&c2, mode, &c2);
3032 if (real_identical (&c2, &c)
3033 && ((optimize_insn_for_speed_p ()
3034 && powi_cost (n/3) <= POWI_MAX_MULTS)
3037 tree call_expr = build_call_expr (fn, 1,narg0);
3038 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3039 if (abs (n) % 3 == 2)
3040 op = expand_simple_binop (mode, MULT, op, op, op,
3041 0, OPTAB_LIB_WIDEN);
3044 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3045 op2 = force_reg (mode, op2);
3046 op2 = expand_powi (op2, mode, abs (n / 3));
3047 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3048 0, OPTAB_LIB_WIDEN);
3049 /* If the original exponent was negative, reciprocate the
3052 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3053 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3059 /* Fall back to optab expansion. */
3060 return expand_builtin_mathfn_2 (exp, target, subtarget);
3063 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3064 a normal call should be emitted rather than expanding the function
3065 in-line. EXP is the expression that is a call to the builtin
3066 function; if convenient, the result should be placed in TARGET. */
3069 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3073 enum machine_mode mode;
3074 enum machine_mode mode2;
3076 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3079 arg0 = CALL_EXPR_ARG (exp, 0);
3080 arg1 = CALL_EXPR_ARG (exp, 1);
3081 mode = TYPE_MODE (TREE_TYPE (exp));
3083 /* Handle constant power. */
3085 if (TREE_CODE (arg1) == INTEGER_CST
3086 && !TREE_OVERFLOW (arg1))
3088 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3090 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3091 Otherwise, check the number of multiplications required. */
3092 if ((TREE_INT_CST_HIGH (arg1) == 0
3093 || TREE_INT_CST_HIGH (arg1) == -1)
3094 && ((n >= -1 && n <= 2)
3095 || (optimize_insn_for_speed_p ()
3096 && powi_cost (n) <= POWI_MAX_MULTS)))
3098 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3099 op0 = force_reg (mode, op0);
3100 return expand_powi (op0, mode, n);
3104 /* Emit a libcall to libgcc. */
3106 /* Mode of the 2nd argument must match that of an int. */
3107 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3109 if (target == NULL_RTX)
3110 target = gen_reg_rtx (mode);
3112 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3113 if (GET_MODE (op0) != mode)
3114 op0 = convert_to_mode (mode, op0, 0);
3115 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3116 if (GET_MODE (op1) != mode2)
3117 op1 = convert_to_mode (mode2, op1, 0);
3119 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3120 target, LCT_CONST, mode, 2,
3121 op0, mode, op1, mode2);
3126 /* Expand expression EXP which is a call to the strlen builtin. Return
3127 NULL_RTX if we failed the caller should emit a normal call, otherwise
3128 try to get the result in TARGET, if convenient. */
3131 expand_builtin_strlen (tree exp, rtx target,
3132 enum machine_mode target_mode)
3134 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3140 tree src = CALL_EXPR_ARG (exp, 0);
3141 rtx result, src_reg, char_rtx, before_strlen;
3142 enum machine_mode insn_mode = target_mode, char_mode;
3143 enum insn_code icode = CODE_FOR_nothing;
3146 /* If the length can be computed at compile-time, return it. */
3147 len = c_strlen (src, 0);
3149 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3151 /* If the length can be computed at compile-time and is constant
3152 integer, but there are side-effects in src, evaluate
3153 src for side-effects, then return len.
3154 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3155 can be optimized into: i++; x = 3; */
3156 len = c_strlen (src, 1);
3157 if (len && TREE_CODE (len) == INTEGER_CST)
3159 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3160 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3163 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3165 /* If SRC is not a pointer type, don't do this operation inline. */
3169 /* Bail out if we can't compute strlen in the right mode. */
3170 while (insn_mode != VOIDmode)
3172 icode = optab_handler (strlen_optab, insn_mode)->insn_code;
3173 if (icode != CODE_FOR_nothing)
3176 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3178 if (insn_mode == VOIDmode)
3181 /* Make a place to write the result of the instruction. */
3185 && GET_MODE (result) == insn_mode
3186 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3187 result = gen_reg_rtx (insn_mode);
3189 /* Make a place to hold the source address. We will not expand
3190 the actual source until we are sure that the expansion will
3191 not fail -- there are trees that cannot be expanded twice. */
3192 src_reg = gen_reg_rtx (Pmode);
3194 /* Mark the beginning of the strlen sequence so we can emit the
3195 source operand later. */
3196 before_strlen = get_last_insn ();
3198 char_rtx = const0_rtx;
3199 char_mode = insn_data[(int) icode].operand[2].mode;
3200 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3202 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3204 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3205 char_rtx, GEN_INT (align));
3210 /* Now that we are assured of success, expand the source. */
3212 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3214 emit_move_insn (src_reg, pat);
3219 emit_insn_after (pat, before_strlen);
3221 emit_insn_before (pat, get_insns ());
3223 /* Return the value in the proper mode for this function. */
3224 if (GET_MODE (result) == target_mode)
3226 else if (target != 0)
3227 convert_move (target, result, 0);
3229 target = convert_to_mode (target_mode, result, 0);
3235 /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the
3236 caller should emit a normal call, otherwise try to get the result
3237 in TARGET, if convenient (and in mode MODE if that's convenient). */
3240 expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode)
3242 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3244 tree type = TREE_TYPE (exp);
3245 tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0),
3246 CALL_EXPR_ARG (exp, 1), type);
3248 return expand_expr (result, target, mode, EXPAND_NORMAL);
3253 /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the
3254 caller should emit a normal call, otherwise try to get the result
3255 in TARGET, if convenient (and in mode MODE if that's convenient). */
3258 expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode)
3260 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3262 tree type = TREE_TYPE (exp);
3263 tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0),
3264 CALL_EXPR_ARG (exp, 1), type);
3266 return expand_expr (result, target, mode, EXPAND_NORMAL);
3268 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3273 /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the
3274 caller should emit a normal call, otherwise try to get the result
3275 in TARGET, if convenient (and in mode MODE if that's convenient). */
3278 expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode)
3280 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3282 tree type = TREE_TYPE (exp);
3283 tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0),
3284 CALL_EXPR_ARG (exp, 1), type);
3286 return expand_expr (result, target, mode, EXPAND_NORMAL);
3291 /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the
3292 caller should emit a normal call, otherwise try to get the result
3293 in TARGET, if convenient (and in mode MODE if that's convenient). */
3296 expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode)
3298 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3300 tree type = TREE_TYPE (exp);
3301 tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0),
3302 CALL_EXPR_ARG (exp, 1), type);
3304 return expand_expr (result, target, mode, EXPAND_NORMAL);
3309 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3310 bytes from constant string DATA + OFFSET and return it as target
3314 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3315 enum machine_mode mode)
3317 const char *str = (const char *) data;
3319 gcc_assert (offset >= 0
3320 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3321 <= strlen (str) + 1));
3323 return c_readstr (str + offset, mode);
3326 /* Expand a call EXP to the memcpy builtin.
3327 Return NULL_RTX if we failed, the caller should emit a normal call,
3328 otherwise try to get the result in TARGET, if convenient (and in
3329 mode MODE if that's convenient). */
3332 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3334 tree fndecl = get_callee_fndecl (exp);
3336 if (!validate_arglist (exp,
3337 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3341 tree dest = CALL_EXPR_ARG (exp, 0);
3342 tree src = CALL_EXPR_ARG (exp, 1);
3343 tree len = CALL_EXPR_ARG (exp, 2);
3344 const char *src_str;
3345 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3346 unsigned int dest_align
3347 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3348 rtx dest_mem, src_mem, dest_addr, len_rtx;
3349 tree result = fold_builtin_memory_op (dest, src, len,
3350 TREE_TYPE (TREE_TYPE (fndecl)),
3352 HOST_WIDE_INT expected_size = -1;
3353 unsigned int expected_align = 0;
3354 tree_ann_common_t ann;
3358 while (TREE_CODE (result) == COMPOUND_EXPR)
3360 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3362 result = TREE_OPERAND (result, 1);
3364 return expand_expr (result, target, mode, EXPAND_NORMAL);
3367 /* If DEST is not a pointer type, call the normal function. */
3368 if (dest_align == 0)
3371 /* If either SRC is not a pointer type, don't do this
3372 operation in-line. */
3376 ann = tree_common_ann (exp);
3378 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3380 if (expected_align < dest_align)
3381 expected_align = dest_align;
3382 dest_mem = get_memory_rtx (dest, len);
3383 set_mem_align (dest_mem, dest_align);
3384 len_rtx = expand_normal (len);
3385 src_str = c_getstr (src);
3387 /* If SRC is a string constant and block move would be done
3388 by pieces, we can avoid loading the string from memory
3389 and only stored the computed constants. */
3391 && GET_CODE (len_rtx) == CONST_INT
3392 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3393 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3394 CONST_CAST (char *, src_str),
3397 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3398 builtin_memcpy_read_str,
3399 CONST_CAST (char *, src_str),
3400 dest_align, false, 0);
3401 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3402 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3406 src_mem = get_memory_rtx (src, len);
3407 set_mem_align (src_mem, src_align);
3409 /* Copy word part most expediently. */
3410 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3411 CALL_EXPR_TAILCALL (exp)
3412 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3413 expected_align, expected_size);
3417 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3418 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3424 /* Expand a call EXP to the mempcpy builtin.
3425 Return NULL_RTX if we failed; the caller should emit a normal call,
3426 otherwise try to get the result in TARGET, if convenient (and in
3427 mode MODE if that's convenient). If ENDP is 0 return the
3428 destination pointer, if ENDP is 1 return the end pointer ala
3429 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3433 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3435 if (!validate_arglist (exp,
3436 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3440 tree dest = CALL_EXPR_ARG (exp, 0);
3441 tree src = CALL_EXPR_ARG (exp, 1);
3442 tree len = CALL_EXPR_ARG (exp, 2);
3443 return expand_builtin_mempcpy_args (dest, src, len,
3445 target, mode, /*endp=*/ 1);
3449 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3450 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3451 so that this can also be called without constructing an actual CALL_EXPR.
3452 TYPE is the return type of the call. The other arguments and return value
3453 are the same as for expand_builtin_mempcpy. */
3456 expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
3457 rtx target, enum machine_mode mode, int endp)
3459 /* If return value is ignored, transform mempcpy into memcpy. */
3460 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3462 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3463 tree result = build_call_expr (fn, 3, dest, src, len);
3465 while (TREE_CODE (result) == COMPOUND_EXPR)
3467 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3469 result = TREE_OPERAND (result, 1);
3471 return expand_expr (result, target, mode, EXPAND_NORMAL);
3475 const char *src_str;
3476 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3477 unsigned int dest_align
3478 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3479 rtx dest_mem, src_mem, len_rtx;
3480 tree result = fold_builtin_memory_op (dest, src, len, type, false, endp);
3484 while (TREE_CODE (result) == COMPOUND_EXPR)
3486 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3488 result = TREE_OPERAND (result, 1);
3490 return expand_expr (result, target, mode, EXPAND_NORMAL);
3493 /* If either SRC or DEST is not a pointer type, don't do this
3494 operation in-line. */
3495 if (dest_align == 0 || src_align == 0)
3498 /* If LEN is not constant, call the normal function. */
3499 if (! host_integerp (len, 1))
3502 len_rtx = expand_normal (len);
3503 src_str = c_getstr (src);
3505 /* If SRC is a string constant and block move would be done
3506 by pieces, we can avoid loading the string from memory
3507 and only stored the computed constants. */
3509 && GET_CODE (len_rtx) == CONST_INT
3510 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3511 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3512 CONST_CAST (char *, src_str),
3515 dest_mem = get_memory_rtx (dest, len);
3516 set_mem_align (dest_mem, dest_align);
3517 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3518 builtin_memcpy_read_str,
3519 CONST_CAST (char *, src_str),
3520 dest_align, false, endp);
3521 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3522 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3526 if (GET_CODE (len_rtx) == CONST_INT
3527 && can_move_by_pieces (INTVAL (len_rtx),
3528 MIN (dest_align, src_align)))
3530 dest_mem = get_memory_rtx (dest, len);
3531 set_mem_align (dest_mem, dest_align);
3532 src_mem = get_memory_rtx (src, len);
3533 set_mem_align (src_mem, src_align);
3534 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3535 MIN (dest_align, src_align), endp);
3536 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3537 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3545 /* Expand expression EXP, which is a call to the memmove builtin. Return
3546 NULL_RTX if we failed; the caller should emit a normal call. */
3549 expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore)
3551 if (!validate_arglist (exp,
3552 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3556 tree dest = CALL_EXPR_ARG (exp, 0);
3557 tree src = CALL_EXPR_ARG (exp, 1);
3558 tree len = CALL_EXPR_ARG (exp, 2);
3559 return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp),
3560 target, mode, ignore);
3564 /* Helper function to do the actual work for expand_builtin_memmove. The
3565 arguments to the builtin_memmove call DEST, SRC, and LEN are broken out
3566 so that this can also be called without constructing an actual CALL_EXPR.
3567 TYPE is the return type of the call. The other arguments and return value
3568 are the same as for expand_builtin_memmove. */
3571 expand_builtin_memmove_args (tree dest, tree src, tree len,
3572 tree type, rtx target, enum machine_mode mode,
3575 tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3);
3579 STRIP_TYPE_NOPS (result);
3580 while (TREE_CODE (result) == COMPOUND_EXPR)
3582 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3584 result = TREE_OPERAND (result, 1);
3586 return expand_expr (result, target, mode, EXPAND_NORMAL);
3589 /* Otherwise, call the normal function. */
3593 /* Expand expression EXP, which is a call to the bcopy builtin. Return
3594 NULL_RTX if we failed the caller should emit a normal call. */
3597 expand_builtin_bcopy (tree exp, int ignore)
3599 tree type = TREE_TYPE (exp);
3600 tree src, dest, size;
3602 if (!validate_arglist (exp,
3603 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3606 src = CALL_EXPR_ARG (exp, 0);
3607 dest = CALL_EXPR_ARG (exp, 1);
3608 size = CALL_EXPR_ARG (exp, 2);
3610 /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z).
3611 This is done this way so that if it isn't expanded inline, we fall
3612 back to calling bcopy instead of memmove. */
3613 return expand_builtin_memmove_args (dest, src,
3614 fold_convert (sizetype, size),
3615 type, const0_rtx, VOIDmode,
3620 # define HAVE_movstr 0
3621 # define CODE_FOR_movstr CODE_FOR_nothing
3624 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3625 we failed, the caller should emit a normal call, otherwise try to
3626 get the result in TARGET, if convenient. If ENDP is 0 return the
3627 destination pointer, if ENDP is 1 return the end pointer ala
3628 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3632 expand_movstr (tree dest, tree src, rtx target, int endp)
3638 const struct insn_data * data;
3643 dest_mem = get_memory_rtx (dest, NULL);
3644 src_mem = get_memory_rtx (src, NULL);
3647 target = force_reg (Pmode, XEXP (dest_mem, 0));
3648 dest_mem = replace_equiv_address (dest_mem, target);
3649 end = gen_reg_rtx (Pmode);
3653 if (target == 0 || target == const0_rtx)
3655 end = gen_reg_rtx (Pmode);
3663 data = insn_data + CODE_FOR_movstr;
3665 if (data->operand[0].mode != VOIDmode)
3666 end = gen_lowpart (data->operand[0].mode, end);
3668 insn = data->genfun (end, dest_mem, src_mem);
3674 /* movstr is supposed to set end to the address of the NUL
3675 terminator. If the caller requested a mempcpy-like return value,
3677 if (endp == 1 && target != const0_rtx)
3679 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3680 emit_move_insn (target, force_operand (tem, NULL_RTX));
3686 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3687 NULL_RTX if we failed the caller should emit a normal call, otherwise
3688 try to get the result in TARGET, if convenient (and in mode MODE if that's
3692 expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode)
3694 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3696 tree dest = CALL_EXPR_ARG (exp, 0);
3697 tree src = CALL_EXPR_ARG (exp, 1);
3698 return expand_builtin_strcpy_args (fndecl, dest, src, target, mode);
3703 /* Helper function to do the actual work for expand_builtin_strcpy. The
3704 arguments to the builtin_strcpy call DEST and SRC are broken out
3705 so that this can also be called without constructing an actual CALL_EXPR.
3706 The other arguments and return value are the same as for
3707 expand_builtin_strcpy. */
3710 expand_builtin_strcpy_args (tree fndecl, tree dest, tree src,
3711 rtx target, enum machine_mode mode)
3713 tree result = fold_builtin_strcpy (fndecl, dest, src, 0);
3715 return expand_expr (result, target, mode, EXPAND_NORMAL);
3716 return expand_movstr (dest, src, target, /*endp=*/0);
3720 /* Expand a call EXP to the stpcpy builtin.
3721 Return NULL_RTX if we failed the caller should emit a normal call,
3722 otherwise try to get the result in TARGET, if convenient (and in
3723 mode MODE if that's convenient). */
3726 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3730 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3733 dst = CALL_EXPR_ARG (exp, 0);
3734 src = CALL_EXPR_ARG (exp, 1);
3736 /* If return value is ignored, transform stpcpy into strcpy. */
3737 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3739 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3740 tree result = build_call_expr (fn, 2, dst, src);
3742 STRIP_NOPS (result);
3743 while (TREE_CODE (result) == COMPOUND_EXPR)
3745 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3747 result = TREE_OPERAND (result, 1);
3749 return expand_expr (result, target, mode, EXPAND_NORMAL);
3756 /* Ensure we get an actual string whose length can be evaluated at
3757 compile-time, not an expression containing a string. This is
3758 because the latter will potentially produce pessimized code
3759 when used to produce the return value. */
3760 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3761 return expand_movstr (dst, src, target, /*endp=*/2);
3763 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3764 ret = expand_builtin_mempcpy_args (dst, src, lenp1, TREE_TYPE (exp),
3765 target, mode, /*endp=*/2);
3770 if (TREE_CODE (len) == INTEGER_CST)
3772 rtx len_rtx = expand_normal (len);
3774 if (GET_CODE (len_rtx) == CONST_INT)
3776 ret = expand_builtin_strcpy_args (get_callee_fndecl (exp),
3777 dst, src, target, mode);
3783 if (mode != VOIDmode)
3784 target = gen_reg_rtx (mode);
3786 target = gen_reg_rtx (GET_MODE (ret));
3788 if (GET_MODE (target) != GET_MODE (ret))
3789 ret = gen_lowpart (GET_MODE (target), ret);
3791 ret = plus_constant (ret, INTVAL (len_rtx));
3792 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3800 return expand_movstr (dst, src, target, /*endp=*/2);
3804 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3805 bytes from constant string DATA + OFFSET and return it as target
3809 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3810 enum machine_mode mode)
3812 const char *str = (const char *) data;
3814 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3817 return c_readstr (str + offset, mode);
3820 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3821 NULL_RTX if we failed the caller should emit a normal call. */
3824 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3826 tree fndecl = get_callee_fndecl (exp);
3828 if (validate_arglist (exp,
3829 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3831 tree dest = CALL_EXPR_ARG (exp, 0);
3832 tree src = CALL_EXPR_ARG (exp, 1);
3833 tree len = CALL_EXPR_ARG (exp, 2);
3834 tree slen = c_strlen (src, 1);
3835 tree result = fold_builtin_strncpy (fndecl, dest, src, len, slen);
3839 while (TREE_CODE (result) == COMPOUND_EXPR)
3841 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3843 result = TREE_OPERAND (result, 1);
3845 return expand_expr (result, target, mode, EXPAND_NORMAL);
3848 /* We must be passed a constant len and src parameter. */
3849 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3852 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3854 /* We're required to pad with trailing zeros if the requested
3855 len is greater than strlen(s2)+1. In that case try to
3856 use store_by_pieces, if it fails, punt. */
3857 if (tree_int_cst_lt (slen, len))
3859 unsigned int dest_align
3860 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3861 const char *p = c_getstr (src);
3864 if (!p || dest_align == 0 || !host_integerp (len, 1)
3865 || !can_store_by_pieces (tree_low_cst (len, 1),
3866 builtin_strncpy_read_str,
3867 CONST_CAST (char *, p),
3871 dest_mem = get_memory_rtx (dest, len);
3872 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3873 builtin_strncpy_read_str,
3874 CONST_CAST (char *, p), dest_align, false, 0);
3875 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3876 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3883 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3884 bytes from constant string DATA + OFFSET and return it as target
3888 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3889 enum machine_mode mode)
3891 const char *c = (const char *) data;
3892 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3894 memset (p, *c, GET_MODE_SIZE (mode));
3896 return c_readstr (p, mode);
3899 /* Callback routine for store_by_pieces. Return the RTL of a register
3900 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3901 char value given in the RTL register data. For example, if mode is
3902 4 bytes wide, return the RTL for 0x01010101*data. */
3905 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3906 enum machine_mode mode)
3912 size = GET_MODE_SIZE (mode);
3916 p = XALLOCAVEC (char, size);
3917 memset (p, 1, size);
3918 coeff = c_readstr (p, mode);
3920 target = convert_to_mode (mode, (rtx) data, 1);
3921 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3922 return force_reg (mode, target);
3925 /* Expand expression EXP, which is a call to the memset builtin. Return
3926 NULL_RTX if we failed the caller should emit a normal call, otherwise
3927 try to get the result in TARGET, if convenient (and in mode MODE if that's
3931 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3933 if (!validate_arglist (exp,
3934 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3938 tree dest = CALL_EXPR_ARG (exp, 0);
3939 tree val = CALL_EXPR_ARG (exp, 1);
3940 tree len = CALL_EXPR_ARG (exp, 2);
3941 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3945 /* Helper function to do the actual work for expand_builtin_memset. The
3946 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3947 so that this can also be called without constructing an actual CALL_EXPR.
3948 The other arguments and return value are the same as for
3949 expand_builtin_memset. */
3952 expand_builtin_memset_args (tree dest, tree val, tree len,
3953 rtx target, enum machine_mode mode, tree orig_exp)
3956 enum built_in_function fcode;
3958 unsigned int dest_align;
3959 rtx dest_mem, dest_addr, len_rtx;
3960 HOST_WIDE_INT expected_size = -1;
3961 unsigned int expected_align = 0;
3962 tree_ann_common_t ann;
3964 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3966 /* If DEST is not a pointer type, don't do this operation in-line. */
3967 if (dest_align == 0)
3970 ann = tree_common_ann (orig_exp);
3972 stringop_block_profile (ann->stmt, &expected_align, &expected_size);
3974 if (expected_align < dest_align)
3975 expected_align = dest_align;
3977 /* If the LEN parameter is zero, return DEST. */
3978 if (integer_zerop (len))
3980 /* Evaluate and ignore VAL in case it has side-effects. */
3981 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3982 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3985 /* Stabilize the arguments in case we fail. */
3986 dest = builtin_save_expr (dest);
3987 val = builtin_save_expr (val);
3988 len = builtin_save_expr (len);
3990 len_rtx = expand_normal (len);
3991 dest_mem = get_memory_rtx (dest, len);
3993 if (TREE_CODE (val) != INTEGER_CST)
3997 val_rtx = expand_normal (val);
3998 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
4001 /* Assume that we can memset by pieces if we can store
4002 * the coefficients by pieces (in the required modes).
4003 * We can't pass builtin_memset_gen_str as that emits RTL. */
4005 if (host_integerp (len, 1)
4006 && can_store_by_pieces (tree_low_cst (len, 1),
4007 builtin_memset_read_str, &c, dest_align,
4010 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
4012 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4013 builtin_memset_gen_str, val_rtx, dest_align,
4016 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4017 dest_align, expected_align,
4021 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4022 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4026 if (target_char_cast (val, &c))
4031 if (host_integerp (len, 1)
4032 && can_store_by_pieces (tree_low_cst (len, 1),
4033 builtin_memset_read_str, &c, dest_align,
4035 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4036 builtin_memset_read_str, &c, dest_align, true, 0);
4037 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
4038 dest_align, expected_align,
4042 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4043 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4047 set_mem_align (dest_mem, dest_align);
4048 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4049 CALL_EXPR_TAILCALL (orig_exp)
4050 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4051 expected_align, expected_size);
4055 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4056 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4062 fndecl = get_callee_fndecl (orig_exp);
4063 fcode = DECL_FUNCTION_CODE (fndecl);
4064 if (fcode == BUILT_IN_MEMSET)
4065 fn = build_call_expr (fndecl, 3, dest, val, len);
4066 else if (fcode == BUILT_IN_BZERO)
4067 fn = build_call_expr (fndecl, 2, dest, len);
4070 if (TREE_CODE (fn) == CALL_EXPR)
4071 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4072 return expand_call (fn, target, target == const0_rtx);
4075 /* Expand expression EXP, which is a call to the bzero builtin. Return
4076 NULL_RTX if we failed the caller should emit a normal call. */
4079 expand_builtin_bzero (tree exp)
4083 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4086 dest = CALL_EXPR_ARG (exp, 0);
4087 size = CALL_EXPR_ARG (exp, 1);
4089 /* New argument list transforming bzero(ptr x, int y) to
4090 memset(ptr x, int 0, size_t y). This is done this way
4091 so that if it isn't expanded inline, we fallback to
4092 calling bzero instead of memset. */
4094 return expand_builtin_memset_args (dest, integer_zero_node,
4095 fold_convert (sizetype, size),
4096 const0_rtx, VOIDmode, exp);
4099 /* Expand a call to the memchr builtin. Return NULL_RTX if we failed the
4100 caller should emit a normal call, otherwise try to get the result
4101 in TARGET, if convenient (and in mode MODE if that's convenient). */
4104 expand_builtin_memchr (tree exp, rtx target, enum machine_mode mode)
4106 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE,
4107 INTEGER_TYPE, VOID_TYPE))
4109 tree type = TREE_TYPE (exp);
4110 tree result = fold_builtin_memchr (CALL_EXPR_ARG (exp, 0),
4111 CALL_EXPR_ARG (exp, 1),
4112 CALL_EXPR_ARG (exp, 2), type);
4114 return expand_expr (result, target, mode, EXPAND_NORMAL);
4119 /* Expand expression EXP, which is a call to the memcmp built-in function.
4120 Return NULL_RTX if we failed and the
4121 caller should emit a normal call, otherwise try to get the result in
4122 TARGET, if convenient (and in mode MODE, if that's convenient). */
4125 expand_builtin_memcmp (tree exp, rtx target, enum machine_mode mode)
4127 if (!validate_arglist (exp,
4128 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4132 tree result = fold_builtin_memcmp (CALL_EXPR_ARG (exp, 0),
4133 CALL_EXPR_ARG (exp, 1),
4134 CALL_EXPR_ARG (exp, 2));
4136 return expand_expr (result, target, mode, EXPAND_NORMAL);
4139 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4141 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4144 tree arg1 = CALL_EXPR_ARG (exp, 0);
4145 tree arg2 = CALL_EXPR_ARG (exp, 1);
4146 tree len = CALL_EXPR_ARG (exp, 2);
4149 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4151 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4152 enum machine_mode insn_mode;
4154 #ifdef HAVE_cmpmemsi
4156 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4159 #ifdef HAVE_cmpstrnsi
4161 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4166 /* If we don't have POINTER_TYPE, call the function. */
4167 if (arg1_align == 0 || arg2_align == 0)
4170 /* Make a place to write the result of the instruction. */
4173 && REG_P (result) && GET_MODE (result) == insn_mode
4174 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4175 result = gen_reg_rtx (insn_mode);
4177 arg1_rtx = get_memory_rtx (arg1, len);
4178 arg2_rtx = get_memory_rtx (arg2, len);
4179 arg3_rtx = expand_normal (len);
4181 /* Set MEM_SIZE as appropriate. */
4182 if (GET_CODE (arg3_rtx) == CONST_INT)
4184 set_mem_size (arg1_rtx, arg3_rtx);
4185 set_mem_size (arg2_rtx, arg3_rtx);
4188 #ifdef HAVE_cmpmemsi
4190 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4191 GEN_INT (MIN (arg1_align, arg2_align)));
4194 #ifdef HAVE_cmpstrnsi
4196 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4197 GEN_INT (MIN (arg1_align, arg2_align)));
4205 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4206 TYPE_MODE (integer_type_node), 3,
4207 XEXP (arg1_rtx, 0), Pmode,
4208 XEXP (arg2_rtx, 0), Pmode,
4209 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4210 TYPE_UNSIGNED (sizetype)),
4211 TYPE_MODE (sizetype));
4213 /* Return the value in the proper mode for this function. */
4214 mode = TYPE_MODE (TREE_TYPE (exp));
4215 if (GET_MODE (result) == mode)
4217 else if (target != 0)
4219 convert_move (target, result, 0);
4223 return convert_to_mode (mode, result, 0);
4230 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4231 if we failed the caller should emit a normal call, otherwise try to get
4232 the result in TARGET, if convenient. */
4235 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
4237 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4241 tree result = fold_builtin_strcmp (CALL_EXPR_ARG (exp, 0),
4242 CALL_EXPR_ARG (exp, 1));
4244 return expand_expr (result, target, mode, EXPAND_NORMAL);
4247 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4248 if (cmpstr_optab[SImode] != CODE_FOR_nothing
4249 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
4251 rtx arg1_rtx, arg2_rtx;
4252 rtx result, insn = NULL_RTX;
4254 tree arg1 = CALL_EXPR_ARG (exp, 0);
4255 tree arg2 = CALL_EXPR_ARG (exp, 1);
4258 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4260 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4262 /* If we don't have POINTER_TYPE, call the function. */
4263 if (arg1_align == 0 || arg2_align == 0)
4266 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4267 arg1 = builtin_save_expr (arg1);
4268 arg2 = builtin_save_expr (arg2);
4270 arg1_rtx = get_memory_rtx (arg1, NULL);
4271 arg2_rtx = get_memory_rtx (arg2, NULL);
4273 #ifdef HAVE_cmpstrsi
4274 /* Try to call cmpstrsi. */
4277 enum machine_mode insn_mode
4278 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4280 /* Make a place to write the result of the instruction. */
4283 && REG_P (result) && GET_MODE (result) == insn_mode
4284 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4285 result = gen_reg_rtx (insn_mode);
4287 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4288 GEN_INT (MIN (arg1_align, arg2_align)));
4291 #ifdef HAVE_cmpstrnsi
4292 /* Try to determine at least one length and call cmpstrnsi. */
4293 if (!insn && HAVE_cmpstrnsi)
4298 enum machine_mode insn_mode
4299 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4300 tree len1 = c_strlen (arg1, 1);
4301 tree len2 = c_strlen (arg2, 1);
4304 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4306 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4308 /* If we don't have a constant length for the first, use the length
4309 of the second, if we know it. We don't require a constant for
4310 this case; some cost analysis could be done if both are available
4311 but neither is constant. For now, assume they're equally cheap,
4312 unless one has side effects. If both strings have constant lengths,
4319 else if (TREE_SIDE_EFFECTS (len1))
4321 else if (TREE_SIDE_EFFECTS (len2))
4323 else if (TREE_CODE (len1) != INTEGER_CST)
4325 else if (TREE_CODE (len2) != INTEGER_CST)
4327 else if (tree_int_cst_lt (len1, len2))
4332 /* If both arguments have side effects, we cannot optimize. */
4333 if (!len || TREE_SIDE_EFFECTS (len))
4336 arg3_rtx = expand_normal (len);
4338 /* Make a place to write the result of the instruction. */
4341 && REG_P (result) && GET_MODE (result) == insn_mode
4342 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4343 result = gen_reg_rtx (insn_mode);
4345 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4346 GEN_INT (MIN (arg1_align, arg2_align)));
4354 /* Return the value in the proper mode for this function. */
4355 mode = TYPE_MODE (TREE_TYPE (exp));
4356 if (GET_MODE (result) == mode)
4359 return convert_to_mode (mode, result, 0);
4360 convert_move (target, result, 0);
4364 /* Expand the library call ourselves using a stabilized argument
4365 list to avoid re-evaluating the function's arguments twice. */
4366 #ifdef HAVE_cmpstrnsi
4369 fndecl = get_callee_fndecl (exp);
4370 fn = build_call_expr (fndecl, 2, arg1, arg2);
4371 if (TREE_CODE (fn) == CALL_EXPR)
4372 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4373 return expand_call (fn, target, target == const0_rtx);
4379 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4380 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4381 the result in TARGET, if convenient. */
4384 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4386 if (!validate_arglist (exp,
4387 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4391 tree result = fold_builtin_strncmp (CALL_EXPR_ARG (exp, 0),
4392 CALL_EXPR_ARG (exp, 1),
4393 CALL_EXPR_ARG (exp, 2));
4395 return expand_expr (result, target, mode, EXPAND_NORMAL);
4398 /* If c_strlen can determine an expression for one of the string
4399 lengths, and it doesn't have side effects, then emit cmpstrnsi
4400 using length MIN(strlen(string)+1, arg3). */
4401 #ifdef HAVE_cmpstrnsi
4404 tree len, len1, len2;
4405 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4408 tree arg1 = CALL_EXPR_ARG (exp, 0);
4409 tree arg2 = CALL_EXPR_ARG (exp, 1);
4410 tree arg3 = CALL_EXPR_ARG (exp, 2);
4413 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4415 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4416 enum machine_mode insn_mode
4417 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4419 len1 = c_strlen (arg1, 1);
4420 len2 = c_strlen (arg2, 1);
4423 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4425 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4427 /* If we don't have a constant length for the first, use the length
4428 of the second, if we know it. We don't require a constant for
4429 this case; some cost analysis could be done if both are available
4430 but neither is constant. For now, assume they're equally cheap,
4431 unless one has side effects. If both strings have constant lengths,
4438 else if (TREE_SIDE_EFFECTS (len1))
4440 else if (TREE_SIDE_EFFECTS (len2))
4442 else if (TREE_CODE (len1) != INTEGER_CST)
4444 else if (TREE_CODE (len2) != INTEGER_CST)
4446 else if (tree_int_cst_lt (len1, len2))
4451 /* If both arguments have side effects, we cannot optimize. */
4452 if (!len || TREE_SIDE_EFFECTS (len))
4455 /* The actual new length parameter is MIN(len,arg3). */
4456 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4457 fold_convert (TREE_TYPE (len), arg3));
4459 /* If we don't have POINTER_TYPE, call the function. */
4460 if (arg1_align == 0 || arg2_align == 0)
4463 /* Make a place to write the result of the instruction. */
4466 && REG_P (result) && GET_MODE (result) == insn_mode
4467 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4468 result = gen_reg_rtx (insn_mode);
4470 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4471 arg1 = builtin_save_expr (arg1);
4472 arg2 = builtin_save_expr (arg2);
4473 len = builtin_save_expr (len);
4475 arg1_rtx = get_memory_rtx (arg1, len);
4476 arg2_rtx = get_memory_rtx (arg2, len);
4477 arg3_rtx = expand_normal (len);
4478 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4479 GEN_INT (MIN (arg1_align, arg2_align)));
4484 /* Return the value in the proper mode for this function. */
4485 mode = TYPE_MODE (TREE_TYPE (exp));
4486 if (GET_MODE (result) == mode)
4489 return convert_to_mode (mode, result, 0);
4490 convert_move (target, result, 0);
4494 /* Expand the library call ourselves using a stabilized argument
4495 list to avoid re-evaluating the function's arguments twice. */
4496 fndecl = get_callee_fndecl (exp);
4497 fn = build_call_expr (fndecl, 3, arg1, arg2, len);
4498 if (TREE_CODE (fn) == CALL_EXPR)
4499 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4500 return expand_call (fn, target, target == const0_rtx);
4506 /* Expand expression EXP, which is a call to the strcat builtin.
4507 Return NULL_RTX if we failed the caller should emit a normal call,
4508 otherwise try to get the result in TARGET, if convenient. */
4511 expand_builtin_strcat (tree fndecl, tree exp, rtx target, enum machine_mode mode)
4513 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4517 tree dst = CALL_EXPR_ARG (exp, 0);
4518 tree src = CALL_EXPR_ARG (exp, 1);
4519 const char *p = c_getstr (src);
4521 /* If the string length is zero, return the dst parameter. */
4522 if (p && *p == '\0')
4523 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4525 if (optimize_insn_for_speed_p ())
4527 /* See if we can store by pieces into (dst + strlen(dst)). */
4528 tree newsrc, newdst,
4529 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4532 /* Stabilize the argument list. */
4533 newsrc = builtin_save_expr (src);
4534 dst = builtin_save_expr (dst);
4538 /* Create strlen (dst). */
4539 newdst = build_call_expr (strlen_fn, 1, dst);
4540 /* Create (dst p+ strlen (dst)). */
4542 newdst = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4543 newdst = builtin_save_expr (newdst);
4545 if (!expand_builtin_strcpy_args (fndecl, newdst, newsrc, target, mode))
4547 end_sequence (); /* Stop sequence. */
4551 /* Output the entire sequence. */
4552 insns = get_insns ();
4556 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4563 /* Expand expression EXP, which is a call to the strncat builtin.
4564 Return NULL_RTX if we failed the caller should emit a normal call,
4565 otherwise try to get the result in TARGET, if convenient. */
4568 expand_builtin_strncat (tree exp, rtx target, enum machine_mode mode)
4570 if (validate_arglist (exp,
4571 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4573 tree result = fold_builtin_strncat (CALL_EXPR_ARG (exp, 0),
4574 CALL_EXPR_ARG (exp, 1),
4575 CALL_EXPR_ARG (exp, 2));
4577 return expand_expr (result, target, mode, EXPAND_NORMAL);
4582 /* Expand expression EXP, which is a call to the strspn builtin.
4583 Return NULL_RTX if we failed the caller should emit a normal call,
4584 otherwise try to get the result in TARGET, if convenient. */
4587 expand_builtin_strspn (tree exp, rtx target, enum machine_mode mode)
4589 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4591 tree result = fold_builtin_strspn (CALL_EXPR_ARG (exp, 0),
4592 CALL_EXPR_ARG (exp, 1));
4594 return expand_expr (result, target, mode, EXPAND_NORMAL);
4599 /* Expand expression EXP, which is a call to the strcspn builtin.
4600 Return NULL_RTX if we failed the caller should emit a normal call,
4601 otherwise try to get the result in TARGET, if convenient. */
4604 expand_builtin_strcspn (tree exp, rtx target, enum machine_mode mode)
4606 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4608 tree result = fold_builtin_strcspn (CALL_EXPR_ARG (exp, 0),
4609 CALL_EXPR_ARG (exp, 1));
4611 return expand_expr (result, target, mode, EXPAND_NORMAL);
4616 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4617 if that's convenient. */
4620 expand_builtin_saveregs (void)
4624 /* Don't do __builtin_saveregs more than once in a function.
4625 Save the result of the first call and reuse it. */
4626 if (saveregs_value != 0)
4627 return saveregs_value;
4629 /* When this function is called, it means that registers must be
4630 saved on entry to this function. So we migrate the call to the
4631 first insn of this function. */
4635 /* Do whatever the machine needs done in this case. */
4636 val = targetm.calls.expand_builtin_saveregs ();
4641 saveregs_value = val;
4643 /* Put the insns after the NOTE that starts the function. If this
4644 is inside a start_sequence, make the outer-level insn chain current, so
4645 the code is placed at the start of the function. */
4646 push_topmost_sequence ();
4647 emit_insn_after (seq, entry_of_function ());
4648 pop_topmost_sequence ();
4653 /* __builtin_args_info (N) returns word N of the arg space info
4654 for the current function. The number and meanings of words
4655 is controlled by the definition of CUMULATIVE_ARGS. */
4658 expand_builtin_args_info (tree exp)
4660 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4661 int *word_ptr = (int *) &crtl->args.info;
4663 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4665 if (call_expr_nargs (exp) != 0)
4667 if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0))
4668 error ("argument of %<__builtin_args_info%> must be constant");
4671 HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0);
4673 if (wordnum < 0 || wordnum >= nwords)
4674 error ("argument of %<__builtin_args_info%> out of range");
4676 return GEN_INT (word_ptr[wordnum]);
4680 error ("missing argument in %<__builtin_args_info%>");
4685 /* Expand a call to __builtin_next_arg. */
4688 expand_builtin_next_arg (void)
4690 /* Checking arguments is already done in fold_builtin_next_arg
4691 that must be called before this function. */
4692 return expand_binop (ptr_mode, add_optab,
4693 crtl->args.internal_arg_pointer,
4694 crtl->args.arg_offset_rtx,
4695 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4698 /* Make it easier for the backends by protecting the valist argument
4699 from multiple evaluations. */
4702 stabilize_va_list (tree valist, int needs_lvalue)
4704 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4706 gcc_assert (vatype != NULL_TREE);
4708 if (TREE_CODE (vatype) == ARRAY_TYPE)
4710 if (TREE_SIDE_EFFECTS (valist))
4711 valist = save_expr (valist);
4713 /* For this case, the backends will be expecting a pointer to
4714 vatype, but it's possible we've actually been given an array
4715 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4717 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4719 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4720 valist = build_fold_addr_expr_with_type (valist, p1);
4729 if (! TREE_SIDE_EFFECTS (valist))
4732 pt = build_pointer_type (vatype);
4733 valist = fold_build1 (ADDR_EXPR, pt, valist);
4734 TREE_SIDE_EFFECTS (valist) = 1;
4737 if (TREE_SIDE_EFFECTS (valist))
4738 valist = save_expr (valist);
4739 valist = build_fold_indirect_ref (valist);
4745 /* The "standard" definition of va_list is void*. */
4748 std_build_builtin_va_list (void)
4750 return ptr_type_node;
4753 /* The "standard" abi va_list is va_list_type_node. */
4756 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4758 return va_list_type_node;
4761 /* The "standard" type of va_list is va_list_type_node. */
4764 std_canonical_va_list_type (tree type)
4768 if (INDIRECT_REF_P (type))
4769 type = TREE_TYPE (type);
4770 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4771 type = TREE_TYPE (type);
4772 wtype = va_list_type_node;
4774 /* Treat structure va_list types. */
4775 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4776 htype = TREE_TYPE (htype);
4777 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4779 /* If va_list is an array type, the argument may have decayed
4780 to a pointer type, e.g. by being passed to another function.
4781 In that case, unwrap both types so that we can compare the
4782 underlying records. */
4783 if (TREE_CODE (htype) == ARRAY_TYPE
4784 || POINTER_TYPE_P (htype))
4786 wtype = TREE_TYPE (wtype);
4787 htype = TREE_TYPE (htype);
4790 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4791 return va_list_type_node;
4796 /* The "standard" implementation of va_start: just assign `nextarg' to
4800 std_expand_builtin_va_start (tree valist, rtx nextarg)
4802 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4803 convert_move (va_r, nextarg, 0);
4806 /* Expand EXP, a call to __builtin_va_start. */
4809 expand_builtin_va_start (tree exp)
4814 if (call_expr_nargs (exp) < 2)
4816 error ("too few arguments to function %<va_start%>");
4820 if (fold_builtin_next_arg (exp, true))
4823 nextarg = expand_builtin_next_arg ();
4824 valist = stabilize_va_list (CALL_EXPR_ARG (exp, 0), 1);
4826 if (targetm.expand_builtin_va_start)
4827 targetm.expand_builtin_va_start (valist, nextarg);
4829 std_expand_builtin_va_start (valist, nextarg);
4834 /* The "standard" implementation of va_arg: read the value from the
4835 current (padded) address and increment by the (padded) size. */
4838 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4841 tree addr, t, type_size, rounded_size, valist_tmp;
4842 unsigned HOST_WIDE_INT align, boundary;
4845 #ifdef ARGS_GROW_DOWNWARD
4846 /* All of the alignment and movement below is for args-grow-up machines.
4847 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4848 implement their own specialized gimplify_va_arg_expr routines. */
4852 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4854 type = build_pointer_type (type);
4856 align = PARM_BOUNDARY / BITS_PER_UNIT;
4857 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4859 /* When we align parameter on stack for caller, if the parameter
4860 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4861 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4862 here with caller. */
4863 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4864 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4866 boundary /= BITS_PER_UNIT;
4868 /* Hoist the valist value into a temporary for the moment. */
4869 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4871 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4872 requires greater alignment, we must perform dynamic alignment. */
4873 if (boundary > align
4874 && !integer_zerop (TYPE_SIZE (type)))
4876 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4877 fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist),
4878 valist_tmp, size_int (boundary - 1)));
4879 gimplify_and_add (t, pre_p);
4881 t = fold_convert (sizetype, valist_tmp);
4882 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4883 fold_convert (TREE_TYPE (valist),
4884 fold_build2 (BIT_AND_EXPR, sizetype, t,
4885 size_int (-boundary))));
4886 gimplify_and_add (t, pre_p);
4891 /* If the actual alignment is less than the alignment of the type,
4892 adjust the type accordingly so that we don't assume strict alignment
4893 when dereferencing the pointer. */
4894 boundary *= BITS_PER_UNIT;
4895 if (boundary < TYPE_ALIGN (type))
4897 type = build_variant_type_copy (type);
4898 TYPE_ALIGN (type) = boundary;
4901 /* Compute the rounded size of the type. */
4902 type_size = size_in_bytes (type);
4903 rounded_size = round_up (type_size, align);
4905 /* Reduce rounded_size so it's sharable with the postqueue. */
4906 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4910 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4912 /* Small args are padded downward. */
4913 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4914 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4915 size_binop (MINUS_EXPR, rounded_size, type_size));
4916 addr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr, t);
4919 /* Compute new value for AP. */
4920 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4921 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4922 gimplify_and_add (t, pre_p);
4924 addr = fold_convert (build_pointer_type (type), addr);
4927 addr = build_va_arg_indirect_ref (addr);
4929 return build_va_arg_indirect_ref (addr);
4932 /* Build an indirect-ref expression over the given TREE, which represents a
4933 piece of a va_arg() expansion. */
4935 build_va_arg_indirect_ref (tree addr)
4937 addr = build_fold_indirect_ref (addr);
4939 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4945 /* Return a dummy expression of type TYPE in order to keep going after an
4949 dummy_object (tree type)
4951 tree t = build_int_cst (build_pointer_type (type), 0);
4952 return build1 (INDIRECT_REF, type, t);
4955 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4956 builtin function, but a very special sort of operator. */
4958 enum gimplify_status
4959 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4961 tree promoted_type, have_va_type;
4962 tree valist = TREE_OPERAND (*expr_p, 0);
4963 tree type = TREE_TYPE (*expr_p);
4966 /* Verify that valist is of the proper type. */
4967 have_va_type = TREE_TYPE (valist);
4968 if (have_va_type == error_mark_node)
4970 have_va_type = targetm.canonical_va_list_type (have_va_type);
4972 if (have_va_type == NULL_TREE)
4974 error ("first argument to %<va_arg%> not of type %<va_list%>");
4978 /* Generate a diagnostic for requesting data of a type that cannot
4979 be passed through `...' due to type promotion at the call site. */
4980 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4983 static bool gave_help;
4986 /* Unfortunately, this is merely undefined, rather than a constraint
4987 violation, so we cannot make this an error. If this call is never
4988 executed, the program is still strictly conforming. */
4989 warned = warning (0, "%qT is promoted to %qT when passed through %<...%>",
4990 type, promoted_type);
4991 if (!gave_help && warned)
4994 inform (input_location, "(so you should pass %qT not %qT to %<va_arg%>)",
4995 promoted_type, type);
4998 /* We can, however, treat "undefined" any way we please.
4999 Call abort to encourage the user to fix the program. */
5001 inform (input_location, "if this code is reached, the program will abort");
5002 t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
5003 gimplify_and_add (t, pre_p);
5005 /* This is dead code, but go ahead and finish so that the
5006 mode of the result comes out right. */
5007 *expr_p = dummy_object (type);
5012 /* Make it easier for the backends by protecting the valist argument
5013 from multiple evaluations. */
5014 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
5016 /* For this case, the backends will be expecting a pointer to
5017 TREE_TYPE (abi), but it's possible we've
5018 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
5020 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5022 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
5023 valist = build_fold_addr_expr_with_type (valist, p1);
5026 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
5029 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
5031 if (!targetm.gimplify_va_arg_expr)
5032 /* FIXME: Once most targets are converted we should merely
5033 assert this is non-null. */
5036 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
5041 /* Expand EXP, a call to __builtin_va_end. */
5044 expand_builtin_va_end (tree exp)
5046 tree valist = CALL_EXPR_ARG (exp, 0);
5048 /* Evaluate for side effects, if needed. I hate macros that don't
5050 if (TREE_SIDE_EFFECTS (valist))
5051 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5056 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5057 builtin rather than just as an assignment in stdarg.h because of the
5058 nastiness of array-type va_list types. */
5061 expand_builtin_va_copy (tree exp)
5065 dst = CALL_EXPR_ARG (exp, 0);
5066 src = CALL_EXPR_ARG (exp, 1);
5068 dst = stabilize_va_list (dst, 1);
5069 src = stabilize_va_list (src, 0);
5071 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5073 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5075 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5076 TREE_SIDE_EFFECTS (t) = 1;
5077 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5081 rtx dstb, srcb, size;
5083 /* Evaluate to pointers. */
5084 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5085 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5086 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5087 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5089 dstb = convert_memory_address (Pmode, dstb);
5090 srcb = convert_memory_address (Pmode, srcb);
5092 /* "Dereference" to BLKmode memories. */
5093 dstb = gen_rtx_MEM (BLKmode, dstb);
5094 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5095 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5096 srcb = gen_rtx_MEM (BLKmode, srcb);
5097 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5098 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5101 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5107 /* Expand a call to one of the builtin functions __builtin_frame_address or
5108 __builtin_return_address. */
5111 expand_builtin_frame_address (tree fndecl, tree exp)
5113 /* The argument must be a nonnegative integer constant.
5114 It counts the number of frames to scan up the stack.
5115 The value is the return address saved in that frame. */
5116 if (call_expr_nargs (exp) == 0)
5117 /* Warning about missing arg was already issued. */
5119 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
5121 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5122 error ("invalid argument to %<__builtin_frame_address%>");
5124 error ("invalid argument to %<__builtin_return_address%>");
5130 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
5131 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
5133 /* Some ports cannot access arbitrary stack frames. */
5136 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5137 warning (0, "unsupported argument to %<__builtin_frame_address%>");
5139 warning (0, "unsupported argument to %<__builtin_return_address%>");
5143 /* For __builtin_frame_address, return what we've got. */
5144 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5148 && ! CONSTANT_P (tem))
5149 tem = copy_to_mode_reg (Pmode, tem);
5154 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
5155 we failed and the caller should emit a normal call, otherwise try to get
5156 the result in TARGET, if convenient. */
5159 expand_builtin_alloca (tree exp, rtx target)
5164 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
5165 should always expand to function calls. These can be intercepted
5170 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5173 /* Compute the argument. */
5174 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5176 /* Allocate the desired space. */
5177 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
5178 result = convert_memory_address (ptr_mode, result);
5183 /* Expand a call to a bswap builtin with argument ARG0. MODE
5184 is the mode to expand with. */
5187 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5189 enum machine_mode mode;
5193 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5196 arg = CALL_EXPR_ARG (exp, 0);
5197 mode = TYPE_MODE (TREE_TYPE (arg));
5198 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5200 target = expand_unop (mode, bswap_optab, op0, target, 1);
5202 gcc_assert (target);
5204 return convert_to_mode (mode, target, 0);
5207 /* Expand a call to a unary builtin in EXP.
5208 Return NULL_RTX if a normal call should be emitted rather than expanding the
5209 function in-line. If convenient, the result should be placed in TARGET.
5210 SUBTARGET may be used as the target for computing one of EXP's operands. */
5213 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5214 rtx subtarget, optab op_optab)
5218 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5221 /* Compute the argument. */
5222 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5223 VOIDmode, EXPAND_NORMAL);
5224 /* Compute op, into TARGET if possible.
5225 Set TARGET to wherever the result comes back. */
5226 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5227 op_optab, op0, target, 1);
5228 gcc_assert (target);
5230 return convert_to_mode (target_mode, target, 0);
5233 /* If the string passed to fputs is a constant and is one character
5234 long, we attempt to transform this call into __builtin_fputc(). */
5237 expand_builtin_fputs (tree exp, rtx target, bool unlocked)
5239 /* Verify the arguments in the original call. */
5240 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5242 tree result = fold_builtin_fputs (CALL_EXPR_ARG (exp, 0),
5243 CALL_EXPR_ARG (exp, 1),
5244 (target == const0_rtx),
5245 unlocked, NULL_TREE);
5247 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
5252 /* Expand a call to __builtin_expect. We just return our argument
5253 as the builtin_expect semantic should've been already executed by
5254 tree branch prediction pass. */
5257 expand_builtin_expect (tree exp, rtx target)
5261 if (call_expr_nargs (exp) < 2)
5263 arg = CALL_EXPR_ARG (exp, 0);
5264 c = CALL_EXPR_ARG (exp, 1);
5266 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5267 /* When guessing was done, the hints should be already stripped away. */
5268 gcc_assert (!flag_guess_branch_prob
5269 || optimize == 0 || errorcount || sorrycount);
5274 expand_builtin_trap (void)
5278 emit_insn (gen_trap ());
5281 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5285 /* Expand EXP, a call to fabs, fabsf or fabsl.
5286 Return NULL_RTX if a normal call should be emitted rather than expanding
5287 the function inline. If convenient, the result should be placed
5288 in TARGET. SUBTARGET may be used as the target for computing
5292 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5294 enum machine_mode mode;
5298 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5301 arg = CALL_EXPR_ARG (exp, 0);
5302 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5303 mode = TYPE_MODE (TREE_TYPE (arg));
5304 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5305 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5308 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5309 Return NULL is a normal call should be emitted rather than expanding the
5310 function inline. If convenient, the result should be placed in TARGET.
5311 SUBTARGET may be used as the target for computing the operand. */
5314 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5319 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5322 arg = CALL_EXPR_ARG (exp, 0);
5323 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5325 arg = CALL_EXPR_ARG (exp, 1);
5326 op1 = expand_normal (arg);
5328 return expand_copysign (op0, op1, target);
5331 /* Create a new constant string literal and return a char* pointer to it.
5332 The STRING_CST value is the LEN characters at STR. */
5334 build_string_literal (int len, const char *str)
5336 tree t, elem, index, type;
5338 t = build_string (len, str);
5339 elem = build_type_variant (char_type_node, 1, 0);
5340 index = build_index_type (size_int (len - 1));
5341 type = build_array_type (elem, index);
5342 TREE_TYPE (t) = type;
5343 TREE_CONSTANT (t) = 1;
5344 TREE_READONLY (t) = 1;
5345 TREE_STATIC (t) = 1;
5347 type = build_pointer_type (elem);
5348 t = build1 (ADDR_EXPR, type,
5349 build4 (ARRAY_REF, elem,
5350 t, integer_zero_node, NULL_TREE, NULL_TREE));
5354 /* Expand EXP, a call to printf or printf_unlocked.
5355 Return NULL_RTX if a normal call should be emitted rather than transforming
5356 the function inline. If convenient, the result should be placed in
5357 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
5360 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
5363 /* If we're using an unlocked function, assume the other unlocked
5364 functions exist explicitly. */
5365 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
5366 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
5367 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
5368 : implicit_built_in_decls[BUILT_IN_PUTS];
5369 const char *fmt_str;
5372 int nargs = call_expr_nargs (exp);
5374 /* If the return value is used, don't do the transformation. */
5375 if (target != const0_rtx)
5378 /* Verify the required arguments in the original call. */
5381 fmt = CALL_EXPR_ARG (exp, 0);
5382 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5385 /* Check whether the format is a literal string constant. */
5386 fmt_str = c_getstr (fmt);
5387 if (fmt_str == NULL)
5390 if (!init_target_chars ())
5393 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5394 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5397 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1))))
5400 fn = build_call_expr (fn_puts, 1, CALL_EXPR_ARG (exp, 1));
5402 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5403 else if (strcmp (fmt_str, target_percent_c) == 0)
5406 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE)
5409 fn = build_call_expr (fn_putchar, 1, CALL_EXPR_ARG (exp, 1));
5413 /* We can't handle anything else with % args or %% ... yet. */
5414 if (strchr (fmt_str, target_percent))
5420 /* If the format specifier was "", printf does nothing. */
5421 if (fmt_str[0] == '\0')
5423 /* If the format specifier has length of 1, call putchar. */
5424 if (fmt_str[1] == '\0')
5426 /* Given printf("c"), (where c is any one character,)
5427 convert "c"[0] to an int and pass that to the replacement
5429 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5431 fn = build_call_expr (fn_putchar, 1, arg);
5435 /* If the format specifier was "string\n", call puts("string"). */
5436 size_t len = strlen (fmt_str);
5437 if ((unsigned char)fmt_str[len - 1] == target_newline)
5439 /* Create a NUL-terminated string that's one char shorter
5440 than the original, stripping off the trailing '\n'. */
5441 char *newstr = XALLOCAVEC (char, len);
5442 memcpy (newstr, fmt_str, len - 1);
5443 newstr[len - 1] = 0;
5444 arg = build_string_literal (len, newstr);
5446 fn = build_call_expr (fn_puts, 1, arg);
5449 /* We'd like to arrange to call fputs(string,stdout) here,
5450 but we need stdout and don't have a way to get it yet. */
5457 if (TREE_CODE (fn) == CALL_EXPR)
5458 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5459 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5462 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5463 Return NULL_RTX if a normal call should be emitted rather than transforming
5464 the function inline. If convenient, the result should be placed in
5465 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5468 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5471 /* If we're using an unlocked function, assume the other unlocked
5472 functions exist explicitly. */
5473 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5474 : implicit_built_in_decls[BUILT_IN_FPUTC];
5475 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5476 : implicit_built_in_decls[BUILT_IN_FPUTS];
5477 const char *fmt_str;
5480 int nargs = call_expr_nargs (exp);
5482 /* If the return value is used, don't do the transformation. */
5483 if (target != const0_rtx)
5486 /* Verify the required arguments in the original call. */
5489 fp = CALL_EXPR_ARG (exp, 0);
5490 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5492 fmt = CALL_EXPR_ARG (exp, 1);
5493 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5496 /* Check whether the format is a literal string constant. */
5497 fmt_str = c_getstr (fmt);
5498 if (fmt_str == NULL)
5501 if (!init_target_chars ())
5504 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5505 if (strcmp (fmt_str, target_percent_s) == 0)
5508 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2))))
5510 arg = CALL_EXPR_ARG (exp, 2);
5512 fn = build_call_expr (fn_fputs, 2, arg, fp);
5514 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5515 else if (strcmp (fmt_str, target_percent_c) == 0)
5518 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE)
5520 arg = CALL_EXPR_ARG (exp, 2);
5522 fn = build_call_expr (fn_fputc, 2, arg, fp);
5526 /* We can't handle anything else with % args or %% ... yet. */
5527 if (strchr (fmt_str, target_percent))
5533 /* If the format specifier was "", fprintf does nothing. */
5534 if (fmt_str[0] == '\0')
5536 /* Evaluate and ignore FILE* argument for side-effects. */
5537 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5541 /* When "string" doesn't contain %, replace all cases of
5542 fprintf(stream,string) with fputs(string,stream). The fputs
5543 builtin will take care of special cases like length == 1. */
5545 fn = build_call_expr (fn_fputs, 2, fmt, fp);
5550 if (TREE_CODE (fn) == CALL_EXPR)
5551 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5552 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5555 /* Expand a call EXP to sprintf. Return NULL_RTX if
5556 a normal call should be emitted rather than expanding the function
5557 inline. If convenient, the result should be placed in TARGET with
5561 expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode)
5564 const char *fmt_str;
5565 int nargs = call_expr_nargs (exp);
5567 /* Verify the required arguments in the original call. */
5570 dest = CALL_EXPR_ARG (exp, 0);
5571 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5573 fmt = CALL_EXPR_ARG (exp, 0);
5574 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5577 /* Check whether the format is a literal string constant. */
5578 fmt_str = c_getstr (fmt);
5579 if (fmt_str == NULL)
5582 if (!init_target_chars ())
5585 /* If the format doesn't contain % args or %%, use strcpy. */
5586 if (strchr (fmt_str, target_percent) == 0)
5588 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5591 if ((nargs > 2) || ! fn)
5593 expand_expr (build_call_expr (fn, 2, dest, fmt),
5594 const0_rtx, VOIDmode, EXPAND_NORMAL);
5595 if (target == const0_rtx)
5597 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5598 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5600 /* If the format is "%s", use strcpy if the result isn't used. */
5601 else if (strcmp (fmt_str, target_percent_s) == 0)
5604 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5610 arg = CALL_EXPR_ARG (exp, 2);
5611 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5614 if (target != const0_rtx)
5616 len = c_strlen (arg, 1);
5617 if (! len || TREE_CODE (len) != INTEGER_CST)
5623 expand_expr (build_call_expr (fn, 2, dest, arg),
5624 const0_rtx, VOIDmode, EXPAND_NORMAL);
5626 if (target == const0_rtx)
5628 return expand_expr (len, target, mode, EXPAND_NORMAL);
5634 /* Expand a call to either the entry or exit function profiler. */
5637 expand_builtin_profile_func (bool exitp)
5639 rtx this_rtx, which;
5641 this_rtx = DECL_RTL (current_function_decl);
5642 gcc_assert (MEM_P (this_rtx));
5643 this_rtx = XEXP (this_rtx, 0);
5646 which = profile_function_exit_libfunc;
5648 which = profile_function_entry_libfunc;
5650 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5651 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5658 /* Expand a call to __builtin___clear_cache. */
5661 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5663 #ifndef HAVE_clear_cache
5664 #ifdef CLEAR_INSN_CACHE
5665 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5666 does something. Just do the default expansion to a call to
5670 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5671 does nothing. There is no need to call it. Do nothing. */
5673 #endif /* CLEAR_INSN_CACHE */
5675 /* We have a "clear_cache" insn, and it will handle everything. */
5677 rtx begin_rtx, end_rtx;
5678 enum insn_code icode;
5680 /* We must not expand to a library call. If we did, any
5681 fallback library function in libgcc that might contain a call to
5682 __builtin___clear_cache() would recurse infinitely. */
5683 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5685 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5689 if (HAVE_clear_cache)
5691 icode = CODE_FOR_clear_cache;
5693 begin = CALL_EXPR_ARG (exp, 0);
5694 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5695 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5696 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5697 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5699 end = CALL_EXPR_ARG (exp, 1);
5700 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5701 end_rtx = convert_memory_address (Pmode, end_rtx);
5702 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5703 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5705 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5708 #endif /* HAVE_clear_cache */
5711 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5714 round_trampoline_addr (rtx tramp)
5716 rtx temp, addend, mask;
5718 /* If we don't need too much alignment, we'll have been guaranteed
5719 proper alignment by get_trampoline_type. */
5720 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5723 /* Round address up to desired boundary. */
5724 temp = gen_reg_rtx (Pmode);
5725 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5726 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5728 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5729 temp, 0, OPTAB_LIB_WIDEN);
5730 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5731 temp, 0, OPTAB_LIB_WIDEN);
5737 expand_builtin_init_trampoline (tree exp)
5739 tree t_tramp, t_func, t_chain;
5740 rtx r_tramp, r_func, r_chain;
5741 #ifdef TRAMPOLINE_TEMPLATE
5745 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5746 POINTER_TYPE, VOID_TYPE))
5749 t_tramp = CALL_EXPR_ARG (exp, 0);
5750 t_func = CALL_EXPR_ARG (exp, 1);
5751 t_chain = CALL_EXPR_ARG (exp, 2);
5753 r_tramp = expand_normal (t_tramp);
5754 r_func = expand_normal (t_func);
5755 r_chain = expand_normal (t_chain);
5757 /* Generate insns to initialize the trampoline. */
5758 r_tramp = round_trampoline_addr (r_tramp);
5759 #ifdef TRAMPOLINE_TEMPLATE
5760 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5761 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5762 emit_block_move (blktramp, assemble_trampoline_template (),
5763 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5765 trampolines_created = 1;
5766 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5772 expand_builtin_adjust_trampoline (tree exp)
5776 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5779 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5780 tramp = round_trampoline_addr (tramp);
5781 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5782 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5788 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5789 function. The function first checks whether the back end provides
5790 an insn to implement signbit for the respective mode. If not, it
5791 checks whether the floating point format of the value is such that
5792 the sign bit can be extracted. If that is not the case, the
5793 function returns NULL_RTX to indicate that a normal call should be
5794 emitted rather than expanding the function in-line. EXP is the
5795 expression that is a call to the builtin function; if convenient,
5796 the result should be placed in TARGET. */
5798 expand_builtin_signbit (tree exp, rtx target)
5800 const struct real_format *fmt;
5801 enum machine_mode fmode, imode, rmode;
5802 HOST_WIDE_INT hi, lo;
5805 enum insn_code icode;
5808 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5811 arg = CALL_EXPR_ARG (exp, 0);
5812 fmode = TYPE_MODE (TREE_TYPE (arg));
5813 rmode = TYPE_MODE (TREE_TYPE (exp));
5814 fmt = REAL_MODE_FORMAT (fmode);
5816 arg = builtin_save_expr (arg);
5818 /* Expand the argument yielding a RTX expression. */
5819 temp = expand_normal (arg);
5821 /* Check if the back end provides an insn that handles signbit for the
5823 icode = signbit_optab->handlers [(int) fmode].insn_code;
5824 if (icode != CODE_FOR_nothing)
5826 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5827 emit_unop_insn (icode, target, temp, UNKNOWN);
5831 /* For floating point formats without a sign bit, implement signbit
5833 bitpos = fmt->signbit_ro;
5836 /* But we can't do this if the format supports signed zero. */
5837 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5840 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5841 build_real (TREE_TYPE (arg), dconst0));
5842 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5845 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5847 imode = int_mode_for_mode (fmode);
5848 if (imode == BLKmode)
5850 temp = gen_lowpart (imode, temp);
5855 /* Handle targets with different FP word orders. */
5856 if (FLOAT_WORDS_BIG_ENDIAN)
5857 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5859 word = bitpos / BITS_PER_WORD;
5860 temp = operand_subword_force (temp, word, fmode);
5861 bitpos = bitpos % BITS_PER_WORD;
5864 /* Force the intermediate word_mode (or narrower) result into a
5865 register. This avoids attempting to create paradoxical SUBREGs
5866 of floating point modes below. */
5867 temp = force_reg (imode, temp);
5869 /* If the bitpos is within the "result mode" lowpart, the operation
5870 can be implement with a single bitwise AND. Otherwise, we need
5871 a right shift and an AND. */
5873 if (bitpos < GET_MODE_BITSIZE (rmode))
5875 if (bitpos < HOST_BITS_PER_WIDE_INT)
5878 lo = (HOST_WIDE_INT) 1 << bitpos;
5882 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5886 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5887 temp = gen_lowpart (rmode, temp);
5888 temp = expand_binop (rmode, and_optab, temp,
5889 immed_double_const (lo, hi, rmode),
5890 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5894 /* Perform a logical right shift to place the signbit in the least
5895 significant bit, then truncate the result to the desired mode
5896 and mask just this bit. */
5897 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5898 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5899 temp = gen_lowpart (rmode, temp);
5900 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5901 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5907 /* Expand fork or exec calls. TARGET is the desired target of the
5908 call. EXP is the call. FN is the
5909 identificator of the actual function. IGNORE is nonzero if the
5910 value is to be ignored. */
5913 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5918 /* If we are not profiling, just call the function. */
5919 if (!profile_arc_flag)
5922 /* Otherwise call the wrapper. This should be equivalent for the rest of
5923 compiler, so the code does not diverge, and the wrapper may run the
5924 code necessary for keeping the profiling sane. */
5926 switch (DECL_FUNCTION_CODE (fn))
5929 id = get_identifier ("__gcov_fork");
5932 case BUILT_IN_EXECL:
5933 id = get_identifier ("__gcov_execl");
5936 case BUILT_IN_EXECV:
5937 id = get_identifier ("__gcov_execv");
5940 case BUILT_IN_EXECLP:
5941 id = get_identifier ("__gcov_execlp");
5944 case BUILT_IN_EXECLE:
5945 id = get_identifier ("__gcov_execle");
5948 case BUILT_IN_EXECVP:
5949 id = get_identifier ("__gcov_execvp");
5952 case BUILT_IN_EXECVE:
5953 id = get_identifier ("__gcov_execve");
5960 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5961 DECL_EXTERNAL (decl) = 1;
5962 TREE_PUBLIC (decl) = 1;
5963 DECL_ARTIFICIAL (decl) = 1;
5964 TREE_NOTHROW (decl) = 1;
5965 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5966 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5967 call = rewrite_call_expr (exp, 0, decl, 0);
5968 return expand_call (call, target, ignore);
5973 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5974 the pointer in these functions is void*, the tree optimizers may remove
5975 casts. The mode computed in expand_builtin isn't reliable either, due
5976 to __sync_bool_compare_and_swap.
5978 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5979 group of builtins. This gives us log2 of the mode size. */
5981 static inline enum machine_mode
5982 get_builtin_sync_mode (int fcode_diff)
5984 /* The size is not negotiable, so ask not to get BLKmode in return
5985 if the target indicates that a smaller size would be better. */
5986 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5989 /* Expand the memory expression LOC and return the appropriate memory operand
5990 for the builtin_sync operations. */
5993 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5997 addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM);
5999 /* Note that we explicitly do not want any alias information for this
6000 memory, so that we kill all other live memories. Otherwise we don't
6001 satisfy the full barrier semantics of the intrinsic. */
6002 mem = validize_mem (gen_rtx_MEM (mode, addr));
6004 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
6005 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6006 MEM_VOLATILE_P (mem) = 1;
6011 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6012 EXP is the CALL_EXPR. CODE is the rtx code
6013 that corresponds to the arithmetic or logical operation from the name;
6014 an exception here is that NOT actually means NAND. TARGET is an optional
6015 place for us to store the results; AFTER is true if this is the
6016 fetch_and_xxx form. IGNORE is true if we don't actually care about
6017 the result of the operation at all. */
6020 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
6021 enum rtx_code code, bool after,
6022 rtx target, bool ignore)
6025 enum machine_mode old_mode;
6027 if (code == NOT && warn_sync_nand)
6029 tree fndecl = get_callee_fndecl (exp);
6030 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6032 static bool warned_f_a_n, warned_n_a_f;
6036 case BUILT_IN_FETCH_AND_NAND_1:
6037 case BUILT_IN_FETCH_AND_NAND_2:
6038 case BUILT_IN_FETCH_AND_NAND_4:
6039 case BUILT_IN_FETCH_AND_NAND_8:
6040 case BUILT_IN_FETCH_AND_NAND_16:
6045 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
6046 inform (input_location,
6047 "%qD changed semantics in GCC 4.4", fndecl);
6048 warned_f_a_n = true;
6051 case BUILT_IN_NAND_AND_FETCH_1:
6052 case BUILT_IN_NAND_AND_FETCH_2:
6053 case BUILT_IN_NAND_AND_FETCH_4:
6054 case BUILT_IN_NAND_AND_FETCH_8:
6055 case BUILT_IN_NAND_AND_FETCH_16:
6060 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
6061 inform (input_location,
6062 "%qD changed semantics in GCC 4.4", fndecl);
6063 warned_n_a_f = true;
6071 /* Expand the operands. */
6072 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6074 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6075 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6076 of CONST_INTs, where we know the old_mode only from the call argument. */
6077 old_mode = GET_MODE (val);
6078 if (old_mode == VOIDmode)
6079 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6080 val = convert_modes (mode, old_mode, val, 1);
6083 return expand_sync_operation (mem, val, code);
6085 return expand_sync_fetch_operation (mem, val, code, after, target);
6088 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6089 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6090 true if this is the boolean form. TARGET is a place for us to store the
6091 results; this is NOT optional if IS_BOOL is true. */
6094 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
6095 bool is_bool, rtx target)
6097 rtx old_val, new_val, mem;
6098 enum machine_mode old_mode;
6100 /* Expand the operands. */
6101 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6104 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
6105 mode, EXPAND_NORMAL);
6106 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6107 of CONST_INTs, where we know the old_mode only from the call argument. */
6108 old_mode = GET_MODE (old_val);
6109 if (old_mode == VOIDmode)
6110 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6111 old_val = convert_modes (mode, old_mode, old_val, 1);
6113 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
6114 mode, EXPAND_NORMAL);
6115 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6116 of CONST_INTs, where we know the old_mode only from the call argument. */
6117 old_mode = GET_MODE (new_val);
6118 if (old_mode == VOIDmode)
6119 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
6120 new_val = convert_modes (mode, old_mode, new_val, 1);
6123 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
6125 return expand_val_compare_and_swap (mem, old_val, new_val, target);
6128 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6129 general form is actually an atomic exchange, and some targets only
6130 support a reduced form with the second argument being a constant 1.
6131 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6135 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
6139 enum machine_mode old_mode;
6141 /* Expand the operands. */
6142 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6143 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
6144 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6145 of CONST_INTs, where we know the old_mode only from the call argument. */
6146 old_mode = GET_MODE (val);
6147 if (old_mode == VOIDmode)
6148 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6149 val = convert_modes (mode, old_mode, val, 1);
6151 return expand_sync_lock_test_and_set (mem, val, target);
6154 /* Expand the __sync_synchronize intrinsic. */
6157 expand_builtin_synchronize (void)
6161 #ifdef HAVE_memory_barrier
6162 if (HAVE_memory_barrier)
6164 emit_insn (gen_memory_barrier ());
6169 if (synchronize_libfunc != NULL_RTX)
6171 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
6175 /* If no explicit memory barrier instruction is available, create an
6176 empty asm stmt with a memory clobber. */
6177 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
6178 tree_cons (NULL, build_string (6, "memory"), NULL));
6179 ASM_VOLATILE_P (x) = 1;
6180 expand_asm_expr (x);
6183 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6186 expand_builtin_lock_release (enum machine_mode mode, tree exp)
6188 enum insn_code icode;
6190 rtx val = const0_rtx;
6192 /* Expand the operands. */
6193 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6195 /* If there is an explicit operation in the md file, use it. */
6196 icode = sync_lock_release[mode];
6197 if (icode != CODE_FOR_nothing)
6199 if (!insn_data[icode].operand[1].predicate (val, mode))
6200 val = force_reg (mode, val);
6202 insn = GEN_FCN (icode) (mem, val);
6210 /* Otherwise we can implement this operation by emitting a barrier
6211 followed by a store of zero. */
6212 expand_builtin_synchronize ();
6213 emit_move_insn (mem, val);
6216 /* Expand an expression EXP that calls a built-in function,
6217 with result going to TARGET if that's convenient
6218 (and in mode MODE if that's convenient).
6219 SUBTARGET may be used as the target for computing one of EXP's operands.
6220 IGNORE is nonzero if the value is to be ignored. */
6223 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
6226 tree fndecl = get_callee_fndecl (exp);
6227 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6228 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6230 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6231 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6233 /* When not optimizing, generate calls to library functions for a certain
6236 && !called_as_built_in (fndecl)
6237 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
6238 && fcode != BUILT_IN_ALLOCA
6239 && fcode != BUILT_IN_FREE)
6240 return expand_call (exp, target, ignore);
6242 /* The built-in function expanders test for target == const0_rtx
6243 to determine whether the function's result will be ignored. */
6245 target = const0_rtx;
6247 /* If the result of a pure or const built-in function is ignored, and
6248 none of its arguments are volatile, we can avoid expanding the
6249 built-in call and just evaluate the arguments for side-effects. */
6250 if (target == const0_rtx
6251 && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl)))
6253 bool volatilep = false;
6255 call_expr_arg_iterator iter;
6257 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6258 if (TREE_THIS_VOLATILE (arg))
6266 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6267 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6274 CASE_FLT_FN (BUILT_IN_FABS):
6275 target = expand_builtin_fabs (exp, target, subtarget);
6280 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6281 target = expand_builtin_copysign (exp, target, subtarget);
6286 /* Just do a normal library call if we were unable to fold
6288 CASE_FLT_FN (BUILT_IN_CABS):
6291 CASE_FLT_FN (BUILT_IN_EXP):
6292 CASE_FLT_FN (BUILT_IN_EXP10):
6293 CASE_FLT_FN (BUILT_IN_POW10):
6294 CASE_FLT_FN (BUILT_IN_EXP2):
6295 CASE_FLT_FN (BUILT_IN_EXPM1):
6296 CASE_FLT_FN (BUILT_IN_LOGB):
6297 CASE_FLT_FN (BUILT_IN_LOG):
6298 CASE_FLT_FN (BUILT_IN_LOG10):
6299 CASE_FLT_FN (BUILT_IN_LOG2):
6300 CASE_FLT_FN (BUILT_IN_LOG1P):
6301 CASE_FLT_FN (BUILT_IN_TAN):
6302 CASE_FLT_FN (BUILT_IN_ASIN):
6303 CASE_FLT_FN (BUILT_IN_ACOS):
6304 CASE_FLT_FN (BUILT_IN_ATAN):
6305 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6306 because of possible accuracy problems. */
6307 if (! flag_unsafe_math_optimizations)
6309 CASE_FLT_FN (BUILT_IN_SQRT):
6310 CASE_FLT_FN (BUILT_IN_FLOOR):
6311 CASE_FLT_FN (BUILT_IN_CEIL):
6312 CASE_FLT_FN (BUILT_IN_TRUNC):
6313 CASE_FLT_FN (BUILT_IN_ROUND):
6314 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6315 CASE_FLT_FN (BUILT_IN_RINT):
6316 target = expand_builtin_mathfn (exp, target, subtarget);
6321 CASE_FLT_FN (BUILT_IN_ILOGB):
6322 if (! flag_unsafe_math_optimizations)
6324 CASE_FLT_FN (BUILT_IN_ISINF):
6325 CASE_FLT_FN (BUILT_IN_FINITE):
6326 case BUILT_IN_ISFINITE:
6327 case BUILT_IN_ISNORMAL:
6328 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
6333 CASE_FLT_FN (BUILT_IN_LCEIL):
6334 CASE_FLT_FN (BUILT_IN_LLCEIL):
6335 CASE_FLT_FN (BUILT_IN_LFLOOR):
6336 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6337 target = expand_builtin_int_roundingfn (exp, target);
6342 CASE_FLT_FN (BUILT_IN_LRINT):
6343 CASE_FLT_FN (BUILT_IN_LLRINT):
6344 CASE_FLT_FN (BUILT_IN_LROUND):
6345 CASE_FLT_FN (BUILT_IN_LLROUND):
6346 target = expand_builtin_int_roundingfn_2 (exp, target);
6351 CASE_FLT_FN (BUILT_IN_POW):
6352 target = expand_builtin_pow (exp, target, subtarget);
6357 CASE_FLT_FN (BUILT_IN_POWI):
6358 target = expand_builtin_powi (exp, target, subtarget);
6363 CASE_FLT_FN (BUILT_IN_ATAN2):
6364 CASE_FLT_FN (BUILT_IN_LDEXP):
6365 CASE_FLT_FN (BUILT_IN_SCALB):
6366 CASE_FLT_FN (BUILT_IN_SCALBN):
6367 CASE_FLT_FN (BUILT_IN_SCALBLN):
6368 if (! flag_unsafe_math_optimizations)
6371 CASE_FLT_FN (BUILT_IN_FMOD):
6372 CASE_FLT_FN (BUILT_IN_REMAINDER):
6373 CASE_FLT_FN (BUILT_IN_DREM):
6374 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6379 CASE_FLT_FN (BUILT_IN_CEXPI):
6380 target = expand_builtin_cexpi (exp, target, subtarget);
6381 gcc_assert (target);
6384 CASE_FLT_FN (BUILT_IN_SIN):
6385 CASE_FLT_FN (BUILT_IN_COS):
6386 if (! flag_unsafe_math_optimizations)
6388 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6393 CASE_FLT_FN (BUILT_IN_SINCOS):
6394 if (! flag_unsafe_math_optimizations)
6396 target = expand_builtin_sincos (exp);
6401 case BUILT_IN_APPLY_ARGS:
6402 return expand_builtin_apply_args ();
6404 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6405 FUNCTION with a copy of the parameters described by
6406 ARGUMENTS, and ARGSIZE. It returns a block of memory
6407 allocated on the stack into which is stored all the registers
6408 that might possibly be used for returning the result of a
6409 function. ARGUMENTS is the value returned by
6410 __builtin_apply_args. ARGSIZE is the number of bytes of
6411 arguments that must be copied. ??? How should this value be
6412 computed? We'll also need a safe worst case value for varargs
6414 case BUILT_IN_APPLY:
6415 if (!validate_arglist (exp, POINTER_TYPE,
6416 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6417 && !validate_arglist (exp, REFERENCE_TYPE,
6418 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6424 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6425 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6426 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6428 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6431 /* __builtin_return (RESULT) causes the function to return the
6432 value described by RESULT. RESULT is address of the block of
6433 memory returned by __builtin_apply. */
6434 case BUILT_IN_RETURN:
6435 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6436 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6439 case BUILT_IN_SAVEREGS:
6440 return expand_builtin_saveregs ();
6442 case BUILT_IN_ARGS_INFO:
6443 return expand_builtin_args_info (exp);
6445 case BUILT_IN_VA_ARG_PACK:
6446 /* All valid uses of __builtin_va_arg_pack () are removed during
6448 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6451 case BUILT_IN_VA_ARG_PACK_LEN:
6452 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6454 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6457 /* Return the address of the first anonymous stack arg. */
6458 case BUILT_IN_NEXT_ARG:
6459 if (fold_builtin_next_arg (exp, false))
6461 return expand_builtin_next_arg ();
6463 case BUILT_IN_CLEAR_CACHE:
6464 target = expand_builtin___clear_cache (exp);
6469 case BUILT_IN_CLASSIFY_TYPE:
6470 return expand_builtin_classify_type (exp);
6472 case BUILT_IN_CONSTANT_P:
6475 case BUILT_IN_FRAME_ADDRESS:
6476 case BUILT_IN_RETURN_ADDRESS:
6477 return expand_builtin_frame_address (fndecl, exp);
6479 /* Returns the address of the area where the structure is returned.
6481 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6482 if (call_expr_nargs (exp) != 0
6483 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6484 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6487 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6489 case BUILT_IN_ALLOCA:
6490 target = expand_builtin_alloca (exp, target);
6495 case BUILT_IN_STACK_SAVE:
6496 return expand_stack_save ();
6498 case BUILT_IN_STACK_RESTORE:
6499 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6502 case BUILT_IN_BSWAP32:
6503 case BUILT_IN_BSWAP64:
6504 target = expand_builtin_bswap (exp, target, subtarget);
6510 CASE_INT_FN (BUILT_IN_FFS):
6511 case BUILT_IN_FFSIMAX:
6512 target = expand_builtin_unop (target_mode, exp, target,
6513 subtarget, ffs_optab);
6518 CASE_INT_FN (BUILT_IN_CLZ):
6519 case BUILT_IN_CLZIMAX:
6520 target = expand_builtin_unop (target_mode, exp, target,
6521 subtarget, clz_optab);
6526 CASE_INT_FN (BUILT_IN_CTZ):
6527 case BUILT_IN_CTZIMAX:
6528 target = expand_builtin_unop (target_mode, exp, target,
6529 subtarget, ctz_optab);
6534 CASE_INT_FN (BUILT_IN_POPCOUNT):
6535 case BUILT_IN_POPCOUNTIMAX:
6536 target = expand_builtin_unop (target_mode, exp, target,
6537 subtarget, popcount_optab);
6542 CASE_INT_FN (BUILT_IN_PARITY):
6543 case BUILT_IN_PARITYIMAX:
6544 target = expand_builtin_unop (target_mode, exp, target,
6545 subtarget, parity_optab);
6550 case BUILT_IN_STRLEN:
6551 target = expand_builtin_strlen (exp, target, target_mode);
6556 case BUILT_IN_STRCPY:
6557 target = expand_builtin_strcpy (fndecl, exp, target, mode);
6562 case BUILT_IN_STRNCPY:
6563 target = expand_builtin_strncpy (exp, target, mode);
6568 case BUILT_IN_STPCPY:
6569 target = expand_builtin_stpcpy (exp, target, mode);
6574 case BUILT_IN_STRCAT:
6575 target = expand_builtin_strcat (fndecl, exp, target, mode);
6580 case BUILT_IN_STRNCAT:
6581 target = expand_builtin_strncat (exp, target, mode);
6586 case BUILT_IN_STRSPN:
6587 target = expand_builtin_strspn (exp, target, mode);
6592 case BUILT_IN_STRCSPN:
6593 target = expand_builtin_strcspn (exp, target, mode);
6598 case BUILT_IN_STRSTR:
6599 target = expand_builtin_strstr (exp, target, mode);
6604 case BUILT_IN_STRPBRK:
6605 target = expand_builtin_strpbrk (exp, target, mode);
6610 case BUILT_IN_INDEX:
6611 case BUILT_IN_STRCHR:
6612 target = expand_builtin_strchr (exp, target, mode);
6617 case BUILT_IN_RINDEX:
6618 case BUILT_IN_STRRCHR:
6619 target = expand_builtin_strrchr (exp, target, mode);
6624 case BUILT_IN_MEMCPY:
6625 target = expand_builtin_memcpy (exp, target, mode);
6630 case BUILT_IN_MEMPCPY:
6631 target = expand_builtin_mempcpy (exp, target, mode);
6636 case BUILT_IN_MEMMOVE:
6637 target = expand_builtin_memmove (exp, target, mode, ignore);
6642 case BUILT_IN_BCOPY:
6643 target = expand_builtin_bcopy (exp, ignore);
6648 case BUILT_IN_MEMSET:
6649 target = expand_builtin_memset (exp, target, mode);
6654 case BUILT_IN_BZERO:
6655 target = expand_builtin_bzero (exp);
6660 case BUILT_IN_STRCMP:
6661 target = expand_builtin_strcmp (exp, target, mode);
6666 case BUILT_IN_STRNCMP:
6667 target = expand_builtin_strncmp (exp, target, mode);
6672 case BUILT_IN_MEMCHR:
6673 target = expand_builtin_memchr (exp, target, mode);
6679 case BUILT_IN_MEMCMP:
6680 target = expand_builtin_memcmp (exp, target, mode);
6685 case BUILT_IN_SETJMP:
6686 /* This should have been lowered to the builtins below. */
6689 case BUILT_IN_SETJMP_SETUP:
6690 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6691 and the receiver label. */
6692 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6694 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6695 VOIDmode, EXPAND_NORMAL);
6696 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6697 rtx label_r = label_rtx (label);
6699 /* This is copied from the handling of non-local gotos. */
6700 expand_builtin_setjmp_setup (buf_addr, label_r);
6701 nonlocal_goto_handler_labels
6702 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6703 nonlocal_goto_handler_labels);
6704 /* ??? Do not let expand_label treat us as such since we would
6705 not want to be both on the list of non-local labels and on
6706 the list of forced labels. */
6707 FORCED_LABEL (label) = 0;
6712 case BUILT_IN_SETJMP_DISPATCHER:
6713 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6714 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6716 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6717 rtx label_r = label_rtx (label);
6719 /* Remove the dispatcher label from the list of non-local labels
6720 since the receiver labels have been added to it above. */
6721 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6726 case BUILT_IN_SETJMP_RECEIVER:
6727 /* __builtin_setjmp_receiver is passed the receiver label. */
6728 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6730 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6731 rtx label_r = label_rtx (label);
6733 expand_builtin_setjmp_receiver (label_r);
6738 /* __builtin_longjmp is passed a pointer to an array of five words.
6739 It's similar to the C library longjmp function but works with
6740 __builtin_setjmp above. */
6741 case BUILT_IN_LONGJMP:
6742 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6744 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6745 VOIDmode, EXPAND_NORMAL);
6746 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6748 if (value != const1_rtx)
6750 error ("%<__builtin_longjmp%> second argument must be 1");
6754 expand_builtin_longjmp (buf_addr, value);
6759 case BUILT_IN_NONLOCAL_GOTO:
6760 target = expand_builtin_nonlocal_goto (exp);
6765 /* This updates the setjmp buffer that is its argument with the value
6766 of the current stack pointer. */
6767 case BUILT_IN_UPDATE_SETJMP_BUF:
6768 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6771 = expand_normal (CALL_EXPR_ARG (exp, 0));
6773 expand_builtin_update_setjmp_buf (buf_addr);
6779 expand_builtin_trap ();
6782 case BUILT_IN_PRINTF:
6783 target = expand_builtin_printf (exp, target, mode, false);
6788 case BUILT_IN_PRINTF_UNLOCKED:
6789 target = expand_builtin_printf (exp, target, mode, true);
6794 case BUILT_IN_FPUTS:
6795 target = expand_builtin_fputs (exp, target, false);
6799 case BUILT_IN_FPUTS_UNLOCKED:
6800 target = expand_builtin_fputs (exp, target, true);
6805 case BUILT_IN_FPRINTF:
6806 target = expand_builtin_fprintf (exp, target, mode, false);
6811 case BUILT_IN_FPRINTF_UNLOCKED:
6812 target = expand_builtin_fprintf (exp, target, mode, true);
6817 case BUILT_IN_SPRINTF:
6818 target = expand_builtin_sprintf (exp, target, mode);
6823 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6824 case BUILT_IN_SIGNBITD32:
6825 case BUILT_IN_SIGNBITD64:
6826 case BUILT_IN_SIGNBITD128:
6827 target = expand_builtin_signbit (exp, target);
6832 /* Various hooks for the DWARF 2 __throw routine. */
6833 case BUILT_IN_UNWIND_INIT:
6834 expand_builtin_unwind_init ();
6836 case BUILT_IN_DWARF_CFA:
6837 return virtual_cfa_rtx;
6838 #ifdef DWARF2_UNWIND_INFO
6839 case BUILT_IN_DWARF_SP_COLUMN:
6840 return expand_builtin_dwarf_sp_column ();
6841 case BUILT_IN_INIT_DWARF_REG_SIZES:
6842 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6845 case BUILT_IN_FROB_RETURN_ADDR:
6846 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6847 case BUILT_IN_EXTRACT_RETURN_ADDR:
6848 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6849 case BUILT_IN_EH_RETURN:
6850 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6851 CALL_EXPR_ARG (exp, 1));
6853 #ifdef EH_RETURN_DATA_REGNO
6854 case BUILT_IN_EH_RETURN_DATA_REGNO:
6855 return expand_builtin_eh_return_data_regno (exp);
6857 case BUILT_IN_EXTEND_POINTER:
6858 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6860 case BUILT_IN_VA_START:
6861 return expand_builtin_va_start (exp);
6862 case BUILT_IN_VA_END:
6863 return expand_builtin_va_end (exp);
6864 case BUILT_IN_VA_COPY:
6865 return expand_builtin_va_copy (exp);
6866 case BUILT_IN_EXPECT:
6867 return expand_builtin_expect (exp, target);
6868 case BUILT_IN_PREFETCH:
6869 expand_builtin_prefetch (exp);
6872 case BUILT_IN_PROFILE_FUNC_ENTER:
6873 return expand_builtin_profile_func (false);
6874 case BUILT_IN_PROFILE_FUNC_EXIT:
6875 return expand_builtin_profile_func (true);
6877 case BUILT_IN_INIT_TRAMPOLINE:
6878 return expand_builtin_init_trampoline (exp);
6879 case BUILT_IN_ADJUST_TRAMPOLINE:
6880 return expand_builtin_adjust_trampoline (exp);
6883 case BUILT_IN_EXECL:
6884 case BUILT_IN_EXECV:
6885 case BUILT_IN_EXECLP:
6886 case BUILT_IN_EXECLE:
6887 case BUILT_IN_EXECVP:
6888 case BUILT_IN_EXECVE:
6889 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6894 case BUILT_IN_FETCH_AND_ADD_1:
6895 case BUILT_IN_FETCH_AND_ADD_2:
6896 case BUILT_IN_FETCH_AND_ADD_4:
6897 case BUILT_IN_FETCH_AND_ADD_8:
6898 case BUILT_IN_FETCH_AND_ADD_16:
6899 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6900 target = expand_builtin_sync_operation (mode, exp, PLUS,
6901 false, target, ignore);
6906 case BUILT_IN_FETCH_AND_SUB_1:
6907 case BUILT_IN_FETCH_AND_SUB_2:
6908 case BUILT_IN_FETCH_AND_SUB_4:
6909 case BUILT_IN_FETCH_AND_SUB_8:
6910 case BUILT_IN_FETCH_AND_SUB_16:
6911 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6912 target = expand_builtin_sync_operation (mode, exp, MINUS,
6913 false, target, ignore);
6918 case BUILT_IN_FETCH_AND_OR_1:
6919 case BUILT_IN_FETCH_AND_OR_2:
6920 case BUILT_IN_FETCH_AND_OR_4:
6921 case BUILT_IN_FETCH_AND_OR_8:
6922 case BUILT_IN_FETCH_AND_OR_16:
6923 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6924 target = expand_builtin_sync_operation (mode, exp, IOR,
6925 false, target, ignore);
6930 case BUILT_IN_FETCH_AND_AND_1:
6931 case BUILT_IN_FETCH_AND_AND_2:
6932 case BUILT_IN_FETCH_AND_AND_4:
6933 case BUILT_IN_FETCH_AND_AND_8:
6934 case BUILT_IN_FETCH_AND_AND_16:
6935 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6936 target = expand_builtin_sync_operation (mode, exp, AND,
6937 false, target, ignore);
6942 case BUILT_IN_FETCH_AND_XOR_1:
6943 case BUILT_IN_FETCH_AND_XOR_2:
6944 case BUILT_IN_FETCH_AND_XOR_4:
6945 case BUILT_IN_FETCH_AND_XOR_8:
6946 case BUILT_IN_FETCH_AND_XOR_16:
6947 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6948 target = expand_builtin_sync_operation (mode, exp, XOR,
6949 false, target, ignore);
6954 case BUILT_IN_FETCH_AND_NAND_1:
6955 case BUILT_IN_FETCH_AND_NAND_2:
6956 case BUILT_IN_FETCH_AND_NAND_4:
6957 case BUILT_IN_FETCH_AND_NAND_8:
6958 case BUILT_IN_FETCH_AND_NAND_16:
6959 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6960 target = expand_builtin_sync_operation (mode, exp, NOT,
6961 false, target, ignore);
6966 case BUILT_IN_ADD_AND_FETCH_1:
6967 case BUILT_IN_ADD_AND_FETCH_2:
6968 case BUILT_IN_ADD_AND_FETCH_4:
6969 case BUILT_IN_ADD_AND_FETCH_8:
6970 case BUILT_IN_ADD_AND_FETCH_16:
6971 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6972 target = expand_builtin_sync_operation (mode, exp, PLUS,
6973 true, target, ignore);
6978 case BUILT_IN_SUB_AND_FETCH_1:
6979 case BUILT_IN_SUB_AND_FETCH_2:
6980 case BUILT_IN_SUB_AND_FETCH_4:
6981 case BUILT_IN_SUB_AND_FETCH_8:
6982 case BUILT_IN_SUB_AND_FETCH_16:
6983 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6984 target = expand_builtin_sync_operation (mode, exp, MINUS,
6985 true, target, ignore);
6990 case BUILT_IN_OR_AND_FETCH_1:
6991 case BUILT_IN_OR_AND_FETCH_2:
6992 case BUILT_IN_OR_AND_FETCH_4:
6993 case BUILT_IN_OR_AND_FETCH_8:
6994 case BUILT_IN_OR_AND_FETCH_16:
6995 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6996 target = expand_builtin_sync_operation (mode, exp, IOR,
6997 true, target, ignore);
7002 case BUILT_IN_AND_AND_FETCH_1:
7003 case BUILT_IN_AND_AND_FETCH_2:
7004 case BUILT_IN_AND_AND_FETCH_4:
7005 case BUILT_IN_AND_AND_FETCH_8:
7006 case BUILT_IN_AND_AND_FETCH_16:
7007 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
7008 target = expand_builtin_sync_operation (mode, exp, AND,
7009 true, target, ignore);
7014 case BUILT_IN_XOR_AND_FETCH_1:
7015 case BUILT_IN_XOR_AND_FETCH_2:
7016 case BUILT_IN_XOR_AND_FETCH_4:
7017 case BUILT_IN_XOR_AND_FETCH_8:
7018 case BUILT_IN_XOR_AND_FETCH_16:
7019 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
7020 target = expand_builtin_sync_operation (mode, exp, XOR,
7021 true, target, ignore);
7026 case BUILT_IN_NAND_AND_FETCH_1:
7027 case BUILT_IN_NAND_AND_FETCH_2:
7028 case BUILT_IN_NAND_AND_FETCH_4:
7029 case BUILT_IN_NAND_AND_FETCH_8:
7030 case BUILT_IN_NAND_AND_FETCH_16:
7031 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
7032 target = expand_builtin_sync_operation (mode, exp, NOT,
7033 true, target, ignore);
7038 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
7039 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
7040 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
7041 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
7042 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
7043 if (mode == VOIDmode)
7044 mode = TYPE_MODE (boolean_type_node);
7045 if (!target || !register_operand (target, mode))
7046 target = gen_reg_rtx (mode);
7048 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
7049 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7054 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
7055 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
7056 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
7057 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
7058 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
7059 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
7060 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7065 case BUILT_IN_LOCK_TEST_AND_SET_1:
7066 case BUILT_IN_LOCK_TEST_AND_SET_2:
7067 case BUILT_IN_LOCK_TEST_AND_SET_4:
7068 case BUILT_IN_LOCK_TEST_AND_SET_8:
7069 case BUILT_IN_LOCK_TEST_AND_SET_16:
7070 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
7071 target = expand_builtin_lock_test_and_set (mode, exp, target);
7076 case BUILT_IN_LOCK_RELEASE_1:
7077 case BUILT_IN_LOCK_RELEASE_2:
7078 case BUILT_IN_LOCK_RELEASE_4:
7079 case BUILT_IN_LOCK_RELEASE_8:
7080 case BUILT_IN_LOCK_RELEASE_16:
7081 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
7082 expand_builtin_lock_release (mode, exp);
7085 case BUILT_IN_SYNCHRONIZE:
7086 expand_builtin_synchronize ();
7089 case BUILT_IN_OBJECT_SIZE:
7090 return expand_builtin_object_size (exp);
7092 case BUILT_IN_MEMCPY_CHK:
7093 case BUILT_IN_MEMPCPY_CHK:
7094 case BUILT_IN_MEMMOVE_CHK:
7095 case BUILT_IN_MEMSET_CHK:
7096 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7101 case BUILT_IN_STRCPY_CHK:
7102 case BUILT_IN_STPCPY_CHK:
7103 case BUILT_IN_STRNCPY_CHK:
7104 case BUILT_IN_STRCAT_CHK:
7105 case BUILT_IN_STRNCAT_CHK:
7106 case BUILT_IN_SNPRINTF_CHK:
7107 case BUILT_IN_VSNPRINTF_CHK:
7108 maybe_emit_chk_warning (exp, fcode);
7111 case BUILT_IN_SPRINTF_CHK:
7112 case BUILT_IN_VSPRINTF_CHK:
7113 maybe_emit_sprintf_chk_warning (exp, fcode);
7117 maybe_emit_free_warning (exp);
7120 default: /* just do library call, if unknown builtin */
7124 /* The switch statement above can drop through to cause the function
7125 to be called normally. */
7126 return expand_call (exp, target, ignore);
7129 /* Determine whether a tree node represents a call to a built-in
7130 function. If the tree T is a call to a built-in function with
7131 the right number of arguments of the appropriate types, return
7132 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7133 Otherwise the return value is END_BUILTINS. */
7135 enum built_in_function
7136 builtin_mathfn_code (const_tree t)
7138 const_tree fndecl, arg, parmlist;
7139 const_tree argtype, parmtype;
7140 const_call_expr_arg_iterator iter;
7142 if (TREE_CODE (t) != CALL_EXPR
7143 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7144 return END_BUILTINS;
7146 fndecl = get_callee_fndecl (t);
7147 if (fndecl == NULL_TREE
7148 || TREE_CODE (fndecl) != FUNCTION_DECL
7149 || ! DECL_BUILT_IN (fndecl)
7150 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7151 return END_BUILTINS;
7153 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7154 init_const_call_expr_arg_iterator (t, &iter);
7155 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7157 /* If a function doesn't take a variable number of arguments,
7158 the last element in the list will have type `void'. */
7159 parmtype = TREE_VALUE (parmlist);
7160 if (VOID_TYPE_P (parmtype))
7162 if (more_const_call_expr_args_p (&iter))
7163 return END_BUILTINS;
7164 return DECL_FUNCTION_CODE (fndecl);
7167 if (! more_const_call_expr_args_p (&iter))
7168 return END_BUILTINS;
7170 arg = next_const_call_expr_arg (&iter);
7171 argtype = TREE_TYPE (arg);
7173 if (SCALAR_FLOAT_TYPE_P (parmtype))
7175 if (! SCALAR_FLOAT_TYPE_P (argtype))
7176 return END_BUILTINS;
7178 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7180 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7181 return END_BUILTINS;
7183 else if (POINTER_TYPE_P (parmtype))
7185 if (! POINTER_TYPE_P (argtype))
7186 return END_BUILTINS;
7188 else if (INTEGRAL_TYPE_P (parmtype))
7190 if (! INTEGRAL_TYPE_P (argtype))
7191 return END_BUILTINS;
7194 return END_BUILTINS;
7197 /* Variable-length argument list. */
7198 return DECL_FUNCTION_CODE (fndecl);
7201 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7202 evaluate to a constant. */
7205 fold_builtin_constant_p (tree arg)
7207 /* We return 1 for a numeric type that's known to be a constant
7208 value at compile-time or for an aggregate type that's a
7209 literal constant. */
7212 /* If we know this is a constant, emit the constant of one. */
7213 if (CONSTANT_CLASS_P (arg)
7214 || (TREE_CODE (arg) == CONSTRUCTOR
7215 && TREE_CONSTANT (arg)))
7216 return integer_one_node;
7217 if (TREE_CODE (arg) == ADDR_EXPR)
7219 tree op = TREE_OPERAND (arg, 0);
7220 if (TREE_CODE (op) == STRING_CST
7221 || (TREE_CODE (op) == ARRAY_REF
7222 && integer_zerop (TREE_OPERAND (op, 1))
7223 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7224 return integer_one_node;
7227 /* If this expression has side effects, show we don't know it to be a
7228 constant. Likewise if it's a pointer or aggregate type since in
7229 those case we only want literals, since those are only optimized
7230 when generating RTL, not later.
7231 And finally, if we are compiling an initializer, not code, we
7232 need to return a definite result now; there's not going to be any
7233 more optimization done. */
7234 if (TREE_SIDE_EFFECTS (arg)
7235 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7236 || POINTER_TYPE_P (TREE_TYPE (arg))
7238 || folding_initializer)
7239 return integer_zero_node;
7244 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7245 return it as a truthvalue. */
7248 build_builtin_expect_predicate (tree pred, tree expected)
7250 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7252 fn = built_in_decls[BUILT_IN_EXPECT];
7253 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7254 ret_type = TREE_TYPE (TREE_TYPE (fn));
7255 pred_type = TREE_VALUE (arg_types);
7256 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7258 pred = fold_convert (pred_type, pred);
7259 expected = fold_convert (expected_type, expected);
7260 call_expr = build_call_expr (fn, 2, pred, expected);
7262 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7263 build_int_cst (ret_type, 0));
7266 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7267 NULL_TREE if no simplification is possible. */
7270 fold_builtin_expect (tree arg0, tree arg1)
7273 enum tree_code code;
7275 /* If this is a builtin_expect within a builtin_expect keep the
7276 inner one. See through a comparison against a constant. It
7277 might have been added to create a thruthvalue. */
7279 if (COMPARISON_CLASS_P (inner)
7280 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7281 inner = TREE_OPERAND (inner, 0);
7283 if (TREE_CODE (inner) == CALL_EXPR
7284 && (fndecl = get_callee_fndecl (inner))
7285 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7286 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7289 /* Distribute the expected value over short-circuiting operators.
7290 See through the cast from truthvalue_type_node to long. */
7292 while (TREE_CODE (inner) == NOP_EXPR
7293 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
7294 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
7295 inner = TREE_OPERAND (inner, 0);
7297 code = TREE_CODE (inner);
7298 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7300 tree op0 = TREE_OPERAND (inner, 0);
7301 tree op1 = TREE_OPERAND (inner, 1);
7303 op0 = build_builtin_expect_predicate (op0, arg1);
7304 op1 = build_builtin_expect_predicate (op1, arg1);
7305 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7307 return fold_convert (TREE_TYPE (arg0), inner);
7310 /* If the argument isn't invariant then there's nothing else we can do. */
7311 if (!TREE_CONSTANT (arg0))
7314 /* If we expect that a comparison against the argument will fold to
7315 a constant return the constant. In practice, this means a true
7316 constant or the address of a non-weak symbol. */
7319 if (TREE_CODE (inner) == ADDR_EXPR)
7323 inner = TREE_OPERAND (inner, 0);
7325 while (TREE_CODE (inner) == COMPONENT_REF
7326 || TREE_CODE (inner) == ARRAY_REF);
7327 if ((TREE_CODE (inner) == VAR_DECL
7328 || TREE_CODE (inner) == FUNCTION_DECL)
7329 && DECL_WEAK (inner))
7333 /* Otherwise, ARG0 already has the proper type for the return value. */
7337 /* Fold a call to __builtin_classify_type with argument ARG. */
7340 fold_builtin_classify_type (tree arg)
7343 return build_int_cst (NULL_TREE, no_type_class);
7345 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
7348 /* Fold a call to __builtin_strlen with argument ARG. */
7351 fold_builtin_strlen (tree arg)
7353 if (!validate_arg (arg, POINTER_TYPE))
7357 tree len = c_strlen (arg, 0);
7361 /* Convert from the internal "sizetype" type to "size_t". */
7363 len = fold_convert (size_type_node, len);
7371 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7374 fold_builtin_inf (tree type, int warn)
7376 REAL_VALUE_TYPE real;
7378 /* __builtin_inff is intended to be usable to define INFINITY on all
7379 targets. If an infinity is not available, INFINITY expands "to a
7380 positive constant of type float that overflows at translation
7381 time", footnote "In this case, using INFINITY will violate the
7382 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7383 Thus we pedwarn to ensure this constraint violation is
7385 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7386 pedwarn (input_location, 0, "target format does not support infinity");
7389 return build_real (type, real);
7392 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7395 fold_builtin_nan (tree arg, tree type, int quiet)
7397 REAL_VALUE_TYPE real;
7400 if (!validate_arg (arg, POINTER_TYPE))
7402 str = c_getstr (arg);
7406 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7409 return build_real (type, real);
7412 /* Return true if the floating point expression T has an integer value.
7413 We also allow +Inf, -Inf and NaN to be considered integer values. */
7416 integer_valued_real_p (tree t)
7418 switch (TREE_CODE (t))
7425 return integer_valued_real_p (TREE_OPERAND (t, 0));
7430 return integer_valued_real_p (TREE_OPERAND (t, 1));
7437 return integer_valued_real_p (TREE_OPERAND (t, 0))
7438 && integer_valued_real_p (TREE_OPERAND (t, 1));
7441 return integer_valued_real_p (TREE_OPERAND (t, 1))
7442 && integer_valued_real_p (TREE_OPERAND (t, 2));
7445 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7449 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7450 if (TREE_CODE (type) == INTEGER_TYPE)
7452 if (TREE_CODE (type) == REAL_TYPE)
7453 return integer_valued_real_p (TREE_OPERAND (t, 0));
7458 switch (builtin_mathfn_code (t))
7460 CASE_FLT_FN (BUILT_IN_CEIL):
7461 CASE_FLT_FN (BUILT_IN_FLOOR):
7462 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7463 CASE_FLT_FN (BUILT_IN_RINT):
7464 CASE_FLT_FN (BUILT_IN_ROUND):
7465 CASE_FLT_FN (BUILT_IN_TRUNC):
7468 CASE_FLT_FN (BUILT_IN_FMIN):
7469 CASE_FLT_FN (BUILT_IN_FMAX):
7470 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7471 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7484 /* FNDECL is assumed to be a builtin where truncation can be propagated
7485 across (for instance floor((double)f) == (double)floorf (f).
7486 Do the transformation for a call with argument ARG. */
7489 fold_trunc_transparent_mathfn (tree fndecl, tree arg)
7491 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7493 if (!validate_arg (arg, REAL_TYPE))
7496 /* Integer rounding functions are idempotent. */
7497 if (fcode == builtin_mathfn_code (arg))
7500 /* If argument is already integer valued, and we don't need to worry
7501 about setting errno, there's no need to perform rounding. */
7502 if (! flag_errno_math && integer_valued_real_p (arg))
7507 tree arg0 = strip_float_extensions (arg);
7508 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7509 tree newtype = TREE_TYPE (arg0);
7512 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7513 && (decl = mathfn_built_in (newtype, fcode)))
7514 return fold_convert (ftype,
7515 build_call_expr (decl, 1,
7516 fold_convert (newtype, arg0)));
7521 /* FNDECL is assumed to be builtin which can narrow the FP type of
7522 the argument, for instance lround((double)f) -> lroundf (f).
7523 Do the transformation for a call with argument ARG. */
7526 fold_fixed_mathfn (tree fndecl, tree arg)
7528 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7530 if (!validate_arg (arg, REAL_TYPE))
7533 /* If argument is already integer valued, and we don't need to worry
7534 about setting errno, there's no need to perform rounding. */
7535 if (! flag_errno_math && integer_valued_real_p (arg))
7536 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
7540 tree ftype = TREE_TYPE (arg);
7541 tree arg0 = strip_float_extensions (arg);
7542 tree newtype = TREE_TYPE (arg0);
7545 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7546 && (decl = mathfn_built_in (newtype, fcode)))
7547 return build_call_expr (decl, 1, fold_convert (newtype, arg0));
7550 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7551 sizeof (long long) == sizeof (long). */
7552 if (TYPE_PRECISION (long_long_integer_type_node)
7553 == TYPE_PRECISION (long_integer_type_node))
7555 tree newfn = NULL_TREE;
7558 CASE_FLT_FN (BUILT_IN_LLCEIL):
7559 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7562 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7563 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7566 CASE_FLT_FN (BUILT_IN_LLROUND):
7567 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7570 CASE_FLT_FN (BUILT_IN_LLRINT):
7571 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7580 tree newcall = build_call_expr(newfn, 1, arg);
7581 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7588 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7589 return type. Return NULL_TREE if no simplification can be made. */
7592 fold_builtin_cabs (tree arg, tree type, tree fndecl)
7596 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7597 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7600 /* Calculate the result when the argument is a constant. */
7601 if (TREE_CODE (arg) == COMPLEX_CST
7602 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7606 if (TREE_CODE (arg) == COMPLEX_EXPR)
7608 tree real = TREE_OPERAND (arg, 0);
7609 tree imag = TREE_OPERAND (arg, 1);
7611 /* If either part is zero, cabs is fabs of the other. */
7612 if (real_zerop (real))
7613 return fold_build1 (ABS_EXPR, type, imag);
7614 if (real_zerop (imag))
7615 return fold_build1 (ABS_EXPR, type, real);
7617 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7618 if (flag_unsafe_math_optimizations
7619 && operand_equal_p (real, imag, OEP_PURE_SAME))
7621 const REAL_VALUE_TYPE sqrt2_trunc
7622 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7624 return fold_build2 (MULT_EXPR, type,
7625 fold_build1 (ABS_EXPR, type, real),
7626 build_real (type, sqrt2_trunc));
7630 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7631 if (TREE_CODE (arg) == NEGATE_EXPR
7632 || TREE_CODE (arg) == CONJ_EXPR)
7633 return build_call_expr (fndecl, 1, TREE_OPERAND (arg, 0));
7635 /* Don't do this when optimizing for size. */
7636 if (flag_unsafe_math_optimizations
7637 && optimize && optimize_function_for_speed_p (cfun))
7639 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7641 if (sqrtfn != NULL_TREE)
7643 tree rpart, ipart, result;
7645 arg = builtin_save_expr (arg);
7647 rpart = fold_build1 (REALPART_EXPR, type, arg);
7648 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7650 rpart = builtin_save_expr (rpart);
7651 ipart = builtin_save_expr (ipart);
7653 result = fold_build2 (PLUS_EXPR, type,
7654 fold_build2 (MULT_EXPR, type,
7656 fold_build2 (MULT_EXPR, type,
7659 return build_call_expr (sqrtfn, 1, result);
7666 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7667 Return NULL_TREE if no simplification can be made. */
7670 fold_builtin_sqrt (tree arg, tree type)
7673 enum built_in_function fcode;
7676 if (!validate_arg (arg, REAL_TYPE))
7679 /* Calculate the result when the argument is a constant. */
7680 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7683 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7684 fcode = builtin_mathfn_code (arg);
7685 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7687 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7688 arg = fold_build2 (MULT_EXPR, type,
7689 CALL_EXPR_ARG (arg, 0),
7690 build_real (type, dconsthalf));
7691 return build_call_expr (expfn, 1, arg);
7694 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7695 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7697 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7701 tree arg0 = CALL_EXPR_ARG (arg, 0);
7703 /* The inner root was either sqrt or cbrt. */
7704 /* This was a conditional expression but it triggered a bug
7706 REAL_VALUE_TYPE dconstroot;
7707 if (BUILTIN_SQRT_P (fcode))
7708 dconstroot = dconsthalf;
7710 dconstroot = dconst_third ();
7712 /* Adjust for the outer root. */
7713 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7714 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7715 tree_root = build_real (type, dconstroot);
7716 return build_call_expr (powfn, 2, arg0, tree_root);
7720 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7721 if (flag_unsafe_math_optimizations
7722 && (fcode == BUILT_IN_POW
7723 || fcode == BUILT_IN_POWF
7724 || fcode == BUILT_IN_POWL))
7726 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7727 tree arg0 = CALL_EXPR_ARG (arg, 0);
7728 tree arg1 = CALL_EXPR_ARG (arg, 1);
7730 if (!tree_expr_nonnegative_p (arg0))
7731 arg0 = build1 (ABS_EXPR, type, arg0);
7732 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7733 build_real (type, dconsthalf));
7734 return build_call_expr (powfn, 2, arg0, narg1);
7740 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7741 Return NULL_TREE if no simplification can be made. */
7744 fold_builtin_cbrt (tree arg, tree type)
7746 const enum built_in_function fcode = builtin_mathfn_code (arg);
7749 if (!validate_arg (arg, REAL_TYPE))
7752 /* Calculate the result when the argument is a constant. */
7753 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7756 if (flag_unsafe_math_optimizations)
7758 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7759 if (BUILTIN_EXPONENT_P (fcode))
7761 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7762 const REAL_VALUE_TYPE third_trunc =
7763 real_value_truncate (TYPE_MODE (type), dconst_third ());
7764 arg = fold_build2 (MULT_EXPR, type,
7765 CALL_EXPR_ARG (arg, 0),
7766 build_real (type, third_trunc));
7767 return build_call_expr (expfn, 1, arg);
7770 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7771 if (BUILTIN_SQRT_P (fcode))
7773 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7777 tree arg0 = CALL_EXPR_ARG (arg, 0);
7779 REAL_VALUE_TYPE dconstroot = dconst_third ();
7781 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7782 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7783 tree_root = build_real (type, dconstroot);
7784 return build_call_expr (powfn, 2, arg0, tree_root);
7788 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7789 if (BUILTIN_CBRT_P (fcode))
7791 tree arg0 = CALL_EXPR_ARG (arg, 0);
7792 if (tree_expr_nonnegative_p (arg0))
7794 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7799 REAL_VALUE_TYPE dconstroot;
7801 real_arithmetic (&dconstroot, MULT_EXPR,
7802 dconst_third_ptr (), dconst_third_ptr ());
7803 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7804 tree_root = build_real (type, dconstroot);
7805 return build_call_expr (powfn, 2, arg0, tree_root);
7810 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7811 if (fcode == BUILT_IN_POW
7812 || fcode == BUILT_IN_POWF
7813 || fcode == BUILT_IN_POWL)
7815 tree arg00 = CALL_EXPR_ARG (arg, 0);
7816 tree arg01 = CALL_EXPR_ARG (arg, 1);
7817 if (tree_expr_nonnegative_p (arg00))
7819 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7820 const REAL_VALUE_TYPE dconstroot
7821 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7822 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7823 build_real (type, dconstroot));
7824 return build_call_expr (powfn, 2, arg00, narg01);
7831 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7832 TYPE is the type of the return value. Return NULL_TREE if no
7833 simplification can be made. */
7836 fold_builtin_cos (tree arg, tree type, tree fndecl)
7840 if (!validate_arg (arg, REAL_TYPE))
7843 /* Calculate the result when the argument is a constant. */
7844 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7847 /* Optimize cos(-x) into cos (x). */
7848 if ((narg = fold_strip_sign_ops (arg)))
7849 return build_call_expr (fndecl, 1, narg);
7854 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7855 Return NULL_TREE if no simplification can be made. */
7858 fold_builtin_cosh (tree arg, tree type, tree fndecl)
7860 if (validate_arg (arg, REAL_TYPE))
7864 /* Calculate the result when the argument is a constant. */
7865 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7868 /* Optimize cosh(-x) into cosh (x). */
7869 if ((narg = fold_strip_sign_ops (arg)))
7870 return build_call_expr (fndecl, 1, narg);
7876 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7877 Return NULL_TREE if no simplification can be made. */
7880 fold_builtin_tan (tree arg, tree type)
7882 enum built_in_function fcode;
7885 if (!validate_arg (arg, REAL_TYPE))
7888 /* Calculate the result when the argument is a constant. */
7889 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7892 /* Optimize tan(atan(x)) = x. */
7893 fcode = builtin_mathfn_code (arg);
7894 if (flag_unsafe_math_optimizations
7895 && (fcode == BUILT_IN_ATAN
7896 || fcode == BUILT_IN_ATANF
7897 || fcode == BUILT_IN_ATANL))
7898 return CALL_EXPR_ARG (arg, 0);
7903 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7904 NULL_TREE if no simplification can be made. */
7907 fold_builtin_sincos (tree arg0, tree arg1, tree arg2)
7912 if (!validate_arg (arg0, REAL_TYPE)
7913 || !validate_arg (arg1, POINTER_TYPE)
7914 || !validate_arg (arg2, POINTER_TYPE))
7917 type = TREE_TYPE (arg0);
7919 /* Calculate the result when the argument is a constant. */
7920 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7923 /* Canonicalize sincos to cexpi. */
7924 if (!TARGET_C99_FUNCTIONS)
7926 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7930 call = build_call_expr (fn, 1, arg0);
7931 call = builtin_save_expr (call);
7933 return build2 (COMPOUND_EXPR, type,
7934 build2 (MODIFY_EXPR, void_type_node,
7935 build_fold_indirect_ref (arg1),
7936 build1 (IMAGPART_EXPR, type, call)),
7937 build2 (MODIFY_EXPR, void_type_node,
7938 build_fold_indirect_ref (arg2),
7939 build1 (REALPART_EXPR, type, call)));
7942 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7943 NULL_TREE if no simplification can be made. */
7946 fold_builtin_cexp (tree arg0, tree type)
7949 tree realp, imagp, ifn;
7951 if (!validate_arg (arg0, COMPLEX_TYPE))
7954 rtype = TREE_TYPE (TREE_TYPE (arg0));
7956 /* In case we can figure out the real part of arg0 and it is constant zero
7958 if (!TARGET_C99_FUNCTIONS)
7960 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7964 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
7965 && real_zerop (realp))
7967 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
7968 return build_call_expr (ifn, 1, narg);
7971 /* In case we can easily decompose real and imaginary parts split cexp
7972 to exp (r) * cexpi (i). */
7973 if (flag_unsafe_math_optimizations
7976 tree rfn, rcall, icall;
7978 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7982 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
7986 icall = build_call_expr (ifn, 1, imagp);
7987 icall = builtin_save_expr (icall);
7988 rcall = build_call_expr (rfn, 1, realp);
7989 rcall = builtin_save_expr (rcall);
7990 return fold_build2 (COMPLEX_EXPR, type,
7991 fold_build2 (MULT_EXPR, rtype,
7993 fold_build1 (REALPART_EXPR, rtype, icall)),
7994 fold_build2 (MULT_EXPR, rtype,
7996 fold_build1 (IMAGPART_EXPR, rtype, icall)));
8002 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8003 Return NULL_TREE if no simplification can be made. */
8006 fold_builtin_trunc (tree fndecl, tree arg)
8008 if (!validate_arg (arg, REAL_TYPE))
8011 /* Optimize trunc of constant value. */
8012 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8014 REAL_VALUE_TYPE r, x;
8015 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8017 x = TREE_REAL_CST (arg);
8018 real_trunc (&r, TYPE_MODE (type), &x);
8019 return build_real (type, r);
8022 return fold_trunc_transparent_mathfn (fndecl, arg);
8025 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8026 Return NULL_TREE if no simplification can be made. */
8029 fold_builtin_floor (tree fndecl, tree arg)
8031 if (!validate_arg (arg, REAL_TYPE))
8034 /* Optimize floor of constant value. */
8035 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8039 x = TREE_REAL_CST (arg);
8040 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8042 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8045 real_floor (&r, TYPE_MODE (type), &x);
8046 return build_real (type, r);
8050 /* Fold floor (x) where x is nonnegative to trunc (x). */
8051 if (tree_expr_nonnegative_p (arg))
8053 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8055 return build_call_expr (truncfn, 1, arg);
8058 return fold_trunc_transparent_mathfn (fndecl, arg);
8061 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8062 Return NULL_TREE if no simplification can be made. */
8065 fold_builtin_ceil (tree fndecl, tree arg)
8067 if (!validate_arg (arg, REAL_TYPE))
8070 /* Optimize ceil of constant value. */
8071 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8075 x = TREE_REAL_CST (arg);
8076 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8078 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8081 real_ceil (&r, TYPE_MODE (type), &x);
8082 return build_real (type, r);
8086 return fold_trunc_transparent_mathfn (fndecl, arg);
8089 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8090 Return NULL_TREE if no simplification can be made. */
8093 fold_builtin_round (tree fndecl, tree arg)
8095 if (!validate_arg (arg, REAL_TYPE))
8098 /* Optimize round of constant value. */
8099 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8103 x = TREE_REAL_CST (arg);
8104 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8106 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8109 real_round (&r, TYPE_MODE (type), &x);
8110 return build_real (type, r);
8114 return fold_trunc_transparent_mathfn (fndecl, arg);
8117 /* Fold function call to builtin lround, lroundf or lroundl (or the
8118 corresponding long long versions) and other rounding functions. ARG
8119 is the argument to the call. Return NULL_TREE if no simplification
8123 fold_builtin_int_roundingfn (tree fndecl, tree arg)
8125 if (!validate_arg (arg, REAL_TYPE))
8128 /* Optimize lround of constant value. */
8129 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8131 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8133 if (real_isfinite (&x))
8135 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8136 tree ftype = TREE_TYPE (arg);
8137 unsigned HOST_WIDE_INT lo2;
8138 HOST_WIDE_INT hi, lo;
8141 switch (DECL_FUNCTION_CODE (fndecl))
8143 CASE_FLT_FN (BUILT_IN_LFLOOR):
8144 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8145 real_floor (&r, TYPE_MODE (ftype), &x);
8148 CASE_FLT_FN (BUILT_IN_LCEIL):
8149 CASE_FLT_FN (BUILT_IN_LLCEIL):
8150 real_ceil (&r, TYPE_MODE (ftype), &x);
8153 CASE_FLT_FN (BUILT_IN_LROUND):
8154 CASE_FLT_FN (BUILT_IN_LLROUND):
8155 real_round (&r, TYPE_MODE (ftype), &x);
8162 REAL_VALUE_TO_INT (&lo, &hi, r);
8163 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
8164 return build_int_cst_wide (itype, lo2, hi);
8168 switch (DECL_FUNCTION_CODE (fndecl))
8170 CASE_FLT_FN (BUILT_IN_LFLOOR):
8171 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8172 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8173 if (tree_expr_nonnegative_p (arg))
8174 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
8180 return fold_fixed_mathfn (fndecl, arg);
8183 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8184 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8185 the argument to the call. Return NULL_TREE if no simplification can
8189 fold_builtin_bitop (tree fndecl, tree arg)
8191 if (!validate_arg (arg, INTEGER_TYPE))
8194 /* Optimize for constant argument. */
8195 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8197 HOST_WIDE_INT hi, width, result;
8198 unsigned HOST_WIDE_INT lo;
8201 type = TREE_TYPE (arg);
8202 width = TYPE_PRECISION (type);
8203 lo = TREE_INT_CST_LOW (arg);
8205 /* Clear all the bits that are beyond the type's precision. */
8206 if (width > HOST_BITS_PER_WIDE_INT)
8208 hi = TREE_INT_CST_HIGH (arg);
8209 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8210 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
8215 if (width < HOST_BITS_PER_WIDE_INT)
8216 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8219 switch (DECL_FUNCTION_CODE (fndecl))
8221 CASE_INT_FN (BUILT_IN_FFS):
8223 result = exact_log2 (lo & -lo) + 1;
8225 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
8230 CASE_INT_FN (BUILT_IN_CLZ):
8232 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8234 result = width - floor_log2 (lo) - 1;
8235 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8239 CASE_INT_FN (BUILT_IN_CTZ):
8241 result = exact_log2 (lo & -lo);
8243 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
8244 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8248 CASE_INT_FN (BUILT_IN_POPCOUNT):
8251 result++, lo &= lo - 1;
8253 result++, hi &= hi - 1;
8256 CASE_INT_FN (BUILT_IN_PARITY):
8259 result++, lo &= lo - 1;
8261 result++, hi &= hi - 1;
8269 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8275 /* Fold function call to builtin_bswap and the long and long long
8276 variants. Return NULL_TREE if no simplification can be made. */
8278 fold_builtin_bswap (tree fndecl, tree arg)
8280 if (! validate_arg (arg, INTEGER_TYPE))
8283 /* Optimize constant value. */
8284 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8286 HOST_WIDE_INT hi, width, r_hi = 0;
8287 unsigned HOST_WIDE_INT lo, r_lo = 0;
8290 type = TREE_TYPE (arg);
8291 width = TYPE_PRECISION (type);
8292 lo = TREE_INT_CST_LOW (arg);
8293 hi = TREE_INT_CST_HIGH (arg);
8295 switch (DECL_FUNCTION_CODE (fndecl))
8297 case BUILT_IN_BSWAP32:
8298 case BUILT_IN_BSWAP64:
8302 for (s = 0; s < width; s += 8)
8304 int d = width - s - 8;
8305 unsigned HOST_WIDE_INT byte;
8307 if (s < HOST_BITS_PER_WIDE_INT)
8308 byte = (lo >> s) & 0xff;
8310 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8312 if (d < HOST_BITS_PER_WIDE_INT)
8315 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8325 if (width < HOST_BITS_PER_WIDE_INT)
8326 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8328 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8334 /* Return true if EXPR is the real constant contained in VALUE. */
8337 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
8341 return ((TREE_CODE (expr) == REAL_CST
8342 && !TREE_OVERFLOW (expr)
8343 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
8344 || (TREE_CODE (expr) == COMPLEX_CST
8345 && real_dconstp (TREE_REALPART (expr), value)
8346 && real_zerop (TREE_IMAGPART (expr))));
8349 /* A subroutine of fold_builtin to fold the various logarithmic
8350 functions. Return NULL_TREE if no simplification can me made.
8351 FUNC is the corresponding MPFR logarithm function. */
8354 fold_builtin_logarithm (tree fndecl, tree arg,
8355 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8357 if (validate_arg (arg, REAL_TYPE))
8359 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8361 const enum built_in_function fcode = builtin_mathfn_code (arg);
8363 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
8364 instead we'll look for 'e' truncated to MODE. So only do
8365 this if flag_unsafe_math_optimizations is set. */
8366 if (flag_unsafe_math_optimizations && func == mpfr_log)
8368 const REAL_VALUE_TYPE e_truncated =
8369 real_value_truncate (TYPE_MODE (type), dconst_e ());
8370 if (real_dconstp (arg, &e_truncated))
8371 return build_real (type, dconst1);
8374 /* Calculate the result when the argument is a constant. */
8375 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8378 /* Special case, optimize logN(expN(x)) = x. */
8379 if (flag_unsafe_math_optimizations
8380 && ((func == mpfr_log
8381 && (fcode == BUILT_IN_EXP
8382 || fcode == BUILT_IN_EXPF
8383 || fcode == BUILT_IN_EXPL))
8384 || (func == mpfr_log2
8385 && (fcode == BUILT_IN_EXP2
8386 || fcode == BUILT_IN_EXP2F
8387 || fcode == BUILT_IN_EXP2L))
8388 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8389 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8391 /* Optimize logN(func()) for various exponential functions. We
8392 want to determine the value "x" and the power "exponent" in
8393 order to transform logN(x**exponent) into exponent*logN(x). */
8394 if (flag_unsafe_math_optimizations)
8396 tree exponent = 0, x = 0;
8400 CASE_FLT_FN (BUILT_IN_EXP):
8401 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8402 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8404 exponent = CALL_EXPR_ARG (arg, 0);
8406 CASE_FLT_FN (BUILT_IN_EXP2):
8407 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8408 x = build_real (type, dconst2);
8409 exponent = CALL_EXPR_ARG (arg, 0);
8411 CASE_FLT_FN (BUILT_IN_EXP10):
8412 CASE_FLT_FN (BUILT_IN_POW10):
8413 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8415 REAL_VALUE_TYPE dconst10;
8416 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8417 x = build_real (type, dconst10);
8419 exponent = CALL_EXPR_ARG (arg, 0);
8421 CASE_FLT_FN (BUILT_IN_SQRT):
8422 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8423 x = CALL_EXPR_ARG (arg, 0);
8424 exponent = build_real (type, dconsthalf);
8426 CASE_FLT_FN (BUILT_IN_CBRT):
8427 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8428 x = CALL_EXPR_ARG (arg, 0);
8429 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8432 CASE_FLT_FN (BUILT_IN_POW):
8433 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8434 x = CALL_EXPR_ARG (arg, 0);
8435 exponent = CALL_EXPR_ARG (arg, 1);
8441 /* Now perform the optimization. */
8444 tree logfn = build_call_expr (fndecl, 1, x);
8445 return fold_build2 (MULT_EXPR, type, exponent, logfn);
8453 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8454 NULL_TREE if no simplification can be made. */
8457 fold_builtin_hypot (tree fndecl, tree arg0, tree arg1, tree type)
8459 tree res, narg0, narg1;
8461 if (!validate_arg (arg0, REAL_TYPE)
8462 || !validate_arg (arg1, REAL_TYPE))
8465 /* Calculate the result when the argument is a constant. */
8466 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8469 /* If either argument to hypot has a negate or abs, strip that off.
8470 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8471 narg0 = fold_strip_sign_ops (arg0);
8472 narg1 = fold_strip_sign_ops (arg1);
8475 return build_call_expr (fndecl, 2, narg0 ? narg0 : arg0,
8476 narg1 ? narg1 : arg1);
8479 /* If either argument is zero, hypot is fabs of the other. */
8480 if (real_zerop (arg0))
8481 return fold_build1 (ABS_EXPR, type, arg1);
8482 else if (real_zerop (arg1))
8483 return fold_build1 (ABS_EXPR, type, arg0);
8485 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8486 if (flag_unsafe_math_optimizations
8487 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8489 const REAL_VALUE_TYPE sqrt2_trunc
8490 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8491 return fold_build2 (MULT_EXPR, type,
8492 fold_build1 (ABS_EXPR, type, arg0),
8493 build_real (type, sqrt2_trunc));
8500 /* Fold a builtin function call to pow, powf, or powl. Return
8501 NULL_TREE if no simplification can be made. */
8503 fold_builtin_pow (tree fndecl, tree arg0, tree arg1, tree type)
8507 if (!validate_arg (arg0, REAL_TYPE)
8508 || !validate_arg (arg1, REAL_TYPE))
8511 /* Calculate the result when the argument is a constant. */
8512 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8515 /* Optimize pow(1.0,y) = 1.0. */
8516 if (real_onep (arg0))
8517 return omit_one_operand (type, build_real (type, dconst1), arg1);
8519 if (TREE_CODE (arg1) == REAL_CST
8520 && !TREE_OVERFLOW (arg1))
8522 REAL_VALUE_TYPE cint;
8526 c = TREE_REAL_CST (arg1);
8528 /* Optimize pow(x,0.0) = 1.0. */
8529 if (REAL_VALUES_EQUAL (c, dconst0))
8530 return omit_one_operand (type, build_real (type, dconst1),
8533 /* Optimize pow(x,1.0) = x. */
8534 if (REAL_VALUES_EQUAL (c, dconst1))
8537 /* Optimize pow(x,-1.0) = 1.0/x. */
8538 if (REAL_VALUES_EQUAL (c, dconstm1))
8539 return fold_build2 (RDIV_EXPR, type,
8540 build_real (type, dconst1), arg0);
8542 /* Optimize pow(x,0.5) = sqrt(x). */
8543 if (flag_unsafe_math_optimizations
8544 && REAL_VALUES_EQUAL (c, dconsthalf))
8546 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8548 if (sqrtfn != NULL_TREE)
8549 return build_call_expr (sqrtfn, 1, arg0);
8552 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8553 if (flag_unsafe_math_optimizations)
8555 const REAL_VALUE_TYPE dconstroot
8556 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8558 if (REAL_VALUES_EQUAL (c, dconstroot))
8560 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8561 if (cbrtfn != NULL_TREE)
8562 return build_call_expr (cbrtfn, 1, arg0);
8566 /* Check for an integer exponent. */
8567 n = real_to_integer (&c);
8568 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8569 if (real_identical (&c, &cint))
8571 /* Attempt to evaluate pow at compile-time, unless this should
8572 raise an exception. */
8573 if (TREE_CODE (arg0) == REAL_CST
8574 && !TREE_OVERFLOW (arg0)
8576 || (!flag_trapping_math && !flag_errno_math)
8577 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8582 x = TREE_REAL_CST (arg0);
8583 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8584 if (flag_unsafe_math_optimizations || !inexact)
8585 return build_real (type, x);
8588 /* Strip sign ops from even integer powers. */
8589 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8591 tree narg0 = fold_strip_sign_ops (arg0);
8593 return build_call_expr (fndecl, 2, narg0, arg1);
8598 if (flag_unsafe_math_optimizations)
8600 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8602 /* Optimize pow(expN(x),y) = expN(x*y). */
8603 if (BUILTIN_EXPONENT_P (fcode))
8605 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8606 tree arg = CALL_EXPR_ARG (arg0, 0);
8607 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8608 return build_call_expr (expfn, 1, arg);
8611 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8612 if (BUILTIN_SQRT_P (fcode))
8614 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8615 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8616 build_real (type, dconsthalf));
8617 return build_call_expr (fndecl, 2, narg0, narg1);
8620 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8621 if (BUILTIN_CBRT_P (fcode))
8623 tree arg = CALL_EXPR_ARG (arg0, 0);
8624 if (tree_expr_nonnegative_p (arg))
8626 const REAL_VALUE_TYPE dconstroot
8627 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8628 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8629 build_real (type, dconstroot));
8630 return build_call_expr (fndecl, 2, arg, narg1);
8634 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8635 if (fcode == BUILT_IN_POW
8636 || fcode == BUILT_IN_POWF
8637 || fcode == BUILT_IN_POWL)
8639 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8640 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8641 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8642 return build_call_expr (fndecl, 2, arg00, narg1);
8649 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8650 Return NULL_TREE if no simplification can be made. */
8652 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED,
8653 tree arg0, tree arg1, tree type)
8655 if (!validate_arg (arg0, REAL_TYPE)
8656 || !validate_arg (arg1, INTEGER_TYPE))
8659 /* Optimize pow(1.0,y) = 1.0. */
8660 if (real_onep (arg0))
8661 return omit_one_operand (type, build_real (type, dconst1), arg1);
8663 if (host_integerp (arg1, 0))
8665 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8667 /* Evaluate powi at compile-time. */
8668 if (TREE_CODE (arg0) == REAL_CST
8669 && !TREE_OVERFLOW (arg0))
8672 x = TREE_REAL_CST (arg0);
8673 real_powi (&x, TYPE_MODE (type), &x, c);
8674 return build_real (type, x);
8677 /* Optimize pow(x,0) = 1.0. */
8679 return omit_one_operand (type, build_real (type, dconst1),
8682 /* Optimize pow(x,1) = x. */
8686 /* Optimize pow(x,-1) = 1.0/x. */
8688 return fold_build2 (RDIV_EXPR, type,
8689 build_real (type, dconst1), arg0);
8695 /* A subroutine of fold_builtin to fold the various exponent
8696 functions. Return NULL_TREE if no simplification can be made.
8697 FUNC is the corresponding MPFR exponent function. */
8700 fold_builtin_exponent (tree fndecl, tree arg,
8701 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8703 if (validate_arg (arg, REAL_TYPE))
8705 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8708 /* Calculate the result when the argument is a constant. */
8709 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8712 /* Optimize expN(logN(x)) = x. */
8713 if (flag_unsafe_math_optimizations)
8715 const enum built_in_function fcode = builtin_mathfn_code (arg);
8717 if ((func == mpfr_exp
8718 && (fcode == BUILT_IN_LOG
8719 || fcode == BUILT_IN_LOGF
8720 || fcode == BUILT_IN_LOGL))
8721 || (func == mpfr_exp2
8722 && (fcode == BUILT_IN_LOG2
8723 || fcode == BUILT_IN_LOG2F
8724 || fcode == BUILT_IN_LOG2L))
8725 || (func == mpfr_exp10
8726 && (fcode == BUILT_IN_LOG10
8727 || fcode == BUILT_IN_LOG10F
8728 || fcode == BUILT_IN_LOG10L)))
8729 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8736 /* Return true if VAR is a VAR_DECL or a component thereof. */
8739 var_decl_component_p (tree var)
8742 while (handled_component_p (inner))
8743 inner = TREE_OPERAND (inner, 0);
8744 return SSA_VAR_P (inner);
8747 /* Fold function call to builtin memset. Return
8748 NULL_TREE if no simplification can be made. */
8751 fold_builtin_memset (tree dest, tree c, tree len, tree type, bool ignore)
8754 unsigned HOST_WIDE_INT length, cval;
8756 if (! validate_arg (dest, POINTER_TYPE)
8757 || ! validate_arg (c, INTEGER_TYPE)
8758 || ! validate_arg (len, INTEGER_TYPE))
8761 if (! host_integerp (len, 1))
8764 /* If the LEN parameter is zero, return DEST. */
8765 if (integer_zerop (len))
8766 return omit_one_operand (type, dest, c);
8768 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8773 if (TREE_CODE (var) != ADDR_EXPR)
8776 var = TREE_OPERAND (var, 0);
8777 if (TREE_THIS_VOLATILE (var))
8780 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8781 && !POINTER_TYPE_P (TREE_TYPE (var)))
8784 if (! var_decl_component_p (var))
8787 length = tree_low_cst (len, 1);
8788 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8789 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8793 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8796 if (integer_zerop (c))
8800 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8803 cval = tree_low_cst (c, 1);
8807 cval |= (cval << 31) << 1;
8810 ret = build_int_cst_type (TREE_TYPE (var), cval);
8811 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
8815 return omit_one_operand (type, dest, ret);
8818 /* Fold function call to builtin memset. Return
8819 NULL_TREE if no simplification can be made. */
8822 fold_builtin_bzero (tree dest, tree size, bool ignore)
8824 if (! validate_arg (dest, POINTER_TYPE)
8825 || ! validate_arg (size, INTEGER_TYPE))
8831 /* New argument list transforming bzero(ptr x, int y) to
8832 memset(ptr x, int 0, size_t y). This is done this way
8833 so that if it isn't expanded inline, we fallback to
8834 calling bzero instead of memset. */
8836 return fold_builtin_memset (dest, integer_zero_node,
8837 fold_convert (sizetype, size),
8838 void_type_node, ignore);
8841 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8842 NULL_TREE if no simplification can be made.
8843 If ENDP is 0, return DEST (like memcpy).
8844 If ENDP is 1, return DEST+LEN (like mempcpy).
8845 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8846 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8850 fold_builtin_memory_op (tree dest, tree src, tree len, tree type, bool ignore, int endp)
8852 tree destvar, srcvar, expr;
8854 if (! validate_arg (dest, POINTER_TYPE)
8855 || ! validate_arg (src, POINTER_TYPE)
8856 || ! validate_arg (len, INTEGER_TYPE))
8859 /* If the LEN parameter is zero, return DEST. */
8860 if (integer_zerop (len))
8861 return omit_one_operand (type, dest, src);
8863 /* If SRC and DEST are the same (and not volatile), return
8864 DEST{,+LEN,+LEN-1}. */
8865 if (operand_equal_p (src, dest, 0))
8869 tree srctype, desttype;
8870 int src_align, dest_align;
8874 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8875 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8877 /* Both DEST and SRC must be pointer types.
8878 ??? This is what old code did. Is the testing for pointer types
8881 If either SRC is readonly or length is 1, we can use memcpy. */
8882 if (dest_align && src_align
8883 && (readonly_data_expr (src)
8884 || (host_integerp (len, 1)
8885 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8886 tree_low_cst (len, 1)))))
8888 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8891 return build_call_expr (fn, 3, dest, src, len);
8896 if (!host_integerp (len, 0))
8899 This logic lose for arguments like (type *)malloc (sizeof (type)),
8900 since we strip the casts of up to VOID return value from malloc.
8901 Perhaps we ought to inherit type from non-VOID argument here? */
8904 srctype = TREE_TYPE (TREE_TYPE (src));
8905 desttype = TREE_TYPE (TREE_TYPE (dest));
8906 if (!srctype || !desttype
8907 || !TYPE_SIZE_UNIT (srctype)
8908 || !TYPE_SIZE_UNIT (desttype)
8909 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8910 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8913 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8914 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8915 if (dest_align < (int) TYPE_ALIGN (desttype)
8916 || src_align < (int) TYPE_ALIGN (srctype))
8920 dest = builtin_save_expr (dest);
8923 if (tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8925 srcvar = build_fold_indirect_ref (src);
8926 if (TREE_THIS_VOLATILE (srcvar))
8928 else if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8930 /* With memcpy, it is possible to bypass aliasing rules, so without
8931 this check i.e. execute/20060930-2.c would be misoptimized,
8932 because it use conflicting alias set to hold argument for the
8933 memcpy call. This check is probably unnecessary with
8934 -fno-strict-aliasing. Similarly for destvar. See also
8936 else if (!var_decl_component_p (srcvar))
8940 destvar = NULL_TREE;
8941 if (tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8943 destvar = build_fold_indirect_ref (dest);
8944 if (TREE_THIS_VOLATILE (destvar))
8945 destvar = NULL_TREE;
8946 else if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8947 destvar = NULL_TREE;
8948 else if (!var_decl_component_p (destvar))
8949 destvar = NULL_TREE;
8952 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8955 if (srcvar == NULL_TREE)
8958 if (TREE_ADDRESSABLE (TREE_TYPE (destvar)))
8962 if (src_align < (int) TYPE_ALIGN (srctype))
8964 if (AGGREGATE_TYPE_P (srctype)
8965 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (srctype), src_align))
8968 srctype = build_variant_type_copy (srctype);
8969 TYPE_ALIGN (srctype) = src_align;
8970 TYPE_USER_ALIGN (srctype) = 1;
8971 TYPE_PACKED (srctype) = 1;
8973 srcptype = build_pointer_type_for_mode (srctype, ptr_mode, true);
8974 src = fold_convert (srcptype, src);
8975 srcvar = build_fold_indirect_ref (src);
8977 else if (destvar == NULL_TREE)
8980 if (TREE_ADDRESSABLE (TREE_TYPE (srcvar)))
8984 if (dest_align < (int) TYPE_ALIGN (desttype))
8986 if (AGGREGATE_TYPE_P (desttype)
8987 || SLOW_UNALIGNED_ACCESS (TYPE_MODE (desttype), dest_align))
8990 desttype = build_variant_type_copy (desttype);
8991 TYPE_ALIGN (desttype) = dest_align;
8992 TYPE_USER_ALIGN (desttype) = 1;
8993 TYPE_PACKED (desttype) = 1;
8995 destptype = build_pointer_type_for_mode (desttype, ptr_mode, true);
8996 dest = fold_convert (destptype, dest);
8997 destvar = build_fold_indirect_ref (dest);
9000 if (srctype == desttype
9001 || (gimple_in_ssa_p (cfun)
9002 && useless_type_conversion_p (desttype, srctype)))
9004 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
9005 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
9006 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
9007 || POINTER_TYPE_P (TREE_TYPE (destvar))))
9008 expr = fold_convert (TREE_TYPE (destvar), srcvar);
9010 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
9011 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
9017 if (endp == 0 || endp == 3)
9018 return omit_one_operand (type, dest, expr);
9024 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
9027 len = fold_convert (sizetype, len);
9028 dest = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
9029 dest = fold_convert (type, dest);
9031 dest = omit_one_operand (type, dest, expr);
9035 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9036 If LEN is not NULL, it represents the length of the string to be
9037 copied. Return NULL_TREE if no simplification can be made. */
9040 fold_builtin_strcpy (tree fndecl, tree dest, tree src, tree len)
9044 if (!validate_arg (dest, POINTER_TYPE)
9045 || !validate_arg (src, POINTER_TYPE))
9048 /* If SRC and DEST are the same (and not volatile), return DEST. */
9049 if (operand_equal_p (src, dest, 0))
9050 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
9052 if (optimize_function_for_size_p (cfun))
9055 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9061 len = c_strlen (src, 1);
9062 if (! len || TREE_SIDE_EFFECTS (len))
9066 len = size_binop (PLUS_EXPR, len, ssize_int (1));
9067 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9068 build_call_expr (fn, 3, dest, src, len));
9071 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9072 If SLEN is not NULL, it represents the length of the source string.
9073 Return NULL_TREE if no simplification can be made. */
9076 fold_builtin_strncpy (tree fndecl, tree dest, tree src, tree len, tree slen)
9080 if (!validate_arg (dest, POINTER_TYPE)
9081 || !validate_arg (src, POINTER_TYPE)
9082 || !validate_arg (len, INTEGER_TYPE))
9085 /* If the LEN parameter is zero, return DEST. */
9086 if (integer_zerop (len))
9087 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9089 /* We can't compare slen with len as constants below if len is not a
9091 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9095 slen = c_strlen (src, 1);
9097 /* Now, we must be passed a constant src ptr parameter. */
9098 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9101 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
9103 /* We do not support simplification of this case, though we do
9104 support it when expanding trees into RTL. */
9105 /* FIXME: generate a call to __builtin_memset. */
9106 if (tree_int_cst_lt (slen, len))
9109 /* OK transform into builtin memcpy. */
9110 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
9113 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
9114 build_call_expr (fn, 3, dest, src, len));
9117 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9118 arguments to the call, and TYPE is its return type.
9119 Return NULL_TREE if no simplification can be made. */
9122 fold_builtin_memchr (tree arg1, tree arg2, tree len, tree type)
9124 if (!validate_arg (arg1, POINTER_TYPE)
9125 || !validate_arg (arg2, INTEGER_TYPE)
9126 || !validate_arg (len, INTEGER_TYPE))
9132 if (TREE_CODE (arg2) != INTEGER_CST
9133 || !host_integerp (len, 1))
9136 p1 = c_getstr (arg1);
9137 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9143 if (target_char_cast (arg2, &c))
9146 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
9149 return build_int_cst (TREE_TYPE (arg1), 0);
9151 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
9153 return fold_convert (type, tem);
9159 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9160 Return NULL_TREE if no simplification can be made. */
9163 fold_builtin_memcmp (tree arg1, tree arg2, tree len)
9165 const char *p1, *p2;
9167 if (!validate_arg (arg1, POINTER_TYPE)
9168 || !validate_arg (arg2, POINTER_TYPE)
9169 || !validate_arg (len, INTEGER_TYPE))
9172 /* If the LEN parameter is zero, return zero. */
9173 if (integer_zerop (len))
9174 return omit_two_operands (integer_type_node, integer_zero_node,
9177 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9178 if (operand_equal_p (arg1, arg2, 0))
9179 return omit_one_operand (integer_type_node, integer_zero_node, len);
9181 p1 = c_getstr (arg1);
9182 p2 = c_getstr (arg2);
9184 /* If all arguments are constant, and the value of len is not greater
9185 than the lengths of arg1 and arg2, evaluate at compile-time. */
9186 if (host_integerp (len, 1) && p1 && p2
9187 && compare_tree_int (len, strlen (p1) + 1) <= 0
9188 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9190 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9193 return integer_one_node;
9195 return integer_minus_one_node;
9197 return integer_zero_node;
9200 /* If len parameter is one, return an expression corresponding to
9201 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9202 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9204 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9205 tree cst_uchar_ptr_node
9206 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9208 tree ind1 = fold_convert (integer_type_node,
9209 build1 (INDIRECT_REF, cst_uchar_node,
9210 fold_convert (cst_uchar_ptr_node,
9212 tree ind2 = fold_convert (integer_type_node,
9213 build1 (INDIRECT_REF, cst_uchar_node,
9214 fold_convert (cst_uchar_ptr_node,
9216 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9222 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9223 Return NULL_TREE if no simplification can be made. */
9226 fold_builtin_strcmp (tree arg1, tree arg2)
9228 const char *p1, *p2;
9230 if (!validate_arg (arg1, POINTER_TYPE)
9231 || !validate_arg (arg2, POINTER_TYPE))
9234 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9235 if (operand_equal_p (arg1, arg2, 0))
9236 return integer_zero_node;
9238 p1 = c_getstr (arg1);
9239 p2 = c_getstr (arg2);
9243 const int i = strcmp (p1, p2);
9245 return integer_minus_one_node;
9247 return integer_one_node;
9249 return integer_zero_node;
9252 /* If the second arg is "", return *(const unsigned char*)arg1. */
9253 if (p2 && *p2 == '\0')
9255 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9256 tree cst_uchar_ptr_node
9257 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9259 return fold_convert (integer_type_node,
9260 build1 (INDIRECT_REF, cst_uchar_node,
9261 fold_convert (cst_uchar_ptr_node,
9265 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9266 if (p1 && *p1 == '\0')
9268 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9269 tree cst_uchar_ptr_node
9270 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9272 tree temp = fold_convert (integer_type_node,
9273 build1 (INDIRECT_REF, cst_uchar_node,
9274 fold_convert (cst_uchar_ptr_node,
9276 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9282 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9283 Return NULL_TREE if no simplification can be made. */
9286 fold_builtin_strncmp (tree arg1, tree arg2, tree len)
9288 const char *p1, *p2;
9290 if (!validate_arg (arg1, POINTER_TYPE)
9291 || !validate_arg (arg2, POINTER_TYPE)
9292 || !validate_arg (len, INTEGER_TYPE))
9295 /* If the LEN parameter is zero, return zero. */
9296 if (integer_zerop (len))
9297 return omit_two_operands (integer_type_node, integer_zero_node,
9300 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9301 if (operand_equal_p (arg1, arg2, 0))
9302 return omit_one_operand (integer_type_node, integer_zero_node, len);
9304 p1 = c_getstr (arg1);
9305 p2 = c_getstr (arg2);
9307 if (host_integerp (len, 1) && p1 && p2)
9309 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9311 return integer_one_node;
9313 return integer_minus_one_node;
9315 return integer_zero_node;
9318 /* If the second arg is "", and the length is greater than zero,
9319 return *(const unsigned char*)arg1. */
9320 if (p2 && *p2 == '\0'
9321 && TREE_CODE (len) == INTEGER_CST
9322 && tree_int_cst_sgn (len) == 1)
9324 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9325 tree cst_uchar_ptr_node
9326 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9328 return fold_convert (integer_type_node,
9329 build1 (INDIRECT_REF, cst_uchar_node,
9330 fold_convert (cst_uchar_ptr_node,
9334 /* If the first arg is "", and the length is greater than zero,
9335 return -*(const unsigned char*)arg2. */
9336 if (p1 && *p1 == '\0'
9337 && TREE_CODE (len) == INTEGER_CST
9338 && tree_int_cst_sgn (len) == 1)
9340 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9341 tree cst_uchar_ptr_node
9342 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9344 tree temp = fold_convert (integer_type_node,
9345 build1 (INDIRECT_REF, cst_uchar_node,
9346 fold_convert (cst_uchar_ptr_node,
9348 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9351 /* If len parameter is one, return an expression corresponding to
9352 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9353 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9355 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9356 tree cst_uchar_ptr_node
9357 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9359 tree ind1 = fold_convert (integer_type_node,
9360 build1 (INDIRECT_REF, cst_uchar_node,
9361 fold_convert (cst_uchar_ptr_node,
9363 tree ind2 = fold_convert (integer_type_node,
9364 build1 (INDIRECT_REF, cst_uchar_node,
9365 fold_convert (cst_uchar_ptr_node,
9367 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9373 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9374 ARG. Return NULL_TREE if no simplification can be made. */
9377 fold_builtin_signbit (tree arg, tree type)
9381 if (!validate_arg (arg, REAL_TYPE))
9384 /* If ARG is a compile-time constant, determine the result. */
9385 if (TREE_CODE (arg) == REAL_CST
9386 && !TREE_OVERFLOW (arg))
9390 c = TREE_REAL_CST (arg);
9391 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9392 return fold_convert (type, temp);
9395 /* If ARG is non-negative, the result is always zero. */
9396 if (tree_expr_nonnegative_p (arg))
9397 return omit_one_operand (type, integer_zero_node, arg);
9399 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9400 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9401 return fold_build2 (LT_EXPR, type, arg,
9402 build_real (TREE_TYPE (arg), dconst0));
9407 /* Fold function call to builtin copysign, copysignf or copysignl with
9408 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9412 fold_builtin_copysign (tree fndecl, tree arg1, tree arg2, tree type)
9416 if (!validate_arg (arg1, REAL_TYPE)
9417 || !validate_arg (arg2, REAL_TYPE))
9420 /* copysign(X,X) is X. */
9421 if (operand_equal_p (arg1, arg2, 0))
9422 return fold_convert (type, arg1);
9424 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9425 if (TREE_CODE (arg1) == REAL_CST
9426 && TREE_CODE (arg2) == REAL_CST
9427 && !TREE_OVERFLOW (arg1)
9428 && !TREE_OVERFLOW (arg2))
9430 REAL_VALUE_TYPE c1, c2;
9432 c1 = TREE_REAL_CST (arg1);
9433 c2 = TREE_REAL_CST (arg2);
9434 /* c1.sign := c2.sign. */
9435 real_copysign (&c1, &c2);
9436 return build_real (type, c1);
9439 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9440 Remember to evaluate Y for side-effects. */
9441 if (tree_expr_nonnegative_p (arg2))
9442 return omit_one_operand (type,
9443 fold_build1 (ABS_EXPR, type, arg1),
9446 /* Strip sign changing operations for the first argument. */
9447 tem = fold_strip_sign_ops (arg1);
9449 return build_call_expr (fndecl, 2, tem, arg2);
9454 /* Fold a call to builtin isascii with argument ARG. */
9457 fold_builtin_isascii (tree arg)
9459 if (!validate_arg (arg, INTEGER_TYPE))
9463 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9464 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
9465 build_int_cst (NULL_TREE,
9466 ~ (unsigned HOST_WIDE_INT) 0x7f));
9467 return fold_build2 (EQ_EXPR, integer_type_node,
9468 arg, integer_zero_node);
9472 /* Fold a call to builtin toascii with argument ARG. */
9475 fold_builtin_toascii (tree arg)
9477 if (!validate_arg (arg, INTEGER_TYPE))
9480 /* Transform toascii(c) -> (c & 0x7f). */
9481 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9482 build_int_cst (NULL_TREE, 0x7f));
9485 /* Fold a call to builtin isdigit with argument ARG. */
9488 fold_builtin_isdigit (tree arg)
9490 if (!validate_arg (arg, INTEGER_TYPE))
9494 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9495 /* According to the C standard, isdigit is unaffected by locale.
9496 However, it definitely is affected by the target character set. */
9497 unsigned HOST_WIDE_INT target_digit0
9498 = lang_hooks.to_target_charset ('0');
9500 if (target_digit0 == 0)
9503 arg = fold_convert (unsigned_type_node, arg);
9504 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
9505 build_int_cst (unsigned_type_node, target_digit0));
9506 return fold_build2 (LE_EXPR, integer_type_node, arg,
9507 build_int_cst (unsigned_type_node, 9));
9511 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9514 fold_builtin_fabs (tree arg, tree type)
9516 if (!validate_arg (arg, REAL_TYPE))
9519 arg = fold_convert (type, arg);
9520 if (TREE_CODE (arg) == REAL_CST)
9521 return fold_abs_const (arg, type);
9522 return fold_build1 (ABS_EXPR, type, arg);
9525 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9528 fold_builtin_abs (tree arg, tree type)
9530 if (!validate_arg (arg, INTEGER_TYPE))
9533 arg = fold_convert (type, arg);
9534 if (TREE_CODE (arg) == INTEGER_CST)
9535 return fold_abs_const (arg, type);
9536 return fold_build1 (ABS_EXPR, type, arg);
9539 /* Fold a call to builtin fmin or fmax. */
9542 fold_builtin_fmin_fmax (tree arg0, tree arg1, tree type, bool max)
9544 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9546 /* Calculate the result when the argument is a constant. */
9547 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9552 /* If either argument is NaN, return the other one. Avoid the
9553 transformation if we get (and honor) a signalling NaN. Using
9554 omit_one_operand() ensures we create a non-lvalue. */
9555 if (TREE_CODE (arg0) == REAL_CST
9556 && real_isnan (&TREE_REAL_CST (arg0))
9557 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9558 || ! TREE_REAL_CST (arg0).signalling))
9559 return omit_one_operand (type, arg1, arg0);
9560 if (TREE_CODE (arg1) == REAL_CST
9561 && real_isnan (&TREE_REAL_CST (arg1))
9562 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9563 || ! TREE_REAL_CST (arg1).signalling))
9564 return omit_one_operand (type, arg0, arg1);
9566 /* Transform fmin/fmax(x,x) -> x. */
9567 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9568 return omit_one_operand (type, arg0, arg1);
9570 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9571 functions to return the numeric arg if the other one is NaN.
9572 These tree codes don't honor that, so only transform if
9573 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9574 handled, so we don't have to worry about it either. */
9575 if (flag_finite_math_only)
9576 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9577 fold_convert (type, arg0),
9578 fold_convert (type, arg1));
9583 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9586 fold_builtin_carg (tree arg, tree type)
9588 if (validate_arg (arg, COMPLEX_TYPE))
9590 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9594 tree new_arg = builtin_save_expr (arg);
9595 tree r_arg = fold_build1 (REALPART_EXPR, type, new_arg);
9596 tree i_arg = fold_build1 (IMAGPART_EXPR, type, new_arg);
9597 return build_call_expr (atan2_fn, 2, i_arg, r_arg);
9604 /* Fold a call to builtin logb/ilogb. */
9607 fold_builtin_logb (tree arg, tree rettype)
9609 if (! validate_arg (arg, REAL_TYPE))
9614 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9616 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9622 /* If arg is Inf or NaN and we're logb, return it. */
9623 if (TREE_CODE (rettype) == REAL_TYPE)
9624 return fold_convert (rettype, arg);
9625 /* Fall through... */
9627 /* Zero may set errno and/or raise an exception for logb, also
9628 for ilogb we don't know FP_ILOGB0. */
9631 /* For normal numbers, proceed iff radix == 2. In GCC,
9632 normalized significands are in the range [0.5, 1.0). We
9633 want the exponent as if they were [1.0, 2.0) so get the
9634 exponent and subtract 1. */
9635 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9636 return fold_convert (rettype, build_int_cst (NULL_TREE,
9637 REAL_EXP (value)-1));
9645 /* Fold a call to builtin significand, if radix == 2. */
9648 fold_builtin_significand (tree arg, tree rettype)
9650 if (! validate_arg (arg, REAL_TYPE))
9655 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9657 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9664 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9665 return fold_convert (rettype, arg);
9667 /* For normal numbers, proceed iff radix == 2. */
9668 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9670 REAL_VALUE_TYPE result = *value;
9671 /* In GCC, normalized significands are in the range [0.5,
9672 1.0). We want them to be [1.0, 2.0) so set the
9674 SET_REAL_EXP (&result, 1);
9675 return build_real (rettype, result);
9684 /* Fold a call to builtin frexp, we can assume the base is 2. */
9687 fold_builtin_frexp (tree arg0, tree arg1, tree rettype)
9689 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9694 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9697 arg1 = build_fold_indirect_ref (arg1);
9699 /* Proceed if a valid pointer type was passed in. */
9700 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9702 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9708 /* For +-0, return (*exp = 0, +-0). */
9709 exp = integer_zero_node;
9714 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9715 return omit_one_operand (rettype, arg0, arg1);
9718 /* Since the frexp function always expects base 2, and in
9719 GCC normalized significands are already in the range
9720 [0.5, 1.0), we have exactly what frexp wants. */
9721 REAL_VALUE_TYPE frac_rvt = *value;
9722 SET_REAL_EXP (&frac_rvt, 0);
9723 frac = build_real (rettype, frac_rvt);
9724 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9731 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9732 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, exp);
9733 TREE_SIDE_EFFECTS (arg1) = 1;
9734 return fold_build2 (COMPOUND_EXPR, rettype, arg1, frac);
9740 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9741 then we can assume the base is two. If it's false, then we have to
9742 check the mode of the TYPE parameter in certain cases. */
9745 fold_builtin_load_exponent (tree arg0, tree arg1, tree type, bool ldexp)
9747 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9752 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9753 if (real_zerop (arg0) || integer_zerop (arg1)
9754 || (TREE_CODE (arg0) == REAL_CST
9755 && !real_isfinite (&TREE_REAL_CST (arg0))))
9756 return omit_one_operand (type, arg0, arg1);
9758 /* If both arguments are constant, then try to evaluate it. */
9759 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9760 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9761 && host_integerp (arg1, 0))
9763 /* Bound the maximum adjustment to twice the range of the
9764 mode's valid exponents. Use abs to ensure the range is
9765 positive as a sanity check. */
9766 const long max_exp_adj = 2 *
9767 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9768 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9770 /* Get the user-requested adjustment. */
9771 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9773 /* The requested adjustment must be inside this range. This
9774 is a preliminary cap to avoid things like overflow, we
9775 may still fail to compute the result for other reasons. */
9776 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9778 REAL_VALUE_TYPE initial_result;
9780 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9782 /* Ensure we didn't overflow. */
9783 if (! real_isinf (&initial_result))
9785 const REAL_VALUE_TYPE trunc_result
9786 = real_value_truncate (TYPE_MODE (type), initial_result);
9788 /* Only proceed if the target mode can hold the
9790 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9791 return build_real (type, trunc_result);
9800 /* Fold a call to builtin modf. */
9803 fold_builtin_modf (tree arg0, tree arg1, tree rettype)
9805 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9810 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9813 arg1 = build_fold_indirect_ref (arg1);
9815 /* Proceed if a valid pointer type was passed in. */
9816 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9818 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9819 REAL_VALUE_TYPE trunc, frac;
9825 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9826 trunc = frac = *value;
9829 /* For +-Inf, return (*arg1 = arg0, +-0). */
9831 frac.sign = value->sign;
9835 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9836 real_trunc (&trunc, VOIDmode, value);
9837 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9838 /* If the original number was negative and already
9839 integral, then the fractional part is -0.0. */
9840 if (value->sign && frac.cl == rvc_zero)
9841 frac.sign = value->sign;
9845 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9846 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1,
9847 build_real (rettype, trunc));
9848 TREE_SIDE_EFFECTS (arg1) = 1;
9849 return fold_build2 (COMPOUND_EXPR, rettype, arg1,
9850 build_real (rettype, frac));
9856 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9857 ARG is the argument for the call. */
9860 fold_builtin_classify (tree fndecl, tree arg, int builtin_index)
9862 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9865 if (!validate_arg (arg, REAL_TYPE))
9868 switch (builtin_index)
9870 case BUILT_IN_ISINF:
9871 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9872 return omit_one_operand (type, integer_zero_node, arg);
9874 if (TREE_CODE (arg) == REAL_CST)
9876 r = TREE_REAL_CST (arg);
9877 if (real_isinf (&r))
9878 return real_compare (GT_EXPR, &r, &dconst0)
9879 ? integer_one_node : integer_minus_one_node;
9881 return integer_zero_node;
9886 case BUILT_IN_ISINF_SIGN:
9888 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9889 /* In a boolean context, GCC will fold the inner COND_EXPR to
9890 1. So e.g. "if (isinf_sign(x))" would be folded to just
9891 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9892 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9893 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9894 tree tmp = NULL_TREE;
9896 arg = builtin_save_expr (arg);
9898 if (signbit_fn && isinf_fn)
9900 tree signbit_call = build_call_expr (signbit_fn, 1, arg);
9901 tree isinf_call = build_call_expr (isinf_fn, 1, arg);
9903 signbit_call = fold_build2 (NE_EXPR, integer_type_node,
9904 signbit_call, integer_zero_node);
9905 isinf_call = fold_build2 (NE_EXPR, integer_type_node,
9906 isinf_call, integer_zero_node);
9908 tmp = fold_build3 (COND_EXPR, integer_type_node, signbit_call,
9909 integer_minus_one_node, integer_one_node);
9910 tmp = fold_build3 (COND_EXPR, integer_type_node, isinf_call, tmp,
9917 case BUILT_IN_ISFINITE:
9918 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9919 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9920 return omit_one_operand (type, integer_one_node, arg);
9922 if (TREE_CODE (arg) == REAL_CST)
9924 r = TREE_REAL_CST (arg);
9925 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9930 case BUILT_IN_ISNAN:
9931 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9932 return omit_one_operand (type, integer_zero_node, arg);
9934 if (TREE_CODE (arg) == REAL_CST)
9936 r = TREE_REAL_CST (arg);
9937 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9940 arg = builtin_save_expr (arg);
9941 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
9948 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9949 This builtin will generate code to return the appropriate floating
9950 point classification depending on the value of the floating point
9951 number passed in. The possible return values must be supplied as
9952 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9953 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9954 one floating point argument which is "type generic". */
9957 fold_builtin_fpclassify (tree exp)
9959 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9960 arg, type, res, tmp;
9961 enum machine_mode mode;
9965 /* Verify the required arguments in the original call. */
9966 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9967 INTEGER_TYPE, INTEGER_TYPE,
9968 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9971 fp_nan = CALL_EXPR_ARG (exp, 0);
9972 fp_infinite = CALL_EXPR_ARG (exp, 1);
9973 fp_normal = CALL_EXPR_ARG (exp, 2);
9974 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9975 fp_zero = CALL_EXPR_ARG (exp, 4);
9976 arg = CALL_EXPR_ARG (exp, 5);
9977 type = TREE_TYPE (arg);
9978 mode = TYPE_MODE (type);
9979 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
9983 (fabs(x) == Inf ? FP_INFINITE :
9984 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9985 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9987 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
9988 build_real (type, dconst0));
9989 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_zero, fp_subnormal);
9991 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9992 real_from_string (&r, buf);
9993 tmp = fold_build2 (GE_EXPR, integer_type_node, arg, build_real (type, r));
9994 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_normal, res);
9996 if (HONOR_INFINITIES (mode))
9999 tmp = fold_build2 (EQ_EXPR, integer_type_node, arg,
10000 build_real (type, r));
10001 res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_infinite, res);
10004 if (HONOR_NANS (mode))
10006 tmp = fold_build2 (ORDERED_EXPR, integer_type_node, arg, arg);
10007 res = fold_build3 (COND_EXPR, integer_type_node, tmp, res, fp_nan);
10013 /* Fold a call to an unordered comparison function such as
10014 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10015 being called and ARG0 and ARG1 are the arguments for the call.
10016 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10017 the opposite of the desired result. UNORDERED_CODE is used
10018 for modes that can hold NaNs and ORDERED_CODE is used for
10022 fold_builtin_unordered_cmp (tree fndecl, tree arg0, tree arg1,
10023 enum tree_code unordered_code,
10024 enum tree_code ordered_code)
10026 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10027 enum tree_code code;
10029 enum tree_code code0, code1;
10030 tree cmp_type = NULL_TREE;
10032 type0 = TREE_TYPE (arg0);
10033 type1 = TREE_TYPE (arg1);
10035 code0 = TREE_CODE (type0);
10036 code1 = TREE_CODE (type1);
10038 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10039 /* Choose the wider of two real types. */
10040 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10042 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10044 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10047 arg0 = fold_convert (cmp_type, arg0);
10048 arg1 = fold_convert (cmp_type, arg1);
10050 if (unordered_code == UNORDERED_EXPR)
10052 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10053 return omit_two_operands (type, integer_zero_node, arg0, arg1);
10054 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
10057 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10059 return fold_build1 (TRUTH_NOT_EXPR, type,
10060 fold_build2 (code, type, arg0, arg1));
10063 /* Fold a call to built-in function FNDECL with 0 arguments.
10064 IGNORE is true if the result of the function call is ignored. This
10065 function returns NULL_TREE if no simplification was possible. */
10068 fold_builtin_0 (tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10070 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10071 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10074 CASE_FLT_FN (BUILT_IN_INF):
10075 case BUILT_IN_INFD32:
10076 case BUILT_IN_INFD64:
10077 case BUILT_IN_INFD128:
10078 return fold_builtin_inf (type, true);
10080 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10081 return fold_builtin_inf (type, false);
10083 case BUILT_IN_CLASSIFY_TYPE:
10084 return fold_builtin_classify_type (NULL_TREE);
10092 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10093 IGNORE is true if the result of the function call is ignored. This
10094 function returns NULL_TREE if no simplification was possible. */
10097 fold_builtin_1 (tree fndecl, tree arg0, bool ignore)
10099 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10100 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10104 case BUILT_IN_CONSTANT_P:
10106 tree val = fold_builtin_constant_p (arg0);
10108 /* Gimplification will pull the CALL_EXPR for the builtin out of
10109 an if condition. When not optimizing, we'll not CSE it back.
10110 To avoid link error types of regressions, return false now. */
10111 if (!val && !optimize)
10112 val = integer_zero_node;
10117 case BUILT_IN_CLASSIFY_TYPE:
10118 return fold_builtin_classify_type (arg0);
10120 case BUILT_IN_STRLEN:
10121 return fold_builtin_strlen (arg0);
10123 CASE_FLT_FN (BUILT_IN_FABS):
10124 return fold_builtin_fabs (arg0, type);
10127 case BUILT_IN_LABS:
10128 case BUILT_IN_LLABS:
10129 case BUILT_IN_IMAXABS:
10130 return fold_builtin_abs (arg0, type);
10132 CASE_FLT_FN (BUILT_IN_CONJ):
10133 if (validate_arg (arg0, COMPLEX_TYPE))
10134 return fold_build1 (CONJ_EXPR, type, arg0);
10137 CASE_FLT_FN (BUILT_IN_CREAL):
10138 if (validate_arg (arg0, COMPLEX_TYPE))
10139 return non_lvalue (fold_build1 (REALPART_EXPR, type, arg0));;
10142 CASE_FLT_FN (BUILT_IN_CIMAG):
10143 if (validate_arg (arg0, COMPLEX_TYPE))
10144 return non_lvalue (fold_build1 (IMAGPART_EXPR, type, arg0));
10147 CASE_FLT_FN (BUILT_IN_CCOS):
10148 CASE_FLT_FN (BUILT_IN_CCOSH):
10149 /* These functions are "even", i.e. f(x) == f(-x). */
10150 if (validate_arg (arg0, COMPLEX_TYPE))
10152 tree narg = fold_strip_sign_ops (arg0);
10154 return build_call_expr (fndecl, 1, narg);
10158 CASE_FLT_FN (BUILT_IN_CABS):
10159 return fold_builtin_cabs (arg0, type, fndecl);
10161 CASE_FLT_FN (BUILT_IN_CARG):
10162 return fold_builtin_carg (arg0, type);
10164 CASE_FLT_FN (BUILT_IN_SQRT):
10165 return fold_builtin_sqrt (arg0, type);
10167 CASE_FLT_FN (BUILT_IN_CBRT):
10168 return fold_builtin_cbrt (arg0, type);
10170 CASE_FLT_FN (BUILT_IN_ASIN):
10171 if (validate_arg (arg0, REAL_TYPE))
10172 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10173 &dconstm1, &dconst1, true);
10176 CASE_FLT_FN (BUILT_IN_ACOS):
10177 if (validate_arg (arg0, REAL_TYPE))
10178 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10179 &dconstm1, &dconst1, true);
10182 CASE_FLT_FN (BUILT_IN_ATAN):
10183 if (validate_arg (arg0, REAL_TYPE))
10184 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10187 CASE_FLT_FN (BUILT_IN_ASINH):
10188 if (validate_arg (arg0, REAL_TYPE))
10189 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10192 CASE_FLT_FN (BUILT_IN_ACOSH):
10193 if (validate_arg (arg0, REAL_TYPE))
10194 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10195 &dconst1, NULL, true);
10198 CASE_FLT_FN (BUILT_IN_ATANH):
10199 if (validate_arg (arg0, REAL_TYPE))
10200 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10201 &dconstm1, &dconst1, false);
10204 CASE_FLT_FN (BUILT_IN_SIN):
10205 if (validate_arg (arg0, REAL_TYPE))
10206 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10209 CASE_FLT_FN (BUILT_IN_COS):
10210 return fold_builtin_cos (arg0, type, fndecl);
10213 CASE_FLT_FN (BUILT_IN_TAN):
10214 return fold_builtin_tan (arg0, type);
10216 CASE_FLT_FN (BUILT_IN_CEXP):
10217 return fold_builtin_cexp (arg0, type);
10219 CASE_FLT_FN (BUILT_IN_CEXPI):
10220 if (validate_arg (arg0, REAL_TYPE))
10221 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10224 CASE_FLT_FN (BUILT_IN_SINH):
10225 if (validate_arg (arg0, REAL_TYPE))
10226 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10229 CASE_FLT_FN (BUILT_IN_COSH):
10230 return fold_builtin_cosh (arg0, type, fndecl);
10232 CASE_FLT_FN (BUILT_IN_TANH):
10233 if (validate_arg (arg0, REAL_TYPE))
10234 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10237 CASE_FLT_FN (BUILT_IN_ERF):
10238 if (validate_arg (arg0, REAL_TYPE))
10239 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10242 CASE_FLT_FN (BUILT_IN_ERFC):
10243 if (validate_arg (arg0, REAL_TYPE))
10244 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10247 CASE_FLT_FN (BUILT_IN_TGAMMA):
10248 if (validate_arg (arg0, REAL_TYPE))
10249 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10252 CASE_FLT_FN (BUILT_IN_EXP):
10253 return fold_builtin_exponent (fndecl, arg0, mpfr_exp);
10255 CASE_FLT_FN (BUILT_IN_EXP2):
10256 return fold_builtin_exponent (fndecl, arg0, mpfr_exp2);
10258 CASE_FLT_FN (BUILT_IN_EXP10):
10259 CASE_FLT_FN (BUILT_IN_POW10):
10260 return fold_builtin_exponent (fndecl, arg0, mpfr_exp10);
10262 CASE_FLT_FN (BUILT_IN_EXPM1):
10263 if (validate_arg (arg0, REAL_TYPE))
10264 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10267 CASE_FLT_FN (BUILT_IN_LOG):
10268 return fold_builtin_logarithm (fndecl, arg0, mpfr_log);
10270 CASE_FLT_FN (BUILT_IN_LOG2):
10271 return fold_builtin_logarithm (fndecl, arg0, mpfr_log2);
10273 CASE_FLT_FN (BUILT_IN_LOG10):
10274 return fold_builtin_logarithm (fndecl, arg0, mpfr_log10);
10276 CASE_FLT_FN (BUILT_IN_LOG1P):
10277 if (validate_arg (arg0, REAL_TYPE))
10278 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10279 &dconstm1, NULL, false);
10282 CASE_FLT_FN (BUILT_IN_J0):
10283 if (validate_arg (arg0, REAL_TYPE))
10284 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10288 CASE_FLT_FN (BUILT_IN_J1):
10289 if (validate_arg (arg0, REAL_TYPE))
10290 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10294 CASE_FLT_FN (BUILT_IN_Y0):
10295 if (validate_arg (arg0, REAL_TYPE))
10296 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10297 &dconst0, NULL, false);
10300 CASE_FLT_FN (BUILT_IN_Y1):
10301 if (validate_arg (arg0, REAL_TYPE))
10302 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10303 &dconst0, NULL, false);
10306 CASE_FLT_FN (BUILT_IN_NAN):
10307 case BUILT_IN_NAND32:
10308 case BUILT_IN_NAND64:
10309 case BUILT_IN_NAND128:
10310 return fold_builtin_nan (arg0, type, true);
10312 CASE_FLT_FN (BUILT_IN_NANS):
10313 return fold_builtin_nan (arg0, type, false);
10315 CASE_FLT_FN (BUILT_IN_FLOOR):
10316 return fold_builtin_floor (fndecl, arg0);
10318 CASE_FLT_FN (BUILT_IN_CEIL):
10319 return fold_builtin_ceil (fndecl, arg0);
10321 CASE_FLT_FN (BUILT_IN_TRUNC):
10322 return fold_builtin_trunc (fndecl, arg0);
10324 CASE_FLT_FN (BUILT_IN_ROUND):
10325 return fold_builtin_round (fndecl, arg0);
10327 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10328 CASE_FLT_FN (BUILT_IN_RINT):
10329 return fold_trunc_transparent_mathfn (fndecl, arg0);
10331 CASE_FLT_FN (BUILT_IN_LCEIL):
10332 CASE_FLT_FN (BUILT_IN_LLCEIL):
10333 CASE_FLT_FN (BUILT_IN_LFLOOR):
10334 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10335 CASE_FLT_FN (BUILT_IN_LROUND):
10336 CASE_FLT_FN (BUILT_IN_LLROUND):
10337 return fold_builtin_int_roundingfn (fndecl, arg0);
10339 CASE_FLT_FN (BUILT_IN_LRINT):
10340 CASE_FLT_FN (BUILT_IN_LLRINT):
10341 return fold_fixed_mathfn (fndecl, arg0);
10343 case BUILT_IN_BSWAP32:
10344 case BUILT_IN_BSWAP64:
10345 return fold_builtin_bswap (fndecl, arg0);
10347 CASE_INT_FN (BUILT_IN_FFS):
10348 CASE_INT_FN (BUILT_IN_CLZ):
10349 CASE_INT_FN (BUILT_IN_CTZ):
10350 CASE_INT_FN (BUILT_IN_POPCOUNT):
10351 CASE_INT_FN (BUILT_IN_PARITY):
10352 return fold_builtin_bitop (fndecl, arg0);
10354 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10355 return fold_builtin_signbit (arg0, type);
10357 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10358 return fold_builtin_significand (arg0, type);
10360 CASE_FLT_FN (BUILT_IN_ILOGB):
10361 CASE_FLT_FN (BUILT_IN_LOGB):
10362 return fold_builtin_logb (arg0, type);
10364 case BUILT_IN_ISASCII:
10365 return fold_builtin_isascii (arg0);
10367 case BUILT_IN_TOASCII:
10368 return fold_builtin_toascii (arg0);
10370 case BUILT_IN_ISDIGIT:
10371 return fold_builtin_isdigit (arg0);
10373 CASE_FLT_FN (BUILT_IN_FINITE):
10374 case BUILT_IN_FINITED32:
10375 case BUILT_IN_FINITED64:
10376 case BUILT_IN_FINITED128:
10377 case BUILT_IN_ISFINITE:
10378 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISFINITE);
10380 CASE_FLT_FN (BUILT_IN_ISINF):
10381 case BUILT_IN_ISINFD32:
10382 case BUILT_IN_ISINFD64:
10383 case BUILT_IN_ISINFD128:
10384 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF);
10386 case BUILT_IN_ISINF_SIGN:
10387 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF_SIGN);
10389 CASE_FLT_FN (BUILT_IN_ISNAN):
10390 case BUILT_IN_ISNAND32:
10391 case BUILT_IN_ISNAND64:
10392 case BUILT_IN_ISNAND128:
10393 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISNAN);
10395 case BUILT_IN_PRINTF:
10396 case BUILT_IN_PRINTF_UNLOCKED:
10397 case BUILT_IN_VPRINTF:
10398 return fold_builtin_printf (fndecl, arg0, NULL_TREE, ignore, fcode);
10408 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10409 IGNORE is true if the result of the function call is ignored. This
10410 function returns NULL_TREE if no simplification was possible. */
10413 fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore)
10415 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10416 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10420 CASE_FLT_FN (BUILT_IN_JN):
10421 if (validate_arg (arg0, INTEGER_TYPE)
10422 && validate_arg (arg1, REAL_TYPE))
10423 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10426 CASE_FLT_FN (BUILT_IN_YN):
10427 if (validate_arg (arg0, INTEGER_TYPE)
10428 && validate_arg (arg1, REAL_TYPE))
10429 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10433 CASE_FLT_FN (BUILT_IN_DREM):
10434 CASE_FLT_FN (BUILT_IN_REMAINDER):
10435 if (validate_arg (arg0, REAL_TYPE)
10436 && validate_arg(arg1, REAL_TYPE))
10437 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10440 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10441 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10442 if (validate_arg (arg0, REAL_TYPE)
10443 && validate_arg(arg1, POINTER_TYPE))
10444 return do_mpfr_lgamma_r (arg0, arg1, type);
10447 CASE_FLT_FN (BUILT_IN_ATAN2):
10448 if (validate_arg (arg0, REAL_TYPE)
10449 && validate_arg(arg1, REAL_TYPE))
10450 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10453 CASE_FLT_FN (BUILT_IN_FDIM):
10454 if (validate_arg (arg0, REAL_TYPE)
10455 && validate_arg(arg1, REAL_TYPE))
10456 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10459 CASE_FLT_FN (BUILT_IN_HYPOT):
10460 return fold_builtin_hypot (fndecl, arg0, arg1, type);
10462 CASE_FLT_FN (BUILT_IN_LDEXP):
10463 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/true);
10464 CASE_FLT_FN (BUILT_IN_SCALBN):
10465 CASE_FLT_FN (BUILT_IN_SCALBLN):
10466 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/false);
10468 CASE_FLT_FN (BUILT_IN_FREXP):
10469 return fold_builtin_frexp (arg0, arg1, type);
10471 CASE_FLT_FN (BUILT_IN_MODF):
10472 return fold_builtin_modf (arg0, arg1, type);
10474 case BUILT_IN_BZERO:
10475 return fold_builtin_bzero (arg0, arg1, ignore);
10477 case BUILT_IN_FPUTS:
10478 return fold_builtin_fputs (arg0, arg1, ignore, false, NULL_TREE);
10480 case BUILT_IN_FPUTS_UNLOCKED:
10481 return fold_builtin_fputs (arg0, arg1, ignore, true, NULL_TREE);
10483 case BUILT_IN_STRSTR:
10484 return fold_builtin_strstr (arg0, arg1, type);
10486 case BUILT_IN_STRCAT:
10487 return fold_builtin_strcat (arg0, arg1);
10489 case BUILT_IN_STRSPN:
10490 return fold_builtin_strspn (arg0, arg1);
10492 case BUILT_IN_STRCSPN:
10493 return fold_builtin_strcspn (arg0, arg1);
10495 case BUILT_IN_STRCHR:
10496 case BUILT_IN_INDEX:
10497 return fold_builtin_strchr (arg0, arg1, type);
10499 case BUILT_IN_STRRCHR:
10500 case BUILT_IN_RINDEX:
10501 return fold_builtin_strrchr (arg0, arg1, type);
10503 case BUILT_IN_STRCPY:
10504 return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE);
10506 case BUILT_IN_STPCPY:
10509 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10513 return build_call_expr (fn, 2, arg0, arg1);
10517 case BUILT_IN_STRCMP:
10518 return fold_builtin_strcmp (arg0, arg1);
10520 case BUILT_IN_STRPBRK:
10521 return fold_builtin_strpbrk (arg0, arg1, type);
10523 case BUILT_IN_EXPECT:
10524 return fold_builtin_expect (arg0, arg1);
10526 CASE_FLT_FN (BUILT_IN_POW):
10527 return fold_builtin_pow (fndecl, arg0, arg1, type);
10529 CASE_FLT_FN (BUILT_IN_POWI):
10530 return fold_builtin_powi (fndecl, arg0, arg1, type);
10532 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10533 return fold_builtin_copysign (fndecl, arg0, arg1, type);
10535 CASE_FLT_FN (BUILT_IN_FMIN):
10536 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/false);
10538 CASE_FLT_FN (BUILT_IN_FMAX):
10539 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/true);
10541 case BUILT_IN_ISGREATER:
10542 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLE_EXPR, LE_EXPR);
10543 case BUILT_IN_ISGREATEREQUAL:
10544 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLT_EXPR, LT_EXPR);
10545 case BUILT_IN_ISLESS:
10546 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGE_EXPR, GE_EXPR);
10547 case BUILT_IN_ISLESSEQUAL:
10548 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGT_EXPR, GT_EXPR);
10549 case BUILT_IN_ISLESSGREATER:
10550 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10551 case BUILT_IN_ISUNORDERED:
10552 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNORDERED_EXPR,
10555 /* We do the folding for va_start in the expander. */
10556 case BUILT_IN_VA_START:
10559 case BUILT_IN_SPRINTF:
10560 return fold_builtin_sprintf (arg0, arg1, NULL_TREE, ignore);
10562 case BUILT_IN_OBJECT_SIZE:
10563 return fold_builtin_object_size (arg0, arg1);
10565 case BUILT_IN_PRINTF:
10566 case BUILT_IN_PRINTF_UNLOCKED:
10567 case BUILT_IN_VPRINTF:
10568 return fold_builtin_printf (fndecl, arg0, arg1, ignore, fcode);
10570 case BUILT_IN_PRINTF_CHK:
10571 case BUILT_IN_VPRINTF_CHK:
10572 if (!validate_arg (arg0, INTEGER_TYPE)
10573 || TREE_SIDE_EFFECTS (arg0))
10576 return fold_builtin_printf (fndecl, arg1, NULL_TREE, ignore, fcode);
10579 case BUILT_IN_FPRINTF:
10580 case BUILT_IN_FPRINTF_UNLOCKED:
10581 case BUILT_IN_VFPRINTF:
10582 return fold_builtin_fprintf (fndecl, arg0, arg1, NULL_TREE,
10591 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10592 and ARG2. IGNORE is true if the result of the function call is ignored.
10593 This function returns NULL_TREE if no simplification was possible. */
10596 fold_builtin_3 (tree fndecl, tree arg0, tree arg1, tree arg2, bool ignore)
10598 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10599 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10603 CASE_FLT_FN (BUILT_IN_SINCOS):
10604 return fold_builtin_sincos (arg0, arg1, arg2);
10606 CASE_FLT_FN (BUILT_IN_FMA):
10607 if (validate_arg (arg0, REAL_TYPE)
10608 && validate_arg(arg1, REAL_TYPE)
10609 && validate_arg(arg2, REAL_TYPE))
10610 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10613 CASE_FLT_FN (BUILT_IN_REMQUO):
10614 if (validate_arg (arg0, REAL_TYPE)
10615 && validate_arg(arg1, REAL_TYPE)
10616 && validate_arg(arg2, POINTER_TYPE))
10617 return do_mpfr_remquo (arg0, arg1, arg2);
10620 case BUILT_IN_MEMSET:
10621 return fold_builtin_memset (arg0, arg1, arg2, type, ignore);
10623 case BUILT_IN_BCOPY:
10624 return fold_builtin_memory_op (arg1, arg0, arg2, void_type_node, true, /*endp=*/3);
10626 case BUILT_IN_MEMCPY:
10627 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/0);
10629 case BUILT_IN_MEMPCPY:
10630 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/1);
10632 case BUILT_IN_MEMMOVE:
10633 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/3);
10635 case BUILT_IN_STRNCAT:
10636 return fold_builtin_strncat (arg0, arg1, arg2);
10638 case BUILT_IN_STRNCPY:
10639 return fold_builtin_strncpy (fndecl, arg0, arg1, arg2, NULL_TREE);
10641 case BUILT_IN_STRNCMP:
10642 return fold_builtin_strncmp (arg0, arg1, arg2);
10644 case BUILT_IN_MEMCHR:
10645 return fold_builtin_memchr (arg0, arg1, arg2, type);
10647 case BUILT_IN_BCMP:
10648 case BUILT_IN_MEMCMP:
10649 return fold_builtin_memcmp (arg0, arg1, arg2);;
10651 case BUILT_IN_SPRINTF:
10652 return fold_builtin_sprintf (arg0, arg1, arg2, ignore);
10654 case BUILT_IN_STRCPY_CHK:
10655 case BUILT_IN_STPCPY_CHK:
10656 return fold_builtin_stxcpy_chk (fndecl, arg0, arg1, arg2, NULL_TREE,
10659 case BUILT_IN_STRCAT_CHK:
10660 return fold_builtin_strcat_chk (fndecl, arg0, arg1, arg2);
10662 case BUILT_IN_PRINTF_CHK:
10663 case BUILT_IN_VPRINTF_CHK:
10664 if (!validate_arg (arg0, INTEGER_TYPE)
10665 || TREE_SIDE_EFFECTS (arg0))
10668 return fold_builtin_printf (fndecl, arg1, arg2, ignore, fcode);
10671 case BUILT_IN_FPRINTF:
10672 case BUILT_IN_FPRINTF_UNLOCKED:
10673 case BUILT_IN_VFPRINTF:
10674 return fold_builtin_fprintf (fndecl, arg0, arg1, arg2, ignore, fcode);
10676 case BUILT_IN_FPRINTF_CHK:
10677 case BUILT_IN_VFPRINTF_CHK:
10678 if (!validate_arg (arg1, INTEGER_TYPE)
10679 || TREE_SIDE_EFFECTS (arg1))
10682 return fold_builtin_fprintf (fndecl, arg0, arg2, NULL_TREE,
10691 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10692 ARG2, and ARG3. IGNORE is true if the result of the function call is
10693 ignored. This function returns NULL_TREE if no simplification was
10697 fold_builtin_4 (tree fndecl, tree arg0, tree arg1, tree arg2, tree arg3,
10700 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10704 case BUILT_IN_MEMCPY_CHK:
10705 case BUILT_IN_MEMPCPY_CHK:
10706 case BUILT_IN_MEMMOVE_CHK:
10707 case BUILT_IN_MEMSET_CHK:
10708 return fold_builtin_memory_chk (fndecl, arg0, arg1, arg2, arg3,
10710 DECL_FUNCTION_CODE (fndecl));
10712 case BUILT_IN_STRNCPY_CHK:
10713 return fold_builtin_strncpy_chk (arg0, arg1, arg2, arg3, NULL_TREE);
10715 case BUILT_IN_STRNCAT_CHK:
10716 return fold_builtin_strncat_chk (fndecl, arg0, arg1, arg2, arg3);
10718 case BUILT_IN_FPRINTF_CHK:
10719 case BUILT_IN_VFPRINTF_CHK:
10720 if (!validate_arg (arg1, INTEGER_TYPE)
10721 || TREE_SIDE_EFFECTS (arg1))
10724 return fold_builtin_fprintf (fndecl, arg0, arg2, arg3,
10734 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10735 arguments, where NARGS <= 4. IGNORE is true if the result of the
10736 function call is ignored. This function returns NULL_TREE if no
10737 simplification was possible. Note that this only folds builtins with
10738 fixed argument patterns. Foldings that do varargs-to-varargs
10739 transformations, or that match calls with more than 4 arguments,
10740 need to be handled with fold_builtin_varargs instead. */
10742 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10745 fold_builtin_n (tree fndecl, tree *args, int nargs, bool ignore)
10747 tree ret = NULL_TREE;
10752 ret = fold_builtin_0 (fndecl, ignore);
10755 ret = fold_builtin_1 (fndecl, args[0], ignore);
10758 ret = fold_builtin_2 (fndecl, args[0], args[1], ignore);
10761 ret = fold_builtin_3 (fndecl, args[0], args[1], args[2], ignore);
10764 ret = fold_builtin_4 (fndecl, args[0], args[1], args[2], args[3],
10772 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10773 TREE_NO_WARNING (ret) = 1;
10779 /* Builtins with folding operations that operate on "..." arguments
10780 need special handling; we need to store the arguments in a convenient
10781 data structure before attempting any folding. Fortunately there are
10782 only a few builtins that fall into this category. FNDECL is the
10783 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10784 result of the function call is ignored. */
10787 fold_builtin_varargs (tree fndecl, tree exp, bool ignore ATTRIBUTE_UNUSED)
10789 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10790 tree ret = NULL_TREE;
10794 case BUILT_IN_SPRINTF_CHK:
10795 case BUILT_IN_VSPRINTF_CHK:
10796 ret = fold_builtin_sprintf_chk (exp, fcode);
10799 case BUILT_IN_SNPRINTF_CHK:
10800 case BUILT_IN_VSNPRINTF_CHK:
10801 ret = fold_builtin_snprintf_chk (exp, NULL_TREE, fcode);
10804 case BUILT_IN_FPCLASSIFY:
10805 ret = fold_builtin_fpclassify (exp);
10813 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10814 TREE_NO_WARNING (ret) = 1;
10820 /* Return true if FNDECL shouldn't be folded right now.
10821 If a built-in function has an inline attribute always_inline
10822 wrapper, defer folding it after always_inline functions have
10823 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10824 might not be performed. */
10827 avoid_folding_inline_builtin (tree fndecl)
10829 return (DECL_DECLARED_INLINE_P (fndecl)
10830 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10832 && !cfun->always_inline_functions_inlined
10833 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10836 /* A wrapper function for builtin folding that prevents warnings for
10837 "statement without effect" and the like, caused by removing the
10838 call node earlier than the warning is generated. */
10841 fold_call_expr (tree exp, bool ignore)
10843 tree ret = NULL_TREE;
10844 tree fndecl = get_callee_fndecl (exp);
10846 && TREE_CODE (fndecl) == FUNCTION_DECL
10847 && DECL_BUILT_IN (fndecl)
10848 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10849 yet. Defer folding until we see all the arguments
10850 (after inlining). */
10851 && !CALL_EXPR_VA_ARG_PACK (exp))
10853 int nargs = call_expr_nargs (exp);
10855 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10856 instead last argument is __builtin_va_arg_pack (). Defer folding
10857 even in that case, until arguments are finalized. */
10858 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10860 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10862 && TREE_CODE (fndecl2) == FUNCTION_DECL
10863 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10864 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10868 if (avoid_folding_inline_builtin (fndecl))
10871 /* FIXME: Don't use a list in this interface. */
10872 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10873 return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore);
10876 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10878 tree *args = CALL_EXPR_ARGP (exp);
10879 ret = fold_builtin_n (fndecl, args, nargs, ignore);
10882 ret = fold_builtin_varargs (fndecl, exp, ignore);
10885 /* Propagate location information from original call to
10886 expansion of builtin. Otherwise things like
10887 maybe_emit_chk_warning, that operate on the expansion
10888 of a builtin, will use the wrong location information. */
10889 if (CAN_HAVE_LOCATION_P (exp) && EXPR_HAS_LOCATION (exp))
10891 tree realret = ret;
10892 if (TREE_CODE (ret) == NOP_EXPR)
10893 realret = TREE_OPERAND (ret, 0);
10894 if (CAN_HAVE_LOCATION_P (realret)
10895 && !EXPR_HAS_LOCATION (realret))
10896 SET_EXPR_LOCATION (realret, EXPR_LOCATION (exp));
10906 /* Conveniently construct a function call expression. FNDECL names the
10907 function to be called and ARGLIST is a TREE_LIST of arguments. */
10910 build_function_call_expr (tree fndecl, tree arglist)
10912 tree fntype = TREE_TYPE (fndecl);
10913 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10914 int n = list_length (arglist);
10915 tree *argarray = (tree *) alloca (n * sizeof (tree));
10918 for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist))
10919 argarray[i] = TREE_VALUE (arglist);
10920 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
10923 /* Conveniently construct a function call expression. FNDECL names the
10924 function to be called, N is the number of arguments, and the "..."
10925 parameters are the argument expressions. */
10928 build_call_expr (tree fndecl, int n, ...)
10931 tree fntype = TREE_TYPE (fndecl);
10932 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10933 tree *argarray = (tree *) alloca (n * sizeof (tree));
10937 for (i = 0; i < n; i++)
10938 argarray[i] = va_arg (ap, tree);
10940 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
10943 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10944 N arguments are passed in the array ARGARRAY. */
10947 fold_builtin_call_array (tree type,
10952 tree ret = NULL_TREE;
10956 if (TREE_CODE (fn) == ADDR_EXPR)
10958 tree fndecl = TREE_OPERAND (fn, 0);
10959 if (TREE_CODE (fndecl) == FUNCTION_DECL
10960 && DECL_BUILT_IN (fndecl))
10962 /* If last argument is __builtin_va_arg_pack (), arguments to this
10963 function are not finalized yet. Defer folding until they are. */
10964 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10966 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10968 && TREE_CODE (fndecl2) == FUNCTION_DECL
10969 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10970 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10971 return build_call_array (type, fn, n, argarray);
10973 if (avoid_folding_inline_builtin (fndecl))
10974 return build_call_array (type, fn, n, argarray);
10975 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10977 tree arglist = NULL_TREE;
10978 for (i = n - 1; i >= 0; i--)
10979 arglist = tree_cons (NULL_TREE, argarray[i], arglist);
10980 ret = targetm.fold_builtin (fndecl, arglist, false);
10983 return build_call_array (type, fn, n, argarray);
10985 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10987 /* First try the transformations that don't require consing up
10989 ret = fold_builtin_n (fndecl, argarray, n, false);
10994 /* If we got this far, we need to build an exp. */
10995 exp = build_call_array (type, fn, n, argarray);
10996 ret = fold_builtin_varargs (fndecl, exp, false);
10997 return ret ? ret : exp;
11001 return build_call_array (type, fn, n, argarray);
11004 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11005 along with N new arguments specified as the "..." parameters. SKIP
11006 is the number of arguments in EXP to be omitted. This function is used
11007 to do varargs-to-varargs transformations. */
11010 rewrite_call_expr (tree exp, int skip, tree fndecl, int n, ...)
11012 int oldnargs = call_expr_nargs (exp);
11013 int nargs = oldnargs - skip + n;
11014 tree fntype = TREE_TYPE (fndecl);
11015 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11023 buffer = XALLOCAVEC (tree, nargs);
11025 for (i = 0; i < n; i++)
11026 buffer[i] = va_arg (ap, tree);
11028 for (j = skip; j < oldnargs; j++, i++)
11029 buffer[i] = CALL_EXPR_ARG (exp, j);
11032 buffer = CALL_EXPR_ARGP (exp) + skip;
11034 return fold (build_call_array (TREE_TYPE (exp), fn, nargs, buffer));
11037 /* Validate a single argument ARG against a tree code CODE representing
11041 validate_arg (const_tree arg, enum tree_code code)
11045 else if (code == POINTER_TYPE)
11046 return POINTER_TYPE_P (TREE_TYPE (arg));
11047 else if (code == INTEGER_TYPE)
11048 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11049 return code == TREE_CODE (TREE_TYPE (arg));
11052 /* This function validates the types of a function call argument list
11053 against a specified list of tree_codes. If the last specifier is a 0,
11054 that represents an ellipses, otherwise the last specifier must be a
11057 This is the GIMPLE version of validate_arglist. Eventually we want to
11058 completely convert builtins.c to work from GIMPLEs and the tree based
11059 validate_arglist will then be removed. */
11062 validate_gimple_arglist (const_gimple call, ...)
11064 enum tree_code code;
11070 va_start (ap, call);
11075 code = va_arg (ap, enum tree_code);
11079 /* This signifies an ellipses, any further arguments are all ok. */
11083 /* This signifies an endlink, if no arguments remain, return
11084 true, otherwise return false. */
11085 res = (i == gimple_call_num_args (call));
11088 /* If no parameters remain or the parameter's code does not
11089 match the specified code, return false. Otherwise continue
11090 checking any remaining arguments. */
11091 arg = gimple_call_arg (call, i++);
11092 if (!validate_arg (arg, code))
11099 /* We need gotos here since we can only have one VA_CLOSE in a
11107 /* This function validates the types of a function call argument list
11108 against a specified list of tree_codes. If the last specifier is a 0,
11109 that represents an ellipses, otherwise the last specifier must be a
11113 validate_arglist (const_tree callexpr, ...)
11115 enum tree_code code;
11118 const_call_expr_arg_iterator iter;
11121 va_start (ap, callexpr);
11122 init_const_call_expr_arg_iterator (callexpr, &iter);
11126 code = va_arg (ap, enum tree_code);
11130 /* This signifies an ellipses, any further arguments are all ok. */
11134 /* This signifies an endlink, if no arguments remain, return
11135 true, otherwise return false. */
11136 res = !more_const_call_expr_args_p (&iter);
11139 /* If no parameters remain or the parameter's code does not
11140 match the specified code, return false. Otherwise continue
11141 checking any remaining arguments. */
11142 arg = next_const_call_expr_arg (&iter);
11143 if (!validate_arg (arg, code))
11150 /* We need gotos here since we can only have one VA_CLOSE in a
11158 /* Default target-specific builtin expander that does nothing. */
11161 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11162 rtx target ATTRIBUTE_UNUSED,
11163 rtx subtarget ATTRIBUTE_UNUSED,
11164 enum machine_mode mode ATTRIBUTE_UNUSED,
11165 int ignore ATTRIBUTE_UNUSED)
11170 /* Returns true is EXP represents data that would potentially reside
11171 in a readonly section. */
11174 readonly_data_expr (tree exp)
11178 if (TREE_CODE (exp) != ADDR_EXPR)
11181 exp = get_base_address (TREE_OPERAND (exp, 0));
11185 /* Make sure we call decl_readonly_section only for trees it
11186 can handle (since it returns true for everything it doesn't
11188 if (TREE_CODE (exp) == STRING_CST
11189 || TREE_CODE (exp) == CONSTRUCTOR
11190 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11191 return decl_readonly_section (exp, 0);
11196 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11197 to the call, and TYPE is its return type.
11199 Return NULL_TREE if no simplification was possible, otherwise return the
11200 simplified form of the call as a tree.
11202 The simplified form may be a constant or other expression which
11203 computes the same value, but in a more efficient manner (including
11204 calls to other builtin functions).
11206 The call may contain arguments which need to be evaluated, but
11207 which are not useful to determine the result of the call. In
11208 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11209 COMPOUND_EXPR will be an argument which must be evaluated.
11210 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11211 COMPOUND_EXPR in the chain will contain the tree for the simplified
11212 form of the builtin function call. */
11215 fold_builtin_strstr (tree s1, tree s2, tree type)
11217 if (!validate_arg (s1, POINTER_TYPE)
11218 || !validate_arg (s2, POINTER_TYPE))
11223 const char *p1, *p2;
11225 p2 = c_getstr (s2);
11229 p1 = c_getstr (s1);
11232 const char *r = strstr (p1, p2);
11236 return build_int_cst (TREE_TYPE (s1), 0);
11238 /* Return an offset into the constant string argument. */
11239 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11240 s1, size_int (r - p1));
11241 return fold_convert (type, tem);
11244 /* The argument is const char *, and the result is char *, so we need
11245 a type conversion here to avoid a warning. */
11247 return fold_convert (type, s1);
11252 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11256 /* New argument list transforming strstr(s1, s2) to
11257 strchr(s1, s2[0]). */
11258 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11262 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11263 the call, and TYPE is its return type.
11265 Return NULL_TREE if no simplification was possible, otherwise return the
11266 simplified form of the call as a tree.
11268 The simplified form may be a constant or other expression which
11269 computes the same value, but in a more efficient manner (including
11270 calls to other builtin functions).
11272 The call may contain arguments which need to be evaluated, but
11273 which are not useful to determine the result of the call. In
11274 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11275 COMPOUND_EXPR will be an argument which must be evaluated.
11276 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11277 COMPOUND_EXPR in the chain will contain the tree for the simplified
11278 form of the builtin function call. */
11281 fold_builtin_strchr (tree s1, tree s2, tree type)
11283 if (!validate_arg (s1, POINTER_TYPE)
11284 || !validate_arg (s2, INTEGER_TYPE))
11290 if (TREE_CODE (s2) != INTEGER_CST)
11293 p1 = c_getstr (s1);
11300 if (target_char_cast (s2, &c))
11303 r = strchr (p1, c);
11306 return build_int_cst (TREE_TYPE (s1), 0);
11308 /* Return an offset into the constant string argument. */
11309 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11310 s1, size_int (r - p1));
11311 return fold_convert (type, tem);
11317 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11318 the call, and TYPE is its return type.
11320 Return NULL_TREE if no simplification was possible, otherwise return the
11321 simplified form of the call as a tree.
11323 The simplified form may be a constant or other expression which
11324 computes the same value, but in a more efficient manner (including
11325 calls to other builtin functions).
11327 The call may contain arguments which need to be evaluated, but
11328 which are not useful to determine the result of the call. In
11329 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11330 COMPOUND_EXPR will be an argument which must be evaluated.
11331 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11332 COMPOUND_EXPR in the chain will contain the tree for the simplified
11333 form of the builtin function call. */
11336 fold_builtin_strrchr (tree s1, tree s2, tree type)
11338 if (!validate_arg (s1, POINTER_TYPE)
11339 || !validate_arg (s2, INTEGER_TYPE))
11346 if (TREE_CODE (s2) != INTEGER_CST)
11349 p1 = c_getstr (s1);
11356 if (target_char_cast (s2, &c))
11359 r = strrchr (p1, c);
11362 return build_int_cst (TREE_TYPE (s1), 0);
11364 /* Return an offset into the constant string argument. */
11365 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11366 s1, size_int (r - p1));
11367 return fold_convert (type, tem);
11370 if (! integer_zerop (s2))
11373 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11377 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11378 return build_call_expr (fn, 2, s1, s2);
11382 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11383 to the call, and TYPE is its return type.
11385 Return NULL_TREE if no simplification was possible, otherwise return the
11386 simplified form of the call as a tree.
11388 The simplified form may be a constant or other expression which
11389 computes the same value, but in a more efficient manner (including
11390 calls to other builtin functions).
11392 The call may contain arguments which need to be evaluated, but
11393 which are not useful to determine the result of the call. In
11394 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11395 COMPOUND_EXPR will be an argument which must be evaluated.
11396 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11397 COMPOUND_EXPR in the chain will contain the tree for the simplified
11398 form of the builtin function call. */
11401 fold_builtin_strpbrk (tree s1, tree s2, tree type)
11403 if (!validate_arg (s1, POINTER_TYPE)
11404 || !validate_arg (s2, POINTER_TYPE))
11409 const char *p1, *p2;
11411 p2 = c_getstr (s2);
11415 p1 = c_getstr (s1);
11418 const char *r = strpbrk (p1, p2);
11422 return build_int_cst (TREE_TYPE (s1), 0);
11424 /* Return an offset into the constant string argument. */
11425 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11426 s1, size_int (r - p1));
11427 return fold_convert (type, tem);
11431 /* strpbrk(x, "") == NULL.
11432 Evaluate and ignore s1 in case it had side-effects. */
11433 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
11436 return NULL_TREE; /* Really call strpbrk. */
11438 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11442 /* New argument list transforming strpbrk(s1, s2) to
11443 strchr(s1, s2[0]). */
11444 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11448 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11451 Return NULL_TREE if no simplification was possible, otherwise return the
11452 simplified form of the call as a tree.
11454 The simplified form may be a constant or other expression which
11455 computes the same value, but in a more efficient manner (including
11456 calls to other builtin functions).
11458 The call may contain arguments which need to be evaluated, but
11459 which are not useful to determine the result of the call. In
11460 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11461 COMPOUND_EXPR will be an argument which must be evaluated.
11462 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11463 COMPOUND_EXPR in the chain will contain the tree for the simplified
11464 form of the builtin function call. */
11467 fold_builtin_strcat (tree dst, tree src)
11469 if (!validate_arg (dst, POINTER_TYPE)
11470 || !validate_arg (src, POINTER_TYPE))
11474 const char *p = c_getstr (src);
11476 /* If the string length is zero, return the dst parameter. */
11477 if (p && *p == '\0')
11484 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11485 arguments to the call.
11487 Return NULL_TREE if no simplification was possible, otherwise return the
11488 simplified form of the call as a tree.
11490 The simplified form may be a constant or other expression which
11491 computes the same value, but in a more efficient manner (including
11492 calls to other builtin functions).
11494 The call may contain arguments which need to be evaluated, but
11495 which are not useful to determine the result of the call. In
11496 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11497 COMPOUND_EXPR will be an argument which must be evaluated.
11498 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11499 COMPOUND_EXPR in the chain will contain the tree for the simplified
11500 form of the builtin function call. */
11503 fold_builtin_strncat (tree dst, tree src, tree len)
11505 if (!validate_arg (dst, POINTER_TYPE)
11506 || !validate_arg (src, POINTER_TYPE)
11507 || !validate_arg (len, INTEGER_TYPE))
11511 const char *p = c_getstr (src);
11513 /* If the requested length is zero, or the src parameter string
11514 length is zero, return the dst parameter. */
11515 if (integer_zerop (len) || (p && *p == '\0'))
11516 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
11518 /* If the requested len is greater than or equal to the string
11519 length, call strcat. */
11520 if (TREE_CODE (len) == INTEGER_CST && p
11521 && compare_tree_int (len, strlen (p)) >= 0)
11523 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11525 /* If the replacement _DECL isn't initialized, don't do the
11530 return build_call_expr (fn, 2, dst, src);
11536 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11539 Return NULL_TREE if no simplification was possible, otherwise return the
11540 simplified form of the call as a tree.
11542 The simplified form may be a constant or other expression which
11543 computes the same value, but in a more efficient manner (including
11544 calls to other builtin functions).
11546 The call may contain arguments which need to be evaluated, but
11547 which are not useful to determine the result of the call. In
11548 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11549 COMPOUND_EXPR will be an argument which must be evaluated.
11550 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11551 COMPOUND_EXPR in the chain will contain the tree for the simplified
11552 form of the builtin function call. */
11555 fold_builtin_strspn (tree s1, tree s2)
11557 if (!validate_arg (s1, POINTER_TYPE)
11558 || !validate_arg (s2, POINTER_TYPE))
11562 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11564 /* If both arguments are constants, evaluate at compile-time. */
11567 const size_t r = strspn (p1, p2);
11568 return size_int (r);
11571 /* If either argument is "", return NULL_TREE. */
11572 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11573 /* Evaluate and ignore both arguments in case either one has
11575 return omit_two_operands (size_type_node, size_zero_node,
11581 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11584 Return NULL_TREE if no simplification was possible, otherwise return the
11585 simplified form of the call as a tree.
11587 The simplified form may be a constant or other expression which
11588 computes the same value, but in a more efficient manner (including
11589 calls to other builtin functions).
11591 The call may contain arguments which need to be evaluated, but
11592 which are not useful to determine the result of the call. In
11593 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11594 COMPOUND_EXPR will be an argument which must be evaluated.
11595 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11596 COMPOUND_EXPR in the chain will contain the tree for the simplified
11597 form of the builtin function call. */
11600 fold_builtin_strcspn (tree s1, tree s2)
11602 if (!validate_arg (s1, POINTER_TYPE)
11603 || !validate_arg (s2, POINTER_TYPE))
11607 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11609 /* If both arguments are constants, evaluate at compile-time. */
11612 const size_t r = strcspn (p1, p2);
11613 return size_int (r);
11616 /* If the first argument is "", return NULL_TREE. */
11617 if (p1 && *p1 == '\0')
11619 /* Evaluate and ignore argument s2 in case it has
11621 return omit_one_operand (size_type_node,
11622 size_zero_node, s2);
11625 /* If the second argument is "", return __builtin_strlen(s1). */
11626 if (p2 && *p2 == '\0')
11628 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11630 /* If the replacement _DECL isn't initialized, don't do the
11635 return build_call_expr (fn, 1, s1);
11641 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11642 to the call. IGNORE is true if the value returned
11643 by the builtin will be ignored. UNLOCKED is true is true if this
11644 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11645 the known length of the string. Return NULL_TREE if no simplification
11649 fold_builtin_fputs (tree arg0, tree arg1, bool ignore, bool unlocked, tree len)
11651 /* If we're using an unlocked function, assume the other unlocked
11652 functions exist explicitly. */
11653 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11654 : implicit_built_in_decls[BUILT_IN_FPUTC];
11655 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11656 : implicit_built_in_decls[BUILT_IN_FWRITE];
11658 /* If the return value is used, don't do the transformation. */
11662 /* Verify the arguments in the original call. */
11663 if (!validate_arg (arg0, POINTER_TYPE)
11664 || !validate_arg (arg1, POINTER_TYPE))
11668 len = c_strlen (arg0, 0);
11670 /* Get the length of the string passed to fputs. If the length
11671 can't be determined, punt. */
11673 || TREE_CODE (len) != INTEGER_CST)
11676 switch (compare_tree_int (len, 1))
11678 case -1: /* length is 0, delete the call entirely . */
11679 return omit_one_operand (integer_type_node, integer_zero_node, arg1);;
11681 case 0: /* length is 1, call fputc. */
11683 const char *p = c_getstr (arg0);
11688 return build_call_expr (fn_fputc, 2,
11689 build_int_cst (NULL_TREE, p[0]), arg1);
11695 case 1: /* length is greater than 1, call fwrite. */
11697 /* If optimizing for size keep fputs. */
11698 if (optimize_function_for_size_p (cfun))
11700 /* New argument list transforming fputs(string, stream) to
11701 fwrite(string, 1, len, stream). */
11703 return build_call_expr (fn_fwrite, 4, arg0, size_one_node, len, arg1);
11708 gcc_unreachable ();
11713 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11714 produced. False otherwise. This is done so that we don't output the error
11715 or warning twice or three times. */
11718 fold_builtin_next_arg (tree exp, bool va_start_p)
11720 tree fntype = TREE_TYPE (current_function_decl);
11721 int nargs = call_expr_nargs (exp);
11724 if (TYPE_ARG_TYPES (fntype) == 0
11725 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11726 == void_type_node))
11728 error ("%<va_start%> used in function with fixed args");
11734 if (va_start_p && (nargs != 2))
11736 error ("wrong number of arguments to function %<va_start%>");
11739 arg = CALL_EXPR_ARG (exp, 1);
11741 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11742 when we checked the arguments and if needed issued a warning. */
11747 /* Evidently an out of date version of <stdarg.h>; can't validate
11748 va_start's second argument, but can still work as intended. */
11749 warning (0, "%<__builtin_next_arg%> called without an argument");
11752 else if (nargs > 1)
11754 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11757 arg = CALL_EXPR_ARG (exp, 0);
11760 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11761 or __builtin_next_arg (0) the first time we see it, after checking
11762 the arguments and if needed issuing a warning. */
11763 if (!integer_zerop (arg))
11765 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11767 /* Strip off all nops for the sake of the comparison. This
11768 is not quite the same as STRIP_NOPS. It does more.
11769 We must also strip off INDIRECT_EXPR for C++ reference
11771 while (CONVERT_EXPR_P (arg)
11772 || TREE_CODE (arg) == INDIRECT_REF)
11773 arg = TREE_OPERAND (arg, 0);
11774 if (arg != last_parm)
11776 /* FIXME: Sometimes with the tree optimizers we can get the
11777 not the last argument even though the user used the last
11778 argument. We just warn and set the arg to be the last
11779 argument so that we will get wrong-code because of
11781 warning (0, "second parameter of %<va_start%> not last named argument");
11784 /* Undefined by C99 7.15.1.4p4 (va_start):
11785 "If the parameter parmN is declared with the register storage
11786 class, with a function or array type, or with a type that is
11787 not compatible with the type that results after application of
11788 the default argument promotions, the behavior is undefined."
11790 else if (DECL_REGISTER (arg))
11791 warning (0, "undefined behaviour when second parameter of "
11792 "%<va_start%> is declared with %<register%> storage");
11794 /* We want to verify the second parameter just once before the tree
11795 optimizers are run and then avoid keeping it in the tree,
11796 as otherwise we could warn even for correct code like:
11797 void foo (int i, ...)
11798 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11800 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11802 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11808 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11809 ORIG may be null if this is a 2-argument call. We don't attempt to
11810 simplify calls with more than 3 arguments.
11812 Return NULL_TREE if no simplification was possible, otherwise return the
11813 simplified form of the call as a tree. If IGNORED is true, it means that
11814 the caller does not use the returned value of the function. */
11817 fold_builtin_sprintf (tree dest, tree fmt, tree orig, int ignored)
11820 const char *fmt_str = NULL;
11822 /* Verify the required arguments in the original call. We deal with two
11823 types of sprintf() calls: 'sprintf (str, fmt)' and
11824 'sprintf (dest, "%s", orig)'. */
11825 if (!validate_arg (dest, POINTER_TYPE)
11826 || !validate_arg (fmt, POINTER_TYPE))
11828 if (orig && !validate_arg (orig, POINTER_TYPE))
11831 /* Check whether the format is a literal string constant. */
11832 fmt_str = c_getstr (fmt);
11833 if (fmt_str == NULL)
11837 retval = NULL_TREE;
11839 if (!init_target_chars ())
11842 /* If the format doesn't contain % args or %%, use strcpy. */
11843 if (strchr (fmt_str, target_percent) == NULL)
11845 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11850 /* Don't optimize sprintf (buf, "abc", ptr++). */
11854 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11855 'format' is known to contain no % formats. */
11856 call = build_call_expr (fn, 2, dest, fmt);
11858 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11861 /* If the format is "%s", use strcpy if the result isn't used. */
11862 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11865 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11870 /* Don't crash on sprintf (str1, "%s"). */
11874 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11877 retval = c_strlen (orig, 1);
11878 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11881 call = build_call_expr (fn, 2, dest, orig);
11884 if (call && retval)
11886 retval = fold_convert
11887 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11889 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11895 /* Expand a call EXP to __builtin_object_size. */
11898 expand_builtin_object_size (tree exp)
11901 int object_size_type;
11902 tree fndecl = get_callee_fndecl (exp);
11904 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11906 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11908 expand_builtin_trap ();
11912 ost = CALL_EXPR_ARG (exp, 1);
11915 if (TREE_CODE (ost) != INTEGER_CST
11916 || tree_int_cst_sgn (ost) < 0
11917 || compare_tree_int (ost, 3) > 0)
11919 error ("%Klast argument of %D is not integer constant between 0 and 3",
11921 expand_builtin_trap ();
11925 object_size_type = tree_low_cst (ost, 0);
11927 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11930 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11931 FCODE is the BUILT_IN_* to use.
11932 Return NULL_RTX if we failed; the caller should emit a normal call,
11933 otherwise try to get the result in TARGET, if convenient (and in
11934 mode MODE if that's convenient). */
11937 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11938 enum built_in_function fcode)
11940 tree dest, src, len, size;
11942 if (!validate_arglist (exp,
11944 fcode == BUILT_IN_MEMSET_CHK
11945 ? INTEGER_TYPE : POINTER_TYPE,
11946 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11949 dest = CALL_EXPR_ARG (exp, 0);
11950 src = CALL_EXPR_ARG (exp, 1);
11951 len = CALL_EXPR_ARG (exp, 2);
11952 size = CALL_EXPR_ARG (exp, 3);
11954 if (! host_integerp (size, 1))
11957 if (host_integerp (len, 1) || integer_all_onesp (size))
11961 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11963 warning (0, "%Kcall to %D will always overflow destination buffer",
11964 exp, get_callee_fndecl (exp));
11969 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11970 mem{cpy,pcpy,move,set} is available. */
11973 case BUILT_IN_MEMCPY_CHK:
11974 fn = built_in_decls[BUILT_IN_MEMCPY];
11976 case BUILT_IN_MEMPCPY_CHK:
11977 fn = built_in_decls[BUILT_IN_MEMPCPY];
11979 case BUILT_IN_MEMMOVE_CHK:
11980 fn = built_in_decls[BUILT_IN_MEMMOVE];
11982 case BUILT_IN_MEMSET_CHK:
11983 fn = built_in_decls[BUILT_IN_MEMSET];
11992 fn = build_call_expr (fn, 3, dest, src, len);
11993 STRIP_TYPE_NOPS (fn);
11994 while (TREE_CODE (fn) == COMPOUND_EXPR)
11996 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
11998 fn = TREE_OPERAND (fn, 1);
12000 if (TREE_CODE (fn) == CALL_EXPR)
12001 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12002 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12004 else if (fcode == BUILT_IN_MEMSET_CHK)
12008 unsigned int dest_align
12009 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12011 /* If DEST is not a pointer type, call the normal function. */
12012 if (dest_align == 0)
12015 /* If SRC and DEST are the same (and not volatile), do nothing. */
12016 if (operand_equal_p (src, dest, 0))
12020 if (fcode != BUILT_IN_MEMPCPY_CHK)
12022 /* Evaluate and ignore LEN in case it has side-effects. */
12023 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12024 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12027 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12028 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12031 /* __memmove_chk special case. */
12032 if (fcode == BUILT_IN_MEMMOVE_CHK)
12034 unsigned int src_align
12035 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12037 if (src_align == 0)
12040 /* If src is categorized for a readonly section we can use
12041 normal __memcpy_chk. */
12042 if (readonly_data_expr (src))
12044 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12047 fn = build_call_expr (fn, 4, dest, src, len, size);
12048 STRIP_TYPE_NOPS (fn);
12049 while (TREE_CODE (fn) == COMPOUND_EXPR)
12051 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
12053 fn = TREE_OPERAND (fn, 1);
12055 if (TREE_CODE (fn) == CALL_EXPR)
12056 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12057 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12064 /* Emit warning if a buffer overflow is detected at compile time. */
12067 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12074 case BUILT_IN_STRCPY_CHK:
12075 case BUILT_IN_STPCPY_CHK:
12076 /* For __strcat_chk the warning will be emitted only if overflowing
12077 by at least strlen (dest) + 1 bytes. */
12078 case BUILT_IN_STRCAT_CHK:
12079 len = CALL_EXPR_ARG (exp, 1);
12080 size = CALL_EXPR_ARG (exp, 2);
12083 case BUILT_IN_STRNCAT_CHK:
12084 case BUILT_IN_STRNCPY_CHK:
12085 len = CALL_EXPR_ARG (exp, 2);
12086 size = CALL_EXPR_ARG (exp, 3);
12088 case BUILT_IN_SNPRINTF_CHK:
12089 case BUILT_IN_VSNPRINTF_CHK:
12090 len = CALL_EXPR_ARG (exp, 1);
12091 size = CALL_EXPR_ARG (exp, 3);
12094 gcc_unreachable ();
12100 if (! host_integerp (size, 1) || integer_all_onesp (size))
12105 len = c_strlen (len, 1);
12106 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12109 else if (fcode == BUILT_IN_STRNCAT_CHK)
12111 tree src = CALL_EXPR_ARG (exp, 1);
12112 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12114 src = c_strlen (src, 1);
12115 if (! src || ! host_integerp (src, 1))
12117 warning (0, "%Kcall to %D might overflow destination buffer",
12118 exp, get_callee_fndecl (exp));
12121 else if (tree_int_cst_lt (src, size))
12124 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12127 warning (0, "%Kcall to %D will always overflow destination buffer",
12128 exp, get_callee_fndecl (exp));
12131 /* Emit warning if a buffer overflow is detected at compile time
12132 in __sprintf_chk/__vsprintf_chk calls. */
12135 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12137 tree dest, size, len, fmt, flag;
12138 const char *fmt_str;
12139 int nargs = call_expr_nargs (exp);
12141 /* Verify the required arguments in the original call. */
12145 dest = CALL_EXPR_ARG (exp, 0);
12146 flag = CALL_EXPR_ARG (exp, 1);
12147 size = CALL_EXPR_ARG (exp, 2);
12148 fmt = CALL_EXPR_ARG (exp, 3);
12150 if (! host_integerp (size, 1) || integer_all_onesp (size))
12153 /* Check whether the format is a literal string constant. */
12154 fmt_str = c_getstr (fmt);
12155 if (fmt_str == NULL)
12158 if (!init_target_chars ())
12161 /* If the format doesn't contain % args or %%, we know its size. */
12162 if (strchr (fmt_str, target_percent) == 0)
12163 len = build_int_cstu (size_type_node, strlen (fmt_str));
12164 /* If the format is "%s" and first ... argument is a string literal,
12166 else if (fcode == BUILT_IN_SPRINTF_CHK
12167 && strcmp (fmt_str, target_percent_s) == 0)
12173 arg = CALL_EXPR_ARG (exp, 4);
12174 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12177 len = c_strlen (arg, 1);
12178 if (!len || ! host_integerp (len, 1))
12184 if (! tree_int_cst_lt (len, size))
12186 warning (0, "%Kcall to %D will always overflow destination buffer",
12187 exp, get_callee_fndecl (exp));
12191 /* Emit warning if a free is called with address of a variable. */
12194 maybe_emit_free_warning (tree exp)
12196 tree arg = CALL_EXPR_ARG (exp, 0);
12199 if (TREE_CODE (arg) != ADDR_EXPR)
12202 arg = get_base_address (TREE_OPERAND (arg, 0));
12203 if (arg == NULL || INDIRECT_REF_P (arg))
12206 if (SSA_VAR_P (arg))
12207 warning (0, "%Kattempt to free a non-heap object %qD", exp, arg);
12209 warning (0, "%Kattempt to free a non-heap object", exp);
12212 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12216 fold_builtin_object_size (tree ptr, tree ost)
12218 tree ret = NULL_TREE;
12219 int object_size_type;
12221 if (!validate_arg (ptr, POINTER_TYPE)
12222 || !validate_arg (ost, INTEGER_TYPE))
12227 if (TREE_CODE (ost) != INTEGER_CST
12228 || tree_int_cst_sgn (ost) < 0
12229 || compare_tree_int (ost, 3) > 0)
12232 object_size_type = tree_low_cst (ost, 0);
12234 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12235 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12236 and (size_t) 0 for types 2 and 3. */
12237 if (TREE_SIDE_EFFECTS (ptr))
12238 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12240 if (TREE_CODE (ptr) == ADDR_EXPR)
12241 ret = build_int_cstu (size_type_node,
12242 compute_builtin_object_size (ptr, object_size_type));
12244 else if (TREE_CODE (ptr) == SSA_NAME)
12246 unsigned HOST_WIDE_INT bytes;
12248 /* If object size is not known yet, delay folding until
12249 later. Maybe subsequent passes will help determining
12251 bytes = compute_builtin_object_size (ptr, object_size_type);
12252 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
12254 ret = build_int_cstu (size_type_node, bytes);
12259 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
12260 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
12261 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
12268 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12269 DEST, SRC, LEN, and SIZE are the arguments to the call.
12270 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12271 code of the builtin. If MAXLEN is not NULL, it is maximum length
12272 passed as third argument. */
12275 fold_builtin_memory_chk (tree fndecl,
12276 tree dest, tree src, tree len, tree size,
12277 tree maxlen, bool ignore,
12278 enum built_in_function fcode)
12282 if (!validate_arg (dest, POINTER_TYPE)
12283 || !validate_arg (src,
12284 (fcode == BUILT_IN_MEMSET_CHK
12285 ? INTEGER_TYPE : POINTER_TYPE))
12286 || !validate_arg (len, INTEGER_TYPE)
12287 || !validate_arg (size, INTEGER_TYPE))
12290 /* If SRC and DEST are the same (and not volatile), return DEST
12291 (resp. DEST+LEN for __mempcpy_chk). */
12292 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12294 if (fcode != BUILT_IN_MEMPCPY_CHK)
12295 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12298 tree temp = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12299 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
12303 if (! host_integerp (size, 1))
12306 if (! integer_all_onesp (size))
12308 if (! host_integerp (len, 1))
12310 /* If LEN is not constant, try MAXLEN too.
12311 For MAXLEN only allow optimizing into non-_ocs function
12312 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12313 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12315 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12317 /* (void) __mempcpy_chk () can be optimized into
12318 (void) __memcpy_chk (). */
12319 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12323 return build_call_expr (fn, 4, dest, src, len, size);
12331 if (tree_int_cst_lt (size, maxlen))
12336 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12337 mem{cpy,pcpy,move,set} is available. */
12340 case BUILT_IN_MEMCPY_CHK:
12341 fn = built_in_decls[BUILT_IN_MEMCPY];
12343 case BUILT_IN_MEMPCPY_CHK:
12344 fn = built_in_decls[BUILT_IN_MEMPCPY];
12346 case BUILT_IN_MEMMOVE_CHK:
12347 fn = built_in_decls[BUILT_IN_MEMMOVE];
12349 case BUILT_IN_MEMSET_CHK:
12350 fn = built_in_decls[BUILT_IN_MEMSET];
12359 return build_call_expr (fn, 3, dest, src, len);
12362 /* Fold a call to the __st[rp]cpy_chk builtin.
12363 DEST, SRC, and SIZE are the arguments to the call.
12364 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12365 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12366 strings passed as second argument. */
12369 fold_builtin_stxcpy_chk (tree fndecl, tree dest, tree src, tree size,
12370 tree maxlen, bool ignore,
12371 enum built_in_function fcode)
12375 if (!validate_arg (dest, POINTER_TYPE)
12376 || !validate_arg (src, POINTER_TYPE)
12377 || !validate_arg (size, INTEGER_TYPE))
12380 /* If SRC and DEST are the same (and not volatile), return DEST. */
12381 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12382 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
12384 if (! host_integerp (size, 1))
12387 if (! integer_all_onesp (size))
12389 len = c_strlen (src, 1);
12390 if (! len || ! host_integerp (len, 1))
12392 /* If LEN is not constant, try MAXLEN too.
12393 For MAXLEN only allow optimizing into non-_ocs function
12394 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12395 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12397 if (fcode == BUILT_IN_STPCPY_CHK)
12402 /* If return value of __stpcpy_chk is ignored,
12403 optimize into __strcpy_chk. */
12404 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12408 return build_call_expr (fn, 3, dest, src, size);
12411 if (! len || TREE_SIDE_EFFECTS (len))
12414 /* If c_strlen returned something, but not a constant,
12415 transform __strcpy_chk into __memcpy_chk. */
12416 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12420 len = size_binop (PLUS_EXPR, len, ssize_int (1));
12421 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
12422 build_call_expr (fn, 4,
12423 dest, src, len, size));
12429 if (! tree_int_cst_lt (maxlen, size))
12433 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12434 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12435 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12439 return build_call_expr (fn, 2, dest, src);
12442 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12443 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12444 length passed as third argument. */
12447 fold_builtin_strncpy_chk (tree dest, tree src, tree len, tree size,
12452 if (!validate_arg (dest, POINTER_TYPE)
12453 || !validate_arg (src, POINTER_TYPE)
12454 || !validate_arg (len, INTEGER_TYPE)
12455 || !validate_arg (size, INTEGER_TYPE))
12458 if (! host_integerp (size, 1))
12461 if (! integer_all_onesp (size))
12463 if (! host_integerp (len, 1))
12465 /* If LEN is not constant, try MAXLEN too.
12466 For MAXLEN only allow optimizing into non-_ocs function
12467 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12468 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12474 if (tree_int_cst_lt (size, maxlen))
12478 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12479 fn = built_in_decls[BUILT_IN_STRNCPY];
12483 return build_call_expr (fn, 3, dest, src, len);
12486 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12487 are the arguments to the call. */
12490 fold_builtin_strcat_chk (tree fndecl, tree dest, tree src, tree size)
12495 if (!validate_arg (dest, POINTER_TYPE)
12496 || !validate_arg (src, POINTER_TYPE)
12497 || !validate_arg (size, INTEGER_TYPE))
12500 p = c_getstr (src);
12501 /* If the SRC parameter is "", return DEST. */
12502 if (p && *p == '\0')
12503 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12505 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12508 /* If __builtin_strcat_chk is used, assume strcat is available. */
12509 fn = built_in_decls[BUILT_IN_STRCAT];
12513 return build_call_expr (fn, 2, dest, src);
12516 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12520 fold_builtin_strncat_chk (tree fndecl,
12521 tree dest, tree src, tree len, tree size)
12526 if (!validate_arg (dest, POINTER_TYPE)
12527 || !validate_arg (src, POINTER_TYPE)
12528 || !validate_arg (size, INTEGER_TYPE)
12529 || !validate_arg (size, INTEGER_TYPE))
12532 p = c_getstr (src);
12533 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12534 if (p && *p == '\0')
12535 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12536 else if (integer_zerop (len))
12537 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12539 if (! host_integerp (size, 1))
12542 if (! integer_all_onesp (size))
12544 tree src_len = c_strlen (src, 1);
12546 && host_integerp (src_len, 1)
12547 && host_integerp (len, 1)
12548 && ! tree_int_cst_lt (len, src_len))
12550 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12551 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12555 return build_call_expr (fn, 3, dest, src, size);
12560 /* If __builtin_strncat_chk is used, assume strncat is available. */
12561 fn = built_in_decls[BUILT_IN_STRNCAT];
12565 return build_call_expr (fn, 3, dest, src, len);
12568 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12569 a normal call should be emitted rather than expanding the function
12570 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12573 fold_builtin_sprintf_chk (tree exp, enum built_in_function fcode)
12575 tree dest, size, len, fn, fmt, flag;
12576 const char *fmt_str;
12577 int nargs = call_expr_nargs (exp);
12579 /* Verify the required arguments in the original call. */
12582 dest = CALL_EXPR_ARG (exp, 0);
12583 if (!validate_arg (dest, POINTER_TYPE))
12585 flag = CALL_EXPR_ARG (exp, 1);
12586 if (!validate_arg (flag, INTEGER_TYPE))
12588 size = CALL_EXPR_ARG (exp, 2);
12589 if (!validate_arg (size, INTEGER_TYPE))
12591 fmt = CALL_EXPR_ARG (exp, 3);
12592 if (!validate_arg (fmt, POINTER_TYPE))
12595 if (! host_integerp (size, 1))
12600 if (!init_target_chars ())
12603 /* Check whether the format is a literal string constant. */
12604 fmt_str = c_getstr (fmt);
12605 if (fmt_str != NULL)
12607 /* If the format doesn't contain % args or %%, we know the size. */
12608 if (strchr (fmt_str, target_percent) == 0)
12610 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12611 len = build_int_cstu (size_type_node, strlen (fmt_str));
12613 /* If the format is "%s" and first ... argument is a string literal,
12614 we know the size too. */
12615 else if (fcode == BUILT_IN_SPRINTF_CHK
12616 && strcmp (fmt_str, target_percent_s) == 0)
12622 arg = CALL_EXPR_ARG (exp, 4);
12623 if (validate_arg (arg, POINTER_TYPE))
12625 len = c_strlen (arg, 1);
12626 if (! len || ! host_integerp (len, 1))
12633 if (! integer_all_onesp (size))
12635 if (! len || ! tree_int_cst_lt (len, size))
12639 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12640 or if format doesn't contain % chars or is "%s". */
12641 if (! integer_zerop (flag))
12643 if (fmt_str == NULL)
12645 if (strchr (fmt_str, target_percent) != NULL
12646 && strcmp (fmt_str, target_percent_s))
12650 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12651 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12652 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12656 return rewrite_call_expr (exp, 4, fn, 2, dest, fmt);
12659 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12660 a normal call should be emitted rather than expanding the function
12661 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12662 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12663 passed as second argument. */
12666 fold_builtin_snprintf_chk (tree exp, tree maxlen,
12667 enum built_in_function fcode)
12669 tree dest, size, len, fn, fmt, flag;
12670 const char *fmt_str;
12672 /* Verify the required arguments in the original call. */
12673 if (call_expr_nargs (exp) < 5)
12675 dest = CALL_EXPR_ARG (exp, 0);
12676 if (!validate_arg (dest, POINTER_TYPE))
12678 len = CALL_EXPR_ARG (exp, 1);
12679 if (!validate_arg (len, INTEGER_TYPE))
12681 flag = CALL_EXPR_ARG (exp, 2);
12682 if (!validate_arg (flag, INTEGER_TYPE))
12684 size = CALL_EXPR_ARG (exp, 3);
12685 if (!validate_arg (size, INTEGER_TYPE))
12687 fmt = CALL_EXPR_ARG (exp, 4);
12688 if (!validate_arg (fmt, POINTER_TYPE))
12691 if (! host_integerp (size, 1))
12694 if (! integer_all_onesp (size))
12696 if (! host_integerp (len, 1))
12698 /* If LEN is not constant, try MAXLEN too.
12699 For MAXLEN only allow optimizing into non-_ocs function
12700 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12701 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12707 if (tree_int_cst_lt (size, maxlen))
12711 if (!init_target_chars ())
12714 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12715 or if format doesn't contain % chars or is "%s". */
12716 if (! integer_zerop (flag))
12718 fmt_str = c_getstr (fmt);
12719 if (fmt_str == NULL)
12721 if (strchr (fmt_str, target_percent) != NULL
12722 && strcmp (fmt_str, target_percent_s))
12726 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12728 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12729 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12733 return rewrite_call_expr (exp, 5, fn, 3, dest, len, fmt);
12736 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12737 FMT and ARG are the arguments to the call; we don't fold cases with
12738 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12740 Return NULL_TREE if no simplification was possible, otherwise return the
12741 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12742 code of the function to be simplified. */
12745 fold_builtin_printf (tree fndecl, tree fmt, tree arg, bool ignore,
12746 enum built_in_function fcode)
12748 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12749 const char *fmt_str = NULL;
12751 /* If the return value is used, don't do the transformation. */
12755 /* Verify the required arguments in the original call. */
12756 if (!validate_arg (fmt, POINTER_TYPE))
12759 /* Check whether the format is a literal string constant. */
12760 fmt_str = c_getstr (fmt);
12761 if (fmt_str == NULL)
12764 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12766 /* If we're using an unlocked function, assume the other
12767 unlocked functions exist explicitly. */
12768 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12769 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12773 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12774 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12777 if (!init_target_chars ())
12780 if (strcmp (fmt_str, target_percent_s) == 0
12781 || strchr (fmt_str, target_percent) == NULL)
12785 if (strcmp (fmt_str, target_percent_s) == 0)
12787 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12790 if (!arg || !validate_arg (arg, POINTER_TYPE))
12793 str = c_getstr (arg);
12799 /* The format specifier doesn't contain any '%' characters. */
12800 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12806 /* If the string was "", printf does nothing. */
12807 if (str[0] == '\0')
12808 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12810 /* If the string has length of 1, call putchar. */
12811 if (str[1] == '\0')
12813 /* Given printf("c"), (where c is any one character,)
12814 convert "c"[0] to an int and pass that to the replacement
12816 newarg = build_int_cst (NULL_TREE, str[0]);
12818 call = build_call_expr (fn_putchar, 1, newarg);
12822 /* If the string was "string\n", call puts("string"). */
12823 size_t len = strlen (str);
12824 if ((unsigned char)str[len - 1] == target_newline)
12826 /* Create a NUL-terminated string that's one char shorter
12827 than the original, stripping off the trailing '\n'. */
12828 char *newstr = XALLOCAVEC (char, len);
12829 memcpy (newstr, str, len - 1);
12830 newstr[len - 1] = 0;
12832 newarg = build_string_literal (len, newstr);
12834 call = build_call_expr (fn_puts, 1, newarg);
12837 /* We'd like to arrange to call fputs(string,stdout) here,
12838 but we need stdout and don't have a way to get it yet. */
12843 /* The other optimizations can be done only on the non-va_list variants. */
12844 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12847 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12848 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12850 if (!arg || !validate_arg (arg, POINTER_TYPE))
12853 call = build_call_expr (fn_puts, 1, arg);
12856 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12857 else if (strcmp (fmt_str, target_percent_c) == 0)
12859 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12862 call = build_call_expr (fn_putchar, 1, arg);
12868 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
12871 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12872 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12873 more than 3 arguments, and ARG may be null in the 2-argument case.
12875 Return NULL_TREE if no simplification was possible, otherwise return the
12876 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12877 code of the function to be simplified. */
12880 fold_builtin_fprintf (tree fndecl, tree fp, tree fmt, tree arg, bool ignore,
12881 enum built_in_function fcode)
12883 tree fn_fputc, fn_fputs, call = NULL_TREE;
12884 const char *fmt_str = NULL;
12886 /* If the return value is used, don't do the transformation. */
12890 /* Verify the required arguments in the original call. */
12891 if (!validate_arg (fp, POINTER_TYPE))
12893 if (!validate_arg (fmt, POINTER_TYPE))
12896 /* Check whether the format is a literal string constant. */
12897 fmt_str = c_getstr (fmt);
12898 if (fmt_str == NULL)
12901 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12903 /* If we're using an unlocked function, assume the other
12904 unlocked functions exist explicitly. */
12905 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12906 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12910 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12911 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12914 if (!init_target_chars ())
12917 /* If the format doesn't contain % args or %%, use strcpy. */
12918 if (strchr (fmt_str, target_percent) == NULL)
12920 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12924 /* If the format specifier was "", fprintf does nothing. */
12925 if (fmt_str[0] == '\0')
12927 /* If FP has side-effects, just wait until gimplification is
12929 if (TREE_SIDE_EFFECTS (fp))
12932 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12935 /* When "string" doesn't contain %, replace all cases of
12936 fprintf (fp, string) with fputs (string, fp). The fputs
12937 builtin will take care of special cases like length == 1. */
12939 call = build_call_expr (fn_fputs, 2, fmt, fp);
12942 /* The other optimizations can be done only on the non-va_list variants. */
12943 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12946 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12947 else if (strcmp (fmt_str, target_percent_s) == 0)
12949 if (!arg || !validate_arg (arg, POINTER_TYPE))
12952 call = build_call_expr (fn_fputs, 2, arg, fp);
12955 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12956 else if (strcmp (fmt_str, target_percent_c) == 0)
12958 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12961 call = build_call_expr (fn_fputc, 2, arg, fp);
12966 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
12969 /* Initialize format string characters in the target charset. */
12972 init_target_chars (void)
12977 target_newline = lang_hooks.to_target_charset ('\n');
12978 target_percent = lang_hooks.to_target_charset ('%');
12979 target_c = lang_hooks.to_target_charset ('c');
12980 target_s = lang_hooks.to_target_charset ('s');
12981 if (target_newline == 0 || target_percent == 0 || target_c == 0
12985 target_percent_c[0] = target_percent;
12986 target_percent_c[1] = target_c;
12987 target_percent_c[2] = '\0';
12989 target_percent_s[0] = target_percent;
12990 target_percent_s[1] = target_s;
12991 target_percent_s[2] = '\0';
12993 target_percent_s_newline[0] = target_percent;
12994 target_percent_s_newline[1] = target_s;
12995 target_percent_s_newline[2] = target_newline;
12996 target_percent_s_newline[3] = '\0';
13003 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13004 and no overflow/underflow occurred. INEXACT is true if M was not
13005 exactly calculated. TYPE is the tree type for the result. This
13006 function assumes that you cleared the MPFR flags and then
13007 calculated M to see if anything subsequently set a flag prior to
13008 entering this function. Return NULL_TREE if any checks fail. */
13011 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13013 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13014 overflow/underflow occurred. If -frounding-math, proceed iff the
13015 result of calling FUNC was exact. */
13016 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13017 && (!flag_rounding_math || !inexact))
13019 REAL_VALUE_TYPE rr;
13021 real_from_mpfr (&rr, m, type, GMP_RNDN);
13022 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13023 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13024 but the mpft_t is not, then we underflowed in the
13026 if (real_isfinite (&rr)
13027 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13029 REAL_VALUE_TYPE rmode;
13031 real_convert (&rmode, TYPE_MODE (type), &rr);
13032 /* Proceed iff the specified mode can hold the value. */
13033 if (real_identical (&rmode, &rr))
13034 return build_real (type, rmode);
13040 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13041 FUNC on it and return the resulting value as a tree with type TYPE.
13042 If MIN and/or MAX are not NULL, then the supplied ARG must be
13043 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13044 acceptable values, otherwise they are not. The mpfr precision is
13045 set to the precision of TYPE. We assume that function FUNC returns
13046 zero if the result could be calculated exactly within the requested
13050 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13051 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13054 tree result = NULL_TREE;
13058 /* To proceed, MPFR must exactly represent the target floating point
13059 format, which only happens when the target base equals two. */
13060 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13061 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13063 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13065 if (real_isfinite (ra)
13066 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13067 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13069 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13070 const int prec = fmt->p;
13071 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13075 mpfr_init2 (m, prec);
13076 mpfr_from_real (m, ra, GMP_RNDN);
13077 mpfr_clear_flags ();
13078 inexact = func (m, m, rnd);
13079 result = do_mpfr_ckconv (m, type, inexact);
13087 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13088 FUNC on it and return the resulting value as a tree with type TYPE.
13089 The mpfr precision is set to the precision of TYPE. We assume that
13090 function FUNC returns zero if the result could be calculated
13091 exactly within the requested precision. */
13094 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13095 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13097 tree result = NULL_TREE;
13102 /* To proceed, MPFR must exactly represent the target floating point
13103 format, which only happens when the target base equals two. */
13104 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13105 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13106 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13108 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13109 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13111 if (real_isfinite (ra1) && real_isfinite (ra2))
13113 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13114 const int prec = fmt->p;
13115 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13119 mpfr_inits2 (prec, m1, m2, NULL);
13120 mpfr_from_real (m1, ra1, GMP_RNDN);
13121 mpfr_from_real (m2, ra2, GMP_RNDN);
13122 mpfr_clear_flags ();
13123 inexact = func (m1, m1, m2, rnd);
13124 result = do_mpfr_ckconv (m1, type, inexact);
13125 mpfr_clears (m1, m2, NULL);
13132 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13133 FUNC on it and return the resulting value as a tree with type TYPE.
13134 The mpfr precision is set to the precision of TYPE. We assume that
13135 function FUNC returns zero if the result could be calculated
13136 exactly within the requested precision. */
13139 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13140 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13142 tree result = NULL_TREE;
13148 /* To proceed, MPFR must exactly represent the target floating point
13149 format, which only happens when the target base equals two. */
13150 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13151 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13152 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13153 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13155 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13156 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13157 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13159 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13161 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13162 const int prec = fmt->p;
13163 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13167 mpfr_inits2 (prec, m1, m2, m3, NULL);
13168 mpfr_from_real (m1, ra1, GMP_RNDN);
13169 mpfr_from_real (m2, ra2, GMP_RNDN);
13170 mpfr_from_real (m3, ra3, GMP_RNDN);
13171 mpfr_clear_flags ();
13172 inexact = func (m1, m1, m2, m3, rnd);
13173 result = do_mpfr_ckconv (m1, type, inexact);
13174 mpfr_clears (m1, m2, m3, NULL);
13181 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13182 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13183 If ARG_SINP and ARG_COSP are NULL then the result is returned
13184 as a complex value.
13185 The type is taken from the type of ARG and is used for setting the
13186 precision of the calculation and results. */
13189 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13191 tree const type = TREE_TYPE (arg);
13192 tree result = NULL_TREE;
13196 /* To proceed, MPFR must exactly represent the target floating point
13197 format, which only happens when the target base equals two. */
13198 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13199 && TREE_CODE (arg) == REAL_CST
13200 && !TREE_OVERFLOW (arg))
13202 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13204 if (real_isfinite (ra))
13206 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13207 const int prec = fmt->p;
13208 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13209 tree result_s, result_c;
13213 mpfr_inits2 (prec, m, ms, mc, NULL);
13214 mpfr_from_real (m, ra, GMP_RNDN);
13215 mpfr_clear_flags ();
13216 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13217 result_s = do_mpfr_ckconv (ms, type, inexact);
13218 result_c = do_mpfr_ckconv (mc, type, inexact);
13219 mpfr_clears (m, ms, mc, NULL);
13220 if (result_s && result_c)
13222 /* If we are to return in a complex value do so. */
13223 if (!arg_sinp && !arg_cosp)
13224 return build_complex (build_complex_type (type),
13225 result_c, result_s);
13227 /* Dereference the sin/cos pointer arguments. */
13228 arg_sinp = build_fold_indirect_ref (arg_sinp);
13229 arg_cosp = build_fold_indirect_ref (arg_cosp);
13230 /* Proceed if valid pointer type were passed in. */
13231 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13232 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13234 /* Set the values. */
13235 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13237 TREE_SIDE_EFFECTS (result_s) = 1;
13238 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13240 TREE_SIDE_EFFECTS (result_c) = 1;
13241 /* Combine the assignments into a compound expr. */
13242 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13243 result_s, result_c));
13251 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13252 two-argument mpfr order N Bessel function FUNC on them and return
13253 the resulting value as a tree with type TYPE. The mpfr precision
13254 is set to the precision of TYPE. We assume that function FUNC
13255 returns zero if the result could be calculated exactly within the
13256 requested precision. */
13258 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13259 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13260 const REAL_VALUE_TYPE *min, bool inclusive)
13262 tree result = NULL_TREE;
13267 /* To proceed, MPFR must exactly represent the target floating point
13268 format, which only happens when the target base equals two. */
13269 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13270 && host_integerp (arg1, 0)
13271 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13273 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13274 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13277 && real_isfinite (ra)
13278 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13280 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13281 const int prec = fmt->p;
13282 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13286 mpfr_init2 (m, prec);
13287 mpfr_from_real (m, ra, GMP_RNDN);
13288 mpfr_clear_flags ();
13289 inexact = func (m, n, m, rnd);
13290 result = do_mpfr_ckconv (m, type, inexact);
13298 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13299 the pointer *(ARG_QUO) and return the result. The type is taken
13300 from the type of ARG0 and is used for setting the precision of the
13301 calculation and results. */
13304 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13306 tree const type = TREE_TYPE (arg0);
13307 tree result = NULL_TREE;
13312 /* To proceed, MPFR must exactly represent the target floating point
13313 format, which only happens when the target base equals two. */
13314 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13315 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13316 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13318 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13319 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13321 if (real_isfinite (ra0) && real_isfinite (ra1))
13323 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13324 const int prec = fmt->p;
13325 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13330 mpfr_inits2 (prec, m0, m1, NULL);
13331 mpfr_from_real (m0, ra0, GMP_RNDN);
13332 mpfr_from_real (m1, ra1, GMP_RNDN);
13333 mpfr_clear_flags ();
13334 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13335 /* Remquo is independent of the rounding mode, so pass
13336 inexact=0 to do_mpfr_ckconv(). */
13337 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13338 mpfr_clears (m0, m1, NULL);
13341 /* MPFR calculates quo in the host's long so it may
13342 return more bits in quo than the target int can hold
13343 if sizeof(host long) > sizeof(target int). This can
13344 happen even for native compilers in LP64 mode. In
13345 these cases, modulo the quo value with the largest
13346 number that the target int can hold while leaving one
13347 bit for the sign. */
13348 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13349 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13351 /* Dereference the quo pointer argument. */
13352 arg_quo = build_fold_indirect_ref (arg_quo);
13353 /* Proceed iff a valid pointer type was passed in. */
13354 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13356 /* Set the value. */
13357 tree result_quo = fold_build2 (MODIFY_EXPR,
13358 TREE_TYPE (arg_quo), arg_quo,
13359 build_int_cst (NULL, integer_quo));
13360 TREE_SIDE_EFFECTS (result_quo) = 1;
13361 /* Combine the quo assignment with the rem. */
13362 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13363 result_quo, result_rem));
13371 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13372 resulting value as a tree with type TYPE. The mpfr precision is
13373 set to the precision of TYPE. We assume that this mpfr function
13374 returns zero if the result could be calculated exactly within the
13375 requested precision. In addition, the integer pointer represented
13376 by ARG_SG will be dereferenced and set to the appropriate signgam
13380 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13382 tree result = NULL_TREE;
13386 /* To proceed, MPFR must exactly represent the target floating point
13387 format, which only happens when the target base equals two. Also
13388 verify ARG is a constant and that ARG_SG is an int pointer. */
13389 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13390 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13391 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13392 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13394 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13396 /* In addition to NaN and Inf, the argument cannot be zero or a
13397 negative integer. */
13398 if (real_isfinite (ra)
13399 && ra->cl != rvc_zero
13400 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13402 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13403 const int prec = fmt->p;
13404 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13409 mpfr_init2 (m, prec);
13410 mpfr_from_real (m, ra, GMP_RNDN);
13411 mpfr_clear_flags ();
13412 inexact = mpfr_lgamma (m, &sg, m, rnd);
13413 result_lg = do_mpfr_ckconv (m, type, inexact);
13419 /* Dereference the arg_sg pointer argument. */
13420 arg_sg = build_fold_indirect_ref (arg_sg);
13421 /* Assign the signgam value into *arg_sg. */
13422 result_sg = fold_build2 (MODIFY_EXPR,
13423 TREE_TYPE (arg_sg), arg_sg,
13424 build_int_cst (NULL, sg));
13425 TREE_SIDE_EFFECTS (result_sg) = 1;
13426 /* Combine the signgam assignment with the lgamma result. */
13427 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13428 result_sg, result_lg));
13437 The functions below provide an alternate interface for folding
13438 builtin function calls presented as GIMPLE_CALL statements rather
13439 than as CALL_EXPRs. The folded result is still expressed as a
13440 tree. There is too much code duplication in the handling of
13441 varargs functions, and a more intrusive re-factoring would permit
13442 better sharing of code between the tree and statement-based
13443 versions of these functions. */
13445 /* Construct a new CALL_EXPR using the tail of the argument list of STMT
13446 along with N new arguments specified as the "..." parameters. SKIP
13447 is the number of arguments in STMT to be omitted. This function is used
13448 to do varargs-to-varargs transformations. */
13451 gimple_rewrite_call_expr (gimple stmt, int skip, tree fndecl, int n, ...)
13453 int oldnargs = gimple_call_num_args (stmt);
13454 int nargs = oldnargs - skip + n;
13455 tree fntype = TREE_TYPE (fndecl);
13456 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
13461 buffer = XALLOCAVEC (tree, nargs);
13463 for (i = 0; i < n; i++)
13464 buffer[i] = va_arg (ap, tree);
13466 for (j = skip; j < oldnargs; j++, i++)
13467 buffer[i] = gimple_call_arg (stmt, j);
13469 return fold (build_call_array (TREE_TYPE (fntype), fn, nargs, buffer));
13472 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13473 a normal call should be emitted rather than expanding the function
13474 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13477 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13479 tree dest, size, len, fn, fmt, flag;
13480 const char *fmt_str;
13481 int nargs = gimple_call_num_args (stmt);
13483 /* Verify the required arguments in the original call. */
13486 dest = gimple_call_arg (stmt, 0);
13487 if (!validate_arg (dest, POINTER_TYPE))
13489 flag = gimple_call_arg (stmt, 1);
13490 if (!validate_arg (flag, INTEGER_TYPE))
13492 size = gimple_call_arg (stmt, 2);
13493 if (!validate_arg (size, INTEGER_TYPE))
13495 fmt = gimple_call_arg (stmt, 3);
13496 if (!validate_arg (fmt, POINTER_TYPE))
13499 if (! host_integerp (size, 1))
13504 if (!init_target_chars ())
13507 /* Check whether the format is a literal string constant. */
13508 fmt_str = c_getstr (fmt);
13509 if (fmt_str != NULL)
13511 /* If the format doesn't contain % args or %%, we know the size. */
13512 if (strchr (fmt_str, target_percent) == 0)
13514 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13515 len = build_int_cstu (size_type_node, strlen (fmt_str));
13517 /* If the format is "%s" and first ... argument is a string literal,
13518 we know the size too. */
13519 else if (fcode == BUILT_IN_SPRINTF_CHK
13520 && strcmp (fmt_str, target_percent_s) == 0)
13526 arg = gimple_call_arg (stmt, 4);
13527 if (validate_arg (arg, POINTER_TYPE))
13529 len = c_strlen (arg, 1);
13530 if (! len || ! host_integerp (len, 1))
13537 if (! integer_all_onesp (size))
13539 if (! len || ! tree_int_cst_lt (len, size))
13543 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13544 or if format doesn't contain % chars or is "%s". */
13545 if (! integer_zerop (flag))
13547 if (fmt_str == NULL)
13549 if (strchr (fmt_str, target_percent) != NULL
13550 && strcmp (fmt_str, target_percent_s))
13554 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13555 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
13556 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
13560 return gimple_rewrite_call_expr (stmt, 4, fn, 2, dest, fmt);
13563 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13564 a normal call should be emitted rather than expanding the function
13565 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13566 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13567 passed as second argument. */
13570 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13571 enum built_in_function fcode)
13573 tree dest, size, len, fn, fmt, flag;
13574 const char *fmt_str;
13576 /* Verify the required arguments in the original call. */
13577 if (gimple_call_num_args (stmt) < 5)
13579 dest = gimple_call_arg (stmt, 0);
13580 if (!validate_arg (dest, POINTER_TYPE))
13582 len = gimple_call_arg (stmt, 1);
13583 if (!validate_arg (len, INTEGER_TYPE))
13585 flag = gimple_call_arg (stmt, 2);
13586 if (!validate_arg (flag, INTEGER_TYPE))
13588 size = gimple_call_arg (stmt, 3);
13589 if (!validate_arg (size, INTEGER_TYPE))
13591 fmt = gimple_call_arg (stmt, 4);
13592 if (!validate_arg (fmt, POINTER_TYPE))
13595 if (! host_integerp (size, 1))
13598 if (! integer_all_onesp (size))
13600 if (! host_integerp (len, 1))
13602 /* If LEN is not constant, try MAXLEN too.
13603 For MAXLEN only allow optimizing into non-_ocs function
13604 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13605 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13611 if (tree_int_cst_lt (size, maxlen))
13615 if (!init_target_chars ())
13618 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13619 or if format doesn't contain % chars or is "%s". */
13620 if (! integer_zerop (flag))
13622 fmt_str = c_getstr (fmt);
13623 if (fmt_str == NULL)
13625 if (strchr (fmt_str, target_percent) != NULL
13626 && strcmp (fmt_str, target_percent_s))
13630 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13632 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
13633 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
13637 return gimple_rewrite_call_expr (stmt, 5, fn, 3, dest, len, fmt);
13640 /* Builtins with folding operations that operate on "..." arguments
13641 need special handling; we need to store the arguments in a convenient
13642 data structure before attempting any folding. Fortunately there are
13643 only a few builtins that fall into this category. FNDECL is the
13644 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13645 result of the function call is ignored. */
13648 gimple_fold_builtin_varargs (tree fndecl, gimple stmt, bool ignore ATTRIBUTE_UNUSED)
13650 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13651 tree ret = NULL_TREE;
13655 case BUILT_IN_SPRINTF_CHK:
13656 case BUILT_IN_VSPRINTF_CHK:
13657 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13660 case BUILT_IN_SNPRINTF_CHK:
13661 case BUILT_IN_VSNPRINTF_CHK:
13662 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13669 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13670 TREE_NO_WARNING (ret) = 1;
13676 /* A wrapper function for builtin folding that prevents warnings for
13677 "statement without effect" and the like, caused by removing the
13678 call node earlier than the warning is generated. */
13681 fold_call_stmt (gimple stmt, bool ignore)
13683 tree ret = NULL_TREE;
13684 tree fndecl = gimple_call_fndecl (stmt);
13686 && TREE_CODE (fndecl) == FUNCTION_DECL
13687 && DECL_BUILT_IN (fndecl)
13688 && !gimple_call_va_arg_pack_p (stmt))
13690 int nargs = gimple_call_num_args (stmt);
13692 if (avoid_folding_inline_builtin (fndecl))
13694 /* FIXME: Don't use a list in this interface. */
13695 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13697 tree arglist = NULL_TREE;
13699 for (i = nargs - 1; i >= 0; i--)
13700 arglist = tree_cons (NULL_TREE, gimple_call_arg (stmt, i), arglist);
13701 return targetm.fold_builtin (fndecl, arglist, ignore);
13705 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13707 tree args[MAX_ARGS_TO_FOLD_BUILTIN];
13709 for (i = 0; i < nargs; i++)
13710 args[i] = gimple_call_arg (stmt, i);
13711 ret = fold_builtin_n (fndecl, args, nargs, ignore);
13714 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13717 /* Propagate location information from original call to
13718 expansion of builtin. Otherwise things like
13719 maybe_emit_chk_warning, that operate on the expansion
13720 of a builtin, will use the wrong location information. */
13721 if (gimple_has_location (stmt))
13723 tree realret = ret;
13724 if (TREE_CODE (ret) == NOP_EXPR)
13725 realret = TREE_OPERAND (ret, 0);
13726 if (CAN_HAVE_LOCATION_P (realret)
13727 && !EXPR_HAS_LOCATION (realret))
13728 SET_EXPR_LOCATION (realret, gimple_location (stmt));