1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
30 #include "tree-gimple.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"
51 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 /* Define the names of the builtin function types and codes. */
56 const char *const built_in_class_names[4]
57 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
60 const char * built_in_names[(int) END_BUILTINS] =
62 #include "builtins.def"
66 /* Setup an array of _DECL trees, make sure each element is
67 initialized to NULL_TREE. */
68 tree built_in_decls[(int) END_BUILTINS];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70 It may be NULL_TREE when this is invalid (for instance runtime is not
71 required to implement the function call in all cases). */
72 tree implicit_built_in_decls[(int) END_BUILTINS];
74 static int get_pointer_alignment (tree, unsigned int);
75 static const char *c_getstr (tree);
76 static rtx c_readstr (const char *, enum machine_mode);
77 static int target_char_cast (tree, char *);
78 static rtx get_memory_rtx (tree, tree);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx result_vector (int, rtx);
84 static void expand_builtin_update_setjmp_buf (rtx);
85 static void expand_builtin_prefetch (tree);
86 static rtx expand_builtin_apply_args (void);
87 static rtx expand_builtin_apply_args_1 (void);
88 static rtx expand_builtin_apply (rtx, rtx, rtx);
89 static void expand_builtin_return (rtx);
90 static enum type_class type_to_class (tree);
91 static rtx expand_builtin_classify_type (tree);
92 static void expand_errno_check (tree, rtx);
93 static rtx expand_builtin_mathfn (tree, rtx, rtx);
94 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
96 static rtx expand_builtin_sincos (tree);
97 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
98 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
99 static rtx expand_builtin_args_info (tree);
100 static rtx expand_builtin_next_arg (void);
101 static rtx expand_builtin_va_start (tree);
102 static rtx expand_builtin_va_end (tree);
103 static rtx expand_builtin_va_copy (tree);
104 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
107 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
108 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
114 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
115 static rtx expand_builtin_bcopy (tree);
116 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
118 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
120 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
123 static rtx expand_builtin_bzero (tree);
124 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, rtx);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static rtx expand_builtin_fputs (tree, rtx, bool);
133 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
136 static tree stabilize_va_list (tree, int);
137 static rtx expand_builtin_expect (tree, rtx);
138 static tree fold_builtin_constant_p (tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (tree);
141 static tree fold_builtin_inf (tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static int validate_arglist (tree, ...);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_tan (tree, tree);
155 static tree fold_builtin_trunc (tree, tree);
156 static tree fold_builtin_floor (tree, tree);
157 static tree fold_builtin_ceil (tree, tree);
158 static tree fold_builtin_round (tree, tree);
159 static tree fold_builtin_int_roundingfn (tree, tree);
160 static tree fold_builtin_bitop (tree, tree);
161 static tree fold_builtin_memory_op (tree, tree, bool, int);
162 static tree fold_builtin_strchr (tree, tree);
163 static tree fold_builtin_memcmp (tree);
164 static tree fold_builtin_strcmp (tree);
165 static tree fold_builtin_strncmp (tree);
166 static tree fold_builtin_signbit (tree, tree);
167 static tree fold_builtin_copysign (tree, tree, tree);
168 static tree fold_builtin_isascii (tree);
169 static tree fold_builtin_toascii (tree);
170 static tree fold_builtin_isdigit (tree);
171 static tree fold_builtin_fabs (tree, tree);
172 static tree fold_builtin_abs (tree, tree);
173 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
175 static tree fold_builtin_1 (tree, tree, bool);
177 static tree fold_builtin_strpbrk (tree, tree);
178 static tree fold_builtin_strstr (tree, tree);
179 static tree fold_builtin_strrchr (tree, tree);
180 static tree fold_builtin_strcat (tree);
181 static tree fold_builtin_strncat (tree);
182 static tree fold_builtin_strspn (tree);
183 static tree fold_builtin_strcspn (tree);
184 static tree fold_builtin_sprintf (tree, int);
186 static rtx expand_builtin_object_size (tree);
187 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
188 enum built_in_function);
189 static void maybe_emit_chk_warning (tree, enum built_in_function);
190 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
191 static tree fold_builtin_object_size (tree);
192 static tree fold_builtin_strcat_chk (tree, tree);
193 static tree fold_builtin_strncat_chk (tree, tree);
194 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
195 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
196 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
197 static bool init_target_chars (void);
199 static unsigned HOST_WIDE_INT target_newline;
200 static unsigned HOST_WIDE_INT target_percent;
201 static unsigned HOST_WIDE_INT target_c;
202 static unsigned HOST_WIDE_INT target_s;
203 static char target_percent_c[3];
204 static char target_percent_s[3];
205 static char target_percent_s_newline[4];
206 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
207 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
208 static tree do_mpfr_arg2 (tree, tree, tree,
209 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
210 static tree do_mpfr_sincos (tree, tree, tree);
212 /* Return true if NODE should be considered for inline expansion regardless
213 of the optimization level. This means whenever a function is invoked with
214 its "internal" name, which normally contains the prefix "__builtin". */
216 static bool called_as_built_in (tree node)
218 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
219 if (strncmp (name, "__builtin_", 10) == 0)
221 if (strncmp (name, "__sync_", 7) == 0)
226 /* Return the alignment in bits of EXP, a pointer valued expression.
227 But don't return more than MAX_ALIGN no matter what.
228 The alignment returned is, by default, the alignment of the thing that
229 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
231 Otherwise, look at the expression to see if we can do better, i.e., if the
232 expression is actually pointing at an object whose alignment is tighter. */
235 get_pointer_alignment (tree exp, unsigned int max_align)
237 unsigned int align, inner;
239 /* We rely on TER to compute accurate alignment information. */
240 if (!(optimize && flag_tree_ter))
243 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
246 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
247 align = MIN (align, max_align);
251 switch (TREE_CODE (exp))
255 case NON_LVALUE_EXPR:
256 exp = TREE_OPERAND (exp, 0);
257 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
260 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
261 align = MIN (inner, max_align);
265 /* If sum of pointer + int, restrict our maximum alignment to that
266 imposed by the integer. If not, we can't do any better than
268 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
271 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
272 & (max_align / BITS_PER_UNIT - 1))
276 exp = TREE_OPERAND (exp, 0);
280 /* See what we are pointing at and look at its alignment. */
281 exp = TREE_OPERAND (exp, 0);
283 if (handled_component_p (exp))
285 HOST_WIDE_INT bitsize, bitpos;
287 enum machine_mode mode;
288 int unsignedp, volatilep;
290 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
291 &mode, &unsignedp, &volatilep, true);
293 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
294 if (offset && TREE_CODE (offset) == PLUS_EXPR
295 && host_integerp (TREE_OPERAND (offset, 1), 1))
297 /* Any overflow in calculating offset_bits won't change
300 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
304 inner = MIN (inner, (offset_bits & -offset_bits));
305 offset = TREE_OPERAND (offset, 0);
307 if (offset && TREE_CODE (offset) == MULT_EXPR
308 && host_integerp (TREE_OPERAND (offset, 1), 1))
310 /* Any overflow in calculating offset_factor won't change
312 unsigned offset_factor
313 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
317 inner = MIN (inner, (offset_factor & -offset_factor));
320 inner = MIN (inner, BITS_PER_UNIT);
322 if (TREE_CODE (exp) == FUNCTION_DECL)
323 align = FUNCTION_BOUNDARY;
324 else if (DECL_P (exp))
325 align = MIN (inner, DECL_ALIGN (exp));
326 #ifdef CONSTANT_ALIGNMENT
327 else if (CONSTANT_CLASS_P (exp))
328 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
330 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
331 || TREE_CODE (exp) == INDIRECT_REF)
332 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
334 align = MIN (align, inner);
335 return MIN (align, max_align);
343 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
344 way, because it could contain a zero byte in the middle.
345 TREE_STRING_LENGTH is the size of the character array, not the string.
347 ONLY_VALUE should be nonzero if the result is not going to be emitted
348 into the instruction stream and zero if it is going to be expanded.
349 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
350 is returned, otherwise NULL, since
351 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
352 evaluate the side-effects.
354 The value returned is of type `ssizetype'.
356 Unfortunately, string_constant can't access the values of const char
357 arrays with initializers, so neither can we do so here. */
360 c_strlen (tree src, int only_value)
363 HOST_WIDE_INT offset;
368 if (TREE_CODE (src) == COND_EXPR
369 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
373 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
374 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
375 if (tree_int_cst_equal (len1, len2))
379 if (TREE_CODE (src) == COMPOUND_EXPR
380 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
381 return c_strlen (TREE_OPERAND (src, 1), only_value);
383 src = string_constant (src, &offset_node);
387 max = TREE_STRING_LENGTH (src) - 1;
388 ptr = TREE_STRING_POINTER (src);
390 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
392 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
393 compute the offset to the following null if we don't know where to
394 start searching for it. */
397 for (i = 0; i < max; i++)
401 /* We don't know the starting offset, but we do know that the string
402 has no internal zero bytes. We can assume that the offset falls
403 within the bounds of the string; otherwise, the programmer deserves
404 what he gets. Subtract the offset from the length of the string,
405 and return that. This would perhaps not be valid if we were dealing
406 with named arrays in addition to literal string constants. */
408 return size_diffop (size_int (max), offset_node);
411 /* We have a known offset into the string. Start searching there for
412 a null character if we can represent it as a single HOST_WIDE_INT. */
413 if (offset_node == 0)
415 else if (! host_integerp (offset_node, 0))
418 offset = tree_low_cst (offset_node, 0);
420 /* If the offset is known to be out of bounds, warn, and call strlen at
422 if (offset < 0 || offset > max)
424 warning (0, "offset outside bounds of constant string");
428 /* Use strlen to search for the first zero byte. Since any strings
429 constructed with build_string will have nulls appended, we win even
430 if we get handed something like (char[4])"abcd".
432 Since OFFSET is our starting index into the string, no further
433 calculation is needed. */
434 return ssize_int (strlen (ptr + offset));
437 /* Return a char pointer for a C string if it is a string constant
438 or sum of string constant and integer constant. */
445 src = string_constant (src, &offset_node);
449 if (offset_node == 0)
450 return TREE_STRING_POINTER (src);
451 else if (!host_integerp (offset_node, 1)
452 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
455 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
458 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
459 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
462 c_readstr (const char *str, enum machine_mode mode)
468 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
473 for (i = 0; i < GET_MODE_SIZE (mode); i++)
476 if (WORDS_BIG_ENDIAN)
477 j = GET_MODE_SIZE (mode) - i - 1;
478 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
479 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
480 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
482 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
485 ch = (unsigned char) str[i];
486 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
488 return immed_double_const (c[0], c[1], mode);
491 /* Cast a target constant CST to target CHAR and if that value fits into
492 host char type, return zero and put that value into variable pointed to by
496 target_char_cast (tree cst, char *p)
498 unsigned HOST_WIDE_INT val, hostval;
500 if (!host_integerp (cst, 1)
501 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
504 val = tree_low_cst (cst, 1);
505 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
506 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
509 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
510 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
519 /* Similar to save_expr, but assumes that arbitrary code is not executed
520 in between the multiple evaluations. In particular, we assume that a
521 non-addressable local variable will not be modified. */
524 builtin_save_expr (tree exp)
526 if (TREE_ADDRESSABLE (exp) == 0
527 && (TREE_CODE (exp) == PARM_DECL
528 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
531 return save_expr (exp);
534 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
535 times to get the address of either a higher stack frame, or a return
536 address located within it (depending on FNDECL_CODE). */
539 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
543 #ifdef INITIAL_FRAME_ADDRESS_RTX
544 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
548 /* For a zero count with __builtin_return_address, we don't care what
549 frame address we return, because target-specific definitions will
550 override us. Therefore frame pointer elimination is OK, and using
551 the soft frame pointer is OK.
553 For a non-zero count, or a zero count with __builtin_frame_address,
554 we require a stable offset from the current frame pointer to the
555 previous one, so we must use the hard frame pointer, and
556 we must disable frame pointer elimination. */
557 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
558 tem = frame_pointer_rtx;
561 tem = hard_frame_pointer_rtx;
563 /* Tell reload not to eliminate the frame pointer. */
564 current_function_accesses_prior_frames = 1;
568 /* Some machines need special handling before we can access
569 arbitrary frames. For example, on the SPARC, we must first flush
570 all register windows to the stack. */
571 #ifdef SETUP_FRAME_ADDRESSES
573 SETUP_FRAME_ADDRESSES ();
576 /* On the SPARC, the return address is not in the frame, it is in a
577 register. There is no way to access it off of the current frame
578 pointer, but it can be accessed off the previous frame pointer by
579 reading the value from the register window save area. */
580 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
581 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
585 /* Scan back COUNT frames to the specified frame. */
586 for (i = 0; i < count; i++)
588 /* Assume the dynamic chain pointer is in the word that the
589 frame address points to, unless otherwise specified. */
590 #ifdef DYNAMIC_CHAIN_ADDRESS
591 tem = DYNAMIC_CHAIN_ADDRESS (tem);
593 tem = memory_address (Pmode, tem);
594 tem = gen_frame_mem (Pmode, tem);
595 tem = copy_to_reg (tem);
598 /* For __builtin_frame_address, return what we've got. But, on
599 the SPARC for example, we may have to add a bias. */
600 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
601 #ifdef FRAME_ADDR_RTX
602 return FRAME_ADDR_RTX (tem);
607 /* For __builtin_return_address, get the return address from that frame. */
608 #ifdef RETURN_ADDR_RTX
609 tem = RETURN_ADDR_RTX (count, tem);
611 tem = memory_address (Pmode,
612 plus_constant (tem, GET_MODE_SIZE (Pmode)));
613 tem = gen_frame_mem (Pmode, tem);
618 /* Alias set used for setjmp buffer. */
619 static HOST_WIDE_INT setjmp_alias_set = -1;
621 /* Construct the leading half of a __builtin_setjmp call. Control will
622 return to RECEIVER_LABEL. This is also called directly by the SJLJ
623 exception handling code. */
626 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
628 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
632 if (setjmp_alias_set == -1)
633 setjmp_alias_set = new_alias_set ();
635 buf_addr = convert_memory_address (Pmode, buf_addr);
637 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
639 /* We store the frame pointer and the address of receiver_label in
640 the buffer and use the rest of it for the stack save area, which
641 is machine-dependent. */
643 mem = gen_rtx_MEM (Pmode, buf_addr);
644 set_mem_alias_set (mem, setjmp_alias_set);
645 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
647 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
648 set_mem_alias_set (mem, setjmp_alias_set);
650 emit_move_insn (validize_mem (mem),
651 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
653 stack_save = gen_rtx_MEM (sa_mode,
654 plus_constant (buf_addr,
655 2 * GET_MODE_SIZE (Pmode)));
656 set_mem_alias_set (stack_save, setjmp_alias_set);
657 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
659 /* If there is further processing to do, do it. */
660 #ifdef HAVE_builtin_setjmp_setup
661 if (HAVE_builtin_setjmp_setup)
662 emit_insn (gen_builtin_setjmp_setup (buf_addr));
665 /* Tell optimize_save_area_alloca that extra work is going to
666 need to go on during alloca. */
667 current_function_calls_setjmp = 1;
669 /* Set this so all the registers get saved in our frame; we need to be
670 able to copy the saved values for any registers from frames we unwind. */
671 current_function_has_nonlocal_label = 1;
674 /* Construct the trailing part of a __builtin_setjmp call. This is
675 also called directly by the SJLJ exception handling code. */
678 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
680 /* Clobber the FP when we get here, so we have to make sure it's
681 marked as used by this function. */
682 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
684 /* Mark the static chain as clobbered here so life information
685 doesn't get messed up for it. */
686 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
688 /* Now put in the code to restore the frame pointer, and argument
689 pointer, if needed. */
690 #ifdef HAVE_nonlocal_goto
691 if (! HAVE_nonlocal_goto)
694 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
695 /* This might change the hard frame pointer in ways that aren't
696 apparent to early optimization passes, so force a clobber. */
697 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
700 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
701 if (fixed_regs[ARG_POINTER_REGNUM])
703 #ifdef ELIMINABLE_REGS
705 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
707 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
708 if (elim_regs[i].from == ARG_POINTER_REGNUM
709 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
712 if (i == ARRAY_SIZE (elim_regs))
715 /* Now restore our arg pointer from the address at which it
716 was saved in our stack frame. */
717 emit_move_insn (virtual_incoming_args_rtx,
718 copy_to_reg (get_arg_pointer_save_area (cfun)));
723 #ifdef HAVE_builtin_setjmp_receiver
724 if (HAVE_builtin_setjmp_receiver)
725 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
728 #ifdef HAVE_nonlocal_goto_receiver
729 if (HAVE_nonlocal_goto_receiver)
730 emit_insn (gen_nonlocal_goto_receiver ());
735 /* @@@ This is a kludge. Not all machine descriptions define a blockage
736 insn, but we must not allow the code we just generated to be reordered
737 by scheduling. Specifically, the update of the frame pointer must
738 happen immediately, not later. So emit an ASM_INPUT to act as blockage
740 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
743 /* __builtin_longjmp is passed a pointer to an array of five words (not
744 all will be used on all machines). It operates similarly to the C
745 library function of the same name, but is more efficient. Much of
746 the code below is copied from the handling of non-local gotos. */
749 expand_builtin_longjmp (rtx buf_addr, rtx value)
751 rtx fp, lab, stack, insn, last;
752 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
754 if (setjmp_alias_set == -1)
755 setjmp_alias_set = new_alias_set ();
757 buf_addr = convert_memory_address (Pmode, buf_addr);
759 buf_addr = force_reg (Pmode, buf_addr);
761 /* We used to store value in static_chain_rtx, but that fails if pointers
762 are smaller than integers. We instead require that the user must pass
763 a second argument of 1, because that is what builtin_setjmp will
764 return. This also makes EH slightly more efficient, since we are no
765 longer copying around a value that we don't care about. */
766 gcc_assert (value == const1_rtx);
768 last = get_last_insn ();
769 #ifdef HAVE_builtin_longjmp
770 if (HAVE_builtin_longjmp)
771 emit_insn (gen_builtin_longjmp (buf_addr));
775 fp = gen_rtx_MEM (Pmode, buf_addr);
776 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
777 GET_MODE_SIZE (Pmode)));
779 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
780 2 * GET_MODE_SIZE (Pmode)));
781 set_mem_alias_set (fp, setjmp_alias_set);
782 set_mem_alias_set (lab, setjmp_alias_set);
783 set_mem_alias_set (stack, setjmp_alias_set);
785 /* Pick up FP, label, and SP from the block and jump. This code is
786 from expand_goto in stmt.c; see there for detailed comments. */
787 #ifdef HAVE_nonlocal_goto
788 if (HAVE_nonlocal_goto)
789 /* We have to pass a value to the nonlocal_goto pattern that will
790 get copied into the static_chain pointer, but it does not matter
791 what that value is, because builtin_setjmp does not use it. */
792 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
796 lab = copy_to_reg (lab);
798 emit_insn (gen_rtx_CLOBBER (VOIDmode,
799 gen_rtx_MEM (BLKmode,
800 gen_rtx_SCRATCH (VOIDmode))));
801 emit_insn (gen_rtx_CLOBBER (VOIDmode,
802 gen_rtx_MEM (BLKmode,
803 hard_frame_pointer_rtx)));
805 emit_move_insn (hard_frame_pointer_rtx, fp);
806 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
808 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
809 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
810 emit_indirect_jump (lab);
814 /* Search backwards and mark the jump insn as a non-local goto.
815 Note that this precludes the use of __builtin_longjmp to a
816 __builtin_setjmp target in the same function. However, we've
817 already cautioned the user that these functions are for
818 internal exception handling use only. */
819 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
821 gcc_assert (insn != last);
825 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
829 else if (CALL_P (insn))
834 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
835 and the address of the save area. */
838 expand_builtin_nonlocal_goto (tree arglist)
840 tree t_label, t_save_area;
841 rtx r_label, r_save_area, r_fp, r_sp, insn;
843 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
846 t_label = TREE_VALUE (arglist);
847 arglist = TREE_CHAIN (arglist);
848 t_save_area = TREE_VALUE (arglist);
850 r_label = expand_normal (t_label);
851 r_label = convert_memory_address (Pmode, r_label);
852 r_save_area = expand_normal (t_save_area);
853 r_save_area = convert_memory_address (Pmode, r_save_area);
854 r_fp = gen_rtx_MEM (Pmode, r_save_area);
855 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
856 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
858 current_function_has_nonlocal_goto = 1;
860 #ifdef HAVE_nonlocal_goto
861 /* ??? We no longer need to pass the static chain value, afaik. */
862 if (HAVE_nonlocal_goto)
863 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
867 r_label = copy_to_reg (r_label);
869 emit_insn (gen_rtx_CLOBBER (VOIDmode,
870 gen_rtx_MEM (BLKmode,
871 gen_rtx_SCRATCH (VOIDmode))));
873 emit_insn (gen_rtx_CLOBBER (VOIDmode,
874 gen_rtx_MEM (BLKmode,
875 hard_frame_pointer_rtx)));
877 /* Restore frame pointer for containing function.
878 This sets the actual hard register used for the frame pointer
879 to the location of the function's incoming static chain info.
880 The non-local goto handler will then adjust it to contain the
881 proper value and reload the argument pointer, if needed. */
882 emit_move_insn (hard_frame_pointer_rtx, r_fp);
883 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
885 /* USE of hard_frame_pointer_rtx added for consistency;
886 not clear if really needed. */
887 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
888 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
889 emit_indirect_jump (r_label);
892 /* Search backwards to the jump insn and mark it as a
894 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
898 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
899 const0_rtx, REG_NOTES (insn));
902 else if (CALL_P (insn))
909 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
910 (not all will be used on all machines) that was passed to __builtin_setjmp.
911 It updates the stack pointer in that block to correspond to the current
915 expand_builtin_update_setjmp_buf (rtx buf_addr)
917 enum machine_mode sa_mode = Pmode;
921 #ifdef HAVE_save_stack_nonlocal
922 if (HAVE_save_stack_nonlocal)
923 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
925 #ifdef STACK_SAVEAREA_MODE
926 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
930 = gen_rtx_MEM (sa_mode,
933 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
937 emit_insn (gen_setjmp ());
940 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
943 /* Expand a call to __builtin_prefetch. For a target that does not support
944 data prefetch, evaluate the memory address argument in case it has side
948 expand_builtin_prefetch (tree arglist)
950 tree arg0, arg1, arg2;
953 if (!validate_arglist (arglist, POINTER_TYPE, 0))
956 arg0 = TREE_VALUE (arglist);
957 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
958 zero (read) and argument 2 (locality) defaults to 3 (high degree of
960 if (TREE_CHAIN (arglist))
962 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
963 if (TREE_CHAIN (TREE_CHAIN (arglist)))
964 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
966 arg2 = build_int_cst (NULL_TREE, 3);
970 arg1 = integer_zero_node;
971 arg2 = build_int_cst (NULL_TREE, 3);
974 /* Argument 0 is an address. */
975 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
977 /* Argument 1 (read/write flag) must be a compile-time constant int. */
978 if (TREE_CODE (arg1) != INTEGER_CST)
980 error ("second argument to %<__builtin_prefetch%> must be a constant");
981 arg1 = integer_zero_node;
983 op1 = expand_normal (arg1);
984 /* Argument 1 must be either zero or one. */
985 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
987 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
992 /* Argument 2 (locality) must be a compile-time constant int. */
993 if (TREE_CODE (arg2) != INTEGER_CST)
995 error ("third argument to %<__builtin_prefetch%> must be a constant");
996 arg2 = integer_zero_node;
998 op2 = expand_normal (arg2);
999 /* Argument 2 must be 0, 1, 2, or 3. */
1000 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1002 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1006 #ifdef HAVE_prefetch
1009 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1011 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1012 || (GET_MODE (op0) != Pmode))
1014 op0 = convert_memory_address (Pmode, op0);
1015 op0 = force_reg (Pmode, op0);
1017 emit_insn (gen_prefetch (op0, op1, op2));
1021 /* Don't do anything with direct references to volatile memory, but
1022 generate code to handle other side effects. */
1023 if (!MEM_P (op0) && side_effects_p (op0))
1027 /* Get a MEM rtx for expression EXP which is the address of an operand
1028 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1029 the maximum length of the block of memory that might be accessed or
1033 get_memory_rtx (tree exp, tree len)
1035 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1036 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1038 /* Get an expression we can use to find the attributes to assign to MEM.
1039 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1040 we can. First remove any nops. */
1041 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1042 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1043 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1044 exp = TREE_OPERAND (exp, 0);
1046 if (TREE_CODE (exp) == ADDR_EXPR)
1047 exp = TREE_OPERAND (exp, 0);
1048 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1049 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1053 /* Honor attributes derived from exp, except for the alias set
1054 (as builtin stringops may alias with anything) and the size
1055 (as stringops may access multiple array elements). */
1058 set_mem_attributes (mem, exp, 0);
1060 /* Allow the string and memory builtins to overflow from one
1061 field into another, see http://gcc.gnu.org/PR23561.
1062 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1063 memory accessed by the string or memory builtin will fit
1064 within the field. */
1065 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1067 tree mem_expr = MEM_EXPR (mem);
1068 HOST_WIDE_INT offset = -1, length = -1;
1071 while (TREE_CODE (inner) == ARRAY_REF
1072 || TREE_CODE (inner) == NOP_EXPR
1073 || TREE_CODE (inner) == CONVERT_EXPR
1074 || TREE_CODE (inner) == NON_LVALUE_EXPR
1075 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1076 || TREE_CODE (inner) == SAVE_EXPR)
1077 inner = TREE_OPERAND (inner, 0);
1079 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1081 if (MEM_OFFSET (mem)
1082 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1083 offset = INTVAL (MEM_OFFSET (mem));
1085 if (offset >= 0 && len && host_integerp (len, 0))
1086 length = tree_low_cst (len, 0);
1088 while (TREE_CODE (inner) == COMPONENT_REF)
1090 tree field = TREE_OPERAND (inner, 1);
1091 gcc_assert (! DECL_BIT_FIELD (field));
1092 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1093 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1096 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1097 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1100 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1101 /* If we can prove the memory starting at XEXP (mem, 0)
1102 and ending at XEXP (mem, 0) + LENGTH will fit into
1103 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1106 && offset + length <= size)
1111 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1112 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1113 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1121 mem_expr = TREE_OPERAND (mem_expr, 0);
1122 inner = TREE_OPERAND (inner, 0);
1125 if (mem_expr == NULL)
1127 if (mem_expr != MEM_EXPR (mem))
1129 set_mem_expr (mem, mem_expr);
1130 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1133 set_mem_alias_set (mem, 0);
1134 set_mem_size (mem, NULL_RTX);
1140 /* Built-in functions to perform an untyped call and return. */
1142 /* For each register that may be used for calling a function, this
1143 gives a mode used to copy the register's value. VOIDmode indicates
1144 the register is not used for calling a function. If the machine
1145 has register windows, this gives only the outbound registers.
1146 INCOMING_REGNO gives the corresponding inbound register. */
1147 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1149 /* For each register that may be used for returning values, this gives
1150 a mode used to copy the register's value. VOIDmode indicates the
1151 register is not used for returning values. If the machine has
1152 register windows, this gives only the outbound registers.
1153 INCOMING_REGNO gives the corresponding inbound register. */
1154 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1156 /* For each register that may be used for calling a function, this
1157 gives the offset of that register into the block returned by
1158 __builtin_apply_args. 0 indicates that the register is not
1159 used for calling a function. */
1160 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1162 /* Return the size required for the block returned by __builtin_apply_args,
1163 and initialize apply_args_mode. */
1166 apply_args_size (void)
1168 static int size = -1;
1171 enum machine_mode mode;
1173 /* The values computed by this function never change. */
1176 /* The first value is the incoming arg-pointer. */
1177 size = GET_MODE_SIZE (Pmode);
1179 /* The second value is the structure value address unless this is
1180 passed as an "invisible" first argument. */
1181 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1182 size += GET_MODE_SIZE (Pmode);
1184 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1185 if (FUNCTION_ARG_REGNO_P (regno))
1187 mode = reg_raw_mode[regno];
1189 gcc_assert (mode != VOIDmode);
1191 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1192 if (size % align != 0)
1193 size = CEIL (size, align) * align;
1194 apply_args_reg_offset[regno] = size;
1195 size += GET_MODE_SIZE (mode);
1196 apply_args_mode[regno] = mode;
1200 apply_args_mode[regno] = VOIDmode;
1201 apply_args_reg_offset[regno] = 0;
1207 /* Return the size required for the block returned by __builtin_apply,
1208 and initialize apply_result_mode. */
1211 apply_result_size (void)
1213 static int size = -1;
1215 enum machine_mode mode;
1217 /* The values computed by this function never change. */
1222 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1223 if (FUNCTION_VALUE_REGNO_P (regno))
1225 mode = reg_raw_mode[regno];
1227 gcc_assert (mode != VOIDmode);
1229 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1230 if (size % align != 0)
1231 size = CEIL (size, align) * align;
1232 size += GET_MODE_SIZE (mode);
1233 apply_result_mode[regno] = mode;
1236 apply_result_mode[regno] = VOIDmode;
1238 /* Allow targets that use untyped_call and untyped_return to override
1239 the size so that machine-specific information can be stored here. */
1240 #ifdef APPLY_RESULT_SIZE
1241 size = APPLY_RESULT_SIZE;
1247 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1248 /* Create a vector describing the result block RESULT. If SAVEP is true,
1249 the result block is used to save the values; otherwise it is used to
1250 restore the values. */
1253 result_vector (int savep, rtx result)
1255 int regno, size, align, nelts;
1256 enum machine_mode mode;
1258 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1261 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1262 if ((mode = apply_result_mode[regno]) != VOIDmode)
1264 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1265 if (size % align != 0)
1266 size = CEIL (size, align) * align;
1267 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1268 mem = adjust_address (result, mode, size);
1269 savevec[nelts++] = (savep
1270 ? gen_rtx_SET (VOIDmode, mem, reg)
1271 : gen_rtx_SET (VOIDmode, reg, mem));
1272 size += GET_MODE_SIZE (mode);
1274 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1276 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1278 /* Save the state required to perform an untyped call with the same
1279 arguments as were passed to the current function. */
1282 expand_builtin_apply_args_1 (void)
1285 int size, align, regno;
1286 enum machine_mode mode;
1287 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1289 /* Create a block where the arg-pointer, structure value address,
1290 and argument registers can be saved. */
1291 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1293 /* Walk past the arg-pointer and structure value address. */
1294 size = GET_MODE_SIZE (Pmode);
1295 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1296 size += GET_MODE_SIZE (Pmode);
1298 /* Save each register used in calling a function to the block. */
1299 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1300 if ((mode = apply_args_mode[regno]) != VOIDmode)
1302 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1303 if (size % align != 0)
1304 size = CEIL (size, align) * align;
1306 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1308 emit_move_insn (adjust_address (registers, mode, size), tem);
1309 size += GET_MODE_SIZE (mode);
1312 /* Save the arg pointer to the block. */
1313 tem = copy_to_reg (virtual_incoming_args_rtx);
1314 #ifdef STACK_GROWS_DOWNWARD
1315 /* We need the pointer as the caller actually passed them to us, not
1316 as we might have pretended they were passed. Make sure it's a valid
1317 operand, as emit_move_insn isn't expected to handle a PLUS. */
1319 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1322 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1324 size = GET_MODE_SIZE (Pmode);
1326 /* Save the structure value address unless this is passed as an
1327 "invisible" first argument. */
1328 if (struct_incoming_value)
1330 emit_move_insn (adjust_address (registers, Pmode, size),
1331 copy_to_reg (struct_incoming_value));
1332 size += GET_MODE_SIZE (Pmode);
1335 /* Return the address of the block. */
1336 return copy_addr_to_reg (XEXP (registers, 0));
1339 /* __builtin_apply_args returns block of memory allocated on
1340 the stack into which is stored the arg pointer, structure
1341 value address, static chain, and all the registers that might
1342 possibly be used in performing a function call. The code is
1343 moved to the start of the function so the incoming values are
1347 expand_builtin_apply_args (void)
1349 /* Don't do __builtin_apply_args more than once in a function.
1350 Save the result of the first call and reuse it. */
1351 if (apply_args_value != 0)
1352 return apply_args_value;
1354 /* When this function is called, it means that registers must be
1355 saved on entry to this function. So we migrate the
1356 call to the first insn of this function. */
1361 temp = expand_builtin_apply_args_1 ();
1365 apply_args_value = temp;
1367 /* Put the insns after the NOTE that starts the function.
1368 If this is inside a start_sequence, make the outer-level insn
1369 chain current, so the code is placed at the start of the
1371 push_topmost_sequence ();
1372 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1373 pop_topmost_sequence ();
1378 /* Perform an untyped call and save the state required to perform an
1379 untyped return of whatever value was returned by the given function. */
1382 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1384 int size, align, regno;
1385 enum machine_mode mode;
1386 rtx incoming_args, result, reg, dest, src, call_insn;
1387 rtx old_stack_level = 0;
1388 rtx call_fusage = 0;
1389 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1391 arguments = convert_memory_address (Pmode, arguments);
1393 /* Create a block where the return registers can be saved. */
1394 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1396 /* Fetch the arg pointer from the ARGUMENTS block. */
1397 incoming_args = gen_reg_rtx (Pmode);
1398 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1399 #ifndef STACK_GROWS_DOWNWARD
1400 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1401 incoming_args, 0, OPTAB_LIB_WIDEN);
1404 /* Push a new argument block and copy the arguments. Do not allow
1405 the (potential) memcpy call below to interfere with our stack
1407 do_pending_stack_adjust ();
1410 /* Save the stack with nonlocal if available. */
1411 #ifdef HAVE_save_stack_nonlocal
1412 if (HAVE_save_stack_nonlocal)
1413 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1416 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1418 /* Allocate a block of memory onto the stack and copy the memory
1419 arguments to the outgoing arguments address. */
1420 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1421 dest = virtual_outgoing_args_rtx;
1422 #ifndef STACK_GROWS_DOWNWARD
1423 if (GET_CODE (argsize) == CONST_INT)
1424 dest = plus_constant (dest, -INTVAL (argsize));
1426 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1428 dest = gen_rtx_MEM (BLKmode, dest);
1429 set_mem_align (dest, PARM_BOUNDARY);
1430 src = gen_rtx_MEM (BLKmode, incoming_args);
1431 set_mem_align (src, PARM_BOUNDARY);
1432 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1434 /* Refer to the argument block. */
1436 arguments = gen_rtx_MEM (BLKmode, arguments);
1437 set_mem_align (arguments, PARM_BOUNDARY);
1439 /* Walk past the arg-pointer and structure value address. */
1440 size = GET_MODE_SIZE (Pmode);
1442 size += GET_MODE_SIZE (Pmode);
1444 /* Restore each of the registers previously saved. Make USE insns
1445 for each of these registers for use in making the call. */
1446 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1447 if ((mode = apply_args_mode[regno]) != VOIDmode)
1449 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1450 if (size % align != 0)
1451 size = CEIL (size, align) * align;
1452 reg = gen_rtx_REG (mode, regno);
1453 emit_move_insn (reg, adjust_address (arguments, mode, size));
1454 use_reg (&call_fusage, reg);
1455 size += GET_MODE_SIZE (mode);
1458 /* Restore the structure value address unless this is passed as an
1459 "invisible" first argument. */
1460 size = GET_MODE_SIZE (Pmode);
1463 rtx value = gen_reg_rtx (Pmode);
1464 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1465 emit_move_insn (struct_value, value);
1466 if (REG_P (struct_value))
1467 use_reg (&call_fusage, struct_value);
1468 size += GET_MODE_SIZE (Pmode);
1471 /* All arguments and registers used for the call are set up by now! */
1472 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1474 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1475 and we don't want to load it into a register as an optimization,
1476 because prepare_call_address already did it if it should be done. */
1477 if (GET_CODE (function) != SYMBOL_REF)
1478 function = memory_address (FUNCTION_MODE, function);
1480 /* Generate the actual call instruction and save the return value. */
1481 #ifdef HAVE_untyped_call
1482 if (HAVE_untyped_call)
1483 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1484 result, result_vector (1, result)));
1487 #ifdef HAVE_call_value
1488 if (HAVE_call_value)
1492 /* Locate the unique return register. It is not possible to
1493 express a call that sets more than one return register using
1494 call_value; use untyped_call for that. In fact, untyped_call
1495 only needs to save the return registers in the given block. */
1496 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1497 if ((mode = apply_result_mode[regno]) != VOIDmode)
1499 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1501 valreg = gen_rtx_REG (mode, regno);
1504 emit_call_insn (GEN_CALL_VALUE (valreg,
1505 gen_rtx_MEM (FUNCTION_MODE, function),
1506 const0_rtx, NULL_RTX, const0_rtx));
1508 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1514 /* Find the CALL insn we just emitted, and attach the register usage
1516 call_insn = last_call_insn ();
1517 add_function_usage_to (call_insn, call_fusage);
1519 /* Restore the stack. */
1520 #ifdef HAVE_save_stack_nonlocal
1521 if (HAVE_save_stack_nonlocal)
1522 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1525 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1529 /* Return the address of the result block. */
1530 result = copy_addr_to_reg (XEXP (result, 0));
1531 return convert_memory_address (ptr_mode, result);
1534 /* Perform an untyped return. */
1537 expand_builtin_return (rtx result)
1539 int size, align, regno;
1540 enum machine_mode mode;
1542 rtx call_fusage = 0;
1544 result = convert_memory_address (Pmode, result);
1546 apply_result_size ();
1547 result = gen_rtx_MEM (BLKmode, result);
1549 #ifdef HAVE_untyped_return
1550 if (HAVE_untyped_return)
1552 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1558 /* Restore the return value and note that each value is used. */
1560 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1561 if ((mode = apply_result_mode[regno]) != VOIDmode)
1563 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1564 if (size % align != 0)
1565 size = CEIL (size, align) * align;
1566 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1567 emit_move_insn (reg, adjust_address (result, mode, size));
1569 push_to_sequence (call_fusage);
1570 emit_insn (gen_rtx_USE (VOIDmode, reg));
1571 call_fusage = get_insns ();
1573 size += GET_MODE_SIZE (mode);
1576 /* Put the USE insns before the return. */
1577 emit_insn (call_fusage);
1579 /* Return whatever values was restored by jumping directly to the end
1581 expand_naked_return ();
1584 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1586 static enum type_class
1587 type_to_class (tree type)
1589 switch (TREE_CODE (type))
1591 case VOID_TYPE: return void_type_class;
1592 case INTEGER_TYPE: return integer_type_class;
1593 case ENUMERAL_TYPE: return enumeral_type_class;
1594 case BOOLEAN_TYPE: return boolean_type_class;
1595 case POINTER_TYPE: return pointer_type_class;
1596 case REFERENCE_TYPE: return reference_type_class;
1597 case OFFSET_TYPE: return offset_type_class;
1598 case REAL_TYPE: return real_type_class;
1599 case COMPLEX_TYPE: return complex_type_class;
1600 case FUNCTION_TYPE: return function_type_class;
1601 case METHOD_TYPE: return method_type_class;
1602 case RECORD_TYPE: return record_type_class;
1604 case QUAL_UNION_TYPE: return union_type_class;
1605 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1606 ? string_type_class : array_type_class);
1607 case LANG_TYPE: return lang_type_class;
1608 default: return no_type_class;
1612 /* Expand a call to __builtin_classify_type with arguments found in
1616 expand_builtin_classify_type (tree arglist)
1619 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1620 return GEN_INT (no_type_class);
1623 /* This helper macro, meant to be used in mathfn_built_in below,
1624 determines which among a set of three builtin math functions is
1625 appropriate for a given type mode. The `F' and `L' cases are
1626 automatically generated from the `double' case. */
1627 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1628 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1629 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1630 fcodel = BUILT_IN_MATHFN##L ; break;
1632 /* Return mathematic function equivalent to FN but operating directly
1633 on TYPE, if available. If we can't do the conversion, return zero. */
1635 mathfn_built_in (tree type, enum built_in_function fn)
1637 enum built_in_function fcode, fcodef, fcodel;
1641 CASE_MATHFN (BUILT_IN_ACOS)
1642 CASE_MATHFN (BUILT_IN_ACOSH)
1643 CASE_MATHFN (BUILT_IN_ASIN)
1644 CASE_MATHFN (BUILT_IN_ASINH)
1645 CASE_MATHFN (BUILT_IN_ATAN)
1646 CASE_MATHFN (BUILT_IN_ATAN2)
1647 CASE_MATHFN (BUILT_IN_ATANH)
1648 CASE_MATHFN (BUILT_IN_CBRT)
1649 CASE_MATHFN (BUILT_IN_CEIL)
1650 CASE_MATHFN (BUILT_IN_COPYSIGN)
1651 CASE_MATHFN (BUILT_IN_COS)
1652 CASE_MATHFN (BUILT_IN_COSH)
1653 CASE_MATHFN (BUILT_IN_DREM)
1654 CASE_MATHFN (BUILT_IN_ERF)
1655 CASE_MATHFN (BUILT_IN_ERFC)
1656 CASE_MATHFN (BUILT_IN_EXP)
1657 CASE_MATHFN (BUILT_IN_EXP10)
1658 CASE_MATHFN (BUILT_IN_EXP2)
1659 CASE_MATHFN (BUILT_IN_EXPM1)
1660 CASE_MATHFN (BUILT_IN_FABS)
1661 CASE_MATHFN (BUILT_IN_FDIM)
1662 CASE_MATHFN (BUILT_IN_FLOOR)
1663 CASE_MATHFN (BUILT_IN_FMA)
1664 CASE_MATHFN (BUILT_IN_FMAX)
1665 CASE_MATHFN (BUILT_IN_FMIN)
1666 CASE_MATHFN (BUILT_IN_FMOD)
1667 CASE_MATHFN (BUILT_IN_FREXP)
1668 CASE_MATHFN (BUILT_IN_GAMMA)
1669 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1670 CASE_MATHFN (BUILT_IN_HYPOT)
1671 CASE_MATHFN (BUILT_IN_ILOGB)
1672 CASE_MATHFN (BUILT_IN_INF)
1673 CASE_MATHFN (BUILT_IN_J0)
1674 CASE_MATHFN (BUILT_IN_J1)
1675 CASE_MATHFN (BUILT_IN_JN)
1676 CASE_MATHFN (BUILT_IN_LCEIL)
1677 CASE_MATHFN (BUILT_IN_LDEXP)
1678 CASE_MATHFN (BUILT_IN_LFLOOR)
1679 CASE_MATHFN (BUILT_IN_LGAMMA)
1680 CASE_MATHFN (BUILT_IN_LLCEIL)
1681 CASE_MATHFN (BUILT_IN_LLFLOOR)
1682 CASE_MATHFN (BUILT_IN_LLRINT)
1683 CASE_MATHFN (BUILT_IN_LLROUND)
1684 CASE_MATHFN (BUILT_IN_LOG)
1685 CASE_MATHFN (BUILT_IN_LOG10)
1686 CASE_MATHFN (BUILT_IN_LOG1P)
1687 CASE_MATHFN (BUILT_IN_LOG2)
1688 CASE_MATHFN (BUILT_IN_LOGB)
1689 CASE_MATHFN (BUILT_IN_LRINT)
1690 CASE_MATHFN (BUILT_IN_LROUND)
1691 CASE_MATHFN (BUILT_IN_MODF)
1692 CASE_MATHFN (BUILT_IN_NAN)
1693 CASE_MATHFN (BUILT_IN_NANS)
1694 CASE_MATHFN (BUILT_IN_NEARBYINT)
1695 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1696 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1697 CASE_MATHFN (BUILT_IN_POW)
1698 CASE_MATHFN (BUILT_IN_POWI)
1699 CASE_MATHFN (BUILT_IN_POW10)
1700 CASE_MATHFN (BUILT_IN_REMAINDER)
1701 CASE_MATHFN (BUILT_IN_REMQUO)
1702 CASE_MATHFN (BUILT_IN_RINT)
1703 CASE_MATHFN (BUILT_IN_ROUND)
1704 CASE_MATHFN (BUILT_IN_SCALB)
1705 CASE_MATHFN (BUILT_IN_SCALBLN)
1706 CASE_MATHFN (BUILT_IN_SCALBN)
1707 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1708 CASE_MATHFN (BUILT_IN_SIN)
1709 CASE_MATHFN (BUILT_IN_SINCOS)
1710 CASE_MATHFN (BUILT_IN_SINH)
1711 CASE_MATHFN (BUILT_IN_SQRT)
1712 CASE_MATHFN (BUILT_IN_TAN)
1713 CASE_MATHFN (BUILT_IN_TANH)
1714 CASE_MATHFN (BUILT_IN_TGAMMA)
1715 CASE_MATHFN (BUILT_IN_TRUNC)
1716 CASE_MATHFN (BUILT_IN_Y0)
1717 CASE_MATHFN (BUILT_IN_Y1)
1718 CASE_MATHFN (BUILT_IN_YN)
1724 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1725 return implicit_built_in_decls[fcode];
1726 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1727 return implicit_built_in_decls[fcodef];
1728 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1729 return implicit_built_in_decls[fcodel];
1734 /* If errno must be maintained, expand the RTL to check if the result,
1735 TARGET, of a built-in function call, EXP, is NaN, and if so set
1739 expand_errno_check (tree exp, rtx target)
1741 rtx lab = gen_label_rtx ();
1743 /* Test the result; if it is NaN, set errno=EDOM because
1744 the argument was not in the domain. */
1745 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1749 /* If this built-in doesn't throw an exception, set errno directly. */
1750 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1752 #ifdef GEN_ERRNO_RTX
1753 rtx errno_rtx = GEN_ERRNO_RTX;
1756 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1758 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1764 /* We can't set errno=EDOM directly; let the library call do it.
1765 Pop the arguments right away in case the call gets deleted. */
1767 expand_call (exp, target, 0);
1773 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1774 Return 0 if a normal call should be emitted rather than expanding the
1775 function in-line. EXP is the expression that is a call to the builtin
1776 function; if convenient, the result should be placed in TARGET.
1777 SUBTARGET may be used as the target for computing one of EXP's operands. */
1780 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1782 optab builtin_optab;
1783 rtx op0, insns, before_call;
1784 tree fndecl = get_callee_fndecl (exp);
1785 tree arglist = TREE_OPERAND (exp, 1);
1786 enum machine_mode mode;
1787 bool errno_set = false;
1790 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1793 arg = TREE_VALUE (arglist);
1795 switch (DECL_FUNCTION_CODE (fndecl))
1797 CASE_FLT_FN (BUILT_IN_SQRT):
1798 errno_set = ! tree_expr_nonnegative_p (arg);
1799 builtin_optab = sqrt_optab;
1801 CASE_FLT_FN (BUILT_IN_EXP):
1802 errno_set = true; builtin_optab = exp_optab; break;
1803 CASE_FLT_FN (BUILT_IN_EXP10):
1804 CASE_FLT_FN (BUILT_IN_POW10):
1805 errno_set = true; builtin_optab = exp10_optab; break;
1806 CASE_FLT_FN (BUILT_IN_EXP2):
1807 errno_set = true; builtin_optab = exp2_optab; break;
1808 CASE_FLT_FN (BUILT_IN_EXPM1):
1809 errno_set = true; builtin_optab = expm1_optab; break;
1810 CASE_FLT_FN (BUILT_IN_LOGB):
1811 errno_set = true; builtin_optab = logb_optab; break;
1812 CASE_FLT_FN (BUILT_IN_ILOGB):
1813 errno_set = true; builtin_optab = ilogb_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOG):
1815 errno_set = true; builtin_optab = log_optab; break;
1816 CASE_FLT_FN (BUILT_IN_LOG10):
1817 errno_set = true; builtin_optab = log10_optab; break;
1818 CASE_FLT_FN (BUILT_IN_LOG2):
1819 errno_set = true; builtin_optab = log2_optab; break;
1820 CASE_FLT_FN (BUILT_IN_LOG1P):
1821 errno_set = true; builtin_optab = log1p_optab; break;
1822 CASE_FLT_FN (BUILT_IN_ASIN):
1823 builtin_optab = asin_optab; break;
1824 CASE_FLT_FN (BUILT_IN_ACOS):
1825 builtin_optab = acos_optab; break;
1826 CASE_FLT_FN (BUILT_IN_TAN):
1827 builtin_optab = tan_optab; break;
1828 CASE_FLT_FN (BUILT_IN_ATAN):
1829 builtin_optab = atan_optab; break;
1830 CASE_FLT_FN (BUILT_IN_FLOOR):
1831 builtin_optab = floor_optab; break;
1832 CASE_FLT_FN (BUILT_IN_CEIL):
1833 builtin_optab = ceil_optab; break;
1834 CASE_FLT_FN (BUILT_IN_TRUNC):
1835 builtin_optab = btrunc_optab; break;
1836 CASE_FLT_FN (BUILT_IN_ROUND):
1837 builtin_optab = round_optab; break;
1838 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1839 builtin_optab = nearbyint_optab;
1840 if (flag_trapping_math)
1842 /* Else fallthrough and expand as rint. */
1843 CASE_FLT_FN (BUILT_IN_RINT):
1844 builtin_optab = rint_optab; break;
1849 /* Make a suitable register to place result in. */
1850 mode = TYPE_MODE (TREE_TYPE (exp));
1852 if (! flag_errno_math || ! HONOR_NANS (mode))
1855 /* Before working hard, check whether the instruction is available. */
1856 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1858 target = gen_reg_rtx (mode);
1860 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1861 need to expand the argument again. This way, we will not perform
1862 side-effects more the once. */
1863 narg = builtin_save_expr (arg);
1867 arglist = build_tree_list (NULL_TREE, arg);
1868 exp = build_function_call_expr (fndecl, arglist);
1871 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1875 /* Compute into TARGET.
1876 Set TARGET to wherever the result comes back. */
1877 target = expand_unop (mode, builtin_optab, op0, target, 0);
1882 expand_errno_check (exp, target);
1884 /* Output the entire sequence. */
1885 insns = get_insns ();
1891 /* If we were unable to expand via the builtin, stop the sequence
1892 (without outputting the insns) and call to the library function
1893 with the stabilized argument list. */
1897 before_call = get_last_insn ();
1899 target = expand_call (exp, target, target == const0_rtx);
1901 /* If this is a sqrt operation and we don't care about errno, try to
1902 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1903 This allows the semantics of the libcall to be visible to the RTL
1905 if (builtin_optab == sqrt_optab && !errno_set)
1907 /* Search backwards through the insns emitted by expand_call looking
1908 for the instruction with the REG_RETVAL note. */
1909 rtx last = get_last_insn ();
1910 while (last != before_call)
1912 if (find_reg_note (last, REG_RETVAL, NULL))
1914 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1915 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1916 two elements, i.e. symbol_ref(sqrt) and the operand. */
1918 && GET_CODE (note) == EXPR_LIST
1919 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1920 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1921 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1923 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1924 /* Check operand is a register with expected mode. */
1927 && GET_MODE (operand) == mode)
1929 /* Replace the REG_EQUAL note with a SQRT rtx. */
1930 rtx equiv = gen_rtx_SQRT (mode, operand);
1931 set_unique_reg_note (last, REG_EQUAL, equiv);
1936 last = PREV_INSN (last);
1943 /* Expand a call to the builtin binary math functions (pow and atan2).
1944 Return 0 if a normal call should be emitted rather than expanding the
1945 function in-line. EXP is the expression that is a call to the builtin
1946 function; if convenient, the result should be placed in TARGET.
1947 SUBTARGET may be used as the target for computing one of EXP's
1951 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1953 optab builtin_optab;
1954 rtx op0, op1, insns;
1955 int op1_type = REAL_TYPE;
1956 tree fndecl = get_callee_fndecl (exp);
1957 tree arglist = TREE_OPERAND (exp, 1);
1958 tree arg0, arg1, temp, narg;
1959 enum machine_mode mode;
1960 bool errno_set = true;
1963 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1964 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1965 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1966 op1_type = INTEGER_TYPE;
1968 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1971 arg0 = TREE_VALUE (arglist);
1972 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1974 switch (DECL_FUNCTION_CODE (fndecl))
1976 CASE_FLT_FN (BUILT_IN_POW):
1977 builtin_optab = pow_optab; break;
1978 CASE_FLT_FN (BUILT_IN_ATAN2):
1979 builtin_optab = atan2_optab; break;
1980 CASE_FLT_FN (BUILT_IN_LDEXP):
1981 builtin_optab = ldexp_optab; break;
1982 CASE_FLT_FN (BUILT_IN_FMOD):
1983 builtin_optab = fmod_optab; break;
1984 CASE_FLT_FN (BUILT_IN_REMAINDER):
1985 CASE_FLT_FN (BUILT_IN_DREM):
1986 builtin_optab = remainder_optab; break;
1991 /* Make a suitable register to place result in. */
1992 mode = TYPE_MODE (TREE_TYPE (exp));
1994 /* Before working hard, check whether the instruction is available. */
1995 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1998 target = gen_reg_rtx (mode);
2000 if (! flag_errno_math || ! HONOR_NANS (mode))
2003 /* Always stabilize the argument list. */
2004 narg = builtin_save_expr (arg1);
2008 temp = build_tree_list (NULL_TREE, narg);
2012 temp = TREE_CHAIN (arglist);
2014 narg = builtin_save_expr (arg0);
2018 arglist = tree_cons (NULL_TREE, narg, temp);
2022 arglist = tree_cons (NULL_TREE, arg0, temp);
2025 exp = build_function_call_expr (fndecl, arglist);
2027 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2028 op1 = expand_normal (arg1);
2032 /* Compute into TARGET.
2033 Set TARGET to wherever the result comes back. */
2034 target = expand_binop (mode, builtin_optab, op0, op1,
2035 target, 0, OPTAB_DIRECT);
2037 /* If we were unable to expand via the builtin, stop the sequence
2038 (without outputting the insns) and call to the library function
2039 with the stabilized argument list. */
2043 return expand_call (exp, target, target == const0_rtx);
2047 expand_errno_check (exp, target);
2049 /* Output the entire sequence. */
2050 insns = get_insns ();
2057 /* Expand a call to the builtin sin and cos math functions.
2058 Return 0 if a normal call should be emitted rather than expanding the
2059 function in-line. EXP is the expression that is a call to the builtin
2060 function; if convenient, the result should be placed in TARGET.
2061 SUBTARGET may be used as the target for computing one of EXP's
2065 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2067 optab builtin_optab;
2069 tree fndecl = get_callee_fndecl (exp);
2070 tree arglist = TREE_OPERAND (exp, 1);
2071 enum machine_mode mode;
2074 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2077 arg = TREE_VALUE (arglist);
2079 switch (DECL_FUNCTION_CODE (fndecl))
2081 CASE_FLT_FN (BUILT_IN_SIN):
2082 CASE_FLT_FN (BUILT_IN_COS):
2083 builtin_optab = sincos_optab; break;
2088 /* Make a suitable register to place result in. */
2089 mode = TYPE_MODE (TREE_TYPE (exp));
2091 /* Check if sincos insn is available, otherwise fallback
2092 to sin or cos insn. */
2093 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2094 switch (DECL_FUNCTION_CODE (fndecl))
2096 CASE_FLT_FN (BUILT_IN_SIN):
2097 builtin_optab = sin_optab; break;
2098 CASE_FLT_FN (BUILT_IN_COS):
2099 builtin_optab = cos_optab; break;
2104 /* Before working hard, check whether the instruction is available. */
2105 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2107 target = gen_reg_rtx (mode);
2109 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2110 need to expand the argument again. This way, we will not perform
2111 side-effects more the once. */
2112 narg = save_expr (arg);
2116 arglist = build_tree_list (NULL_TREE, arg);
2117 exp = build_function_call_expr (fndecl, arglist);
2120 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2124 /* Compute into TARGET.
2125 Set TARGET to wherever the result comes back. */
2126 if (builtin_optab == sincos_optab)
2130 switch (DECL_FUNCTION_CODE (fndecl))
2132 CASE_FLT_FN (BUILT_IN_SIN):
2133 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2135 CASE_FLT_FN (BUILT_IN_COS):
2136 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2141 gcc_assert (result);
2145 target = expand_unop (mode, builtin_optab, op0, target, 0);
2150 /* Output the entire sequence. */
2151 insns = get_insns ();
2157 /* If we were unable to expand via the builtin, stop the sequence
2158 (without outputting the insns) and call to the library function
2159 with the stabilized argument list. */
2163 target = expand_call (exp, target, target == const0_rtx);
2168 /* Expand a call to the builtin sincos math function.
2169 Return 0 if a normal call should be emitted rather than expanding the
2170 function in-line. EXP is the expression that is a call to the builtin
2174 expand_builtin_sincos (tree exp)
2176 rtx op0, op1, op2, target1, target2;
2177 tree arglist = TREE_OPERAND (exp, 1);
2178 enum machine_mode mode;
2179 tree arg, sinp, cosp;
2182 if (!validate_arglist (arglist, REAL_TYPE,
2183 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2186 arg = TREE_VALUE (arglist);
2187 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2188 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2190 /* Make a suitable register to place result in. */
2191 mode = TYPE_MODE (TREE_TYPE (arg));
2193 /* Check if sincos insn is available, otherwise emit the call. */
2194 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2197 target1 = gen_reg_rtx (mode);
2198 target2 = gen_reg_rtx (mode);
2200 op0 = expand_normal (arg);
2201 op1 = expand_normal (build_fold_indirect_ref (sinp));
2202 op2 = expand_normal (build_fold_indirect_ref (cosp));
2204 /* Compute into target1 and target2.
2205 Set TARGET to wherever the result comes back. */
2206 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2207 gcc_assert (result);
2209 /* Move target1 and target2 to the memory locations indicated
2211 emit_move_insn (op1, target1);
2212 emit_move_insn (op2, target2);
2217 /* Expand a call to one of the builtin rounding functions gcc defines
2218 as an extension (lfloor and lceil). As these are gcc extensions we
2219 do not need to worry about setting errno to EDOM.
2220 If expanding via optab fails, lower expression to (int)(floor(x)).
2221 EXP is the expression that is a call to the builtin function;
2222 if convenient, the result should be placed in TARGET. SUBTARGET may
2223 be used as the target for computing one of EXP's operands. */
2226 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2228 convert_optab builtin_optab;
2229 rtx op0, insns, tmp;
2230 tree fndecl = get_callee_fndecl (exp);
2231 tree arglist = TREE_OPERAND (exp, 1);
2232 enum built_in_function fallback_fn;
2233 tree fallback_fndecl;
2234 enum machine_mode mode;
2237 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2240 arg = TREE_VALUE (arglist);
2242 switch (DECL_FUNCTION_CODE (fndecl))
2244 CASE_FLT_FN (BUILT_IN_LCEIL):
2245 CASE_FLT_FN (BUILT_IN_LLCEIL):
2246 builtin_optab = lceil_optab;
2247 fallback_fn = BUILT_IN_CEIL;
2250 CASE_FLT_FN (BUILT_IN_LFLOOR):
2251 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2252 builtin_optab = lfloor_optab;
2253 fallback_fn = BUILT_IN_FLOOR;
2260 /* Make a suitable register to place result in. */
2261 mode = TYPE_MODE (TREE_TYPE (exp));
2263 target = gen_reg_rtx (mode);
2265 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2266 need to expand the argument again. This way, we will not perform
2267 side-effects more the once. */
2268 narg = builtin_save_expr (arg);
2272 arglist = build_tree_list (NULL_TREE, arg);
2273 exp = build_function_call_expr (fndecl, arglist);
2276 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2280 /* Compute into TARGET. */
2281 if (expand_sfix_optab (target, op0, builtin_optab))
2283 /* Output the entire sequence. */
2284 insns = get_insns ();
2290 /* If we were unable to expand via the builtin, stop the sequence
2291 (without outputting the insns). */
2294 /* Fall back to floating point rounding optab. */
2295 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2296 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2297 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2298 gcc_assert (fallback_fndecl != NULL_TREE);
2299 exp = build_function_call_expr (fallback_fndecl, arglist);
2301 tmp = expand_normal (exp);
2303 /* Truncate the result of floating point optab to integer
2304 via expand_fix (). */
2305 target = gen_reg_rtx (mode);
2306 expand_fix (target, tmp, 0);
2311 /* Expand a call to one of the builtin math functions doing integer
2313 Return 0 if a normal call should be emitted rather than expanding the
2314 function in-line. EXP is the expression that is a call to the builtin
2315 function; if convenient, the result should be placed in TARGET.
2316 SUBTARGET may be used as the target for computing one of EXP's operands. */
2319 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2321 convert_optab builtin_optab;
2323 tree fndecl = get_callee_fndecl (exp);
2324 tree arglist = TREE_OPERAND (exp, 1);
2325 enum machine_mode mode;
2328 /* There's no easy way to detect the case we need to set EDOM. */
2329 if (flag_errno_math)
2332 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2335 arg = TREE_VALUE (arglist);
2337 switch (DECL_FUNCTION_CODE (fndecl))
2339 CASE_FLT_FN (BUILT_IN_LRINT):
2340 CASE_FLT_FN (BUILT_IN_LLRINT):
2341 builtin_optab = lrint_optab; break;
2342 CASE_FLT_FN (BUILT_IN_LROUND):
2343 CASE_FLT_FN (BUILT_IN_LLROUND):
2344 builtin_optab = lround_optab; break;
2349 /* Make a suitable register to place result in. */
2350 mode = TYPE_MODE (TREE_TYPE (exp));
2352 target = gen_reg_rtx (mode);
2354 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2355 need to expand the argument again. This way, we will not perform
2356 side-effects more the once. */
2357 narg = builtin_save_expr (arg);
2361 arglist = build_tree_list (NULL_TREE, arg);
2362 exp = build_function_call_expr (fndecl, arglist);
2365 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2369 if (expand_sfix_optab (target, op0, builtin_optab))
2371 /* Output the entire sequence. */
2372 insns = get_insns ();
2378 /* If we were unable to expand via the builtin, stop the sequence
2379 (without outputting the insns) and call to the library function
2380 with the stabilized argument list. */
2383 target = expand_call (exp, target, target == const0_rtx);
2388 /* To evaluate powi(x,n), the floating point value x raised to the
2389 constant integer exponent n, we use a hybrid algorithm that
2390 combines the "window method" with look-up tables. For an
2391 introduction to exponentiation algorithms and "addition chains",
2392 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2393 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2394 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2395 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2397 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2398 multiplications to inline before calling the system library's pow
2399 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2400 so this default never requires calling pow, powf or powl. */
2402 #ifndef POWI_MAX_MULTS
2403 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2406 /* The size of the "optimal power tree" lookup table. All
2407 exponents less than this value are simply looked up in the
2408 powi_table below. This threshold is also used to size the
2409 cache of pseudo registers that hold intermediate results. */
2410 #define POWI_TABLE_SIZE 256
2412 /* The size, in bits of the window, used in the "window method"
2413 exponentiation algorithm. This is equivalent to a radix of
2414 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2415 #define POWI_WINDOW_SIZE 3
2417 /* The following table is an efficient representation of an
2418 "optimal power tree". For each value, i, the corresponding
2419 value, j, in the table states than an optimal evaluation
2420 sequence for calculating pow(x,i) can be found by evaluating
2421 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2422 100 integers is given in Knuth's "Seminumerical algorithms". */
2424 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2426 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2427 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2428 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2429 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2430 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2431 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2432 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2433 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2434 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2435 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2436 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2437 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2438 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2439 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2440 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2441 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2442 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2443 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2444 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2445 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2446 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2447 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2448 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2449 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2450 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2451 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2452 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2453 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2454 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2455 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2456 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2457 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2461 /* Return the number of multiplications required to calculate
2462 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2463 subroutine of powi_cost. CACHE is an array indicating
2464 which exponents have already been calculated. */
2467 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2469 /* If we've already calculated this exponent, then this evaluation
2470 doesn't require any additional multiplications. */
2475 return powi_lookup_cost (n - powi_table[n], cache)
2476 + powi_lookup_cost (powi_table[n], cache) + 1;
2479 /* Return the number of multiplications required to calculate
2480 powi(x,n) for an arbitrary x, given the exponent N. This
2481 function needs to be kept in sync with expand_powi below. */
2484 powi_cost (HOST_WIDE_INT n)
2486 bool cache[POWI_TABLE_SIZE];
2487 unsigned HOST_WIDE_INT digit;
2488 unsigned HOST_WIDE_INT val;
2494 /* Ignore the reciprocal when calculating the cost. */
2495 val = (n < 0) ? -n : n;
2497 /* Initialize the exponent cache. */
2498 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2503 while (val >= POWI_TABLE_SIZE)
2507 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2508 result += powi_lookup_cost (digit, cache)
2509 + POWI_WINDOW_SIZE + 1;
2510 val >>= POWI_WINDOW_SIZE;
2519 return result + powi_lookup_cost (val, cache);
2522 /* Recursive subroutine of expand_powi. This function takes the array,
2523 CACHE, of already calculated exponents and an exponent N and returns
2524 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2527 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2529 unsigned HOST_WIDE_INT digit;
2533 if (n < POWI_TABLE_SIZE)
2538 target = gen_reg_rtx (mode);
2541 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2542 op1 = expand_powi_1 (mode, powi_table[n], cache);
2546 target = gen_reg_rtx (mode);
2547 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2548 op0 = expand_powi_1 (mode, n - digit, cache);
2549 op1 = expand_powi_1 (mode, digit, cache);
2553 target = gen_reg_rtx (mode);
2554 op0 = expand_powi_1 (mode, n >> 1, cache);
2558 result = expand_mult (mode, op0, op1, target, 0);
2559 if (result != target)
2560 emit_move_insn (target, result);
2564 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2565 floating point operand in mode MODE, and N is the exponent. This
2566 function needs to be kept in sync with powi_cost above. */
2569 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2571 unsigned HOST_WIDE_INT val;
2572 rtx cache[POWI_TABLE_SIZE];
2576 return CONST1_RTX (mode);
2578 val = (n < 0) ? -n : n;
2580 memset (cache, 0, sizeof (cache));
2583 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2585 /* If the original exponent was negative, reciprocate the result. */
2587 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2588 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2593 /* Expand a call to the pow built-in mathematical function. Return 0 if
2594 a normal call should be emitted rather than expanding the function
2595 in-line. EXP is the expression that is a call to the builtin
2596 function; if convenient, the result should be placed in TARGET. */
2599 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2601 tree arglist = TREE_OPERAND (exp, 1);
2604 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2607 arg0 = TREE_VALUE (arglist);
2608 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2610 if (TREE_CODE (arg1) == REAL_CST
2611 && ! TREE_CONSTANT_OVERFLOW (arg1))
2613 REAL_VALUE_TYPE cint;
2617 c = TREE_REAL_CST (arg1);
2618 n = real_to_integer (&c);
2619 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2620 if (real_identical (&c, &cint))
2622 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2623 Otherwise, check the number of multiplications required.
2624 Note that pow never sets errno for an integer exponent. */
2625 if ((n >= -1 && n <= 2)
2626 || (flag_unsafe_math_optimizations
2628 && powi_cost (n) <= POWI_MAX_MULTS))
2630 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2631 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2632 op = force_reg (mode, op);
2633 return expand_powi (op, mode, n);
2638 if (! flag_unsafe_math_optimizations)
2640 return expand_builtin_mathfn_2 (exp, target, subtarget);
2643 /* Expand a call to the powi built-in mathematical function. Return 0 if
2644 a normal call should be emitted rather than expanding the function
2645 in-line. EXP is the expression that is a call to the builtin
2646 function; if convenient, the result should be placed in TARGET. */
2649 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2651 tree arglist = TREE_OPERAND (exp, 1);
2654 enum machine_mode mode;
2655 enum machine_mode mode2;
2657 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2660 arg0 = TREE_VALUE (arglist);
2661 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2662 mode = TYPE_MODE (TREE_TYPE (exp));
2664 /* Handle constant power. */
2666 if (TREE_CODE (arg1) == INTEGER_CST
2667 && ! TREE_CONSTANT_OVERFLOW (arg1))
2669 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2671 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2672 Otherwise, check the number of multiplications required. */
2673 if ((TREE_INT_CST_HIGH (arg1) == 0
2674 || TREE_INT_CST_HIGH (arg1) == -1)
2675 && ((n >= -1 && n <= 2)
2677 && powi_cost (n) <= POWI_MAX_MULTS)))
2679 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2680 op0 = force_reg (mode, op0);
2681 return expand_powi (op0, mode, n);
2685 /* Emit a libcall to libgcc. */
2687 /* Mode of the 2nd argument must match that of an int. */
2688 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2690 if (target == NULL_RTX)
2691 target = gen_reg_rtx (mode);
2693 op0 = expand_expr (arg0, subtarget, mode, 0);
2694 if (GET_MODE (op0) != mode)
2695 op0 = convert_to_mode (mode, op0, 0);
2696 op1 = expand_expr (arg1, 0, mode2, 0);
2697 if (GET_MODE (op1) != mode2)
2698 op1 = convert_to_mode (mode2, op1, 0);
2700 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2701 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2702 op0, mode, op1, mode2);
2707 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2708 if we failed the caller should emit a normal call, otherwise
2709 try to get the result in TARGET, if convenient. */
2712 expand_builtin_strlen (tree arglist, rtx target,
2713 enum machine_mode target_mode)
2715 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2720 tree len, src = TREE_VALUE (arglist);
2721 rtx result, src_reg, char_rtx, before_strlen;
2722 enum machine_mode insn_mode = target_mode, char_mode;
2723 enum insn_code icode = CODE_FOR_nothing;
2726 /* If the length can be computed at compile-time, return it. */
2727 len = c_strlen (src, 0);
2729 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2731 /* If the length can be computed at compile-time and is constant
2732 integer, but there are side-effects in src, evaluate
2733 src for side-effects, then return len.
2734 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2735 can be optimized into: i++; x = 3; */
2736 len = c_strlen (src, 1);
2737 if (len && TREE_CODE (len) == INTEGER_CST)
2739 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2740 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2743 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2745 /* If SRC is not a pointer type, don't do this operation inline. */
2749 /* Bail out if we can't compute strlen in the right mode. */
2750 while (insn_mode != VOIDmode)
2752 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2753 if (icode != CODE_FOR_nothing)
2756 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2758 if (insn_mode == VOIDmode)
2761 /* Make a place to write the result of the instruction. */
2765 && GET_MODE (result) == insn_mode
2766 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2767 result = gen_reg_rtx (insn_mode);
2769 /* Make a place to hold the source address. We will not expand
2770 the actual source until we are sure that the expansion will
2771 not fail -- there are trees that cannot be expanded twice. */
2772 src_reg = gen_reg_rtx (Pmode);
2774 /* Mark the beginning of the strlen sequence so we can emit the
2775 source operand later. */
2776 before_strlen = get_last_insn ();
2778 char_rtx = const0_rtx;
2779 char_mode = insn_data[(int) icode].operand[2].mode;
2780 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2782 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2784 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2785 char_rtx, GEN_INT (align));
2790 /* Now that we are assured of success, expand the source. */
2792 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2794 emit_move_insn (src_reg, pat);
2799 emit_insn_after (pat, before_strlen);
2801 emit_insn_before (pat, get_insns ());
2803 /* Return the value in the proper mode for this function. */
2804 if (GET_MODE (result) == target_mode)
2806 else if (target != 0)
2807 convert_move (target, result, 0);
2809 target = convert_to_mode (target_mode, result, 0);
2815 /* Expand a call to the strstr builtin. Return 0 if we failed the
2816 caller should emit a normal call, otherwise try to get the result
2817 in TARGET, if convenient (and in mode MODE if that's convenient). */
2820 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2822 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2824 tree result = fold_builtin_strstr (arglist, type);
2826 return expand_expr (result, target, mode, EXPAND_NORMAL);
2831 /* Expand a call to the strchr builtin. Return 0 if we failed the
2832 caller should emit a normal call, otherwise try to get the result
2833 in TARGET, if convenient (and in mode MODE if that's convenient). */
2836 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2838 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2840 tree result = fold_builtin_strchr (arglist, type);
2842 return expand_expr (result, target, mode, EXPAND_NORMAL);
2844 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2849 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2850 caller should emit a normal call, otherwise try to get the result
2851 in TARGET, if convenient (and in mode MODE if that's convenient). */
2854 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2856 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2858 tree result = fold_builtin_strrchr (arglist, type);
2860 return expand_expr (result, target, mode, EXPAND_NORMAL);
2865 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2866 caller should emit a normal call, otherwise try to get the result
2867 in TARGET, if convenient (and in mode MODE if that's convenient). */
2870 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2872 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2874 tree result = fold_builtin_strpbrk (arglist, type);
2876 return expand_expr (result, target, mode, EXPAND_NORMAL);
2881 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2882 bytes from constant string DATA + OFFSET and return it as target
2886 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2887 enum machine_mode mode)
2889 const char *str = (const char *) data;
2891 gcc_assert (offset >= 0
2892 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2893 <= strlen (str) + 1));
2895 return c_readstr (str + offset, mode);
2898 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2899 Return 0 if we failed, the caller should emit a normal call,
2900 otherwise try to get the result in TARGET, if convenient (and in
2901 mode MODE if that's convenient). */
2903 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2905 tree fndecl = get_callee_fndecl (exp);
2906 tree arglist = TREE_OPERAND (exp, 1);
2907 if (!validate_arglist (arglist,
2908 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2912 tree dest = TREE_VALUE (arglist);
2913 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2914 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2915 const char *src_str;
2916 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2917 unsigned int dest_align
2918 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2919 rtx dest_mem, src_mem, dest_addr, len_rtx;
2920 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
2925 while (TREE_CODE (result) == COMPOUND_EXPR)
2927 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2929 result = TREE_OPERAND (result, 1);
2931 return expand_expr (result, target, mode, EXPAND_NORMAL);
2934 /* If DEST is not a pointer type, call the normal function. */
2935 if (dest_align == 0)
2938 /* If either SRC is not a pointer type, don't do this
2939 operation in-line. */
2943 dest_mem = get_memory_rtx (dest, len);
2944 set_mem_align (dest_mem, dest_align);
2945 len_rtx = expand_normal (len);
2946 src_str = c_getstr (src);
2948 /* If SRC is a string constant and block move would be done
2949 by pieces, we can avoid loading the string from memory
2950 and only stored the computed constants. */
2952 && GET_CODE (len_rtx) == CONST_INT
2953 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2954 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2955 (void *) src_str, dest_align))
2957 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2958 builtin_memcpy_read_str,
2959 (void *) src_str, dest_align, 0);
2960 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2961 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2965 src_mem = get_memory_rtx (src, len);
2966 set_mem_align (src_mem, src_align);
2968 /* Copy word part most expediently. */
2969 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2970 CALL_EXPR_TAILCALL (exp)
2971 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2975 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2976 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2982 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2983 Return 0 if we failed; the caller should emit a normal call,
2984 otherwise try to get the result in TARGET, if convenient (and in
2985 mode MODE if that's convenient). If ENDP is 0 return the
2986 destination pointer, if ENDP is 1 return the end pointer ala
2987 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2991 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2994 if (!validate_arglist (arglist,
2995 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2997 /* If return value is ignored, transform mempcpy into memcpy. */
2998 else if (target == const0_rtx)
3000 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3005 return expand_expr (build_function_call_expr (fn, arglist),
3006 target, mode, EXPAND_NORMAL);
3010 tree dest = TREE_VALUE (arglist);
3011 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3012 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3013 const char *src_str;
3014 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3015 unsigned int dest_align
3016 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3017 rtx dest_mem, src_mem, len_rtx;
3018 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3022 while (TREE_CODE (result) == COMPOUND_EXPR)
3024 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3026 result = TREE_OPERAND (result, 1);
3028 return expand_expr (result, target, mode, EXPAND_NORMAL);
3031 /* If either SRC or DEST is not a pointer type, don't do this
3032 operation in-line. */
3033 if (dest_align == 0 || src_align == 0)
3036 /* If LEN is not constant, call the normal function. */
3037 if (! host_integerp (len, 1))
3040 len_rtx = expand_normal (len);
3041 src_str = c_getstr (src);
3043 /* If SRC is a string constant and block move would be done
3044 by pieces, we can avoid loading the string from memory
3045 and only stored the computed constants. */
3047 && GET_CODE (len_rtx) == CONST_INT
3048 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3049 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3050 (void *) src_str, dest_align))
3052 dest_mem = get_memory_rtx (dest, len);
3053 set_mem_align (dest_mem, dest_align);
3054 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3055 builtin_memcpy_read_str,
3056 (void *) src_str, dest_align, endp);
3057 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3058 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3062 if (GET_CODE (len_rtx) == CONST_INT
3063 && can_move_by_pieces (INTVAL (len_rtx),
3064 MIN (dest_align, src_align)))
3066 dest_mem = get_memory_rtx (dest, len);
3067 set_mem_align (dest_mem, dest_align);
3068 src_mem = get_memory_rtx (src, len);
3069 set_mem_align (src_mem, src_align);
3070 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3071 MIN (dest_align, src_align), endp);
3072 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3073 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3081 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3082 if we failed; the caller should emit a normal call. */
3085 expand_builtin_memmove (tree arglist, tree type, rtx target,
3086 enum machine_mode mode)
3088 if (!validate_arglist (arglist,
3089 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3093 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3097 while (TREE_CODE (result) == COMPOUND_EXPR)
3099 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3101 result = TREE_OPERAND (result, 1);
3103 return expand_expr (result, target, mode, EXPAND_NORMAL);
3106 /* Otherwise, call the normal function. */
3111 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3112 if we failed the caller should emit a normal call. */
3115 expand_builtin_bcopy (tree exp)
3117 tree arglist = TREE_OPERAND (exp, 1);
3118 tree type = TREE_TYPE (exp);
3119 tree src, dest, size, newarglist;
3121 if (!validate_arglist (arglist,
3122 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3125 src = TREE_VALUE (arglist);
3126 dest = TREE_VALUE (TREE_CHAIN (arglist));
3127 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3129 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3130 memmove(ptr y, ptr x, size_t z). This is done this way
3131 so that if it isn't expanded inline, we fallback to
3132 calling bcopy instead of memmove. */
3134 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3135 newarglist = tree_cons (NULL_TREE, src, newarglist);
3136 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3138 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3142 # define HAVE_movstr 0
3143 # define CODE_FOR_movstr CODE_FOR_nothing
3146 /* Expand into a movstr instruction, if one is available. Return 0 if
3147 we failed, the caller should emit a normal call, otherwise try to
3148 get the result in TARGET, if convenient. If ENDP is 0 return the
3149 destination pointer, if ENDP is 1 return the end pointer ala
3150 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3154 expand_movstr (tree dest, tree src, rtx target, int endp)
3160 const struct insn_data * data;
3165 dest_mem = get_memory_rtx (dest, NULL);
3166 src_mem = get_memory_rtx (src, NULL);
3169 target = force_reg (Pmode, XEXP (dest_mem, 0));
3170 dest_mem = replace_equiv_address (dest_mem, target);
3171 end = gen_reg_rtx (Pmode);
3175 if (target == 0 || target == const0_rtx)
3177 end = gen_reg_rtx (Pmode);
3185 data = insn_data + CODE_FOR_movstr;
3187 if (data->operand[0].mode != VOIDmode)
3188 end = gen_lowpart (data->operand[0].mode, end);
3190 insn = data->genfun (end, dest_mem, src_mem);
3196 /* movstr is supposed to set end to the address of the NUL
3197 terminator. If the caller requested a mempcpy-like return value,
3199 if (endp == 1 && target != const0_rtx)
3201 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3202 emit_move_insn (target, force_operand (tem, NULL_RTX));
3208 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3209 if we failed the caller should emit a normal call, otherwise try to get
3210 the result in TARGET, if convenient (and in mode MODE if that's
3214 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3216 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3218 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3221 while (TREE_CODE (result) == COMPOUND_EXPR)
3223 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3225 result = TREE_OPERAND (result, 1);
3227 return expand_expr (result, target, mode, EXPAND_NORMAL);
3230 return expand_movstr (TREE_VALUE (arglist),
3231 TREE_VALUE (TREE_CHAIN (arglist)),
3232 target, /*endp=*/0);
3237 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3238 Return 0 if we failed the caller should emit a normal call,
3239 otherwise try to get the result in TARGET, if convenient (and in
3240 mode MODE if that's convenient). */
3243 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3245 tree arglist = TREE_OPERAND (exp, 1);
3246 /* If return value is ignored, transform stpcpy into strcpy. */
3247 if (target == const0_rtx)
3249 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3253 return expand_expr (build_function_call_expr (fn, arglist),
3254 target, mode, EXPAND_NORMAL);
3257 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3261 tree dst, src, len, lenp1;
3265 /* Ensure we get an actual string whose length can be evaluated at
3266 compile-time, not an expression containing a string. This is
3267 because the latter will potentially produce pessimized code
3268 when used to produce the return value. */
3269 src = TREE_VALUE (TREE_CHAIN (arglist));
3270 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3271 return expand_movstr (TREE_VALUE (arglist),
3272 TREE_VALUE (TREE_CHAIN (arglist)),
3273 target, /*endp=*/2);
3275 dst = TREE_VALUE (arglist);
3276 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3277 narglist = build_tree_list (NULL_TREE, lenp1);
3278 narglist = tree_cons (NULL_TREE, src, narglist);
3279 narglist = tree_cons (NULL_TREE, dst, narglist);
3280 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3281 target, mode, /*endp=*/2);
3286 if (TREE_CODE (len) == INTEGER_CST)
3288 rtx len_rtx = expand_normal (len);
3290 if (GET_CODE (len_rtx) == CONST_INT)
3292 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3293 arglist, target, mode);
3299 if (mode != VOIDmode)
3300 target = gen_reg_rtx (mode);
3302 target = gen_reg_rtx (GET_MODE (ret));
3304 if (GET_MODE (target) != GET_MODE (ret))
3305 ret = gen_lowpart (GET_MODE (target), ret);
3307 ret = plus_constant (ret, INTVAL (len_rtx));
3308 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3316 return expand_movstr (TREE_VALUE (arglist),
3317 TREE_VALUE (TREE_CHAIN (arglist)),
3318 target, /*endp=*/2);
3322 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3323 bytes from constant string DATA + OFFSET and return it as target
3327 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3328 enum machine_mode mode)
3330 const char *str = (const char *) data;
3332 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3335 return c_readstr (str + offset, mode);
3338 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3339 if we failed the caller should emit a normal call. */
3342 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3344 tree fndecl = get_callee_fndecl (exp);
3345 tree arglist = TREE_OPERAND (exp, 1);
3346 if (validate_arglist (arglist,
3347 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3349 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3350 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3351 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3355 while (TREE_CODE (result) == COMPOUND_EXPR)
3357 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3359 result = TREE_OPERAND (result, 1);
3361 return expand_expr (result, target, mode, EXPAND_NORMAL);
3364 /* We must be passed a constant len and src parameter. */
3365 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3368 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3370 /* We're required to pad with trailing zeros if the requested
3371 len is greater than strlen(s2)+1. In that case try to
3372 use store_by_pieces, if it fails, punt. */
3373 if (tree_int_cst_lt (slen, len))
3375 tree dest = TREE_VALUE (arglist);
3376 unsigned int dest_align
3377 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3378 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3381 if (!p || dest_align == 0 || !host_integerp (len, 1)
3382 || !can_store_by_pieces (tree_low_cst (len, 1),
3383 builtin_strncpy_read_str,
3384 (void *) p, dest_align))
3387 dest_mem = get_memory_rtx (dest, len);
3388 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3389 builtin_strncpy_read_str,
3390 (void *) p, dest_align, 0);
3391 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3392 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3399 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3400 bytes from constant string DATA + OFFSET and return it as target
3404 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3405 enum machine_mode mode)
3407 const char *c = (const char *) data;
3408 char *p = alloca (GET_MODE_SIZE (mode));
3410 memset (p, *c, GET_MODE_SIZE (mode));
3412 return c_readstr (p, mode);
3415 /* Callback routine for store_by_pieces. Return the RTL of a register
3416 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3417 char value given in the RTL register data. For example, if mode is
3418 4 bytes wide, return the RTL for 0x01010101*data. */
3421 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3422 enum machine_mode mode)
3428 size = GET_MODE_SIZE (mode);
3433 memset (p, 1, size);
3434 coeff = c_readstr (p, mode);
3436 target = convert_to_mode (mode, (rtx) data, 1);
3437 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3438 return force_reg (mode, target);
3441 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3442 if we failed the caller should emit a normal call, otherwise try to get
3443 the result in TARGET, if convenient (and in mode MODE if that's
3447 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3450 if (!validate_arglist (arglist,
3451 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3455 tree dest = TREE_VALUE (arglist);
3456 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3457 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3459 enum built_in_function fcode;
3461 unsigned int dest_align;
3462 rtx dest_mem, dest_addr, len_rtx;
3464 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3466 /* If DEST is not a pointer type, don't do this
3467 operation in-line. */
3468 if (dest_align == 0)
3471 /* If the LEN parameter is zero, return DEST. */
3472 if (integer_zerop (len))
3474 /* Evaluate and ignore VAL in case it has side-effects. */
3475 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3476 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3479 /* Stabilize the arguments in case we fail. */
3480 dest = builtin_save_expr (dest);
3481 val = builtin_save_expr (val);
3482 len = builtin_save_expr (len);
3484 len_rtx = expand_normal (len);
3485 dest_mem = get_memory_rtx (dest, len);
3487 if (TREE_CODE (val) != INTEGER_CST)
3491 val_rtx = expand_normal (val);
3492 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3495 /* Assume that we can memset by pieces if we can store the
3496 * the coefficients by pieces (in the required modes).
3497 * We can't pass builtin_memset_gen_str as that emits RTL. */
3499 if (host_integerp (len, 1)
3500 && !(optimize_size && tree_low_cst (len, 1) > 1)
3501 && can_store_by_pieces (tree_low_cst (len, 1),
3502 builtin_memset_read_str, &c, dest_align))
3504 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3506 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3507 builtin_memset_gen_str, val_rtx, dest_align, 0);
3509 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3513 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3514 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3518 if (target_char_cast (val, &c))
3523 if (host_integerp (len, 1)
3524 && !(optimize_size && tree_low_cst (len, 1) > 1)
3525 && can_store_by_pieces (tree_low_cst (len, 1),
3526 builtin_memset_read_str, &c, dest_align))
3527 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3528 builtin_memset_read_str, &c, dest_align, 0);
3529 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3533 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3534 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3538 set_mem_align (dest_mem, dest_align);
3539 dest_addr = clear_storage (dest_mem, len_rtx,
3540 CALL_EXPR_TAILCALL (orig_exp)
3541 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3545 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3546 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3552 fndecl = get_callee_fndecl (orig_exp);
3553 fcode = DECL_FUNCTION_CODE (fndecl);
3554 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3555 arglist = build_tree_list (NULL_TREE, len);
3556 if (fcode == BUILT_IN_MEMSET)
3557 arglist = tree_cons (NULL_TREE, val, arglist);
3558 arglist = tree_cons (NULL_TREE, dest, arglist);
3559 fn = build_function_call_expr (fndecl, arglist);
3560 if (TREE_CODE (fn) == CALL_EXPR)
3561 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3562 return expand_call (fn, target, target == const0_rtx);
3566 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3567 if we failed the caller should emit a normal call. */
3570 expand_builtin_bzero (tree exp)
3572 tree arglist = TREE_OPERAND (exp, 1);
3573 tree dest, size, newarglist;
3575 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3578 dest = TREE_VALUE (arglist);
3579 size = TREE_VALUE (TREE_CHAIN (arglist));
3581 /* New argument list transforming bzero(ptr x, int y) to
3582 memset(ptr x, int 0, size_t y). This is done this way
3583 so that if it isn't expanded inline, we fallback to
3584 calling bzero instead of memset. */
3586 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3587 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3588 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3590 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3593 /* Expand expression EXP, which is a call to the memcmp built-in function.
3594 ARGLIST is the argument list for this call. Return 0 if we failed and the
3595 caller should emit a normal call, otherwise try to get the result in
3596 TARGET, if convenient (and in mode MODE, if that's convenient). */
3599 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3600 enum machine_mode mode)
3602 if (!validate_arglist (arglist,
3603 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3607 tree result = fold_builtin_memcmp (arglist);
3609 return expand_expr (result, target, mode, EXPAND_NORMAL);
3612 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3614 tree arg1 = TREE_VALUE (arglist);
3615 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3616 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3617 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3622 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3624 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3625 enum machine_mode insn_mode;
3627 #ifdef HAVE_cmpmemsi
3629 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3632 #ifdef HAVE_cmpstrnsi
3634 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3639 /* If we don't have POINTER_TYPE, call the function. */
3640 if (arg1_align == 0 || arg2_align == 0)
3643 /* Make a place to write the result of the instruction. */
3646 && REG_P (result) && GET_MODE (result) == insn_mode
3647 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3648 result = gen_reg_rtx (insn_mode);
3650 arg1_rtx = get_memory_rtx (arg1, len);
3651 arg2_rtx = get_memory_rtx (arg2, len);
3652 arg3_rtx = expand_normal (len);
3654 /* Set MEM_SIZE as appropriate. */
3655 if (GET_CODE (arg3_rtx) == CONST_INT)
3657 set_mem_size (arg1_rtx, arg3_rtx);
3658 set_mem_size (arg2_rtx, arg3_rtx);
3661 #ifdef HAVE_cmpmemsi
3663 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3664 GEN_INT (MIN (arg1_align, arg2_align)));
3667 #ifdef HAVE_cmpstrnsi
3669 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3670 GEN_INT (MIN (arg1_align, arg2_align)));
3678 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3679 TYPE_MODE (integer_type_node), 3,
3680 XEXP (arg1_rtx, 0), Pmode,
3681 XEXP (arg2_rtx, 0), Pmode,
3682 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3683 TYPE_UNSIGNED (sizetype)),
3684 TYPE_MODE (sizetype));
3686 /* Return the value in the proper mode for this function. */
3687 mode = TYPE_MODE (TREE_TYPE (exp));
3688 if (GET_MODE (result) == mode)
3690 else if (target != 0)
3692 convert_move (target, result, 0);
3696 return convert_to_mode (mode, result, 0);
3703 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3704 if we failed the caller should emit a normal call, otherwise try to get
3705 the result in TARGET, if convenient. */
3708 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3710 tree arglist = TREE_OPERAND (exp, 1);
3712 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3716 tree result = fold_builtin_strcmp (arglist);
3718 return expand_expr (result, target, mode, EXPAND_NORMAL);
3721 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3722 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3723 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3725 rtx arg1_rtx, arg2_rtx;
3726 rtx result, insn = NULL_RTX;
3729 tree arg1 = TREE_VALUE (arglist);
3730 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3732 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3734 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3736 /* If we don't have POINTER_TYPE, call the function. */
3737 if (arg1_align == 0 || arg2_align == 0)
3740 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3741 arg1 = builtin_save_expr (arg1);
3742 arg2 = builtin_save_expr (arg2);
3744 arg1_rtx = get_memory_rtx (arg1, NULL);
3745 arg2_rtx = get_memory_rtx (arg2, NULL);
3747 #ifdef HAVE_cmpstrsi
3748 /* Try to call cmpstrsi. */
3751 enum machine_mode insn_mode
3752 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3754 /* Make a place to write the result of the instruction. */
3757 && REG_P (result) && GET_MODE (result) == insn_mode
3758 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3759 result = gen_reg_rtx (insn_mode);
3761 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3762 GEN_INT (MIN (arg1_align, arg2_align)));
3765 #ifdef HAVE_cmpstrnsi
3766 /* Try to determine at least one length and call cmpstrnsi. */
3767 if (!insn && HAVE_cmpstrnsi)
3772 enum machine_mode insn_mode
3773 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3774 tree len1 = c_strlen (arg1, 1);
3775 tree len2 = c_strlen (arg2, 1);
3778 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3780 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3782 /* If we don't have a constant length for the first, use the length
3783 of the second, if we know it. We don't require a constant for
3784 this case; some cost analysis could be done if both are available
3785 but neither is constant. For now, assume they're equally cheap,
3786 unless one has side effects. If both strings have constant lengths,
3793 else if (TREE_SIDE_EFFECTS (len1))
3795 else if (TREE_SIDE_EFFECTS (len2))
3797 else if (TREE_CODE (len1) != INTEGER_CST)
3799 else if (TREE_CODE (len2) != INTEGER_CST)
3801 else if (tree_int_cst_lt (len1, len2))
3806 /* If both arguments have side effects, we cannot optimize. */
3807 if (!len || TREE_SIDE_EFFECTS (len))
3810 arg3_rtx = expand_normal (len);
3812 /* Make a place to write the result of the instruction. */
3815 && REG_P (result) && GET_MODE (result) == insn_mode
3816 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3817 result = gen_reg_rtx (insn_mode);
3819 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3820 GEN_INT (MIN (arg1_align, arg2_align)));
3828 /* Return the value in the proper mode for this function. */
3829 mode = TYPE_MODE (TREE_TYPE (exp));
3830 if (GET_MODE (result) == mode)
3833 return convert_to_mode (mode, result, 0);
3834 convert_move (target, result, 0);
3838 /* Expand the library call ourselves using a stabilized argument
3839 list to avoid re-evaluating the function's arguments twice. */
3840 #ifdef HAVE_cmpstrnsi
3843 arglist = build_tree_list (NULL_TREE, arg2);
3844 arglist = tree_cons (NULL_TREE, arg1, arglist);
3845 fndecl = get_callee_fndecl (exp);
3846 fn = build_function_call_expr (fndecl, arglist);
3847 if (TREE_CODE (fn) == CALL_EXPR)
3848 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3849 return expand_call (fn, target, target == const0_rtx);
3855 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3856 if we failed the caller should emit a normal call, otherwise try to get
3857 the result in TARGET, if convenient. */
3860 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3862 tree arglist = TREE_OPERAND (exp, 1);
3864 if (!validate_arglist (arglist,
3865 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3869 tree result = fold_builtin_strncmp (arglist);
3871 return expand_expr (result, target, mode, EXPAND_NORMAL);
3874 /* If c_strlen can determine an expression for one of the string
3875 lengths, and it doesn't have side effects, then emit cmpstrnsi
3876 using length MIN(strlen(string)+1, arg3). */
3877 #ifdef HAVE_cmpstrnsi
3880 tree arg1 = TREE_VALUE (arglist);
3881 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3882 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3883 tree len, len1, len2;
3884 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3889 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3891 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3892 enum machine_mode insn_mode
3893 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3895 len1 = c_strlen (arg1, 1);
3896 len2 = c_strlen (arg2, 1);
3899 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3901 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3903 /* If we don't have a constant length for the first, use the length
3904 of the second, if we know it. We don't require a constant for
3905 this case; some cost analysis could be done if both are available
3906 but neither is constant. For now, assume they're equally cheap,
3907 unless one has side effects. If both strings have constant lengths,
3914 else if (TREE_SIDE_EFFECTS (len1))
3916 else if (TREE_SIDE_EFFECTS (len2))
3918 else if (TREE_CODE (len1) != INTEGER_CST)
3920 else if (TREE_CODE (len2) != INTEGER_CST)
3922 else if (tree_int_cst_lt (len1, len2))
3927 /* If both arguments have side effects, we cannot optimize. */
3928 if (!len || TREE_SIDE_EFFECTS (len))
3931 /* The actual new length parameter is MIN(len,arg3). */
3932 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
3933 fold_convert (TREE_TYPE (len), arg3));
3935 /* If we don't have POINTER_TYPE, call the function. */
3936 if (arg1_align == 0 || arg2_align == 0)
3939 /* Make a place to write the result of the instruction. */
3942 && REG_P (result) && GET_MODE (result) == insn_mode
3943 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3944 result = gen_reg_rtx (insn_mode);
3946 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3947 arg1 = builtin_save_expr (arg1);
3948 arg2 = builtin_save_expr (arg2);
3949 len = builtin_save_expr (len);
3951 arg1_rtx = get_memory_rtx (arg1, len);
3952 arg2_rtx = get_memory_rtx (arg2, len);
3953 arg3_rtx = expand_normal (len);
3954 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3955 GEN_INT (MIN (arg1_align, arg2_align)));
3960 /* Return the value in the proper mode for this function. */
3961 mode = TYPE_MODE (TREE_TYPE (exp));
3962 if (GET_MODE (result) == mode)
3965 return convert_to_mode (mode, result, 0);
3966 convert_move (target, result, 0);
3970 /* Expand the library call ourselves using a stabilized argument
3971 list to avoid re-evaluating the function's arguments twice. */
3972 arglist = build_tree_list (NULL_TREE, len);
3973 arglist = tree_cons (NULL_TREE, arg2, arglist);
3974 arglist = tree_cons (NULL_TREE, arg1, arglist);
3975 fndecl = get_callee_fndecl (exp);
3976 fn = build_function_call_expr (fndecl, arglist);
3977 if (TREE_CODE (fn) == CALL_EXPR)
3978 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3979 return expand_call (fn, target, target == const0_rtx);
3985 /* Expand expression EXP, which is a call to the strcat builtin.
3986 Return 0 if we failed the caller should emit a normal call,
3987 otherwise try to get the result in TARGET, if convenient. */
3990 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3992 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3996 tree dst = TREE_VALUE (arglist),
3997 src = TREE_VALUE (TREE_CHAIN (arglist));
3998 const char *p = c_getstr (src);
4000 /* If the string length is zero, return the dst parameter. */
4001 if (p && *p == '\0')
4002 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4006 /* See if we can store by pieces into (dst + strlen(dst)). */
4007 tree newsrc, newdst,
4008 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4011 /* Stabilize the argument list. */
4012 newsrc = builtin_save_expr (src);
4014 arglist = build_tree_list (NULL_TREE, newsrc);
4016 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4018 dst = builtin_save_expr (dst);
4022 /* Create strlen (dst). */
4024 build_function_call_expr (strlen_fn,
4025 build_tree_list (NULL_TREE, dst));
4026 /* Create (dst + (cast) strlen (dst)). */
4027 newdst = fold_convert (TREE_TYPE (dst), newdst);
4028 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4030 newdst = builtin_save_expr (newdst);
4031 arglist = tree_cons (NULL_TREE, newdst, arglist);
4033 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4035 end_sequence (); /* Stop sequence. */
4039 /* Output the entire sequence. */
4040 insns = get_insns ();
4044 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4051 /* Expand expression EXP, which is a call to the strncat builtin.
4052 Return 0 if we failed the caller should emit a normal call,
4053 otherwise try to get the result in TARGET, if convenient. */
4056 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4058 if (validate_arglist (arglist,
4059 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4061 tree result = fold_builtin_strncat (arglist);
4063 return expand_expr (result, target, mode, EXPAND_NORMAL);
4068 /* Expand expression EXP, which is a call to the strspn builtin.
4069 Return 0 if we failed the caller should emit a normal call,
4070 otherwise try to get the result in TARGET, if convenient. */
4073 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4075 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4077 tree result = fold_builtin_strspn (arglist);
4079 return expand_expr (result, target, mode, EXPAND_NORMAL);
4084 /* Expand expression EXP, which is a call to the strcspn builtin.
4085 Return 0 if we failed the caller should emit a normal call,
4086 otherwise try to get the result in TARGET, if convenient. */
4089 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4091 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4093 tree result = fold_builtin_strcspn (arglist);
4095 return expand_expr (result, target, mode, EXPAND_NORMAL);
4100 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4101 if that's convenient. */
4104 expand_builtin_saveregs (void)
4108 /* Don't do __builtin_saveregs more than once in a function.
4109 Save the result of the first call and reuse it. */
4110 if (saveregs_value != 0)
4111 return saveregs_value;
4113 /* When this function is called, it means that registers must be
4114 saved on entry to this function. So we migrate the call to the
4115 first insn of this function. */
4119 /* Do whatever the machine needs done in this case. */
4120 val = targetm.calls.expand_builtin_saveregs ();
4125 saveregs_value = val;
4127 /* Put the insns after the NOTE that starts the function. If this
4128 is inside a start_sequence, make the outer-level insn chain current, so
4129 the code is placed at the start of the function. */
4130 push_topmost_sequence ();
4131 emit_insn_after (seq, entry_of_function ());
4132 pop_topmost_sequence ();
4137 /* __builtin_args_info (N) returns word N of the arg space info
4138 for the current function. The number and meanings of words
4139 is controlled by the definition of CUMULATIVE_ARGS. */
4142 expand_builtin_args_info (tree arglist)
4144 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4145 int *word_ptr = (int *) ¤t_function_args_info;
4147 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4151 if (!host_integerp (TREE_VALUE (arglist), 0))
4152 error ("argument of %<__builtin_args_info%> must be constant");
4155 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4157 if (wordnum < 0 || wordnum >= nwords)
4158 error ("argument of %<__builtin_args_info%> out of range");
4160 return GEN_INT (word_ptr[wordnum]);
4164 error ("missing argument in %<__builtin_args_info%>");
4169 /* Expand a call to __builtin_next_arg. */
4172 expand_builtin_next_arg (void)
4174 /* Checking arguments is already done in fold_builtin_next_arg
4175 that must be called before this function. */
4176 return expand_binop (Pmode, add_optab,
4177 current_function_internal_arg_pointer,
4178 current_function_arg_offset_rtx,
4179 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4182 /* Make it easier for the backends by protecting the valist argument
4183 from multiple evaluations. */
4186 stabilize_va_list (tree valist, int needs_lvalue)
4188 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4190 if (TREE_SIDE_EFFECTS (valist))
4191 valist = save_expr (valist);
4193 /* For this case, the backends will be expecting a pointer to
4194 TREE_TYPE (va_list_type_node), but it's possible we've
4195 actually been given an array (an actual va_list_type_node).
4197 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4199 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4200 valist = build_fold_addr_expr_with_type (valist, p1);
4209 if (! TREE_SIDE_EFFECTS (valist))
4212 pt = build_pointer_type (va_list_type_node);
4213 valist = fold_build1 (ADDR_EXPR, pt, valist);
4214 TREE_SIDE_EFFECTS (valist) = 1;
4217 if (TREE_SIDE_EFFECTS (valist))
4218 valist = save_expr (valist);
4219 valist = build_fold_indirect_ref (valist);
4225 /* The "standard" definition of va_list is void*. */
4228 std_build_builtin_va_list (void)
4230 return ptr_type_node;
4233 /* The "standard" implementation of va_start: just assign `nextarg' to
4237 std_expand_builtin_va_start (tree valist, rtx nextarg)
4241 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4242 make_tree (ptr_type_node, nextarg));
4243 TREE_SIDE_EFFECTS (t) = 1;
4245 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4248 /* Expand ARGLIST, from a call to __builtin_va_start. */
4251 expand_builtin_va_start (tree arglist)
4256 chain = TREE_CHAIN (arglist);
4260 error ("too few arguments to function %<va_start%>");
4264 if (fold_builtin_next_arg (chain))
4267 nextarg = expand_builtin_next_arg ();
4268 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4270 #ifdef EXPAND_BUILTIN_VA_START
4271 EXPAND_BUILTIN_VA_START (valist, nextarg);
4273 std_expand_builtin_va_start (valist, nextarg);
4279 /* The "standard" implementation of va_arg: read the value from the
4280 current (padded) address and increment by the (padded) size. */
4283 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4285 tree addr, t, type_size, rounded_size, valist_tmp;
4286 unsigned HOST_WIDE_INT align, boundary;
4289 #ifdef ARGS_GROW_DOWNWARD
4290 /* All of the alignment and movement below is for args-grow-up machines.
4291 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4292 implement their own specialized gimplify_va_arg_expr routines. */
4296 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4298 type = build_pointer_type (type);
4300 align = PARM_BOUNDARY / BITS_PER_UNIT;
4301 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4303 /* Hoist the valist value into a temporary for the moment. */
4304 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4306 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4307 requires greater alignment, we must perform dynamic alignment. */
4308 if (boundary > align
4309 && !integer_zerop (TYPE_SIZE (type)))
4311 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4312 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4313 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4314 gimplify_and_add (t, pre_p);
4316 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4317 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4318 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4319 gimplify_and_add (t, pre_p);
4324 /* If the actual alignment is less than the alignment of the type,
4325 adjust the type accordingly so that we don't assume strict alignment
4326 when deferencing the pointer. */
4327 boundary *= BITS_PER_UNIT;
4328 if (boundary < TYPE_ALIGN (type))
4330 type = build_variant_type_copy (type);
4331 TYPE_ALIGN (type) = boundary;
4334 /* Compute the rounded size of the type. */
4335 type_size = size_in_bytes (type);
4336 rounded_size = round_up (type_size, align);
4338 /* Reduce rounded_size so it's sharable with the postqueue. */
4339 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4343 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4345 /* Small args are padded downward. */
4346 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4347 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4348 size_binop (MINUS_EXPR, rounded_size, type_size));
4349 t = fold_convert (TREE_TYPE (addr), t);
4350 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4353 /* Compute new value for AP. */
4354 t = fold_convert (TREE_TYPE (valist), rounded_size);
4355 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4356 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4357 gimplify_and_add (t, pre_p);
4359 addr = fold_convert (build_pointer_type (type), addr);
4362 addr = build_va_arg_indirect_ref (addr);
4364 return build_va_arg_indirect_ref (addr);
4367 /* Build an indirect-ref expression over the given TREE, which represents a
4368 piece of a va_arg() expansion. */
4370 build_va_arg_indirect_ref (tree addr)
4372 addr = build_fold_indirect_ref (addr);
4374 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4380 /* Return a dummy expression of type TYPE in order to keep going after an
4384 dummy_object (tree type)
4386 tree t = build_int_cst (build_pointer_type (type), 0);
4387 return build1 (INDIRECT_REF, type, t);
4390 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4391 builtin function, but a very special sort of operator. */
4393 enum gimplify_status
4394 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4396 tree promoted_type, want_va_type, have_va_type;
4397 tree valist = TREE_OPERAND (*expr_p, 0);
4398 tree type = TREE_TYPE (*expr_p);
4401 /* Verify that valist is of the proper type. */
4402 want_va_type = va_list_type_node;
4403 have_va_type = TREE_TYPE (valist);
4405 if (have_va_type == error_mark_node)
4408 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4410 /* If va_list is an array type, the argument may have decayed
4411 to a pointer type, e.g. by being passed to another function.
4412 In that case, unwrap both types so that we can compare the
4413 underlying records. */
4414 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4415 || POINTER_TYPE_P (have_va_type))
4417 want_va_type = TREE_TYPE (want_va_type);
4418 have_va_type = TREE_TYPE (have_va_type);
4422 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4424 error ("first argument to %<va_arg%> not of type %<va_list%>");
4428 /* Generate a diagnostic for requesting data of a type that cannot
4429 be passed through `...' due to type promotion at the call site. */
4430 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4433 static bool gave_help;
4435 /* Unfortunately, this is merely undefined, rather than a constraint
4436 violation, so we cannot make this an error. If this call is never
4437 executed, the program is still strictly conforming. */
4438 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4439 type, promoted_type);
4443 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4444 promoted_type, type);
4447 /* We can, however, treat "undefined" any way we please.
4448 Call abort to encourage the user to fix the program. */
4449 inform ("if this code is reached, the program will abort");
4450 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4452 append_to_statement_list (t, pre_p);
4454 /* This is dead code, but go ahead and finish so that the
4455 mode of the result comes out right. */
4456 *expr_p = dummy_object (type);
4461 /* Make it easier for the backends by protecting the valist argument
4462 from multiple evaluations. */
4463 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4465 /* For this case, the backends will be expecting a pointer to
4466 TREE_TYPE (va_list_type_node), but it's possible we've
4467 actually been given an array (an actual va_list_type_node).
4469 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4471 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4472 valist = build_fold_addr_expr_with_type (valist, p1);
4474 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4477 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4479 if (!targetm.gimplify_va_arg_expr)
4480 /* FIXME:Once most targets are converted we should merely
4481 assert this is non-null. */
4484 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4489 /* Expand ARGLIST, from a call to __builtin_va_end. */
4492 expand_builtin_va_end (tree arglist)
4494 tree valist = TREE_VALUE (arglist);
4496 /* Evaluate for side effects, if needed. I hate macros that don't
4498 if (TREE_SIDE_EFFECTS (valist))
4499 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4504 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4505 builtin rather than just as an assignment in stdarg.h because of the
4506 nastiness of array-type va_list types. */
4509 expand_builtin_va_copy (tree arglist)
4513 dst = TREE_VALUE (arglist);
4514 src = TREE_VALUE (TREE_CHAIN (arglist));
4516 dst = stabilize_va_list (dst, 1);
4517 src = stabilize_va_list (src, 0);
4519 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4521 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4522 TREE_SIDE_EFFECTS (t) = 1;
4523 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4527 rtx dstb, srcb, size;
4529 /* Evaluate to pointers. */
4530 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4531 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4532 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4533 VOIDmode, EXPAND_NORMAL);
4535 dstb = convert_memory_address (Pmode, dstb);
4536 srcb = convert_memory_address (Pmode, srcb);
4538 /* "Dereference" to BLKmode memories. */
4539 dstb = gen_rtx_MEM (BLKmode, dstb);
4540 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4541 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4542 srcb = gen_rtx_MEM (BLKmode, srcb);
4543 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4544 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4547 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4553 /* Expand a call to one of the builtin functions __builtin_frame_address or
4554 __builtin_return_address. */
4557 expand_builtin_frame_address (tree fndecl, tree arglist)
4559 /* The argument must be a nonnegative integer constant.
4560 It counts the number of frames to scan up the stack.
4561 The value is the return address saved in that frame. */
4563 /* Warning about missing arg was already issued. */
4565 else if (! host_integerp (TREE_VALUE (arglist), 1))
4567 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4568 error ("invalid argument to %<__builtin_frame_address%>");
4570 error ("invalid argument to %<__builtin_return_address%>");
4576 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4577 tree_low_cst (TREE_VALUE (arglist), 1));
4579 /* Some ports cannot access arbitrary stack frames. */
4582 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4583 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4585 warning (0, "unsupported argument to %<__builtin_return_address%>");
4589 /* For __builtin_frame_address, return what we've got. */
4590 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4594 && ! CONSTANT_P (tem))
4595 tem = copy_to_mode_reg (Pmode, tem);
4600 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4601 we failed and the caller should emit a normal call, otherwise try to get
4602 the result in TARGET, if convenient. */
4605 expand_builtin_alloca (tree arglist, rtx target)
4610 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4611 should always expand to function calls. These can be intercepted
4616 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4619 /* Compute the argument. */
4620 op0 = expand_normal (TREE_VALUE (arglist));
4622 /* Allocate the desired space. */
4623 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4624 result = convert_memory_address (ptr_mode, result);
4629 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4630 is the mode to expand with. */
4633 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4635 enum machine_mode mode;
4639 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4642 arg = TREE_VALUE (arglist);
4643 mode = TYPE_MODE (TREE_TYPE (arg));
4644 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4646 target = expand_unop (mode, bswap_optab, op0, target, 1);
4648 gcc_assert (target);
4650 return convert_to_mode (mode, target, 0);
4653 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4654 Return 0 if a normal call should be emitted rather than expanding the
4655 function in-line. If convenient, the result should be placed in TARGET.
4656 SUBTARGET may be used as the target for computing one of EXP's operands. */
4659 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4660 rtx subtarget, optab op_optab)
4663 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4666 /* Compute the argument. */
4667 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4668 /* Compute op, into TARGET if possible.
4669 Set TARGET to wherever the result comes back. */
4670 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4671 op_optab, op0, target, 1);
4672 gcc_assert (target);
4674 return convert_to_mode (target_mode, target, 0);
4677 /* If the string passed to fputs is a constant and is one character
4678 long, we attempt to transform this call into __builtin_fputc(). */
4681 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4683 /* Verify the arguments in the original call. */
4684 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4686 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4687 unlocked, NULL_TREE);
4689 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4694 /* Expand a call to __builtin_expect. We return our argument and emit a
4695 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4696 a non-jump context. */
4699 expand_builtin_expect (tree arglist, rtx target)
4704 if (arglist == NULL_TREE
4705 || TREE_CHAIN (arglist) == NULL_TREE)
4707 exp = TREE_VALUE (arglist);
4708 c = TREE_VALUE (TREE_CHAIN (arglist));
4710 if (TREE_CODE (c) != INTEGER_CST)
4712 error ("second argument to %<__builtin_expect%> must be a constant");
4713 c = integer_zero_node;
4716 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4718 /* Don't bother with expected value notes for integral constants. */
4719 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4721 /* We do need to force this into a register so that we can be
4722 moderately sure to be able to correctly interpret the branch
4724 target = force_reg (GET_MODE (target), target);
4726 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4728 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4729 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4735 /* Like expand_builtin_expect, except do this in a jump context. This is
4736 called from do_jump if the conditional is a __builtin_expect. Return either
4737 a list of insns to emit the jump or NULL if we cannot optimize
4738 __builtin_expect. We need to optimize this at jump time so that machines
4739 like the PowerPC don't turn the test into a SCC operation, and then jump
4740 based on the test being 0/1. */
4743 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4745 tree arglist = TREE_OPERAND (exp, 1);
4746 tree arg0 = TREE_VALUE (arglist);
4747 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4750 /* Only handle __builtin_expect (test, 0) and
4751 __builtin_expect (test, 1). */
4752 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4753 && (integer_zerop (arg1) || integer_onep (arg1)))
4755 rtx insn, drop_through_label, temp;
4757 /* Expand the jump insns. */
4759 do_jump (arg0, if_false_label, if_true_label);
4762 drop_through_label = get_last_insn ();
4763 if (drop_through_label && NOTE_P (drop_through_label))
4764 drop_through_label = prev_nonnote_insn (drop_through_label);
4765 if (drop_through_label && !LABEL_P (drop_through_label))
4766 drop_through_label = NULL_RTX;
4769 if (! if_true_label)
4770 if_true_label = drop_through_label;
4771 if (! if_false_label)
4772 if_false_label = drop_through_label;
4774 /* Go through and add the expect's to each of the conditional jumps. */
4776 while (insn != NULL_RTX)
4778 rtx next = NEXT_INSN (insn);
4780 if (JUMP_P (insn) && any_condjump_p (insn))
4782 rtx ifelse = SET_SRC (pc_set (insn));
4783 rtx then_dest = XEXP (ifelse, 1);
4784 rtx else_dest = XEXP (ifelse, 2);
4787 /* First check if we recognize any of the labels. */
4788 if (GET_CODE (then_dest) == LABEL_REF
4789 && XEXP (then_dest, 0) == if_true_label)
4791 else if (GET_CODE (then_dest) == LABEL_REF
4792 && XEXP (then_dest, 0) == if_false_label)
4794 else if (GET_CODE (else_dest) == LABEL_REF
4795 && XEXP (else_dest, 0) == if_false_label)
4797 else if (GET_CODE (else_dest) == LABEL_REF
4798 && XEXP (else_dest, 0) == if_true_label)
4800 /* Otherwise check where we drop through. */
4801 else if (else_dest == pc_rtx)
4803 if (next && NOTE_P (next))
4804 next = next_nonnote_insn (next);
4806 if (next && JUMP_P (next)
4807 && any_uncondjump_p (next))
4808 temp = XEXP (SET_SRC (pc_set (next)), 0);
4812 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4813 else that can't possibly match either target label. */
4814 if (temp == if_false_label)
4816 else if (temp == if_true_label)
4819 else if (then_dest == pc_rtx)
4821 if (next && NOTE_P (next))
4822 next = next_nonnote_insn (next);
4824 if (next && JUMP_P (next)
4825 && any_uncondjump_p (next))
4826 temp = XEXP (SET_SRC (pc_set (next)), 0);
4830 if (temp == if_false_label)
4832 else if (temp == if_true_label)
4838 /* If the test is expected to fail, reverse the
4840 if (integer_zerop (arg1))
4842 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4854 expand_builtin_trap (void)
4858 emit_insn (gen_trap ());
4861 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4865 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4866 Return 0 if a normal call should be emitted rather than expanding
4867 the function inline. If convenient, the result should be placed
4868 in TARGET. SUBTARGET may be used as the target for computing
4872 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4874 enum machine_mode mode;
4878 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4881 arg = TREE_VALUE (arglist);
4882 mode = TYPE_MODE (TREE_TYPE (arg));
4883 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4884 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4887 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4888 Return NULL is a normal call should be emitted rather than expanding the
4889 function inline. If convenient, the result should be placed in TARGET.
4890 SUBTARGET may be used as the target for computing the operand. */
4893 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4898 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4901 arg = TREE_VALUE (arglist);
4902 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4904 arg = TREE_VALUE (TREE_CHAIN (arglist));
4905 op1 = expand_normal (arg);
4907 return expand_copysign (op0, op1, target);
4910 /* Create a new constant string literal and return a char* pointer to it.
4911 The STRING_CST value is the LEN characters at STR. */
4913 build_string_literal (int len, const char *str)
4915 tree t, elem, index, type;
4917 t = build_string (len, str);
4918 elem = build_type_variant (char_type_node, 1, 0);
4919 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4920 type = build_array_type (elem, index);
4921 TREE_TYPE (t) = type;
4922 TREE_CONSTANT (t) = 1;
4923 TREE_INVARIANT (t) = 1;
4924 TREE_READONLY (t) = 1;
4925 TREE_STATIC (t) = 1;
4927 type = build_pointer_type (type);
4928 t = build1 (ADDR_EXPR, type, t);
4930 type = build_pointer_type (elem);
4931 t = build1 (NOP_EXPR, type, t);
4935 /* Expand EXP, a call to printf or printf_unlocked.
4936 Return 0 if a normal call should be emitted rather than transforming
4937 the function inline. If convenient, the result should be placed in
4938 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4941 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4944 tree arglist = TREE_OPERAND (exp, 1);
4945 /* If we're using an unlocked function, assume the other unlocked
4946 functions exist explicitly. */
4947 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4948 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4949 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4950 : implicit_built_in_decls[BUILT_IN_PUTS];
4951 const char *fmt_str;
4954 /* If the return value is used, don't do the transformation. */
4955 if (target != const0_rtx)
4958 /* Verify the required arguments in the original call. */
4961 fmt = TREE_VALUE (arglist);
4962 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4964 arglist = TREE_CHAIN (arglist);
4966 /* Check whether the format is a literal string constant. */
4967 fmt_str = c_getstr (fmt);
4968 if (fmt_str == NULL)
4971 if (!init_target_chars())
4974 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4975 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4978 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4979 || TREE_CHAIN (arglist))
4983 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4984 else if (strcmp (fmt_str, target_percent_c) == 0)
4987 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4988 || TREE_CHAIN (arglist))
4994 /* We can't handle anything else with % args or %% ... yet. */
4995 if (strchr (fmt_str, target_percent))
5001 /* If the format specifier was "", printf does nothing. */
5002 if (fmt_str[0] == '\0')
5004 /* If the format specifier has length of 1, call putchar. */
5005 if (fmt_str[1] == '\0')
5007 /* Given printf("c"), (where c is any one character,)
5008 convert "c"[0] to an int and pass that to the replacement
5010 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5011 arglist = build_tree_list (NULL_TREE, arg);
5016 /* If the format specifier was "string\n", call puts("string"). */
5017 size_t len = strlen (fmt_str);
5018 if ((unsigned char)fmt_str[len - 1] == target_newline)
5020 /* Create a NUL-terminated string that's one char shorter
5021 than the original, stripping off the trailing '\n'. */
5022 char *newstr = alloca (len);
5023 memcpy (newstr, fmt_str, len - 1);
5024 newstr[len - 1] = 0;
5026 arg = build_string_literal (len, newstr);
5027 arglist = build_tree_list (NULL_TREE, arg);
5031 /* We'd like to arrange to call fputs(string,stdout) here,
5032 but we need stdout and don't have a way to get it yet. */
5039 fn = build_function_call_expr (fn, arglist);
5040 if (TREE_CODE (fn) == CALL_EXPR)
5041 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5042 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5045 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5046 Return 0 if a normal call should be emitted rather than transforming
5047 the function inline. If convenient, the result should be placed in
5048 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5051 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5054 tree arglist = TREE_OPERAND (exp, 1);
5055 /* If we're using an unlocked function, assume the other unlocked
5056 functions exist explicitly. */
5057 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5058 : implicit_built_in_decls[BUILT_IN_FPUTC];
5059 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5060 : implicit_built_in_decls[BUILT_IN_FPUTS];
5061 const char *fmt_str;
5062 tree fn, fmt, fp, arg;
5064 /* If the return value is used, don't do the transformation. */
5065 if (target != const0_rtx)
5068 /* Verify the required arguments in the original call. */
5071 fp = TREE_VALUE (arglist);
5072 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5074 arglist = TREE_CHAIN (arglist);
5077 fmt = TREE_VALUE (arglist);
5078 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5080 arglist = TREE_CHAIN (arglist);
5082 /* Check whether the format is a literal string constant. */
5083 fmt_str = c_getstr (fmt);
5084 if (fmt_str == NULL)
5087 if (!init_target_chars())
5090 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5091 if (strcmp (fmt_str, target_percent_s) == 0)
5094 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5095 || TREE_CHAIN (arglist))
5097 arg = TREE_VALUE (arglist);
5098 arglist = build_tree_list (NULL_TREE, fp);
5099 arglist = tree_cons (NULL_TREE, arg, arglist);
5102 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5103 else if (strcmp (fmt_str, target_percent_c) == 0)
5106 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5107 || TREE_CHAIN (arglist))
5109 arg = TREE_VALUE (arglist);
5110 arglist = build_tree_list (NULL_TREE, fp);
5111 arglist = tree_cons (NULL_TREE, arg, arglist);
5116 /* We can't handle anything else with % args or %% ... yet. */
5117 if (strchr (fmt_str, target_percent))
5123 /* If the format specifier was "", fprintf does nothing. */
5124 if (fmt_str[0] == '\0')
5126 /* Evaluate and ignore FILE* argument for side-effects. */
5127 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5131 /* When "string" doesn't contain %, replace all cases of
5132 fprintf(stream,string) with fputs(string,stream). The fputs
5133 builtin will take care of special cases like length == 1. */
5134 arglist = build_tree_list (NULL_TREE, fp);
5135 arglist = tree_cons (NULL_TREE, fmt, arglist);
5141 fn = build_function_call_expr (fn, arglist);
5142 if (TREE_CODE (fn) == CALL_EXPR)
5143 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5144 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5147 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5148 a normal call should be emitted rather than expanding the function
5149 inline. If convenient, the result should be placed in TARGET with
5153 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5155 tree orig_arglist, dest, fmt;
5156 const char *fmt_str;
5158 orig_arglist = arglist;
5160 /* Verify the required arguments in the original call. */
5163 dest = TREE_VALUE (arglist);
5164 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5166 arglist = TREE_CHAIN (arglist);
5169 fmt = TREE_VALUE (arglist);
5170 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5172 arglist = TREE_CHAIN (arglist);
5174 /* Check whether the format is a literal string constant. */
5175 fmt_str = c_getstr (fmt);
5176 if (fmt_str == NULL)
5179 if (!init_target_chars())
5182 /* If the format doesn't contain % args or %%, use strcpy. */
5183 if (strchr (fmt_str, target_percent) == 0)
5185 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5188 if (arglist || ! fn)
5190 expand_expr (build_function_call_expr (fn, orig_arglist),
5191 const0_rtx, VOIDmode, EXPAND_NORMAL);
5192 if (target == const0_rtx)
5194 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5195 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5197 /* If the format is "%s", use strcpy if the result isn't used. */
5198 else if (strcmp (fmt_str, target_percent_s) == 0)
5201 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5206 if (! arglist || TREE_CHAIN (arglist))
5208 arg = TREE_VALUE (arglist);
5209 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5212 if (target != const0_rtx)
5214 len = c_strlen (arg, 1);
5215 if (! len || TREE_CODE (len) != INTEGER_CST)
5221 arglist = build_tree_list (NULL_TREE, arg);
5222 arglist = tree_cons (NULL_TREE, dest, arglist);
5223 expand_expr (build_function_call_expr (fn, arglist),
5224 const0_rtx, VOIDmode, EXPAND_NORMAL);
5226 if (target == const0_rtx)
5228 return expand_expr (len, target, mode, EXPAND_NORMAL);
5234 /* Expand a call to either the entry or exit function profiler. */
5237 expand_builtin_profile_func (bool exitp)
5241 this = DECL_RTL (current_function_decl);
5242 gcc_assert (MEM_P (this));
5243 this = XEXP (this, 0);
5246 which = profile_function_exit_libfunc;
5248 which = profile_function_entry_libfunc;
5250 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5251 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5258 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5261 round_trampoline_addr (rtx tramp)
5263 rtx temp, addend, mask;
5265 /* If we don't need too much alignment, we'll have been guaranteed
5266 proper alignment by get_trampoline_type. */
5267 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5270 /* Round address up to desired boundary. */
5271 temp = gen_reg_rtx (Pmode);
5272 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5273 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5275 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5276 temp, 0, OPTAB_LIB_WIDEN);
5277 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5278 temp, 0, OPTAB_LIB_WIDEN);
5284 expand_builtin_init_trampoline (tree arglist)
5286 tree t_tramp, t_func, t_chain;
5287 rtx r_tramp, r_func, r_chain;
5288 #ifdef TRAMPOLINE_TEMPLATE
5292 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5293 POINTER_TYPE, VOID_TYPE))
5296 t_tramp = TREE_VALUE (arglist);
5297 arglist = TREE_CHAIN (arglist);
5298 t_func = TREE_VALUE (arglist);
5299 arglist = TREE_CHAIN (arglist);
5300 t_chain = TREE_VALUE (arglist);
5302 r_tramp = expand_normal (t_tramp);
5303 r_func = expand_normal (t_func);
5304 r_chain = expand_normal (t_chain);
5306 /* Generate insns to initialize the trampoline. */
5307 r_tramp = round_trampoline_addr (r_tramp);
5308 #ifdef TRAMPOLINE_TEMPLATE
5309 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5310 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5311 emit_block_move (blktramp, assemble_trampoline_template (),
5312 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5314 trampolines_created = 1;
5315 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5321 expand_builtin_adjust_trampoline (tree arglist)
5325 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5328 tramp = expand_normal (TREE_VALUE (arglist));
5329 tramp = round_trampoline_addr (tramp);
5330 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5331 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5337 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5338 Return NULL_RTX if a normal call should be emitted rather than expanding
5339 the function in-line. EXP is the expression that is a call to the builtin
5340 function; if convenient, the result should be placed in TARGET. */
5343 expand_builtin_signbit (tree exp, rtx target)
5345 const struct real_format *fmt;
5346 enum machine_mode fmode, imode, rmode;
5347 HOST_WIDE_INT hi, lo;
5352 arglist = TREE_OPERAND (exp, 1);
5353 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5356 arg = TREE_VALUE (arglist);
5357 fmode = TYPE_MODE (TREE_TYPE (arg));
5358 rmode = TYPE_MODE (TREE_TYPE (exp));
5359 fmt = REAL_MODE_FORMAT (fmode);
5361 /* For floating point formats without a sign bit, implement signbit
5363 bitpos = fmt->signbit_ro;
5366 /* But we can't do this if the format supports signed zero. */
5367 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5370 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5371 build_real (TREE_TYPE (arg), dconst0));
5372 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5375 temp = expand_normal (arg);
5376 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5378 imode = int_mode_for_mode (fmode);
5379 if (imode == BLKmode)
5381 temp = gen_lowpart (imode, temp);
5386 /* Handle targets with different FP word orders. */
5387 if (FLOAT_WORDS_BIG_ENDIAN)
5388 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5390 word = bitpos / BITS_PER_WORD;
5391 temp = operand_subword_force (temp, word, fmode);
5392 bitpos = bitpos % BITS_PER_WORD;
5395 /* Force the intermediate word_mode (or narrower) result into a
5396 register. This avoids attempting to create paradoxical SUBREGs
5397 of floating point modes below. */
5398 temp = force_reg (imode, temp);
5400 /* If the bitpos is within the "result mode" lowpart, the operation
5401 can be implement with a single bitwise AND. Otherwise, we need
5402 a right shift and an AND. */
5404 if (bitpos < GET_MODE_BITSIZE (rmode))
5406 if (bitpos < HOST_BITS_PER_WIDE_INT)
5409 lo = (HOST_WIDE_INT) 1 << bitpos;
5413 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5418 temp = gen_lowpart (rmode, temp);
5419 temp = expand_binop (rmode, and_optab, temp,
5420 immed_double_const (lo, hi, rmode),
5421 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5425 /* Perform a logical right shift to place the signbit in the least
5426 significant bit, then truncate the result to the desired mode
5427 and mask just this bit. */
5428 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5429 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5430 temp = gen_lowpart (rmode, temp);
5431 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5432 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5438 /* Expand fork or exec calls. TARGET is the desired target of the
5439 call. ARGLIST is the list of arguments of the call. FN is the
5440 identificator of the actual function. IGNORE is nonzero if the
5441 value is to be ignored. */
5444 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5449 /* If we are not profiling, just call the function. */
5450 if (!profile_arc_flag)
5453 /* Otherwise call the wrapper. This should be equivalent for the rest of
5454 compiler, so the code does not diverge, and the wrapper may run the
5455 code necessary for keeping the profiling sane. */
5457 switch (DECL_FUNCTION_CODE (fn))
5460 id = get_identifier ("__gcov_fork");
5463 case BUILT_IN_EXECL:
5464 id = get_identifier ("__gcov_execl");
5467 case BUILT_IN_EXECV:
5468 id = get_identifier ("__gcov_execv");
5471 case BUILT_IN_EXECLP:
5472 id = get_identifier ("__gcov_execlp");
5475 case BUILT_IN_EXECLE:
5476 id = get_identifier ("__gcov_execle");
5479 case BUILT_IN_EXECVP:
5480 id = get_identifier ("__gcov_execvp");
5483 case BUILT_IN_EXECVE:
5484 id = get_identifier ("__gcov_execve");
5491 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5492 DECL_EXTERNAL (decl) = 1;
5493 TREE_PUBLIC (decl) = 1;
5494 DECL_ARTIFICIAL (decl) = 1;
5495 TREE_NOTHROW (decl) = 1;
5496 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5497 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5498 call = build_function_call_expr (decl, arglist);
5500 return expand_call (call, target, ignore);
5504 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5505 the pointer in these functions is void*, the tree optimizers may remove
5506 casts. The mode computed in expand_builtin isn't reliable either, due
5507 to __sync_bool_compare_and_swap.
5509 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5510 group of builtins. This gives us log2 of the mode size. */
5512 static inline enum machine_mode
5513 get_builtin_sync_mode (int fcode_diff)
5515 /* The size is not negotiable, so ask not to get BLKmode in return
5516 if the target indicates that a smaller size would be better. */
5517 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5520 /* Expand the memory expression LOC and return the appropriate memory operand
5521 for the builtin_sync operations. */
5524 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5528 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5530 /* Note that we explicitly do not want any alias information for this
5531 memory, so that we kill all other live memories. Otherwise we don't
5532 satisfy the full barrier semantics of the intrinsic. */
5533 mem = validize_mem (gen_rtx_MEM (mode, addr));
5535 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5536 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5537 MEM_VOLATILE_P (mem) = 1;
5542 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5543 ARGLIST is the operands list to the function. CODE is the rtx code
5544 that corresponds to the arithmetic or logical operation from the name;
5545 an exception here is that NOT actually means NAND. TARGET is an optional
5546 place for us to store the results; AFTER is true if this is the
5547 fetch_and_xxx form. IGNORE is true if we don't actually care about
5548 the result of the operation at all. */
5551 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5552 enum rtx_code code, bool after,
5553 rtx target, bool ignore)
5557 /* Expand the operands. */
5558 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5560 arglist = TREE_CHAIN (arglist);
5561 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5562 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5563 val = convert_to_mode (mode, val, 1);
5566 return expand_sync_operation (mem, val, code);
5568 return expand_sync_fetch_operation (mem, val, code, after, target);
5571 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5572 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5573 true if this is the boolean form. TARGET is a place for us to store the
5574 results; this is NOT optional if IS_BOOL is true. */
5577 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5578 bool is_bool, rtx target)
5580 rtx old_val, new_val, mem;
5582 /* Expand the operands. */
5583 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5585 arglist = TREE_CHAIN (arglist);
5586 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5587 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5588 old_val = convert_to_mode (mode, old_val, 1);
5590 arglist = TREE_CHAIN (arglist);
5591 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5592 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5593 new_val = convert_to_mode (mode, new_val, 1);
5596 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5598 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5601 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5602 general form is actually an atomic exchange, and some targets only
5603 support a reduced form with the second argument being a constant 1.
5604 ARGLIST is the operands list to the function; TARGET is an optional
5605 place for us to store the results. */
5608 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5613 /* Expand the operands. */
5614 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5616 arglist = TREE_CHAIN (arglist);
5617 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5618 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5619 val = convert_to_mode (mode, val, 1);
5621 return expand_sync_lock_test_and_set (mem, val, target);
5624 /* Expand the __sync_synchronize intrinsic. */
5627 expand_builtin_synchronize (void)
5631 #ifdef HAVE_memory_barrier
5632 if (HAVE_memory_barrier)
5634 emit_insn (gen_memory_barrier ());
5639 /* If no explicit memory barrier instruction is available, create an
5640 empty asm stmt with a memory clobber. */
5641 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5642 tree_cons (NULL, build_string (6, "memory"), NULL));
5643 ASM_VOLATILE_P (x) = 1;
5644 expand_asm_expr (x);
5647 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5651 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5653 enum insn_code icode;
5655 rtx val = const0_rtx;
5657 /* Expand the operands. */
5658 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5660 /* If there is an explicit operation in the md file, use it. */
5661 icode = sync_lock_release[mode];
5662 if (icode != CODE_FOR_nothing)
5664 if (!insn_data[icode].operand[1].predicate (val, mode))
5665 val = force_reg (mode, val);
5667 insn = GEN_FCN (icode) (mem, val);
5675 /* Otherwise we can implement this operation by emitting a barrier
5676 followed by a store of zero. */
5677 expand_builtin_synchronize ();
5678 emit_move_insn (mem, val);
5681 /* Expand an expression EXP that calls a built-in function,
5682 with result going to TARGET if that's convenient
5683 (and in mode MODE if that's convenient).
5684 SUBTARGET may be used as the target for computing one of EXP's operands.
5685 IGNORE is nonzero if the value is to be ignored. */
5688 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5691 tree fndecl = get_callee_fndecl (exp);
5692 tree arglist = TREE_OPERAND (exp, 1);
5693 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5694 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5696 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5697 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5699 /* When not optimizing, generate calls to library functions for a certain
5702 && !called_as_built_in (fndecl)
5703 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5704 && fcode != BUILT_IN_ALLOCA)
5705 return expand_call (exp, target, ignore);
5707 /* The built-in function expanders test for target == const0_rtx
5708 to determine whether the function's result will be ignored. */
5710 target = const0_rtx;
5712 /* If the result of a pure or const built-in function is ignored, and
5713 none of its arguments are volatile, we can avoid expanding the
5714 built-in call and just evaluate the arguments for side-effects. */
5715 if (target == const0_rtx
5716 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5718 bool volatilep = false;
5721 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5722 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5730 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5731 expand_expr (TREE_VALUE (arg), const0_rtx,
5732 VOIDmode, EXPAND_NORMAL);
5739 CASE_FLT_FN (BUILT_IN_FABS):
5740 target = expand_builtin_fabs (arglist, target, subtarget);
5745 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5746 target = expand_builtin_copysign (arglist, target, subtarget);
5751 /* Just do a normal library call if we were unable to fold
5753 CASE_FLT_FN (BUILT_IN_CABS):
5756 CASE_FLT_FN (BUILT_IN_EXP):
5757 CASE_FLT_FN (BUILT_IN_EXP10):
5758 CASE_FLT_FN (BUILT_IN_POW10):
5759 CASE_FLT_FN (BUILT_IN_EXP2):
5760 CASE_FLT_FN (BUILT_IN_EXPM1):
5761 CASE_FLT_FN (BUILT_IN_LOGB):
5762 CASE_FLT_FN (BUILT_IN_ILOGB):
5763 CASE_FLT_FN (BUILT_IN_LOG):
5764 CASE_FLT_FN (BUILT_IN_LOG10):
5765 CASE_FLT_FN (BUILT_IN_LOG2):
5766 CASE_FLT_FN (BUILT_IN_LOG1P):
5767 CASE_FLT_FN (BUILT_IN_TAN):
5768 CASE_FLT_FN (BUILT_IN_ASIN):
5769 CASE_FLT_FN (BUILT_IN_ACOS):
5770 CASE_FLT_FN (BUILT_IN_ATAN):
5771 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5772 because of possible accuracy problems. */
5773 if (! flag_unsafe_math_optimizations)
5775 CASE_FLT_FN (BUILT_IN_SQRT):
5776 CASE_FLT_FN (BUILT_IN_FLOOR):
5777 CASE_FLT_FN (BUILT_IN_CEIL):
5778 CASE_FLT_FN (BUILT_IN_TRUNC):
5779 CASE_FLT_FN (BUILT_IN_ROUND):
5780 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5781 CASE_FLT_FN (BUILT_IN_RINT):
5782 target = expand_builtin_mathfn (exp, target, subtarget);
5787 CASE_FLT_FN (BUILT_IN_LCEIL):
5788 CASE_FLT_FN (BUILT_IN_LLCEIL):
5789 CASE_FLT_FN (BUILT_IN_LFLOOR):
5790 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5791 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5796 CASE_FLT_FN (BUILT_IN_LRINT):
5797 CASE_FLT_FN (BUILT_IN_LLRINT):
5798 CASE_FLT_FN (BUILT_IN_LROUND):
5799 CASE_FLT_FN (BUILT_IN_LLROUND):
5800 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5805 CASE_FLT_FN (BUILT_IN_POW):
5806 target = expand_builtin_pow (exp, target, subtarget);
5811 CASE_FLT_FN (BUILT_IN_POWI):
5812 target = expand_builtin_powi (exp, target, subtarget);
5817 CASE_FLT_FN (BUILT_IN_ATAN2):
5818 CASE_FLT_FN (BUILT_IN_LDEXP):
5819 if (! flag_unsafe_math_optimizations)
5822 CASE_FLT_FN (BUILT_IN_FMOD):
5823 CASE_FLT_FN (BUILT_IN_REMAINDER):
5824 CASE_FLT_FN (BUILT_IN_DREM):
5825 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5830 CASE_FLT_FN (BUILT_IN_SIN):
5831 CASE_FLT_FN (BUILT_IN_COS):
5832 if (! flag_unsafe_math_optimizations)
5834 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5839 CASE_FLT_FN (BUILT_IN_SINCOS):
5840 if (! flag_unsafe_math_optimizations)
5842 target = expand_builtin_sincos (exp);
5847 case BUILT_IN_APPLY_ARGS:
5848 return expand_builtin_apply_args ();
5850 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5851 FUNCTION with a copy of the parameters described by
5852 ARGUMENTS, and ARGSIZE. It returns a block of memory
5853 allocated on the stack into which is stored all the registers
5854 that might possibly be used for returning the result of a
5855 function. ARGUMENTS is the value returned by
5856 __builtin_apply_args. ARGSIZE is the number of bytes of
5857 arguments that must be copied. ??? How should this value be
5858 computed? We'll also need a safe worst case value for varargs
5860 case BUILT_IN_APPLY:
5861 if (!validate_arglist (arglist, POINTER_TYPE,
5862 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5863 && !validate_arglist (arglist, REFERENCE_TYPE,
5864 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5872 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5873 ops[i] = expand_normal (TREE_VALUE (t));
5875 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5878 /* __builtin_return (RESULT) causes the function to return the
5879 value described by RESULT. RESULT is address of the block of
5880 memory returned by __builtin_apply. */
5881 case BUILT_IN_RETURN:
5882 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5883 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5886 case BUILT_IN_SAVEREGS:
5887 return expand_builtin_saveregs ();
5889 case BUILT_IN_ARGS_INFO:
5890 return expand_builtin_args_info (arglist);
5892 /* Return the address of the first anonymous stack arg. */
5893 case BUILT_IN_NEXT_ARG:
5894 if (fold_builtin_next_arg (arglist))
5896 return expand_builtin_next_arg ();
5898 case BUILT_IN_CLASSIFY_TYPE:
5899 return expand_builtin_classify_type (arglist);
5901 case BUILT_IN_CONSTANT_P:
5904 case BUILT_IN_FRAME_ADDRESS:
5905 case BUILT_IN_RETURN_ADDRESS:
5906 return expand_builtin_frame_address (fndecl, arglist);
5908 /* Returns the address of the area where the structure is returned.
5910 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5912 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5913 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5916 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5918 case BUILT_IN_ALLOCA:
5919 target = expand_builtin_alloca (arglist, target);
5924 case BUILT_IN_STACK_SAVE:
5925 return expand_stack_save ();
5927 case BUILT_IN_STACK_RESTORE:
5928 expand_stack_restore (TREE_VALUE (arglist));
5931 case BUILT_IN_BSWAP32:
5932 case BUILT_IN_BSWAP64:
5933 target = expand_builtin_bswap (arglist, target, subtarget);
5939 CASE_INT_FN (BUILT_IN_FFS):
5940 case BUILT_IN_FFSIMAX:
5941 target = expand_builtin_unop (target_mode, arglist, target,
5942 subtarget, ffs_optab);
5947 CASE_INT_FN (BUILT_IN_CLZ):
5948 case BUILT_IN_CLZIMAX:
5949 target = expand_builtin_unop (target_mode, arglist, target,
5950 subtarget, clz_optab);
5955 CASE_INT_FN (BUILT_IN_CTZ):
5956 case BUILT_IN_CTZIMAX:
5957 target = expand_builtin_unop (target_mode, arglist, target,
5958 subtarget, ctz_optab);
5963 CASE_INT_FN (BUILT_IN_POPCOUNT):
5964 case BUILT_IN_POPCOUNTIMAX:
5965 target = expand_builtin_unop (target_mode, arglist, target,
5966 subtarget, popcount_optab);
5971 CASE_INT_FN (BUILT_IN_PARITY):
5972 case BUILT_IN_PARITYIMAX:
5973 target = expand_builtin_unop (target_mode, arglist, target,
5974 subtarget, parity_optab);
5979 case BUILT_IN_STRLEN:
5980 target = expand_builtin_strlen (arglist, target, target_mode);
5985 case BUILT_IN_STRCPY:
5986 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5991 case BUILT_IN_STRNCPY:
5992 target = expand_builtin_strncpy (exp, target, mode);
5997 case BUILT_IN_STPCPY:
5998 target = expand_builtin_stpcpy (exp, target, mode);
6003 case BUILT_IN_STRCAT:
6004 target = expand_builtin_strcat (fndecl, arglist, target, mode);
6009 case BUILT_IN_STRNCAT:
6010 target = expand_builtin_strncat (arglist, target, mode);
6015 case BUILT_IN_STRSPN:
6016 target = expand_builtin_strspn (arglist, target, mode);
6021 case BUILT_IN_STRCSPN:
6022 target = expand_builtin_strcspn (arglist, target, mode);
6027 case BUILT_IN_STRSTR:
6028 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6033 case BUILT_IN_STRPBRK:
6034 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6039 case BUILT_IN_INDEX:
6040 case BUILT_IN_STRCHR:
6041 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6046 case BUILT_IN_RINDEX:
6047 case BUILT_IN_STRRCHR:
6048 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6053 case BUILT_IN_MEMCPY:
6054 target = expand_builtin_memcpy (exp, target, mode);
6059 case BUILT_IN_MEMPCPY:
6060 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6065 case BUILT_IN_MEMMOVE:
6066 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6072 case BUILT_IN_BCOPY:
6073 target = expand_builtin_bcopy (exp);
6078 case BUILT_IN_MEMSET:
6079 target = expand_builtin_memset (arglist, target, mode, exp);
6084 case BUILT_IN_BZERO:
6085 target = expand_builtin_bzero (exp);
6090 case BUILT_IN_STRCMP:
6091 target = expand_builtin_strcmp (exp, target, mode);
6096 case BUILT_IN_STRNCMP:
6097 target = expand_builtin_strncmp (exp, target, mode);
6103 case BUILT_IN_MEMCMP:
6104 target = expand_builtin_memcmp (exp, arglist, target, mode);
6109 case BUILT_IN_SETJMP:
6110 /* This should have been lowered to the builtins below. */
6113 case BUILT_IN_SETJMP_SETUP:
6114 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6115 and the receiver label. */
6116 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6118 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6119 VOIDmode, EXPAND_NORMAL);
6120 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6121 rtx label_r = label_rtx (label);
6123 /* This is copied from the handling of non-local gotos. */
6124 expand_builtin_setjmp_setup (buf_addr, label_r);
6125 nonlocal_goto_handler_labels
6126 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6127 nonlocal_goto_handler_labels);
6128 /* ??? Do not let expand_label treat us as such since we would
6129 not want to be both on the list of non-local labels and on
6130 the list of forced labels. */
6131 FORCED_LABEL (label) = 0;
6136 case BUILT_IN_SETJMP_DISPATCHER:
6137 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6138 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6140 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6141 rtx label_r = label_rtx (label);
6143 /* Remove the dispatcher label from the list of non-local labels
6144 since the receiver labels have been added to it above. */
6145 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6150 case BUILT_IN_SETJMP_RECEIVER:
6151 /* __builtin_setjmp_receiver is passed the receiver label. */
6152 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6154 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6155 rtx label_r = label_rtx (label);
6157 expand_builtin_setjmp_receiver (label_r);
6162 /* __builtin_longjmp is passed a pointer to an array of five words.
6163 It's similar to the C library longjmp function but works with
6164 __builtin_setjmp above. */
6165 case BUILT_IN_LONGJMP:
6166 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6168 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6169 VOIDmode, EXPAND_NORMAL);
6170 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6172 if (value != const1_rtx)
6174 error ("%<__builtin_longjmp%> second argument must be 1");
6178 expand_builtin_longjmp (buf_addr, value);
6183 case BUILT_IN_NONLOCAL_GOTO:
6184 target = expand_builtin_nonlocal_goto (arglist);
6189 /* This updates the setjmp buffer that is its argument with the value
6190 of the current stack pointer. */
6191 case BUILT_IN_UPDATE_SETJMP_BUF:
6192 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6195 = expand_normal (TREE_VALUE (arglist));
6197 expand_builtin_update_setjmp_buf (buf_addr);
6203 expand_builtin_trap ();
6206 case BUILT_IN_PRINTF:
6207 target = expand_builtin_printf (exp, target, mode, false);
6212 case BUILT_IN_PRINTF_UNLOCKED:
6213 target = expand_builtin_printf (exp, target, mode, true);
6218 case BUILT_IN_FPUTS:
6219 target = expand_builtin_fputs (arglist, target, false);
6223 case BUILT_IN_FPUTS_UNLOCKED:
6224 target = expand_builtin_fputs (arglist, target, true);
6229 case BUILT_IN_FPRINTF:
6230 target = expand_builtin_fprintf (exp, target, mode, false);
6235 case BUILT_IN_FPRINTF_UNLOCKED:
6236 target = expand_builtin_fprintf (exp, target, mode, true);
6241 case BUILT_IN_SPRINTF:
6242 target = expand_builtin_sprintf (arglist, target, mode);
6247 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6248 target = expand_builtin_signbit (exp, target);
6253 /* Various hooks for the DWARF 2 __throw routine. */
6254 case BUILT_IN_UNWIND_INIT:
6255 expand_builtin_unwind_init ();
6257 case BUILT_IN_DWARF_CFA:
6258 return virtual_cfa_rtx;
6259 #ifdef DWARF2_UNWIND_INFO
6260 case BUILT_IN_DWARF_SP_COLUMN:
6261 return expand_builtin_dwarf_sp_column ();
6262 case BUILT_IN_INIT_DWARF_REG_SIZES:
6263 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6266 case BUILT_IN_FROB_RETURN_ADDR:
6267 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6268 case BUILT_IN_EXTRACT_RETURN_ADDR:
6269 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6270 case BUILT_IN_EH_RETURN:
6271 expand_builtin_eh_return (TREE_VALUE (arglist),
6272 TREE_VALUE (TREE_CHAIN (arglist)));
6274 #ifdef EH_RETURN_DATA_REGNO
6275 case BUILT_IN_EH_RETURN_DATA_REGNO:
6276 return expand_builtin_eh_return_data_regno (arglist);
6278 case BUILT_IN_EXTEND_POINTER:
6279 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6281 case BUILT_IN_VA_START:
6282 case BUILT_IN_STDARG_START:
6283 return expand_builtin_va_start (arglist);
6284 case BUILT_IN_VA_END:
6285 return expand_builtin_va_end (arglist);
6286 case BUILT_IN_VA_COPY:
6287 return expand_builtin_va_copy (arglist);
6288 case BUILT_IN_EXPECT:
6289 return expand_builtin_expect (arglist, target);
6290 case BUILT_IN_PREFETCH:
6291 expand_builtin_prefetch (arglist);
6294 case BUILT_IN_PROFILE_FUNC_ENTER:
6295 return expand_builtin_profile_func (false);
6296 case BUILT_IN_PROFILE_FUNC_EXIT:
6297 return expand_builtin_profile_func (true);
6299 case BUILT_IN_INIT_TRAMPOLINE:
6300 return expand_builtin_init_trampoline (arglist);
6301 case BUILT_IN_ADJUST_TRAMPOLINE:
6302 return expand_builtin_adjust_trampoline (arglist);
6305 case BUILT_IN_EXECL:
6306 case BUILT_IN_EXECV:
6307 case BUILT_IN_EXECLP:
6308 case BUILT_IN_EXECLE:
6309 case BUILT_IN_EXECVP:
6310 case BUILT_IN_EXECVE:
6311 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6316 case BUILT_IN_FETCH_AND_ADD_1:
6317 case BUILT_IN_FETCH_AND_ADD_2:
6318 case BUILT_IN_FETCH_AND_ADD_4:
6319 case BUILT_IN_FETCH_AND_ADD_8:
6320 case BUILT_IN_FETCH_AND_ADD_16:
6321 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6322 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6323 false, target, ignore);
6328 case BUILT_IN_FETCH_AND_SUB_1:
6329 case BUILT_IN_FETCH_AND_SUB_2:
6330 case BUILT_IN_FETCH_AND_SUB_4:
6331 case BUILT_IN_FETCH_AND_SUB_8:
6332 case BUILT_IN_FETCH_AND_SUB_16:
6333 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6334 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6335 false, target, ignore);
6340 case BUILT_IN_FETCH_AND_OR_1:
6341 case BUILT_IN_FETCH_AND_OR_2:
6342 case BUILT_IN_FETCH_AND_OR_4:
6343 case BUILT_IN_FETCH_AND_OR_8:
6344 case BUILT_IN_FETCH_AND_OR_16:
6345 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6346 target = expand_builtin_sync_operation (mode, arglist, IOR,
6347 false, target, ignore);
6352 case BUILT_IN_FETCH_AND_AND_1:
6353 case BUILT_IN_FETCH_AND_AND_2:
6354 case BUILT_IN_FETCH_AND_AND_4:
6355 case BUILT_IN_FETCH_AND_AND_8:
6356 case BUILT_IN_FETCH_AND_AND_16:
6357 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6358 target = expand_builtin_sync_operation (mode, arglist, AND,
6359 false, target, ignore);
6364 case BUILT_IN_FETCH_AND_XOR_1:
6365 case BUILT_IN_FETCH_AND_XOR_2:
6366 case BUILT_IN_FETCH_AND_XOR_4:
6367 case BUILT_IN_FETCH_AND_XOR_8:
6368 case BUILT_IN_FETCH_AND_XOR_16:
6369 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6370 target = expand_builtin_sync_operation (mode, arglist, XOR,
6371 false, target, ignore);
6376 case BUILT_IN_FETCH_AND_NAND_1:
6377 case BUILT_IN_FETCH_AND_NAND_2:
6378 case BUILT_IN_FETCH_AND_NAND_4:
6379 case BUILT_IN_FETCH_AND_NAND_8:
6380 case BUILT_IN_FETCH_AND_NAND_16:
6381 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6382 target = expand_builtin_sync_operation (mode, arglist, NOT,
6383 false, target, ignore);
6388 case BUILT_IN_ADD_AND_FETCH_1:
6389 case BUILT_IN_ADD_AND_FETCH_2:
6390 case BUILT_IN_ADD_AND_FETCH_4:
6391 case BUILT_IN_ADD_AND_FETCH_8:
6392 case BUILT_IN_ADD_AND_FETCH_16:
6393 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6394 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6395 true, target, ignore);
6400 case BUILT_IN_SUB_AND_FETCH_1:
6401 case BUILT_IN_SUB_AND_FETCH_2:
6402 case BUILT_IN_SUB_AND_FETCH_4:
6403 case BUILT_IN_SUB_AND_FETCH_8:
6404 case BUILT_IN_SUB_AND_FETCH_16:
6405 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6406 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6407 true, target, ignore);
6412 case BUILT_IN_OR_AND_FETCH_1:
6413 case BUILT_IN_OR_AND_FETCH_2:
6414 case BUILT_IN_OR_AND_FETCH_4:
6415 case BUILT_IN_OR_AND_FETCH_8:
6416 case BUILT_IN_OR_AND_FETCH_16:
6417 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6418 target = expand_builtin_sync_operation (mode, arglist, IOR,
6419 true, target, ignore);
6424 case BUILT_IN_AND_AND_FETCH_1:
6425 case BUILT_IN_AND_AND_FETCH_2:
6426 case BUILT_IN_AND_AND_FETCH_4:
6427 case BUILT_IN_AND_AND_FETCH_8:
6428 case BUILT_IN_AND_AND_FETCH_16:
6429 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6430 target = expand_builtin_sync_operation (mode, arglist, AND,
6431 true, target, ignore);
6436 case BUILT_IN_XOR_AND_FETCH_1:
6437 case BUILT_IN_XOR_AND_FETCH_2:
6438 case BUILT_IN_XOR_AND_FETCH_4:
6439 case BUILT_IN_XOR_AND_FETCH_8:
6440 case BUILT_IN_XOR_AND_FETCH_16:
6441 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6442 target = expand_builtin_sync_operation (mode, arglist, XOR,
6443 true, target, ignore);
6448 case BUILT_IN_NAND_AND_FETCH_1:
6449 case BUILT_IN_NAND_AND_FETCH_2:
6450 case BUILT_IN_NAND_AND_FETCH_4:
6451 case BUILT_IN_NAND_AND_FETCH_8:
6452 case BUILT_IN_NAND_AND_FETCH_16:
6453 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6454 target = expand_builtin_sync_operation (mode, arglist, NOT,
6455 true, target, ignore);
6460 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6461 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6462 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6463 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6464 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6465 if (mode == VOIDmode)
6466 mode = TYPE_MODE (boolean_type_node);
6467 if (!target || !register_operand (target, mode))
6468 target = gen_reg_rtx (mode);
6470 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6471 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6476 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6477 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6478 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6479 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6480 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6481 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6482 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6487 case BUILT_IN_LOCK_TEST_AND_SET_1:
6488 case BUILT_IN_LOCK_TEST_AND_SET_2:
6489 case BUILT_IN_LOCK_TEST_AND_SET_4:
6490 case BUILT_IN_LOCK_TEST_AND_SET_8:
6491 case BUILT_IN_LOCK_TEST_AND_SET_16:
6492 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6493 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6498 case BUILT_IN_LOCK_RELEASE_1:
6499 case BUILT_IN_LOCK_RELEASE_2:
6500 case BUILT_IN_LOCK_RELEASE_4:
6501 case BUILT_IN_LOCK_RELEASE_8:
6502 case BUILT_IN_LOCK_RELEASE_16:
6503 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6504 expand_builtin_lock_release (mode, arglist);
6507 case BUILT_IN_SYNCHRONIZE:
6508 expand_builtin_synchronize ();
6511 case BUILT_IN_OBJECT_SIZE:
6512 return expand_builtin_object_size (exp);
6514 case BUILT_IN_MEMCPY_CHK:
6515 case BUILT_IN_MEMPCPY_CHK:
6516 case BUILT_IN_MEMMOVE_CHK:
6517 case BUILT_IN_MEMSET_CHK:
6518 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6523 case BUILT_IN_STRCPY_CHK:
6524 case BUILT_IN_STPCPY_CHK:
6525 case BUILT_IN_STRNCPY_CHK:
6526 case BUILT_IN_STRCAT_CHK:
6527 case BUILT_IN_STRNCAT_CHK:
6528 case BUILT_IN_SNPRINTF_CHK:
6529 case BUILT_IN_VSNPRINTF_CHK:
6530 maybe_emit_chk_warning (exp, fcode);
6533 case BUILT_IN_SPRINTF_CHK:
6534 case BUILT_IN_VSPRINTF_CHK:
6535 maybe_emit_sprintf_chk_warning (exp, fcode);
6538 default: /* just do library call, if unknown builtin */
6542 /* The switch statement above can drop through to cause the function
6543 to be called normally. */
6544 return expand_call (exp, target, ignore);
6547 /* Determine whether a tree node represents a call to a built-in
6548 function. If the tree T is a call to a built-in function with
6549 the right number of arguments of the appropriate types, return
6550 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6551 Otherwise the return value is END_BUILTINS. */
6553 enum built_in_function
6554 builtin_mathfn_code (tree t)
6556 tree fndecl, arglist, parmlist;
6557 tree argtype, parmtype;
6559 if (TREE_CODE (t) != CALL_EXPR
6560 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6561 return END_BUILTINS;
6563 fndecl = get_callee_fndecl (t);
6564 if (fndecl == NULL_TREE
6565 || TREE_CODE (fndecl) != FUNCTION_DECL
6566 || ! DECL_BUILT_IN (fndecl)
6567 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6568 return END_BUILTINS;
6570 arglist = TREE_OPERAND (t, 1);
6571 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6572 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6574 /* If a function doesn't take a variable number of arguments,
6575 the last element in the list will have type `void'. */
6576 parmtype = TREE_VALUE (parmlist);
6577 if (VOID_TYPE_P (parmtype))
6580 return END_BUILTINS;
6581 return DECL_FUNCTION_CODE (fndecl);
6585 return END_BUILTINS;
6587 argtype = TREE_TYPE (TREE_VALUE (arglist));
6589 if (SCALAR_FLOAT_TYPE_P (parmtype))
6591 if (! SCALAR_FLOAT_TYPE_P (argtype))
6592 return END_BUILTINS;
6594 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6596 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6597 return END_BUILTINS;
6599 else if (POINTER_TYPE_P (parmtype))
6601 if (! POINTER_TYPE_P (argtype))
6602 return END_BUILTINS;
6604 else if (INTEGRAL_TYPE_P (parmtype))
6606 if (! INTEGRAL_TYPE_P (argtype))
6607 return END_BUILTINS;
6610 return END_BUILTINS;
6612 arglist = TREE_CHAIN (arglist);
6615 /* Variable-length argument list. */
6616 return DECL_FUNCTION_CODE (fndecl);
6619 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6620 constant. ARGLIST is the argument list of the call. */
6623 fold_builtin_constant_p (tree arglist)
6628 arglist = TREE_VALUE (arglist);
6630 /* We return 1 for a numeric type that's known to be a constant
6631 value at compile-time or for an aggregate type that's a
6632 literal constant. */
6633 STRIP_NOPS (arglist);
6635 /* If we know this is a constant, emit the constant of one. */
6636 if (CONSTANT_CLASS_P (arglist)
6637 || (TREE_CODE (arglist) == CONSTRUCTOR
6638 && TREE_CONSTANT (arglist)))
6639 return integer_one_node;
6640 if (TREE_CODE (arglist) == ADDR_EXPR)
6642 tree op = TREE_OPERAND (arglist, 0);
6643 if (TREE_CODE (op) == STRING_CST
6644 || (TREE_CODE (op) == ARRAY_REF
6645 && integer_zerop (TREE_OPERAND (op, 1))
6646 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6647 return integer_one_node;
6650 /* If this expression has side effects, show we don't know it to be a
6651 constant. Likewise if it's a pointer or aggregate type since in
6652 those case we only want literals, since those are only optimized
6653 when generating RTL, not later.
6654 And finally, if we are compiling an initializer, not code, we
6655 need to return a definite result now; there's not going to be any
6656 more optimization done. */
6657 if (TREE_SIDE_EFFECTS (arglist)
6658 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6659 || POINTER_TYPE_P (TREE_TYPE (arglist))
6661 || folding_initializer)
6662 return integer_zero_node;
6667 /* Fold a call to __builtin_expect, if we expect that a comparison against
6668 the argument will fold to a constant. In practice, this means a true
6669 constant or the address of a non-weak symbol. ARGLIST is the argument
6670 list of the call. */
6673 fold_builtin_expect (tree arglist)
6680 arg = TREE_VALUE (arglist);
6682 /* If the argument isn't invariant, then there's nothing we can do. */
6683 if (!TREE_INVARIANT (arg))
6686 /* If we're looking at an address of a weak decl, then do not fold. */
6689 if (TREE_CODE (inner) == ADDR_EXPR)
6693 inner = TREE_OPERAND (inner, 0);
6695 while (TREE_CODE (inner) == COMPONENT_REF
6696 || TREE_CODE (inner) == ARRAY_REF);
6697 if (DECL_P (inner) && DECL_WEAK (inner))
6701 /* Otherwise, ARG already has the proper type for the return value. */
6705 /* Fold a call to __builtin_classify_type. */
6708 fold_builtin_classify_type (tree arglist)
6711 return build_int_cst (NULL_TREE, no_type_class);
6713 return build_int_cst (NULL_TREE,
6714 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6717 /* Fold a call to __builtin_strlen. */
6720 fold_builtin_strlen (tree arglist)
6722 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6726 tree len = c_strlen (TREE_VALUE (arglist), 0);
6730 /* Convert from the internal "sizetype" type to "size_t". */
6732 len = fold_convert (size_type_node, len);
6740 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6743 fold_builtin_inf (tree type, int warn)
6745 REAL_VALUE_TYPE real;
6747 /* __builtin_inff is intended to be usable to define INFINITY on all
6748 targets. If an infinity is not available, INFINITY expands "to a
6749 positive constant of type float that overflows at translation
6750 time", footnote "In this case, using INFINITY will violate the
6751 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6752 Thus we pedwarn to ensure this constraint violation is
6754 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6755 pedwarn ("target format does not support infinity");
6758 return build_real (type, real);
6761 /* Fold a call to __builtin_nan or __builtin_nans. */
6764 fold_builtin_nan (tree arglist, tree type, int quiet)
6766 REAL_VALUE_TYPE real;
6769 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6771 str = c_getstr (TREE_VALUE (arglist));
6775 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6778 return build_real (type, real);
6781 /* Return true if the floating point expression T has an integer value.
6782 We also allow +Inf, -Inf and NaN to be considered integer values. */
6785 integer_valued_real_p (tree t)
6787 switch (TREE_CODE (t))
6794 case NON_LVALUE_EXPR:
6795 return integer_valued_real_p (TREE_OPERAND (t, 0));
6800 return integer_valued_real_p (TREE_OPERAND (t, 1));
6807 return integer_valued_real_p (TREE_OPERAND (t, 0))
6808 && integer_valued_real_p (TREE_OPERAND (t, 1));
6811 return integer_valued_real_p (TREE_OPERAND (t, 1))
6812 && integer_valued_real_p (TREE_OPERAND (t, 2));
6815 if (! TREE_CONSTANT_OVERFLOW (t))
6817 REAL_VALUE_TYPE c, cint;
6819 c = TREE_REAL_CST (t);
6820 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6821 return real_identical (&c, &cint);
6827 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6828 if (TREE_CODE (type) == INTEGER_TYPE)
6830 if (TREE_CODE (type) == REAL_TYPE)
6831 return integer_valued_real_p (TREE_OPERAND (t, 0));
6836 switch (builtin_mathfn_code (t))
6838 CASE_FLT_FN (BUILT_IN_CEIL):
6839 CASE_FLT_FN (BUILT_IN_FLOOR):
6840 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6841 CASE_FLT_FN (BUILT_IN_RINT):
6842 CASE_FLT_FN (BUILT_IN_ROUND):
6843 CASE_FLT_FN (BUILT_IN_TRUNC):
6857 /* EXP is assumed to be builtin call where truncation can be propagated
6858 across (for instance floor((double)f) == (double)floorf (f).
6859 Do the transformation. */
6862 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6864 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6867 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6870 arg = TREE_VALUE (arglist);
6871 /* Integer rounding functions are idempotent. */
6872 if (fcode == builtin_mathfn_code (arg))
6875 /* If argument is already integer valued, and we don't need to worry
6876 about setting errno, there's no need to perform rounding. */
6877 if (! flag_errno_math && integer_valued_real_p (arg))
6882 tree arg0 = strip_float_extensions (arg);
6883 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6884 tree newtype = TREE_TYPE (arg0);
6887 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6888 && (decl = mathfn_built_in (newtype, fcode)))
6891 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6892 return fold_convert (ftype,
6893 build_function_call_expr (decl, arglist));
6899 /* EXP is assumed to be builtin call which can narrow the FP type of
6900 the argument, for instance lround((double)f) -> lroundf (f). */
6903 fold_fixed_mathfn (tree fndecl, tree arglist)
6905 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6908 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6911 arg = TREE_VALUE (arglist);
6913 /* If argument is already integer valued, and we don't need to worry
6914 about setting errno, there's no need to perform rounding. */
6915 if (! flag_errno_math && integer_valued_real_p (arg))
6916 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6920 tree ftype = TREE_TYPE (arg);
6921 tree arg0 = strip_float_extensions (arg);
6922 tree newtype = TREE_TYPE (arg0);
6925 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6926 && (decl = mathfn_built_in (newtype, fcode)))
6929 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6930 return build_function_call_expr (decl, arglist);
6934 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6935 sizeof (long long) == sizeof (long). */
6936 if (TYPE_PRECISION (long_long_integer_type_node)
6937 == TYPE_PRECISION (long_integer_type_node))
6939 tree newfn = NULL_TREE;
6942 CASE_FLT_FN (BUILT_IN_LLCEIL):
6943 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6946 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6947 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6950 CASE_FLT_FN (BUILT_IN_LLROUND):
6951 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6954 CASE_FLT_FN (BUILT_IN_LLRINT):
6955 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6964 tree newcall = build_function_call_expr (newfn, arglist);
6965 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6972 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6973 is the argument list, TYPE is the return type and FNDECL is the
6974 original function DECL. Return NULL_TREE if no if no simplification
6978 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6982 if (!arglist || TREE_CHAIN (arglist))
6985 arg = TREE_VALUE (arglist);
6986 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6987 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6990 /* Evaluate cabs of a constant at compile-time. */
6991 if (flag_unsafe_math_optimizations
6992 && TREE_CODE (arg) == COMPLEX_CST
6993 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6994 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6995 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6996 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6998 REAL_VALUE_TYPE r, i;
7000 r = TREE_REAL_CST (TREE_REALPART (arg));
7001 i = TREE_REAL_CST (TREE_IMAGPART (arg));
7003 real_arithmetic (&r, MULT_EXPR, &r, &r);
7004 real_arithmetic (&i, MULT_EXPR, &i, &i);
7005 real_arithmetic (&r, PLUS_EXPR, &r, &i);
7006 if (real_sqrt (&r, TYPE_MODE (type), &r)
7007 || ! flag_trapping_math)
7008 return build_real (type, r);
7011 /* If either part is zero, cabs is fabs of the other. */
7012 if (TREE_CODE (arg) == COMPLEX_EXPR
7013 && real_zerop (TREE_OPERAND (arg, 0)))
7014 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
7015 if (TREE_CODE (arg) == COMPLEX_EXPR
7016 && real_zerop (TREE_OPERAND (arg, 1)))
7017 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
7019 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7020 if (TREE_CODE (arg) == NEGATE_EXPR
7021 || TREE_CODE (arg) == CONJ_EXPR)
7023 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
7024 return build_function_call_expr (fndecl, arglist);
7027 /* Don't do this when optimizing for size. */
7028 if (flag_unsafe_math_optimizations
7029 && optimize && !optimize_size)
7031 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7033 if (sqrtfn != NULL_TREE)
7035 tree rpart, ipart, result, arglist;
7037 arg = builtin_save_expr (arg);
7039 rpart = fold_build1 (REALPART_EXPR, type, arg);
7040 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7042 rpart = builtin_save_expr (rpart);
7043 ipart = builtin_save_expr (ipart);
7045 result = fold_build2 (PLUS_EXPR, type,
7046 fold_build2 (MULT_EXPR, type,
7048 fold_build2 (MULT_EXPR, type,
7051 arglist = build_tree_list (NULL_TREE, result);
7052 return build_function_call_expr (sqrtfn, arglist);
7059 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7060 NULL_TREE if no simplification can be made. */
7063 fold_builtin_sqrt (tree arglist, tree type)
7066 enum built_in_function fcode;
7067 tree arg = TREE_VALUE (arglist);
7069 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7072 /* Optimize sqrt of constant value. */
7073 if (TREE_CODE (arg) == REAL_CST
7074 && ! TREE_CONSTANT_OVERFLOW (arg))
7076 REAL_VALUE_TYPE r, x;
7078 x = TREE_REAL_CST (arg);
7079 if (real_sqrt (&r, TYPE_MODE (type), &x)
7080 || (!flag_trapping_math && !flag_errno_math))
7081 return build_real (type, r);
7084 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7085 fcode = builtin_mathfn_code (arg);
7086 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7088 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7089 arg = fold_build2 (MULT_EXPR, type,
7090 TREE_VALUE (TREE_OPERAND (arg, 1)),
7091 build_real (type, dconsthalf));
7092 arglist = build_tree_list (NULL_TREE, arg);
7093 return build_function_call_expr (expfn, arglist);
7096 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7097 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7099 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7103 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7105 /* The inner root was either sqrt or cbrt. */
7106 REAL_VALUE_TYPE dconstroot =
7107 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7109 /* Adjust for the outer root. */
7110 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7111 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7112 tree_root = build_real (type, dconstroot);
7113 arglist = tree_cons (NULL_TREE, arg0,
7114 build_tree_list (NULL_TREE, tree_root));
7115 return build_function_call_expr (powfn, arglist);
7119 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7120 if (flag_unsafe_math_optimizations
7121 && (fcode == BUILT_IN_POW
7122 || fcode == BUILT_IN_POWF
7123 || fcode == BUILT_IN_POWL))
7125 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7126 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7127 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7129 if (!tree_expr_nonnegative_p (arg0))
7130 arg0 = build1 (ABS_EXPR, type, arg0);
7131 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7132 build_real (type, dconsthalf));
7133 arglist = tree_cons (NULL_TREE, arg0,
7134 build_tree_list (NULL_TREE, narg1));
7135 return build_function_call_expr (powfn, arglist);
7141 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7142 NULL_TREE if no simplification can be made. */
7144 fold_builtin_cbrt (tree arglist, tree type)
7146 tree arg = TREE_VALUE (arglist);
7147 const enum built_in_function fcode = builtin_mathfn_code (arg);
7150 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7153 /* Calculate the result when the argument is a constant. */
7154 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7157 if (flag_unsafe_math_optimizations)
7159 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7160 if (BUILTIN_EXPONENT_P (fcode))
7162 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7163 const REAL_VALUE_TYPE third_trunc =
7164 real_value_truncate (TYPE_MODE (type), dconstthird);
7165 arg = fold_build2 (MULT_EXPR, type,
7166 TREE_VALUE (TREE_OPERAND (arg, 1)),
7167 build_real (type, third_trunc));
7168 arglist = build_tree_list (NULL_TREE, arg);
7169 return build_function_call_expr (expfn, arglist);
7172 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7173 if (BUILTIN_SQRT_P (fcode))
7175 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7179 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7181 REAL_VALUE_TYPE dconstroot = dconstthird;
7183 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7184 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7185 tree_root = build_real (type, dconstroot);
7186 arglist = tree_cons (NULL_TREE, arg0,
7187 build_tree_list (NULL_TREE, tree_root));
7188 return build_function_call_expr (powfn, arglist);
7192 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7193 if (BUILTIN_CBRT_P (fcode))
7195 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7196 if (tree_expr_nonnegative_p (arg0))
7198 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7203 REAL_VALUE_TYPE dconstroot;
7205 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7206 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7207 tree_root = build_real (type, dconstroot);
7208 arglist = tree_cons (NULL_TREE, arg0,
7209 build_tree_list (NULL_TREE, tree_root));
7210 return build_function_call_expr (powfn, arglist);
7215 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7216 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7217 || fcode == BUILT_IN_POWL)
7219 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7220 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7221 if (tree_expr_nonnegative_p (arg00))
7223 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7224 const REAL_VALUE_TYPE dconstroot
7225 = real_value_truncate (TYPE_MODE (type), dconstthird);
7226 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7227 build_real (type, dconstroot));
7228 arglist = tree_cons (NULL_TREE, arg00,
7229 build_tree_list (NULL_TREE, narg01));
7230 return build_function_call_expr (powfn, arglist);
7237 /* Fold function call to builtin cos, cosf, or cosl. Return
7238 NULL_TREE if no simplification can be made. */
7240 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7242 tree arg = TREE_VALUE (arglist);
7245 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7248 /* Calculate the result when the argument is a constant. */
7249 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7252 /* Optimize cos(-x) into cos (x). */
7253 if (TREE_CODE (arg) == NEGATE_EXPR)
7255 tree args = build_tree_list (NULL_TREE,
7256 TREE_OPERAND (arg, 0));
7257 return build_function_call_expr (fndecl, args);
7263 /* Fold function call to builtin tan, tanf, or tanl. Return
7264 NULL_TREE if no simplification can be made. */
7266 fold_builtin_tan (tree arglist, tree type)
7268 enum built_in_function fcode;
7269 tree arg = TREE_VALUE (arglist);
7272 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7275 /* Calculate the result when the argument is a constant. */
7276 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7279 /* Optimize tan(atan(x)) = x. */
7280 fcode = builtin_mathfn_code (arg);
7281 if (flag_unsafe_math_optimizations
7282 && (fcode == BUILT_IN_ATAN
7283 || fcode == BUILT_IN_ATANF
7284 || fcode == BUILT_IN_ATANL))
7285 return TREE_VALUE (TREE_OPERAND (arg, 1));
7290 /* Fold function call to builtin trunc, truncf or truncl. Return
7291 NULL_TREE if no simplification can be made. */
7294 fold_builtin_trunc (tree fndecl, tree arglist)
7298 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7301 /* Optimize trunc of constant value. */
7302 arg = TREE_VALUE (arglist);
7303 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7305 REAL_VALUE_TYPE r, x;
7306 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7308 x = TREE_REAL_CST (arg);
7309 real_trunc (&r, TYPE_MODE (type), &x);
7310 return build_real (type, r);
7313 return fold_trunc_transparent_mathfn (fndecl, arglist);
7316 /* Fold function call to builtin floor, floorf or floorl. Return
7317 NULL_TREE if no simplification can be made. */
7320 fold_builtin_floor (tree fndecl, tree arglist)
7324 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7327 /* Optimize floor of constant value. */
7328 arg = TREE_VALUE (arglist);
7329 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7333 x = TREE_REAL_CST (arg);
7334 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7336 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7339 real_floor (&r, TYPE_MODE (type), &x);
7340 return build_real (type, r);
7344 /* Fold floor (x) where x is nonnegative to trunc (x). */
7345 if (tree_expr_nonnegative_p (arg))
7347 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7349 return build_function_call_expr (truncfn, arglist);
7352 return fold_trunc_transparent_mathfn (fndecl, arglist);
7355 /* Fold function call to builtin ceil, ceilf or ceill. Return
7356 NULL_TREE if no simplification can be made. */
7359 fold_builtin_ceil (tree fndecl, tree arglist)
7363 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7366 /* Optimize ceil of constant value. */
7367 arg = TREE_VALUE (arglist);
7368 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7372 x = TREE_REAL_CST (arg);
7373 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7375 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7378 real_ceil (&r, TYPE_MODE (type), &x);
7379 return build_real (type, r);
7383 return fold_trunc_transparent_mathfn (fndecl, arglist);
7386 /* Fold function call to builtin round, roundf or roundl. Return
7387 NULL_TREE if no simplification can be made. */
7390 fold_builtin_round (tree fndecl, tree arglist)
7394 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7397 /* Optimize round of constant value. */
7398 arg = TREE_VALUE (arglist);
7399 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7403 x = TREE_REAL_CST (arg);
7404 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7406 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7409 real_round (&r, TYPE_MODE (type), &x);
7410 return build_real (type, r);
7414 return fold_trunc_transparent_mathfn (fndecl, arglist);
7417 /* Fold function call to builtin lround, lroundf or lroundl (or the
7418 corresponding long long versions) and other rounding functions.
7419 Return NULL_TREE if no simplification can be made. */
7422 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7426 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7429 /* Optimize lround of constant value. */
7430 arg = TREE_VALUE (arglist);
7431 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7433 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7435 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7437 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7438 tree ftype = TREE_TYPE (arg), result;
7439 HOST_WIDE_INT hi, lo;
7442 switch (DECL_FUNCTION_CODE (fndecl))
7444 CASE_FLT_FN (BUILT_IN_LFLOOR):
7445 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7446 real_floor (&r, TYPE_MODE (ftype), &x);
7449 CASE_FLT_FN (BUILT_IN_LCEIL):
7450 CASE_FLT_FN (BUILT_IN_LLCEIL):
7451 real_ceil (&r, TYPE_MODE (ftype), &x);
7454 CASE_FLT_FN (BUILT_IN_LROUND):
7455 CASE_FLT_FN (BUILT_IN_LLROUND):
7456 real_round (&r, TYPE_MODE (ftype), &x);
7463 REAL_VALUE_TO_INT (&lo, &hi, r);
7464 result = build_int_cst_wide (NULL_TREE, lo, hi);
7465 if (int_fits_type_p (result, itype))
7466 return fold_convert (itype, result);
7470 switch (DECL_FUNCTION_CODE (fndecl))
7472 CASE_FLT_FN (BUILT_IN_LFLOOR):
7473 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7474 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7475 if (tree_expr_nonnegative_p (arg))
7476 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7482 return fold_fixed_mathfn (fndecl, arglist);
7485 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7486 and their long and long long variants (i.e. ffsl and ffsll).
7487 Return NULL_TREE if no simplification can be made. */
7490 fold_builtin_bitop (tree fndecl, tree arglist)
7494 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7497 /* Optimize for constant argument. */
7498 arg = TREE_VALUE (arglist);
7499 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7501 HOST_WIDE_INT hi, width, result;
7502 unsigned HOST_WIDE_INT lo;
7505 type = TREE_TYPE (arg);
7506 width = TYPE_PRECISION (type);
7507 lo = TREE_INT_CST_LOW (arg);
7509 /* Clear all the bits that are beyond the type's precision. */
7510 if (width > HOST_BITS_PER_WIDE_INT)
7512 hi = TREE_INT_CST_HIGH (arg);
7513 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7514 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7519 if (width < HOST_BITS_PER_WIDE_INT)
7520 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7523 switch (DECL_FUNCTION_CODE (fndecl))
7525 CASE_INT_FN (BUILT_IN_FFS):
7527 result = exact_log2 (lo & -lo) + 1;
7529 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7534 CASE_INT_FN (BUILT_IN_CLZ):
7536 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7538 result = width - floor_log2 (lo) - 1;
7539 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7543 CASE_INT_FN (BUILT_IN_CTZ):
7545 result = exact_log2 (lo & -lo);
7547 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7548 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7552 CASE_INT_FN (BUILT_IN_POPCOUNT):
7555 result++, lo &= lo - 1;
7557 result++, hi &= hi - 1;
7560 CASE_INT_FN (BUILT_IN_PARITY):
7563 result++, lo &= lo - 1;
7565 result++, hi &= hi - 1;
7573 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7579 /* Fold function call to builtin_bswap and the long and long long
7580 variants. Return NULL_TREE if no simplification can be made. */
7582 fold_builtin_bswap (tree fndecl, tree arglist)
7586 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7589 /* Optimize constant value. */
7590 arg = TREE_VALUE (arglist);
7591 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7593 HOST_WIDE_INT hi, width, r_hi = 0;
7594 unsigned HOST_WIDE_INT lo, r_lo = 0;
7597 type = TREE_TYPE (arg);
7598 width = TYPE_PRECISION (type);
7599 lo = TREE_INT_CST_LOW (arg);
7600 hi = TREE_INT_CST_HIGH (arg);
7602 switch (DECL_FUNCTION_CODE (fndecl))
7604 case BUILT_IN_BSWAP32:
7605 case BUILT_IN_BSWAP64:
7609 for (s = 0; s < width; s += 8)
7611 int d = width - s - 8;
7612 unsigned HOST_WIDE_INT byte;
7614 if (s < HOST_BITS_PER_WIDE_INT)
7615 byte = (lo >> s) & 0xff;
7617 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7619 if (d < HOST_BITS_PER_WIDE_INT)
7622 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7632 if (width < HOST_BITS_PER_WIDE_INT)
7633 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7635 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7640 /* Return true if EXPR is the real constant contained in VALUE. */
7643 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7647 return ((TREE_CODE (expr) == REAL_CST
7648 && ! TREE_CONSTANT_OVERFLOW (expr)
7649 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7650 || (TREE_CODE (expr) == COMPLEX_CST
7651 && real_dconstp (TREE_REALPART (expr), value)
7652 && real_zerop (TREE_IMAGPART (expr))));
7655 /* A subroutine of fold_builtin to fold the various logarithmic
7656 functions. Return NULL_TREE if no simplification can me made.
7657 FUNC is the corresponding MPFR logarithm function. */
7660 fold_builtin_logarithm (tree fndecl, tree arglist,
7661 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7663 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7665 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7666 tree arg = TREE_VALUE (arglist);
7668 const enum built_in_function fcode = builtin_mathfn_code (arg);
7670 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7671 instead we'll look for 'e' truncated to MODE. So only do
7672 this if flag_unsafe_math_optimizations is set. */
7673 if (flag_unsafe_math_optimizations && func == mpfr_log)
7675 const REAL_VALUE_TYPE e_truncated =
7676 real_value_truncate (TYPE_MODE (type), dconste);
7677 if (real_dconstp (arg, &e_truncated))
7678 return build_real (type, dconst1);
7681 /* Calculate the result when the argument is a constant. */
7682 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7685 /* Special case, optimize logN(expN(x)) = x. */
7686 if (flag_unsafe_math_optimizations
7687 && ((func == mpfr_log
7688 && (fcode == BUILT_IN_EXP
7689 || fcode == BUILT_IN_EXPF
7690 || fcode == BUILT_IN_EXPL))
7691 || (func == mpfr_log2
7692 && (fcode == BUILT_IN_EXP2
7693 || fcode == BUILT_IN_EXP2F
7694 || fcode == BUILT_IN_EXP2L))
7695 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7696 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7698 /* Optimize logN(func()) for various exponential functions. We
7699 want to determine the value "x" and the power "exponent" in
7700 order to transform logN(x**exponent) into exponent*logN(x). */
7701 if (flag_unsafe_math_optimizations)
7703 tree exponent = 0, x = 0;
7707 CASE_FLT_FN (BUILT_IN_EXP):
7708 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7709 x = build_real (type,
7710 real_value_truncate (TYPE_MODE (type), dconste));
7711 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7713 CASE_FLT_FN (BUILT_IN_EXP2):
7714 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7715 x = build_real (type, dconst2);
7716 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7718 CASE_FLT_FN (BUILT_IN_EXP10):
7719 CASE_FLT_FN (BUILT_IN_POW10):
7720 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7721 x = build_real (type, dconst10);
7722 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7724 CASE_FLT_FN (BUILT_IN_SQRT):
7725 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7726 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7727 exponent = build_real (type, dconsthalf);
7729 CASE_FLT_FN (BUILT_IN_CBRT):
7730 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7731 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7732 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7735 CASE_FLT_FN (BUILT_IN_POW):
7736 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7737 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7738 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7744 /* Now perform the optimization. */
7748 arglist = build_tree_list (NULL_TREE, x);
7749 logfn = build_function_call_expr (fndecl, arglist);
7750 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7758 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7759 NULL_TREE if no simplification can be made. */
7762 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7764 tree arg0 = TREE_VALUE (arglist);
7765 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7768 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7771 /* Calculate the result when the argument is a constant. */
7772 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7775 /* If either argument to hypot has a negate or abs, strip that off.
7776 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7777 if (TREE_CODE (arg0) == NEGATE_EXPR || TREE_CODE (arg1) == NEGATE_EXPR
7778 || TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg1) == ABS_EXPR)
7780 tree narg0 = (TREE_CODE (arg0) == NEGATE_EXPR
7781 || TREE_CODE (arg0) == ABS_EXPR)
7782 ? TREE_OPERAND (arg0, 0) : arg0;
7783 tree narg1 = (TREE_CODE (arg1) == NEGATE_EXPR
7784 || TREE_CODE (arg1) == ABS_EXPR)
7785 ? TREE_OPERAND (arg1, 0) : arg1;
7786 tree narglist = tree_cons (NULL_TREE, narg0,
7787 build_tree_list (NULL_TREE, narg1));
7788 return build_function_call_expr (fndecl, narglist);
7791 /* If either argument is zero, hypot is fabs of the other. */
7792 if (real_zerop (arg0))
7793 return fold_build1 (ABS_EXPR, type, arg1);
7794 else if (real_zerop (arg1))
7795 return fold_build1 (ABS_EXPR, type, arg0);
7797 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7798 if (flag_unsafe_math_optimizations
7799 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7801 REAL_VALUE_TYPE sqrt2;
7803 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7804 return fold_build2 (MULT_EXPR, type,
7805 fold_build1 (ABS_EXPR, type, arg0),
7806 build_real (type, sqrt2));
7813 /* Fold a builtin function call to pow, powf, or powl. Return
7814 NULL_TREE if no simplification can be made. */
7816 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7818 tree arg0 = TREE_VALUE (arglist);
7819 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7822 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7825 /* Calculate the result when the argument is a constant. */
7826 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7829 /* Optimize pow(1.0,y) = 1.0. */
7830 if (real_onep (arg0))
7831 return omit_one_operand (type, build_real (type, dconst1), arg1);
7833 if (TREE_CODE (arg1) == REAL_CST
7834 && ! TREE_CONSTANT_OVERFLOW (arg1))
7836 REAL_VALUE_TYPE cint;
7840 c = TREE_REAL_CST (arg1);
7842 /* Optimize pow(x,0.0) = 1.0. */
7843 if (REAL_VALUES_EQUAL (c, dconst0))
7844 return omit_one_operand (type, build_real (type, dconst1),
7847 /* Optimize pow(x,1.0) = x. */
7848 if (REAL_VALUES_EQUAL (c, dconst1))
7851 /* Optimize pow(x,-1.0) = 1.0/x. */
7852 if (REAL_VALUES_EQUAL (c, dconstm1))
7853 return fold_build2 (RDIV_EXPR, type,
7854 build_real (type, dconst1), arg0);
7856 /* Optimize pow(x,0.5) = sqrt(x). */
7857 if (flag_unsafe_math_optimizations
7858 && REAL_VALUES_EQUAL (c, dconsthalf))
7860 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7862 if (sqrtfn != NULL_TREE)
7864 tree arglist = build_tree_list (NULL_TREE, arg0);
7865 return build_function_call_expr (sqrtfn, arglist);
7869 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7870 if (flag_unsafe_math_optimizations)
7872 const REAL_VALUE_TYPE dconstroot
7873 = real_value_truncate (TYPE_MODE (type), dconstthird);
7875 if (REAL_VALUES_EQUAL (c, dconstroot))
7877 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7878 if (cbrtfn != NULL_TREE)
7880 tree arglist = build_tree_list (NULL_TREE, arg0);
7881 return build_function_call_expr (cbrtfn, arglist);
7886 /* Check for an integer exponent. */
7887 n = real_to_integer (&c);
7888 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7889 if (real_identical (&c, &cint))
7891 /* Attempt to evaluate pow at compile-time. */
7892 if (TREE_CODE (arg0) == REAL_CST
7893 && ! TREE_CONSTANT_OVERFLOW (arg0))
7898 x = TREE_REAL_CST (arg0);
7899 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7900 if (flag_unsafe_math_optimizations || !inexact)
7901 return build_real (type, x);
7904 /* Strip sign ops from even integer powers. */
7905 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7907 tree narg0 = fold_strip_sign_ops (arg0);
7910 arglist = build_tree_list (NULL_TREE, arg1);
7911 arglist = tree_cons (NULL_TREE, narg0, arglist);
7912 return build_function_call_expr (fndecl, arglist);
7918 if (flag_unsafe_math_optimizations)
7920 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7922 /* Optimize pow(expN(x),y) = expN(x*y). */
7923 if (BUILTIN_EXPONENT_P (fcode))
7925 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7926 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7927 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7928 arglist = build_tree_list (NULL_TREE, arg);
7929 return build_function_call_expr (expfn, arglist);
7932 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7933 if (BUILTIN_SQRT_P (fcode))
7935 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7936 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7937 build_real (type, dconsthalf));
7939 arglist = tree_cons (NULL_TREE, narg0,
7940 build_tree_list (NULL_TREE, narg1));
7941 return build_function_call_expr (fndecl, arglist);
7944 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7945 if (BUILTIN_CBRT_P (fcode))
7947 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7948 if (tree_expr_nonnegative_p (arg))
7950 const REAL_VALUE_TYPE dconstroot
7951 = real_value_truncate (TYPE_MODE (type), dconstthird);
7952 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7953 build_real (type, dconstroot));
7954 arglist = tree_cons (NULL_TREE, arg,
7955 build_tree_list (NULL_TREE, narg1));
7956 return build_function_call_expr (fndecl, arglist);
7960 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7961 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7962 || fcode == BUILT_IN_POWL)
7964 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7965 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7966 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7967 arglist = tree_cons (NULL_TREE, arg00,
7968 build_tree_list (NULL_TREE, narg1));
7969 return build_function_call_expr (fndecl, arglist);
7976 /* Fold a builtin function call to powi, powif, or powil. Return
7977 NULL_TREE if no simplification can be made. */
7979 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7981 tree arg0 = TREE_VALUE (arglist);
7982 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7984 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7987 /* Optimize pow(1.0,y) = 1.0. */
7988 if (real_onep (arg0))
7989 return omit_one_operand (type, build_real (type, dconst1), arg1);
7991 if (host_integerp (arg1, 0))
7993 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7995 /* Evaluate powi at compile-time. */
7996 if (TREE_CODE (arg0) == REAL_CST
7997 && ! TREE_CONSTANT_OVERFLOW (arg0))
8000 x = TREE_REAL_CST (arg0);
8001 real_powi (&x, TYPE_MODE (type), &x, c);
8002 return build_real (type, x);
8005 /* Optimize pow(x,0) = 1.0. */
8007 return omit_one_operand (type, build_real (type, dconst1),
8010 /* Optimize pow(x,1) = x. */
8014 /* Optimize pow(x,-1) = 1.0/x. */
8016 return fold_build2 (RDIV_EXPR, type,
8017 build_real (type, dconst1), arg0);
8023 /* A subroutine of fold_builtin to fold the various exponent
8024 functions. Return NULL_TREE if no simplification can me made.
8025 FUNC is the corresponding MPFR exponent function. */
8028 fold_builtin_exponent (tree fndecl, tree arglist,
8029 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8031 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8033 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8034 tree arg = TREE_VALUE (arglist);
8037 /* Calculate the result when the argument is a constant. */
8038 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8041 /* Optimize expN(logN(x)) = x. */
8042 if (flag_unsafe_math_optimizations)
8044 const enum built_in_function fcode = builtin_mathfn_code (arg);
8046 if ((func == mpfr_exp
8047 && (fcode == BUILT_IN_LOG
8048 || fcode == BUILT_IN_LOGF
8049 || fcode == BUILT_IN_LOGL))
8050 || (func == mpfr_exp2
8051 && (fcode == BUILT_IN_LOG2
8052 || fcode == BUILT_IN_LOG2F
8053 || fcode == BUILT_IN_LOG2L))
8054 || (func == mpfr_exp10
8055 && (fcode == BUILT_IN_LOG10
8056 || fcode == BUILT_IN_LOG10F
8057 || fcode == BUILT_IN_LOG10L)))
8058 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8065 /* Return true if VAR is a VAR_DECL or a component thereof. */
8068 var_decl_component_p (tree var)
8071 while (handled_component_p (inner))
8072 inner = TREE_OPERAND (inner, 0);
8073 return SSA_VAR_P (inner);
8076 /* Fold function call to builtin memset. Return
8077 NULL_TREE if no simplification can be made. */
8080 fold_builtin_memset (tree arglist, tree type, bool ignore)
8082 tree dest, c, len, var, ret;
8083 unsigned HOST_WIDE_INT length, cval;
8085 if (!validate_arglist (arglist,
8086 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8089 dest = TREE_VALUE (arglist);
8090 c = TREE_VALUE (TREE_CHAIN (arglist));
8091 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8093 if (! host_integerp (len, 1))
8096 /* If the LEN parameter is zero, return DEST. */
8097 if (integer_zerop (len))
8098 return omit_one_operand (type, dest, c);
8100 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8105 if (TREE_CODE (var) != ADDR_EXPR)
8108 var = TREE_OPERAND (var, 0);
8109 if (TREE_THIS_VOLATILE (var))
8112 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8113 && !POINTER_TYPE_P (TREE_TYPE (var)))
8116 if (! var_decl_component_p (var))
8119 length = tree_low_cst (len, 1);
8120 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8121 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8125 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8128 if (integer_zerop (c))
8132 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8135 cval = tree_low_cst (c, 1);
8139 cval |= (cval << 31) << 1;
8142 ret = build_int_cst_type (TREE_TYPE (var), cval);
8143 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
8147 return omit_one_operand (type, dest, ret);
8150 /* Fold function call to builtin memset. Return
8151 NULL_TREE if no simplification can be made. */
8154 fold_builtin_bzero (tree arglist, bool ignore)
8156 tree dest, size, newarglist;
8158 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8164 dest = TREE_VALUE (arglist);
8165 size = TREE_VALUE (TREE_CHAIN (arglist));
8167 /* New argument list transforming bzero(ptr x, int y) to
8168 memset(ptr x, int 0, size_t y). This is done this way
8169 so that if it isn't expanded inline, we fallback to
8170 calling bzero instead of memset. */
8172 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8173 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8174 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8175 return fold_builtin_memset (newarglist, void_type_node, ignore);
8178 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8179 NULL_TREE if no simplification can be made.
8180 If ENDP is 0, return DEST (like memcpy).
8181 If ENDP is 1, return DEST+LEN (like mempcpy).
8182 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8183 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8187 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8189 tree dest, src, len, destvar, srcvar, expr;
8190 unsigned HOST_WIDE_INT length;
8192 if (! validate_arglist (arglist,
8193 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8196 dest = TREE_VALUE (arglist);
8197 src = TREE_VALUE (TREE_CHAIN (arglist));
8198 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8200 /* If the LEN parameter is zero, return DEST. */
8201 if (integer_zerop (len))
8202 return omit_one_operand (type, dest, src);
8204 /* If SRC and DEST are the same (and not volatile), return
8205 DEST{,+LEN,+LEN-1}. */
8206 if (operand_equal_p (src, dest, 0))
8212 unsigned int src_align
8213 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8214 unsigned int dest_align
8215 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8216 /* Both DEST and SRC must be pointer types.
8217 ??? This is what old code did. Is the testing for pointer types
8220 If either SRC is readonly or length is 1, we can use memcpy. */
8221 if (dest_align && src_align
8222 && (readonly_data_expr (src)
8223 || integer_onep (len)))
8225 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8228 return build_function_call_expr (fn, arglist);
8231 if (! host_integerp (len, 1))
8234 if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src))
8238 STRIP_NOPS (destvar);
8239 if (TREE_CODE (destvar) != ADDR_EXPR)
8242 destvar = TREE_OPERAND (destvar, 0);
8243 if (TREE_THIS_VOLATILE (destvar))
8246 if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8247 && !POINTER_TYPE_P (TREE_TYPE (destvar))
8248 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar)))
8251 if (! var_decl_component_p (destvar))
8255 STRIP_NOPS (srcvar);
8256 if (TREE_CODE (srcvar) != ADDR_EXPR)
8259 srcvar = TREE_OPERAND (srcvar, 0);
8260 if (TREE_THIS_VOLATILE (srcvar))
8263 if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8264 && !POINTER_TYPE_P (TREE_TYPE (srcvar))
8265 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar)))
8268 if (! var_decl_component_p (srcvar))
8271 length = tree_low_cst (len, 1);
8272 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length
8273 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8275 || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length
8276 || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8280 if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8281 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8282 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8283 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8284 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8286 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8287 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8293 if (endp == 0 || endp == 3)
8294 return omit_one_operand (type, dest, expr);
8300 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8303 len = fold_convert (TREE_TYPE (dest), len);
8304 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8305 dest = fold_convert (type, dest);
8307 dest = omit_one_operand (type, dest, expr);
8311 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8312 simplification can be made. */
8315 fold_builtin_bcopy (tree arglist, bool ignore)
8317 tree src, dest, size, newarglist;
8319 if (!validate_arglist (arglist,
8320 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8326 src = TREE_VALUE (arglist);
8327 dest = TREE_VALUE (TREE_CHAIN (arglist));
8328 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8330 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8331 memmove(ptr y, ptr x, size_t z). This is done this way
8332 so that if it isn't expanded inline, we fallback to
8333 calling bcopy instead of memmove. */
8335 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8336 newarglist = tree_cons (NULL_TREE, src, newarglist);
8337 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8339 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8342 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8343 the length of the string to be copied. Return NULL_TREE if no
8344 simplification can be made. */
8347 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8351 if (!validate_arglist (arglist,
8352 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8355 dest = TREE_VALUE (arglist);
8356 src = TREE_VALUE (TREE_CHAIN (arglist));
8358 /* If SRC and DEST are the same (and not volatile), return DEST. */
8359 if (operand_equal_p (src, dest, 0))
8360 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8365 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8371 len = c_strlen (src, 1);
8372 if (! len || TREE_SIDE_EFFECTS (len))
8376 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8377 arglist = build_tree_list (NULL_TREE, len);
8378 arglist = tree_cons (NULL_TREE, src, arglist);
8379 arglist = tree_cons (NULL_TREE, dest, arglist);
8380 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8381 build_function_call_expr (fn, arglist));
8384 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8385 the length of the source string. Return NULL_TREE if no simplification
8389 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8391 tree dest, src, len, fn;
8393 if (!validate_arglist (arglist,
8394 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8397 dest = TREE_VALUE (arglist);
8398 src = TREE_VALUE (TREE_CHAIN (arglist));
8399 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8401 /* If the LEN parameter is zero, return DEST. */
8402 if (integer_zerop (len))
8403 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8405 /* We can't compare slen with len as constants below if len is not a
8407 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8411 slen = c_strlen (src, 1);
8413 /* Now, we must be passed a constant src ptr parameter. */
8414 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8417 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8419 /* We do not support simplification of this case, though we do
8420 support it when expanding trees into RTL. */
8421 /* FIXME: generate a call to __builtin_memset. */
8422 if (tree_int_cst_lt (slen, len))
8425 /* OK transform into builtin memcpy. */
8426 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8429 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8430 build_function_call_expr (fn, arglist));
8433 /* Fold function call to builtin memcmp. Return
8434 NULL_TREE if no simplification can be made. */
8437 fold_builtin_memcmp (tree arglist)
8439 tree arg1, arg2, len;
8440 const char *p1, *p2;
8442 if (!validate_arglist (arglist,
8443 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8446 arg1 = TREE_VALUE (arglist);
8447 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8448 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8450 /* If the LEN parameter is zero, return zero. */
8451 if (integer_zerop (len))
8452 return omit_two_operands (integer_type_node, integer_zero_node,
8455 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8456 if (operand_equal_p (arg1, arg2, 0))
8457 return omit_one_operand (integer_type_node, integer_zero_node, len);
8459 p1 = c_getstr (arg1);
8460 p2 = c_getstr (arg2);
8462 /* If all arguments are constant, and the value of len is not greater
8463 than the lengths of arg1 and arg2, evaluate at compile-time. */
8464 if (host_integerp (len, 1) && p1 && p2
8465 && compare_tree_int (len, strlen (p1) + 1) <= 0
8466 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8468 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8471 return integer_one_node;
8473 return integer_minus_one_node;
8475 return integer_zero_node;
8478 /* If len parameter is one, return an expression corresponding to
8479 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8480 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8482 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8483 tree cst_uchar_ptr_node
8484 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8486 tree ind1 = fold_convert (integer_type_node,
8487 build1 (INDIRECT_REF, cst_uchar_node,
8488 fold_convert (cst_uchar_ptr_node,
8490 tree ind2 = fold_convert (integer_type_node,
8491 build1 (INDIRECT_REF, cst_uchar_node,
8492 fold_convert (cst_uchar_ptr_node,
8494 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8500 /* Fold function call to builtin strcmp. Return
8501 NULL_TREE if no simplification can be made. */
8504 fold_builtin_strcmp (tree arglist)
8507 const char *p1, *p2;
8509 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8512 arg1 = TREE_VALUE (arglist);
8513 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8515 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8516 if (operand_equal_p (arg1, arg2, 0))
8517 return integer_zero_node;
8519 p1 = c_getstr (arg1);
8520 p2 = c_getstr (arg2);
8524 const int i = strcmp (p1, p2);
8526 return integer_minus_one_node;
8528 return integer_one_node;
8530 return integer_zero_node;
8533 /* If the second arg is "", return *(const unsigned char*)arg1. */
8534 if (p2 && *p2 == '\0')
8536 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8537 tree cst_uchar_ptr_node
8538 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8540 return fold_convert (integer_type_node,
8541 build1 (INDIRECT_REF, cst_uchar_node,
8542 fold_convert (cst_uchar_ptr_node,
8546 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8547 if (p1 && *p1 == '\0')
8549 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8550 tree cst_uchar_ptr_node
8551 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8553 tree temp = fold_convert (integer_type_node,
8554 build1 (INDIRECT_REF, cst_uchar_node,
8555 fold_convert (cst_uchar_ptr_node,
8557 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8563 /* Fold function call to builtin strncmp. Return
8564 NULL_TREE if no simplification can be made. */
8567 fold_builtin_strncmp (tree arglist)
8569 tree arg1, arg2, len;
8570 const char *p1, *p2;
8572 if (!validate_arglist (arglist,
8573 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8576 arg1 = TREE_VALUE (arglist);
8577 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8578 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8580 /* If the LEN parameter is zero, return zero. */
8581 if (integer_zerop (len))
8582 return omit_two_operands (integer_type_node, integer_zero_node,
8585 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8586 if (operand_equal_p (arg1, arg2, 0))
8587 return omit_one_operand (integer_type_node, integer_zero_node, len);
8589 p1 = c_getstr (arg1);
8590 p2 = c_getstr (arg2);
8592 if (host_integerp (len, 1) && p1 && p2)
8594 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8596 return integer_one_node;
8598 return integer_minus_one_node;
8600 return integer_zero_node;
8603 /* If the second arg is "", and the length is greater than zero,
8604 return *(const unsigned char*)arg1. */
8605 if (p2 && *p2 == '\0'
8606 && TREE_CODE (len) == INTEGER_CST
8607 && tree_int_cst_sgn (len) == 1)
8609 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8610 tree cst_uchar_ptr_node
8611 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8613 return fold_convert (integer_type_node,
8614 build1 (INDIRECT_REF, cst_uchar_node,
8615 fold_convert (cst_uchar_ptr_node,
8619 /* If the first arg is "", and the length is greater than zero,
8620 return -*(const unsigned char*)arg2. */
8621 if (p1 && *p1 == '\0'
8622 && TREE_CODE (len) == INTEGER_CST
8623 && tree_int_cst_sgn (len) == 1)
8625 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8626 tree cst_uchar_ptr_node
8627 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8629 tree temp = fold_convert (integer_type_node,
8630 build1 (INDIRECT_REF, cst_uchar_node,
8631 fold_convert (cst_uchar_ptr_node,
8633 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8636 /* If len parameter is one, return an expression corresponding to
8637 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8638 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8640 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8641 tree cst_uchar_ptr_node
8642 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8644 tree ind1 = fold_convert (integer_type_node,
8645 build1 (INDIRECT_REF, cst_uchar_node,
8646 fold_convert (cst_uchar_ptr_node,
8648 tree ind2 = fold_convert (integer_type_node,
8649 build1 (INDIRECT_REF, cst_uchar_node,
8650 fold_convert (cst_uchar_ptr_node,
8652 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8658 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8659 NULL_TREE if no simplification can be made. */
8662 fold_builtin_signbit (tree fndecl, tree arglist)
8664 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8667 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8670 arg = TREE_VALUE (arglist);
8672 /* If ARG is a compile-time constant, determine the result. */
8673 if (TREE_CODE (arg) == REAL_CST
8674 && !TREE_CONSTANT_OVERFLOW (arg))
8678 c = TREE_REAL_CST (arg);
8679 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8680 return fold_convert (type, temp);
8683 /* If ARG is non-negative, the result is always zero. */
8684 if (tree_expr_nonnegative_p (arg))
8685 return omit_one_operand (type, integer_zero_node, arg);
8687 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8688 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8689 return fold_build2 (LT_EXPR, type, arg,
8690 build_real (TREE_TYPE (arg), dconst0));
8695 /* Fold function call to builtin copysign, copysignf or copysignl.
8696 Return NULL_TREE if no simplification can be made. */
8699 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8701 tree arg1, arg2, tem;
8703 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8706 arg1 = TREE_VALUE (arglist);
8707 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8709 /* copysign(X,X) is X. */
8710 if (operand_equal_p (arg1, arg2, 0))
8711 return fold_convert (type, arg1);
8713 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8714 if (TREE_CODE (arg1) == REAL_CST
8715 && TREE_CODE (arg2) == REAL_CST
8716 && !TREE_CONSTANT_OVERFLOW (arg1)
8717 && !TREE_CONSTANT_OVERFLOW (arg2))
8719 REAL_VALUE_TYPE c1, c2;
8721 c1 = TREE_REAL_CST (arg1);
8722 c2 = TREE_REAL_CST (arg2);
8723 /* c1.sign := c2.sign. */
8724 real_copysign (&c1, &c2);
8725 return build_real (type, c1);
8728 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8729 Remember to evaluate Y for side-effects. */
8730 if (tree_expr_nonnegative_p (arg2))
8731 return omit_one_operand (type,
8732 fold_build1 (ABS_EXPR, type, arg1),
8735 /* Strip sign changing operations for the first argument. */
8736 tem = fold_strip_sign_ops (arg1);
8739 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8740 return build_function_call_expr (fndecl, arglist);
8746 /* Fold a call to builtin isascii. */
8749 fold_builtin_isascii (tree arglist)
8751 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8755 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8756 tree arg = TREE_VALUE (arglist);
8758 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8759 build_int_cst (NULL_TREE,
8760 ~ (unsigned HOST_WIDE_INT) 0x7f));
8761 return fold_build2 (EQ_EXPR, integer_type_node,
8762 arg, integer_zero_node);
8766 /* Fold a call to builtin toascii. */
8769 fold_builtin_toascii (tree arglist)
8771 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8775 /* Transform toascii(c) -> (c & 0x7f). */
8776 tree arg = TREE_VALUE (arglist);
8778 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8779 build_int_cst (NULL_TREE, 0x7f));
8783 /* Fold a call to builtin isdigit. */
8786 fold_builtin_isdigit (tree arglist)
8788 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8792 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8793 /* According to the C standard, isdigit is unaffected by locale.
8794 However, it definitely is affected by the target character set. */
8796 unsigned HOST_WIDE_INT target_digit0
8797 = lang_hooks.to_target_charset ('0');
8799 if (target_digit0 == 0)
8802 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8803 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8804 build_int_cst (unsigned_type_node, target_digit0));
8805 return fold_build2 (LE_EXPR, integer_type_node, arg,
8806 build_int_cst (unsigned_type_node, 9));
8810 /* Fold a call to fabs, fabsf or fabsl. */
8813 fold_builtin_fabs (tree arglist, tree type)
8817 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8820 arg = TREE_VALUE (arglist);
8821 arg = fold_convert (type, arg);
8822 if (TREE_CODE (arg) == REAL_CST)
8823 return fold_abs_const (arg, type);
8824 return fold_build1 (ABS_EXPR, type, arg);
8827 /* Fold a call to abs, labs, llabs or imaxabs. */
8830 fold_builtin_abs (tree arglist, tree type)
8834 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8837 arg = TREE_VALUE (arglist);
8838 arg = fold_convert (type, arg);
8839 if (TREE_CODE (arg) == INTEGER_CST)
8840 return fold_abs_const (arg, type);
8841 return fold_build1 (ABS_EXPR, type, arg);
8844 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8845 EXP is the CALL_EXPR for the call. */
8848 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8850 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8854 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8856 /* Check that we have exactly one argument. */
8859 error ("too few arguments to function %qs",
8860 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8861 return error_mark_node;
8863 else if (TREE_CHAIN (arglist) != 0)
8865 error ("too many arguments to function %qs",
8866 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8867 return error_mark_node;
8871 error ("non-floating-point argument to function %qs",
8872 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8873 return error_mark_node;
8877 arg = TREE_VALUE (arglist);
8878 switch (builtin_index)
8880 case BUILT_IN_ISINF:
8881 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8882 return omit_one_operand (type, integer_zero_node, arg);
8884 if (TREE_CODE (arg) == REAL_CST)
8886 r = TREE_REAL_CST (arg);
8887 if (real_isinf (&r))
8888 return real_compare (GT_EXPR, &r, &dconst0)
8889 ? integer_one_node : integer_minus_one_node;
8891 return integer_zero_node;
8896 case BUILT_IN_FINITE:
8897 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
8898 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8899 return omit_one_operand (type, integer_one_node, arg);
8901 if (TREE_CODE (arg) == REAL_CST)
8903 r = TREE_REAL_CST (arg);
8904 return real_isinf (&r) || real_isnan (&r)
8905 ? integer_zero_node : integer_one_node;
8910 case BUILT_IN_ISNAN:
8911 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
8912 return omit_one_operand (type, integer_zero_node, arg);
8914 if (TREE_CODE (arg) == REAL_CST)
8916 r = TREE_REAL_CST (arg);
8917 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8920 arg = builtin_save_expr (arg);
8921 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8928 /* Fold a call to an unordered comparison function such as
8929 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8930 being called and ARGLIST is the argument list for the call.
8931 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8932 the opposite of the desired result. UNORDERED_CODE is used
8933 for modes that can hold NaNs and ORDERED_CODE is used for
8937 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8938 enum tree_code unordered_code,
8939 enum tree_code ordered_code)
8941 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8942 enum tree_code code;
8945 enum tree_code code0, code1;
8946 tree cmp_type = NULL_TREE;
8948 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8950 /* Check that we have exactly two arguments. */
8951 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8953 error ("too few arguments to function %qs",
8954 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8955 return error_mark_node;
8957 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8959 error ("too many arguments to function %qs",
8960 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8961 return error_mark_node;
8965 arg0 = TREE_VALUE (arglist);
8966 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8968 type0 = TREE_TYPE (arg0);
8969 type1 = TREE_TYPE (arg1);
8971 code0 = TREE_CODE (type0);
8972 code1 = TREE_CODE (type1);
8974 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8975 /* Choose the wider of two real types. */
8976 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8978 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8980 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8984 error ("non-floating-point argument to function %qs",
8985 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8986 return error_mark_node;
8989 arg0 = fold_convert (cmp_type, arg0);
8990 arg1 = fold_convert (cmp_type, arg1);
8992 if (unordered_code == UNORDERED_EXPR)
8994 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8995 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8996 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
8999 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9001 return fold_build1 (TRUTH_NOT_EXPR, type,
9002 fold_build2 (code, type, arg0, arg1));
9005 /* Used by constant folding to simplify calls to builtin functions. EXP is
9006 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9007 result of the function call is ignored. This function returns NULL_TREE
9008 if no simplification was possible. */
9011 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9013 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9014 enum built_in_function fcode;
9016 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9017 return targetm.fold_builtin (fndecl, arglist, ignore);
9019 fcode = DECL_FUNCTION_CODE (fndecl);
9022 case BUILT_IN_FPUTS:
9023 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9025 case BUILT_IN_FPUTS_UNLOCKED:
9026 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9028 case BUILT_IN_STRSTR:
9029 return fold_builtin_strstr (arglist, type);
9031 case BUILT_IN_STRCAT:
9032 return fold_builtin_strcat (arglist);
9034 case BUILT_IN_STRNCAT:
9035 return fold_builtin_strncat (arglist);
9037 case BUILT_IN_STRSPN:
9038 return fold_builtin_strspn (arglist);
9040 case BUILT_IN_STRCSPN:
9041 return fold_builtin_strcspn (arglist);
9043 case BUILT_IN_STRCHR:
9044 case BUILT_IN_INDEX:
9045 return fold_builtin_strchr (arglist, type);
9047 case BUILT_IN_STRRCHR:
9048 case BUILT_IN_RINDEX:
9049 return fold_builtin_strrchr (arglist, type);
9051 case BUILT_IN_STRCPY:
9052 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9054 case BUILT_IN_STRNCPY:
9055 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9057 case BUILT_IN_STRCMP:
9058 return fold_builtin_strcmp (arglist);
9060 case BUILT_IN_STRNCMP:
9061 return fold_builtin_strncmp (arglist);
9063 case BUILT_IN_STRPBRK:
9064 return fold_builtin_strpbrk (arglist, type);
9067 case BUILT_IN_MEMCMP:
9068 return fold_builtin_memcmp (arglist);
9070 case BUILT_IN_SPRINTF:
9071 return fold_builtin_sprintf (arglist, ignore);
9073 case BUILT_IN_CONSTANT_P:
9077 val = fold_builtin_constant_p (arglist);
9078 /* Gimplification will pull the CALL_EXPR for the builtin out of
9079 an if condition. When not optimizing, we'll not CSE it back.
9080 To avoid link error types of regressions, return false now. */
9081 if (!val && !optimize)
9082 val = integer_zero_node;
9087 case BUILT_IN_EXPECT:
9088 return fold_builtin_expect (arglist);
9090 case BUILT_IN_CLASSIFY_TYPE:
9091 return fold_builtin_classify_type (arglist);
9093 case BUILT_IN_STRLEN:
9094 return fold_builtin_strlen (arglist);
9096 CASE_FLT_FN (BUILT_IN_FABS):
9097 return fold_builtin_fabs (arglist, type);
9101 case BUILT_IN_LLABS:
9102 case BUILT_IN_IMAXABS:
9103 return fold_builtin_abs (arglist, type);
9105 CASE_FLT_FN (BUILT_IN_CONJ):
9106 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9107 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9110 CASE_FLT_FN (BUILT_IN_CREAL):
9111 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9112 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9113 TREE_VALUE (arglist)));
9116 CASE_FLT_FN (BUILT_IN_CIMAG):
9117 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9118 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9119 TREE_VALUE (arglist)));
9122 CASE_FLT_FN (BUILT_IN_CABS):
9123 return fold_builtin_cabs (arglist, type, fndecl);
9125 CASE_FLT_FN (BUILT_IN_SQRT):
9126 return fold_builtin_sqrt (arglist, type);
9128 CASE_FLT_FN (BUILT_IN_CBRT):
9129 return fold_builtin_cbrt (arglist, type);
9131 CASE_FLT_FN (BUILT_IN_ASIN):
9132 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9133 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9134 &dconstm1, &dconst1, true);
9137 CASE_FLT_FN (BUILT_IN_ACOS):
9138 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9139 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9140 &dconstm1, &dconst1, true);
9143 CASE_FLT_FN (BUILT_IN_ATAN):
9144 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9145 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9149 CASE_FLT_FN (BUILT_IN_ASINH):
9150 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9151 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9155 CASE_FLT_FN (BUILT_IN_ACOSH):
9156 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9157 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9158 &dconst1, NULL, true);
9161 CASE_FLT_FN (BUILT_IN_ATANH):
9162 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9163 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9164 &dconstm1, &dconst1, false);
9167 CASE_FLT_FN (BUILT_IN_SIN):
9168 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9169 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9173 CASE_FLT_FN (BUILT_IN_COS):
9174 return fold_builtin_cos (arglist, type, fndecl);
9176 CASE_FLT_FN (BUILT_IN_TAN):
9177 return fold_builtin_tan (arglist, type);
9179 CASE_FLT_FN (BUILT_IN_SINCOS):
9180 if (validate_arglist (arglist, REAL_TYPE, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9181 return do_mpfr_sincos (TREE_VALUE (arglist), TREE_VALUE (TREE_CHAIN (arglist)),
9182 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))));
9185 CASE_FLT_FN (BUILT_IN_SINH):
9186 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9187 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9191 CASE_FLT_FN (BUILT_IN_COSH):
9192 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9193 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_cosh,
9197 CASE_FLT_FN (BUILT_IN_TANH):
9198 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9199 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9203 CASE_FLT_FN (BUILT_IN_ERF):
9204 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9205 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9209 CASE_FLT_FN (BUILT_IN_ERFC):
9210 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9211 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9215 CASE_FLT_FN (BUILT_IN_TGAMMA):
9216 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9217 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9221 CASE_FLT_FN (BUILT_IN_EXP):
9222 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9224 CASE_FLT_FN (BUILT_IN_EXP2):
9225 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9227 CASE_FLT_FN (BUILT_IN_EXP10):
9228 CASE_FLT_FN (BUILT_IN_POW10):
9229 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9231 CASE_FLT_FN (BUILT_IN_EXPM1):
9232 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9233 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9237 CASE_FLT_FN (BUILT_IN_LOG):
9238 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9240 CASE_FLT_FN (BUILT_IN_LOG2):
9241 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9243 CASE_FLT_FN (BUILT_IN_LOG10):
9244 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9246 CASE_FLT_FN (BUILT_IN_LOG1P):
9247 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9248 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9249 &dconstm1, NULL, false);
9252 CASE_FLT_FN (BUILT_IN_ATAN2):
9253 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9254 return do_mpfr_arg2 (TREE_VALUE (arglist),
9255 TREE_VALUE (TREE_CHAIN (arglist)),
9259 CASE_FLT_FN (BUILT_IN_HYPOT):
9260 return fold_builtin_hypot (fndecl, arglist, type);
9262 CASE_FLT_FN (BUILT_IN_POW):
9263 return fold_builtin_pow (fndecl, arglist, type);
9265 CASE_FLT_FN (BUILT_IN_POWI):
9266 return fold_builtin_powi (fndecl, arglist, type);
9268 CASE_FLT_FN (BUILT_IN_INF):
9269 case BUILT_IN_INFD32:
9270 case BUILT_IN_INFD64:
9271 case BUILT_IN_INFD128:
9272 return fold_builtin_inf (type, true);
9274 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9275 return fold_builtin_inf (type, false);
9277 CASE_FLT_FN (BUILT_IN_NAN):
9278 case BUILT_IN_NAND32:
9279 case BUILT_IN_NAND64:
9280 case BUILT_IN_NAND128:
9281 return fold_builtin_nan (arglist, type, true);
9283 CASE_FLT_FN (BUILT_IN_NANS):
9284 return fold_builtin_nan (arglist, type, false);
9286 CASE_FLT_FN (BUILT_IN_FLOOR):
9287 return fold_builtin_floor (fndecl, arglist);
9289 CASE_FLT_FN (BUILT_IN_CEIL):
9290 return fold_builtin_ceil (fndecl, arglist);
9292 CASE_FLT_FN (BUILT_IN_TRUNC):
9293 return fold_builtin_trunc (fndecl, arglist);
9295 CASE_FLT_FN (BUILT_IN_ROUND):
9296 return fold_builtin_round (fndecl, arglist);
9298 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9299 CASE_FLT_FN (BUILT_IN_RINT):
9300 return fold_trunc_transparent_mathfn (fndecl, arglist);
9302 CASE_FLT_FN (BUILT_IN_LCEIL):
9303 CASE_FLT_FN (BUILT_IN_LLCEIL):
9304 CASE_FLT_FN (BUILT_IN_LFLOOR):
9305 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9306 CASE_FLT_FN (BUILT_IN_LROUND):
9307 CASE_FLT_FN (BUILT_IN_LLROUND):
9308 return fold_builtin_int_roundingfn (fndecl, arglist);
9310 CASE_FLT_FN (BUILT_IN_LRINT):
9311 CASE_FLT_FN (BUILT_IN_LLRINT):
9312 return fold_fixed_mathfn (fndecl, arglist);
9314 case BUILT_IN_BSWAP32:
9315 case BUILT_IN_BSWAP64:
9316 return fold_builtin_bswap (fndecl, arglist);
9318 CASE_INT_FN (BUILT_IN_FFS):
9319 CASE_INT_FN (BUILT_IN_CLZ):
9320 CASE_INT_FN (BUILT_IN_CTZ):
9321 CASE_INT_FN (BUILT_IN_POPCOUNT):
9322 CASE_INT_FN (BUILT_IN_PARITY):
9323 return fold_builtin_bitop (fndecl, arglist);
9325 case BUILT_IN_MEMSET:
9326 return fold_builtin_memset (arglist, type, ignore);
9328 case BUILT_IN_MEMCPY:
9329 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9331 case BUILT_IN_MEMPCPY:
9332 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9334 case BUILT_IN_MEMMOVE:
9335 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9337 case BUILT_IN_BZERO:
9338 return fold_builtin_bzero (arglist, ignore);
9340 case BUILT_IN_BCOPY:
9341 return fold_builtin_bcopy (arglist, ignore);
9343 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9344 return fold_builtin_signbit (fndecl, arglist);
9346 case BUILT_IN_ISASCII:
9347 return fold_builtin_isascii (arglist);
9349 case BUILT_IN_TOASCII:
9350 return fold_builtin_toascii (arglist);
9352 case BUILT_IN_ISDIGIT:
9353 return fold_builtin_isdigit (arglist);
9355 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9356 return fold_builtin_copysign (fndecl, arglist, type);
9358 CASE_FLT_FN (BUILT_IN_FINITE):
9359 case BUILT_IN_FINITED32:
9360 case BUILT_IN_FINITED64:
9361 case BUILT_IN_FINITED128:
9362 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9364 CASE_FLT_FN (BUILT_IN_ISINF):
9365 case BUILT_IN_ISINFD32:
9366 case BUILT_IN_ISINFD64:
9367 case BUILT_IN_ISINFD128:
9368 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9370 CASE_FLT_FN (BUILT_IN_ISNAN):
9371 case BUILT_IN_ISNAND32:
9372 case BUILT_IN_ISNAND64:
9373 case BUILT_IN_ISNAND128:
9374 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9376 case BUILT_IN_ISGREATER:
9377 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9378 case BUILT_IN_ISGREATEREQUAL:
9379 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9380 case BUILT_IN_ISLESS:
9381 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9382 case BUILT_IN_ISLESSEQUAL:
9383 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9384 case BUILT_IN_ISLESSGREATER:
9385 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9386 case BUILT_IN_ISUNORDERED:
9387 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9390 /* We do the folding for va_start in the expander. */
9391 case BUILT_IN_VA_START:
9394 case BUILT_IN_OBJECT_SIZE:
9395 return fold_builtin_object_size (arglist);
9396 case BUILT_IN_MEMCPY_CHK:
9397 case BUILT_IN_MEMPCPY_CHK:
9398 case BUILT_IN_MEMMOVE_CHK:
9399 case BUILT_IN_MEMSET_CHK:
9400 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9401 DECL_FUNCTION_CODE (fndecl));
9402 case BUILT_IN_STRCPY_CHK:
9403 case BUILT_IN_STPCPY_CHK:
9404 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9405 DECL_FUNCTION_CODE (fndecl));
9406 case BUILT_IN_STRNCPY_CHK:
9407 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9408 case BUILT_IN_STRCAT_CHK:
9409 return fold_builtin_strcat_chk (fndecl, arglist);
9410 case BUILT_IN_STRNCAT_CHK:
9411 return fold_builtin_strncat_chk (fndecl, arglist);
9412 case BUILT_IN_SPRINTF_CHK:
9413 case BUILT_IN_VSPRINTF_CHK:
9414 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9415 case BUILT_IN_SNPRINTF_CHK:
9416 case BUILT_IN_VSNPRINTF_CHK:
9417 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9418 DECL_FUNCTION_CODE (fndecl));
9420 case BUILT_IN_PRINTF:
9421 case BUILT_IN_PRINTF_UNLOCKED:
9422 case BUILT_IN_VPRINTF:
9423 case BUILT_IN_PRINTF_CHK:
9424 case BUILT_IN_VPRINTF_CHK:
9425 return fold_builtin_printf (fndecl, arglist, ignore,
9426 DECL_FUNCTION_CODE (fndecl));
9428 case BUILT_IN_FPRINTF:
9429 case BUILT_IN_FPRINTF_UNLOCKED:
9430 case BUILT_IN_VFPRINTF:
9431 case BUILT_IN_FPRINTF_CHK:
9432 case BUILT_IN_VFPRINTF_CHK:
9433 return fold_builtin_fprintf (fndecl, arglist, ignore,
9434 DECL_FUNCTION_CODE (fndecl));
9443 /* A wrapper function for builtin folding that prevents warnings for
9444 "statement without effect" and the like, caused by removing the
9445 call node earlier than the warning is generated. */
9448 fold_builtin (tree fndecl, tree arglist, bool ignore)
9450 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9453 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
9454 TREE_NO_WARNING (exp) = 1;
9460 /* Conveniently construct a function call expression. */
9463 build_function_call_expr (tree fn, tree arglist)
9467 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9468 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9469 call_expr, arglist, NULL_TREE);
9472 /* This function validates the types of a function call argument list
9473 represented as a tree chain of parameters against a specified list
9474 of tree_codes. If the last specifier is a 0, that represents an
9475 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9478 validate_arglist (tree arglist, ...)
9480 enum tree_code code;
9484 va_start (ap, arglist);
9488 code = va_arg (ap, enum tree_code);
9492 /* This signifies an ellipses, any further arguments are all ok. */
9496 /* This signifies an endlink, if no arguments remain, return
9497 true, otherwise return false. */
9501 /* If no parameters remain or the parameter's code does not
9502 match the specified code, return false. Otherwise continue
9503 checking any remaining arguments. */
9506 if (code == POINTER_TYPE)
9508 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9511 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9515 arglist = TREE_CHAIN (arglist);
9519 /* We need gotos here since we can only have one VA_CLOSE in a
9527 /* Default target-specific builtin expander that does nothing. */
9530 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9531 rtx target ATTRIBUTE_UNUSED,
9532 rtx subtarget ATTRIBUTE_UNUSED,
9533 enum machine_mode mode ATTRIBUTE_UNUSED,
9534 int ignore ATTRIBUTE_UNUSED)
9539 /* Returns true is EXP represents data that would potentially reside
9540 in a readonly section. */
9543 readonly_data_expr (tree exp)
9547 if (TREE_CODE (exp) != ADDR_EXPR)
9550 exp = get_base_address (TREE_OPERAND (exp, 0));
9554 /* Make sure we call decl_readonly_section only for trees it
9555 can handle (since it returns true for everything it doesn't
9557 if (TREE_CODE (exp) == STRING_CST
9558 || TREE_CODE (exp) == CONSTRUCTOR
9559 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9560 return decl_readonly_section (exp, 0);
9565 /* Simplify a call to the strstr builtin.
9567 Return 0 if no simplification was possible, otherwise return the
9568 simplified form of the call as a tree.
9570 The simplified form may be a constant or other expression which
9571 computes the same value, but in a more efficient manner (including
9572 calls to other builtin functions).
9574 The call may contain arguments which need to be evaluated, but
9575 which are not useful to determine the result of the call. In
9576 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9577 COMPOUND_EXPR will be an argument which must be evaluated.
9578 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9579 COMPOUND_EXPR in the chain will contain the tree for the simplified
9580 form of the builtin function call. */
9583 fold_builtin_strstr (tree arglist, tree type)
9585 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9589 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9591 const char *p1, *p2;
9600 const char *r = strstr (p1, p2);
9604 return build_int_cst (TREE_TYPE (s1), 0);
9606 /* Return an offset into the constant string argument. */
9607 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9608 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9609 return fold_convert (type, tem);
9612 /* The argument is const char *, and the result is char *, so we need
9613 a type conversion here to avoid a warning. */
9615 return fold_convert (type, s1);
9620 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9624 /* New argument list transforming strstr(s1, s2) to
9625 strchr(s1, s2[0]). */
9626 arglist = build_tree_list (NULL_TREE,
9627 build_int_cst (NULL_TREE, p2[0]));
9628 arglist = tree_cons (NULL_TREE, s1, arglist);
9629 return build_function_call_expr (fn, arglist);
9633 /* Simplify a call to the strchr builtin.
9635 Return 0 if no simplification was possible, otherwise return the
9636 simplified form of the call as a tree.
9638 The simplified form may be a constant or other expression which
9639 computes the same value, but in a more efficient manner (including
9640 calls to other builtin functions).
9642 The call may contain arguments which need to be evaluated, but
9643 which are not useful to determine the result of the call. In
9644 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9645 COMPOUND_EXPR will be an argument which must be evaluated.
9646 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9647 COMPOUND_EXPR in the chain will contain the tree for the simplified
9648 form of the builtin function call. */
9651 fold_builtin_strchr (tree arglist, tree type)
9653 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9657 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9660 if (TREE_CODE (s2) != INTEGER_CST)
9670 if (target_char_cast (s2, &c))
9676 return build_int_cst (TREE_TYPE (s1), 0);
9678 /* Return an offset into the constant string argument. */
9679 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9680 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9681 return fold_convert (type, tem);
9687 /* Simplify a call to the strrchr builtin.
9689 Return 0 if no simplification was possible, otherwise return the
9690 simplified form of the call as a tree.
9692 The simplified form may be a constant or other expression which
9693 computes the same value, but in a more efficient manner (including
9694 calls to other builtin functions).
9696 The call may contain arguments which need to be evaluated, but
9697 which are not useful to determine the result of the call. In
9698 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9699 COMPOUND_EXPR will be an argument which must be evaluated.
9700 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9701 COMPOUND_EXPR in the chain will contain the tree for the simplified
9702 form of the builtin function call. */
9705 fold_builtin_strrchr (tree arglist, tree type)
9707 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9711 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9715 if (TREE_CODE (s2) != INTEGER_CST)
9725 if (target_char_cast (s2, &c))
9728 r = strrchr (p1, c);
9731 return build_int_cst (TREE_TYPE (s1), 0);
9733 /* Return an offset into the constant string argument. */
9734 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9735 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9736 return fold_convert (type, tem);
9739 if (! integer_zerop (s2))
9742 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9746 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9747 return build_function_call_expr (fn, arglist);
9751 /* Simplify a call to the strpbrk builtin.
9753 Return 0 if no simplification was possible, otherwise return the
9754 simplified form of the call as a tree.
9756 The simplified form may be a constant or other expression which
9757 computes the same value, but in a more efficient manner (including
9758 calls to other builtin functions).
9760 The call may contain arguments which need to be evaluated, but
9761 which are not useful to determine the result of the call. In
9762 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9763 COMPOUND_EXPR will be an argument which must be evaluated.
9764 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9765 COMPOUND_EXPR in the chain will contain the tree for the simplified
9766 form of the builtin function call. */
9769 fold_builtin_strpbrk (tree arglist, tree type)
9771 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9775 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9777 const char *p1, *p2;
9786 const char *r = strpbrk (p1, p2);
9790 return build_int_cst (TREE_TYPE (s1), 0);
9792 /* Return an offset into the constant string argument. */
9793 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9794 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9795 return fold_convert (type, tem);
9799 /* strpbrk(x, "") == NULL.
9800 Evaluate and ignore s1 in case it had side-effects. */
9801 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9804 return 0; /* Really call strpbrk. */
9806 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9810 /* New argument list transforming strpbrk(s1, s2) to
9811 strchr(s1, s2[0]). */
9812 arglist = build_tree_list (NULL_TREE,
9813 build_int_cst (NULL_TREE, p2[0]));
9814 arglist = tree_cons (NULL_TREE, s1, arglist);
9815 return build_function_call_expr (fn, arglist);
9819 /* Simplify a call to the strcat builtin.
9821 Return 0 if no simplification was possible, otherwise return the
9822 simplified form of the call as a tree.
9824 The simplified form may be a constant or other expression which
9825 computes the same value, but in a more efficient manner (including
9826 calls to other builtin functions).
9828 The call may contain arguments which need to be evaluated, but
9829 which are not useful to determine the result of the call. In
9830 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9831 COMPOUND_EXPR will be an argument which must be evaluated.
9832 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9833 COMPOUND_EXPR in the chain will contain the tree for the simplified
9834 form of the builtin function call. */
9837 fold_builtin_strcat (tree arglist)
9839 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9843 tree dst = TREE_VALUE (arglist),
9844 src = TREE_VALUE (TREE_CHAIN (arglist));
9845 const char *p = c_getstr (src);
9847 /* If the string length is zero, return the dst parameter. */
9848 if (p && *p == '\0')
9855 /* Simplify a call to the strncat builtin.
9857 Return 0 if no simplification was possible, otherwise return the
9858 simplified form of the call as a tree.
9860 The simplified form may be a constant or other expression which
9861 computes the same value, but in a more efficient manner (including
9862 calls to other builtin functions).
9864 The call may contain arguments which need to be evaluated, but
9865 which are not useful to determine the result of the call. In
9866 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9867 COMPOUND_EXPR will be an argument which must be evaluated.
9868 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9869 COMPOUND_EXPR in the chain will contain the tree for the simplified
9870 form of the builtin function call. */
9873 fold_builtin_strncat (tree arglist)
9875 if (!validate_arglist (arglist,
9876 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9880 tree dst = TREE_VALUE (arglist);
9881 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9882 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9883 const char *p = c_getstr (src);
9885 /* If the requested length is zero, or the src parameter string
9886 length is zero, return the dst parameter. */
9887 if (integer_zerop (len) || (p && *p == '\0'))
9888 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9890 /* If the requested len is greater than or equal to the string
9891 length, call strcat. */
9892 if (TREE_CODE (len) == INTEGER_CST && p
9893 && compare_tree_int (len, strlen (p)) >= 0)
9896 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9897 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9899 /* If the replacement _DECL isn't initialized, don't do the
9904 return build_function_call_expr (fn, newarglist);
9910 /* Simplify a call to the strspn builtin.
9912 Return 0 if no simplification was possible, otherwise return the
9913 simplified form of the call as a tree.
9915 The simplified form may be a constant or other expression which
9916 computes the same value, but in a more efficient manner (including
9917 calls to other builtin functions).
9919 The call may contain arguments which need to be evaluated, but
9920 which are not useful to determine the result of the call. In
9921 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9922 COMPOUND_EXPR will be an argument which must be evaluated.
9923 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9924 COMPOUND_EXPR in the chain will contain the tree for the simplified
9925 form of the builtin function call. */
9928 fold_builtin_strspn (tree arglist)
9930 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9934 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9935 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9937 /* If both arguments are constants, evaluate at compile-time. */
9940 const size_t r = strspn (p1, p2);
9941 return size_int (r);
9944 /* If either argument is "", return 0. */
9945 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9946 /* Evaluate and ignore both arguments in case either one has
9948 return omit_two_operands (integer_type_node, integer_zero_node,
9954 /* Simplify a call to the strcspn builtin.
9956 Return 0 if no simplification was possible, otherwise return the
9957 simplified form of the call as a tree.
9959 The simplified form may be a constant or other expression which
9960 computes the same value, but in a more efficient manner (including
9961 calls to other builtin functions).
9963 The call may contain arguments which need to be evaluated, but
9964 which are not useful to determine the result of the call. In
9965 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9966 COMPOUND_EXPR will be an argument which must be evaluated.
9967 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9968 COMPOUND_EXPR in the chain will contain the tree for the simplified
9969 form of the builtin function call. */
9972 fold_builtin_strcspn (tree arglist)
9974 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9978 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9979 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9981 /* If both arguments are constants, evaluate at compile-time. */
9984 const size_t r = strcspn (p1, p2);
9985 return size_int (r);
9988 /* If the first argument is "", return 0. */
9989 if (p1 && *p1 == '\0')
9991 /* Evaluate and ignore argument s2 in case it has
9993 return omit_one_operand (integer_type_node,
9994 integer_zero_node, s2);
9997 /* If the second argument is "", return __builtin_strlen(s1). */
9998 if (p2 && *p2 == '\0')
10000 tree newarglist = build_tree_list (NULL_TREE, s1),
10001 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10003 /* If the replacement _DECL isn't initialized, don't do the
10008 return build_function_call_expr (fn, newarglist);
10014 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10015 by the builtin will be ignored. UNLOCKED is true is true if this
10016 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10017 the known length of the string. Return NULL_TREE if no simplification
10021 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10024 /* If we're using an unlocked function, assume the other unlocked
10025 functions exist explicitly. */
10026 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10027 : implicit_built_in_decls[BUILT_IN_FPUTC];
10028 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10029 : implicit_built_in_decls[BUILT_IN_FWRITE];
10031 /* If the return value is used, don't do the transformation. */
10035 /* Verify the arguments in the original call. */
10036 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10040 len = c_strlen (TREE_VALUE (arglist), 0);
10042 /* Get the length of the string passed to fputs. If the length
10043 can't be determined, punt. */
10045 || TREE_CODE (len) != INTEGER_CST)
10048 switch (compare_tree_int (len, 1))
10050 case -1: /* length is 0, delete the call entirely . */
10051 return omit_one_operand (integer_type_node, integer_zero_node,
10052 TREE_VALUE (TREE_CHAIN (arglist)));
10054 case 0: /* length is 1, call fputc. */
10056 const char *p = c_getstr (TREE_VALUE (arglist));
10060 /* New argument list transforming fputs(string, stream) to
10061 fputc(string[0], stream). */
10062 arglist = build_tree_list (NULL_TREE,
10063 TREE_VALUE (TREE_CHAIN (arglist)));
10064 arglist = tree_cons (NULL_TREE,
10065 build_int_cst (NULL_TREE, p[0]),
10072 case 1: /* length is greater than 1, call fwrite. */
10076 /* If optimizing for size keep fputs. */
10079 string_arg = TREE_VALUE (arglist);
10080 /* New argument list transforming fputs(string, stream) to
10081 fwrite(string, 1, len, stream). */
10082 arglist = build_tree_list (NULL_TREE,
10083 TREE_VALUE (TREE_CHAIN (arglist)));
10084 arglist = tree_cons (NULL_TREE, len, arglist);
10085 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10086 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10091 gcc_unreachable ();
10094 /* If the replacement _DECL isn't initialized, don't do the
10099 /* These optimizations are only performed when the result is ignored,
10100 hence there's no need to cast the result to integer_type_node. */
10101 return build_function_call_expr (fn, arglist);
10104 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10105 produced. False otherwise. This is done so that we don't output the error
10106 or warning twice or three times. */
10108 fold_builtin_next_arg (tree arglist)
10110 tree fntype = TREE_TYPE (current_function_decl);
10112 if (TYPE_ARG_TYPES (fntype) == 0
10113 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10114 == void_type_node))
10116 error ("%<va_start%> used in function with fixed args");
10121 /* Evidently an out of date version of <stdarg.h>; can't validate
10122 va_start's second argument, but can still work as intended. */
10123 warning (0, "%<__builtin_next_arg%> called without an argument");
10126 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10127 when we checked the arguments and if needed issued a warning. */
10128 else if (!TREE_CHAIN (arglist)
10129 || !integer_zerop (TREE_VALUE (arglist))
10130 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10131 || TREE_CHAIN (TREE_CHAIN (arglist)))
10133 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10134 tree arg = TREE_VALUE (arglist);
10136 if (TREE_CHAIN (arglist))
10138 error ("%<va_start%> used with too many arguments");
10142 /* Strip off all nops for the sake of the comparison. This
10143 is not quite the same as STRIP_NOPS. It does more.
10144 We must also strip off INDIRECT_EXPR for C++ reference
10146 while (TREE_CODE (arg) == NOP_EXPR
10147 || TREE_CODE (arg) == CONVERT_EXPR
10148 || TREE_CODE (arg) == NON_LVALUE_EXPR
10149 || TREE_CODE (arg) == INDIRECT_REF)
10150 arg = TREE_OPERAND (arg, 0);
10151 if (arg != last_parm)
10153 /* FIXME: Sometimes with the tree optimizers we can get the
10154 not the last argument even though the user used the last
10155 argument. We just warn and set the arg to be the last
10156 argument so that we will get wrong-code because of
10158 warning (0, "second parameter of %<va_start%> not last named argument");
10160 /* We want to verify the second parameter just once before the tree
10161 optimizers are run and then avoid keeping it in the tree,
10162 as otherwise we could warn even for correct code like:
10163 void foo (int i, ...)
10164 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10165 TREE_VALUE (arglist) = integer_zero_node;
10166 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10172 /* Simplify a call to the sprintf builtin.
10174 Return 0 if no simplification was possible, otherwise return the
10175 simplified form of the call as a tree. If IGNORED is true, it means that
10176 the caller does not use the returned value of the function. */
10179 fold_builtin_sprintf (tree arglist, int ignored)
10181 tree call, retval, dest, fmt;
10182 const char *fmt_str = NULL;
10184 /* Verify the required arguments in the original call. We deal with two
10185 types of sprintf() calls: 'sprintf (str, fmt)' and
10186 'sprintf (dest, "%s", orig)'. */
10187 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10188 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10192 /* Get the destination string and the format specifier. */
10193 dest = TREE_VALUE (arglist);
10194 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10196 /* Check whether the format is a literal string constant. */
10197 fmt_str = c_getstr (fmt);
10198 if (fmt_str == NULL)
10202 retval = NULL_TREE;
10204 if (!init_target_chars())
10207 /* If the format doesn't contain % args or %%, use strcpy. */
10208 if (strchr (fmt_str, target_percent) == NULL)
10210 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10215 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10216 'format' is known to contain no % formats. */
10217 arglist = build_tree_list (NULL_TREE, fmt);
10218 arglist = tree_cons (NULL_TREE, dest, arglist);
10219 call = build_function_call_expr (fn, arglist);
10221 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10224 /* If the format is "%s", use strcpy if the result isn't used. */
10225 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10228 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10233 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10234 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10235 arglist = build_tree_list (NULL_TREE, orig);
10236 arglist = tree_cons (NULL_TREE, dest, arglist);
10239 retval = c_strlen (orig, 1);
10240 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10243 call = build_function_call_expr (fn, arglist);
10246 if (call && retval)
10248 retval = fold_convert
10249 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10251 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10257 /* Expand a call to __builtin_object_size. */
10260 expand_builtin_object_size (tree exp)
10263 int object_size_type;
10264 tree fndecl = get_callee_fndecl (exp);
10265 tree arglist = TREE_OPERAND (exp, 1);
10266 location_t locus = EXPR_LOCATION (exp);
10268 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10270 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10272 expand_builtin_trap ();
10276 ost = TREE_VALUE (TREE_CHAIN (arglist));
10279 if (TREE_CODE (ost) != INTEGER_CST
10280 || tree_int_cst_sgn (ost) < 0
10281 || compare_tree_int (ost, 3) > 0)
10283 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10285 expand_builtin_trap ();
10289 object_size_type = tree_low_cst (ost, 0);
10291 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10294 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10295 FCODE is the BUILT_IN_* to use.
10296 Return 0 if we failed; the caller should emit a normal call,
10297 otherwise try to get the result in TARGET, if convenient (and in
10298 mode MODE if that's convenient). */
10301 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10302 enum built_in_function fcode)
10304 tree arglist = TREE_OPERAND (exp, 1);
10305 tree dest, src, len, size;
10307 if (!validate_arglist (arglist,
10309 fcode == BUILT_IN_MEMSET_CHK
10310 ? INTEGER_TYPE : POINTER_TYPE,
10311 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10314 dest = TREE_VALUE (arglist);
10315 src = TREE_VALUE (TREE_CHAIN (arglist));
10316 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10317 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10319 if (! host_integerp (size, 1))
10322 if (host_integerp (len, 1) || integer_all_onesp (size))
10326 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10328 location_t locus = EXPR_LOCATION (exp);
10329 warning (0, "%Hcall to %D will always overflow destination buffer",
10330 &locus, get_callee_fndecl (exp));
10334 arglist = build_tree_list (NULL_TREE, len);
10335 arglist = tree_cons (NULL_TREE, src, arglist);
10336 arglist = tree_cons (NULL_TREE, dest, arglist);
10339 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10340 mem{cpy,pcpy,move,set} is available. */
10343 case BUILT_IN_MEMCPY_CHK:
10344 fn = built_in_decls[BUILT_IN_MEMCPY];
10346 case BUILT_IN_MEMPCPY_CHK:
10347 fn = built_in_decls[BUILT_IN_MEMPCPY];
10349 case BUILT_IN_MEMMOVE_CHK:
10350 fn = built_in_decls[BUILT_IN_MEMMOVE];
10352 case BUILT_IN_MEMSET_CHK:
10353 fn = built_in_decls[BUILT_IN_MEMSET];
10362 fn = build_function_call_expr (fn, arglist);
10363 if (TREE_CODE (fn) == CALL_EXPR)
10364 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10365 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10367 else if (fcode == BUILT_IN_MEMSET_CHK)
10371 unsigned int dest_align
10372 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10374 /* If DEST is not a pointer type, call the normal function. */
10375 if (dest_align == 0)
10378 /* If SRC and DEST are the same (and not volatile), do nothing. */
10379 if (operand_equal_p (src, dest, 0))
10383 if (fcode != BUILT_IN_MEMPCPY_CHK)
10385 /* Evaluate and ignore LEN in case it has side-effects. */
10386 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10387 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10390 len = fold_convert (TREE_TYPE (dest), len);
10391 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10392 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10395 /* __memmove_chk special case. */
10396 if (fcode == BUILT_IN_MEMMOVE_CHK)
10398 unsigned int src_align
10399 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10401 if (src_align == 0)
10404 /* If src is categorized for a readonly section we can use
10405 normal __memcpy_chk. */
10406 if (readonly_data_expr (src))
10408 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10411 fn = build_function_call_expr (fn, arglist);
10412 if (TREE_CODE (fn) == CALL_EXPR)
10413 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10414 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10421 /* Emit warning if a buffer overflow is detected at compile time. */
10424 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10426 int arg_mask, is_strlen = 0;
10427 tree arglist = TREE_OPERAND (exp, 1), a;
10433 case BUILT_IN_STRCPY_CHK:
10434 case BUILT_IN_STPCPY_CHK:
10435 /* For __strcat_chk the warning will be emitted only if overflowing
10436 by at least strlen (dest) + 1 bytes. */
10437 case BUILT_IN_STRCAT_CHK:
10441 case BUILT_IN_STRNCAT_CHK:
10442 /* For __strncat_chk the warning will be emitted only if overflowing
10443 by at least strlen (dest) + 1 bytes. */
10446 case BUILT_IN_STRNCPY_CHK:
10449 case BUILT_IN_SNPRINTF_CHK:
10450 case BUILT_IN_VSNPRINTF_CHK:
10454 gcc_unreachable ();
10459 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10471 len = TREE_VALUE (len);
10472 size = TREE_VALUE (size);
10474 if (! host_integerp (size, 1) || integer_all_onesp (size))
10479 len = c_strlen (len, 1);
10480 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10483 else if (fcode == BUILT_IN_STRNCAT_CHK)
10485 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10486 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10488 src = c_strlen (src, 1);
10489 if (! src || ! host_integerp (src, 1))
10491 locus = EXPR_LOCATION (exp);
10492 warning (0, "%Hcall to %D might overflow destination buffer",
10493 &locus, get_callee_fndecl (exp));
10496 else if (tree_int_cst_lt (src, size))
10499 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10502 locus = EXPR_LOCATION (exp);
10503 warning (0, "%Hcall to %D will always overflow destination buffer",
10504 &locus, get_callee_fndecl (exp));
10507 /* Emit warning if a buffer overflow is detected at compile time
10508 in __sprintf_chk/__vsprintf_chk calls. */
10511 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10513 tree arglist = TREE_OPERAND (exp, 1);
10514 tree dest, size, len, fmt, flag;
10515 const char *fmt_str;
10517 /* Verify the required arguments in the original call. */
10520 dest = TREE_VALUE (arglist);
10521 arglist = TREE_CHAIN (arglist);
10524 flag = TREE_VALUE (arglist);
10525 arglist = TREE_CHAIN (arglist);
10528 size = TREE_VALUE (arglist);
10529 arglist = TREE_CHAIN (arglist);
10532 fmt = TREE_VALUE (arglist);
10533 arglist = TREE_CHAIN (arglist);
10535 if (! host_integerp (size, 1) || integer_all_onesp (size))
10538 /* Check whether the format is a literal string constant. */
10539 fmt_str = c_getstr (fmt);
10540 if (fmt_str == NULL)
10543 if (!init_target_chars())
10546 /* If the format doesn't contain % args or %%, we know its size. */
10547 if (strchr (fmt_str, target_percent) == 0)
10548 len = build_int_cstu (size_type_node, strlen (fmt_str));
10549 /* If the format is "%s" and first ... argument is a string literal,
10551 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10557 arg = TREE_VALUE (arglist);
10558 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10561 len = c_strlen (arg, 1);
10562 if (!len || ! host_integerp (len, 1))
10568 if (! tree_int_cst_lt (len, size))
10570 location_t locus = EXPR_LOCATION (exp);
10571 warning (0, "%Hcall to %D will always overflow destination buffer",
10572 &locus, get_callee_fndecl (exp));
10576 /* Fold a call to __builtin_object_size, if possible. */
10579 fold_builtin_object_size (tree arglist)
10581 tree ptr, ost, ret = 0;
10582 int object_size_type;
10584 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10587 ptr = TREE_VALUE (arglist);
10588 ost = TREE_VALUE (TREE_CHAIN (arglist));
10591 if (TREE_CODE (ost) != INTEGER_CST
10592 || tree_int_cst_sgn (ost) < 0
10593 || compare_tree_int (ost, 3) > 0)
10596 object_size_type = tree_low_cst (ost, 0);
10598 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10599 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10600 and (size_t) 0 for types 2 and 3. */
10601 if (TREE_SIDE_EFFECTS (ptr))
10602 return fold_convert (size_type_node,
10603 object_size_type < 2
10604 ? integer_minus_one_node : integer_zero_node);
10606 if (TREE_CODE (ptr) == ADDR_EXPR)
10607 ret = build_int_cstu (size_type_node,
10608 compute_builtin_object_size (ptr, object_size_type));
10610 else if (TREE_CODE (ptr) == SSA_NAME)
10612 unsigned HOST_WIDE_INT bytes;
10614 /* If object size is not known yet, delay folding until
10615 later. Maybe subsequent passes will help determining
10617 bytes = compute_builtin_object_size (ptr, object_size_type);
10618 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10620 ret = build_int_cstu (size_type_node, bytes);
10625 ret = force_fit_type (ret, -1, false, false);
10626 if (TREE_CONSTANT_OVERFLOW (ret))
10633 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10634 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10635 code of the builtin. If MAXLEN is not NULL, it is maximum length
10636 passed as third argument. */
10639 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10640 enum built_in_function fcode)
10642 tree dest, src, len, size, fn;
10644 if (!validate_arglist (arglist,
10646 fcode == BUILT_IN_MEMSET_CHK
10647 ? INTEGER_TYPE : POINTER_TYPE,
10648 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10651 dest = TREE_VALUE (arglist);
10652 /* Actually val for __memset_chk, but it doesn't matter. */
10653 src = TREE_VALUE (TREE_CHAIN (arglist));
10654 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10655 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10657 /* If SRC and DEST are the same (and not volatile), return DEST
10658 (resp. DEST+LEN for __mempcpy_chk). */
10659 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10661 if (fcode != BUILT_IN_MEMPCPY_CHK)
10662 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10665 tree temp = fold_convert (TREE_TYPE (dest), len);
10666 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10667 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10671 if (! host_integerp (size, 1))
10674 if (! integer_all_onesp (size))
10676 if (! host_integerp (len, 1))
10678 /* If LEN is not constant, try MAXLEN too.
10679 For MAXLEN only allow optimizing into non-_ocs function
10680 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10681 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10683 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10685 /* (void) __mempcpy_chk () can be optimized into
10686 (void) __memcpy_chk (). */
10687 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10691 return build_function_call_expr (fn, arglist);
10699 if (tree_int_cst_lt (size, maxlen))
10703 arglist = build_tree_list (NULL_TREE, len);
10704 arglist = tree_cons (NULL_TREE, src, arglist);
10705 arglist = tree_cons (NULL_TREE, dest, arglist);
10708 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10709 mem{cpy,pcpy,move,set} is available. */
10712 case BUILT_IN_MEMCPY_CHK:
10713 fn = built_in_decls[BUILT_IN_MEMCPY];
10715 case BUILT_IN_MEMPCPY_CHK:
10716 fn = built_in_decls[BUILT_IN_MEMPCPY];
10718 case BUILT_IN_MEMMOVE_CHK:
10719 fn = built_in_decls[BUILT_IN_MEMMOVE];
10721 case BUILT_IN_MEMSET_CHK:
10722 fn = built_in_decls[BUILT_IN_MEMSET];
10731 return build_function_call_expr (fn, arglist);
10734 /* Fold a call to the __st[rp]cpy_chk builtin.
10735 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10736 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10737 strings passed as second argument. */
10740 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10741 enum built_in_function fcode)
10743 tree dest, src, size, len, fn;
10745 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10749 dest = TREE_VALUE (arglist);
10750 src = TREE_VALUE (TREE_CHAIN (arglist));
10751 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10753 /* If SRC and DEST are the same (and not volatile), return DEST. */
10754 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10755 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10757 if (! host_integerp (size, 1))
10760 if (! integer_all_onesp (size))
10762 len = c_strlen (src, 1);
10763 if (! len || ! host_integerp (len, 1))
10765 /* If LEN is not constant, try MAXLEN too.
10766 For MAXLEN only allow optimizing into non-_ocs function
10767 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10768 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10770 if (fcode == BUILT_IN_STPCPY_CHK)
10775 /* If return value of __stpcpy_chk is ignored,
10776 optimize into __strcpy_chk. */
10777 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10781 return build_function_call_expr (fn, arglist);
10784 if (! len || TREE_SIDE_EFFECTS (len))
10787 /* If c_strlen returned something, but not a constant,
10788 transform __strcpy_chk into __memcpy_chk. */
10789 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10793 len = size_binop (PLUS_EXPR, len, ssize_int (1));
10794 arglist = build_tree_list (NULL_TREE, size);
10795 arglist = tree_cons (NULL_TREE, len, arglist);
10796 arglist = tree_cons (NULL_TREE, src, arglist);
10797 arglist = tree_cons (NULL_TREE, dest, arglist);
10798 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10799 build_function_call_expr (fn, arglist));
10805 if (! tree_int_cst_lt (maxlen, size))
10809 arglist = build_tree_list (NULL_TREE, src);
10810 arglist = tree_cons (NULL_TREE, dest, arglist);
10812 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10813 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10814 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10818 return build_function_call_expr (fn, arglist);
10821 /* Fold a call to the __strncpy_chk builtin.
10822 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10825 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10827 tree dest, src, size, len, fn;
10829 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10830 INTEGER_TYPE, VOID_TYPE))
10833 dest = TREE_VALUE (arglist);
10834 src = TREE_VALUE (TREE_CHAIN (arglist));
10835 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10836 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10838 if (! host_integerp (size, 1))
10841 if (! integer_all_onesp (size))
10843 if (! host_integerp (len, 1))
10845 /* If LEN is not constant, try MAXLEN too.
10846 For MAXLEN only allow optimizing into non-_ocs function
10847 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10848 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10854 if (tree_int_cst_lt (size, maxlen))
10858 arglist = build_tree_list (NULL_TREE, len);
10859 arglist = tree_cons (NULL_TREE, src, arglist);
10860 arglist = tree_cons (NULL_TREE, dest, arglist);
10862 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10863 fn = built_in_decls[BUILT_IN_STRNCPY];
10867 return build_function_call_expr (fn, arglist);
10870 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10873 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10875 tree dest, src, size, fn;
10878 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10882 dest = TREE_VALUE (arglist);
10883 src = TREE_VALUE (TREE_CHAIN (arglist));
10884 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10886 p = c_getstr (src);
10887 /* If the SRC parameter is "", return DEST. */
10888 if (p && *p == '\0')
10889 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10891 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10894 arglist = build_tree_list (NULL_TREE, src);
10895 arglist = tree_cons (NULL_TREE, dest, arglist);
10897 /* If __builtin_strcat_chk is used, assume strcat is available. */
10898 fn = built_in_decls[BUILT_IN_STRCAT];
10902 return build_function_call_expr (fn, arglist);
10905 /* Fold a call to the __strncat_chk builtin EXP. */
10908 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10910 tree dest, src, size, len, fn;
10913 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10914 INTEGER_TYPE, VOID_TYPE))
10917 dest = TREE_VALUE (arglist);
10918 src = TREE_VALUE (TREE_CHAIN (arglist));
10919 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10920 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10922 p = c_getstr (src);
10923 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10924 if (p && *p == '\0')
10925 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10926 else if (integer_zerop (len))
10927 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10929 if (! host_integerp (size, 1))
10932 if (! integer_all_onesp (size))
10934 tree src_len = c_strlen (src, 1);
10936 && host_integerp (src_len, 1)
10937 && host_integerp (len, 1)
10938 && ! tree_int_cst_lt (len, src_len))
10940 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10941 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10945 arglist = build_tree_list (NULL_TREE, size);
10946 arglist = tree_cons (NULL_TREE, src, arglist);
10947 arglist = tree_cons (NULL_TREE, dest, arglist);
10948 return build_function_call_expr (fn, arglist);
10953 arglist = build_tree_list (NULL_TREE, len);
10954 arglist = tree_cons (NULL_TREE, src, arglist);
10955 arglist = tree_cons (NULL_TREE, dest, arglist);
10957 /* If __builtin_strncat_chk is used, assume strncat is available. */
10958 fn = built_in_decls[BUILT_IN_STRNCAT];
10962 return build_function_call_expr (fn, arglist);
10965 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10966 a normal call should be emitted rather than expanding the function
10967 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10970 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
10972 tree dest, size, len, fn, fmt, flag;
10973 const char *fmt_str;
10975 /* Verify the required arguments in the original call. */
10978 dest = TREE_VALUE (arglist);
10979 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10981 arglist = TREE_CHAIN (arglist);
10984 flag = TREE_VALUE (arglist);
10985 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
10987 arglist = TREE_CHAIN (arglist);
10990 size = TREE_VALUE (arglist);
10991 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10993 arglist = TREE_CHAIN (arglist);
10996 fmt = TREE_VALUE (arglist);
10997 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10999 arglist = TREE_CHAIN (arglist);
11001 if (! host_integerp (size, 1))
11006 if (!init_target_chars())
11009 /* Check whether the format is a literal string constant. */
11010 fmt_str = c_getstr (fmt);
11011 if (fmt_str != NULL)
11013 /* If the format doesn't contain % args or %%, we know the size. */
11014 if (strchr (fmt_str, target_percent) == 0)
11016 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11017 len = build_int_cstu (size_type_node, strlen (fmt_str));
11019 /* If the format is "%s" and first ... argument is a string literal,
11020 we know the size too. */
11021 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11025 if (arglist && !TREE_CHAIN (arglist))
11027 arg = TREE_VALUE (arglist);
11028 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11030 len = c_strlen (arg, 1);
11031 if (! len || ! host_integerp (len, 1))
11038 if (! integer_all_onesp (size))
11040 if (! len || ! tree_int_cst_lt (len, size))
11044 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11045 or if format doesn't contain % chars or is "%s". */
11046 if (! integer_zerop (flag))
11048 if (fmt_str == NULL)
11050 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11054 arglist = tree_cons (NULL_TREE, fmt, arglist);
11055 arglist = tree_cons (NULL_TREE, dest, arglist);
11057 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11058 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11059 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11063 return build_function_call_expr (fn, arglist);
11066 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11067 a normal call should be emitted rather than expanding the function
11068 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11069 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11070 passed as second argument. */
11073 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11074 enum built_in_function fcode)
11076 tree dest, size, len, fn, fmt, flag;
11077 const char *fmt_str;
11079 /* Verify the required arguments in the original call. */
11082 dest = TREE_VALUE (arglist);
11083 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11085 arglist = TREE_CHAIN (arglist);
11088 len = TREE_VALUE (arglist);
11089 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11091 arglist = TREE_CHAIN (arglist);
11094 flag = TREE_VALUE (arglist);
11095 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11097 arglist = TREE_CHAIN (arglist);
11100 size = TREE_VALUE (arglist);
11101 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11103 arglist = TREE_CHAIN (arglist);
11106 fmt = TREE_VALUE (arglist);
11107 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11109 arglist = TREE_CHAIN (arglist);
11111 if (! host_integerp (size, 1))
11114 if (! integer_all_onesp (size))
11116 if (! host_integerp (len, 1))
11118 /* If LEN is not constant, try MAXLEN too.
11119 For MAXLEN only allow optimizing into non-_ocs function
11120 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11121 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11127 if (tree_int_cst_lt (size, maxlen))
11131 if (!init_target_chars())
11134 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11135 or if format doesn't contain % chars or is "%s". */
11136 if (! integer_zerop (flag))
11138 fmt_str = c_getstr (fmt);
11139 if (fmt_str == NULL)
11141 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11145 arglist = tree_cons (NULL_TREE, fmt, arglist);
11146 arglist = tree_cons (NULL_TREE, len, arglist);
11147 arglist = tree_cons (NULL_TREE, dest, arglist);
11149 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11151 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11152 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11156 return build_function_call_expr (fn, arglist);
11159 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11161 Return 0 if no simplification was possible, otherwise return the
11162 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11163 code of the function to be simplified. */
11166 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11167 enum built_in_function fcode)
11169 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11170 const char *fmt_str = NULL;
11172 /* If the return value is used, don't do the transformation. */
11176 /* Verify the required arguments in the original call. */
11177 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11183 flag = TREE_VALUE (arglist);
11184 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11185 || TREE_SIDE_EFFECTS (flag))
11187 arglist = TREE_CHAIN (arglist);
11192 fmt = TREE_VALUE (arglist);
11193 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11195 arglist = TREE_CHAIN (arglist);
11197 /* Check whether the format is a literal string constant. */
11198 fmt_str = c_getstr (fmt);
11199 if (fmt_str == NULL)
11202 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11204 /* If we're using an unlocked function, assume the other
11205 unlocked functions exist explicitly. */
11206 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11207 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11211 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11212 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11215 if (!init_target_chars())
11218 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11222 if (strcmp (fmt_str, target_percent_s) == 0)
11224 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11228 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11229 || TREE_CHAIN (arglist))
11232 str = c_getstr (TREE_VALUE (arglist));
11238 /* The format specifier doesn't contain any '%' characters. */
11239 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11245 /* If the string was "", printf does nothing. */
11246 if (str[0] == '\0')
11247 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11249 /* If the string has length of 1, call putchar. */
11250 if (str[1] == '\0')
11252 /* Given printf("c"), (where c is any one character,)
11253 convert "c"[0] to an int and pass that to the replacement
11255 arg = build_int_cst (NULL_TREE, str[0]);
11256 arglist = build_tree_list (NULL_TREE, arg);
11261 /* If the string was "string\n", call puts("string"). */
11262 size_t len = strlen (str);
11263 if ((unsigned char)str[len - 1] == target_newline)
11265 /* Create a NUL-terminated string that's one char shorter
11266 than the original, stripping off the trailing '\n'. */
11267 char *newstr = alloca (len);
11268 memcpy (newstr, str, len - 1);
11269 newstr[len - 1] = 0;
11271 arg = build_string_literal (len, newstr);
11272 arglist = build_tree_list (NULL_TREE, arg);
11276 /* We'd like to arrange to call fputs(string,stdout) here,
11277 but we need stdout and don't have a way to get it yet. */
11282 /* The other optimizations can be done only on the non-va_list variants. */
11283 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11286 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11287 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11290 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11291 || TREE_CHAIN (arglist))
11296 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11297 else if (strcmp (fmt_str, target_percent_c) == 0)
11300 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11301 || TREE_CHAIN (arglist))
11309 call = build_function_call_expr (fn, arglist);
11310 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11313 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11315 Return 0 if no simplification was possible, otherwise return the
11316 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11317 code of the function to be simplified. */
11320 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11321 enum built_in_function fcode)
11323 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11324 const char *fmt_str = NULL;
11326 /* If the return value is used, don't do the transformation. */
11330 /* Verify the required arguments in the original call. */
11333 fp = TREE_VALUE (arglist);
11334 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11336 arglist = TREE_CHAIN (arglist);
11338 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11344 flag = TREE_VALUE (arglist);
11345 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11346 || TREE_SIDE_EFFECTS (flag))
11348 arglist = TREE_CHAIN (arglist);
11353 fmt = TREE_VALUE (arglist);
11354 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11356 arglist = TREE_CHAIN (arglist);
11358 /* Check whether the format is a literal string constant. */
11359 fmt_str = c_getstr (fmt);
11360 if (fmt_str == NULL)
11363 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11365 /* If we're using an unlocked function, assume the other
11366 unlocked functions exist explicitly. */
11367 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11368 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11372 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11373 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11376 if (!init_target_chars())
11379 /* If the format doesn't contain % args or %%, use strcpy. */
11380 if (strchr (fmt_str, target_percent) == NULL)
11382 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11386 /* If the format specifier was "", fprintf does nothing. */
11387 if (fmt_str[0] == '\0')
11389 /* If FP has side-effects, just wait until gimplification is
11391 if (TREE_SIDE_EFFECTS (fp))
11394 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11397 /* When "string" doesn't contain %, replace all cases of
11398 fprintf (fp, string) with fputs (string, fp). The fputs
11399 builtin will take care of special cases like length == 1. */
11400 arglist = build_tree_list (NULL_TREE, fp);
11401 arglist = tree_cons (NULL_TREE, fmt, arglist);
11405 /* The other optimizations can be done only on the non-va_list variants. */
11406 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11409 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11410 else if (strcmp (fmt_str, target_percent_s) == 0)
11413 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11414 || TREE_CHAIN (arglist))
11416 arg = TREE_VALUE (arglist);
11417 arglist = build_tree_list (NULL_TREE, fp);
11418 arglist = tree_cons (NULL_TREE, arg, arglist);
11422 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11423 else if (strcmp (fmt_str, target_percent_c) == 0)
11426 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11427 || TREE_CHAIN (arglist))
11429 arg = TREE_VALUE (arglist);
11430 arglist = build_tree_list (NULL_TREE, fp);
11431 arglist = tree_cons (NULL_TREE, arg, arglist);
11438 call = build_function_call_expr (fn, arglist);
11439 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11442 /* Initialize format string characters in the target charset. */
11445 init_target_chars (void)
11450 target_newline = lang_hooks.to_target_charset ('\n');
11451 target_percent = lang_hooks.to_target_charset ('%');
11452 target_c = lang_hooks.to_target_charset ('c');
11453 target_s = lang_hooks.to_target_charset ('s');
11454 if (target_newline == 0 || target_percent == 0 || target_c == 0
11458 target_percent_c[0] = target_percent;
11459 target_percent_c[1] = target_c;
11460 target_percent_c[2] = '\0';
11462 target_percent_s[0] = target_percent;
11463 target_percent_s[1] = target_s;
11464 target_percent_s[2] = '\0';
11466 target_percent_s_newline[0] = target_percent;
11467 target_percent_s_newline[1] = target_s;
11468 target_percent_s_newline[2] = target_newline;
11469 target_percent_s_newline[3] = '\0';
11476 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11477 and no overflow/underflow occurred. INEXACT is true if M was not
11478 exacly calculated. TYPE is the tree type for the result. This
11479 function assumes that you cleared the MPFR flags and then
11480 calculated M to see if anything subsequently set a flag prior to
11481 entering this function. Return NULL_TREE if any checks fail. */
11484 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11486 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11487 overflow/underflow occurred. If -frounding-math, proceed iff the
11488 result of calling FUNC was exact. */
11489 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11490 && (!flag_rounding_math || !inexact))
11492 REAL_VALUE_TYPE rr;
11494 real_from_mpfr (&rr, m);
11495 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11496 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11497 but the mpft_t is not, then we underflowed in the
11499 if (!real_isnan (&rr) && !real_isinf (&rr)
11500 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11502 REAL_VALUE_TYPE rmode;
11504 real_convert (&rmode, TYPE_MODE (type), &rr);
11505 /* Proceed iff the specified mode can hold the value. */
11506 if (real_identical (&rmode, &rr))
11507 return build_real (type, rmode);
11513 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11514 FUNC on it and return the resulting value as a tree with type TYPE.
11515 If MIN and/or MAX are not NULL, then the supplied ARG must be
11516 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11517 acceptable values, otherwise they are not. The mpfr precision is
11518 set to the precision of TYPE. We assume that function FUNC returns
11519 zero if the result could be calculated exactly within the requested
11523 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11524 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11527 tree result = NULL_TREE;
11531 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11533 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11535 if (!real_isnan (ra) && !real_isinf (ra)
11536 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11537 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11539 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11543 mpfr_init2 (m, prec);
11544 mpfr_from_real (m, ra);
11545 mpfr_clear_flags();
11546 inexact = func (m, m, GMP_RNDN);
11547 result = do_mpfr_ckconv (m, type, inexact);
11555 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11556 FUNC on it and return the resulting value as a tree with type TYPE.
11557 The mpfr precision is set to the precision of TYPE. We assume that
11558 function FUNC returns zero if the result could be calculated
11559 exactly within the requested precision. */
11562 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11563 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11565 tree result = NULL_TREE;
11570 if (TREE_CODE (arg1) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg1)
11571 && TREE_CODE (arg2) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg2))
11573 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11574 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11576 if (!real_isnan (ra1) && !real_isinf (ra1)
11577 && !real_isnan (ra2) && !real_isinf (ra2))
11579 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11583 mpfr_inits2 (prec, m1, m2, NULL);
11584 mpfr_from_real (m1, ra1);
11585 mpfr_from_real (m2, ra2);
11586 mpfr_clear_flags();
11587 inexact = func (m1, m1, m2, GMP_RNDN);
11588 result = do_mpfr_ckconv (m1, type, inexact);
11589 mpfr_clears (m1, m2, NULL);
11596 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11597 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11598 The type is taken from the type of ARG and is used for setting the
11599 precision of the calculation and results. */
11602 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11604 tree result = NULL_TREE;
11608 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11610 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11612 if (!real_isnan (ra) && !real_isinf (ra))
11614 tree const type = TREE_TYPE (arg);
11615 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11616 tree result_s, result_c;
11620 mpfr_inits2 (prec, m, ms, mc, NULL);
11621 mpfr_from_real (m, ra);
11622 mpfr_clear_flags();
11623 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
11624 result_s = do_mpfr_ckconv (ms, type, inexact);
11625 result_c = do_mpfr_ckconv (mc, type, inexact);
11626 mpfr_clears (m, ms, mc, NULL);
11627 if (result_s && result_c)
11629 /* Dereference the sin/cos pointer arguments. */
11630 arg_sinp = build_fold_indirect_ref (arg_sinp);
11631 arg_cosp = build_fold_indirect_ref (arg_cosp);
11632 /* Proceed if valid pointer type were passed in. */
11633 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11634 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11636 /* Set the values. */
11637 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp, result_s);
11638 TREE_SIDE_EFFECTS (result_s) = 1;
11639 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp, result_c);
11640 TREE_SIDE_EFFECTS (result_c) = 1;
11641 /* Combine the assignments into a compound expr. */
11642 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11643 result_s, result_c));