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_args_info (tree);
99 static rtx expand_builtin_next_arg (void);
100 static rtx expand_builtin_va_start (tree);
101 static rtx expand_builtin_va_end (tree);
102 static rtx expand_builtin_va_copy (tree);
103 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
104 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
106 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
107 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
113 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode, tree);
114 static rtx expand_builtin_bcopy (tree);
115 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
116 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
117 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
118 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
119 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
120 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
122 static rtx expand_builtin_bzero (tree);
123 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, rtx);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static rtx expand_builtin_fputs (tree, rtx, bool);
132 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
133 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
135 static tree stabilize_va_list (tree, int);
136 static rtx expand_builtin_expect (tree, rtx);
137 static tree fold_builtin_constant_p (tree);
138 static tree fold_builtin_classify_type (tree);
139 static tree fold_builtin_strlen (tree);
140 static tree fold_builtin_inf (tree, int);
141 static tree fold_builtin_nan (tree, tree, int);
142 static int validate_arglist (tree, ...);
143 static bool integer_valued_real_p (tree);
144 static tree fold_trunc_transparent_mathfn (tree, tree);
145 static bool readonly_data_expr (tree);
146 static rtx expand_builtin_fabs (tree, rtx, rtx);
147 static rtx expand_builtin_signbit (tree, rtx);
148 static tree fold_builtin_sqrt (tree, tree);
149 static tree fold_builtin_cbrt (tree, tree);
150 static tree fold_builtin_pow (tree, tree, tree);
151 static tree fold_builtin_powi (tree, tree, tree);
152 static tree fold_builtin_sin (tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_tan (tree);
155 static tree fold_builtin_atan (tree, tree);
156 static tree fold_builtin_trunc (tree, tree);
157 static tree fold_builtin_floor (tree, tree);
158 static tree fold_builtin_ceil (tree, tree);
159 static tree fold_builtin_round (tree, tree);
160 static tree fold_builtin_int_roundingfn (tree, tree);
161 static tree fold_builtin_bitop (tree, tree);
162 static tree fold_builtin_memory_op (tree, tree, bool, int);
163 static tree fold_builtin_strchr (tree, tree);
164 static tree fold_builtin_memcmp (tree);
165 static tree fold_builtin_strcmp (tree);
166 static tree fold_builtin_strncmp (tree);
167 static tree fold_builtin_signbit (tree, tree);
168 static tree fold_builtin_copysign (tree, tree, tree);
169 static tree fold_builtin_isascii (tree);
170 static tree fold_builtin_toascii (tree);
171 static tree fold_builtin_isdigit (tree);
172 static tree fold_builtin_fabs (tree, tree);
173 static tree fold_builtin_abs (tree, tree);
174 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
176 static tree fold_builtin_1 (tree, tree, bool);
178 static tree fold_builtin_strpbrk (tree, tree);
179 static tree fold_builtin_strstr (tree, tree);
180 static tree fold_builtin_strrchr (tree, tree);
181 static tree fold_builtin_strcat (tree);
182 static tree fold_builtin_strncat (tree);
183 static tree fold_builtin_strspn (tree);
184 static tree fold_builtin_strcspn (tree);
185 static tree fold_builtin_sprintf (tree, int);
187 static rtx expand_builtin_object_size (tree);
188 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
189 enum built_in_function);
190 static void maybe_emit_chk_warning (tree, enum built_in_function);
191 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
192 static tree fold_builtin_object_size (tree);
193 static tree fold_builtin_strcat_chk (tree, tree);
194 static tree fold_builtin_strncat_chk (tree, tree);
195 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
196 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
197 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
198 static bool init_target_chars (void);
200 static unsigned HOST_WIDE_INT target_newline;
201 static unsigned HOST_WIDE_INT target_percent;
202 static unsigned HOST_WIDE_INT target_c;
203 static unsigned HOST_WIDE_INT target_s;
204 static char target_percent_c[3];
205 static char target_percent_s[3];
206 static char target_percent_s_newline[4];
208 /* Return true if NODE should be considered for inline expansion regardless
209 of the optimization level. This means whenever a function is invoked with
210 its "internal" name, which normally contains the prefix "__builtin". */
212 static bool called_as_built_in (tree node)
214 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
215 if (strncmp (name, "__builtin_", 10) == 0)
217 if (strncmp (name, "__sync_", 7) == 0)
222 /* Return the alignment in bits of EXP, a pointer valued expression.
223 But don't return more than MAX_ALIGN no matter what.
224 The alignment returned is, by default, the alignment of the thing that
225 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
227 Otherwise, look at the expression to see if we can do better, i.e., if the
228 expression is actually pointing at an object whose alignment is tighter. */
231 get_pointer_alignment (tree exp, unsigned int max_align)
233 unsigned int align, inner;
235 /* We rely on TER to compute accurate alignment information. */
236 if (!(optimize && flag_tree_ter))
239 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
242 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
243 align = MIN (align, max_align);
247 switch (TREE_CODE (exp))
251 case NON_LVALUE_EXPR:
252 exp = TREE_OPERAND (exp, 0);
253 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
256 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
257 align = MIN (inner, max_align);
261 /* If sum of pointer + int, restrict our maximum alignment to that
262 imposed by the integer. If not, we can't do any better than
264 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
267 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
268 & (max_align / BITS_PER_UNIT - 1))
272 exp = TREE_OPERAND (exp, 0);
276 /* See what we are pointing at and look at its alignment. */
277 exp = TREE_OPERAND (exp, 0);
279 if (handled_component_p (exp))
281 HOST_WIDE_INT bitsize, bitpos;
283 enum machine_mode mode;
284 int unsignedp, volatilep;
286 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
287 &mode, &unsignedp, &volatilep, true);
289 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
290 if (offset && TREE_CODE (offset) == PLUS_EXPR
291 && host_integerp (TREE_OPERAND (offset, 1), 1))
293 /* Any overflow in calculating offset_bits won't change
296 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
300 inner = MIN (inner, (offset_bits & -offset_bits));
301 offset = TREE_OPERAND (offset, 0);
303 if (offset && TREE_CODE (offset) == MULT_EXPR
304 && host_integerp (TREE_OPERAND (offset, 1), 1))
306 /* Any overflow in calculating offset_factor won't change
308 unsigned offset_factor
309 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
313 inner = MIN (inner, (offset_factor & -offset_factor));
316 inner = MIN (inner, BITS_PER_UNIT);
319 align = MIN (inner, DECL_ALIGN (exp));
320 #ifdef CONSTANT_ALIGNMENT
321 else if (CONSTANT_CLASS_P (exp))
322 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
324 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
325 || TREE_CODE (exp) == INDIRECT_REF)
326 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
328 align = MIN (align, inner);
329 return MIN (align, max_align);
337 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
338 way, because it could contain a zero byte in the middle.
339 TREE_STRING_LENGTH is the size of the character array, not the string.
341 ONLY_VALUE should be nonzero if the result is not going to be emitted
342 into the instruction stream and zero if it is going to be expanded.
343 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
344 is returned, otherwise NULL, since
345 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
346 evaluate the side-effects.
348 The value returned is of type `ssizetype'.
350 Unfortunately, string_constant can't access the values of const char
351 arrays with initializers, so neither can we do so here. */
354 c_strlen (tree src, int only_value)
357 HOST_WIDE_INT offset;
362 if (TREE_CODE (src) == COND_EXPR
363 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
367 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
368 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
369 if (tree_int_cst_equal (len1, len2))
373 if (TREE_CODE (src) == COMPOUND_EXPR
374 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
375 return c_strlen (TREE_OPERAND (src, 1), only_value);
377 src = string_constant (src, &offset_node);
381 max = TREE_STRING_LENGTH (src) - 1;
382 ptr = TREE_STRING_POINTER (src);
384 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
386 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
387 compute the offset to the following null if we don't know where to
388 start searching for it. */
391 for (i = 0; i < max; i++)
395 /* We don't know the starting offset, but we do know that the string
396 has no internal zero bytes. We can assume that the offset falls
397 within the bounds of the string; otherwise, the programmer deserves
398 what he gets. Subtract the offset from the length of the string,
399 and return that. This would perhaps not be valid if we were dealing
400 with named arrays in addition to literal string constants. */
402 return size_diffop (size_int (max), offset_node);
405 /* We have a known offset into the string. Start searching there for
406 a null character if we can represent it as a single HOST_WIDE_INT. */
407 if (offset_node == 0)
409 else if (! host_integerp (offset_node, 0))
412 offset = tree_low_cst (offset_node, 0);
414 /* If the offset is known to be out of bounds, warn, and call strlen at
416 if (offset < 0 || offset > max)
418 warning (0, "offset outside bounds of constant string");
422 /* Use strlen to search for the first zero byte. Since any strings
423 constructed with build_string will have nulls appended, we win even
424 if we get handed something like (char[4])"abcd".
426 Since OFFSET is our starting index into the string, no further
427 calculation is needed. */
428 return ssize_int (strlen (ptr + offset));
431 /* Return a char pointer for a C string if it is a string constant
432 or sum of string constant and integer constant. */
439 src = string_constant (src, &offset_node);
443 if (offset_node == 0)
444 return TREE_STRING_POINTER (src);
445 else if (!host_integerp (offset_node, 1)
446 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
449 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
452 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
453 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
456 c_readstr (const char *str, enum machine_mode mode)
462 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
467 for (i = 0; i < GET_MODE_SIZE (mode); i++)
470 if (WORDS_BIG_ENDIAN)
471 j = GET_MODE_SIZE (mode) - i - 1;
472 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
473 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
474 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
476 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
479 ch = (unsigned char) str[i];
480 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
482 return immed_double_const (c[0], c[1], mode);
485 /* Cast a target constant CST to target CHAR and if that value fits into
486 host char type, return zero and put that value into variable pointed to by
490 target_char_cast (tree cst, char *p)
492 unsigned HOST_WIDE_INT val, hostval;
494 if (!host_integerp (cst, 1)
495 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
498 val = tree_low_cst (cst, 1);
499 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
500 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
503 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
504 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
513 /* Similar to save_expr, but assumes that arbitrary code is not executed
514 in between the multiple evaluations. In particular, we assume that a
515 non-addressable local variable will not be modified. */
518 builtin_save_expr (tree exp)
520 if (TREE_ADDRESSABLE (exp) == 0
521 && (TREE_CODE (exp) == PARM_DECL
522 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
525 return save_expr (exp);
528 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
529 times to get the address of either a higher stack frame, or a return
530 address located within it (depending on FNDECL_CODE). */
533 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
537 #ifdef INITIAL_FRAME_ADDRESS_RTX
538 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
542 /* For a zero count with __builtin_return_address, we don't care what
543 frame address we return, because target-specific definitions will
544 override us. Therefore frame pointer elimination is OK, and using
545 the soft frame pointer is OK.
547 For a non-zero count, or a zero count with __builtin_frame_address,
548 we require a stable offset from the current frame pointer to the
549 previous one, so we must use the hard frame pointer, and
550 we must disable frame pointer elimination. */
551 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
552 tem = frame_pointer_rtx;
555 tem = hard_frame_pointer_rtx;
557 /* Tell reload not to eliminate the frame pointer. */
558 current_function_accesses_prior_frames = 1;
562 /* Some machines need special handling before we can access
563 arbitrary frames. For example, on the SPARC, we must first flush
564 all register windows to the stack. */
565 #ifdef SETUP_FRAME_ADDRESSES
567 SETUP_FRAME_ADDRESSES ();
570 /* On the SPARC, the return address is not in the frame, it is in a
571 register. There is no way to access it off of the current frame
572 pointer, but it can be accessed off the previous frame pointer by
573 reading the value from the register window save area. */
574 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
575 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
579 /* Scan back COUNT frames to the specified frame. */
580 for (i = 0; i < count; i++)
582 /* Assume the dynamic chain pointer is in the word that the
583 frame address points to, unless otherwise specified. */
584 #ifdef DYNAMIC_CHAIN_ADDRESS
585 tem = DYNAMIC_CHAIN_ADDRESS (tem);
587 tem = memory_address (Pmode, tem);
588 tem = gen_frame_mem (Pmode, tem);
589 tem = copy_to_reg (tem);
592 /* For __builtin_frame_address, return what we've got. But, on
593 the SPARC for example, we may have to add a bias. */
594 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
595 #ifdef FRAME_ADDR_RTX
596 return FRAME_ADDR_RTX (tem);
601 /* For __builtin_return_address, get the return address from that frame. */
602 #ifdef RETURN_ADDR_RTX
603 tem = RETURN_ADDR_RTX (count, tem);
605 tem = memory_address (Pmode,
606 plus_constant (tem, GET_MODE_SIZE (Pmode)));
607 tem = gen_frame_mem (Pmode, tem);
612 /* Alias set used for setjmp buffer. */
613 static HOST_WIDE_INT setjmp_alias_set = -1;
615 /* Construct the leading half of a __builtin_setjmp call. Control will
616 return to RECEIVER_LABEL. This is also called directly by the SJLJ
617 exception handling code. */
620 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
622 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
626 if (setjmp_alias_set == -1)
627 setjmp_alias_set = new_alias_set ();
629 buf_addr = convert_memory_address (Pmode, buf_addr);
631 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
633 /* We store the frame pointer and the address of receiver_label in
634 the buffer and use the rest of it for the stack save area, which
635 is machine-dependent. */
637 mem = gen_rtx_MEM (Pmode, buf_addr);
638 set_mem_alias_set (mem, setjmp_alias_set);
639 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
641 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
642 set_mem_alias_set (mem, setjmp_alias_set);
644 emit_move_insn (validize_mem (mem),
645 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
647 stack_save = gen_rtx_MEM (sa_mode,
648 plus_constant (buf_addr,
649 2 * GET_MODE_SIZE (Pmode)));
650 set_mem_alias_set (stack_save, setjmp_alias_set);
651 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
653 /* If there is further processing to do, do it. */
654 #ifdef HAVE_builtin_setjmp_setup
655 if (HAVE_builtin_setjmp_setup)
656 emit_insn (gen_builtin_setjmp_setup (buf_addr));
659 /* Tell optimize_save_area_alloca that extra work is going to
660 need to go on during alloca. */
661 current_function_calls_setjmp = 1;
663 /* Set this so all the registers get saved in our frame; we need to be
664 able to copy the saved values for any registers from frames we unwind. */
665 current_function_has_nonlocal_label = 1;
668 /* Construct the trailing part of a __builtin_setjmp call. This is
669 also called directly by the SJLJ exception handling code. */
672 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
674 /* Clobber the FP when we get here, so we have to make sure it's
675 marked as used by this function. */
676 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
678 /* Mark the static chain as clobbered here so life information
679 doesn't get messed up for it. */
680 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
682 /* Now put in the code to restore the frame pointer, and argument
683 pointer, if needed. */
684 #ifdef HAVE_nonlocal_goto
685 if (! HAVE_nonlocal_goto)
688 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
689 /* This might change the hard frame pointer in ways that aren't
690 apparent to early optimization passes, so force a clobber. */
691 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
694 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
695 if (fixed_regs[ARG_POINTER_REGNUM])
697 #ifdef ELIMINABLE_REGS
699 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
701 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
702 if (elim_regs[i].from == ARG_POINTER_REGNUM
703 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
706 if (i == ARRAY_SIZE (elim_regs))
709 /* Now restore our arg pointer from the address at which it
710 was saved in our stack frame. */
711 emit_move_insn (virtual_incoming_args_rtx,
712 copy_to_reg (get_arg_pointer_save_area (cfun)));
717 #ifdef HAVE_builtin_setjmp_receiver
718 if (HAVE_builtin_setjmp_receiver)
719 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
722 #ifdef HAVE_nonlocal_goto_receiver
723 if (HAVE_nonlocal_goto_receiver)
724 emit_insn (gen_nonlocal_goto_receiver ());
729 /* @@@ This is a kludge. Not all machine descriptions define a blockage
730 insn, but we must not allow the code we just generated to be reordered
731 by scheduling. Specifically, the update of the frame pointer must
732 happen immediately, not later. So emit an ASM_INPUT to act as blockage
734 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
737 /* __builtin_longjmp is passed a pointer to an array of five words (not
738 all will be used on all machines). It operates similarly to the C
739 library function of the same name, but is more efficient. Much of
740 the code below is copied from the handling of non-local gotos. */
743 expand_builtin_longjmp (rtx buf_addr, rtx value)
745 rtx fp, lab, stack, insn, last;
746 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
748 if (setjmp_alias_set == -1)
749 setjmp_alias_set = new_alias_set ();
751 buf_addr = convert_memory_address (Pmode, buf_addr);
753 buf_addr = force_reg (Pmode, buf_addr);
755 /* We used to store value in static_chain_rtx, but that fails if pointers
756 are smaller than integers. We instead require that the user must pass
757 a second argument of 1, because that is what builtin_setjmp will
758 return. This also makes EH slightly more efficient, since we are no
759 longer copying around a value that we don't care about. */
760 gcc_assert (value == const1_rtx);
762 last = get_last_insn ();
763 #ifdef HAVE_builtin_longjmp
764 if (HAVE_builtin_longjmp)
765 emit_insn (gen_builtin_longjmp (buf_addr));
769 fp = gen_rtx_MEM (Pmode, buf_addr);
770 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
771 GET_MODE_SIZE (Pmode)));
773 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
774 2 * GET_MODE_SIZE (Pmode)));
775 set_mem_alias_set (fp, setjmp_alias_set);
776 set_mem_alias_set (lab, setjmp_alias_set);
777 set_mem_alias_set (stack, setjmp_alias_set);
779 /* Pick up FP, label, and SP from the block and jump. This code is
780 from expand_goto in stmt.c; see there for detailed comments. */
781 #ifdef HAVE_nonlocal_goto
782 if (HAVE_nonlocal_goto)
783 /* We have to pass a value to the nonlocal_goto pattern that will
784 get copied into the static_chain pointer, but it does not matter
785 what that value is, because builtin_setjmp does not use it. */
786 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
790 lab = copy_to_reg (lab);
792 emit_insn (gen_rtx_CLOBBER (VOIDmode,
793 gen_rtx_MEM (BLKmode,
794 gen_rtx_SCRATCH (VOIDmode))));
795 emit_insn (gen_rtx_CLOBBER (VOIDmode,
796 gen_rtx_MEM (BLKmode,
797 hard_frame_pointer_rtx)));
799 emit_move_insn (hard_frame_pointer_rtx, fp);
800 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
802 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
803 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
804 emit_indirect_jump (lab);
808 /* Search backwards and mark the jump insn as a non-local goto.
809 Note that this precludes the use of __builtin_longjmp to a
810 __builtin_setjmp target in the same function. However, we've
811 already cautioned the user that these functions are for
812 internal exception handling use only. */
813 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
815 gcc_assert (insn != last);
819 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
823 else if (CALL_P (insn))
828 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
829 and the address of the save area. */
832 expand_builtin_nonlocal_goto (tree arglist)
834 tree t_label, t_save_area;
835 rtx r_label, r_save_area, r_fp, r_sp, insn;
837 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
840 t_label = TREE_VALUE (arglist);
841 arglist = TREE_CHAIN (arglist);
842 t_save_area = TREE_VALUE (arglist);
844 r_label = expand_normal (t_label);
845 r_label = convert_memory_address (Pmode, r_label);
846 r_save_area = expand_normal (t_save_area);
847 r_save_area = convert_memory_address (Pmode, r_save_area);
848 r_fp = gen_rtx_MEM (Pmode, r_save_area);
849 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
850 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
852 current_function_has_nonlocal_goto = 1;
854 #ifdef HAVE_nonlocal_goto
855 /* ??? We no longer need to pass the static chain value, afaik. */
856 if (HAVE_nonlocal_goto)
857 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
861 r_label = copy_to_reg (r_label);
863 emit_insn (gen_rtx_CLOBBER (VOIDmode,
864 gen_rtx_MEM (BLKmode,
865 gen_rtx_SCRATCH (VOIDmode))));
867 emit_insn (gen_rtx_CLOBBER (VOIDmode,
868 gen_rtx_MEM (BLKmode,
869 hard_frame_pointer_rtx)));
871 /* Restore frame pointer for containing function.
872 This sets the actual hard register used for the frame pointer
873 to the location of the function's incoming static chain info.
874 The non-local goto handler will then adjust it to contain the
875 proper value and reload the argument pointer, if needed. */
876 emit_move_insn (hard_frame_pointer_rtx, r_fp);
877 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
879 /* USE of hard_frame_pointer_rtx added for consistency;
880 not clear if really needed. */
881 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
882 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
883 emit_indirect_jump (r_label);
886 /* Search backwards to the jump insn and mark it as a
888 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
892 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
893 const0_rtx, REG_NOTES (insn));
896 else if (CALL_P (insn))
903 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
904 (not all will be used on all machines) that was passed to __builtin_setjmp.
905 It updates the stack pointer in that block to correspond to the current
909 expand_builtin_update_setjmp_buf (rtx buf_addr)
911 enum machine_mode sa_mode = Pmode;
915 #ifdef HAVE_save_stack_nonlocal
916 if (HAVE_save_stack_nonlocal)
917 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
919 #ifdef STACK_SAVEAREA_MODE
920 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
924 = gen_rtx_MEM (sa_mode,
927 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
931 emit_insn (gen_setjmp ());
934 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
937 /* Expand a call to __builtin_prefetch. For a target that does not support
938 data prefetch, evaluate the memory address argument in case it has side
942 expand_builtin_prefetch (tree arglist)
944 tree arg0, arg1, arg2;
947 if (!validate_arglist (arglist, POINTER_TYPE, 0))
950 arg0 = TREE_VALUE (arglist);
951 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
952 zero (read) and argument 2 (locality) defaults to 3 (high degree of
954 if (TREE_CHAIN (arglist))
956 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
957 if (TREE_CHAIN (TREE_CHAIN (arglist)))
958 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
960 arg2 = build_int_cst (NULL_TREE, 3);
964 arg1 = integer_zero_node;
965 arg2 = build_int_cst (NULL_TREE, 3);
968 /* Argument 0 is an address. */
969 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
971 /* Argument 1 (read/write flag) must be a compile-time constant int. */
972 if (TREE_CODE (arg1) != INTEGER_CST)
974 error ("second argument to %<__builtin_prefetch%> must be a constant");
975 arg1 = integer_zero_node;
977 op1 = expand_normal (arg1);
978 /* Argument 1 must be either zero or one. */
979 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
981 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
986 /* Argument 2 (locality) must be a compile-time constant int. */
987 if (TREE_CODE (arg2) != INTEGER_CST)
989 error ("third argument to %<__builtin_prefetch%> must be a constant");
990 arg2 = integer_zero_node;
992 op2 = expand_normal (arg2);
993 /* Argument 2 must be 0, 1, 2, or 3. */
994 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
996 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1000 #ifdef HAVE_prefetch
1003 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1005 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1006 || (GET_MODE (op0) != Pmode))
1008 op0 = convert_memory_address (Pmode, op0);
1009 op0 = force_reg (Pmode, op0);
1011 emit_insn (gen_prefetch (op0, op1, op2));
1015 /* Don't do anything with direct references to volatile memory, but
1016 generate code to handle other side effects. */
1017 if (!MEM_P (op0) && side_effects_p (op0))
1021 /* Get a MEM rtx for expression EXP which is the address of an operand
1022 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1023 the maximum length of the block of memory that might be accessed or
1027 get_memory_rtx (tree exp, tree len)
1029 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1030 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1032 /* Get an expression we can use to find the attributes to assign to MEM.
1033 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1034 we can. First remove any nops. */
1035 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1036 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1037 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1038 exp = TREE_OPERAND (exp, 0);
1040 if (TREE_CODE (exp) == ADDR_EXPR)
1041 exp = TREE_OPERAND (exp, 0);
1042 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1043 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1047 /* Honor attributes derived from exp, except for the alias set
1048 (as builtin stringops may alias with anything) and the size
1049 (as stringops may access multiple array elements). */
1052 set_mem_attributes (mem, exp, 0);
1054 /* Allow the string and memory builtins to overflow from one
1055 field into another, see http://gcc.gnu.org/PR23561.
1056 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1057 memory accessed by the string or memory builtin will fit
1058 within the field. */
1059 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1061 tree mem_expr = MEM_EXPR (mem);
1062 HOST_WIDE_INT offset = -1, length = -1;
1065 while (TREE_CODE (inner) == ARRAY_REF
1066 || TREE_CODE (inner) == NOP_EXPR
1067 || TREE_CODE (inner) == CONVERT_EXPR
1068 || TREE_CODE (inner) == NON_LVALUE_EXPR
1069 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1070 || TREE_CODE (inner) == SAVE_EXPR)
1071 inner = TREE_OPERAND (inner, 0);
1073 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1075 if (MEM_OFFSET (mem)
1076 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1077 offset = INTVAL (MEM_OFFSET (mem));
1079 if (offset >= 0 && len && host_integerp (len, 0))
1080 length = tree_low_cst (len, 0);
1082 while (TREE_CODE (inner) == COMPONENT_REF)
1084 tree field = TREE_OPERAND (inner, 1);
1085 gcc_assert (! DECL_BIT_FIELD (field));
1086 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1087 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1090 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1091 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1094 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1095 /* If we can prove the memory starting at XEXP (mem, 0)
1096 and ending at XEXP (mem, 0) + LENGTH will fit into
1097 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1100 && offset + length <= size)
1105 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1106 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1107 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1115 mem_expr = TREE_OPERAND (mem_expr, 0);
1116 inner = TREE_OPERAND (inner, 0);
1119 if (mem_expr == NULL)
1121 if (mem_expr != MEM_EXPR (mem))
1123 set_mem_expr (mem, mem_expr);
1124 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1127 set_mem_alias_set (mem, 0);
1128 set_mem_size (mem, NULL_RTX);
1134 /* Built-in functions to perform an untyped call and return. */
1136 /* For each register that may be used for calling a function, this
1137 gives a mode used to copy the register's value. VOIDmode indicates
1138 the register is not used for calling a function. If the machine
1139 has register windows, this gives only the outbound registers.
1140 INCOMING_REGNO gives the corresponding inbound register. */
1141 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1143 /* For each register that may be used for returning values, this gives
1144 a mode used to copy the register's value. VOIDmode indicates the
1145 register is not used for returning values. If the machine has
1146 register windows, this gives only the outbound registers.
1147 INCOMING_REGNO gives the corresponding inbound register. */
1148 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1150 /* For each register that may be used for calling a function, this
1151 gives the offset of that register into the block returned by
1152 __builtin_apply_args. 0 indicates that the register is not
1153 used for calling a function. */
1154 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1156 /* Return the size required for the block returned by __builtin_apply_args,
1157 and initialize apply_args_mode. */
1160 apply_args_size (void)
1162 static int size = -1;
1165 enum machine_mode mode;
1167 /* The values computed by this function never change. */
1170 /* The first value is the incoming arg-pointer. */
1171 size = GET_MODE_SIZE (Pmode);
1173 /* The second value is the structure value address unless this is
1174 passed as an "invisible" first argument. */
1175 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1176 size += GET_MODE_SIZE (Pmode);
1178 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1179 if (FUNCTION_ARG_REGNO_P (regno))
1181 mode = reg_raw_mode[regno];
1183 gcc_assert (mode != VOIDmode);
1185 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1186 if (size % align != 0)
1187 size = CEIL (size, align) * align;
1188 apply_args_reg_offset[regno] = size;
1189 size += GET_MODE_SIZE (mode);
1190 apply_args_mode[regno] = mode;
1194 apply_args_mode[regno] = VOIDmode;
1195 apply_args_reg_offset[regno] = 0;
1201 /* Return the size required for the block returned by __builtin_apply,
1202 and initialize apply_result_mode. */
1205 apply_result_size (void)
1207 static int size = -1;
1209 enum machine_mode mode;
1211 /* The values computed by this function never change. */
1216 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1217 if (FUNCTION_VALUE_REGNO_P (regno))
1219 mode = reg_raw_mode[regno];
1221 gcc_assert (mode != VOIDmode);
1223 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1224 if (size % align != 0)
1225 size = CEIL (size, align) * align;
1226 size += GET_MODE_SIZE (mode);
1227 apply_result_mode[regno] = mode;
1230 apply_result_mode[regno] = VOIDmode;
1232 /* Allow targets that use untyped_call and untyped_return to override
1233 the size so that machine-specific information can be stored here. */
1234 #ifdef APPLY_RESULT_SIZE
1235 size = APPLY_RESULT_SIZE;
1241 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1242 /* Create a vector describing the result block RESULT. If SAVEP is true,
1243 the result block is used to save the values; otherwise it is used to
1244 restore the values. */
1247 result_vector (int savep, rtx result)
1249 int regno, size, align, nelts;
1250 enum machine_mode mode;
1252 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1255 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1256 if ((mode = apply_result_mode[regno]) != VOIDmode)
1258 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1259 if (size % align != 0)
1260 size = CEIL (size, align) * align;
1261 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1262 mem = adjust_address (result, mode, size);
1263 savevec[nelts++] = (savep
1264 ? gen_rtx_SET (VOIDmode, mem, reg)
1265 : gen_rtx_SET (VOIDmode, reg, mem));
1266 size += GET_MODE_SIZE (mode);
1268 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1270 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1272 /* Save the state required to perform an untyped call with the same
1273 arguments as were passed to the current function. */
1276 expand_builtin_apply_args_1 (void)
1279 int size, align, regno;
1280 enum machine_mode mode;
1281 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1283 /* Create a block where the arg-pointer, structure value address,
1284 and argument registers can be saved. */
1285 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1287 /* Walk past the arg-pointer and structure value address. */
1288 size = GET_MODE_SIZE (Pmode);
1289 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1290 size += GET_MODE_SIZE (Pmode);
1292 /* Save each register used in calling a function to the block. */
1293 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1294 if ((mode = apply_args_mode[regno]) != VOIDmode)
1296 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1297 if (size % align != 0)
1298 size = CEIL (size, align) * align;
1300 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1302 emit_move_insn (adjust_address (registers, mode, size), tem);
1303 size += GET_MODE_SIZE (mode);
1306 /* Save the arg pointer to the block. */
1307 tem = copy_to_reg (virtual_incoming_args_rtx);
1308 #ifdef STACK_GROWS_DOWNWARD
1309 /* We need the pointer as the caller actually passed them to us, not
1310 as we might have pretended they were passed. Make sure it's a valid
1311 operand, as emit_move_insn isn't expected to handle a PLUS. */
1313 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1316 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1318 size = GET_MODE_SIZE (Pmode);
1320 /* Save the structure value address unless this is passed as an
1321 "invisible" first argument. */
1322 if (struct_incoming_value)
1324 emit_move_insn (adjust_address (registers, Pmode, size),
1325 copy_to_reg (struct_incoming_value));
1326 size += GET_MODE_SIZE (Pmode);
1329 /* Return the address of the block. */
1330 return copy_addr_to_reg (XEXP (registers, 0));
1333 /* __builtin_apply_args returns block of memory allocated on
1334 the stack into which is stored the arg pointer, structure
1335 value address, static chain, and all the registers that might
1336 possibly be used in performing a function call. The code is
1337 moved to the start of the function so the incoming values are
1341 expand_builtin_apply_args (void)
1343 /* Don't do __builtin_apply_args more than once in a function.
1344 Save the result of the first call and reuse it. */
1345 if (apply_args_value != 0)
1346 return apply_args_value;
1348 /* When this function is called, it means that registers must be
1349 saved on entry to this function. So we migrate the
1350 call to the first insn of this function. */
1355 temp = expand_builtin_apply_args_1 ();
1359 apply_args_value = temp;
1361 /* Put the insns after the NOTE that starts the function.
1362 If this is inside a start_sequence, make the outer-level insn
1363 chain current, so the code is placed at the start of the
1365 push_topmost_sequence ();
1366 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1367 pop_topmost_sequence ();
1372 /* Perform an untyped call and save the state required to perform an
1373 untyped return of whatever value was returned by the given function. */
1376 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1378 int size, align, regno;
1379 enum machine_mode mode;
1380 rtx incoming_args, result, reg, dest, src, call_insn;
1381 rtx old_stack_level = 0;
1382 rtx call_fusage = 0;
1383 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1385 arguments = convert_memory_address (Pmode, arguments);
1387 /* Create a block where the return registers can be saved. */
1388 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1390 /* Fetch the arg pointer from the ARGUMENTS block. */
1391 incoming_args = gen_reg_rtx (Pmode);
1392 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1393 #ifndef STACK_GROWS_DOWNWARD
1394 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1395 incoming_args, 0, OPTAB_LIB_WIDEN);
1398 /* Push a new argument block and copy the arguments. Do not allow
1399 the (potential) memcpy call below to interfere with our stack
1401 do_pending_stack_adjust ();
1404 /* Save the stack with nonlocal if available. */
1405 #ifdef HAVE_save_stack_nonlocal
1406 if (HAVE_save_stack_nonlocal)
1407 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1410 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1412 /* Allocate a block of memory onto the stack and copy the memory
1413 arguments to the outgoing arguments address. */
1414 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1415 dest = virtual_outgoing_args_rtx;
1416 #ifndef STACK_GROWS_DOWNWARD
1417 if (GET_CODE (argsize) == CONST_INT)
1418 dest = plus_constant (dest, -INTVAL (argsize));
1420 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1422 dest = gen_rtx_MEM (BLKmode, dest);
1423 set_mem_align (dest, PARM_BOUNDARY);
1424 src = gen_rtx_MEM (BLKmode, incoming_args);
1425 set_mem_align (src, PARM_BOUNDARY);
1426 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1428 /* Refer to the argument block. */
1430 arguments = gen_rtx_MEM (BLKmode, arguments);
1431 set_mem_align (arguments, PARM_BOUNDARY);
1433 /* Walk past the arg-pointer and structure value address. */
1434 size = GET_MODE_SIZE (Pmode);
1436 size += GET_MODE_SIZE (Pmode);
1438 /* Restore each of the registers previously saved. Make USE insns
1439 for each of these registers for use in making the call. */
1440 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1441 if ((mode = apply_args_mode[regno]) != VOIDmode)
1443 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1444 if (size % align != 0)
1445 size = CEIL (size, align) * align;
1446 reg = gen_rtx_REG (mode, regno);
1447 emit_move_insn (reg, adjust_address (arguments, mode, size));
1448 use_reg (&call_fusage, reg);
1449 size += GET_MODE_SIZE (mode);
1452 /* Restore the structure value address unless this is passed as an
1453 "invisible" first argument. */
1454 size = GET_MODE_SIZE (Pmode);
1457 rtx value = gen_reg_rtx (Pmode);
1458 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1459 emit_move_insn (struct_value, value);
1460 if (REG_P (struct_value))
1461 use_reg (&call_fusage, struct_value);
1462 size += GET_MODE_SIZE (Pmode);
1465 /* All arguments and registers used for the call are set up by now! */
1466 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1468 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1469 and we don't want to load it into a register as an optimization,
1470 because prepare_call_address already did it if it should be done. */
1471 if (GET_CODE (function) != SYMBOL_REF)
1472 function = memory_address (FUNCTION_MODE, function);
1474 /* Generate the actual call instruction and save the return value. */
1475 #ifdef HAVE_untyped_call
1476 if (HAVE_untyped_call)
1477 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1478 result, result_vector (1, result)));
1481 #ifdef HAVE_call_value
1482 if (HAVE_call_value)
1486 /* Locate the unique return register. It is not possible to
1487 express a call that sets more than one return register using
1488 call_value; use untyped_call for that. In fact, untyped_call
1489 only needs to save the return registers in the given block. */
1490 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1491 if ((mode = apply_result_mode[regno]) != VOIDmode)
1493 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1495 valreg = gen_rtx_REG (mode, regno);
1498 emit_call_insn (GEN_CALL_VALUE (valreg,
1499 gen_rtx_MEM (FUNCTION_MODE, function),
1500 const0_rtx, NULL_RTX, const0_rtx));
1502 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1508 /* Find the CALL insn we just emitted, and attach the register usage
1510 call_insn = last_call_insn ();
1511 add_function_usage_to (call_insn, call_fusage);
1513 /* Restore the stack. */
1514 #ifdef HAVE_save_stack_nonlocal
1515 if (HAVE_save_stack_nonlocal)
1516 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1519 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1523 /* Return the address of the result block. */
1524 result = copy_addr_to_reg (XEXP (result, 0));
1525 return convert_memory_address (ptr_mode, result);
1528 /* Perform an untyped return. */
1531 expand_builtin_return (rtx result)
1533 int size, align, regno;
1534 enum machine_mode mode;
1536 rtx call_fusage = 0;
1538 result = convert_memory_address (Pmode, result);
1540 apply_result_size ();
1541 result = gen_rtx_MEM (BLKmode, result);
1543 #ifdef HAVE_untyped_return
1544 if (HAVE_untyped_return)
1546 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1552 /* Restore the return value and note that each value is used. */
1554 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1555 if ((mode = apply_result_mode[regno]) != VOIDmode)
1557 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1558 if (size % align != 0)
1559 size = CEIL (size, align) * align;
1560 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1561 emit_move_insn (reg, adjust_address (result, mode, size));
1563 push_to_sequence (call_fusage);
1564 emit_insn (gen_rtx_USE (VOIDmode, reg));
1565 call_fusage = get_insns ();
1567 size += GET_MODE_SIZE (mode);
1570 /* Put the USE insns before the return. */
1571 emit_insn (call_fusage);
1573 /* Return whatever values was restored by jumping directly to the end
1575 expand_naked_return ();
1578 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1580 static enum type_class
1581 type_to_class (tree type)
1583 switch (TREE_CODE (type))
1585 case VOID_TYPE: return void_type_class;
1586 case INTEGER_TYPE: return integer_type_class;
1587 case ENUMERAL_TYPE: return enumeral_type_class;
1588 case BOOLEAN_TYPE: return boolean_type_class;
1589 case POINTER_TYPE: return pointer_type_class;
1590 case REFERENCE_TYPE: return reference_type_class;
1591 case OFFSET_TYPE: return offset_type_class;
1592 case REAL_TYPE: return real_type_class;
1593 case COMPLEX_TYPE: return complex_type_class;
1594 case FUNCTION_TYPE: return function_type_class;
1595 case METHOD_TYPE: return method_type_class;
1596 case RECORD_TYPE: return record_type_class;
1598 case QUAL_UNION_TYPE: return union_type_class;
1599 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1600 ? string_type_class : array_type_class);
1601 case LANG_TYPE: return lang_type_class;
1602 default: return no_type_class;
1606 /* Expand a call to __builtin_classify_type with arguments found in
1610 expand_builtin_classify_type (tree arglist)
1613 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1614 return GEN_INT (no_type_class);
1617 /* This helper macro, meant to be used in mathfn_built_in below,
1618 determines which among a set of three builtin math functions is
1619 appropriate for a given type mode. The `F' and `L' cases are
1620 automatically generated from the `double' case. */
1621 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1622 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1623 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1624 fcodel = BUILT_IN_MATHFN##L ; break;
1626 /* Return mathematic function equivalent to FN but operating directly
1627 on TYPE, if available. If we can't do the conversion, return zero. */
1629 mathfn_built_in (tree type, enum built_in_function fn)
1631 enum built_in_function fcode, fcodef, fcodel;
1635 CASE_MATHFN (BUILT_IN_ACOS)
1636 CASE_MATHFN (BUILT_IN_ACOSH)
1637 CASE_MATHFN (BUILT_IN_ASIN)
1638 CASE_MATHFN (BUILT_IN_ASINH)
1639 CASE_MATHFN (BUILT_IN_ATAN)
1640 CASE_MATHFN (BUILT_IN_ATAN2)
1641 CASE_MATHFN (BUILT_IN_ATANH)
1642 CASE_MATHFN (BUILT_IN_CBRT)
1643 CASE_MATHFN (BUILT_IN_CEIL)
1644 CASE_MATHFN (BUILT_IN_COPYSIGN)
1645 CASE_MATHFN (BUILT_IN_COS)
1646 CASE_MATHFN (BUILT_IN_COSH)
1647 CASE_MATHFN (BUILT_IN_DREM)
1648 CASE_MATHFN (BUILT_IN_ERF)
1649 CASE_MATHFN (BUILT_IN_ERFC)
1650 CASE_MATHFN (BUILT_IN_EXP)
1651 CASE_MATHFN (BUILT_IN_EXP10)
1652 CASE_MATHFN (BUILT_IN_EXP2)
1653 CASE_MATHFN (BUILT_IN_EXPM1)
1654 CASE_MATHFN (BUILT_IN_FABS)
1655 CASE_MATHFN (BUILT_IN_FDIM)
1656 CASE_MATHFN (BUILT_IN_FLOOR)
1657 CASE_MATHFN (BUILT_IN_FMA)
1658 CASE_MATHFN (BUILT_IN_FMAX)
1659 CASE_MATHFN (BUILT_IN_FMIN)
1660 CASE_MATHFN (BUILT_IN_FMOD)
1661 CASE_MATHFN (BUILT_IN_FREXP)
1662 CASE_MATHFN (BUILT_IN_GAMMA)
1663 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1664 CASE_MATHFN (BUILT_IN_HYPOT)
1665 CASE_MATHFN (BUILT_IN_ILOGB)
1666 CASE_MATHFN (BUILT_IN_INF)
1667 CASE_MATHFN (BUILT_IN_J0)
1668 CASE_MATHFN (BUILT_IN_J1)
1669 CASE_MATHFN (BUILT_IN_JN)
1670 CASE_MATHFN (BUILT_IN_LCEIL)
1671 CASE_MATHFN (BUILT_IN_LDEXP)
1672 CASE_MATHFN (BUILT_IN_LFLOOR)
1673 CASE_MATHFN (BUILT_IN_LGAMMA)
1674 CASE_MATHFN (BUILT_IN_LLCEIL)
1675 CASE_MATHFN (BUILT_IN_LLFLOOR)
1676 CASE_MATHFN (BUILT_IN_LLRINT)
1677 CASE_MATHFN (BUILT_IN_LLROUND)
1678 CASE_MATHFN (BUILT_IN_LOG)
1679 CASE_MATHFN (BUILT_IN_LOG10)
1680 CASE_MATHFN (BUILT_IN_LOG1P)
1681 CASE_MATHFN (BUILT_IN_LOG2)
1682 CASE_MATHFN (BUILT_IN_LOGB)
1683 CASE_MATHFN (BUILT_IN_LRINT)
1684 CASE_MATHFN (BUILT_IN_LROUND)
1685 CASE_MATHFN (BUILT_IN_MODF)
1686 CASE_MATHFN (BUILT_IN_NAN)
1687 CASE_MATHFN (BUILT_IN_NANS)
1688 CASE_MATHFN (BUILT_IN_NEARBYINT)
1689 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1690 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1691 CASE_MATHFN (BUILT_IN_POW)
1692 CASE_MATHFN (BUILT_IN_POWI)
1693 CASE_MATHFN (BUILT_IN_POW10)
1694 CASE_MATHFN (BUILT_IN_REMAINDER)
1695 CASE_MATHFN (BUILT_IN_REMQUO)
1696 CASE_MATHFN (BUILT_IN_RINT)
1697 CASE_MATHFN (BUILT_IN_ROUND)
1698 CASE_MATHFN (BUILT_IN_SCALB)
1699 CASE_MATHFN (BUILT_IN_SCALBLN)
1700 CASE_MATHFN (BUILT_IN_SCALBN)
1701 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1702 CASE_MATHFN (BUILT_IN_SIN)
1703 CASE_MATHFN (BUILT_IN_SINCOS)
1704 CASE_MATHFN (BUILT_IN_SINH)
1705 CASE_MATHFN (BUILT_IN_SQRT)
1706 CASE_MATHFN (BUILT_IN_TAN)
1707 CASE_MATHFN (BUILT_IN_TANH)
1708 CASE_MATHFN (BUILT_IN_TGAMMA)
1709 CASE_MATHFN (BUILT_IN_TRUNC)
1710 CASE_MATHFN (BUILT_IN_Y0)
1711 CASE_MATHFN (BUILT_IN_Y1)
1712 CASE_MATHFN (BUILT_IN_YN)
1718 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1719 return implicit_built_in_decls[fcode];
1720 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1721 return implicit_built_in_decls[fcodef];
1722 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1723 return implicit_built_in_decls[fcodel];
1728 /* If errno must be maintained, expand the RTL to check if the result,
1729 TARGET, of a built-in function call, EXP, is NaN, and if so set
1733 expand_errno_check (tree exp, rtx target)
1735 rtx lab = gen_label_rtx ();
1737 /* Test the result; if it is NaN, set errno=EDOM because
1738 the argument was not in the domain. */
1739 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1743 /* If this built-in doesn't throw an exception, set errno directly. */
1744 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1746 #ifdef GEN_ERRNO_RTX
1747 rtx errno_rtx = GEN_ERRNO_RTX;
1750 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1752 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1758 /* We can't set errno=EDOM directly; let the library call do it.
1759 Pop the arguments right away in case the call gets deleted. */
1761 expand_call (exp, target, 0);
1767 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1768 Return 0 if a normal call should be emitted rather than expanding the
1769 function in-line. EXP is the expression that is a call to the builtin
1770 function; if convenient, the result should be placed in TARGET.
1771 SUBTARGET may be used as the target for computing one of EXP's operands. */
1774 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1776 optab builtin_optab;
1777 rtx op0, insns, before_call;
1778 tree fndecl = get_callee_fndecl (exp);
1779 tree arglist = TREE_OPERAND (exp, 1);
1780 enum machine_mode mode;
1781 bool errno_set = false;
1784 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1787 arg = TREE_VALUE (arglist);
1789 switch (DECL_FUNCTION_CODE (fndecl))
1791 CASE_FLT_FN (BUILT_IN_SQRT):
1792 errno_set = ! tree_expr_nonnegative_p (arg);
1793 builtin_optab = sqrt_optab;
1795 CASE_FLT_FN (BUILT_IN_EXP):
1796 errno_set = true; builtin_optab = exp_optab; break;
1797 CASE_FLT_FN (BUILT_IN_EXP10):
1798 CASE_FLT_FN (BUILT_IN_POW10):
1799 errno_set = true; builtin_optab = exp10_optab; break;
1800 CASE_FLT_FN (BUILT_IN_EXP2):
1801 errno_set = true; builtin_optab = exp2_optab; break;
1802 CASE_FLT_FN (BUILT_IN_EXPM1):
1803 errno_set = true; builtin_optab = expm1_optab; break;
1804 CASE_FLT_FN (BUILT_IN_LOGB):
1805 errno_set = true; builtin_optab = logb_optab; break;
1806 CASE_FLT_FN (BUILT_IN_ILOGB):
1807 errno_set = true; builtin_optab = ilogb_optab; break;
1808 CASE_FLT_FN (BUILT_IN_LOG):
1809 errno_set = true; builtin_optab = log_optab; break;
1810 CASE_FLT_FN (BUILT_IN_LOG10):
1811 errno_set = true; builtin_optab = log10_optab; break;
1812 CASE_FLT_FN (BUILT_IN_LOG2):
1813 errno_set = true; builtin_optab = log2_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOG1P):
1815 errno_set = true; builtin_optab = log1p_optab; break;
1816 CASE_FLT_FN (BUILT_IN_ASIN):
1817 builtin_optab = asin_optab; break;
1818 CASE_FLT_FN (BUILT_IN_ACOS):
1819 builtin_optab = acos_optab; break;
1820 CASE_FLT_FN (BUILT_IN_TAN):
1821 builtin_optab = tan_optab; break;
1822 CASE_FLT_FN (BUILT_IN_ATAN):
1823 builtin_optab = atan_optab; break;
1824 CASE_FLT_FN (BUILT_IN_FLOOR):
1825 builtin_optab = floor_optab; break;
1826 CASE_FLT_FN (BUILT_IN_CEIL):
1827 builtin_optab = ceil_optab; break;
1828 CASE_FLT_FN (BUILT_IN_TRUNC):
1829 builtin_optab = btrunc_optab; break;
1830 CASE_FLT_FN (BUILT_IN_ROUND):
1831 builtin_optab = round_optab; break;
1832 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1833 builtin_optab = nearbyint_optab; break;
1834 CASE_FLT_FN (BUILT_IN_RINT):
1835 builtin_optab = rint_optab; break;
1836 CASE_FLT_FN (BUILT_IN_LRINT):
1837 CASE_FLT_FN (BUILT_IN_LLRINT):
1838 builtin_optab = lrint_optab; break;
1843 /* Make a suitable register to place result in. */
1844 mode = TYPE_MODE (TREE_TYPE (exp));
1846 if (! flag_errno_math || ! HONOR_NANS (mode))
1849 /* Before working hard, check whether the instruction is available. */
1850 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1852 target = gen_reg_rtx (mode);
1854 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1855 need to expand the argument again. This way, we will not perform
1856 side-effects more the once. */
1857 narg = builtin_save_expr (arg);
1861 arglist = build_tree_list (NULL_TREE, arg);
1862 exp = build_function_call_expr (fndecl, arglist);
1865 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1869 /* Compute into TARGET.
1870 Set TARGET to wherever the result comes back. */
1871 target = expand_unop (mode, builtin_optab, op0, target, 0);
1876 expand_errno_check (exp, target);
1878 /* Output the entire sequence. */
1879 insns = get_insns ();
1885 /* If we were unable to expand via the builtin, stop the sequence
1886 (without outputting the insns) and call to the library function
1887 with the stabilized argument list. */
1891 before_call = get_last_insn ();
1893 target = expand_call (exp, target, target == const0_rtx);
1895 /* If this is a sqrt operation and we don't care about errno, try to
1896 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1897 This allows the semantics of the libcall to be visible to the RTL
1899 if (builtin_optab == sqrt_optab && !errno_set)
1901 /* Search backwards through the insns emitted by expand_call looking
1902 for the instruction with the REG_RETVAL note. */
1903 rtx last = get_last_insn ();
1904 while (last != before_call)
1906 if (find_reg_note (last, REG_RETVAL, NULL))
1908 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1909 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1910 two elements, i.e. symbol_ref(sqrt) and the operand. */
1912 && GET_CODE (note) == EXPR_LIST
1913 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1914 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1915 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1917 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1918 /* Check operand is a register with expected mode. */
1921 && GET_MODE (operand) == mode)
1923 /* Replace the REG_EQUAL note with a SQRT rtx. */
1924 rtx equiv = gen_rtx_SQRT (mode, operand);
1925 set_unique_reg_note (last, REG_EQUAL, equiv);
1930 last = PREV_INSN (last);
1937 /* Expand a call to the builtin binary math functions (pow and atan2).
1938 Return 0 if a normal call should be emitted rather than expanding the
1939 function in-line. EXP is the expression that is a call to the builtin
1940 function; if convenient, the result should be placed in TARGET.
1941 SUBTARGET may be used as the target for computing one of EXP's
1945 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1947 optab builtin_optab;
1948 rtx op0, op1, insns;
1949 int op1_type = REAL_TYPE;
1950 tree fndecl = get_callee_fndecl (exp);
1951 tree arglist = TREE_OPERAND (exp, 1);
1952 tree arg0, arg1, temp, narg;
1953 enum machine_mode mode;
1954 bool errno_set = true;
1957 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1958 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1959 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1960 op1_type = INTEGER_TYPE;
1962 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1965 arg0 = TREE_VALUE (arglist);
1966 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1968 switch (DECL_FUNCTION_CODE (fndecl))
1970 CASE_FLT_FN (BUILT_IN_POW):
1971 builtin_optab = pow_optab; break;
1972 CASE_FLT_FN (BUILT_IN_ATAN2):
1973 builtin_optab = atan2_optab; break;
1974 CASE_FLT_FN (BUILT_IN_LDEXP):
1975 builtin_optab = ldexp_optab; break;
1976 CASE_FLT_FN (BUILT_IN_FMOD):
1977 builtin_optab = fmod_optab; break;
1978 CASE_FLT_FN (BUILT_IN_DREM):
1979 builtin_optab = drem_optab; break;
1984 /* Make a suitable register to place result in. */
1985 mode = TYPE_MODE (TREE_TYPE (exp));
1987 /* Before working hard, check whether the instruction is available. */
1988 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1991 target = gen_reg_rtx (mode);
1993 if (! flag_errno_math || ! HONOR_NANS (mode))
1996 /* Always stabilize the argument list. */
1997 narg = builtin_save_expr (arg1);
2001 temp = build_tree_list (NULL_TREE, narg);
2005 temp = TREE_CHAIN (arglist);
2007 narg = builtin_save_expr (arg0);
2011 arglist = tree_cons (NULL_TREE, narg, temp);
2015 arglist = tree_cons (NULL_TREE, arg0, temp);
2018 exp = build_function_call_expr (fndecl, arglist);
2020 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2021 op1 = expand_normal (arg1);
2025 /* Compute into TARGET.
2026 Set TARGET to wherever the result comes back. */
2027 target = expand_binop (mode, builtin_optab, op0, op1,
2028 target, 0, OPTAB_DIRECT);
2030 /* If we were unable to expand via the builtin, stop the sequence
2031 (without outputting the insns) and call to the library function
2032 with the stabilized argument list. */
2036 return expand_call (exp, target, target == const0_rtx);
2040 expand_errno_check (exp, target);
2042 /* Output the entire sequence. */
2043 insns = get_insns ();
2050 /* Expand a call to the builtin sin and cos math functions.
2051 Return 0 if a normal call should be emitted rather than expanding the
2052 function in-line. EXP is the expression that is a call to the builtin
2053 function; if convenient, the result should be placed in TARGET.
2054 SUBTARGET may be used as the target for computing one of EXP's
2058 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2060 optab builtin_optab;
2062 tree fndecl = get_callee_fndecl (exp);
2063 tree arglist = TREE_OPERAND (exp, 1);
2064 enum machine_mode mode;
2067 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2070 arg = TREE_VALUE (arglist);
2072 switch (DECL_FUNCTION_CODE (fndecl))
2074 CASE_FLT_FN (BUILT_IN_SIN):
2075 CASE_FLT_FN (BUILT_IN_COS):
2076 builtin_optab = sincos_optab; break;
2081 /* Make a suitable register to place result in. */
2082 mode = TYPE_MODE (TREE_TYPE (exp));
2084 /* Check if sincos insn is available, otherwise fallback
2085 to sin or cos insn. */
2086 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2087 switch (DECL_FUNCTION_CODE (fndecl))
2089 CASE_FLT_FN (BUILT_IN_SIN):
2090 builtin_optab = sin_optab; break;
2091 CASE_FLT_FN (BUILT_IN_COS):
2092 builtin_optab = cos_optab; break;
2098 /* Before working hard, check whether the instruction is available. */
2099 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2101 target = gen_reg_rtx (mode);
2103 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2104 need to expand the argument again. This way, we will not perform
2105 side-effects more the once. */
2106 narg = save_expr (arg);
2110 arglist = build_tree_list (NULL_TREE, arg);
2111 exp = build_function_call_expr (fndecl, arglist);
2114 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2118 /* Compute into TARGET.
2119 Set TARGET to wherever the result comes back. */
2120 if (builtin_optab == sincos_optab)
2124 switch (DECL_FUNCTION_CODE (fndecl))
2126 CASE_FLT_FN (BUILT_IN_SIN):
2127 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2129 CASE_FLT_FN (BUILT_IN_COS):
2130 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2135 gcc_assert (result);
2139 target = expand_unop (mode, builtin_optab, op0, target, 0);
2144 /* Output the entire sequence. */
2145 insns = get_insns ();
2151 /* If we were unable to expand via the builtin, stop the sequence
2152 (without outputting the insns) and call to the library function
2153 with the stabilized argument list. */
2157 target = expand_call (exp, target, target == const0_rtx);
2162 /* Expand a call to the builtin sincos math function.
2163 Return 0 if a normal call should be emitted rather than expanding the
2164 function in-line. EXP is the expression that is a call to the builtin
2168 expand_builtin_sincos (tree exp)
2170 rtx op0, op1, op2, target1, target2;
2171 tree arglist = TREE_OPERAND (exp, 1);
2172 enum machine_mode mode;
2173 tree arg, sinp, cosp;
2176 if (!validate_arglist (arglist, REAL_TYPE,
2177 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2180 arg = TREE_VALUE (arglist);
2181 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2182 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2184 /* Make a suitable register to place result in. */
2185 mode = TYPE_MODE (TREE_TYPE (arg));
2187 /* Check if sincos insn is available, otherwise emit the call. */
2188 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2191 target1 = gen_reg_rtx (mode);
2192 target2 = gen_reg_rtx (mode);
2194 op0 = expand_normal (arg);
2195 op1 = expand_normal (build_fold_indirect_ref (sinp));
2196 op2 = expand_normal (build_fold_indirect_ref (cosp));
2198 /* Compute into target1 and target2.
2199 Set TARGET to wherever the result comes back. */
2200 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2201 gcc_assert (result);
2203 /* Move target1 and target2 to the memory locations indicated
2205 emit_move_insn (op1, target1);
2206 emit_move_insn (op2, target2);
2211 /* Expand a call to one of the builtin rounding functions (lfloor).
2212 If expanding via optab fails, lower expression to (int)(floor(x)).
2213 EXP is the expression that is a call to the builtin function;
2214 if convenient, the result should be placed in TARGET. SUBTARGET may
2215 be used as the target for computing one of EXP's operands. */
2218 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2220 optab builtin_optab;
2221 rtx op0, insns, tmp;
2222 tree fndecl = get_callee_fndecl (exp);
2223 tree arglist = TREE_OPERAND (exp, 1);
2224 enum built_in_function fallback_fn;
2225 tree fallback_fndecl;
2226 enum machine_mode mode;
2229 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2232 arg = TREE_VALUE (arglist);
2234 switch (DECL_FUNCTION_CODE (fndecl))
2236 CASE_FLT_FN (BUILT_IN_LCEIL):
2237 CASE_FLT_FN (BUILT_IN_LLCEIL):
2238 builtin_optab = lceil_optab;
2239 fallback_fn = BUILT_IN_CEIL;
2242 CASE_FLT_FN (BUILT_IN_LFLOOR):
2243 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2244 builtin_optab = lfloor_optab;
2245 fallback_fn = BUILT_IN_FLOOR;
2252 /* Make a suitable register to place result in. */
2253 mode = TYPE_MODE (TREE_TYPE (exp));
2255 /* Before working hard, check whether the instruction is available. */
2256 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2258 target = gen_reg_rtx (mode);
2260 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2261 need to expand the argument again. This way, we will not perform
2262 side-effects more the once. */
2263 narg = builtin_save_expr (arg);
2267 arglist = build_tree_list (NULL_TREE, arg);
2268 exp = build_function_call_expr (fndecl, arglist);
2271 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2275 /* Compute into TARGET.
2276 Set TARGET to wherever the result comes back. */
2277 target = expand_unop (mode, builtin_optab, op0, target, 0);
2281 /* Output the entire sequence. */
2282 insns = get_insns ();
2288 /* If we were unable to expand via the builtin, stop the sequence
2289 (without outputting the insns). */
2293 /* Fall back to floating point rounding optab. */
2294 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2295 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2296 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2297 gcc_assert (fallback_fndecl != NULL_TREE);
2298 exp = build_function_call_expr (fallback_fndecl, arglist);
2300 tmp = expand_normal (exp);
2302 /* Truncate the result of floating point optab to integer
2303 via expand_fix (). */
2304 target = gen_reg_rtx (mode);
2305 expand_fix (target, tmp, 0);
2310 /* To evaluate powi(x,n), the floating point value x raised to the
2311 constant integer exponent n, we use a hybrid algorithm that
2312 combines the "window method" with look-up tables. For an
2313 introduction to exponentiation algorithms and "addition chains",
2314 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2315 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2316 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2317 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2319 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2320 multiplications to inline before calling the system library's pow
2321 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2322 so this default never requires calling pow, powf or powl. */
2324 #ifndef POWI_MAX_MULTS
2325 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2328 /* The size of the "optimal power tree" lookup table. All
2329 exponents less than this value are simply looked up in the
2330 powi_table below. This threshold is also used to size the
2331 cache of pseudo registers that hold intermediate results. */
2332 #define POWI_TABLE_SIZE 256
2334 /* The size, in bits of the window, used in the "window method"
2335 exponentiation algorithm. This is equivalent to a radix of
2336 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2337 #define POWI_WINDOW_SIZE 3
2339 /* The following table is an efficient representation of an
2340 "optimal power tree". For each value, i, the corresponding
2341 value, j, in the table states than an optimal evaluation
2342 sequence for calculating pow(x,i) can be found by evaluating
2343 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2344 100 integers is given in Knuth's "Seminumerical algorithms". */
2346 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2348 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2349 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2350 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2351 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2352 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2353 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2354 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2355 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2356 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2357 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2358 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2359 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2360 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2361 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2362 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2363 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2364 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2365 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2366 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2367 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2368 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2369 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2370 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2371 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2372 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2373 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2374 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2375 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2376 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2377 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2378 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2379 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2383 /* Return the number of multiplications required to calculate
2384 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2385 subroutine of powi_cost. CACHE is an array indicating
2386 which exponents have already been calculated. */
2389 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2391 /* If we've already calculated this exponent, then this evaluation
2392 doesn't require any additional multiplications. */
2397 return powi_lookup_cost (n - powi_table[n], cache)
2398 + powi_lookup_cost (powi_table[n], cache) + 1;
2401 /* Return the number of multiplications required to calculate
2402 powi(x,n) for an arbitrary x, given the exponent N. This
2403 function needs to be kept in sync with expand_powi below. */
2406 powi_cost (HOST_WIDE_INT n)
2408 bool cache[POWI_TABLE_SIZE];
2409 unsigned HOST_WIDE_INT digit;
2410 unsigned HOST_WIDE_INT val;
2416 /* Ignore the reciprocal when calculating the cost. */
2417 val = (n < 0) ? -n : n;
2419 /* Initialize the exponent cache. */
2420 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2425 while (val >= POWI_TABLE_SIZE)
2429 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2430 result += powi_lookup_cost (digit, cache)
2431 + POWI_WINDOW_SIZE + 1;
2432 val >>= POWI_WINDOW_SIZE;
2441 return result + powi_lookup_cost (val, cache);
2444 /* Recursive subroutine of expand_powi. This function takes the array,
2445 CACHE, of already calculated exponents and an exponent N and returns
2446 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2449 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2451 unsigned HOST_WIDE_INT digit;
2455 if (n < POWI_TABLE_SIZE)
2460 target = gen_reg_rtx (mode);
2463 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2464 op1 = expand_powi_1 (mode, powi_table[n], cache);
2468 target = gen_reg_rtx (mode);
2469 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2470 op0 = expand_powi_1 (mode, n - digit, cache);
2471 op1 = expand_powi_1 (mode, digit, cache);
2475 target = gen_reg_rtx (mode);
2476 op0 = expand_powi_1 (mode, n >> 1, cache);
2480 result = expand_mult (mode, op0, op1, target, 0);
2481 if (result != target)
2482 emit_move_insn (target, result);
2486 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2487 floating point operand in mode MODE, and N is the exponent. This
2488 function needs to be kept in sync with powi_cost above. */
2491 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2493 unsigned HOST_WIDE_INT val;
2494 rtx cache[POWI_TABLE_SIZE];
2498 return CONST1_RTX (mode);
2500 val = (n < 0) ? -n : n;
2502 memset (cache, 0, sizeof (cache));
2505 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2507 /* If the original exponent was negative, reciprocate the result. */
2509 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2510 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2515 /* Expand a call to the pow built-in mathematical function. Return 0 if
2516 a normal call should be emitted rather than expanding the function
2517 in-line. EXP is the expression that is a call to the builtin
2518 function; if convenient, the result should be placed in TARGET. */
2521 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2523 tree arglist = TREE_OPERAND (exp, 1);
2526 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2529 arg0 = TREE_VALUE (arglist);
2530 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2532 if (TREE_CODE (arg1) == REAL_CST
2533 && ! TREE_CONSTANT_OVERFLOW (arg1))
2535 REAL_VALUE_TYPE cint;
2539 c = TREE_REAL_CST (arg1);
2540 n = real_to_integer (&c);
2541 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2542 if (real_identical (&c, &cint))
2544 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2545 Otherwise, check the number of multiplications required.
2546 Note that pow never sets errno for an integer exponent. */
2547 if ((n >= -1 && n <= 2)
2548 || (flag_unsafe_math_optimizations
2550 && powi_cost (n) <= POWI_MAX_MULTS))
2552 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2553 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2554 op = force_reg (mode, op);
2555 return expand_powi (op, mode, n);
2560 if (! flag_unsafe_math_optimizations)
2562 return expand_builtin_mathfn_2 (exp, target, subtarget);
2565 /* Expand a call to the powi built-in mathematical function. Return 0 if
2566 a normal call should be emitted rather than expanding the function
2567 in-line. EXP is the expression that is a call to the builtin
2568 function; if convenient, the result should be placed in TARGET. */
2571 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2573 tree arglist = TREE_OPERAND (exp, 1);
2576 enum machine_mode mode;
2577 enum machine_mode mode2;
2579 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2582 arg0 = TREE_VALUE (arglist);
2583 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2584 mode = TYPE_MODE (TREE_TYPE (exp));
2586 /* Handle constant power. */
2588 if (TREE_CODE (arg1) == INTEGER_CST
2589 && ! TREE_CONSTANT_OVERFLOW (arg1))
2591 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2593 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2594 Otherwise, check the number of multiplications required. */
2595 if ((TREE_INT_CST_HIGH (arg1) == 0
2596 || TREE_INT_CST_HIGH (arg1) == -1)
2597 && ((n >= -1 && n <= 2)
2599 && powi_cost (n) <= POWI_MAX_MULTS)))
2601 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2602 op0 = force_reg (mode, op0);
2603 return expand_powi (op0, mode, n);
2607 /* Emit a libcall to libgcc. */
2609 /* Mode of the 2nd argument must match that of an int. */
2610 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2612 if (target == NULL_RTX)
2613 target = gen_reg_rtx (mode);
2615 op0 = expand_expr (arg0, subtarget, mode, 0);
2616 if (GET_MODE (op0) != mode)
2617 op0 = convert_to_mode (mode, op0, 0);
2618 op1 = expand_expr (arg1, 0, mode2, 0);
2619 if (GET_MODE (op1) != mode2)
2620 op1 = convert_to_mode (mode2, op1, 0);
2622 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2623 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2624 op0, mode, op1, mode2);
2629 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2630 if we failed the caller should emit a normal call, otherwise
2631 try to get the result in TARGET, if convenient. */
2634 expand_builtin_strlen (tree arglist, rtx target,
2635 enum machine_mode target_mode)
2637 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2642 tree len, src = TREE_VALUE (arglist);
2643 rtx result, src_reg, char_rtx, before_strlen;
2644 enum machine_mode insn_mode = target_mode, char_mode;
2645 enum insn_code icode = CODE_FOR_nothing;
2648 /* If the length can be computed at compile-time, return it. */
2649 len = c_strlen (src, 0);
2651 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2653 /* If the length can be computed at compile-time and is constant
2654 integer, but there are side-effects in src, evaluate
2655 src for side-effects, then return len.
2656 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2657 can be optimized into: i++; x = 3; */
2658 len = c_strlen (src, 1);
2659 if (len && TREE_CODE (len) == INTEGER_CST)
2661 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2662 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2665 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2667 /* If SRC is not a pointer type, don't do this operation inline. */
2671 /* Bail out if we can't compute strlen in the right mode. */
2672 while (insn_mode != VOIDmode)
2674 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2675 if (icode != CODE_FOR_nothing)
2678 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2680 if (insn_mode == VOIDmode)
2683 /* Make a place to write the result of the instruction. */
2687 && GET_MODE (result) == insn_mode
2688 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2689 result = gen_reg_rtx (insn_mode);
2691 /* Make a place to hold the source address. We will not expand
2692 the actual source until we are sure that the expansion will
2693 not fail -- there are trees that cannot be expanded twice. */
2694 src_reg = gen_reg_rtx (Pmode);
2696 /* Mark the beginning of the strlen sequence so we can emit the
2697 source operand later. */
2698 before_strlen = get_last_insn ();
2700 char_rtx = const0_rtx;
2701 char_mode = insn_data[(int) icode].operand[2].mode;
2702 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2704 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2706 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2707 char_rtx, GEN_INT (align));
2712 /* Now that we are assured of success, expand the source. */
2714 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2716 emit_move_insn (src_reg, pat);
2721 emit_insn_after (pat, before_strlen);
2723 emit_insn_before (pat, get_insns ());
2725 /* Return the value in the proper mode for this function. */
2726 if (GET_MODE (result) == target_mode)
2728 else if (target != 0)
2729 convert_move (target, result, 0);
2731 target = convert_to_mode (target_mode, result, 0);
2737 /* Expand a call to the strstr builtin. Return 0 if we failed the
2738 caller should emit a normal call, otherwise try to get the result
2739 in TARGET, if convenient (and in mode MODE if that's convenient). */
2742 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2744 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2746 tree result = fold_builtin_strstr (arglist, type);
2748 return expand_expr (result, target, mode, EXPAND_NORMAL);
2753 /* Expand a call to the strchr builtin. Return 0 if we failed the
2754 caller should emit a normal call, otherwise try to get the result
2755 in TARGET, if convenient (and in mode MODE if that's convenient). */
2758 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2760 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2762 tree result = fold_builtin_strchr (arglist, type);
2764 return expand_expr (result, target, mode, EXPAND_NORMAL);
2766 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2771 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2772 caller should emit a normal call, otherwise try to get the result
2773 in TARGET, if convenient (and in mode MODE if that's convenient). */
2776 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2778 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2780 tree result = fold_builtin_strrchr (arglist, type);
2782 return expand_expr (result, target, mode, EXPAND_NORMAL);
2787 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2788 caller should emit a normal call, otherwise try to get the result
2789 in TARGET, if convenient (and in mode MODE if that's convenient). */
2792 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2794 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2796 tree result = fold_builtin_strpbrk (arglist, type);
2798 return expand_expr (result, target, mode, EXPAND_NORMAL);
2803 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2804 bytes from constant string DATA + OFFSET and return it as target
2808 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2809 enum machine_mode mode)
2811 const char *str = (const char *) data;
2813 gcc_assert (offset >= 0
2814 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2815 <= strlen (str) + 1));
2817 return c_readstr (str + offset, mode);
2820 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2821 Return 0 if we failed, the caller should emit a normal call,
2822 otherwise try to get the result in TARGET, if convenient (and in
2823 mode MODE if that's convenient). */
2825 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2827 tree fndecl = get_callee_fndecl (exp);
2828 tree arglist = TREE_OPERAND (exp, 1);
2829 if (!validate_arglist (arglist,
2830 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2834 tree dest = TREE_VALUE (arglist);
2835 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2836 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2837 const char *src_str;
2838 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2839 unsigned int dest_align
2840 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2841 rtx dest_mem, src_mem, dest_addr, len_rtx;
2842 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
2847 while (TREE_CODE (result) == COMPOUND_EXPR)
2849 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2851 result = TREE_OPERAND (result, 1);
2853 return expand_expr (result, target, mode, EXPAND_NORMAL);
2856 /* If DEST is not a pointer type, call the normal function. */
2857 if (dest_align == 0)
2860 /* If either SRC is not a pointer type, don't do this
2861 operation in-line. */
2865 dest_mem = get_memory_rtx (dest, len);
2866 set_mem_align (dest_mem, dest_align);
2867 len_rtx = expand_normal (len);
2868 src_str = c_getstr (src);
2870 /* If SRC is a string constant and block move would be done
2871 by pieces, we can avoid loading the string from memory
2872 and only stored the computed constants. */
2874 && GET_CODE (len_rtx) == CONST_INT
2875 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2876 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2877 (void *) src_str, dest_align))
2879 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2880 builtin_memcpy_read_str,
2881 (void *) src_str, dest_align, 0);
2882 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2883 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2887 src_mem = get_memory_rtx (src, len);
2888 set_mem_align (src_mem, src_align);
2890 /* Copy word part most expediently. */
2891 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2892 CALL_EXPR_TAILCALL (exp)
2893 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2897 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2898 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2904 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2905 Return 0 if we failed; the caller should emit a normal call,
2906 otherwise try to get the result in TARGET, if convenient (and in
2907 mode MODE if that's convenient). If ENDP is 0 return the
2908 destination pointer, if ENDP is 1 return the end pointer ala
2909 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2913 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2916 if (!validate_arglist (arglist,
2917 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2919 /* If return value is ignored, transform mempcpy into memcpy. */
2920 else if (target == const0_rtx)
2922 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2927 return expand_expr (build_function_call_expr (fn, arglist),
2928 target, mode, EXPAND_NORMAL);
2932 tree dest = TREE_VALUE (arglist);
2933 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2934 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2935 const char *src_str;
2936 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2937 unsigned int dest_align
2938 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2939 rtx dest_mem, src_mem, len_rtx;
2940 tree result = fold_builtin_memory_op (arglist, type, false, endp);
2944 while (TREE_CODE (result) == COMPOUND_EXPR)
2946 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2948 result = TREE_OPERAND (result, 1);
2950 return expand_expr (result, target, mode, EXPAND_NORMAL);
2953 /* If either SRC or DEST is not a pointer type, don't do this
2954 operation in-line. */
2955 if (dest_align == 0 || src_align == 0)
2958 /* If LEN is not constant, call the normal function. */
2959 if (! host_integerp (len, 1))
2962 len_rtx = expand_normal (len);
2963 src_str = c_getstr (src);
2965 /* If SRC is a string constant and block move would be done
2966 by pieces, we can avoid loading the string from memory
2967 and only stored the computed constants. */
2969 && GET_CODE (len_rtx) == CONST_INT
2970 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2971 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2972 (void *) src_str, dest_align))
2974 dest_mem = get_memory_rtx (dest, len);
2975 set_mem_align (dest_mem, dest_align);
2976 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2977 builtin_memcpy_read_str,
2978 (void *) src_str, dest_align, endp);
2979 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2980 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2984 if (GET_CODE (len_rtx) == CONST_INT
2985 && can_move_by_pieces (INTVAL (len_rtx),
2986 MIN (dest_align, src_align)))
2988 dest_mem = get_memory_rtx (dest, len);
2989 set_mem_align (dest_mem, dest_align);
2990 src_mem = get_memory_rtx (src, len);
2991 set_mem_align (src_mem, src_align);
2992 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2993 MIN (dest_align, src_align), endp);
2994 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2995 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3003 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3004 if we failed; the caller should emit a normal call. */
3007 expand_builtin_memmove (tree arglist, tree type, rtx target,
3008 enum machine_mode mode, tree orig_exp)
3010 if (!validate_arglist (arglist,
3011 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3015 tree dest = TREE_VALUE (arglist);
3016 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3017 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3019 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3020 unsigned int dest_align
3021 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3022 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3026 while (TREE_CODE (result) == COMPOUND_EXPR)
3028 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3030 result = TREE_OPERAND (result, 1);
3032 return expand_expr (result, target, mode, EXPAND_NORMAL);
3035 /* If DEST is not a pointer type, call the normal function. */
3036 if (dest_align == 0)
3039 /* If either SRC is not a pointer type, don't do this
3040 operation in-line. */
3044 /* If src is categorized for a readonly section we can use
3046 if (readonly_data_expr (src))
3048 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3051 fn = build_function_call_expr (fn, arglist);
3052 if (TREE_CODE (fn) == CALL_EXPR)
3053 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3054 return expand_expr (fn, target, mode, EXPAND_NORMAL);
3057 /* If length is 1 and we can expand memcpy call inline,
3058 it is ok to use memcpy as well. */
3059 if (integer_onep (len))
3061 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
3067 /* Otherwise, call the normal function. */
3072 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3073 if we failed the caller should emit a normal call. */
3076 expand_builtin_bcopy (tree exp)
3078 tree arglist = TREE_OPERAND (exp, 1);
3079 tree type = TREE_TYPE (exp);
3080 tree src, dest, size, newarglist;
3082 if (!validate_arglist (arglist,
3083 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3086 src = TREE_VALUE (arglist);
3087 dest = TREE_VALUE (TREE_CHAIN (arglist));
3088 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3090 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3091 memmove(ptr y, ptr x, size_t z). This is done this way
3092 so that if it isn't expanded inline, we fallback to
3093 calling bcopy instead of memmove. */
3095 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3096 newarglist = tree_cons (NULL_TREE, src, newarglist);
3097 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3099 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode, exp);
3103 # define HAVE_movstr 0
3104 # define CODE_FOR_movstr CODE_FOR_nothing
3107 /* Expand into a movstr instruction, if one is available. Return 0 if
3108 we failed, the caller should emit a normal call, otherwise try to
3109 get the result in TARGET, if convenient. If ENDP is 0 return the
3110 destination pointer, if ENDP is 1 return the end pointer ala
3111 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3115 expand_movstr (tree dest, tree src, rtx target, int endp)
3121 const struct insn_data * data;
3126 dest_mem = get_memory_rtx (dest, NULL);
3127 src_mem = get_memory_rtx (src, NULL);
3130 target = force_reg (Pmode, XEXP (dest_mem, 0));
3131 dest_mem = replace_equiv_address (dest_mem, target);
3132 end = gen_reg_rtx (Pmode);
3136 if (target == 0 || target == const0_rtx)
3138 end = gen_reg_rtx (Pmode);
3146 data = insn_data + CODE_FOR_movstr;
3148 if (data->operand[0].mode != VOIDmode)
3149 end = gen_lowpart (data->operand[0].mode, end);
3151 insn = data->genfun (end, dest_mem, src_mem);
3157 /* movstr is supposed to set end to the address of the NUL
3158 terminator. If the caller requested a mempcpy-like return value,
3160 if (endp == 1 && target != const0_rtx)
3162 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3163 emit_move_insn (target, force_operand (tem, NULL_RTX));
3169 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3170 if we failed the caller should emit a normal call, otherwise try to get
3171 the result in TARGET, if convenient (and in mode MODE if that's
3175 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3177 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3179 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3182 while (TREE_CODE (result) == COMPOUND_EXPR)
3184 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3186 result = TREE_OPERAND (result, 1);
3188 return expand_expr (result, target, mode, EXPAND_NORMAL);
3191 return expand_movstr (TREE_VALUE (arglist),
3192 TREE_VALUE (TREE_CHAIN (arglist)),
3193 target, /*endp=*/0);
3198 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3199 Return 0 if we failed the caller should emit a normal call,
3200 otherwise try to get the result in TARGET, if convenient (and in
3201 mode MODE if that's convenient). */
3204 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3206 tree arglist = TREE_OPERAND (exp, 1);
3207 /* If return value is ignored, transform stpcpy into strcpy. */
3208 if (target == const0_rtx)
3210 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3214 return expand_expr (build_function_call_expr (fn, arglist),
3215 target, mode, EXPAND_NORMAL);
3218 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3222 tree dst, src, len, lenp1;
3226 /* Ensure we get an actual string whose length can be evaluated at
3227 compile-time, not an expression containing a string. This is
3228 because the latter will potentially produce pessimized code
3229 when used to produce the return value. */
3230 src = TREE_VALUE (TREE_CHAIN (arglist));
3231 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3232 return expand_movstr (TREE_VALUE (arglist),
3233 TREE_VALUE (TREE_CHAIN (arglist)),
3234 target, /*endp=*/2);
3236 dst = TREE_VALUE (arglist);
3237 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3238 narglist = build_tree_list (NULL_TREE, lenp1);
3239 narglist = tree_cons (NULL_TREE, src, narglist);
3240 narglist = tree_cons (NULL_TREE, dst, narglist);
3241 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3242 target, mode, /*endp=*/2);
3247 if (TREE_CODE (len) == INTEGER_CST)
3249 rtx len_rtx = expand_normal (len);
3251 if (GET_CODE (len_rtx) == CONST_INT)
3253 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3254 arglist, target, mode);
3260 if (mode != VOIDmode)
3261 target = gen_reg_rtx (mode);
3263 target = gen_reg_rtx (GET_MODE (ret));
3265 if (GET_MODE (target) != GET_MODE (ret))
3266 ret = gen_lowpart (GET_MODE (target), ret);
3268 ret = plus_constant (ret, INTVAL (len_rtx));
3269 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3277 return expand_movstr (TREE_VALUE (arglist),
3278 TREE_VALUE (TREE_CHAIN (arglist)),
3279 target, /*endp=*/2);
3283 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3284 bytes from constant string DATA + OFFSET and return it as target
3288 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3289 enum machine_mode mode)
3291 const char *str = (const char *) data;
3293 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3296 return c_readstr (str + offset, mode);
3299 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3300 if we failed the caller should emit a normal call. */
3303 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3305 tree fndecl = get_callee_fndecl (exp);
3306 tree arglist = TREE_OPERAND (exp, 1);
3307 if (validate_arglist (arglist,
3308 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3310 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3311 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3312 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3316 while (TREE_CODE (result) == COMPOUND_EXPR)
3318 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3320 result = TREE_OPERAND (result, 1);
3322 return expand_expr (result, target, mode, EXPAND_NORMAL);
3325 /* We must be passed a constant len and src parameter. */
3326 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3329 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3331 /* We're required to pad with trailing zeros if the requested
3332 len is greater than strlen(s2)+1. In that case try to
3333 use store_by_pieces, if it fails, punt. */
3334 if (tree_int_cst_lt (slen, len))
3336 tree dest = TREE_VALUE (arglist);
3337 unsigned int dest_align
3338 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3339 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3342 if (!p || dest_align == 0 || !host_integerp (len, 1)
3343 || !can_store_by_pieces (tree_low_cst (len, 1),
3344 builtin_strncpy_read_str,
3345 (void *) p, dest_align))
3348 dest_mem = get_memory_rtx (dest, len);
3349 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3350 builtin_strncpy_read_str,
3351 (void *) p, dest_align, 0);
3352 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3353 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3360 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3361 bytes from constant string DATA + OFFSET and return it as target
3365 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3366 enum machine_mode mode)
3368 const char *c = (const char *) data;
3369 char *p = alloca (GET_MODE_SIZE (mode));
3371 memset (p, *c, GET_MODE_SIZE (mode));
3373 return c_readstr (p, mode);
3376 /* Callback routine for store_by_pieces. Return the RTL of a register
3377 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3378 char value given in the RTL register data. For example, if mode is
3379 4 bytes wide, return the RTL for 0x01010101*data. */
3382 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3383 enum machine_mode mode)
3389 size = GET_MODE_SIZE (mode);
3394 memset (p, 1, size);
3395 coeff = c_readstr (p, mode);
3397 target = convert_to_mode (mode, (rtx) data, 1);
3398 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3399 return force_reg (mode, target);
3402 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3403 if we failed the caller should emit a normal call, otherwise try to get
3404 the result in TARGET, if convenient (and in mode MODE if that's
3408 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3411 if (!validate_arglist (arglist,
3412 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3416 tree dest = TREE_VALUE (arglist);
3417 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3418 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3420 enum built_in_function fcode;
3422 unsigned int dest_align;
3423 rtx dest_mem, dest_addr, len_rtx;
3425 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3427 /* If DEST is not a pointer type, don't do this
3428 operation in-line. */
3429 if (dest_align == 0)
3432 /* If the LEN parameter is zero, return DEST. */
3433 if (integer_zerop (len))
3435 /* Evaluate and ignore VAL in case it has side-effects. */
3436 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3437 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3440 /* Stabilize the arguments in case we fail. */
3441 dest = builtin_save_expr (dest);
3442 val = builtin_save_expr (val);
3443 len = builtin_save_expr (len);
3445 len_rtx = expand_normal (len);
3446 dest_mem = get_memory_rtx (dest, len);
3448 if (TREE_CODE (val) != INTEGER_CST)
3452 val_rtx = expand_normal (val);
3453 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3456 /* Assume that we can memset by pieces if we can store the
3457 * the coefficients by pieces (in the required modes).
3458 * We can't pass builtin_memset_gen_str as that emits RTL. */
3460 if (host_integerp (len, 1)
3461 && !(optimize_size && tree_low_cst (len, 1) > 1)
3462 && can_store_by_pieces (tree_low_cst (len, 1),
3463 builtin_memset_read_str, &c, dest_align))
3465 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3467 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3468 builtin_memset_gen_str, val_rtx, dest_align, 0);
3470 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3474 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3475 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3479 if (target_char_cast (val, &c))
3484 if (host_integerp (len, 1)
3485 && !(optimize_size && tree_low_cst (len, 1) > 1)
3486 && can_store_by_pieces (tree_low_cst (len, 1),
3487 builtin_memset_read_str, &c, dest_align))
3488 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3489 builtin_memset_read_str, &c, dest_align, 0);
3490 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3494 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3495 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3499 set_mem_align (dest_mem, dest_align);
3500 dest_addr = clear_storage (dest_mem, len_rtx,
3501 CALL_EXPR_TAILCALL (orig_exp)
3502 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3506 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3507 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3513 fndecl = get_callee_fndecl (orig_exp);
3514 fcode = DECL_FUNCTION_CODE (fndecl);
3515 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3516 arglist = build_tree_list (NULL_TREE, len);
3517 if (fcode == BUILT_IN_MEMSET)
3518 arglist = tree_cons (NULL_TREE, val, arglist);
3519 arglist = tree_cons (NULL_TREE, dest, arglist);
3520 fn = build_function_call_expr (fndecl, arglist);
3521 if (TREE_CODE (fn) == CALL_EXPR)
3522 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3523 return expand_call (fn, target, target == const0_rtx);
3527 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3528 if we failed the caller should emit a normal call. */
3531 expand_builtin_bzero (tree exp)
3533 tree arglist = TREE_OPERAND (exp, 1);
3534 tree dest, size, newarglist;
3536 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3539 dest = TREE_VALUE (arglist);
3540 size = TREE_VALUE (TREE_CHAIN (arglist));
3542 /* New argument list transforming bzero(ptr x, int y) to
3543 memset(ptr x, int 0, size_t y). This is done this way
3544 so that if it isn't expanded inline, we fallback to
3545 calling bzero instead of memset. */
3547 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3548 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3549 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3551 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3554 /* Expand expression EXP, which is a call to the memcmp built-in function.
3555 ARGLIST is the argument list for this call. Return 0 if we failed and the
3556 caller should emit a normal call, otherwise try to get the result in
3557 TARGET, if convenient (and in mode MODE, if that's convenient). */
3560 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3561 enum machine_mode mode)
3563 if (!validate_arglist (arglist,
3564 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3568 tree result = fold_builtin_memcmp (arglist);
3570 return expand_expr (result, target, mode, EXPAND_NORMAL);
3573 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3575 tree arg1 = TREE_VALUE (arglist);
3576 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3577 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3578 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3583 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3585 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3586 enum machine_mode insn_mode;
3588 #ifdef HAVE_cmpmemsi
3590 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3593 #ifdef HAVE_cmpstrnsi
3595 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3600 /* If we don't have POINTER_TYPE, call the function. */
3601 if (arg1_align == 0 || arg2_align == 0)
3604 /* Make a place to write the result of the instruction. */
3607 && REG_P (result) && GET_MODE (result) == insn_mode
3608 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3609 result = gen_reg_rtx (insn_mode);
3611 arg1_rtx = get_memory_rtx (arg1, len);
3612 arg2_rtx = get_memory_rtx (arg2, len);
3613 arg3_rtx = expand_normal (len);
3615 /* Set MEM_SIZE as appropriate. */
3616 if (GET_CODE (arg3_rtx) == CONST_INT)
3618 set_mem_size (arg1_rtx, arg3_rtx);
3619 set_mem_size (arg2_rtx, arg3_rtx);
3622 #ifdef HAVE_cmpmemsi
3624 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3625 GEN_INT (MIN (arg1_align, arg2_align)));
3628 #ifdef HAVE_cmpstrnsi
3630 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3631 GEN_INT (MIN (arg1_align, arg2_align)));
3639 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3640 TYPE_MODE (integer_type_node), 3,
3641 XEXP (arg1_rtx, 0), Pmode,
3642 XEXP (arg2_rtx, 0), Pmode,
3643 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3644 TYPE_UNSIGNED (sizetype)),
3645 TYPE_MODE (sizetype));
3647 /* Return the value in the proper mode for this function. */
3648 mode = TYPE_MODE (TREE_TYPE (exp));
3649 if (GET_MODE (result) == mode)
3651 else if (target != 0)
3653 convert_move (target, result, 0);
3657 return convert_to_mode (mode, result, 0);
3664 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3665 if we failed the caller should emit a normal call, otherwise try to get
3666 the result in TARGET, if convenient. */
3669 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3671 tree arglist = TREE_OPERAND (exp, 1);
3673 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3677 tree result = fold_builtin_strcmp (arglist);
3679 return expand_expr (result, target, mode, EXPAND_NORMAL);
3682 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3683 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3684 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3686 rtx arg1_rtx, arg2_rtx;
3687 rtx result, insn = NULL_RTX;
3690 tree arg1 = TREE_VALUE (arglist);
3691 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3693 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3695 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3697 /* If we don't have POINTER_TYPE, call the function. */
3698 if (arg1_align == 0 || arg2_align == 0)
3701 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3702 arg1 = builtin_save_expr (arg1);
3703 arg2 = builtin_save_expr (arg2);
3705 arg1_rtx = get_memory_rtx (arg1, NULL);
3706 arg2_rtx = get_memory_rtx (arg2, NULL);
3708 #ifdef HAVE_cmpstrsi
3709 /* Try to call cmpstrsi. */
3712 enum machine_mode insn_mode
3713 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3715 /* Make a place to write the result of the instruction. */
3718 && REG_P (result) && GET_MODE (result) == insn_mode
3719 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3720 result = gen_reg_rtx (insn_mode);
3722 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3723 GEN_INT (MIN (arg1_align, arg2_align)));
3726 #ifdef HAVE_cmpstrnsi
3727 /* Try to determine at least one length and call cmpstrnsi. */
3728 if (!insn && HAVE_cmpstrnsi)
3733 enum machine_mode insn_mode
3734 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3735 tree len1 = c_strlen (arg1, 1);
3736 tree len2 = c_strlen (arg2, 1);
3739 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3741 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3743 /* If we don't have a constant length for the first, use the length
3744 of the second, if we know it. We don't require a constant for
3745 this case; some cost analysis could be done if both are available
3746 but neither is constant. For now, assume they're equally cheap,
3747 unless one has side effects. If both strings have constant lengths,
3754 else if (TREE_SIDE_EFFECTS (len1))
3756 else if (TREE_SIDE_EFFECTS (len2))
3758 else if (TREE_CODE (len1) != INTEGER_CST)
3760 else if (TREE_CODE (len2) != INTEGER_CST)
3762 else if (tree_int_cst_lt (len1, len2))
3767 /* If both arguments have side effects, we cannot optimize. */
3768 if (!len || TREE_SIDE_EFFECTS (len))
3771 arg3_rtx = expand_normal (len);
3773 /* Make a place to write the result of the instruction. */
3776 && REG_P (result) && GET_MODE (result) == insn_mode
3777 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3778 result = gen_reg_rtx (insn_mode);
3780 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3781 GEN_INT (MIN (arg1_align, arg2_align)));
3789 /* Return the value in the proper mode for this function. */
3790 mode = TYPE_MODE (TREE_TYPE (exp));
3791 if (GET_MODE (result) == mode)
3794 return convert_to_mode (mode, result, 0);
3795 convert_move (target, result, 0);
3799 /* Expand the library call ourselves using a stabilized argument
3800 list to avoid re-evaluating the function's arguments twice. */
3801 #ifdef HAVE_cmpstrnsi
3804 arglist = build_tree_list (NULL_TREE, arg2);
3805 arglist = tree_cons (NULL_TREE, arg1, arglist);
3806 fndecl = get_callee_fndecl (exp);
3807 fn = build_function_call_expr (fndecl, arglist);
3808 if (TREE_CODE (fn) == CALL_EXPR)
3809 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3810 return expand_call (fn, target, target == const0_rtx);
3816 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3817 if we failed the caller should emit a normal call, otherwise try to get
3818 the result in TARGET, if convenient. */
3821 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3823 tree arglist = TREE_OPERAND (exp, 1);
3825 if (!validate_arglist (arglist,
3826 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3830 tree result = fold_builtin_strncmp (arglist);
3832 return expand_expr (result, target, mode, EXPAND_NORMAL);
3835 /* If c_strlen can determine an expression for one of the string
3836 lengths, and it doesn't have side effects, then emit cmpstrnsi
3837 using length MIN(strlen(string)+1, arg3). */
3838 #ifdef HAVE_cmpstrnsi
3841 tree arg1 = TREE_VALUE (arglist);
3842 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3843 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3844 tree len, len1, len2;
3845 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3850 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3852 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3853 enum machine_mode insn_mode
3854 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3856 len1 = c_strlen (arg1, 1);
3857 len2 = c_strlen (arg2, 1);
3860 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3862 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3864 /* If we don't have a constant length for the first, use the length
3865 of the second, if we know it. We don't require a constant for
3866 this case; some cost analysis could be done if both are available
3867 but neither is constant. For now, assume they're equally cheap,
3868 unless one has side effects. If both strings have constant lengths,
3875 else if (TREE_SIDE_EFFECTS (len1))
3877 else if (TREE_SIDE_EFFECTS (len2))
3879 else if (TREE_CODE (len1) != INTEGER_CST)
3881 else if (TREE_CODE (len2) != INTEGER_CST)
3883 else if (tree_int_cst_lt (len1, len2))
3888 /* If both arguments have side effects, we cannot optimize. */
3889 if (!len || TREE_SIDE_EFFECTS (len))
3892 /* The actual new length parameter is MIN(len,arg3). */
3893 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
3894 fold_convert (TREE_TYPE (len), arg3));
3896 /* If we don't have POINTER_TYPE, call the function. */
3897 if (arg1_align == 0 || arg2_align == 0)
3900 /* Make a place to write the result of the instruction. */
3903 && REG_P (result) && GET_MODE (result) == insn_mode
3904 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3905 result = gen_reg_rtx (insn_mode);
3907 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3908 arg1 = builtin_save_expr (arg1);
3909 arg2 = builtin_save_expr (arg2);
3910 len = builtin_save_expr (len);
3912 arg1_rtx = get_memory_rtx (arg1, len);
3913 arg2_rtx = get_memory_rtx (arg2, len);
3914 arg3_rtx = expand_normal (len);
3915 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3916 GEN_INT (MIN (arg1_align, arg2_align)));
3921 /* Return the value in the proper mode for this function. */
3922 mode = TYPE_MODE (TREE_TYPE (exp));
3923 if (GET_MODE (result) == mode)
3926 return convert_to_mode (mode, result, 0);
3927 convert_move (target, result, 0);
3931 /* Expand the library call ourselves using a stabilized argument
3932 list to avoid re-evaluating the function's arguments twice. */
3933 arglist = build_tree_list (NULL_TREE, len);
3934 arglist = tree_cons (NULL_TREE, arg2, arglist);
3935 arglist = tree_cons (NULL_TREE, arg1, arglist);
3936 fndecl = get_callee_fndecl (exp);
3937 fn = build_function_call_expr (fndecl, arglist);
3938 if (TREE_CODE (fn) == CALL_EXPR)
3939 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3940 return expand_call (fn, target, target == const0_rtx);
3946 /* Expand expression EXP, which is a call to the strcat builtin.
3947 Return 0 if we failed the caller should emit a normal call,
3948 otherwise try to get the result in TARGET, if convenient. */
3951 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3953 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3957 tree dst = TREE_VALUE (arglist),
3958 src = TREE_VALUE (TREE_CHAIN (arglist));
3959 const char *p = c_getstr (src);
3961 /* If the string length is zero, return the dst parameter. */
3962 if (p && *p == '\0')
3963 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3967 /* See if we can store by pieces into (dst + strlen(dst)). */
3968 tree newsrc, newdst,
3969 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3972 /* Stabilize the argument list. */
3973 newsrc = builtin_save_expr (src);
3975 arglist = build_tree_list (NULL_TREE, newsrc);
3977 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
3979 dst = builtin_save_expr (dst);
3983 /* Create strlen (dst). */
3985 build_function_call_expr (strlen_fn,
3986 build_tree_list (NULL_TREE, dst));
3987 /* Create (dst + (cast) strlen (dst)). */
3988 newdst = fold_convert (TREE_TYPE (dst), newdst);
3989 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
3991 newdst = builtin_save_expr (newdst);
3992 arglist = tree_cons (NULL_TREE, newdst, arglist);
3994 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
3996 end_sequence (); /* Stop sequence. */
4000 /* Output the entire sequence. */
4001 insns = get_insns ();
4005 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4012 /* Expand expression EXP, which is a call to the strncat builtin.
4013 Return 0 if we failed the caller should emit a normal call,
4014 otherwise try to get the result in TARGET, if convenient. */
4017 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4019 if (validate_arglist (arglist,
4020 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4022 tree result = fold_builtin_strncat (arglist);
4024 return expand_expr (result, target, mode, EXPAND_NORMAL);
4029 /* Expand expression EXP, which is a call to the strspn builtin.
4030 Return 0 if we failed the caller should emit a normal call,
4031 otherwise try to get the result in TARGET, if convenient. */
4034 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4036 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4038 tree result = fold_builtin_strspn (arglist);
4040 return expand_expr (result, target, mode, EXPAND_NORMAL);
4045 /* Expand expression EXP, which is a call to the strcspn builtin.
4046 Return 0 if we failed the caller should emit a normal call,
4047 otherwise try to get the result in TARGET, if convenient. */
4050 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4052 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4054 tree result = fold_builtin_strcspn (arglist);
4056 return expand_expr (result, target, mode, EXPAND_NORMAL);
4061 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4062 if that's convenient. */
4065 expand_builtin_saveregs (void)
4069 /* Don't do __builtin_saveregs more than once in a function.
4070 Save the result of the first call and reuse it. */
4071 if (saveregs_value != 0)
4072 return saveregs_value;
4074 /* When this function is called, it means that registers must be
4075 saved on entry to this function. So we migrate the call to the
4076 first insn of this function. */
4080 /* Do whatever the machine needs done in this case. */
4081 val = targetm.calls.expand_builtin_saveregs ();
4086 saveregs_value = val;
4088 /* Put the insns after the NOTE that starts the function. If this
4089 is inside a start_sequence, make the outer-level insn chain current, so
4090 the code is placed at the start of the function. */
4091 push_topmost_sequence ();
4092 emit_insn_after (seq, entry_of_function ());
4093 pop_topmost_sequence ();
4098 /* __builtin_args_info (N) returns word N of the arg space info
4099 for the current function. The number and meanings of words
4100 is controlled by the definition of CUMULATIVE_ARGS. */
4103 expand_builtin_args_info (tree arglist)
4105 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4106 int *word_ptr = (int *) ¤t_function_args_info;
4108 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4112 if (!host_integerp (TREE_VALUE (arglist), 0))
4113 error ("argument of %<__builtin_args_info%> must be constant");
4116 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4118 if (wordnum < 0 || wordnum >= nwords)
4119 error ("argument of %<__builtin_args_info%> out of range");
4121 return GEN_INT (word_ptr[wordnum]);
4125 error ("missing argument in %<__builtin_args_info%>");
4130 /* Expand a call to __builtin_next_arg. */
4133 expand_builtin_next_arg (void)
4135 /* Checking arguments is already done in fold_builtin_next_arg
4136 that must be called before this function. */
4137 return expand_binop (Pmode, add_optab,
4138 current_function_internal_arg_pointer,
4139 current_function_arg_offset_rtx,
4140 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4143 /* Make it easier for the backends by protecting the valist argument
4144 from multiple evaluations. */
4147 stabilize_va_list (tree valist, int needs_lvalue)
4149 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4151 if (TREE_SIDE_EFFECTS (valist))
4152 valist = save_expr (valist);
4154 /* For this case, the backends will be expecting a pointer to
4155 TREE_TYPE (va_list_type_node), but it's possible we've
4156 actually been given an array (an actual va_list_type_node).
4158 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4160 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4161 valist = build_fold_addr_expr_with_type (valist, p1);
4170 if (! TREE_SIDE_EFFECTS (valist))
4173 pt = build_pointer_type (va_list_type_node);
4174 valist = fold_build1 (ADDR_EXPR, pt, valist);
4175 TREE_SIDE_EFFECTS (valist) = 1;
4178 if (TREE_SIDE_EFFECTS (valist))
4179 valist = save_expr (valist);
4180 valist = build_fold_indirect_ref (valist);
4186 /* The "standard" definition of va_list is void*. */
4189 std_build_builtin_va_list (void)
4191 return ptr_type_node;
4194 /* The "standard" implementation of va_start: just assign `nextarg' to
4198 std_expand_builtin_va_start (tree valist, rtx nextarg)
4202 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4203 make_tree (ptr_type_node, nextarg));
4204 TREE_SIDE_EFFECTS (t) = 1;
4206 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4209 /* Expand ARGLIST, from a call to __builtin_va_start. */
4212 expand_builtin_va_start (tree arglist)
4217 chain = TREE_CHAIN (arglist);
4221 error ("too few arguments to function %<va_start%>");
4225 if (fold_builtin_next_arg (chain))
4228 nextarg = expand_builtin_next_arg ();
4229 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4231 #ifdef EXPAND_BUILTIN_VA_START
4232 EXPAND_BUILTIN_VA_START (valist, nextarg);
4234 std_expand_builtin_va_start (valist, nextarg);
4240 /* The "standard" implementation of va_arg: read the value from the
4241 current (padded) address and increment by the (padded) size. */
4244 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4246 tree addr, t, type_size, rounded_size, valist_tmp;
4247 unsigned HOST_WIDE_INT align, boundary;
4250 #ifdef ARGS_GROW_DOWNWARD
4251 /* All of the alignment and movement below is for args-grow-up machines.
4252 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4253 implement their own specialized gimplify_va_arg_expr routines. */
4257 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4259 type = build_pointer_type (type);
4261 align = PARM_BOUNDARY / BITS_PER_UNIT;
4262 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4264 /* Hoist the valist value into a temporary for the moment. */
4265 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4267 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4268 requires greater alignment, we must perform dynamic alignment. */
4269 if (boundary > align
4270 && !integer_zerop (TYPE_SIZE (type)))
4272 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4273 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4274 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4275 gimplify_and_add (t, pre_p);
4277 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4278 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4279 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4280 gimplify_and_add (t, pre_p);
4285 /* If the actual alignment is less than the alignment of the type,
4286 adjust the type accordingly so that we don't assume strict alignment
4287 when deferencing the pointer. */
4288 boundary *= BITS_PER_UNIT;
4289 if (boundary < TYPE_ALIGN (type))
4291 type = build_variant_type_copy (type);
4292 TYPE_ALIGN (type) = boundary;
4295 /* Compute the rounded size of the type. */
4296 type_size = size_in_bytes (type);
4297 rounded_size = round_up (type_size, align);
4299 /* Reduce rounded_size so it's sharable with the postqueue. */
4300 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4304 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4306 /* Small args are padded downward. */
4307 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4308 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4309 size_binop (MINUS_EXPR, rounded_size, type_size));
4310 t = fold_convert (TREE_TYPE (addr), t);
4311 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4314 /* Compute new value for AP. */
4315 t = fold_convert (TREE_TYPE (valist), rounded_size);
4316 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4317 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4318 gimplify_and_add (t, pre_p);
4320 addr = fold_convert (build_pointer_type (type), addr);
4323 addr = build_va_arg_indirect_ref (addr);
4325 return build_va_arg_indirect_ref (addr);
4328 /* Build an indirect-ref expression over the given TREE, which represents a
4329 piece of a va_arg() expansion. */
4331 build_va_arg_indirect_ref (tree addr)
4333 addr = build_fold_indirect_ref (addr);
4335 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4341 /* Return a dummy expression of type TYPE in order to keep going after an
4345 dummy_object (tree type)
4347 tree t = build_int_cst (build_pointer_type (type), 0);
4348 return build1 (INDIRECT_REF, type, t);
4351 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4352 builtin function, but a very special sort of operator. */
4354 enum gimplify_status
4355 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4357 tree promoted_type, want_va_type, have_va_type;
4358 tree valist = TREE_OPERAND (*expr_p, 0);
4359 tree type = TREE_TYPE (*expr_p);
4362 /* Verify that valist is of the proper type. */
4363 want_va_type = va_list_type_node;
4364 have_va_type = TREE_TYPE (valist);
4366 if (have_va_type == error_mark_node)
4369 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4371 /* If va_list is an array type, the argument may have decayed
4372 to a pointer type, e.g. by being passed to another function.
4373 In that case, unwrap both types so that we can compare the
4374 underlying records. */
4375 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4376 || POINTER_TYPE_P (have_va_type))
4378 want_va_type = TREE_TYPE (want_va_type);
4379 have_va_type = TREE_TYPE (have_va_type);
4383 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4385 error ("first argument to %<va_arg%> not of type %<va_list%>");
4389 /* Generate a diagnostic for requesting data of a type that cannot
4390 be passed through `...' due to type promotion at the call site. */
4391 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4394 static bool gave_help;
4396 /* Unfortunately, this is merely undefined, rather than a constraint
4397 violation, so we cannot make this an error. If this call is never
4398 executed, the program is still strictly conforming. */
4399 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4400 type, promoted_type);
4404 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4405 promoted_type, type);
4408 /* We can, however, treat "undefined" any way we please.
4409 Call abort to encourage the user to fix the program. */
4410 inform ("if this code is reached, the program will abort");
4411 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4413 append_to_statement_list (t, pre_p);
4415 /* This is dead code, but go ahead and finish so that the
4416 mode of the result comes out right. */
4417 *expr_p = dummy_object (type);
4422 /* Make it easier for the backends by protecting the valist argument
4423 from multiple evaluations. */
4424 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4426 /* For this case, the backends will be expecting a pointer to
4427 TREE_TYPE (va_list_type_node), but it's possible we've
4428 actually been given an array (an actual va_list_type_node).
4430 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4432 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4433 valist = build_fold_addr_expr_with_type (valist, p1);
4435 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4438 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4440 if (!targetm.gimplify_va_arg_expr)
4441 /* FIXME:Once most targets are converted we should merely
4442 assert this is non-null. */
4445 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4450 /* Expand ARGLIST, from a call to __builtin_va_end. */
4453 expand_builtin_va_end (tree arglist)
4455 tree valist = TREE_VALUE (arglist);
4457 /* Evaluate for side effects, if needed. I hate macros that don't
4459 if (TREE_SIDE_EFFECTS (valist))
4460 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4465 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4466 builtin rather than just as an assignment in stdarg.h because of the
4467 nastiness of array-type va_list types. */
4470 expand_builtin_va_copy (tree arglist)
4474 dst = TREE_VALUE (arglist);
4475 src = TREE_VALUE (TREE_CHAIN (arglist));
4477 dst = stabilize_va_list (dst, 1);
4478 src = stabilize_va_list (src, 0);
4480 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4482 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4483 TREE_SIDE_EFFECTS (t) = 1;
4484 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4488 rtx dstb, srcb, size;
4490 /* Evaluate to pointers. */
4491 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4492 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4493 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4494 VOIDmode, EXPAND_NORMAL);
4496 dstb = convert_memory_address (Pmode, dstb);
4497 srcb = convert_memory_address (Pmode, srcb);
4499 /* "Dereference" to BLKmode memories. */
4500 dstb = gen_rtx_MEM (BLKmode, dstb);
4501 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4502 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4503 srcb = gen_rtx_MEM (BLKmode, srcb);
4504 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4505 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4508 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4514 /* Expand a call to one of the builtin functions __builtin_frame_address or
4515 __builtin_return_address. */
4518 expand_builtin_frame_address (tree fndecl, tree arglist)
4520 /* The argument must be a nonnegative integer constant.
4521 It counts the number of frames to scan up the stack.
4522 The value is the return address saved in that frame. */
4524 /* Warning about missing arg was already issued. */
4526 else if (! host_integerp (TREE_VALUE (arglist), 1))
4528 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4529 error ("invalid argument to %<__builtin_frame_address%>");
4531 error ("invalid argument to %<__builtin_return_address%>");
4537 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4538 tree_low_cst (TREE_VALUE (arglist), 1));
4540 /* Some ports cannot access arbitrary stack frames. */
4543 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4544 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4546 warning (0, "unsupported argument to %<__builtin_return_address%>");
4550 /* For __builtin_frame_address, return what we've got. */
4551 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4555 && ! CONSTANT_P (tem))
4556 tem = copy_to_mode_reg (Pmode, tem);
4561 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4562 we failed and the caller should emit a normal call, otherwise try to get
4563 the result in TARGET, if convenient. */
4566 expand_builtin_alloca (tree arglist, rtx target)
4571 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4572 should always expand to function calls. These can be intercepted
4577 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4580 /* Compute the argument. */
4581 op0 = expand_normal (TREE_VALUE (arglist));
4583 /* Allocate the desired space. */
4584 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4585 result = convert_memory_address (ptr_mode, result);
4590 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4591 is the mode to expand with. */
4594 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4596 enum machine_mode mode;
4600 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4603 arg = TREE_VALUE (arglist);
4604 mode = TYPE_MODE (TREE_TYPE (arg));
4605 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4607 target = expand_unop (mode, bswap_optab, op0, target, 1);
4609 gcc_assert (target);
4611 return convert_to_mode (mode, target, 0);
4614 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4615 Return 0 if a normal call should be emitted rather than expanding the
4616 function in-line. If convenient, the result should be placed in TARGET.
4617 SUBTARGET may be used as the target for computing one of EXP's operands. */
4620 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4621 rtx subtarget, optab op_optab)
4624 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4627 /* Compute the argument. */
4628 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4629 /* Compute op, into TARGET if possible.
4630 Set TARGET to wherever the result comes back. */
4631 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4632 op_optab, op0, target, 1);
4633 gcc_assert (target);
4635 return convert_to_mode (target_mode, target, 0);
4638 /* If the string passed to fputs is a constant and is one character
4639 long, we attempt to transform this call into __builtin_fputc(). */
4642 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4644 /* Verify the arguments in the original call. */
4645 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4647 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4648 unlocked, NULL_TREE);
4650 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4655 /* Expand a call to __builtin_expect. We return our argument and emit a
4656 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4657 a non-jump context. */
4660 expand_builtin_expect (tree arglist, rtx target)
4665 if (arglist == NULL_TREE
4666 || TREE_CHAIN (arglist) == NULL_TREE)
4668 exp = TREE_VALUE (arglist);
4669 c = TREE_VALUE (TREE_CHAIN (arglist));
4671 if (TREE_CODE (c) != INTEGER_CST)
4673 error ("second argument to %<__builtin_expect%> must be a constant");
4674 c = integer_zero_node;
4677 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4679 /* Don't bother with expected value notes for integral constants. */
4680 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4682 /* We do need to force this into a register so that we can be
4683 moderately sure to be able to correctly interpret the branch
4685 target = force_reg (GET_MODE (target), target);
4687 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4689 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4690 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4696 /* Like expand_builtin_expect, except do this in a jump context. This is
4697 called from do_jump if the conditional is a __builtin_expect. Return either
4698 a list of insns to emit the jump or NULL if we cannot optimize
4699 __builtin_expect. We need to optimize this at jump time so that machines
4700 like the PowerPC don't turn the test into a SCC operation, and then jump
4701 based on the test being 0/1. */
4704 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4706 tree arglist = TREE_OPERAND (exp, 1);
4707 tree arg0 = TREE_VALUE (arglist);
4708 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4711 /* Only handle __builtin_expect (test, 0) and
4712 __builtin_expect (test, 1). */
4713 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4714 && (integer_zerop (arg1) || integer_onep (arg1)))
4716 rtx insn, drop_through_label, temp;
4718 /* Expand the jump insns. */
4720 do_jump (arg0, if_false_label, if_true_label);
4723 drop_through_label = get_last_insn ();
4724 if (drop_through_label && NOTE_P (drop_through_label))
4725 drop_through_label = prev_nonnote_insn (drop_through_label);
4726 if (drop_through_label && !LABEL_P (drop_through_label))
4727 drop_through_label = NULL_RTX;
4730 if (! if_true_label)
4731 if_true_label = drop_through_label;
4732 if (! if_false_label)
4733 if_false_label = drop_through_label;
4735 /* Go through and add the expect's to each of the conditional jumps. */
4737 while (insn != NULL_RTX)
4739 rtx next = NEXT_INSN (insn);
4741 if (JUMP_P (insn) && any_condjump_p (insn))
4743 rtx ifelse = SET_SRC (pc_set (insn));
4744 rtx then_dest = XEXP (ifelse, 1);
4745 rtx else_dest = XEXP (ifelse, 2);
4748 /* First check if we recognize any of the labels. */
4749 if (GET_CODE (then_dest) == LABEL_REF
4750 && XEXP (then_dest, 0) == if_true_label)
4752 else if (GET_CODE (then_dest) == LABEL_REF
4753 && XEXP (then_dest, 0) == if_false_label)
4755 else if (GET_CODE (else_dest) == LABEL_REF
4756 && XEXP (else_dest, 0) == if_false_label)
4758 else if (GET_CODE (else_dest) == LABEL_REF
4759 && XEXP (else_dest, 0) == if_true_label)
4761 /* Otherwise check where we drop through. */
4762 else if (else_dest == pc_rtx)
4764 if (next && NOTE_P (next))
4765 next = next_nonnote_insn (next);
4767 if (next && JUMP_P (next)
4768 && any_uncondjump_p (next))
4769 temp = XEXP (SET_SRC (pc_set (next)), 0);
4773 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4774 else that can't possibly match either target label. */
4775 if (temp == if_false_label)
4777 else if (temp == if_true_label)
4780 else if (then_dest == pc_rtx)
4782 if (next && NOTE_P (next))
4783 next = next_nonnote_insn (next);
4785 if (next && JUMP_P (next)
4786 && any_uncondjump_p (next))
4787 temp = XEXP (SET_SRC (pc_set (next)), 0);
4791 if (temp == if_false_label)
4793 else if (temp == if_true_label)
4799 /* If the test is expected to fail, reverse the
4801 if (integer_zerop (arg1))
4803 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4815 expand_builtin_trap (void)
4819 emit_insn (gen_trap ());
4822 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4826 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4827 Return 0 if a normal call should be emitted rather than expanding
4828 the function inline. If convenient, the result should be placed
4829 in TARGET. SUBTARGET may be used as the target for computing
4833 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4835 enum machine_mode mode;
4839 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4842 arg = TREE_VALUE (arglist);
4843 mode = TYPE_MODE (TREE_TYPE (arg));
4844 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4845 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4848 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4849 Return NULL is a normal call should be emitted rather than expanding the
4850 function inline. If convenient, the result should be placed in TARGET.
4851 SUBTARGET may be used as the target for computing the operand. */
4854 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4859 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4862 arg = TREE_VALUE (arglist);
4863 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4865 arg = TREE_VALUE (TREE_CHAIN (arglist));
4866 op1 = expand_normal (arg);
4868 return expand_copysign (op0, op1, target);
4871 /* Create a new constant string literal and return a char* pointer to it.
4872 The STRING_CST value is the LEN characters at STR. */
4874 build_string_literal (int len, const char *str)
4876 tree t, elem, index, type;
4878 t = build_string (len, str);
4879 elem = build_type_variant (char_type_node, 1, 0);
4880 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4881 type = build_array_type (elem, index);
4882 TREE_TYPE (t) = type;
4883 TREE_CONSTANT (t) = 1;
4884 TREE_INVARIANT (t) = 1;
4885 TREE_READONLY (t) = 1;
4886 TREE_STATIC (t) = 1;
4888 type = build_pointer_type (type);
4889 t = build1 (ADDR_EXPR, type, t);
4891 type = build_pointer_type (elem);
4892 t = build1 (NOP_EXPR, type, t);
4896 /* Expand EXP, a call to printf or printf_unlocked.
4897 Return 0 if a normal call should be emitted rather than transforming
4898 the function inline. If convenient, the result should be placed in
4899 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4902 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4905 tree arglist = TREE_OPERAND (exp, 1);
4906 /* If we're using an unlocked function, assume the other unlocked
4907 functions exist explicitly. */
4908 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4909 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4910 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4911 : implicit_built_in_decls[BUILT_IN_PUTS];
4912 const char *fmt_str;
4915 /* If the return value is used, don't do the transformation. */
4916 if (target != const0_rtx)
4919 /* Verify the required arguments in the original call. */
4922 fmt = TREE_VALUE (arglist);
4923 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4925 arglist = TREE_CHAIN (arglist);
4927 /* Check whether the format is a literal string constant. */
4928 fmt_str = c_getstr (fmt);
4929 if (fmt_str == NULL)
4932 if (!init_target_chars())
4935 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4936 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4939 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4940 || TREE_CHAIN (arglist))
4944 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4945 else if (strcmp (fmt_str, target_percent_c) == 0)
4948 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4949 || TREE_CHAIN (arglist))
4955 /* We can't handle anything else with % args or %% ... yet. */
4956 if (strchr (fmt_str, target_percent))
4962 /* If the format specifier was "", printf does nothing. */
4963 if (fmt_str[0] == '\0')
4965 /* If the format specifier has length of 1, call putchar. */
4966 if (fmt_str[1] == '\0')
4968 /* Given printf("c"), (where c is any one character,)
4969 convert "c"[0] to an int and pass that to the replacement
4971 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4972 arglist = build_tree_list (NULL_TREE, arg);
4977 /* If the format specifier was "string\n", call puts("string"). */
4978 size_t len = strlen (fmt_str);
4979 if ((unsigned char)fmt_str[len - 1] == target_newline)
4981 /* Create a NUL-terminated string that's one char shorter
4982 than the original, stripping off the trailing '\n'. */
4983 char *newstr = alloca (len);
4984 memcpy (newstr, fmt_str, len - 1);
4985 newstr[len - 1] = 0;
4987 arg = build_string_literal (len, newstr);
4988 arglist = build_tree_list (NULL_TREE, arg);
4992 /* We'd like to arrange to call fputs(string,stdout) here,
4993 but we need stdout and don't have a way to get it yet. */
5000 fn = build_function_call_expr (fn, arglist);
5001 if (TREE_CODE (fn) == CALL_EXPR)
5002 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5003 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5006 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5007 Return 0 if a normal call should be emitted rather than transforming
5008 the function inline. If convenient, the result should be placed in
5009 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5012 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5015 tree arglist = TREE_OPERAND (exp, 1);
5016 /* If we're using an unlocked function, assume the other unlocked
5017 functions exist explicitly. */
5018 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5019 : implicit_built_in_decls[BUILT_IN_FPUTC];
5020 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5021 : implicit_built_in_decls[BUILT_IN_FPUTS];
5022 const char *fmt_str;
5023 tree fn, fmt, fp, arg;
5025 /* If the return value is used, don't do the transformation. */
5026 if (target != const0_rtx)
5029 /* Verify the required arguments in the original call. */
5032 fp = TREE_VALUE (arglist);
5033 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5035 arglist = TREE_CHAIN (arglist);
5038 fmt = TREE_VALUE (arglist);
5039 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5041 arglist = TREE_CHAIN (arglist);
5043 /* Check whether the format is a literal string constant. */
5044 fmt_str = c_getstr (fmt);
5045 if (fmt_str == NULL)
5048 if (!init_target_chars())
5051 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5052 if (strcmp (fmt_str, target_percent_s) == 0)
5055 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5056 || TREE_CHAIN (arglist))
5058 arg = TREE_VALUE (arglist);
5059 arglist = build_tree_list (NULL_TREE, fp);
5060 arglist = tree_cons (NULL_TREE, arg, arglist);
5063 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5064 else if (strcmp (fmt_str, target_percent_c) == 0)
5067 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5068 || TREE_CHAIN (arglist))
5070 arg = TREE_VALUE (arglist);
5071 arglist = build_tree_list (NULL_TREE, fp);
5072 arglist = tree_cons (NULL_TREE, arg, arglist);
5077 /* We can't handle anything else with % args or %% ... yet. */
5078 if (strchr (fmt_str, target_percent))
5084 /* If the format specifier was "", fprintf does nothing. */
5085 if (fmt_str[0] == '\0')
5087 /* Evaluate and ignore FILE* argument for side-effects. */
5088 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5092 /* When "string" doesn't contain %, replace all cases of
5093 fprintf(stream,string) with fputs(string,stream). The fputs
5094 builtin will take care of special cases like length == 1. */
5095 arglist = build_tree_list (NULL_TREE, fp);
5096 arglist = tree_cons (NULL_TREE, fmt, arglist);
5102 fn = build_function_call_expr (fn, arglist);
5103 if (TREE_CODE (fn) == CALL_EXPR)
5104 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5105 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5108 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5109 a normal call should be emitted rather than expanding the function
5110 inline. If convenient, the result should be placed in TARGET with
5114 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5116 tree orig_arglist, dest, fmt;
5117 const char *fmt_str;
5119 orig_arglist = arglist;
5121 /* Verify the required arguments in the original call. */
5124 dest = TREE_VALUE (arglist);
5125 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5127 arglist = TREE_CHAIN (arglist);
5130 fmt = TREE_VALUE (arglist);
5131 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5133 arglist = TREE_CHAIN (arglist);
5135 /* Check whether the format is a literal string constant. */
5136 fmt_str = c_getstr (fmt);
5137 if (fmt_str == NULL)
5140 if (!init_target_chars())
5143 /* If the format doesn't contain % args or %%, use strcpy. */
5144 if (strchr (fmt_str, target_percent) == 0)
5146 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5149 if (arglist || ! fn)
5151 expand_expr (build_function_call_expr (fn, orig_arglist),
5152 const0_rtx, VOIDmode, EXPAND_NORMAL);
5153 if (target == const0_rtx)
5155 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5156 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5158 /* If the format is "%s", use strcpy if the result isn't used. */
5159 else if (strcmp (fmt_str, target_percent_s) == 0)
5162 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5167 if (! arglist || TREE_CHAIN (arglist))
5169 arg = TREE_VALUE (arglist);
5170 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5173 if (target != const0_rtx)
5175 len = c_strlen (arg, 1);
5176 if (! len || TREE_CODE (len) != INTEGER_CST)
5182 arglist = build_tree_list (NULL_TREE, arg);
5183 arglist = tree_cons (NULL_TREE, dest, arglist);
5184 expand_expr (build_function_call_expr (fn, arglist),
5185 const0_rtx, VOIDmode, EXPAND_NORMAL);
5187 if (target == const0_rtx)
5189 return expand_expr (len, target, mode, EXPAND_NORMAL);
5195 /* Expand a call to either the entry or exit function profiler. */
5198 expand_builtin_profile_func (bool exitp)
5202 this = DECL_RTL (current_function_decl);
5203 gcc_assert (MEM_P (this));
5204 this = XEXP (this, 0);
5207 which = profile_function_exit_libfunc;
5209 which = profile_function_entry_libfunc;
5211 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5212 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5219 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5222 round_trampoline_addr (rtx tramp)
5224 rtx temp, addend, mask;
5226 /* If we don't need too much alignment, we'll have been guaranteed
5227 proper alignment by get_trampoline_type. */
5228 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5231 /* Round address up to desired boundary. */
5232 temp = gen_reg_rtx (Pmode);
5233 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5234 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5236 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5237 temp, 0, OPTAB_LIB_WIDEN);
5238 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5239 temp, 0, OPTAB_LIB_WIDEN);
5245 expand_builtin_init_trampoline (tree arglist)
5247 tree t_tramp, t_func, t_chain;
5248 rtx r_tramp, r_func, r_chain;
5249 #ifdef TRAMPOLINE_TEMPLATE
5253 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5254 POINTER_TYPE, VOID_TYPE))
5257 t_tramp = TREE_VALUE (arglist);
5258 arglist = TREE_CHAIN (arglist);
5259 t_func = TREE_VALUE (arglist);
5260 arglist = TREE_CHAIN (arglist);
5261 t_chain = TREE_VALUE (arglist);
5263 r_tramp = expand_normal (t_tramp);
5264 r_func = expand_normal (t_func);
5265 r_chain = expand_normal (t_chain);
5267 /* Generate insns to initialize the trampoline. */
5268 r_tramp = round_trampoline_addr (r_tramp);
5269 #ifdef TRAMPOLINE_TEMPLATE
5270 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5271 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5272 emit_block_move (blktramp, assemble_trampoline_template (),
5273 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5275 trampolines_created = 1;
5276 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5282 expand_builtin_adjust_trampoline (tree arglist)
5286 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5289 tramp = expand_normal (TREE_VALUE (arglist));
5290 tramp = round_trampoline_addr (tramp);
5291 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5292 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5298 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5299 Return NULL_RTX if a normal call should be emitted rather than expanding
5300 the function in-line. EXP is the expression that is a call to the builtin
5301 function; if convenient, the result should be placed in TARGET. */
5304 expand_builtin_signbit (tree exp, rtx target)
5306 const struct real_format *fmt;
5307 enum machine_mode fmode, imode, rmode;
5308 HOST_WIDE_INT hi, lo;
5313 arglist = TREE_OPERAND (exp, 1);
5314 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5317 arg = TREE_VALUE (arglist);
5318 fmode = TYPE_MODE (TREE_TYPE (arg));
5319 rmode = TYPE_MODE (TREE_TYPE (exp));
5320 fmt = REAL_MODE_FORMAT (fmode);
5322 /* For floating point formats without a sign bit, implement signbit
5324 bitpos = fmt->signbit_ro;
5327 /* But we can't do this if the format supports signed zero. */
5328 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5331 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5332 build_real (TREE_TYPE (arg), dconst0));
5333 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5336 temp = expand_normal (arg);
5337 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5339 imode = int_mode_for_mode (fmode);
5340 if (imode == BLKmode)
5342 temp = gen_lowpart (imode, temp);
5347 /* Handle targets with different FP word orders. */
5348 if (FLOAT_WORDS_BIG_ENDIAN)
5349 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5351 word = bitpos / BITS_PER_WORD;
5352 temp = operand_subword_force (temp, word, fmode);
5353 bitpos = bitpos % BITS_PER_WORD;
5356 /* Force the intermediate word_mode (or narrower) result into a
5357 register. This avoids attempting to create paradoxical SUBREGs
5358 of floating point modes below. */
5359 temp = force_reg (imode, temp);
5361 /* If the bitpos is within the "result mode" lowpart, the operation
5362 can be implement with a single bitwise AND. Otherwise, we need
5363 a right shift and an AND. */
5365 if (bitpos < GET_MODE_BITSIZE (rmode))
5367 if (bitpos < HOST_BITS_PER_WIDE_INT)
5370 lo = (HOST_WIDE_INT) 1 << bitpos;
5374 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5379 temp = gen_lowpart (rmode, temp);
5380 temp = expand_binop (rmode, and_optab, temp,
5381 immed_double_const (lo, hi, rmode),
5382 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5386 /* Perform a logical right shift to place the signbit in the least
5387 significant bit, then truncate the result to the desired mode
5388 and mask just this bit. */
5389 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5390 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5391 temp = gen_lowpart (rmode, temp);
5392 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5393 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5399 /* Expand fork or exec calls. TARGET is the desired target of the
5400 call. ARGLIST is the list of arguments of the call. FN is the
5401 identificator of the actual function. IGNORE is nonzero if the
5402 value is to be ignored. */
5405 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5410 /* If we are not profiling, just call the function. */
5411 if (!profile_arc_flag)
5414 /* Otherwise call the wrapper. This should be equivalent for the rest of
5415 compiler, so the code does not diverge, and the wrapper may run the
5416 code necessary for keeping the profiling sane. */
5418 switch (DECL_FUNCTION_CODE (fn))
5421 id = get_identifier ("__gcov_fork");
5424 case BUILT_IN_EXECL:
5425 id = get_identifier ("__gcov_execl");
5428 case BUILT_IN_EXECV:
5429 id = get_identifier ("__gcov_execv");
5432 case BUILT_IN_EXECLP:
5433 id = get_identifier ("__gcov_execlp");
5436 case BUILT_IN_EXECLE:
5437 id = get_identifier ("__gcov_execle");
5440 case BUILT_IN_EXECVP:
5441 id = get_identifier ("__gcov_execvp");
5444 case BUILT_IN_EXECVE:
5445 id = get_identifier ("__gcov_execve");
5452 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5453 DECL_EXTERNAL (decl) = 1;
5454 TREE_PUBLIC (decl) = 1;
5455 DECL_ARTIFICIAL (decl) = 1;
5456 TREE_NOTHROW (decl) = 1;
5457 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5458 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5459 call = build_function_call_expr (decl, arglist);
5461 return expand_call (call, target, ignore);
5465 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5466 the pointer in these functions is void*, the tree optimizers may remove
5467 casts. The mode computed in expand_builtin isn't reliable either, due
5468 to __sync_bool_compare_and_swap.
5470 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5471 group of builtins. This gives us log2 of the mode size. */
5473 static inline enum machine_mode
5474 get_builtin_sync_mode (int fcode_diff)
5476 /* The size is not negotiable, so ask not to get BLKmode in return
5477 if the target indicates that a smaller size would be better. */
5478 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5481 /* Expand the memory expression LOC and return the appropriate memory operand
5482 for the builtin_sync operations. */
5485 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5489 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5491 /* Note that we explicitly do not want any alias information for this
5492 memory, so that we kill all other live memories. Otherwise we don't
5493 satisfy the full barrier semantics of the intrinsic. */
5494 mem = validize_mem (gen_rtx_MEM (mode, addr));
5496 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5497 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5498 MEM_VOLATILE_P (mem) = 1;
5503 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5504 ARGLIST is the operands list to the function. CODE is the rtx code
5505 that corresponds to the arithmetic or logical operation from the name;
5506 an exception here is that NOT actually means NAND. TARGET is an optional
5507 place for us to store the results; AFTER is true if this is the
5508 fetch_and_xxx form. IGNORE is true if we don't actually care about
5509 the result of the operation at all. */
5512 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5513 enum rtx_code code, bool after,
5514 rtx target, bool ignore)
5517 enum machine_mode old_mode;
5519 /* Expand the operands. */
5520 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5522 arglist = TREE_CHAIN (arglist);
5523 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5524 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5525 of CONST_INTs, where we know the old_mode only from the call argument. */
5526 old_mode = GET_MODE (val);
5527 if (old_mode == VOIDmode)
5528 old_mode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
5529 val = convert_modes (mode, old_mode, val, 1);
5532 return expand_sync_operation (mem, val, code);
5534 return expand_sync_fetch_operation (mem, val, code, after, target);
5537 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5538 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5539 true if this is the boolean form. TARGET is a place for us to store the
5540 results; this is NOT optional if IS_BOOL is true. */
5543 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5544 bool is_bool, rtx target)
5546 rtx old_val, new_val, mem;
5547 enum machine_mode old_mode;
5549 /* Expand the operands. */
5550 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5552 arglist = TREE_CHAIN (arglist);
5553 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5554 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5555 of CONST_INTs, where we know the old_mode only from the call argument. */
5556 old_mode = GET_MODE (old_val);
5557 if (old_mode == VOIDmode)
5558 old_mode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
5559 old_val = convert_modes (mode, old_mode, old_val, 1);
5561 arglist = TREE_CHAIN (arglist);
5562 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5563 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5564 of CONST_INTs, where we know the old_mode only from the call argument. */
5565 old_mode = GET_MODE (new_val);
5566 if (old_mode == VOIDmode)
5567 old_mode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
5568 new_val = convert_modes (mode, old_mode, new_val, 1);
5571 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5573 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5576 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5577 general form is actually an atomic exchange, and some targets only
5578 support a reduced form with the second argument being a constant 1.
5579 ARGLIST is the operands list to the function; TARGET is an optional
5580 place for us to store the results. */
5583 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5587 enum machine_mode old_mode;
5589 /* Expand the operands. */
5590 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5592 arglist = TREE_CHAIN (arglist);
5593 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5594 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5595 of CONST_INTs, where we know the old_mode only from the call argument. */
5596 old_mode = GET_MODE (val);
5597 if (old_mode == VOIDmode)
5598 old_mode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
5599 val = convert_modes (mode, old_mode, val, 1);
5601 return expand_sync_lock_test_and_set (mem, val, target);
5604 /* Expand the __sync_synchronize intrinsic. */
5607 expand_builtin_synchronize (void)
5611 #ifdef HAVE_memory_barrier
5612 if (HAVE_memory_barrier)
5614 emit_insn (gen_memory_barrier ());
5619 /* If no explicit memory barrier instruction is available, create an
5620 empty asm stmt with a memory clobber. */
5621 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5622 tree_cons (NULL, build_string (6, "memory"), NULL));
5623 ASM_VOLATILE_P (x) = 1;
5624 expand_asm_expr (x);
5627 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5631 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5633 enum insn_code icode;
5635 rtx val = const0_rtx;
5637 /* Expand the operands. */
5638 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5640 /* If there is an explicit operation in the md file, use it. */
5641 icode = sync_lock_release[mode];
5642 if (icode != CODE_FOR_nothing)
5644 if (!insn_data[icode].operand[1].predicate (val, mode))
5645 val = force_reg (mode, val);
5647 insn = GEN_FCN (icode) (mem, val);
5655 /* Otherwise we can implement this operation by emitting a barrier
5656 followed by a store of zero. */
5657 expand_builtin_synchronize ();
5658 emit_move_insn (mem, val);
5661 /* Expand an expression EXP that calls a built-in function,
5662 with result going to TARGET if that's convenient
5663 (and in mode MODE if that's convenient).
5664 SUBTARGET may be used as the target for computing one of EXP's operands.
5665 IGNORE is nonzero if the value is to be ignored. */
5668 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5671 tree fndecl = get_callee_fndecl (exp);
5672 tree arglist = TREE_OPERAND (exp, 1);
5673 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5674 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5676 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5677 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5679 /* When not optimizing, generate calls to library functions for a certain
5682 && !called_as_built_in (fndecl)
5683 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5684 && fcode != BUILT_IN_ALLOCA)
5685 return expand_call (exp, target, ignore);
5687 /* The built-in function expanders test for target == const0_rtx
5688 to determine whether the function's result will be ignored. */
5690 target = const0_rtx;
5692 /* If the result of a pure or const built-in function is ignored, and
5693 none of its arguments are volatile, we can avoid expanding the
5694 built-in call and just evaluate the arguments for side-effects. */
5695 if (target == const0_rtx
5696 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5698 bool volatilep = false;
5701 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5702 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5710 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5711 expand_expr (TREE_VALUE (arg), const0_rtx,
5712 VOIDmode, EXPAND_NORMAL);
5719 CASE_FLT_FN (BUILT_IN_FABS):
5720 target = expand_builtin_fabs (arglist, target, subtarget);
5725 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5726 target = expand_builtin_copysign (arglist, target, subtarget);
5731 /* Just do a normal library call if we were unable to fold
5733 CASE_FLT_FN (BUILT_IN_CABS):
5736 CASE_FLT_FN (BUILT_IN_EXP):
5737 CASE_FLT_FN (BUILT_IN_EXP10):
5738 CASE_FLT_FN (BUILT_IN_POW10):
5739 CASE_FLT_FN (BUILT_IN_EXP2):
5740 CASE_FLT_FN (BUILT_IN_EXPM1):
5741 CASE_FLT_FN (BUILT_IN_LOGB):
5742 CASE_FLT_FN (BUILT_IN_ILOGB):
5743 CASE_FLT_FN (BUILT_IN_LOG):
5744 CASE_FLT_FN (BUILT_IN_LOG10):
5745 CASE_FLT_FN (BUILT_IN_LOG2):
5746 CASE_FLT_FN (BUILT_IN_LOG1P):
5747 CASE_FLT_FN (BUILT_IN_TAN):
5748 CASE_FLT_FN (BUILT_IN_ASIN):
5749 CASE_FLT_FN (BUILT_IN_ACOS):
5750 CASE_FLT_FN (BUILT_IN_ATAN):
5751 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5752 because of possible accuracy problems. */
5753 if (! flag_unsafe_math_optimizations)
5755 CASE_FLT_FN (BUILT_IN_SQRT):
5756 CASE_FLT_FN (BUILT_IN_FLOOR):
5757 CASE_FLT_FN (BUILT_IN_CEIL):
5758 CASE_FLT_FN (BUILT_IN_TRUNC):
5759 CASE_FLT_FN (BUILT_IN_ROUND):
5760 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5761 CASE_FLT_FN (BUILT_IN_RINT):
5762 CASE_FLT_FN (BUILT_IN_LRINT):
5763 CASE_FLT_FN (BUILT_IN_LLRINT):
5764 target = expand_builtin_mathfn (exp, target, subtarget);
5769 CASE_FLT_FN (BUILT_IN_LCEIL):
5770 CASE_FLT_FN (BUILT_IN_LLCEIL):
5771 CASE_FLT_FN (BUILT_IN_LFLOOR):
5772 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5773 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5778 CASE_FLT_FN (BUILT_IN_POW):
5779 target = expand_builtin_pow (exp, target, subtarget);
5784 CASE_FLT_FN (BUILT_IN_POWI):
5785 target = expand_builtin_powi (exp, target, subtarget);
5790 CASE_FLT_FN (BUILT_IN_ATAN2):
5791 CASE_FLT_FN (BUILT_IN_LDEXP):
5792 CASE_FLT_FN (BUILT_IN_FMOD):
5793 CASE_FLT_FN (BUILT_IN_DREM):
5794 if (! flag_unsafe_math_optimizations)
5796 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5801 CASE_FLT_FN (BUILT_IN_SIN):
5802 CASE_FLT_FN (BUILT_IN_COS):
5803 if (! flag_unsafe_math_optimizations)
5805 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5810 CASE_FLT_FN (BUILT_IN_SINCOS):
5811 if (! flag_unsafe_math_optimizations)
5813 target = expand_builtin_sincos (exp);
5818 case BUILT_IN_APPLY_ARGS:
5819 return expand_builtin_apply_args ();
5821 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5822 FUNCTION with a copy of the parameters described by
5823 ARGUMENTS, and ARGSIZE. It returns a block of memory
5824 allocated on the stack into which is stored all the registers
5825 that might possibly be used for returning the result of a
5826 function. ARGUMENTS is the value returned by
5827 __builtin_apply_args. ARGSIZE is the number of bytes of
5828 arguments that must be copied. ??? How should this value be
5829 computed? We'll also need a safe worst case value for varargs
5831 case BUILT_IN_APPLY:
5832 if (!validate_arglist (arglist, POINTER_TYPE,
5833 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5834 && !validate_arglist (arglist, REFERENCE_TYPE,
5835 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5843 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5844 ops[i] = expand_normal (TREE_VALUE (t));
5846 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5849 /* __builtin_return (RESULT) causes the function to return the
5850 value described by RESULT. RESULT is address of the block of
5851 memory returned by __builtin_apply. */
5852 case BUILT_IN_RETURN:
5853 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5854 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5857 case BUILT_IN_SAVEREGS:
5858 return expand_builtin_saveregs ();
5860 case BUILT_IN_ARGS_INFO:
5861 return expand_builtin_args_info (arglist);
5863 /* Return the address of the first anonymous stack arg. */
5864 case BUILT_IN_NEXT_ARG:
5865 if (fold_builtin_next_arg (arglist))
5867 return expand_builtin_next_arg ();
5869 case BUILT_IN_CLASSIFY_TYPE:
5870 return expand_builtin_classify_type (arglist);
5872 case BUILT_IN_CONSTANT_P:
5875 case BUILT_IN_FRAME_ADDRESS:
5876 case BUILT_IN_RETURN_ADDRESS:
5877 return expand_builtin_frame_address (fndecl, arglist);
5879 /* Returns the address of the area where the structure is returned.
5881 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5883 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5884 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5887 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5889 case BUILT_IN_ALLOCA:
5890 target = expand_builtin_alloca (arglist, target);
5895 case BUILT_IN_STACK_SAVE:
5896 return expand_stack_save ();
5898 case BUILT_IN_STACK_RESTORE:
5899 expand_stack_restore (TREE_VALUE (arglist));
5902 case BUILT_IN_BSWAP32:
5903 case BUILT_IN_BSWAP64:
5904 target = expand_builtin_bswap (arglist, target, subtarget);
5910 CASE_INT_FN (BUILT_IN_FFS):
5911 case BUILT_IN_FFSIMAX:
5912 target = expand_builtin_unop (target_mode, arglist, target,
5913 subtarget, ffs_optab);
5918 CASE_INT_FN (BUILT_IN_CLZ):
5919 case BUILT_IN_CLZIMAX:
5920 target = expand_builtin_unop (target_mode, arglist, target,
5921 subtarget, clz_optab);
5926 CASE_INT_FN (BUILT_IN_CTZ):
5927 case BUILT_IN_CTZIMAX:
5928 target = expand_builtin_unop (target_mode, arglist, target,
5929 subtarget, ctz_optab);
5934 CASE_INT_FN (BUILT_IN_POPCOUNT):
5935 case BUILT_IN_POPCOUNTIMAX:
5936 target = expand_builtin_unop (target_mode, arglist, target,
5937 subtarget, popcount_optab);
5942 CASE_INT_FN (BUILT_IN_PARITY):
5943 case BUILT_IN_PARITYIMAX:
5944 target = expand_builtin_unop (target_mode, arglist, target,
5945 subtarget, parity_optab);
5950 case BUILT_IN_STRLEN:
5951 target = expand_builtin_strlen (arglist, target, target_mode);
5956 case BUILT_IN_STRCPY:
5957 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5962 case BUILT_IN_STRNCPY:
5963 target = expand_builtin_strncpy (exp, target, mode);
5968 case BUILT_IN_STPCPY:
5969 target = expand_builtin_stpcpy (exp, target, mode);
5974 case BUILT_IN_STRCAT:
5975 target = expand_builtin_strcat (fndecl, arglist, target, mode);
5980 case BUILT_IN_STRNCAT:
5981 target = expand_builtin_strncat (arglist, target, mode);
5986 case BUILT_IN_STRSPN:
5987 target = expand_builtin_strspn (arglist, target, mode);
5992 case BUILT_IN_STRCSPN:
5993 target = expand_builtin_strcspn (arglist, target, mode);
5998 case BUILT_IN_STRSTR:
5999 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6004 case BUILT_IN_STRPBRK:
6005 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6010 case BUILT_IN_INDEX:
6011 case BUILT_IN_STRCHR:
6012 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6017 case BUILT_IN_RINDEX:
6018 case BUILT_IN_STRRCHR:
6019 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6024 case BUILT_IN_MEMCPY:
6025 target = expand_builtin_memcpy (exp, target, mode);
6030 case BUILT_IN_MEMPCPY:
6031 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6036 case BUILT_IN_MEMMOVE:
6037 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6043 case BUILT_IN_BCOPY:
6044 target = expand_builtin_bcopy (exp);
6049 case BUILT_IN_MEMSET:
6050 target = expand_builtin_memset (arglist, target, mode, exp);
6055 case BUILT_IN_BZERO:
6056 target = expand_builtin_bzero (exp);
6061 case BUILT_IN_STRCMP:
6062 target = expand_builtin_strcmp (exp, target, mode);
6067 case BUILT_IN_STRNCMP:
6068 target = expand_builtin_strncmp (exp, target, mode);
6074 case BUILT_IN_MEMCMP:
6075 target = expand_builtin_memcmp (exp, arglist, target, mode);
6080 case BUILT_IN_SETJMP:
6081 /* This should have been lowered to the builtins below. */
6084 case BUILT_IN_SETJMP_SETUP:
6085 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6086 and the receiver label. */
6087 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6089 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6090 VOIDmode, EXPAND_NORMAL);
6091 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6092 rtx label_r = label_rtx (label);
6094 /* This is copied from the handling of non-local gotos. */
6095 expand_builtin_setjmp_setup (buf_addr, label_r);
6096 nonlocal_goto_handler_labels
6097 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6098 nonlocal_goto_handler_labels);
6099 /* ??? Do not let expand_label treat us as such since we would
6100 not want to be both on the list of non-local labels and on
6101 the list of forced labels. */
6102 FORCED_LABEL (label) = 0;
6107 case BUILT_IN_SETJMP_DISPATCHER:
6108 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6109 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6111 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6112 rtx label_r = label_rtx (label);
6114 /* Remove the dispatcher label from the list of non-local labels
6115 since the receiver labels have been added to it above. */
6116 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6121 case BUILT_IN_SETJMP_RECEIVER:
6122 /* __builtin_setjmp_receiver is passed the receiver label. */
6123 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6125 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6126 rtx label_r = label_rtx (label);
6128 expand_builtin_setjmp_receiver (label_r);
6133 /* __builtin_longjmp is passed a pointer to an array of five words.
6134 It's similar to the C library longjmp function but works with
6135 __builtin_setjmp above. */
6136 case BUILT_IN_LONGJMP:
6137 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6139 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6140 VOIDmode, EXPAND_NORMAL);
6141 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6143 if (value != const1_rtx)
6145 error ("%<__builtin_longjmp%> second argument must be 1");
6149 expand_builtin_longjmp (buf_addr, value);
6154 case BUILT_IN_NONLOCAL_GOTO:
6155 target = expand_builtin_nonlocal_goto (arglist);
6160 /* This updates the setjmp buffer that is its argument with the value
6161 of the current stack pointer. */
6162 case BUILT_IN_UPDATE_SETJMP_BUF:
6163 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6166 = expand_normal (TREE_VALUE (arglist));
6168 expand_builtin_update_setjmp_buf (buf_addr);
6174 expand_builtin_trap ();
6177 case BUILT_IN_PRINTF:
6178 target = expand_builtin_printf (exp, target, mode, false);
6183 case BUILT_IN_PRINTF_UNLOCKED:
6184 target = expand_builtin_printf (exp, target, mode, true);
6189 case BUILT_IN_FPUTS:
6190 target = expand_builtin_fputs (arglist, target, false);
6194 case BUILT_IN_FPUTS_UNLOCKED:
6195 target = expand_builtin_fputs (arglist, target, true);
6200 case BUILT_IN_FPRINTF:
6201 target = expand_builtin_fprintf (exp, target, mode, false);
6206 case BUILT_IN_FPRINTF_UNLOCKED:
6207 target = expand_builtin_fprintf (exp, target, mode, true);
6212 case BUILT_IN_SPRINTF:
6213 target = expand_builtin_sprintf (arglist, target, mode);
6218 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6219 target = expand_builtin_signbit (exp, target);
6224 /* Various hooks for the DWARF 2 __throw routine. */
6225 case BUILT_IN_UNWIND_INIT:
6226 expand_builtin_unwind_init ();
6228 case BUILT_IN_DWARF_CFA:
6229 return virtual_cfa_rtx;
6230 #ifdef DWARF2_UNWIND_INFO
6231 case BUILT_IN_DWARF_SP_COLUMN:
6232 return expand_builtin_dwarf_sp_column ();
6233 case BUILT_IN_INIT_DWARF_REG_SIZES:
6234 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6237 case BUILT_IN_FROB_RETURN_ADDR:
6238 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6239 case BUILT_IN_EXTRACT_RETURN_ADDR:
6240 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6241 case BUILT_IN_EH_RETURN:
6242 expand_builtin_eh_return (TREE_VALUE (arglist),
6243 TREE_VALUE (TREE_CHAIN (arglist)));
6245 #ifdef EH_RETURN_DATA_REGNO
6246 case BUILT_IN_EH_RETURN_DATA_REGNO:
6247 return expand_builtin_eh_return_data_regno (arglist);
6249 case BUILT_IN_EXTEND_POINTER:
6250 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6252 case BUILT_IN_VA_START:
6253 case BUILT_IN_STDARG_START:
6254 return expand_builtin_va_start (arglist);
6255 case BUILT_IN_VA_END:
6256 return expand_builtin_va_end (arglist);
6257 case BUILT_IN_VA_COPY:
6258 return expand_builtin_va_copy (arglist);
6259 case BUILT_IN_EXPECT:
6260 return expand_builtin_expect (arglist, target);
6261 case BUILT_IN_PREFETCH:
6262 expand_builtin_prefetch (arglist);
6265 case BUILT_IN_PROFILE_FUNC_ENTER:
6266 return expand_builtin_profile_func (false);
6267 case BUILT_IN_PROFILE_FUNC_EXIT:
6268 return expand_builtin_profile_func (true);
6270 case BUILT_IN_INIT_TRAMPOLINE:
6271 return expand_builtin_init_trampoline (arglist);
6272 case BUILT_IN_ADJUST_TRAMPOLINE:
6273 return expand_builtin_adjust_trampoline (arglist);
6276 case BUILT_IN_EXECL:
6277 case BUILT_IN_EXECV:
6278 case BUILT_IN_EXECLP:
6279 case BUILT_IN_EXECLE:
6280 case BUILT_IN_EXECVP:
6281 case BUILT_IN_EXECVE:
6282 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6287 case BUILT_IN_FETCH_AND_ADD_1:
6288 case BUILT_IN_FETCH_AND_ADD_2:
6289 case BUILT_IN_FETCH_AND_ADD_4:
6290 case BUILT_IN_FETCH_AND_ADD_8:
6291 case BUILT_IN_FETCH_AND_ADD_16:
6292 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6293 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6294 false, target, ignore);
6299 case BUILT_IN_FETCH_AND_SUB_1:
6300 case BUILT_IN_FETCH_AND_SUB_2:
6301 case BUILT_IN_FETCH_AND_SUB_4:
6302 case BUILT_IN_FETCH_AND_SUB_8:
6303 case BUILT_IN_FETCH_AND_SUB_16:
6304 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6305 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6306 false, target, ignore);
6311 case BUILT_IN_FETCH_AND_OR_1:
6312 case BUILT_IN_FETCH_AND_OR_2:
6313 case BUILT_IN_FETCH_AND_OR_4:
6314 case BUILT_IN_FETCH_AND_OR_8:
6315 case BUILT_IN_FETCH_AND_OR_16:
6316 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6317 target = expand_builtin_sync_operation (mode, arglist, IOR,
6318 false, target, ignore);
6323 case BUILT_IN_FETCH_AND_AND_1:
6324 case BUILT_IN_FETCH_AND_AND_2:
6325 case BUILT_IN_FETCH_AND_AND_4:
6326 case BUILT_IN_FETCH_AND_AND_8:
6327 case BUILT_IN_FETCH_AND_AND_16:
6328 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6329 target = expand_builtin_sync_operation (mode, arglist, AND,
6330 false, target, ignore);
6335 case BUILT_IN_FETCH_AND_XOR_1:
6336 case BUILT_IN_FETCH_AND_XOR_2:
6337 case BUILT_IN_FETCH_AND_XOR_4:
6338 case BUILT_IN_FETCH_AND_XOR_8:
6339 case BUILT_IN_FETCH_AND_XOR_16:
6340 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6341 target = expand_builtin_sync_operation (mode, arglist, XOR,
6342 false, target, ignore);
6347 case BUILT_IN_FETCH_AND_NAND_1:
6348 case BUILT_IN_FETCH_AND_NAND_2:
6349 case BUILT_IN_FETCH_AND_NAND_4:
6350 case BUILT_IN_FETCH_AND_NAND_8:
6351 case BUILT_IN_FETCH_AND_NAND_16:
6352 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6353 target = expand_builtin_sync_operation (mode, arglist, NOT,
6354 false, target, ignore);
6359 case BUILT_IN_ADD_AND_FETCH_1:
6360 case BUILT_IN_ADD_AND_FETCH_2:
6361 case BUILT_IN_ADD_AND_FETCH_4:
6362 case BUILT_IN_ADD_AND_FETCH_8:
6363 case BUILT_IN_ADD_AND_FETCH_16:
6364 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6365 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6366 true, target, ignore);
6371 case BUILT_IN_SUB_AND_FETCH_1:
6372 case BUILT_IN_SUB_AND_FETCH_2:
6373 case BUILT_IN_SUB_AND_FETCH_4:
6374 case BUILT_IN_SUB_AND_FETCH_8:
6375 case BUILT_IN_SUB_AND_FETCH_16:
6376 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6377 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6378 true, target, ignore);
6383 case BUILT_IN_OR_AND_FETCH_1:
6384 case BUILT_IN_OR_AND_FETCH_2:
6385 case BUILT_IN_OR_AND_FETCH_4:
6386 case BUILT_IN_OR_AND_FETCH_8:
6387 case BUILT_IN_OR_AND_FETCH_16:
6388 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6389 target = expand_builtin_sync_operation (mode, arglist, IOR,
6390 true, target, ignore);
6395 case BUILT_IN_AND_AND_FETCH_1:
6396 case BUILT_IN_AND_AND_FETCH_2:
6397 case BUILT_IN_AND_AND_FETCH_4:
6398 case BUILT_IN_AND_AND_FETCH_8:
6399 case BUILT_IN_AND_AND_FETCH_16:
6400 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6401 target = expand_builtin_sync_operation (mode, arglist, AND,
6402 true, target, ignore);
6407 case BUILT_IN_XOR_AND_FETCH_1:
6408 case BUILT_IN_XOR_AND_FETCH_2:
6409 case BUILT_IN_XOR_AND_FETCH_4:
6410 case BUILT_IN_XOR_AND_FETCH_8:
6411 case BUILT_IN_XOR_AND_FETCH_16:
6412 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6413 target = expand_builtin_sync_operation (mode, arglist, XOR,
6414 true, target, ignore);
6419 case BUILT_IN_NAND_AND_FETCH_1:
6420 case BUILT_IN_NAND_AND_FETCH_2:
6421 case BUILT_IN_NAND_AND_FETCH_4:
6422 case BUILT_IN_NAND_AND_FETCH_8:
6423 case BUILT_IN_NAND_AND_FETCH_16:
6424 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6425 target = expand_builtin_sync_operation (mode, arglist, NOT,
6426 true, target, ignore);
6431 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6432 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6433 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6434 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6435 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6436 if (mode == VOIDmode)
6437 mode = TYPE_MODE (boolean_type_node);
6438 if (!target || !register_operand (target, mode))
6439 target = gen_reg_rtx (mode);
6441 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6442 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6447 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6448 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6449 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6450 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6451 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6453 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6458 case BUILT_IN_LOCK_TEST_AND_SET_1:
6459 case BUILT_IN_LOCK_TEST_AND_SET_2:
6460 case BUILT_IN_LOCK_TEST_AND_SET_4:
6461 case BUILT_IN_LOCK_TEST_AND_SET_8:
6462 case BUILT_IN_LOCK_TEST_AND_SET_16:
6463 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6464 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6469 case BUILT_IN_LOCK_RELEASE_1:
6470 case BUILT_IN_LOCK_RELEASE_2:
6471 case BUILT_IN_LOCK_RELEASE_4:
6472 case BUILT_IN_LOCK_RELEASE_8:
6473 case BUILT_IN_LOCK_RELEASE_16:
6474 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6475 expand_builtin_lock_release (mode, arglist);
6478 case BUILT_IN_SYNCHRONIZE:
6479 expand_builtin_synchronize ();
6482 case BUILT_IN_OBJECT_SIZE:
6483 return expand_builtin_object_size (exp);
6485 case BUILT_IN_MEMCPY_CHK:
6486 case BUILT_IN_MEMPCPY_CHK:
6487 case BUILT_IN_MEMMOVE_CHK:
6488 case BUILT_IN_MEMSET_CHK:
6489 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6494 case BUILT_IN_STRCPY_CHK:
6495 case BUILT_IN_STPCPY_CHK:
6496 case BUILT_IN_STRNCPY_CHK:
6497 case BUILT_IN_STRCAT_CHK:
6498 case BUILT_IN_SNPRINTF_CHK:
6499 case BUILT_IN_VSNPRINTF_CHK:
6500 maybe_emit_chk_warning (exp, fcode);
6503 case BUILT_IN_SPRINTF_CHK:
6504 case BUILT_IN_VSPRINTF_CHK:
6505 maybe_emit_sprintf_chk_warning (exp, fcode);
6508 default: /* just do library call, if unknown builtin */
6512 /* The switch statement above can drop through to cause the function
6513 to be called normally. */
6514 return expand_call (exp, target, ignore);
6517 /* Determine whether a tree node represents a call to a built-in
6518 function. If the tree T is a call to a built-in function with
6519 the right number of arguments of the appropriate types, return
6520 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6521 Otherwise the return value is END_BUILTINS. */
6523 enum built_in_function
6524 builtin_mathfn_code (tree t)
6526 tree fndecl, arglist, parmlist;
6527 tree argtype, parmtype;
6529 if (TREE_CODE (t) != CALL_EXPR
6530 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6531 return END_BUILTINS;
6533 fndecl = get_callee_fndecl (t);
6534 if (fndecl == NULL_TREE
6535 || TREE_CODE (fndecl) != FUNCTION_DECL
6536 || ! DECL_BUILT_IN (fndecl)
6537 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6538 return END_BUILTINS;
6540 arglist = TREE_OPERAND (t, 1);
6541 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6542 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6544 /* If a function doesn't take a variable number of arguments,
6545 the last element in the list will have type `void'. */
6546 parmtype = TREE_VALUE (parmlist);
6547 if (VOID_TYPE_P (parmtype))
6550 return END_BUILTINS;
6551 return DECL_FUNCTION_CODE (fndecl);
6555 return END_BUILTINS;
6557 argtype = TREE_TYPE (TREE_VALUE (arglist));
6559 if (SCALAR_FLOAT_TYPE_P (parmtype))
6561 if (! SCALAR_FLOAT_TYPE_P (argtype))
6562 return END_BUILTINS;
6564 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6566 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6567 return END_BUILTINS;
6569 else if (POINTER_TYPE_P (parmtype))
6571 if (! POINTER_TYPE_P (argtype))
6572 return END_BUILTINS;
6574 else if (INTEGRAL_TYPE_P (parmtype))
6576 if (! INTEGRAL_TYPE_P (argtype))
6577 return END_BUILTINS;
6580 return END_BUILTINS;
6582 arglist = TREE_CHAIN (arglist);
6585 /* Variable-length argument list. */
6586 return DECL_FUNCTION_CODE (fndecl);
6589 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6590 constant. ARGLIST is the argument list of the call. */
6593 fold_builtin_constant_p (tree arglist)
6598 arglist = TREE_VALUE (arglist);
6600 /* We return 1 for a numeric type that's known to be a constant
6601 value at compile-time or for an aggregate type that's a
6602 literal constant. */
6603 STRIP_NOPS (arglist);
6605 /* If we know this is a constant, emit the constant of one. */
6606 if (CONSTANT_CLASS_P (arglist)
6607 || (TREE_CODE (arglist) == CONSTRUCTOR
6608 && TREE_CONSTANT (arglist)))
6609 return integer_one_node;
6610 if (TREE_CODE (arglist) == ADDR_EXPR)
6612 tree op = TREE_OPERAND (arglist, 0);
6613 if (TREE_CODE (op) == STRING_CST
6614 || (TREE_CODE (op) == ARRAY_REF
6615 && integer_zerop (TREE_OPERAND (op, 1))
6616 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6617 return integer_one_node;
6620 /* If this expression has side effects, show we don't know it to be a
6621 constant. Likewise if it's a pointer or aggregate type since in
6622 those case we only want literals, since those are only optimized
6623 when generating RTL, not later.
6624 And finally, if we are compiling an initializer, not code, we
6625 need to return a definite result now; there's not going to be any
6626 more optimization done. */
6627 if (TREE_SIDE_EFFECTS (arglist)
6628 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6629 || POINTER_TYPE_P (TREE_TYPE (arglist))
6631 || folding_initializer)
6632 return integer_zero_node;
6637 /* Fold a call to __builtin_expect, if we expect that a comparison against
6638 the argument will fold to a constant. In practice, this means a true
6639 constant or the address of a non-weak symbol. ARGLIST is the argument
6640 list of the call. */
6643 fold_builtin_expect (tree arglist)
6650 arg = TREE_VALUE (arglist);
6652 /* If the argument isn't invariant, then there's nothing we can do. */
6653 if (!TREE_INVARIANT (arg))
6656 /* If we're looking at an address of a weak decl, then do not fold. */
6659 if (TREE_CODE (inner) == ADDR_EXPR)
6663 inner = TREE_OPERAND (inner, 0);
6665 while (TREE_CODE (inner) == COMPONENT_REF
6666 || TREE_CODE (inner) == ARRAY_REF);
6667 if (DECL_P (inner) && DECL_WEAK (inner))
6671 /* Otherwise, ARG already has the proper type for the return value. */
6675 /* Fold a call to __builtin_classify_type. */
6678 fold_builtin_classify_type (tree arglist)
6681 return build_int_cst (NULL_TREE, no_type_class);
6683 return build_int_cst (NULL_TREE,
6684 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6687 /* Fold a call to __builtin_strlen. */
6690 fold_builtin_strlen (tree arglist)
6692 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6696 tree len = c_strlen (TREE_VALUE (arglist), 0);
6700 /* Convert from the internal "sizetype" type to "size_t". */
6702 len = fold_convert (size_type_node, len);
6710 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6713 fold_builtin_inf (tree type, int warn)
6715 REAL_VALUE_TYPE real;
6717 /* __builtin_inff is intended to be usable to define INFINITY on all
6718 targets. If an infinity is not available, INFINITY expands "to a
6719 positive constant of type float that overflows at translation
6720 time", footnote "In this case, using INFINITY will violate the
6721 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6722 Thus we pedwarn to ensure this constraint violation is
6724 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6725 pedwarn ("target format does not support infinity");
6728 return build_real (type, real);
6731 /* Fold a call to __builtin_nan or __builtin_nans. */
6734 fold_builtin_nan (tree arglist, tree type, int quiet)
6736 REAL_VALUE_TYPE real;
6739 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6741 str = c_getstr (TREE_VALUE (arglist));
6745 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6748 return build_real (type, real);
6751 /* Return true if the floating point expression T has an integer value.
6752 We also allow +Inf, -Inf and NaN to be considered integer values. */
6755 integer_valued_real_p (tree t)
6757 switch (TREE_CODE (t))
6764 case NON_LVALUE_EXPR:
6765 return integer_valued_real_p (TREE_OPERAND (t, 0));
6770 return integer_valued_real_p (TREE_OPERAND (t, 1));
6777 return integer_valued_real_p (TREE_OPERAND (t, 0))
6778 && integer_valued_real_p (TREE_OPERAND (t, 1));
6781 return integer_valued_real_p (TREE_OPERAND (t, 1))
6782 && integer_valued_real_p (TREE_OPERAND (t, 2));
6785 if (! TREE_CONSTANT_OVERFLOW (t))
6787 REAL_VALUE_TYPE c, cint;
6789 c = TREE_REAL_CST (t);
6790 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6791 return real_identical (&c, &cint);
6797 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6798 if (TREE_CODE (type) == INTEGER_TYPE)
6800 if (TREE_CODE (type) == REAL_TYPE)
6801 return integer_valued_real_p (TREE_OPERAND (t, 0));
6806 switch (builtin_mathfn_code (t))
6808 CASE_FLT_FN (BUILT_IN_CEIL):
6809 CASE_FLT_FN (BUILT_IN_FLOOR):
6810 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6811 CASE_FLT_FN (BUILT_IN_RINT):
6812 CASE_FLT_FN (BUILT_IN_ROUND):
6813 CASE_FLT_FN (BUILT_IN_TRUNC):
6827 /* EXP is assumed to be builtin call where truncation can be propagated
6828 across (for instance floor((double)f) == (double)floorf (f).
6829 Do the transformation. */
6832 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6834 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6837 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6840 arg = TREE_VALUE (arglist);
6841 /* Integer rounding functions are idempotent. */
6842 if (fcode == builtin_mathfn_code (arg))
6845 /* If argument is already integer valued, and we don't need to worry
6846 about setting errno, there's no need to perform rounding. */
6847 if (! flag_errno_math && integer_valued_real_p (arg))
6852 tree arg0 = strip_float_extensions (arg);
6853 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6854 tree newtype = TREE_TYPE (arg0);
6857 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6858 && (decl = mathfn_built_in (newtype, fcode)))
6861 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6862 return fold_convert (ftype,
6863 build_function_call_expr (decl, arglist));
6869 /* EXP is assumed to be builtin call which can narrow the FP type of
6870 the argument, for instance lround((double)f) -> lroundf (f). */
6873 fold_fixed_mathfn (tree fndecl, tree arglist)
6875 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6878 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6881 arg = TREE_VALUE (arglist);
6883 /* If argument is already integer valued, and we don't need to worry
6884 about setting errno, there's no need to perform rounding. */
6885 if (! flag_errno_math && integer_valued_real_p (arg))
6886 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6890 tree ftype = TREE_TYPE (arg);
6891 tree arg0 = strip_float_extensions (arg);
6892 tree newtype = TREE_TYPE (arg0);
6895 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6896 && (decl = mathfn_built_in (newtype, fcode)))
6899 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6900 return build_function_call_expr (decl, arglist);
6904 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6905 sizeof (long long) == sizeof (long). */
6906 if (TYPE_PRECISION (long_long_integer_type_node)
6907 == TYPE_PRECISION (long_integer_type_node))
6909 tree newfn = NULL_TREE;
6912 CASE_FLT_FN (BUILT_IN_LLCEIL):
6913 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6916 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6917 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6920 CASE_FLT_FN (BUILT_IN_LLROUND):
6921 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6924 CASE_FLT_FN (BUILT_IN_LLRINT):
6925 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6934 tree newcall = build_function_call_expr (newfn, arglist);
6935 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6942 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6943 is the argument list, TYPE is the return type and FNDECL is the
6944 original function DECL. Return NULL_TREE if no if no simplification
6948 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6952 if (!arglist || TREE_CHAIN (arglist))
6955 arg = TREE_VALUE (arglist);
6956 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6957 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6960 /* Evaluate cabs of a constant at compile-time. */
6961 if (flag_unsafe_math_optimizations
6962 && TREE_CODE (arg) == COMPLEX_CST
6963 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6964 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6965 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6966 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6968 REAL_VALUE_TYPE r, i;
6970 r = TREE_REAL_CST (TREE_REALPART (arg));
6971 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6973 real_arithmetic (&r, MULT_EXPR, &r, &r);
6974 real_arithmetic (&i, MULT_EXPR, &i, &i);
6975 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6976 if (real_sqrt (&r, TYPE_MODE (type), &r)
6977 || ! flag_trapping_math)
6978 return build_real (type, r);
6981 /* If either part is zero, cabs is fabs of the other. */
6982 if (TREE_CODE (arg) == COMPLEX_EXPR
6983 && real_zerop (TREE_OPERAND (arg, 0)))
6984 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
6985 if (TREE_CODE (arg) == COMPLEX_EXPR
6986 && real_zerop (TREE_OPERAND (arg, 1)))
6987 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
6989 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6990 if (TREE_CODE (arg) == NEGATE_EXPR
6991 || TREE_CODE (arg) == CONJ_EXPR)
6993 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
6994 return build_function_call_expr (fndecl, arglist);
6997 /* Don't do this when optimizing for size. */
6998 if (flag_unsafe_math_optimizations
6999 && optimize && !optimize_size)
7001 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7003 if (sqrtfn != NULL_TREE)
7005 tree rpart, ipart, result, arglist;
7007 arg = builtin_save_expr (arg);
7009 rpart = fold_build1 (REALPART_EXPR, type, arg);
7010 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7012 rpart = builtin_save_expr (rpart);
7013 ipart = builtin_save_expr (ipart);
7015 result = fold_build2 (PLUS_EXPR, type,
7016 fold_build2 (MULT_EXPR, type,
7018 fold_build2 (MULT_EXPR, type,
7021 arglist = build_tree_list (NULL_TREE, result);
7022 return build_function_call_expr (sqrtfn, arglist);
7029 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7030 NULL_TREE if no simplification can be made. */
7033 fold_builtin_sqrt (tree arglist, tree type)
7036 enum built_in_function fcode;
7037 tree arg = TREE_VALUE (arglist);
7039 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7042 /* Optimize sqrt of constant value. */
7043 if (TREE_CODE (arg) == REAL_CST
7044 && ! TREE_CONSTANT_OVERFLOW (arg))
7046 REAL_VALUE_TYPE r, x;
7048 x = TREE_REAL_CST (arg);
7049 if (real_sqrt (&r, TYPE_MODE (type), &x)
7050 || (!flag_trapping_math && !flag_errno_math))
7051 return build_real (type, r);
7054 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7055 fcode = builtin_mathfn_code (arg);
7056 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7058 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7059 arg = fold_build2 (MULT_EXPR, type,
7060 TREE_VALUE (TREE_OPERAND (arg, 1)),
7061 build_real (type, dconsthalf));
7062 arglist = build_tree_list (NULL_TREE, arg);
7063 return build_function_call_expr (expfn, arglist);
7066 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7067 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7069 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7073 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7075 /* The inner root was either sqrt or cbrt. */
7076 REAL_VALUE_TYPE dconstroot =
7077 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7079 /* Adjust for the outer root. */
7080 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7081 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7082 tree_root = build_real (type, dconstroot);
7083 arglist = tree_cons (NULL_TREE, arg0,
7084 build_tree_list (NULL_TREE, tree_root));
7085 return build_function_call_expr (powfn, arglist);
7089 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7090 if (flag_unsafe_math_optimizations
7091 && (fcode == BUILT_IN_POW
7092 || fcode == BUILT_IN_POWF
7093 || fcode == BUILT_IN_POWL))
7095 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7096 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7097 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7099 if (!tree_expr_nonnegative_p (arg0))
7100 arg0 = build1 (ABS_EXPR, type, arg0);
7101 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7102 build_real (type, dconsthalf));
7103 arglist = tree_cons (NULL_TREE, arg0,
7104 build_tree_list (NULL_TREE, narg1));
7105 return build_function_call_expr (powfn, arglist);
7111 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7112 NULL_TREE if no simplification can be made. */
7114 fold_builtin_cbrt (tree arglist, tree type)
7116 tree arg = TREE_VALUE (arglist);
7117 const enum built_in_function fcode = builtin_mathfn_code (arg);
7119 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7122 /* Optimize cbrt of constant value. */
7123 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
7126 if (flag_unsafe_math_optimizations)
7128 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7129 if (BUILTIN_EXPONENT_P (fcode))
7131 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7132 const REAL_VALUE_TYPE third_trunc =
7133 real_value_truncate (TYPE_MODE (type), dconstthird);
7134 arg = fold_build2 (MULT_EXPR, type,
7135 TREE_VALUE (TREE_OPERAND (arg, 1)),
7136 build_real (type, third_trunc));
7137 arglist = build_tree_list (NULL_TREE, arg);
7138 return build_function_call_expr (expfn, arglist);
7141 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7142 if (BUILTIN_SQRT_P (fcode))
7144 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7148 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7150 REAL_VALUE_TYPE dconstroot = dconstthird;
7152 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7153 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7154 tree_root = build_real (type, dconstroot);
7155 arglist = tree_cons (NULL_TREE, arg0,
7156 build_tree_list (NULL_TREE, tree_root));
7157 return build_function_call_expr (powfn, arglist);
7161 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7162 if (BUILTIN_CBRT_P (fcode))
7164 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7165 if (tree_expr_nonnegative_p (arg0))
7167 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7172 REAL_VALUE_TYPE dconstroot;
7174 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7175 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7176 tree_root = build_real (type, dconstroot);
7177 arglist = tree_cons (NULL_TREE, arg0,
7178 build_tree_list (NULL_TREE, tree_root));
7179 return build_function_call_expr (powfn, arglist);
7184 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7185 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7186 || fcode == BUILT_IN_POWL)
7188 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7189 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7190 if (tree_expr_nonnegative_p (arg00))
7192 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7193 const REAL_VALUE_TYPE dconstroot
7194 = real_value_truncate (TYPE_MODE (type), dconstthird);
7195 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7196 build_real (type, dconstroot));
7197 arglist = tree_cons (NULL_TREE, arg00,
7198 build_tree_list (NULL_TREE, narg01));
7199 return build_function_call_expr (powfn, arglist);
7206 /* Fold function call to builtin sin, sinf, or sinl. Return
7207 NULL_TREE if no simplification can be made. */
7209 fold_builtin_sin (tree arglist)
7211 tree arg = TREE_VALUE (arglist);
7213 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7216 /* Optimize sin (0.0) = 0.0. */
7217 if (real_zerop (arg))
7223 /* Fold function call to builtin cos, cosf, or cosl. Return
7224 NULL_TREE if no simplification can be made. */
7226 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7228 tree arg = TREE_VALUE (arglist);
7230 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7233 /* Optimize cos (0.0) = 1.0. */
7234 if (real_zerop (arg))
7235 return build_real (type, dconst1);
7237 /* Optimize cos(-x) into cos (x). */
7238 if (TREE_CODE (arg) == NEGATE_EXPR)
7240 tree args = build_tree_list (NULL_TREE,
7241 TREE_OPERAND (arg, 0));
7242 return build_function_call_expr (fndecl, args);
7248 /* Fold function call to builtin tan, tanf, or tanl. Return
7249 NULL_TREE if no simplification can be made. */
7251 fold_builtin_tan (tree arglist)
7253 enum built_in_function fcode;
7254 tree arg = TREE_VALUE (arglist);
7256 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7259 /* Optimize tan(0.0) = 0.0. */
7260 if (real_zerop (arg))
7263 /* Optimize tan(atan(x)) = x. */
7264 fcode = builtin_mathfn_code (arg);
7265 if (flag_unsafe_math_optimizations
7266 && (fcode == BUILT_IN_ATAN
7267 || fcode == BUILT_IN_ATANF
7268 || fcode == BUILT_IN_ATANL))
7269 return TREE_VALUE (TREE_OPERAND (arg, 1));
7274 /* Fold function call to builtin atan, atanf, or atanl. Return
7275 NULL_TREE if no simplification can be made. */
7278 fold_builtin_atan (tree arglist, tree type)
7281 tree arg = TREE_VALUE (arglist);
7283 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7286 /* Optimize atan(0.0) = 0.0. */
7287 if (real_zerop (arg))
7290 /* Optimize atan(1.0) = pi/4. */
7291 if (real_onep (arg))
7293 REAL_VALUE_TYPE cst;
7295 real_convert (&cst, TYPE_MODE (type), &dconstpi);
7296 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
7297 return build_real (type, cst);
7303 /* Fold function call to builtin trunc, truncf or truncl. Return
7304 NULL_TREE if no simplification can be made. */
7307 fold_builtin_trunc (tree fndecl, tree arglist)
7311 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7314 /* Optimize trunc of constant value. */
7315 arg = TREE_VALUE (arglist);
7316 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7318 REAL_VALUE_TYPE r, x;
7319 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7321 x = TREE_REAL_CST (arg);
7322 real_trunc (&r, TYPE_MODE (type), &x);
7323 return build_real (type, r);
7326 return fold_trunc_transparent_mathfn (fndecl, arglist);
7329 /* Fold function call to builtin floor, floorf or floorl. Return
7330 NULL_TREE if no simplification can be made. */
7333 fold_builtin_floor (tree fndecl, tree arglist)
7337 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7340 /* Optimize floor of constant value. */
7341 arg = TREE_VALUE (arglist);
7342 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7346 x = TREE_REAL_CST (arg);
7347 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7349 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7352 real_floor (&r, TYPE_MODE (type), &x);
7353 return build_real (type, r);
7357 return fold_trunc_transparent_mathfn (fndecl, arglist);
7360 /* Fold function call to builtin ceil, ceilf or ceill. Return
7361 NULL_TREE if no simplification can be made. */
7364 fold_builtin_ceil (tree fndecl, tree arglist)
7368 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7371 /* Optimize ceil of constant value. */
7372 arg = TREE_VALUE (arglist);
7373 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7377 x = TREE_REAL_CST (arg);
7378 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7380 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7383 real_ceil (&r, TYPE_MODE (type), &x);
7384 return build_real (type, r);
7388 return fold_trunc_transparent_mathfn (fndecl, arglist);
7391 /* Fold function call to builtin round, roundf or roundl. Return
7392 NULL_TREE if no simplification can be made. */
7395 fold_builtin_round (tree fndecl, tree arglist)
7399 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7402 /* Optimize round of constant value. */
7403 arg = TREE_VALUE (arglist);
7404 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7408 x = TREE_REAL_CST (arg);
7409 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7411 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7414 real_round (&r, TYPE_MODE (type), &x);
7415 return build_real (type, r);
7419 return fold_trunc_transparent_mathfn (fndecl, arglist);
7422 /* Fold function call to builtin lround, lroundf or lroundl (or the
7423 corresponding long long versions) and other rounding functions.
7424 Return NULL_TREE if no simplification can be made. */
7427 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7431 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7434 /* Optimize lround of constant value. */
7435 arg = TREE_VALUE (arglist);
7436 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7438 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7440 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7442 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7443 tree ftype = TREE_TYPE (arg), result;
7444 HOST_WIDE_INT hi, lo;
7447 switch (DECL_FUNCTION_CODE (fndecl))
7449 CASE_FLT_FN (BUILT_IN_LFLOOR):
7450 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7451 real_floor (&r, TYPE_MODE (ftype), &x);
7454 CASE_FLT_FN (BUILT_IN_LCEIL):
7455 CASE_FLT_FN (BUILT_IN_LLCEIL):
7456 real_ceil (&r, TYPE_MODE (ftype), &x);
7459 CASE_FLT_FN (BUILT_IN_LROUND):
7460 CASE_FLT_FN (BUILT_IN_LLROUND):
7461 real_round (&r, TYPE_MODE (ftype), &x);
7468 REAL_VALUE_TO_INT (&lo, &hi, r);
7469 result = build_int_cst_wide (NULL_TREE, lo, hi);
7470 if (int_fits_type_p (result, itype))
7471 return fold_convert (itype, result);
7475 return fold_fixed_mathfn (fndecl, arglist);
7478 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7479 and their long and long long variants (i.e. ffsl and ffsll).
7480 Return NULL_TREE if no simplification can be made. */
7483 fold_builtin_bitop (tree fndecl, tree arglist)
7487 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7490 /* Optimize for constant argument. */
7491 arg = TREE_VALUE (arglist);
7492 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7494 HOST_WIDE_INT hi, width, result;
7495 unsigned HOST_WIDE_INT lo;
7498 type = TREE_TYPE (arg);
7499 width = TYPE_PRECISION (type);
7500 lo = TREE_INT_CST_LOW (arg);
7502 /* Clear all the bits that are beyond the type's precision. */
7503 if (width > HOST_BITS_PER_WIDE_INT)
7505 hi = TREE_INT_CST_HIGH (arg);
7506 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7507 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7512 if (width < HOST_BITS_PER_WIDE_INT)
7513 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7516 switch (DECL_FUNCTION_CODE (fndecl))
7518 CASE_INT_FN (BUILT_IN_FFS):
7520 result = exact_log2 (lo & -lo) + 1;
7522 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7527 CASE_INT_FN (BUILT_IN_CLZ):
7529 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7531 result = width - floor_log2 (lo) - 1;
7532 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7536 CASE_INT_FN (BUILT_IN_CTZ):
7538 result = exact_log2 (lo & -lo);
7540 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7541 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7545 CASE_INT_FN (BUILT_IN_POPCOUNT):
7548 result++, lo &= lo - 1;
7550 result++, hi &= hi - 1;
7553 CASE_INT_FN (BUILT_IN_PARITY):
7556 result++, lo &= lo - 1;
7558 result++, hi &= hi - 1;
7566 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7572 /* Fold function call to builtin_bswap and the long and long long
7573 variants. Return NULL_TREE if no simplification can be made. */
7575 fold_builtin_bswap (tree fndecl, tree arglist)
7579 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7582 /* Optimize constant value. */
7583 arg = TREE_VALUE (arglist);
7584 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7586 HOST_WIDE_INT hi, width, r_hi = 0;
7587 unsigned HOST_WIDE_INT lo, r_lo = 0;
7590 type = TREE_TYPE (arg);
7591 width = TYPE_PRECISION (type);
7592 lo = TREE_INT_CST_LOW (arg);
7593 hi = TREE_INT_CST_HIGH (arg);
7595 switch (DECL_FUNCTION_CODE (fndecl))
7597 case BUILT_IN_BSWAP32:
7598 case BUILT_IN_BSWAP64:
7602 for (s = 0; s < width; s += 8)
7604 int d = width - s - 8;
7605 unsigned HOST_WIDE_INT byte;
7607 if (s < HOST_BITS_PER_WIDE_INT)
7608 byte = (lo >> s) & 0xff;
7610 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7612 if (d < HOST_BITS_PER_WIDE_INT)
7615 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7625 if (width < HOST_BITS_PER_WIDE_INT)
7626 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7628 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7633 /* Return true if EXPR is the real constant contained in VALUE. */
7636 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7640 return ((TREE_CODE (expr) == REAL_CST
7641 && ! TREE_CONSTANT_OVERFLOW (expr)
7642 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7643 || (TREE_CODE (expr) == COMPLEX_CST
7644 && real_dconstp (TREE_REALPART (expr), value)
7645 && real_zerop (TREE_IMAGPART (expr))));
7648 /* A subroutine of fold_builtin to fold the various logarithmic
7649 functions. EXP is the CALL_EXPR of a call to a builtin logN
7650 function. VALUE is the base of the logN function. */
7653 fold_builtin_logarithm (tree fndecl, tree arglist,
7654 const REAL_VALUE_TYPE *value)
7656 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7658 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7659 tree arg = TREE_VALUE (arglist);
7660 const enum built_in_function fcode = builtin_mathfn_code (arg);
7662 /* Optimize logN(1.0) = 0.0. */
7663 if (real_onep (arg))
7664 return build_real (type, dconst0);
7666 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
7667 exactly, then only do this if flag_unsafe_math_optimizations. */
7668 if (exact_real_truncate (TYPE_MODE (type), value)
7669 || flag_unsafe_math_optimizations)
7671 const REAL_VALUE_TYPE value_truncate =
7672 real_value_truncate (TYPE_MODE (type), *value);
7673 if (real_dconstp (arg, &value_truncate))
7674 return build_real (type, dconst1);
7677 /* Special case, optimize logN(expN(x)) = x. */
7678 if (flag_unsafe_math_optimizations
7679 && ((value == &dconste
7680 && (fcode == BUILT_IN_EXP
7681 || fcode == BUILT_IN_EXPF
7682 || fcode == BUILT_IN_EXPL))
7683 || (value == &dconst2
7684 && (fcode == BUILT_IN_EXP2
7685 || fcode == BUILT_IN_EXP2F
7686 || fcode == BUILT_IN_EXP2L))
7687 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
7688 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7690 /* Optimize logN(func()) for various exponential functions. We
7691 want to determine the value "x" and the power "exponent" in
7692 order to transform logN(x**exponent) into exponent*logN(x). */
7693 if (flag_unsafe_math_optimizations)
7695 tree exponent = 0, x = 0;
7699 CASE_FLT_FN (BUILT_IN_EXP):
7700 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7701 x = build_real (type,
7702 real_value_truncate (TYPE_MODE (type), dconste));
7703 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7705 CASE_FLT_FN (BUILT_IN_EXP2):
7706 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7707 x = build_real (type, dconst2);
7708 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7710 CASE_FLT_FN (BUILT_IN_EXP10):
7711 CASE_FLT_FN (BUILT_IN_POW10):
7712 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7713 x = build_real (type, dconst10);
7714 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7716 CASE_FLT_FN (BUILT_IN_SQRT):
7717 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7718 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7719 exponent = build_real (type, dconsthalf);
7721 CASE_FLT_FN (BUILT_IN_CBRT):
7722 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7723 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7724 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7727 CASE_FLT_FN (BUILT_IN_POW):
7728 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7729 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7730 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7736 /* Now perform the optimization. */
7740 arglist = build_tree_list (NULL_TREE, x);
7741 logfn = build_function_call_expr (fndecl, arglist);
7742 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7750 /* Fold a builtin function call to pow, powf, or powl. Return
7751 NULL_TREE if no simplification can be made. */
7753 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7755 tree arg0 = TREE_VALUE (arglist);
7756 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7758 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7761 /* Optimize pow(1.0,y) = 1.0. */
7762 if (real_onep (arg0))
7763 return omit_one_operand (type, build_real (type, dconst1), arg1);
7765 if (TREE_CODE (arg1) == REAL_CST
7766 && ! TREE_CONSTANT_OVERFLOW (arg1))
7768 REAL_VALUE_TYPE cint;
7772 c = TREE_REAL_CST (arg1);
7774 /* Optimize pow(x,0.0) = 1.0. */
7775 if (REAL_VALUES_EQUAL (c, dconst0))
7776 return omit_one_operand (type, build_real (type, dconst1),
7779 /* Optimize pow(x,1.0) = x. */
7780 if (REAL_VALUES_EQUAL (c, dconst1))
7783 /* Optimize pow(x,-1.0) = 1.0/x. */
7784 if (REAL_VALUES_EQUAL (c, dconstm1))
7785 return fold_build2 (RDIV_EXPR, type,
7786 build_real (type, dconst1), arg0);
7788 /* Optimize pow(x,0.5) = sqrt(x). */
7789 if (flag_unsafe_math_optimizations
7790 && REAL_VALUES_EQUAL (c, dconsthalf))
7792 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7794 if (sqrtfn != NULL_TREE)
7796 tree arglist = build_tree_list (NULL_TREE, arg0);
7797 return build_function_call_expr (sqrtfn, arglist);
7801 /* Check for an integer exponent. */
7802 n = real_to_integer (&c);
7803 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7804 if (real_identical (&c, &cint))
7806 /* Attempt to evaluate pow at compile-time. */
7807 if (TREE_CODE (arg0) == REAL_CST
7808 && ! TREE_CONSTANT_OVERFLOW (arg0))
7813 x = TREE_REAL_CST (arg0);
7814 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7815 if (flag_unsafe_math_optimizations || !inexact)
7816 return build_real (type, x);
7819 /* Strip sign ops from even integer powers. */
7820 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7822 tree narg0 = fold_strip_sign_ops (arg0);
7825 arglist = build_tree_list (NULL_TREE, arg1);
7826 arglist = tree_cons (NULL_TREE, narg0, arglist);
7827 return build_function_call_expr (fndecl, arglist);
7833 if (flag_unsafe_math_optimizations)
7835 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7837 /* Optimize pow(expN(x),y) = expN(x*y). */
7838 if (BUILTIN_EXPONENT_P (fcode))
7840 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7841 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7842 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7843 arglist = build_tree_list (NULL_TREE, arg);
7844 return build_function_call_expr (expfn, arglist);
7847 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7848 if (BUILTIN_SQRT_P (fcode))
7850 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7851 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7852 build_real (type, dconsthalf));
7854 arglist = tree_cons (NULL_TREE, narg0,
7855 build_tree_list (NULL_TREE, narg1));
7856 return build_function_call_expr (fndecl, arglist);
7859 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7860 if (BUILTIN_CBRT_P (fcode))
7862 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7863 if (tree_expr_nonnegative_p (arg))
7865 const REAL_VALUE_TYPE dconstroot
7866 = real_value_truncate (TYPE_MODE (type), dconstthird);
7867 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7868 build_real (type, dconstroot));
7869 arglist = tree_cons (NULL_TREE, arg,
7870 build_tree_list (NULL_TREE, narg1));
7871 return build_function_call_expr (fndecl, arglist);
7875 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7876 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7877 || fcode == BUILT_IN_POWL)
7879 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7880 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7881 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7882 arglist = tree_cons (NULL_TREE, arg00,
7883 build_tree_list (NULL_TREE, narg1));
7884 return build_function_call_expr (fndecl, arglist);
7891 /* Fold a builtin function call to powi, powif, or powil. Return
7892 NULL_TREE if no simplification can be made. */
7894 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7896 tree arg0 = TREE_VALUE (arglist);
7897 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7899 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7902 /* Optimize pow(1.0,y) = 1.0. */
7903 if (real_onep (arg0))
7904 return omit_one_operand (type, build_real (type, dconst1), arg1);
7906 if (host_integerp (arg1, 0))
7908 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7910 /* Evaluate powi at compile-time. */
7911 if (TREE_CODE (arg0) == REAL_CST
7912 && ! TREE_CONSTANT_OVERFLOW (arg0))
7915 x = TREE_REAL_CST (arg0);
7916 real_powi (&x, TYPE_MODE (type), &x, c);
7917 return build_real (type, x);
7920 /* Optimize pow(x,0) = 1.0. */
7922 return omit_one_operand (type, build_real (type, dconst1),
7925 /* Optimize pow(x,1) = x. */
7929 /* Optimize pow(x,-1) = 1.0/x. */
7931 return fold_build2 (RDIV_EXPR, type,
7932 build_real (type, dconst1), arg0);
7938 /* A subroutine of fold_builtin to fold the various exponent
7939 functions. EXP is the CALL_EXPR of a call to a builtin function.
7940 VALUE is the value which will be raised to a power. */
7943 fold_builtin_exponent (tree fndecl, tree arglist,
7944 const REAL_VALUE_TYPE *value)
7946 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7948 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7949 tree arg = TREE_VALUE (arglist);
7951 /* Optimize exp*(0.0) = 1.0. */
7952 if (real_zerop (arg))
7953 return build_real (type, dconst1);
7955 /* Optimize expN(1.0) = N. */
7956 if (real_onep (arg))
7958 REAL_VALUE_TYPE cst;
7960 real_convert (&cst, TYPE_MODE (type), value);
7961 return build_real (type, cst);
7964 /* Attempt to evaluate expN(integer) at compile-time. */
7965 if (flag_unsafe_math_optimizations
7966 && TREE_CODE (arg) == REAL_CST
7967 && ! TREE_CONSTANT_OVERFLOW (arg))
7969 REAL_VALUE_TYPE cint;
7973 c = TREE_REAL_CST (arg);
7974 n = real_to_integer (&c);
7975 real_from_integer (&cint, VOIDmode, n,
7977 if (real_identical (&c, &cint))
7981 real_powi (&x, TYPE_MODE (type), value, n);
7982 return build_real (type, x);
7986 /* Optimize expN(logN(x)) = x. */
7987 if (flag_unsafe_math_optimizations)
7989 const enum built_in_function fcode = builtin_mathfn_code (arg);
7991 if ((value == &dconste
7992 && (fcode == BUILT_IN_LOG
7993 || fcode == BUILT_IN_LOGF
7994 || fcode == BUILT_IN_LOGL))
7995 || (value == &dconst2
7996 && (fcode == BUILT_IN_LOG2
7997 || fcode == BUILT_IN_LOG2F
7998 || fcode == BUILT_IN_LOG2L))
7999 || (value == &dconst10
8000 && (fcode == BUILT_IN_LOG10
8001 || fcode == BUILT_IN_LOG10F
8002 || fcode == BUILT_IN_LOG10L)))
8003 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8010 /* Return true if VAR is a VAR_DECL or a component thereof. */
8013 var_decl_component_p (tree var)
8016 while (handled_component_p (inner))
8017 inner = TREE_OPERAND (inner, 0);
8018 return SSA_VAR_P (inner);
8021 /* Fold function call to builtin memset. Return
8022 NULL_TREE if no simplification can be made. */
8025 fold_builtin_memset (tree arglist, tree type, bool ignore)
8027 tree dest, c, len, var, ret;
8028 unsigned HOST_WIDE_INT length, cval;
8030 if (!validate_arglist (arglist,
8031 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8034 dest = TREE_VALUE (arglist);
8035 c = TREE_VALUE (TREE_CHAIN (arglist));
8036 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8038 if (! host_integerp (len, 1))
8041 /* If the LEN parameter is zero, return DEST. */
8042 if (integer_zerop (len))
8043 return omit_one_operand (type, dest, c);
8045 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8050 if (TREE_CODE (var) != ADDR_EXPR)
8053 var = TREE_OPERAND (var, 0);
8054 if (TREE_THIS_VOLATILE (var))
8057 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8058 && !POINTER_TYPE_P (TREE_TYPE (var)))
8061 if (! var_decl_component_p (var))
8064 length = tree_low_cst (len, 1);
8065 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8066 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8070 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8073 if (integer_zerop (c))
8077 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8080 cval = tree_low_cst (c, 1);
8084 cval |= (cval << 31) << 1;
8087 ret = build_int_cst_type (TREE_TYPE (var), cval);
8088 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
8092 return omit_one_operand (type, dest, ret);
8095 /* Fold function call to builtin memset. Return
8096 NULL_TREE if no simplification can be made. */
8099 fold_builtin_bzero (tree arglist, bool ignore)
8101 tree dest, size, newarglist;
8103 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8109 dest = TREE_VALUE (arglist);
8110 size = TREE_VALUE (TREE_CHAIN (arglist));
8112 /* New argument list transforming bzero(ptr x, int y) to
8113 memset(ptr x, int 0, size_t y). This is done this way
8114 so that if it isn't expanded inline, we fallback to
8115 calling bzero instead of memset. */
8117 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8118 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8119 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8120 return fold_builtin_memset (newarglist, void_type_node, ignore);
8123 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8124 NULL_TREE if no simplification can be made.
8125 If ENDP is 0, return DEST (like memcpy).
8126 If ENDP is 1, return DEST+LEN (like mempcpy).
8127 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8128 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8132 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8134 tree dest, src, len, destvar, srcvar, expr;
8135 unsigned HOST_WIDE_INT length;
8137 if (! validate_arglist (arglist,
8138 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8141 dest = TREE_VALUE (arglist);
8142 src = TREE_VALUE (TREE_CHAIN (arglist));
8143 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8145 /* If the LEN parameter is zero, return DEST. */
8146 if (integer_zerop (len))
8147 return omit_one_operand (type, dest, src);
8149 /* If SRC and DEST are the same (and not volatile), return
8150 DEST{,+LEN,+LEN-1}. */
8151 if (operand_equal_p (src, dest, 0))
8155 if (! host_integerp (len, 1))
8158 if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src))
8162 STRIP_NOPS (destvar);
8163 if (TREE_CODE (destvar) != ADDR_EXPR)
8166 destvar = TREE_OPERAND (destvar, 0);
8167 if (TREE_THIS_VOLATILE (destvar))
8170 if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8171 && !POINTER_TYPE_P (TREE_TYPE (destvar))
8172 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar)))
8175 if (! var_decl_component_p (destvar))
8179 STRIP_NOPS (srcvar);
8180 if (TREE_CODE (srcvar) != ADDR_EXPR)
8183 srcvar = TREE_OPERAND (srcvar, 0);
8184 if (TREE_THIS_VOLATILE (srcvar))
8187 if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8188 && !POINTER_TYPE_P (TREE_TYPE (srcvar))
8189 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar)))
8192 if (! var_decl_component_p (srcvar))
8195 length = tree_low_cst (len, 1);
8196 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length
8197 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8199 || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length
8200 || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8204 if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8205 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8206 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8207 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8208 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8210 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8211 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8217 if (endp == 0 || endp == 3)
8218 return omit_one_operand (type, dest, expr);
8224 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8227 len = fold_convert (TREE_TYPE (dest), len);
8228 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8229 dest = fold_convert (type, dest);
8231 dest = omit_one_operand (type, dest, expr);
8235 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8236 simplification can be made. */
8239 fold_builtin_bcopy (tree arglist, bool ignore)
8241 tree src, dest, size, newarglist;
8243 if (!validate_arglist (arglist,
8244 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8250 src = TREE_VALUE (arglist);
8251 dest = TREE_VALUE (TREE_CHAIN (arglist));
8252 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8254 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8255 memmove(ptr y, ptr x, size_t z). This is done this way
8256 so that if it isn't expanded inline, we fallback to
8257 calling bcopy instead of memmove. */
8259 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8260 newarglist = tree_cons (NULL_TREE, src, newarglist);
8261 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8263 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8266 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8267 the length of the string to be copied. Return NULL_TREE if no
8268 simplification can be made. */
8271 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8275 if (!validate_arglist (arglist,
8276 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8279 dest = TREE_VALUE (arglist);
8280 src = TREE_VALUE (TREE_CHAIN (arglist));
8282 /* If SRC and DEST are the same (and not volatile), return DEST. */
8283 if (operand_equal_p (src, dest, 0))
8284 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8289 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8295 len = c_strlen (src, 1);
8296 if (! len || TREE_SIDE_EFFECTS (len))
8300 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8301 arglist = build_tree_list (NULL_TREE, len);
8302 arglist = tree_cons (NULL_TREE, src, arglist);
8303 arglist = tree_cons (NULL_TREE, dest, arglist);
8304 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8305 build_function_call_expr (fn, arglist));
8308 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8309 the length of the source string. Return NULL_TREE if no simplification
8313 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8315 tree dest, src, len, fn;
8317 if (!validate_arglist (arglist,
8318 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8321 dest = TREE_VALUE (arglist);
8322 src = TREE_VALUE (TREE_CHAIN (arglist));
8323 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8325 /* If the LEN parameter is zero, return DEST. */
8326 if (integer_zerop (len))
8327 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8329 /* We can't compare slen with len as constants below if len is not a
8331 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8335 slen = c_strlen (src, 1);
8337 /* Now, we must be passed a constant src ptr parameter. */
8338 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8341 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8343 /* We do not support simplification of this case, though we do
8344 support it when expanding trees into RTL. */
8345 /* FIXME: generate a call to __builtin_memset. */
8346 if (tree_int_cst_lt (slen, len))
8349 /* OK transform into builtin memcpy. */
8350 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8353 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8354 build_function_call_expr (fn, arglist));
8357 /* Fold function call to builtin memcmp. Return
8358 NULL_TREE if no simplification can be made. */
8361 fold_builtin_memcmp (tree arglist)
8363 tree arg1, arg2, len;
8364 const char *p1, *p2;
8366 if (!validate_arglist (arglist,
8367 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8370 arg1 = TREE_VALUE (arglist);
8371 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8372 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8374 /* If the LEN parameter is zero, return zero. */
8375 if (integer_zerop (len))
8376 return omit_two_operands (integer_type_node, integer_zero_node,
8379 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8380 if (operand_equal_p (arg1, arg2, 0))
8381 return omit_one_operand (integer_type_node, integer_zero_node, len);
8383 p1 = c_getstr (arg1);
8384 p2 = c_getstr (arg2);
8386 /* If all arguments are constant, and the value of len is not greater
8387 than the lengths of arg1 and arg2, evaluate at compile-time. */
8388 if (host_integerp (len, 1) && p1 && p2
8389 && compare_tree_int (len, strlen (p1) + 1) <= 0
8390 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8392 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8395 return integer_one_node;
8397 return integer_minus_one_node;
8399 return integer_zero_node;
8402 /* If len parameter is one, return an expression corresponding to
8403 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8404 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8406 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8407 tree cst_uchar_ptr_node
8408 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8410 tree ind1 = fold_convert (integer_type_node,
8411 build1 (INDIRECT_REF, cst_uchar_node,
8412 fold_convert (cst_uchar_ptr_node,
8414 tree ind2 = fold_convert (integer_type_node,
8415 build1 (INDIRECT_REF, cst_uchar_node,
8416 fold_convert (cst_uchar_ptr_node,
8418 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8424 /* Fold function call to builtin strcmp. Return
8425 NULL_TREE if no simplification can be made. */
8428 fold_builtin_strcmp (tree arglist)
8431 const char *p1, *p2;
8433 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8436 arg1 = TREE_VALUE (arglist);
8437 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8439 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8440 if (operand_equal_p (arg1, arg2, 0))
8441 return integer_zero_node;
8443 p1 = c_getstr (arg1);
8444 p2 = c_getstr (arg2);
8448 const int i = strcmp (p1, p2);
8450 return integer_minus_one_node;
8452 return integer_one_node;
8454 return integer_zero_node;
8457 /* If the second arg is "", return *(const unsigned char*)arg1. */
8458 if (p2 && *p2 == '\0')
8460 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8461 tree cst_uchar_ptr_node
8462 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8464 return fold_convert (integer_type_node,
8465 build1 (INDIRECT_REF, cst_uchar_node,
8466 fold_convert (cst_uchar_ptr_node,
8470 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8471 if (p1 && *p1 == '\0')
8473 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8474 tree cst_uchar_ptr_node
8475 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8477 tree temp = fold_convert (integer_type_node,
8478 build1 (INDIRECT_REF, cst_uchar_node,
8479 fold_convert (cst_uchar_ptr_node,
8481 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8487 /* Fold function call to builtin strncmp. Return
8488 NULL_TREE if no simplification can be made. */
8491 fold_builtin_strncmp (tree arglist)
8493 tree arg1, arg2, len;
8494 const char *p1, *p2;
8496 if (!validate_arglist (arglist,
8497 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8500 arg1 = TREE_VALUE (arglist);
8501 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8502 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8504 /* If the LEN parameter is zero, return zero. */
8505 if (integer_zerop (len))
8506 return omit_two_operands (integer_type_node, integer_zero_node,
8509 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8510 if (operand_equal_p (arg1, arg2, 0))
8511 return omit_one_operand (integer_type_node, integer_zero_node, len);
8513 p1 = c_getstr (arg1);
8514 p2 = c_getstr (arg2);
8516 if (host_integerp (len, 1) && p1 && p2)
8518 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8520 return integer_one_node;
8522 return integer_minus_one_node;
8524 return integer_zero_node;
8527 /* If the second arg is "", and the length is greater than zero,
8528 return *(const unsigned char*)arg1. */
8529 if (p2 && *p2 == '\0'
8530 && TREE_CODE (len) == INTEGER_CST
8531 && tree_int_cst_sgn (len) == 1)
8533 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8534 tree cst_uchar_ptr_node
8535 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8537 return fold_convert (integer_type_node,
8538 build1 (INDIRECT_REF, cst_uchar_node,
8539 fold_convert (cst_uchar_ptr_node,
8543 /* If the first arg is "", and the length is greater than zero,
8544 return -*(const unsigned char*)arg2. */
8545 if (p1 && *p1 == '\0'
8546 && TREE_CODE (len) == INTEGER_CST
8547 && tree_int_cst_sgn (len) == 1)
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);
8560 /* If len parameter is one, return an expression corresponding to
8561 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8562 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8564 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8565 tree cst_uchar_ptr_node
8566 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8568 tree ind1 = fold_convert (integer_type_node,
8569 build1 (INDIRECT_REF, cst_uchar_node,
8570 fold_convert (cst_uchar_ptr_node,
8572 tree ind2 = fold_convert (integer_type_node,
8573 build1 (INDIRECT_REF, cst_uchar_node,
8574 fold_convert (cst_uchar_ptr_node,
8576 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8582 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8583 NULL_TREE if no simplification can be made. */
8586 fold_builtin_signbit (tree fndecl, tree arglist)
8588 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8591 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8594 arg = TREE_VALUE (arglist);
8596 /* If ARG is a compile-time constant, determine the result. */
8597 if (TREE_CODE (arg) == REAL_CST
8598 && !TREE_CONSTANT_OVERFLOW (arg))
8602 c = TREE_REAL_CST (arg);
8603 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8604 return fold_convert (type, temp);
8607 /* If ARG is non-negative, the result is always zero. */
8608 if (tree_expr_nonnegative_p (arg))
8609 return omit_one_operand (type, integer_zero_node, arg);
8611 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8612 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8613 return fold_build2 (LT_EXPR, type, arg,
8614 build_real (TREE_TYPE (arg), dconst0));
8619 /* Fold function call to builtin copysign, copysignf or copysignl.
8620 Return NULL_TREE if no simplification can be made. */
8623 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8625 tree arg1, arg2, tem;
8627 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8630 arg1 = TREE_VALUE (arglist);
8631 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8633 /* copysign(X,X) is X. */
8634 if (operand_equal_p (arg1, arg2, 0))
8635 return fold_convert (type, arg1);
8637 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8638 if (TREE_CODE (arg1) == REAL_CST
8639 && TREE_CODE (arg2) == REAL_CST
8640 && !TREE_CONSTANT_OVERFLOW (arg1)
8641 && !TREE_CONSTANT_OVERFLOW (arg2))
8643 REAL_VALUE_TYPE c1, c2;
8645 c1 = TREE_REAL_CST (arg1);
8646 c2 = TREE_REAL_CST (arg2);
8647 /* c1.sign := c2.sign. */
8648 real_copysign (&c1, &c2);
8649 return build_real (type, c1);
8652 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8653 Remember to evaluate Y for side-effects. */
8654 if (tree_expr_nonnegative_p (arg2))
8655 return omit_one_operand (type,
8656 fold_build1 (ABS_EXPR, type, arg1),
8659 /* Strip sign changing operations for the first argument. */
8660 tem = fold_strip_sign_ops (arg1);
8663 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8664 return build_function_call_expr (fndecl, arglist);
8670 /* Fold a call to builtin isascii. */
8673 fold_builtin_isascii (tree arglist)
8675 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8679 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8680 tree arg = TREE_VALUE (arglist);
8682 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8683 build_int_cst (NULL_TREE,
8684 ~ (unsigned HOST_WIDE_INT) 0x7f));
8685 arg = fold_build2 (EQ_EXPR, integer_type_node,
8686 arg, integer_zero_node);
8688 if (in_gimple_form && !TREE_CONSTANT (arg))
8695 /* Fold a call to builtin toascii. */
8698 fold_builtin_toascii (tree arglist)
8700 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8704 /* Transform toascii(c) -> (c & 0x7f). */
8705 tree arg = TREE_VALUE (arglist);
8707 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8708 build_int_cst (NULL_TREE, 0x7f));
8712 /* Fold a call to builtin isdigit. */
8715 fold_builtin_isdigit (tree arglist)
8717 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8721 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8722 /* According to the C standard, isdigit is unaffected by locale.
8723 However, it definitely is affected by the target character set. */
8725 unsigned HOST_WIDE_INT target_digit0
8726 = lang_hooks.to_target_charset ('0');
8728 if (target_digit0 == 0)
8731 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8732 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8733 build_int_cst (unsigned_type_node, target_digit0));
8734 arg = fold_build2 (LE_EXPR, integer_type_node, arg,
8735 build_int_cst (unsigned_type_node, 9));
8736 if (in_gimple_form && !TREE_CONSTANT (arg))
8743 /* Fold a call to fabs, fabsf or fabsl. */
8746 fold_builtin_fabs (tree arglist, tree type)
8750 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8753 arg = TREE_VALUE (arglist);
8754 arg = fold_convert (type, arg);
8755 if (TREE_CODE (arg) == REAL_CST)
8756 return fold_abs_const (arg, type);
8757 return fold_build1 (ABS_EXPR, type, arg);
8760 /* Fold a call to abs, labs, llabs or imaxabs. */
8763 fold_builtin_abs (tree arglist, tree type)
8767 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8770 arg = TREE_VALUE (arglist);
8771 arg = fold_convert (type, arg);
8772 if (TREE_CODE (arg) == INTEGER_CST)
8773 return fold_abs_const (arg, type);
8774 return fold_build1 (ABS_EXPR, type, arg);
8777 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8778 EXP is the CALL_EXPR for the call. */
8781 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8783 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8787 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8789 /* Check that we have exactly one argument. */
8792 error ("too few arguments to function %qs",
8793 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8794 return error_mark_node;
8796 else if (TREE_CHAIN (arglist) != 0)
8798 error ("too many arguments to function %qs",
8799 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8800 return error_mark_node;
8804 error ("non-floating-point argument to function %qs",
8805 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8806 return error_mark_node;
8810 arg = TREE_VALUE (arglist);
8811 switch (builtin_index)
8813 case BUILT_IN_ISINF:
8814 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8815 return omit_one_operand (type, integer_zero_node, arg);
8817 if (TREE_CODE (arg) == REAL_CST)
8819 r = TREE_REAL_CST (arg);
8820 if (real_isinf (&r))
8821 return real_compare (GT_EXPR, &r, &dconst0)
8822 ? integer_one_node : integer_minus_one_node;
8824 return integer_zero_node;
8829 case BUILT_IN_FINITE:
8830 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
8831 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8832 return omit_one_operand (type, integer_one_node, arg);
8834 if (TREE_CODE (arg) == REAL_CST)
8836 r = TREE_REAL_CST (arg);
8837 return real_isinf (&r) || real_isnan (&r)
8838 ? integer_zero_node : integer_one_node;
8843 case BUILT_IN_ISNAN:
8844 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
8845 return omit_one_operand (type, integer_zero_node, arg);
8847 if (TREE_CODE (arg) == REAL_CST)
8849 r = TREE_REAL_CST (arg);
8850 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8853 arg = builtin_save_expr (arg);
8854 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8861 /* Fold a call to an unordered comparison function such as
8862 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8863 being called and ARGLIST is the argument list for the call.
8864 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8865 the opposite of the desired result. UNORDERED_CODE is used
8866 for modes that can hold NaNs and ORDERED_CODE is used for
8870 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8871 enum tree_code unordered_code,
8872 enum tree_code ordered_code)
8874 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8875 enum tree_code code;
8878 enum tree_code code0, code1;
8879 tree cmp_type = NULL_TREE;
8881 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8883 /* Check that we have exactly two arguments. */
8884 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8886 error ("too few arguments to function %qs",
8887 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8888 return error_mark_node;
8890 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8892 error ("too many arguments to function %qs",
8893 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8894 return error_mark_node;
8898 arg0 = TREE_VALUE (arglist);
8899 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8901 type0 = TREE_TYPE (arg0);
8902 type1 = TREE_TYPE (arg1);
8904 code0 = TREE_CODE (type0);
8905 code1 = TREE_CODE (type1);
8907 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8908 /* Choose the wider of two real types. */
8909 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8911 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8913 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8917 error ("non-floating-point argument to function %qs",
8918 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8919 return error_mark_node;
8922 arg0 = fold_convert (cmp_type, arg0);
8923 arg1 = fold_convert (cmp_type, arg1);
8925 if (unordered_code == UNORDERED_EXPR)
8927 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8928 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8929 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
8932 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8934 return fold_build1 (TRUTH_NOT_EXPR, type,
8935 fold_build2 (code, type, arg0, arg1));
8938 /* Used by constant folding to simplify calls to builtin functions. EXP is
8939 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8940 result of the function call is ignored. This function returns NULL_TREE
8941 if no simplification was possible. */
8944 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
8946 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8947 enum built_in_function fcode;
8949 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8950 return targetm.fold_builtin (fndecl, arglist, ignore);
8952 fcode = DECL_FUNCTION_CODE (fndecl);
8955 case BUILT_IN_FPUTS:
8956 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8958 case BUILT_IN_FPUTS_UNLOCKED:
8959 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8961 case BUILT_IN_STRSTR:
8962 return fold_builtin_strstr (arglist, type);
8964 case BUILT_IN_STRCAT:
8965 return fold_builtin_strcat (arglist);
8967 case BUILT_IN_STRNCAT:
8968 return fold_builtin_strncat (arglist);
8970 case BUILT_IN_STRSPN:
8971 return fold_builtin_strspn (arglist);
8973 case BUILT_IN_STRCSPN:
8974 return fold_builtin_strcspn (arglist);
8976 case BUILT_IN_STRCHR:
8977 case BUILT_IN_INDEX:
8978 return fold_builtin_strchr (arglist, type);
8980 case BUILT_IN_STRRCHR:
8981 case BUILT_IN_RINDEX:
8982 return fold_builtin_strrchr (arglist, type);
8984 case BUILT_IN_STRCPY:
8985 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
8987 case BUILT_IN_STRNCPY:
8988 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8990 case BUILT_IN_STRCMP:
8991 return fold_builtin_strcmp (arglist);
8993 case BUILT_IN_STRNCMP:
8994 return fold_builtin_strncmp (arglist);
8996 case BUILT_IN_STRPBRK:
8997 return fold_builtin_strpbrk (arglist, type);
9000 case BUILT_IN_MEMCMP:
9001 return fold_builtin_memcmp (arglist);
9003 case BUILT_IN_SPRINTF:
9004 return fold_builtin_sprintf (arglist, ignore);
9006 case BUILT_IN_CONSTANT_P:
9010 val = fold_builtin_constant_p (arglist);
9011 /* Gimplification will pull the CALL_EXPR for the builtin out of
9012 an if condition. When not optimizing, we'll not CSE it back.
9013 To avoid link error types of regressions, return false now. */
9014 if (!val && !optimize)
9015 val = integer_zero_node;
9020 case BUILT_IN_EXPECT:
9021 return fold_builtin_expect (arglist);
9023 case BUILT_IN_CLASSIFY_TYPE:
9024 return fold_builtin_classify_type (arglist);
9026 case BUILT_IN_STRLEN:
9027 return fold_builtin_strlen (arglist);
9029 CASE_FLT_FN (BUILT_IN_FABS):
9030 return fold_builtin_fabs (arglist, type);
9034 case BUILT_IN_LLABS:
9035 case BUILT_IN_IMAXABS:
9036 return fold_builtin_abs (arglist, type);
9038 CASE_FLT_FN (BUILT_IN_CONJ):
9039 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9040 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9043 CASE_FLT_FN (BUILT_IN_CREAL):
9044 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9045 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9046 TREE_VALUE (arglist)));
9049 CASE_FLT_FN (BUILT_IN_CIMAG):
9050 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9051 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9052 TREE_VALUE (arglist)));
9055 CASE_FLT_FN (BUILT_IN_CABS):
9056 return fold_builtin_cabs (arglist, type, fndecl);
9058 CASE_FLT_FN (BUILT_IN_SQRT):
9059 return fold_builtin_sqrt (arglist, type);
9061 CASE_FLT_FN (BUILT_IN_CBRT):
9062 return fold_builtin_cbrt (arglist, type);
9064 CASE_FLT_FN (BUILT_IN_SIN):
9065 return fold_builtin_sin (arglist);
9067 CASE_FLT_FN (BUILT_IN_COS):
9068 return fold_builtin_cos (arglist, type, fndecl);
9070 CASE_FLT_FN (BUILT_IN_EXP):
9071 return fold_builtin_exponent (fndecl, arglist, &dconste);
9073 CASE_FLT_FN (BUILT_IN_EXP2):
9074 return fold_builtin_exponent (fndecl, arglist, &dconst2);
9076 CASE_FLT_FN (BUILT_IN_EXP10):
9077 CASE_FLT_FN (BUILT_IN_POW10):
9078 return fold_builtin_exponent (fndecl, arglist, &dconst10);
9080 CASE_FLT_FN (BUILT_IN_LOG):
9081 return fold_builtin_logarithm (fndecl, arglist, &dconste);
9083 CASE_FLT_FN (BUILT_IN_LOG2):
9084 return fold_builtin_logarithm (fndecl, arglist, &dconst2);
9086 CASE_FLT_FN (BUILT_IN_LOG10):
9087 return fold_builtin_logarithm (fndecl, arglist, &dconst10);
9089 CASE_FLT_FN (BUILT_IN_TAN):
9090 return fold_builtin_tan (arglist);
9092 CASE_FLT_FN (BUILT_IN_ATAN):
9093 return fold_builtin_atan (arglist, type);
9095 CASE_FLT_FN (BUILT_IN_POW):
9096 return fold_builtin_pow (fndecl, arglist, type);
9098 CASE_FLT_FN (BUILT_IN_POWI):
9099 return fold_builtin_powi (fndecl, arglist, type);
9101 CASE_FLT_FN (BUILT_IN_INF):
9102 case BUILT_IN_INFD32:
9103 case BUILT_IN_INFD64:
9104 case BUILT_IN_INFD128:
9105 return fold_builtin_inf (type, true);
9107 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9108 return fold_builtin_inf (type, false);
9110 CASE_FLT_FN (BUILT_IN_NAN):
9111 case BUILT_IN_NAND32:
9112 case BUILT_IN_NAND64:
9113 case BUILT_IN_NAND128:
9114 return fold_builtin_nan (arglist, type, true);
9116 CASE_FLT_FN (BUILT_IN_NANS):
9117 return fold_builtin_nan (arglist, type, false);
9119 CASE_FLT_FN (BUILT_IN_FLOOR):
9120 return fold_builtin_floor (fndecl, arglist);
9122 CASE_FLT_FN (BUILT_IN_CEIL):
9123 return fold_builtin_ceil (fndecl, arglist);
9125 CASE_FLT_FN (BUILT_IN_TRUNC):
9126 return fold_builtin_trunc (fndecl, arglist);
9128 CASE_FLT_FN (BUILT_IN_ROUND):
9129 return fold_builtin_round (fndecl, arglist);
9131 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9132 CASE_FLT_FN (BUILT_IN_RINT):
9133 return fold_trunc_transparent_mathfn (fndecl, arglist);
9135 CASE_FLT_FN (BUILT_IN_LCEIL):
9136 CASE_FLT_FN (BUILT_IN_LLCEIL):
9137 CASE_FLT_FN (BUILT_IN_LFLOOR):
9138 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9139 CASE_FLT_FN (BUILT_IN_LROUND):
9140 CASE_FLT_FN (BUILT_IN_LLROUND):
9141 return fold_builtin_int_roundingfn (fndecl, arglist);
9143 CASE_FLT_FN (BUILT_IN_LRINT):
9144 CASE_FLT_FN (BUILT_IN_LLRINT):
9145 return fold_fixed_mathfn (fndecl, arglist);
9147 case BUILT_IN_BSWAP32:
9148 case BUILT_IN_BSWAP64:
9149 return fold_builtin_bswap (fndecl, arglist);
9151 CASE_INT_FN (BUILT_IN_FFS):
9152 CASE_INT_FN (BUILT_IN_CLZ):
9153 CASE_INT_FN (BUILT_IN_CTZ):
9154 CASE_INT_FN (BUILT_IN_POPCOUNT):
9155 CASE_INT_FN (BUILT_IN_PARITY):
9156 return fold_builtin_bitop (fndecl, arglist);
9158 case BUILT_IN_MEMSET:
9159 return fold_builtin_memset (arglist, type, ignore);
9161 case BUILT_IN_MEMCPY:
9162 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9164 case BUILT_IN_MEMPCPY:
9165 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9167 case BUILT_IN_MEMMOVE:
9168 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9170 case BUILT_IN_BZERO:
9171 return fold_builtin_bzero (arglist, ignore);
9173 case BUILT_IN_BCOPY:
9174 return fold_builtin_bcopy (arglist, ignore);
9176 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9177 return fold_builtin_signbit (fndecl, arglist);
9179 case BUILT_IN_ISASCII:
9180 return fold_builtin_isascii (arglist);
9182 case BUILT_IN_TOASCII:
9183 return fold_builtin_toascii (arglist);
9185 case BUILT_IN_ISDIGIT:
9186 return fold_builtin_isdigit (arglist);
9188 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9189 return fold_builtin_copysign (fndecl, arglist, type);
9191 CASE_FLT_FN (BUILT_IN_FINITE):
9192 case BUILT_IN_FINITED32:
9193 case BUILT_IN_FINITED64:
9194 case BUILT_IN_FINITED128:
9195 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9197 CASE_FLT_FN (BUILT_IN_ISINF):
9198 case BUILT_IN_ISINFD32:
9199 case BUILT_IN_ISINFD64:
9200 case BUILT_IN_ISINFD128:
9201 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9203 CASE_FLT_FN (BUILT_IN_ISNAN):
9204 case BUILT_IN_ISNAND32:
9205 case BUILT_IN_ISNAND64:
9206 case BUILT_IN_ISNAND128:
9207 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9209 case BUILT_IN_ISGREATER:
9210 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9211 case BUILT_IN_ISGREATEREQUAL:
9212 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9213 case BUILT_IN_ISLESS:
9214 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9215 case BUILT_IN_ISLESSEQUAL:
9216 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9217 case BUILT_IN_ISLESSGREATER:
9218 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9219 case BUILT_IN_ISUNORDERED:
9220 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9223 /* We do the folding for va_start in the expander. */
9224 case BUILT_IN_VA_START:
9227 case BUILT_IN_OBJECT_SIZE:
9228 return fold_builtin_object_size (arglist);
9229 case BUILT_IN_MEMCPY_CHK:
9230 case BUILT_IN_MEMPCPY_CHK:
9231 case BUILT_IN_MEMMOVE_CHK:
9232 case BUILT_IN_MEMSET_CHK:
9233 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9234 DECL_FUNCTION_CODE (fndecl));
9235 case BUILT_IN_STRCPY_CHK:
9236 case BUILT_IN_STPCPY_CHK:
9237 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9238 DECL_FUNCTION_CODE (fndecl));
9239 case BUILT_IN_STRNCPY_CHK:
9240 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9241 case BUILT_IN_STRCAT_CHK:
9242 return fold_builtin_strcat_chk (fndecl, arglist);
9243 case BUILT_IN_STRNCAT_CHK:
9244 return fold_builtin_strncat_chk (fndecl, arglist);
9245 case BUILT_IN_SPRINTF_CHK:
9246 case BUILT_IN_VSPRINTF_CHK:
9247 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9248 case BUILT_IN_SNPRINTF_CHK:
9249 case BUILT_IN_VSNPRINTF_CHK:
9250 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9251 DECL_FUNCTION_CODE (fndecl));
9253 case BUILT_IN_PRINTF:
9254 case BUILT_IN_PRINTF_UNLOCKED:
9255 case BUILT_IN_VPRINTF:
9256 case BUILT_IN_PRINTF_CHK:
9257 case BUILT_IN_VPRINTF_CHK:
9258 return fold_builtin_printf (fndecl, arglist, ignore,
9259 DECL_FUNCTION_CODE (fndecl));
9261 case BUILT_IN_FPRINTF:
9262 case BUILT_IN_FPRINTF_UNLOCKED:
9263 case BUILT_IN_VFPRINTF:
9264 case BUILT_IN_FPRINTF_CHK:
9265 case BUILT_IN_VFPRINTF_CHK:
9266 return fold_builtin_fprintf (fndecl, arglist, ignore,
9267 DECL_FUNCTION_CODE (fndecl));
9276 /* A wrapper function for builtin folding that prevents warnings for
9277 "statement without effect" and the like, caused by removing the
9278 call node earlier than the warning is generated. */
9281 fold_builtin (tree fndecl, tree arglist, bool ignore)
9283 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9286 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
9287 TREE_NO_WARNING (exp) = 1;
9293 /* Conveniently construct a function call expression. */
9296 build_function_call_expr (tree fn, tree arglist)
9300 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9301 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9302 call_expr, arglist, NULL_TREE);
9305 /* This function validates the types of a function call argument list
9306 represented as a tree chain of parameters against a specified list
9307 of tree_codes. If the last specifier is a 0, that represents an
9308 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9311 validate_arglist (tree arglist, ...)
9313 enum tree_code code;
9317 va_start (ap, arglist);
9321 code = va_arg (ap, enum tree_code);
9325 /* This signifies an ellipses, any further arguments are all ok. */
9329 /* This signifies an endlink, if no arguments remain, return
9330 true, otherwise return false. */
9334 /* If no parameters remain or the parameter's code does not
9335 match the specified code, return false. Otherwise continue
9336 checking any remaining arguments. */
9339 if (code == POINTER_TYPE)
9341 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9344 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9348 arglist = TREE_CHAIN (arglist);
9352 /* We need gotos here since we can only have one VA_CLOSE in a
9360 /* Default target-specific builtin expander that does nothing. */
9363 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9364 rtx target ATTRIBUTE_UNUSED,
9365 rtx subtarget ATTRIBUTE_UNUSED,
9366 enum machine_mode mode ATTRIBUTE_UNUSED,
9367 int ignore ATTRIBUTE_UNUSED)
9372 /* Returns true is EXP represents data that would potentially reside
9373 in a readonly section. */
9376 readonly_data_expr (tree exp)
9380 if (TREE_CODE (exp) != ADDR_EXPR)
9383 exp = get_base_address (TREE_OPERAND (exp, 0));
9387 /* Make sure we call decl_readonly_section only for trees it
9388 can handle (since it returns true for everything it doesn't
9390 if (TREE_CODE (exp) == STRING_CST
9391 || TREE_CODE (exp) == CONSTRUCTOR
9392 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9393 return decl_readonly_section (exp, 0);
9398 /* Simplify a call to the strstr builtin.
9400 Return 0 if no simplification was possible, otherwise return the
9401 simplified form of the call as a tree.
9403 The simplified form may be a constant or other expression which
9404 computes the same value, but in a more efficient manner (including
9405 calls to other builtin functions).
9407 The call may contain arguments which need to be evaluated, but
9408 which are not useful to determine the result of the call. In
9409 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9410 COMPOUND_EXPR will be an argument which must be evaluated.
9411 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9412 COMPOUND_EXPR in the chain will contain the tree for the simplified
9413 form of the builtin function call. */
9416 fold_builtin_strstr (tree arglist, tree type)
9418 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9422 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9424 const char *p1, *p2;
9433 const char *r = strstr (p1, p2);
9437 return build_int_cst (TREE_TYPE (s1), 0);
9439 /* Return an offset into the constant string argument. */
9440 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9441 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9442 return fold_convert (type, tem);
9445 /* The argument is const char *, and the result is char *, so we need
9446 a type conversion here to avoid a warning. */
9448 return fold_convert (type, s1);
9453 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9457 /* New argument list transforming strstr(s1, s2) to
9458 strchr(s1, s2[0]). */
9459 arglist = build_tree_list (NULL_TREE,
9460 build_int_cst (NULL_TREE, p2[0]));
9461 arglist = tree_cons (NULL_TREE, s1, arglist);
9462 return build_function_call_expr (fn, arglist);
9466 /* Simplify a call to the strchr builtin.
9468 Return 0 if no simplification was possible, otherwise return the
9469 simplified form of the call as a tree.
9471 The simplified form may be a constant or other expression which
9472 computes the same value, but in a more efficient manner (including
9473 calls to other builtin functions).
9475 The call may contain arguments which need to be evaluated, but
9476 which are not useful to determine the result of the call. In
9477 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9478 COMPOUND_EXPR will be an argument which must be evaluated.
9479 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9480 COMPOUND_EXPR in the chain will contain the tree for the simplified
9481 form of the builtin function call. */
9484 fold_builtin_strchr (tree arglist, tree type)
9486 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9490 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9493 if (TREE_CODE (s2) != INTEGER_CST)
9503 if (target_char_cast (s2, &c))
9509 return build_int_cst (TREE_TYPE (s1), 0);
9511 /* Return an offset into the constant string argument. */
9512 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9513 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9514 return fold_convert (type, tem);
9520 /* Simplify a call to the strrchr builtin.
9522 Return 0 if no simplification was possible, otherwise return the
9523 simplified form of the call as a tree.
9525 The simplified form may be a constant or other expression which
9526 computes the same value, but in a more efficient manner (including
9527 calls to other builtin functions).
9529 The call may contain arguments which need to be evaluated, but
9530 which are not useful to determine the result of the call. In
9531 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9532 COMPOUND_EXPR will be an argument which must be evaluated.
9533 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9534 COMPOUND_EXPR in the chain will contain the tree for the simplified
9535 form of the builtin function call. */
9538 fold_builtin_strrchr (tree arglist, tree type)
9540 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9544 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9548 if (TREE_CODE (s2) != INTEGER_CST)
9558 if (target_char_cast (s2, &c))
9561 r = strrchr (p1, c);
9564 return build_int_cst (TREE_TYPE (s1), 0);
9566 /* Return an offset into the constant string argument. */
9567 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9568 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9569 return fold_convert (type, tem);
9572 if (! integer_zerop (s2))
9575 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9579 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9580 return build_function_call_expr (fn, arglist);
9584 /* Simplify a call to the strpbrk builtin.
9586 Return 0 if no simplification was possible, otherwise return the
9587 simplified form of the call as a tree.
9589 The simplified form may be a constant or other expression which
9590 computes the same value, but in a more efficient manner (including
9591 calls to other builtin functions).
9593 The call may contain arguments which need to be evaluated, but
9594 which are not useful to determine the result of the call. In
9595 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9596 COMPOUND_EXPR will be an argument which must be evaluated.
9597 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9598 COMPOUND_EXPR in the chain will contain the tree for the simplified
9599 form of the builtin function call. */
9602 fold_builtin_strpbrk (tree arglist, tree type)
9604 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9608 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9610 const char *p1, *p2;
9619 const char *r = strpbrk (p1, p2);
9623 return build_int_cst (TREE_TYPE (s1), 0);
9625 /* Return an offset into the constant string argument. */
9626 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9627 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9628 return fold_convert (type, tem);
9632 /* strpbrk(x, "") == NULL.
9633 Evaluate and ignore s1 in case it had side-effects. */
9634 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9637 return 0; /* Really call strpbrk. */
9639 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9643 /* New argument list transforming strpbrk(s1, s2) to
9644 strchr(s1, s2[0]). */
9645 arglist = build_tree_list (NULL_TREE,
9646 build_int_cst (NULL_TREE, p2[0]));
9647 arglist = tree_cons (NULL_TREE, s1, arglist);
9648 return build_function_call_expr (fn, arglist);
9652 /* Simplify a call to the strcat builtin.
9654 Return 0 if no simplification was possible, otherwise return the
9655 simplified form of the call as a tree.
9657 The simplified form may be a constant or other expression which
9658 computes the same value, but in a more efficient manner (including
9659 calls to other builtin functions).
9661 The call may contain arguments which need to be evaluated, but
9662 which are not useful to determine the result of the call. In
9663 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9664 COMPOUND_EXPR will be an argument which must be evaluated.
9665 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9666 COMPOUND_EXPR in the chain will contain the tree for the simplified
9667 form of the builtin function call. */
9670 fold_builtin_strcat (tree arglist)
9672 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9676 tree dst = TREE_VALUE (arglist),
9677 src = TREE_VALUE (TREE_CHAIN (arglist));
9678 const char *p = c_getstr (src);
9680 /* If the string length is zero, return the dst parameter. */
9681 if (p && *p == '\0')
9688 /* Simplify a call to the strncat builtin.
9690 Return 0 if no simplification was possible, otherwise return the
9691 simplified form of the call as a tree.
9693 The simplified form may be a constant or other expression which
9694 computes the same value, but in a more efficient manner (including
9695 calls to other builtin functions).
9697 The call may contain arguments which need to be evaluated, but
9698 which are not useful to determine the result of the call. In
9699 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9700 COMPOUND_EXPR will be an argument which must be evaluated.
9701 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9702 COMPOUND_EXPR in the chain will contain the tree for the simplified
9703 form of the builtin function call. */
9706 fold_builtin_strncat (tree arglist)
9708 if (!validate_arglist (arglist,
9709 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9713 tree dst = TREE_VALUE (arglist);
9714 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9715 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9716 const char *p = c_getstr (src);
9718 /* If the requested length is zero, or the src parameter string
9719 length is zero, return the dst parameter. */
9720 if (integer_zerop (len) || (p && *p == '\0'))
9721 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9723 /* If the requested len is greater than or equal to the string
9724 length, call strcat. */
9725 if (TREE_CODE (len) == INTEGER_CST && p
9726 && compare_tree_int (len, strlen (p)) >= 0)
9729 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9730 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9732 /* If the replacement _DECL isn't initialized, don't do the
9737 return build_function_call_expr (fn, newarglist);
9743 /* Simplify a call to the strspn builtin.
9745 Return 0 if no simplification was possible, otherwise return the
9746 simplified form of the call as a tree.
9748 The simplified form may be a constant or other expression which
9749 computes the same value, but in a more efficient manner (including
9750 calls to other builtin functions).
9752 The call may contain arguments which need to be evaluated, but
9753 which are not useful to determine the result of the call. In
9754 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9755 COMPOUND_EXPR will be an argument which must be evaluated.
9756 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9757 COMPOUND_EXPR in the chain will contain the tree for the simplified
9758 form of the builtin function call. */
9761 fold_builtin_strspn (tree arglist)
9763 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9767 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9768 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9770 /* If both arguments are constants, evaluate at compile-time. */
9773 const size_t r = strspn (p1, p2);
9774 return size_int (r);
9777 /* If either argument is "", return 0. */
9778 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9779 /* Evaluate and ignore both arguments in case either one has
9781 return omit_two_operands (integer_type_node, integer_zero_node,
9787 /* Simplify a call to the strcspn builtin.
9789 Return 0 if no simplification was possible, otherwise return the
9790 simplified form of the call as a tree.
9792 The simplified form may be a constant or other expression which
9793 computes the same value, but in a more efficient manner (including
9794 calls to other builtin functions).
9796 The call may contain arguments which need to be evaluated, but
9797 which are not useful to determine the result of the call. In
9798 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9799 COMPOUND_EXPR will be an argument which must be evaluated.
9800 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9801 COMPOUND_EXPR in the chain will contain the tree for the simplified
9802 form of the builtin function call. */
9805 fold_builtin_strcspn (tree arglist)
9807 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9811 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9812 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9814 /* If both arguments are constants, evaluate at compile-time. */
9817 const size_t r = strcspn (p1, p2);
9818 return size_int (r);
9821 /* If the first argument is "", return 0. */
9822 if (p1 && *p1 == '\0')
9824 /* Evaluate and ignore argument s2 in case it has
9826 return omit_one_operand (integer_type_node,
9827 integer_zero_node, s2);
9830 /* If the second argument is "", return __builtin_strlen(s1). */
9831 if (p2 && *p2 == '\0')
9833 tree newarglist = build_tree_list (NULL_TREE, s1),
9834 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
9836 /* If the replacement _DECL isn't initialized, don't do the
9841 return build_function_call_expr (fn, newarglist);
9847 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
9848 by the builtin will be ignored. UNLOCKED is true is true if this
9849 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9850 the known length of the string. Return NULL_TREE if no simplification
9854 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
9857 /* If we're using an unlocked function, assume the other unlocked
9858 functions exist explicitly. */
9859 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9860 : implicit_built_in_decls[BUILT_IN_FPUTC];
9861 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9862 : implicit_built_in_decls[BUILT_IN_FWRITE];
9864 /* If the return value is used, don't do the transformation. */
9868 /* Verify the arguments in the original call. */
9869 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9873 len = c_strlen (TREE_VALUE (arglist), 0);
9875 /* Get the length of the string passed to fputs. If the length
9876 can't be determined, punt. */
9878 || TREE_CODE (len) != INTEGER_CST)
9881 switch (compare_tree_int (len, 1))
9883 case -1: /* length is 0, delete the call entirely . */
9884 return omit_one_operand (integer_type_node, integer_zero_node,
9885 TREE_VALUE (TREE_CHAIN (arglist)));
9887 case 0: /* length is 1, call fputc. */
9889 const char *p = c_getstr (TREE_VALUE (arglist));
9893 /* New argument list transforming fputs(string, stream) to
9894 fputc(string[0], stream). */
9895 arglist = build_tree_list (NULL_TREE,
9896 TREE_VALUE (TREE_CHAIN (arglist)));
9897 arglist = tree_cons (NULL_TREE,
9898 build_int_cst (NULL_TREE, p[0]),
9905 case 1: /* length is greater than 1, call fwrite. */
9909 /* If optimizing for size keep fputs. */
9912 string_arg = TREE_VALUE (arglist);
9913 /* New argument list transforming fputs(string, stream) to
9914 fwrite(string, 1, len, stream). */
9915 arglist = build_tree_list (NULL_TREE,
9916 TREE_VALUE (TREE_CHAIN (arglist)));
9917 arglist = tree_cons (NULL_TREE, len, arglist);
9918 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9919 arglist = tree_cons (NULL_TREE, string_arg, arglist);
9927 /* If the replacement _DECL isn't initialized, don't do the
9932 /* These optimizations are only performed when the result is ignored,
9933 hence there's no need to cast the result to integer_type_node. */
9934 return build_function_call_expr (fn, arglist);
9937 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9938 produced. False otherwise. This is done so that we don't output the error
9939 or warning twice or three times. */
9941 fold_builtin_next_arg (tree arglist)
9943 tree fntype = TREE_TYPE (current_function_decl);
9945 if (TYPE_ARG_TYPES (fntype) == 0
9946 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9949 error ("%<va_start%> used in function with fixed args");
9954 /* Evidently an out of date version of <stdarg.h>; can't validate
9955 va_start's second argument, but can still work as intended. */
9956 warning (0, "%<__builtin_next_arg%> called without an argument");
9959 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9960 when we checked the arguments and if needed issued a warning. */
9961 else if (!TREE_CHAIN (arglist)
9962 || !integer_zerop (TREE_VALUE (arglist))
9963 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
9964 || TREE_CHAIN (TREE_CHAIN (arglist)))
9966 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9967 tree arg = TREE_VALUE (arglist);
9969 if (TREE_CHAIN (arglist))
9971 error ("%<va_start%> used with too many arguments");
9975 /* Strip off all nops for the sake of the comparison. This
9976 is not quite the same as STRIP_NOPS. It does more.
9977 We must also strip off INDIRECT_EXPR for C++ reference
9979 while (TREE_CODE (arg) == NOP_EXPR
9980 || TREE_CODE (arg) == CONVERT_EXPR
9981 || TREE_CODE (arg) == NON_LVALUE_EXPR
9982 || TREE_CODE (arg) == INDIRECT_REF)
9983 arg = TREE_OPERAND (arg, 0);
9984 if (arg != last_parm)
9986 /* FIXME: Sometimes with the tree optimizers we can get the
9987 not the last argument even though the user used the last
9988 argument. We just warn and set the arg to be the last
9989 argument so that we will get wrong-code because of
9991 warning (0, "second parameter of %<va_start%> not last named argument");
9993 /* We want to verify the second parameter just once before the tree
9994 optimizers are run and then avoid keeping it in the tree,
9995 as otherwise we could warn even for correct code like:
9996 void foo (int i, ...)
9997 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9998 TREE_VALUE (arglist) = integer_zero_node;
9999 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10005 /* Simplify a call to the sprintf builtin.
10007 Return 0 if no simplification was possible, otherwise return the
10008 simplified form of the call as a tree. If IGNORED is true, it means that
10009 the caller does not use the returned value of the function. */
10012 fold_builtin_sprintf (tree arglist, int ignored)
10014 tree call, retval, dest, fmt;
10015 const char *fmt_str = NULL;
10017 /* Verify the required arguments in the original call. We deal with two
10018 types of sprintf() calls: 'sprintf (str, fmt)' and
10019 'sprintf (dest, "%s", orig)'. */
10020 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10021 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10025 /* Get the destination string and the format specifier. */
10026 dest = TREE_VALUE (arglist);
10027 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10028 arglist = TREE_CHAIN (TREE_CHAIN (arglist));
10030 /* Check whether the format is a literal string constant. */
10031 fmt_str = c_getstr (fmt);
10032 if (fmt_str == NULL)
10036 retval = NULL_TREE;
10038 if (!init_target_chars())
10041 /* If the format doesn't contain % args or %%, use strcpy. */
10042 if (strchr (fmt_str, target_percent) == NULL)
10044 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10049 /* Don't optimize sprintf (buf, "abc", ptr++). */
10053 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10054 'format' is known to contain no % formats. */
10055 arglist = build_tree_list (NULL_TREE, fmt);
10056 arglist = tree_cons (NULL_TREE, dest, arglist);
10057 call = build_function_call_expr (fn, arglist);
10059 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10062 /* If the format is "%s", use strcpy if the result isn't used. */
10063 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10066 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10071 /* Don't crash on sprintf (str1, "%s"). */
10075 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10076 orig = TREE_VALUE (arglist);
10077 arglist = build_tree_list (NULL_TREE, orig);
10078 arglist = tree_cons (NULL_TREE, dest, arglist);
10081 retval = c_strlen (orig, 1);
10082 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10085 call = build_function_call_expr (fn, arglist);
10088 if (call && retval)
10090 retval = fold_convert
10091 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10093 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10099 /* Expand a call to __builtin_object_size. */
10102 expand_builtin_object_size (tree exp)
10105 int object_size_type;
10106 tree fndecl = get_callee_fndecl (exp);
10107 tree arglist = TREE_OPERAND (exp, 1);
10108 location_t locus = EXPR_LOCATION (exp);
10110 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10112 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10114 expand_builtin_trap ();
10118 ost = TREE_VALUE (TREE_CHAIN (arglist));
10121 if (TREE_CODE (ost) != INTEGER_CST
10122 || tree_int_cst_sgn (ost) < 0
10123 || compare_tree_int (ost, 3) > 0)
10125 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10127 expand_builtin_trap ();
10131 object_size_type = tree_low_cst (ost, 0);
10133 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10136 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10137 FCODE is the BUILT_IN_* to use.
10138 Return 0 if we failed; the caller should emit a normal call,
10139 otherwise try to get the result in TARGET, if convenient (and in
10140 mode MODE if that's convenient). */
10143 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10144 enum built_in_function fcode)
10146 tree arglist = TREE_OPERAND (exp, 1);
10147 tree dest, src, len, size;
10149 if (!validate_arglist (arglist,
10151 fcode == BUILT_IN_MEMSET_CHK
10152 ? INTEGER_TYPE : POINTER_TYPE,
10153 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10156 dest = TREE_VALUE (arglist);
10157 src = TREE_VALUE (TREE_CHAIN (arglist));
10158 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10159 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10161 if (! host_integerp (size, 1))
10164 if (host_integerp (len, 1) || integer_all_onesp (size))
10168 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10170 location_t locus = EXPR_LOCATION (exp);
10171 warning (0, "%Hcall to %D will always overflow destination buffer",
10172 &locus, get_callee_fndecl (exp));
10176 arglist = build_tree_list (NULL_TREE, len);
10177 arglist = tree_cons (NULL_TREE, src, arglist);
10178 arglist = tree_cons (NULL_TREE, dest, arglist);
10181 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10182 mem{cpy,pcpy,move,set} is available. */
10185 case BUILT_IN_MEMCPY_CHK:
10186 fn = built_in_decls[BUILT_IN_MEMCPY];
10188 case BUILT_IN_MEMPCPY_CHK:
10189 fn = built_in_decls[BUILT_IN_MEMPCPY];
10191 case BUILT_IN_MEMMOVE_CHK:
10192 fn = built_in_decls[BUILT_IN_MEMMOVE];
10194 case BUILT_IN_MEMSET_CHK:
10195 fn = built_in_decls[BUILT_IN_MEMSET];
10204 fn = build_function_call_expr (fn, arglist);
10205 if (TREE_CODE (fn) == CALL_EXPR)
10206 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10207 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10209 else if (fcode == BUILT_IN_MEMSET_CHK)
10213 unsigned int dest_align
10214 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10216 /* If DEST is not a pointer type, call the normal function. */
10217 if (dest_align == 0)
10220 /* If SRC and DEST are the same (and not volatile), do nothing. */
10221 if (operand_equal_p (src, dest, 0))
10225 if (fcode != BUILT_IN_MEMPCPY_CHK)
10227 /* Evaluate and ignore LEN in case it has side-effects. */
10228 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10229 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10232 len = fold_convert (TREE_TYPE (dest), len);
10233 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10234 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10237 /* __memmove_chk special case. */
10238 if (fcode == BUILT_IN_MEMMOVE_CHK)
10240 unsigned int src_align
10241 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10243 if (src_align == 0)
10246 /* If src is categorized for a readonly section we can use
10247 normal __memcpy_chk. */
10248 if (readonly_data_expr (src))
10250 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10253 fn = build_function_call_expr (fn, arglist);
10254 if (TREE_CODE (fn) == CALL_EXPR)
10255 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10256 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10263 /* Emit warning if a buffer overflow is detected at compile time. */
10266 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10268 int arg_mask, is_strlen = 0;
10269 tree arglist = TREE_OPERAND (exp, 1), a;
10275 case BUILT_IN_STRCPY_CHK:
10276 case BUILT_IN_STPCPY_CHK:
10277 /* For __strcat_chk the warning will be emitted only if overflowing
10278 by at least strlen (dest) + 1 bytes. */
10279 case BUILT_IN_STRCAT_CHK:
10283 case BUILT_IN_STRNCPY_CHK:
10286 case BUILT_IN_SNPRINTF_CHK:
10287 case BUILT_IN_VSNPRINTF_CHK:
10291 gcc_unreachable ();
10296 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10308 len = TREE_VALUE (len);
10309 size = TREE_VALUE (size);
10311 if (! host_integerp (size, 1) || integer_all_onesp (size))
10316 len = c_strlen (len, 1);
10317 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10320 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10323 locus = EXPR_LOCATION (exp);
10324 warning (0, "%Hcall to %D will always overflow destination buffer",
10325 &locus, get_callee_fndecl (exp));
10328 /* Emit warning if a buffer overflow is detected at compile time
10329 in __sprintf_chk/__vsprintf_chk calls. */
10332 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10334 tree arglist = TREE_OPERAND (exp, 1);
10335 tree dest, size, len, fmt, flag;
10336 const char *fmt_str;
10338 /* Verify the required arguments in the original call. */
10341 dest = TREE_VALUE (arglist);
10342 arglist = TREE_CHAIN (arglist);
10345 flag = TREE_VALUE (arglist);
10346 arglist = TREE_CHAIN (arglist);
10349 size = TREE_VALUE (arglist);
10350 arglist = TREE_CHAIN (arglist);
10353 fmt = TREE_VALUE (arglist);
10354 arglist = TREE_CHAIN (arglist);
10356 if (! host_integerp (size, 1) || integer_all_onesp (size))
10359 /* Check whether the format is a literal string constant. */
10360 fmt_str = c_getstr (fmt);
10361 if (fmt_str == NULL)
10364 if (!init_target_chars())
10367 /* If the format doesn't contain % args or %%, we know its size. */
10368 if (strchr (fmt_str, target_percent) == 0)
10369 len = build_int_cstu (size_type_node, strlen (fmt_str));
10370 /* If the format is "%s" and first ... argument is a string literal,
10372 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10378 arg = TREE_VALUE (arglist);
10379 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10382 len = c_strlen (arg, 1);
10383 if (!len || ! host_integerp (len, 1))
10389 if (! tree_int_cst_lt (len, size))
10391 location_t locus = EXPR_LOCATION (exp);
10392 warning (0, "%Hcall to %D will always overflow destination buffer",
10393 &locus, get_callee_fndecl (exp));
10397 /* Fold a call to __builtin_object_size, if possible. */
10400 fold_builtin_object_size (tree arglist)
10402 tree ptr, ost, ret = 0;
10403 int object_size_type;
10405 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10408 ptr = TREE_VALUE (arglist);
10409 ost = TREE_VALUE (TREE_CHAIN (arglist));
10412 if (TREE_CODE (ost) != INTEGER_CST
10413 || tree_int_cst_sgn (ost) < 0
10414 || compare_tree_int (ost, 3) > 0)
10417 object_size_type = tree_low_cst (ost, 0);
10419 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10420 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10421 and (size_t) 0 for types 2 and 3. */
10422 if (TREE_SIDE_EFFECTS (ptr))
10423 return fold_convert (size_type_node,
10424 object_size_type < 2
10425 ? integer_minus_one_node : integer_zero_node);
10427 if (TREE_CODE (ptr) == ADDR_EXPR)
10428 ret = build_int_cstu (size_type_node,
10429 compute_builtin_object_size (ptr, object_size_type));
10431 else if (TREE_CODE (ptr) == SSA_NAME)
10433 unsigned HOST_WIDE_INT bytes;
10435 /* If object size is not known yet, delay folding until
10436 later. Maybe subsequent passes will help determining
10438 bytes = compute_builtin_object_size (ptr, object_size_type);
10439 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10441 ret = build_int_cstu (size_type_node, bytes);
10446 ret = force_fit_type (ret, -1, false, false);
10447 if (TREE_CONSTANT_OVERFLOW (ret))
10454 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10455 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10456 code of the builtin. If MAXLEN is not NULL, it is maximum length
10457 passed as third argument. */
10460 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10461 enum built_in_function fcode)
10463 tree dest, src, len, size, fn;
10465 if (!validate_arglist (arglist,
10467 fcode == BUILT_IN_MEMSET_CHK
10468 ? INTEGER_TYPE : POINTER_TYPE,
10469 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10472 dest = TREE_VALUE (arglist);
10473 /* Actually val for __memset_chk, but it doesn't matter. */
10474 src = TREE_VALUE (TREE_CHAIN (arglist));
10475 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10476 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10478 /* If SRC and DEST are the same (and not volatile), return DEST
10479 (resp. DEST+LEN for __mempcpy_chk). */
10480 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10482 if (fcode != BUILT_IN_MEMPCPY_CHK)
10483 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10486 tree temp = fold_convert (TREE_TYPE (dest), len);
10487 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10488 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10492 if (! host_integerp (size, 1))
10495 if (! integer_all_onesp (size))
10497 if (! host_integerp (len, 1))
10499 /* If LEN is not constant, try MAXLEN too.
10500 For MAXLEN only allow optimizing into non-_ocs function
10501 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10502 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10504 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10506 /* (void) __mempcpy_chk () can be optimized into
10507 (void) __memcpy_chk (). */
10508 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10512 return build_function_call_expr (fn, arglist);
10520 if (tree_int_cst_lt (size, maxlen))
10524 arglist = build_tree_list (NULL_TREE, len);
10525 arglist = tree_cons (NULL_TREE, src, arglist);
10526 arglist = tree_cons (NULL_TREE, dest, arglist);
10529 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10530 mem{cpy,pcpy,move,set} is available. */
10533 case BUILT_IN_MEMCPY_CHK:
10534 fn = built_in_decls[BUILT_IN_MEMCPY];
10536 case BUILT_IN_MEMPCPY_CHK:
10537 fn = built_in_decls[BUILT_IN_MEMPCPY];
10539 case BUILT_IN_MEMMOVE_CHK:
10540 fn = built_in_decls[BUILT_IN_MEMMOVE];
10542 case BUILT_IN_MEMSET_CHK:
10543 fn = built_in_decls[BUILT_IN_MEMSET];
10552 return build_function_call_expr (fn, arglist);
10555 /* Fold a call to the __st[rp]cpy_chk builtin.
10556 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10557 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10558 strings passed as second argument. */
10561 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10562 enum built_in_function fcode)
10564 tree dest, src, size, len, fn;
10566 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10570 dest = TREE_VALUE (arglist);
10571 src = TREE_VALUE (TREE_CHAIN (arglist));
10572 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10574 /* If SRC and DEST are the same (and not volatile), return DEST. */
10575 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10576 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10578 if (! host_integerp (size, 1))
10581 if (! integer_all_onesp (size))
10583 len = c_strlen (src, 1);
10584 if (! len || ! host_integerp (len, 1))
10586 /* If LEN is not constant, try MAXLEN too.
10587 For MAXLEN only allow optimizing into non-_ocs function
10588 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10589 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10591 if (fcode == BUILT_IN_STPCPY_CHK)
10596 /* If return value of __stpcpy_chk is ignored,
10597 optimize into __strcpy_chk. */
10598 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10602 return build_function_call_expr (fn, arglist);
10605 if (! len || TREE_SIDE_EFFECTS (len))
10608 /* If c_strlen returned something, but not a constant,
10609 transform __strcpy_chk into __memcpy_chk. */
10610 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10614 len = size_binop (PLUS_EXPR, len, ssize_int (1));
10615 arglist = build_tree_list (NULL_TREE, size);
10616 arglist = tree_cons (NULL_TREE, len, arglist);
10617 arglist = tree_cons (NULL_TREE, src, arglist);
10618 arglist = tree_cons (NULL_TREE, dest, arglist);
10619 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10620 build_function_call_expr (fn, arglist));
10626 if (! tree_int_cst_lt (maxlen, size))
10630 arglist = build_tree_list (NULL_TREE, src);
10631 arglist = tree_cons (NULL_TREE, dest, arglist);
10633 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10634 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10635 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10639 return build_function_call_expr (fn, arglist);
10642 /* Fold a call to the __strncpy_chk builtin.
10643 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10646 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10648 tree dest, src, size, len, fn;
10650 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10651 INTEGER_TYPE, VOID_TYPE))
10654 dest = TREE_VALUE (arglist);
10655 src = TREE_VALUE (TREE_CHAIN (arglist));
10656 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10657 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10659 if (! host_integerp (size, 1))
10662 if (! integer_all_onesp (size))
10664 if (! host_integerp (len, 1))
10666 /* If LEN is not constant, try MAXLEN too.
10667 For MAXLEN only allow optimizing into non-_ocs function
10668 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10669 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10675 if (tree_int_cst_lt (size, maxlen))
10679 arglist = build_tree_list (NULL_TREE, len);
10680 arglist = tree_cons (NULL_TREE, src, arglist);
10681 arglist = tree_cons (NULL_TREE, dest, arglist);
10683 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10684 fn = built_in_decls[BUILT_IN_STRNCPY];
10688 return build_function_call_expr (fn, arglist);
10691 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10694 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10696 tree dest, src, size, fn;
10699 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10703 dest = TREE_VALUE (arglist);
10704 src = TREE_VALUE (TREE_CHAIN (arglist));
10705 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10707 p = c_getstr (src);
10708 /* If the SRC parameter is "", return DEST. */
10709 if (p && *p == '\0')
10710 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10712 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10715 arglist = build_tree_list (NULL_TREE, src);
10716 arglist = tree_cons (NULL_TREE, dest, arglist);
10718 /* If __builtin_strcat_chk is used, assume strcat is available. */
10719 fn = built_in_decls[BUILT_IN_STRCAT];
10723 return build_function_call_expr (fn, arglist);
10726 /* Fold a call to the __strncat_chk builtin EXP. */
10729 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10731 tree dest, src, size, len, fn;
10734 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10735 INTEGER_TYPE, VOID_TYPE))
10738 dest = TREE_VALUE (arglist);
10739 src = TREE_VALUE (TREE_CHAIN (arglist));
10740 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10741 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10743 p = c_getstr (src);
10744 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10745 if (p && *p == '\0')
10746 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10747 else if (integer_zerop (len))
10748 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10750 if (! host_integerp (size, 1))
10753 if (! integer_all_onesp (size))
10755 tree src_len = c_strlen (src, 1);
10757 && host_integerp (src_len, 1)
10758 && host_integerp (len, 1)
10759 && ! tree_int_cst_lt (len, src_len))
10761 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10762 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10766 arglist = build_tree_list (NULL_TREE, size);
10767 arglist = tree_cons (NULL_TREE, src, arglist);
10768 arglist = tree_cons (NULL_TREE, dest, arglist);
10769 return build_function_call_expr (fn, arglist);
10774 arglist = build_tree_list (NULL_TREE, len);
10775 arglist = tree_cons (NULL_TREE, src, arglist);
10776 arglist = tree_cons (NULL_TREE, dest, arglist);
10778 /* If __builtin_strncat_chk is used, assume strncat is available. */
10779 fn = built_in_decls[BUILT_IN_STRNCAT];
10783 return build_function_call_expr (fn, arglist);
10786 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10787 a normal call should be emitted rather than expanding the function
10788 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10791 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
10793 tree dest, size, len, fn, fmt, flag;
10794 const char *fmt_str;
10796 /* Verify the required arguments in the original call. */
10799 dest = TREE_VALUE (arglist);
10800 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10802 arglist = TREE_CHAIN (arglist);
10805 flag = TREE_VALUE (arglist);
10806 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
10808 arglist = TREE_CHAIN (arglist);
10811 size = TREE_VALUE (arglist);
10812 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10814 arglist = TREE_CHAIN (arglist);
10817 fmt = TREE_VALUE (arglist);
10818 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10820 arglist = TREE_CHAIN (arglist);
10822 if (! host_integerp (size, 1))
10827 if (!init_target_chars())
10830 /* Check whether the format is a literal string constant. */
10831 fmt_str = c_getstr (fmt);
10832 if (fmt_str != NULL)
10834 /* If the format doesn't contain % args or %%, we know the size. */
10835 if (strchr (fmt_str, target_percent) == 0)
10837 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
10838 len = build_int_cstu (size_type_node, strlen (fmt_str));
10840 /* If the format is "%s" and first ... argument is a string literal,
10841 we know the size too. */
10842 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10846 if (arglist && !TREE_CHAIN (arglist))
10848 arg = TREE_VALUE (arglist);
10849 if (POINTER_TYPE_P (TREE_TYPE (arg)))
10851 len = c_strlen (arg, 1);
10852 if (! len || ! host_integerp (len, 1))
10859 if (! integer_all_onesp (size))
10861 if (! len || ! tree_int_cst_lt (len, size))
10865 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
10866 or if format doesn't contain % chars or is "%s". */
10867 if (! integer_zerop (flag))
10869 if (fmt_str == NULL)
10871 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10875 arglist = tree_cons (NULL_TREE, fmt, arglist);
10876 arglist = tree_cons (NULL_TREE, dest, arglist);
10878 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
10879 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
10880 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
10884 return build_function_call_expr (fn, arglist);
10887 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
10888 a normal call should be emitted rather than expanding the function
10889 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
10890 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
10891 passed as second argument. */
10894 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
10895 enum built_in_function fcode)
10897 tree dest, size, len, fn, fmt, flag;
10898 const char *fmt_str;
10900 /* Verify the required arguments in the original call. */
10903 dest = TREE_VALUE (arglist);
10904 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10906 arglist = TREE_CHAIN (arglist);
10909 len = TREE_VALUE (arglist);
10910 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10912 arglist = TREE_CHAIN (arglist);
10915 flag = TREE_VALUE (arglist);
10916 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10918 arglist = TREE_CHAIN (arglist);
10921 size = TREE_VALUE (arglist);
10922 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10924 arglist = TREE_CHAIN (arglist);
10927 fmt = TREE_VALUE (arglist);
10928 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10930 arglist = TREE_CHAIN (arglist);
10932 if (! host_integerp (size, 1))
10935 if (! integer_all_onesp (size))
10937 if (! host_integerp (len, 1))
10939 /* If LEN is not constant, try MAXLEN too.
10940 For MAXLEN only allow optimizing into non-_ocs function
10941 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10942 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10948 if (tree_int_cst_lt (size, maxlen))
10952 if (!init_target_chars())
10955 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
10956 or if format doesn't contain % chars or is "%s". */
10957 if (! integer_zerop (flag))
10959 fmt_str = c_getstr (fmt);
10960 if (fmt_str == NULL)
10962 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10966 arglist = tree_cons (NULL_TREE, fmt, arglist);
10967 arglist = tree_cons (NULL_TREE, len, arglist);
10968 arglist = tree_cons (NULL_TREE, dest, arglist);
10970 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
10972 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
10973 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
10977 return build_function_call_expr (fn, arglist);
10980 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
10982 Return 0 if no simplification was possible, otherwise return the
10983 simplified form of the call as a tree. FCODE is the BUILT_IN_*
10984 code of the function to be simplified. */
10987 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
10988 enum built_in_function fcode)
10990 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
10991 const char *fmt_str = NULL;
10993 /* If the return value is used, don't do the transformation. */
10997 /* Verify the required arguments in the original call. */
10998 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11004 flag = TREE_VALUE (arglist);
11005 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11006 || TREE_SIDE_EFFECTS (flag))
11008 arglist = TREE_CHAIN (arglist);
11013 fmt = TREE_VALUE (arglist);
11014 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11016 arglist = TREE_CHAIN (arglist);
11018 /* Check whether the format is a literal string constant. */
11019 fmt_str = c_getstr (fmt);
11020 if (fmt_str == NULL)
11023 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11025 /* If we're using an unlocked function, assume the other
11026 unlocked functions exist explicitly. */
11027 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11028 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11032 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11033 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11036 if (!init_target_chars())
11039 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11043 if (strcmp (fmt_str, target_percent_s) == 0)
11045 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11049 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11050 || TREE_CHAIN (arglist))
11053 str = c_getstr (TREE_VALUE (arglist));
11059 /* The format specifier doesn't contain any '%' characters. */
11060 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11066 /* If the string was "", printf does nothing. */
11067 if (str[0] == '\0')
11068 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11070 /* If the string has length of 1, call putchar. */
11071 if (str[1] == '\0')
11073 /* Given printf("c"), (where c is any one character,)
11074 convert "c"[0] to an int and pass that to the replacement
11076 arg = build_int_cst (NULL_TREE, str[0]);
11077 arglist = build_tree_list (NULL_TREE, arg);
11082 /* If the string was "string\n", call puts("string"). */
11083 size_t len = strlen (str);
11084 if ((unsigned char)str[len - 1] == target_newline)
11086 /* Create a NUL-terminated string that's one char shorter
11087 than the original, stripping off the trailing '\n'. */
11088 char *newstr = alloca (len);
11089 memcpy (newstr, str, len - 1);
11090 newstr[len - 1] = 0;
11092 arg = build_string_literal (len, newstr);
11093 arglist = build_tree_list (NULL_TREE, arg);
11097 /* We'd like to arrange to call fputs(string,stdout) here,
11098 but we need stdout and don't have a way to get it yet. */
11103 /* The other optimizations can be done only on the non-va_list variants. */
11104 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11107 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11108 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11111 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11112 || TREE_CHAIN (arglist))
11117 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11118 else if (strcmp (fmt_str, target_percent_c) == 0)
11121 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11122 || TREE_CHAIN (arglist))
11130 call = build_function_call_expr (fn, arglist);
11131 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11134 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11136 Return 0 if no simplification was possible, otherwise return the
11137 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11138 code of the function to be simplified. */
11141 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11142 enum built_in_function fcode)
11144 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11145 const char *fmt_str = NULL;
11147 /* If the return value is used, don't do the transformation. */
11151 /* Verify the required arguments in the original call. */
11154 fp = TREE_VALUE (arglist);
11155 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11157 arglist = TREE_CHAIN (arglist);
11159 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11165 flag = TREE_VALUE (arglist);
11166 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11167 || TREE_SIDE_EFFECTS (flag))
11169 arglist = TREE_CHAIN (arglist);
11174 fmt = TREE_VALUE (arglist);
11175 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11177 arglist = TREE_CHAIN (arglist);
11179 /* Check whether the format is a literal string constant. */
11180 fmt_str = c_getstr (fmt);
11181 if (fmt_str == NULL)
11184 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11186 /* If we're using an unlocked function, assume the other
11187 unlocked functions exist explicitly. */
11188 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11189 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11193 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11194 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11197 if (!init_target_chars())
11200 /* If the format doesn't contain % args or %%, use strcpy. */
11201 if (strchr (fmt_str, target_percent) == NULL)
11203 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11207 /* If the format specifier was "", fprintf does nothing. */
11208 if (fmt_str[0] == '\0')
11210 /* If FP has side-effects, just wait until gimplification is
11212 if (TREE_SIDE_EFFECTS (fp))
11215 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11218 /* When "string" doesn't contain %, replace all cases of
11219 fprintf (fp, string) with fputs (string, fp). The fputs
11220 builtin will take care of special cases like length == 1. */
11221 arglist = build_tree_list (NULL_TREE, fp);
11222 arglist = tree_cons (NULL_TREE, fmt, arglist);
11226 /* The other optimizations can be done only on the non-va_list variants. */
11227 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11230 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11231 else if (strcmp (fmt_str, target_percent_s) == 0)
11234 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11235 || TREE_CHAIN (arglist))
11237 arg = TREE_VALUE (arglist);
11238 arglist = build_tree_list (NULL_TREE, fp);
11239 arglist = tree_cons (NULL_TREE, arg, arglist);
11243 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11244 else if (strcmp (fmt_str, target_percent_c) == 0)
11247 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11248 || TREE_CHAIN (arglist))
11250 arg = TREE_VALUE (arglist);
11251 arglist = build_tree_list (NULL_TREE, fp);
11252 arglist = tree_cons (NULL_TREE, arg, arglist);
11259 call = build_function_call_expr (fn, arglist);
11260 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11263 /* Initialize format string characters in the target charset. */
11266 init_target_chars (void)
11271 target_newline = lang_hooks.to_target_charset ('\n');
11272 target_percent = lang_hooks.to_target_charset ('%');
11273 target_c = lang_hooks.to_target_charset ('c');
11274 target_s = lang_hooks.to_target_charset ('s');
11275 if (target_newline == 0 || target_percent == 0 || target_c == 0
11279 target_percent_c[0] = target_percent;
11280 target_percent_c[1] = target_c;
11281 target_percent_c[2] = '\0';
11283 target_percent_s[0] = target_percent;
11284 target_percent_s[1] = target_s;
11285 target_percent_s[2] = '\0';
11287 target_percent_s_newline[0] = target_percent;
11288 target_percent_s_newline[1] = target_s;
11289 target_percent_s_newline[2] = target_newline;
11290 target_percent_s_newline[3] = '\0';